Representing Weighted Graphs
Explore how to represent weighted graphs in C++ by adapting adjacency matrices and lists. Understand handling zero and negative weights with boolean and weight matrices, and learn to use floating-point types for real-numbered weights.
We'll cover the following...
We'll cover the following...
Let’s see how we can adapt the graph representations introduced in the previous lessons to weighted graphs. Throughout this lesson, we’ll use the following weighted graph as a running example:
An example weighted graph
Weighted adjacency matrix
To represent a weighted graph using its adjacency matrix, we can simply use a matrix of integers instead of booleans. The entry will be when there is no edge from to , otherwise, it will be the weight of the edge . Here is the weighted adjacency matrix of our example graph:
...