Overview
TheAdjacencyListGraph class represents an unweighted graph using an adjacency list. It stores each vertex and its list of adjacent vertices in a dictionary, making it space-efficient for sparse graphs.
Constructor
Whether the graph is directed
List of vertex labels to initialize the graph with
Attributes
Whether the graph is directed
Whether the graph is weighted (always False)
Methods
add_vertex
Add a vertex to the graph.The vertex label to add
ValueError if the vertex already exists
delete_vertex
Delete a vertex from the graph and remove all edges connected to it.The vertex label to delete
add_edge
Add an edge between two vertices.Starting vertex label
Ending vertex label
Override the graph’s default directed behavior. If None, uses
self.is_directedadd_directed_edge
Add a directed edge from start to end.Starting vertex label
Ending vertex label
delete_edge
Delete an edge between two vertices.Starting vertex label
Ending vertex label
Override the graph’s default directed behavior. If None, uses
self.is_directedKeyError if either vertex or the edge does not exist
has_vertex
Check if a vertex exists in the graph.The vertex label to check
True if the vertex exists, False otherwise
has_edge
Check if an edge exists between two vertices.Starting vertex label
Ending vertex label
True if an edge exists from start to end, False otherwise
KeyError if either vertex does not exist
vertices
Get a list of all vertex labels.List of all vertex labels in the graph
edges
Get a list of all edges in the graph.List of edges, each represented as a tuple
(start, end)undirected_edges
Get a list of edges without duplicates for undirected graphs.List of unique edges, each represented as a tuple
(start, end)adjacents
Get a list of adjacent vertices for a given vertex.The vertex label
List of adjacent vertex labels
order
Get the number of vertices in the graph.The number of vertices
size
Get the number of edges in the graph.The number of edges
to_dict
Convert the graph to a dictionary representation.Dictionary where keys are vertices and values are lists of adjacent vertices
Class Methods
from_dict
Create a graph from a dictionary representation.Dictionary where keys are vertices and values are lists of adjacent vertices
Whether the graph is directed
A new graph instance
Special Methods
__getitem__
Get adjacent vertices using bracket notation.__contains__
Check if a vertex exists using thein operator.
__len__
Get the number of vertices.Complete Examples
Performance
- Space Complexity: O(V + E) where V = vertices, E = edges
- Add Vertex: O(1)
- Delete Vertex: O(V + E) - must remove vertex from all adjacency lists
- Add Edge: O(1) average case
- Delete Edge: O(degree(V)) - must search adjacency list
- Has Edge: O(degree(V))
- Get Adjacents: O(1)
- Get All Edges: O(V + E)
Notes
- Best for sparse graphs where E is much less than V²
- More space-efficient than adjacency matrix for sparse graphs
- Faster vertex addition than adjacency matrix
- Slower edge lookup than adjacency matrix
- Vertex labels must be strings
- Adjacency lists are implemented using Python dictionaries and lists
- For undirected graphs, edges are stored in both directions