Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Solutions networkx

Technische Universität Berlin

Exercise 1

Task 1: Build the graph shown above in networkx using the functions to add graphs. The weight of each edge should correspond to the sum of the node labels it connects.

Notebook Cell
Notebook Cell
Notebook Cell
Notebook Cell

Task 2: Draw the finished graph with matplotlib.

Notebook Cell
<Figure size 640x480 with 1 Axes>

Exercise 2

Reconsider the example of the European transmission network graph. Use the following code in a fresh notebook to get started:

url = "https://tubcloud.tu-berlin.de/s/FmFrJkiWpg2QcQA/download/edges.csv"
edges = pd.read_csv(url, index_col=0)

G = nx.from_pandas_edgelist(edges, "bus0", "bus1", edge_attr=["x_pu", "s_nom"])

subgraphs = []
for c in nx.connected_components(G):
    subgraphs.append(G.subgraph(c).copy())

Choose one of the subgraphs (each representing a synchronous zone) and perform the following analyses:

Notebook Cell
Notebook Cell

Task 1: Determine the number of transmission lines and buses.

Notebook Cell
408
Notebook Cell
286

Task 2: Check whether the network is planar.

Notebook Cell
False

Task 3: Calculate the average number of transmission lines connecting to a bus.

Notebook Cell
np.float64(2.8531468531468533)

Task 4: Determine the number of cycles forming the cycle basis.

Notebook Cell
123

Task 5: Create a histogram of the length of the cycles (i.e. number of edges per cycle) in the cycle basis.

Notebook Cell
Notebook Cell
<Figure size 640x480 with 1 Axes>
Notebook Cell
<Figure size 640x480 with 1 Axes>

Task 6: Calculate the average length of the cycles in the cycle basis.

Notebook Cell
count 123.000000 mean 8.918699 std 8.933813 min 3.000000 25% 3.000000 50% 5.000000 75% 11.000000 max 58.000000 dtype: float64

Task 7: Obtain the directed adjacency matrix.

Notebook Cell
array([[0, 0, 0, ..., 0, 0, 0], [0, 0, 0, ..., 0, 0, 0], [0, 0, 0, ..., 0, 0, 0], ..., [0, 0, 0, ..., 0, 0, 0], [0, 0, 0, ..., 0, 0, 0], [0, 0, 0, ..., 0, 0, 0]], shape=(286, 286))

Task 8: Obtain the directed incidence matrix.

Notebook Cell
array([[1., 1., 0., ..., 0., 0., 0.], [0., 0., 1., ..., 0., 0., 0.], [0., 0., 0., ..., 0., 0., 0.], ..., [0., 0., 0., ..., 0., 0., 0.], [0., 0., 0., ..., 0., 0., 0.], [0., 0., 0., ..., 0., 0., 0.]], shape=(286, 408))