Understanding the structure of a graph is much easier when you can see it. Visualization reveals topology at a glance — you can spot cycles, isolated vertices, and heavily connected hubs in seconds. In the UCALDAS Data Structures course, graph diagrams are produced by bridging the customDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/tutosrive/ed/llms.txt
Use this file to discover all available pages before exploring further.
Graph / Vertex classes with NetworkX and Matplotlib, two widely used Python libraries for graph analysis and scientific plotting.
Installing Dependencies
If you are working inside Google Colab, both
networkx and matplotlib are pre-installed. You can skip the installation step and proceed directly to importing them.Converting to NetworkX
NetworkX cannot read the customGraph class directly, so a short loop transfers all edges — along with their weights — into a nx.DiGraph object.
g.vertex_listis the master dictionary from the customGraphclass.vertex.get_connections()returns the adjacency dict{neighbor_id: weight, ...}.nx.DiGraph.add_edge(u, v, weight=w)registers each directed, weighted edge.
Use
nx.DiGraph() for directed graphs and nx.Graph() for undirected graphs. Choosing the wrong type will cause arrows to disappear or duplicate edges to appear in the visualization.Drawing the Graph
Once the NetworkX graph is populated, three steps produce the final diagram: compute a layout, collect edge labels, then draw.| Parameter | Purpose |
|---|---|
pos | Dictionary mapping each node to an (x, y) coordinate. |
with_labels=True | Draws vertex IDs inside each node circle. |
node_color='skyblue' | Fill color for all nodes. |
node_size=1500 | Pixel area of each node circle. |
arrows=True | Draws arrowheads to indicate edge direction. |
font_color='red' | Color of the edge-weight labels. |
Advanced Graph Example
The snippet below builds the more complex graph fromGraph.ipynb — featuring ten vertices, nineteen directed edges, alternative paths, return edges, and a long low-cost shortcut — and renders it with NetworkX in a single runnable block.
A→B→D vs. A→C→D), return edges (E→B, F→C), and a long low-cost shortcut (A→I), making it the exact test graph used in Graph.ipynb for the Bellman-Ford and Dijkstra shortest-path algorithms.