3. Graphs with NetworkX

NetworkX is a Python package for the creation, manipulation, and study of the structure, dynamics, and functions of complex networks [1]. In this notebook we will use this library to analyze graphs.

3.1. Graph or Undirected Simple Graph

A graph (sometimes called undirected graph for distinguishing from a directed graph) is a pair $ G = (V, E) $, where $V$ is a set whose elements are called vertices (singular: vertex), and $E$ is a set of two-sets (sets with two distinct elements) of vertices, whose elements are called edges (sometimes links or lines). [2]

The vertices $u$ and $v$ of an edge $ \{ u, v \} $ are called the endpoints of the edge. The edge is said to join $u$ and $v$ and to be incident on $u$ and $v$. A vertex may not belong to any edge.

A multigraph is a generalization that allows multiple edges adjacent to the same pair of vertices. In some texts, multigraphs are simply called graphs.

Graph density

Vertex degree

Adjacency matrix

Incidence matrix

Plotting Undirected Simple Graph

3.2. Graph Traversal

In computer science, graph traversal (also known as graph search) refers to the process of visiting (checking and/or updating) each vertex in a graph. Such traversals are classified by the order in which the vertices are visited. Tree traversal is a special case of graph traversal. [3]

Given the following undirected graph:

3.2.1. Breadth first search (BFS)

3.2.2. Depth first search (DFS)

3.3. Minimum Spanning Tree

A minimum spanning tree (MST) or minimum weight spanning tree is a subset of the edges of a connected, edge-weighted undirected graph (UWG) that connects all the vertices together, without any cycles and with the minimum possible total edge weight. [4]

If the weights are positive, then a minimum spanning tree is in fact a minimum-cost subgraph connecting all vertices, since subgraphs containing cycles necessarily have more total weight.

3.3.1. The MST with the Prim algorithm

3.3.2. The MST with the Kruskal algorithm from Scratch

Below, the Kruskal's minimum spanning tree algorithm with utility functions for sets union.

Plotting Minimum Spanning Tree of an UWG

3.4. Eulerian Circuit and Path

In graph theory, an Eulerian path is a trail in a finite graph that visits every edge exactly once (allowing for revisiting vertices). Similarly, an Eulerian circuit or Eulerian cycle is an Eulerian trail that starts and ends on the same vertex. They were first discussed by Leonhard Euler while solving the famous Seven Bridges of Königsberg problem in 1736. [5]

Plotting Eulerian Circuit

3.5. Shortest Path

A directed graph or digraph is a graph in which edges have orientations [6]. In formal terms, a directed graph is an ordered pair $ G = \langle V, E \rangle $ where:

3.5.1. The Shortest Path with the Dijkstra algorithm

Given a directed graph $ G = \langle V, E \rangle $, the time complexity of Dijkstra's algorithm is $ \Theta (V^2) $ but with min-priority queue it drops down to $ \Theta((E + V) \thinspace log{V}) $. [7]

3.5.2. The Dijkstra algorithm from Scratch

Below, a detailed version of the Dijkstra algorithm for directed graphs with edges with positive weights is shown.

Plotting Shortest Path of a DWG

3.6. All-Pairs Shortest Path

The all-pairs shortest path problem is the determination of the shortest graph distances between every pair of vertices in a given graph. The problem can be solved using $n$ applications of Dijkstra's algorithm or all at once using the Floyd-Warshall algorithm. [8]

3.6.1. Floyd-Warshall algorithm

The Floyd–Warshall algorithm is a method for finding shortest paths in a weighted graph with positive or negative edge weights (but with no negative cycles).

The Floyd–Warshall algorithm compares all possible paths through the graph between each pair of vertices. It is able to do this with $ \Theta (|V|^{3}) $ comparisons in a graph.

3.7. Graph Coloring

In graph theory, graph coloring is a special case of graph labeling; it is an assignment of labels traditionally called "colors" to elements of a graph subject to certain constraints. In its simplest form, it is a way of coloring the vertices of a graph such that no two adjacent vertices are of the same color; this is called a vertex coloring. [9]

Four color theorem

In mathematics, the four color theorem, or the four color map theorem, states that, given any separation of a plane (without crossings) into contiguous regions, producing a figure called a map, no more than four colors are required to color the regions of the map so that no two adjacent regions have the same color. Adjacent means that two regions share a common boundary curve segment, not merely a corner where three or more regions meet. It was the first major theorem to be proved using a computer. [10]

Given the following undirected plane graph of South America, color it using the Welsh-Powell algorithm, to validate the four color theorem.

Welsh-Powell Algorithm:

Plotting Graph Coloring

Reference

[1] GitHub - NetworkX.
[2] Wikipedia - Graphs.
[3] Wikipedia - Graph traversal.
[4] Wikipedia - Minimum spanning tree.
[5] Wikipedia - Eulerian path.
[6] Wikipedia - Directed graph.
[7] Wikipedia - Dijkstra's algorithm.
[8] Wikipedia - All-Pairs Shortest Path.
[9] Wikipedia - Graph coloring.
[10] Wikipedia - Four color theorem.


« Home