Data structures and algorithms

Exercise solutions: Graphs

Here are suggested solutions to the exercises about graphs.

Core exercises

Undirected graphs (A)

A2. Adjacency lists

The last edge added (say x–y) will be first in two adjacency lists (x–y in adj(x) and y–x in adj(y)). So, any adjacency list where there is no edge occurring first twice is impossible, such as this one:

A3. BFS tree

Not much. Only that dist(v,w) ≤ dist(s,v) + dist(s,w).

A4. BFS with a stack

No.

A5. Nonisomprphic graphs

See here: http://www.graphclasses.org/smallgraphs.html#nodes2

Directed graphs (B)

B1. Draw adjacency lists

Any variant of this, where the lists of outgoing edges from a node can be in any order:

For example, 5 can map to the list [5→1, 5→4, 5→7], or [5→4, 5→1, 5→7], or [5→7, 5→4, 5→1], or … etc. etc.

B3. Topological orderings

Any two out of the four possible topological orderings of the graph:

Minimum spanning trees (C)

C1. Compute a MST

There is only one MST, and it’s a tree with the following three branches: A–D–E–H–G, A–B–C, and D–F. (In other words, the following edges are not in the MST: BE, CE, FG).

C2. Maximum spanning tree

Negate all weights, and then build the minimum spanning tree.

Alternatively (and equivalently): build a tree the same way you would a minimum spanning tree using a different criterion for picking edges – pick the maximum weighted edge instead of minimum.

C3. Adding an edge to the MST

(from the S&W website) “Adding the edge e to the MST creates a unique cycle. Delete the maximum weight edge on this cycle.”

Shortest paths (D)

D1. Draw the shortest path tree

D2. Draw another SPT

Bonus exercises

Undirected graphs (E)

E1. Nonisomorphic graphs

See here: http://www.graphclasses.org/smallgraphs.html#nodes2

E2. Removing vertices

E3. Eccentricity

Here’s a sketch:

E4. Eulerian and Hamiltonian cycles

Left as an exercise.

Directed graphs (F)

F1. Nonisomprphic graphs

Start from http://www.graphclasses.org/smallgraphs.html#nodes2, and for every graph class, count the number of possible isomorphic ways to add directions to the edges (without creating cycles):

These are part of the integer sequence A003087 “Number of acyclic digraphs with n unlabeled nodes”.

F2. Unique topological ordering

Left as an (advanced) exercise.

Minimum spanning trees (G)

G1. Draw all MSTs

Start from the mid node (4), and think about which connections it can have in a spanning tree. For each possible way of connecting (4), count the possible spanning trees:

G2. Uniqueness of MST

Our hypothesis: that all edges in a given graph have distinct weights.

We can proceed by contradiction: suppose there are two different MSTs T1, T2. (Side note: this means particularly that they must have the same overall weight: Σe ∈ T1 ω(e) = Σe ∈ T2 ω(e)). Since they are different, there must be at least one edge in one of the trees which is not in the other (and vice versa!). Let’s say e1 is the edge with minimum possible weight which can be found in one tree but not the other. We may assume e1 ∈ T1, and so e1 ∉ T2 (the converse would not affect the following reasoning). If we add e1 to T2 (call this new graph G) we would create a cycle in it (make sure you understand why adding an edge between existing nodes in a tree creates a cycle). T1 is a tree, therefore it cannot contain the cycle we have just created. This means some edge in that cycle must not be in T1. Let’s name this edge e2.

Now we can build a new spanning tree by removing e2 from G (let’s call it T2*; this graph is still a tree: removing an edge from the cycle in G breaks the cycle and still maintains the connectivity). Necessarily, ω(e2) > ω(e1) because we chose e1 to be the smallest edge only present in one of the trees. So ω( T2* ) = ω(T2) + ω(e1) – ω(e2) < ω(T2). That is, T2* is a spanning tree with lower weight than the initial T2. We’ve arrived at a contradiction! (T2 was assumed to be a minimum spanning tree.)

Shortest paths (H)

H1. Vertex weights

Sketch: Split every vertex into two: one with only the ingoing edges and one with only the outgoing edges. Connect the two using one new edge having the weight of the original vertex.

H2. Number maze

General idea: create a directed graph where:

We may then solve the shortest path problem between the node corresponding to the start cell (source of the path) and the node corresponding to the goal cell.

Note that more details should be worked out to implement this! E.g.: how to specify which edges to have in the graph.