Documentation Index
Fetch the complete documentation index at: https://mintlify.com/ucxinstructor/dsa_package/llms.txt
Use this file to discover all available pages before exploring further.
Overview
TheAdjacencyMatrixGraph class represents an unweighted graph using an adjacency matrix. It supports both directed and undirected graphs and stores edges as boolean values in a 2D matrix.
Constructor
Whether the graph is directed
Whether the graph is weighted (always False for this class)
List of vertex labels to initialize the graph with
ValueError if duplicate vertex labels are provided
Attributes
Whether the graph is directed
Whether the graph is weighted (always False)
List of all vertex labels in the graph
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.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_directeddelete_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 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
print_graph
Print a visual representation of the adjacency matrix.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²) where V is the number of vertices
- Add Vertex: O(V²) - requires resizing matrix
- Delete Vertex: O(V²) - requires resizing matrix
- Add Edge: O(1)
- Delete Edge: O(1)
- Has Edge: O(1)
- Get Adjacents: O(V)
- Get All Edges: O(V²)
Notes
- Best for dense graphs where most vertices are connected
- Provides O(1) edge lookup time
- Uses more space than adjacency list for sparse graphs
- Vertex labels must be strings
- Self-loops are not supported
- For undirected graphs, edges are stored symmetrically in the matrix