Answers to Self-Study Questions

Test Yourself #1

Test Yourself #2

Question 1:

Question 2:

Given a weighted graph with edges stored using adjacency lists, the edge weights could be stored in the same lists as "extra" fields. For example, if node n's list of successors is stored in a linked list, then instead of having a single "data" field in each list node (containing a pointer to one of n's successors), there could be two "data" fields: one for the successor m, and one for the "weight" associated with the edge from n to m.

If edges are stored in an adjacency matrix, a similar technique could be used: instead of storing a single boolean value in matrix[j,k], we could store two values, a boolean (indicating whether there is an edge from j to k) and (if the boolean == true) the "weight" associated with that edge. If there is some special "weight" value that is never used (for example, if the weights are all non-negative integers then the value -1 will never be used) we could just store weights (not booleans), using the special value to indicate that there is no edge from j to k.