An adjacency list is the standard data structure for representing sparse graphs. Unlike an adjacency matrix — which allocates anDocumentation 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.
n × n grid even when most cells are empty — an adjacency list only records edges that actually exist. For a graph with V vertices and E edges, the adjacency list uses O(V + E) space, making it far more memory-efficient when the number of edges is much smaller than V². In the UCALDAS Data Structures course, this structure is implemented using Python dictionaries inside each Vertex object.
Adding Vertices
add_vertex(id) creates a new Vertex, registers it in the graph’s master dictionary, increments the vertex counter, and returns the new object.
Vertex can be used immediately — for example, to inspect it or chain further operations.
Adding Edges
add_edge(from_id, to_id, weight) creates a directed edge from from_id to to_id with the given weight. If either vertex does not exist yet, it is created on the spot.
weight parameter defaults to 0, so unweighted graphs work without any extra arguments.
Inspecting the Graph
Graph-level queries
get_vertex(id)— returns theVertexobject for the given ID, orNoneif the vertex does not exist.get_vertices()— returns a view of all vertex IDs in the graph.
Vertex-level queries
Complete Working Example
The graph below models a directed network with verticesA through H, including a back edge from D back to B:
The back edge
D → B creates a cycle in the graph. Algorithms that traverse this graph (such as DFS or BFS) must track visited vertices to avoid infinite loops.