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.

Fundamentals

Import modules:

What is a Graph?

Definition: A graph GG consists of a collection VV of vertices and a collection of links EE, for which we denote G=(V,E)G = (V, E). Each link ee is said to connect two vertices, which are called its endpoints. If ee connects u,v∈Vu, v \in V, we write e=⟨u,v⟩e = \langle u, v \rangle. In this case, the vertices uu and vv are called adjacent.

<Figure size 640x480 with 1 Axes>
<Figure size 400x400 with 1 Axes>
5 [0, 1, 2, 3, 4]
6 [(0, 1), (0, 2), (1, 2), (1, 3), (2, 3), (3, 4)]
True False
DegreeView({0: 2, 1: 3})
AtlasView({1: {}, 2: {}})
<Figure size 640x480 with 1 Axes>
<Figure size 640x480 with 1 Axes>
<Figure size 640x480 with 1 Axes>
<Figure size 640x480 with 1 Axes>

The complement of a graph GG, denoted as G‾\overline{G} is the graph obtained by removing all links and connecting exactly those vertices that are not adjacent in GG.

Of course, if we take a graph GG and its complement G‾\overline{G} and join them, we obtain a complete graph.

(-0.9989638474274827, 1.1899467909320232, -1.150778381847691, 1.1507784414523377)
<Figure size 400x400 with 1 Axes>

Subgraphs

Definition: A subgraph GG is a graph HH with V(H)⊆V(G)V(H) \subseteq V(G) and E(H)⊆E(G)E(H) \subseteq E(G).

The subgraph of G=(V,E)G = (V,E) induced by V′⊆VV' \subseteq V , defined as G[V′]G[V'], is a graph such that (V′,{(ni,nj)∣(ni,nj)∈E∧(ni,nj)∈V′})(V', \{(n_i,n_j) | (n_i, n_j) \in E \land (n_i, n_j) \in V'\}).

<Figure size 1000x500 with 2 Axes>

Graph representations and data structures

There are different ways to represent a graph. Perhaps the most common is to use an adjacency matrix. Consider a graph GG with nn vertices and mm links. Its adjacency matrix is a matrix AA with nn columns and nn rows with entries A[i,j]A[i, j] denoting the number of links connected by vertices viv_i and vjv_j.

What properties can we see from this example?

An adjacency matrix is symmetric, if for all ii and jj, A[i,j]=A[j,i]A[i,j] = A[j,i]. This property reflects the fact that a link is represented as an unordered pair of vertices (i.e., e=⟨vi,vj⟩=⟨vj,vi⟩e = \langle v_i, v_j \rangle = \langle v_j, v_i \rangle). 2. A graph GG is simple if and only if for each ii and jj, A[i,j]≤A[i,j] \leq and A[i,i]=0A[i,i] = 0. In other words, there can be at least one link connecting vertices viv_i and vjv_j and, in particular, no link connects to a vertex by itself.

As an alternative, we can also use the incidence matrix of a graph as its representation. An incidence matrix MM of graph GG consists of nn rows and mm columns such that M[i,j]M[i,j] counts the number of times the link eje_j is incident to vertex viv_i.

Adjacency Matrix:
 [[0 1 1 0 0]
 [1 0 1 1 0]
 [1 1 0 1 0]
 [0 1 1 0 1]
 [0 0 0 1 0]]
Incidence Matrix:
 [[1 1 0 0 0 0]
 [1 0 1 1 0 0]
 [0 1 1 0 1 0]
 [0 0 0 1 1 1]
 [0 0 0 0 0 1]]

📚 Further Reading

If you want more control over layout, labels, and LaTeX-style formatting, refer to the following extended version, which includes:

  • Custom edge and node labels

  • Graph drawing alongside matrix visualization

  • Use of draw_networkx_edge_labels and draw_networkx_labels

  • LaTeX-style labels for publication-quality figures

{(0, 1): '$e_0$', (0, 2): '$e_1$', (1, 2): '$e_2$', (2, 3): '$e_3$', (3, 4): '$e_4$', (1, 3): '$e_5$'}

Adjacency matrix

dict_values(['$v_0$', '$v_1$', '$v_2$', '$v_3$', '$v_4$'])
<Figure size 700x300 with 2 Axes>

Incidence matrix

array([[1, 1, 0, 0, 0, 0], [1, 0, 1, 0, 0, 1], [0, 1, 1, 1, 0, 0], [0, 0, 0, 1, 1, 1], [0, 0, 0, 0, 1, 0]], dtype=int8)
<Figure size 400x300 with 1 Axes>

List of edges

(⟨v0,v1⟩,⟨v0,v2⟩,⟨v1,v2⟩,⟨v2,v3⟩,⟨v3,v4⟩,⟨v1,v3⟩)(\langle v_0, v_1 \rangle, \langle v_0, v_2 \rangle, \langle v_1, v_2 \rangle, \langle v_2, v_3 \rangle, \langle v_3, v_4 \rangle, \langle v_1, v_3 \rangle)