Solution file

advertisement
Homework 5 Solution
Problem 25.3-1
Define arbitrary source vertex S. Then we run Bellman-Ford:
Then we calculate h[v] and ŵ[v] based on BF results.
h( 1 ) = -5
h( 2 ) = -3
h( 3 ) = 0
h( 4 ) = -1
h( 5 ) = -6
h( 6 ) = -8
ŵ (v1 , v5 ) = -1 - 5 + 6 = 0
ŵ (v2 , v1 ) = 1 - 3 + 5 = 3
ŵ (v2 , v4 ) = 2 - 3 + 1 = 0
ŵ (v3 , v2 ) = 2 + 0 + 3 = 5
ŵ (v3 , v6 ) = -8 + 0 + 8 = 0
ŵ (v4 , v1 ) = -4 - 1 + 5 = 0
ŵ (v4 , v5 ) = 3 - 1 + 6 = 8
ŵ (v5 , v2 ) = 7 - 6 + 3 = 4
ŵ (v6 , v2 ) = 5 - 8 + 3 = 0
ŵ (v6 , v3 ) = 10 - 8 - 0 = 2
Then we run Dijkstra for each vertex:
After that we assemble final matrix from Dijkstra’s result:
Problem 25.3-4
It changes shortest paths. Consider the following graph. V = {s,x,y,z}, and there are 4 edges: w(s,x)=2,
w(x,y)=2, w(s,y)=5 and w(s,z)=-10. So we’d add 10 to every weight to make ŵ. With w, the shortest
path from s to y is s→x→y, with weight 4. With ŵ, the shortest path from s to y is s→y, with weight
15. (The path s→x→y has weight 24.) The problem is that by just adding the same amount to every
edge, you penalize paths with more edges, even if their weights are low.
Problem 22.4-2
Add a field to the vertex representation to hold an integer count. Initially, set vertex t’s count to 1
and other vertices’ count to 0. Start running DFS with s as the start vertex. When t is discovered, it
should be immediately marked as finished (BLACK), without further processing starting from it.
Subsequently, each time DFS finishes a vertex v, set v’s count to the sum of the counts of all vertices
adjacent to v. When DFS finishes vertex s, stop and return the count computed for s.
Problem 22.4-3
An undirected graph is acyclic (i.e., a forest) if and only if a DFS yields no back edges.
If there’s a back edge, there’s a cycle.
If there’s no back edge, then by Theorem 22.10, there are only tree edges.
Hence, the graph is acyclic.
Thus, we can run DFS: if we find a back edge, there’s a cycle. If we ever see |V| distinct edges, we
must have seen a back edge.
Problem 24.4-1
As shown below, the constraint graph is constructed and solved by Bellman-Ford algorithm,which
returns TRUE. Therefore, a feasible solution is x = (-5, -3, 0, -1, -6, -8).
Bellman-Ford execution:
Download