Overview
TheAdjacencyMatrixWeightedGraph class represents a weighted graph using an adjacency matrix. It extends AdjacencyMatrixGraph and stores edge weights as numeric values in a 2D matrix. Supports both directed and undirected graphs.
Constructor
Whether the graph is directed
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 True)
List of all vertex labels in the graph
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_directedget_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
edges
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)adjacent_items
Get a list of adjacent vertices with their edge weights.The vertex label
List of tuples
(neighbor_label, weight)print_graph
Print a visual representation of the weighted adjacency matrix.to_dict
Convert the graph to a dictionary representation.Dictionary where keys are vertices and values are dictionaries mapping neighbors to weights
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.Inherited Methods
The following methods are inherited fromAdjacencyMatrixGraph:
add_vertex(label)- Add a vertexdelete_vertex(label)- Delete a vertexdelete_edge(start, end, directed=None)- Delete an edgehas_vertex(label)- Check if vertex existshas_edge(start, end)- Check if edge existsvertices()- Get all verticesadjacents(vertex)- Get adjacent vertices (without weights)order()- Get number of verticessize()- Get number of edges__len__()- Get number of vertices__contains__(label)- Check vertex membership
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 Weight: 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 weight lookup
- Weights can be any numeric type (int, float, etc.)
- Self-loops are prevented (diagonal is set to None)
- For undirected graphs, edge weights are stored symmetrically
- Vertex labels must be strings
- Useful for shortest path algorithms (Dijkstra, Floyd-Warshall)
- Ideal for minimum spanning tree algorithms (Prim, Kruskal)