Search⌘ K
AI Features

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.

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:

g 0 0 1 1 0->1 5 1->0 3 2 2 1->2 7 3 3 1->3 4 2->0:se 11 2->3 4
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 A[i,j]A[i,j] will be 00 when there is no edge from i ...