Overview
TheAdjacencyListWeightedGraph class represents a weighted graph using an adjacency list. It extends AdjacencyListGraph and stores each vertex with a dictionary mapping adjacent vertices to edge weights, making it space-efficient for sparse weighted 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 True)
Methods
add_edge
Add a weighted edge between two vertices.Starting vertex label
Ending vertex label
The weight of the edge
Override the graph’s default directed behavior. If None, uses
self.is_directeddelete_edge
Delete a weighted 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
get_weight
Get the weight of an edge between two vertices.Starting vertex label
Ending vertex label
The weight of the edge from start to end
adjacents
Get a list of adjacent vertices for a given vertex.The vertex label
List of adjacent vertex labels (without weights)
adjacent_items
Get a list of adjacent vertices with their edge weights.The vertex label
Dictionary items view of
(neighbor_label, weight) pairsedges
Get a list of all weighted edges in the graph.List of edges, each represented as a tuple
(start, end, weight)undirected_edges
Get a list of weighted edges without duplicates for undirected graphs.List of unique weighted edges, each represented as a tuple
(start, end, weight)Class Methods
from_dict
Create a weighted graph from a dictionary representation.Dictionary where keys are vertices and values are dictionaries mapping neighbors to weights
Whether the graph is directed
A new weighted graph instance
Special Methods
__getitem__
Get adjacent vertices and weights using bracket notation.__len__
Get the number of vertices.Inherited Methods
The following methods are inherited fromAdjacencyListGraph:
add_vertex(label)- Add a vertexdelete_vertex(label)- Delete a vertexhas_vertex(label)- Check if vertex existshas_edge(start, end)- Check if edge existsvertices()- Get all verticesorder()- Get number of verticessize()- Get number of edgesto_dict()- Convert to dictionary (returns weighted format)__contains__(label)- Check vertex membership
Complete Examples
Performance
- Space Complexity: O(V + E) where V = vertices, E = edges
- Add Vertex: O(1)
- Delete Vertex: O(V + E)
- Add Edge: O(1) average case
- Delete Edge: O(1) average case (dictionary deletion)
- Has Edge: O(1) average case
- Get Weight: O(1) average case
- Get Adjacents: O(1)
- Get Adjacent Items: O(1)
- Get All Edges: O(V + E)
Notes
- Best for sparse weighted graphs where E is much less than V²
- More space-efficient than weighted adjacency matrix
- Fast edge weight lookups using dictionary structure
- Weights can be any numeric type (int, float, etc.)
- Vertex labels must be strings
- Ideal for shortest path algorithms (Dijkstra, Bellman-Ford)
- Good for minimum spanning tree algorithms (Prim, Kruskal)
- For undirected graphs, edge weights are stored in both directions
- Adjacent items are stored as dictionary items for efficient iteration