![]() |
VOOZH | about |
Graphs represents relationships in data like social networks or transportation systems. When graphs are large and mostly empty (i.e., sparse) normal storage becomes inefficient. Compressed Sparse Graphs (CSGraph) solve this by storing only actual connections (non-zero values) using sparse matrix formats. This saves memory and speeds up graph operations.
SciPy’s scipy.sparse.csgraph module provides tools and algorithms to work with these sparse graph structures using formats like CSR (Compressed Sparse Row) or CSC (Compressed Sparse Column).
scipy.sparse.csgraph subpackage offers a wide range of functionalities and algorithms for efficient graph analysis. Let's look into it:
To use SciPy’s graph algorithms, graph must be represented in a compressed sparse format done by defining a graph using an adjacency matrix or edge list, converting it to a sparse matrix and then using csgraph_from_dense() to create CSGraph.
The examples below show how to do this in different ways.
This example shows how to create a CSGraph from an empty sparse matrix. It demonstrates basic structure of a graph with no edges, helps to understand how CSGraphs are initialized.
Output
[[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]]
Explanation:
A directed graph has edges that point from one node to another. An adjacency matrix represents this with a 2D grid if [i][j] = 1, there's an edge from node i to node j. It is a clear way to define graph structure to use in SciPy. Let's see below example:
Output
Coords Values
(0, 1) 1.0
(0, 3) 1.0
(1, 2) 1.0
(2, 3) 1.0
Explanation:
An edge list represents a graph using node to node connections. It's simple and efficient for sparse graphs and can be easily converted to a CSGraph for fast analysis in SciPy. Let's see below code:
Output
[[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]]
Explanation:
An undirected graph has edges with no direction, meaning connections go both ways. A symmetric matrix is used to represent it, where the value at [i][j] equals [j][i]. Let's see this in Example:
Output
Coords Values
(0, 1) 1.0
(0, 3) 1.0
(1, 0) 1.0
(1, 2) 1.0
(2, 1) 1.0
(2, 3) 1.0
(3, 0) 1.0
(3, 2) 1.0
Explanation:
Once a CSGraph is created, SciPy lets you apply graph algorithms efficiently. These include BFS, DFS, shortest paths, MST etc. Let's look at some of the Examples:
BFS (Breadth-First Search) visits nodes level by level and is useful for finding shortest path in unweighted graphs. Below code performs BFS on a directed graph starting from node 0 and prints traversal order.
Output
Coords Values
(0, 1) 1
(0, 2) 2
(1, 3) 1
(2, 0) 2
(2, 3) 3
Breadth-first travelling order: [0 1 2 3]
Explanation:
DFS (Depth-First Search) explores as far as possible along each branch before backtracking. The code below performs DFS on a directed graph starting from node 1 and prints order of traversal.
Output
Coords Values
(0, 1) 1
(0, 2) 2
(1, 3) 1
(2, 0) 2
(2, 3) 3
Depth First Travelling order: [1 3]
Explanation: depth_first_order(): starts DFS traversal from node 1 without returning the predecessors.
Shortest Path finds minimum distance between nodes. The code computes shortest paths from a source node and between all nodes using SciPy’s shortest_path.
Output
Distance from Node 1 to other Nodes: [1. 0. 3. 1.]
All-pairs shortest distances:
[[0. 1. 2. 2.]
[1. 0. 3. 1.]
[2. 3. 0. 3.]
[2. 1. 3. 0.]]
Explanation: shortest_path(.): calculates shortest paths (from one node or between all nodes).
Minimum Spanning Tree connects all nodes in a graph with least total edge weight. The code finds MST using SciPy’s minimum_spanning_tree, ensuring no cycles and minimum cost connections.
Output
[[0. 0. 0. 3.]
[0. 0. 2. 5.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]
Explanation:
Maximum Flow finds greatest amount of flow that can be pushed from a source node to a sink node in a network. The code computes maximum flow and flow distribution using SciPy’s maximum_flow function.
Output
Maximum Flow Value: 23
Flow Distribution:
[[ 0 12 11 0 0 0]
[-12 0 0 12 0 0]
[-11 0 0 0 11 0]
[ 0 -12 0 0 -7 19]
[ 0 0 -11 7 0 4]
[ 0 0 0 -19 -4 0]]
Explanation: