6 Graph Algorithms

advertisement
Tutorial Exercise (Graph Algorithms)
CS3381 Design and Analysis of Algorithms
Helena Wong, 2001
Heaps and Priority Queues
1. For an array of input numbers: 1,13,9,5,12,8,7,4,0,6,2,15
a.) Illustrate the operation of BUILD-MAX-HEAP on these data.
b.) For the resultant heap A obtained in (a), illustrate the operation of HEAP-EXTRACT-MAX.
c.) For the resultant heap A obtained in (a), illustrate the operation of MAX-HEAP-INSERT(A,10).
2. Write pseudocode for the procedures HEAP-MINIMUM, HEAP-EXTRACT-MIN, HEAPDECREASE-KEY, and MIN-HEAP-INSERT that implement a min-priority queue with a min-heap.
3. Why do we want the loop index i in line 2 of BUILD-MAX-HEAP to decrease from  n/2  down to 1
rather than increase from 1 to  n/2 ?
4. Argue the correctness of HEAP-INCREASE-HEY using the following loop invariant:
At the start of each iteration of the while loop, the array A[1 .. n] satisfies the max-heap property,
except that there may be one violation: A[i] may be larger than A[PARENT(i)]
HEAP-INCREASE-KEY(A,i,key)
A[i] = key
While i > 1 and A[PARENT(i)] < A[i]
Do exchange A[i] and A[PARENT(i)]
i = PARENT(i)
Data structures for Graph Problems
5. Given an adjacency-matrix representation of a directed graph, how long does it take to compute
the out-degree of every vertex? How long does it take to compute the in-degrees?
Given an adjacency-list representation of a directed graph, how long does it take to compute the
out-degree of every vertex? How long does it take to compute the in-degrees?
6. Give an adjacency-list representation for a complete binary tree on 7 vertices, assume that the
vertices are numbered from 1 to 7 as in a binary heap.
Give an adjacency-matrix representation.
Breadth First Search
7. Show the distance and pred values that result from running breadth-first search on the following
graph, using vertex 3 as the source.
1
2
3
4
5
6
8. What is the running time of BFS if its input graph is represented by an adjacency matrix and the
algorithm is modified to handle this form of input?
9. Argue that in a breadth-first search, the value u.distance is independent of the order in which the
vertices in each adjacency list are given.
Using the following graph as an example, show that the breadth-first tree computed by BFS can
depend on the ordering within adjacency lists.
r


v
s


w
t


x
u


y
Tutorial Exercise (Graph Algorithms)
CS3381 Design and Analysis of Algorithms
Helena Wong, 2001
Depth First Search and Topological Sorting
10. Show how depth-first search works on the following graph. Assume that the For loops of the DFS
procedure considers the vertices in alphabetical order, and assume that each adjacency list is
ordered alphabetically. Show the discovery and finishing times for each vertex.
Show the ordering of vertices produced by TOPOLOGICAL-SORT when it is run on this graph.
q
s
v
r
t
u
w
x
y
z
11. Explain how a vertex u of a directed graph can end up in a depth-first tree containing only u, even
though u has both incoming and outgoing edges in G.
Minimum Spanning Trees
12. Run the Kruskal’s algorithm on the following graph:
1
1
4
6
4
2
2
4
5
3
5
6
6
8
7
4
3
3
7
13. Suppose that the graph G=(V,E) is represented as an adjacency matrix.
implementation of Prim’s algorithm for this case that runs in O(V2) time.
Give a simple
Single-Source Shortest Paths
14. Run the Bellman-Ford algorithm on the following directed graph, using vertex z as the source.
t

6
5
x
-2

-3
8
s 0
2
7

y
7
-4
9

z
15. The basic Bellman-Ford algorithm is to relax the whole set of edges |V|-1 times. This is carried
out in the |V|-1 iterations of the For loop. A more efficient version of the Bellman-Ford algorithm is
to keep track of whether there is any changes to the d values in each iteration, and to stop when
there is no change in one iteration. Implement this version by changing some codes of the basic
Bellman-Ford algorithm and the relaxation routine.
Tutorial Exercise (Graph Algorithms)
CS3381 Design and Analysis of Algorithms
Helena Wong, 2001
16. Run DAG-SHORTEST-PATHS on the following graph, using vertex s as the source.
6
1
s  5 0 2  7  -1  -2 
4 2
3
17. Give a simple example of a directed graph with negative-weight edges for which Dijkstra’s
algorithm produces incorrect answers.
All-Pairs Shortest Paths
18. Run SLOW-ALL-PAIRS-SHORTEST-PATHS on the following graph, showing the matrixes that
result for each iteration of the loop.
Do the same for FASTER-ALL-PAIRS-SHORTEST-PATHS.
1
1
-4
4
2
2
-1
3
7
5
2
5
3
10
-8
6
19. Suppose we also wish to compute the vertices on shortest paths in the SLOW-ALL-PAIRSSHORTEST-PATHS algorithm. Show how to compute the predecessor matrix from the
completed matrix L of shortest-path weights in O(n3) time.
20. Run the Floyd-Warshall algorithm on the graph for question 18. Show the matrix D (k) that results
for each iteration of the outer loop.
21. How can the output of the Floyd-Warshall algorithm be used to detect the presence of a negativeweight cycle?
22. Use Johnson’s algorithm to find the shortest paths between all pairs of vertices in the graph for
question 18. Show the values of w’ and  computed by the algorithm.
Download