CS331 Advanced Data Structures – Spring 2016 Due: Mar. 16

advertisement
CS331 Advanced Data Structures – Spring 2016
Homework 4 – 50 points
Due: Mar. 16
All graphs used in these problems are shown on the other side of this sheet.
1. (5 points) Specify a topological ordering of the vertices in graph 1
2. (10 points)
(a) Determine a DFS and a BFS spanning tree starting at vertex A in graph 2, assuming
that the vertices are stored in alphabetical order in adjacency lists. Do the same thing
starting at vertex G.
(b) Remove edges (A,B) and (D,G) and repeat part (a)
3. (10 points)
(a) Find the shortest weighted path from C to all other vertices in graph 2 using Dijkstra’s
algorithm. List both the nodes on each path and the cost of each path.
(b) Specify what the dist field for each node is immediately after node D has been selected
off of the priority queue (but not processed).
4. (5 points) Show how to modify Dijkstra’s algorithm to count the number of minimum costs
paths from the source vertex s to all other vertices.
5. (5 points) One idea to find minimum cost paths in graphs with negative weights (but still
no negative cycles) is the following: Add a constant value c to all edge weights so that there
are no negative weights and then use Dijkstra’s algorithm to find the minimum cost paths.
Either prove that this approach works or find a small counter example that shows that it
won’t work.
6. (5 points) Consider the following statement:
If a directed graph G = (V, E) is acyclic and no edge between u and v is in E, then
at least one of (u, v) or (v, u) can be added to E without creating a cycle.
Give a (short) proof that this statement is correct or find a counterexample.
7. (10 points)
(a) Use the Bellman-Ford algorithm to find the shortest paths from v3 to all other vertices
in graph 3 (found on the next page). Show how the dynamic programming table changes
after each pass of the algorithm (show both the dist and prev values for all vertices).
(b) What would the shortest paths have looked like if Dijkstra’s algorithm had been used?
Graph 1
- C
A
B
@
@
@
?
?
@
R
@
s
t
D
E
F
I
@
@
@
@
@
@
@
?
@
R @ @
G
H
I
Graph 2
7
A
9
C
@
@ 4
8
@
@
@
4
D
B
@
@ 2
@
@
@
8
@
@ 1
2
@
@
@
@
@ 4
4
@
@
@
5
F
Graph 3
-3
v1
E
G
- v2
@
@
@
I
@ -1
@
@ 2
5
1
3@@
@
@ @
R
@
@
R
@
4
4
@
- v4
- v5
v3
@
I
@
@
@ 1
2
-2
@ -5
@
@
@
R
@
@
3
- v7
v6
Download