Here are suggested solutions to the exercises about graphs.
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:
Not much. Only that dist(v,w) ≤ dist(s,v) + dist(s,w).
No.
See here: http://www.graphclasses.org/smallgraphs.html#nodes2
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.
Any two out of the four possible topological orderings of the graph:
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).
[a] Kruskal: Consider the edges in weight order – add an edge to the SPT if it doesn’t create a cycle. The edges will be added in the following order: AD, DE, GH, AB, BC, DF, EH. (DE and GH can be added in any order, as can AB and BC).
[b] Prim, starting from A: The edges will be added to the SPT in the following order: AD, DE, AB, BC, DF, EH, GH.
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.
(from the S&W website) “Adding the edge e to the MST creates a unique cycle. Delete the maximum weight edge on this cycle.”
See here: http://www.graphclasses.org/smallgraphs.html#nodes2
Here’s a sketch:
diameter(): max(eccentricity(v) for every v in V)
radius(): min(eccentricity(v) for every v in V)
Left as an exercise.
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”.
Left as an (advanced) exercise.
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:
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.)
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.
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.