Computing and Software Systems 343, Winter 2003

advertisement
Computing and Software Systems 343, Winter 2007
Mathematical Principles of Computing II
Assignment 6. Version 1.0.
Due Wednesday, Mar. 7, 2 PM.
1. The following is an implementation of Prim’s algorithm:
Algorithm Prim(A, G)
Input: a weighted, connected graph G = (V,E), V = set of Vertices, E = set of edges
source vertex A  V. Weights on an edge e are accessed by e.weight.
assumes all vertices of G are unmarked at the beginning.
assumes G stored as an adjacency list.
Each vertex v can store a parent edge (accessible by v.parentEdge)
All parent edges are initially null.
Output: A list L of edges representing a spanning tree of the Graph G.
PQ  new unsorted array-based priority queue PQ
// PQ contains list of reachable vertices at each step,
// where reachable = not yet in tree, but adjacent to some vertex in tree.
Mark Vertex A.
PQ.insertItem(0,A) // insert A into PQ with minimum priority.
while PQ is not empty {
NextVertex  PQ.removeMin();
add edge NextVertex.parentEdge to list L. //(using L.add(NextVertex.parentEdge))
for each incident edge e of NextVertex {
C  G.opposite(NextVertex, e) // edge e connects NextVertex to C.
if C is not yet marked {
mark C
C.parentEdge  e
PQ.insertItem(e.weight, C)
// insert vertex C into PQ sorted by
// weight of incident edge e.
}
else {
if current priority of vertex C inside PQ > e.weight
PQ.updatePriority(e.weight, C) // change priority of C to reflect new edge.
C.parentEdge e
}
}
} // PQ is now empty.
return L ;
Figure out the worst-case run-time cost of the above algorithm. Use n for number of vertices,
and m for number of edges. For the unsorted-array priority queue in the above code, you
should assume that adding an item into the priority queue is O(1), removing an item is O(k)
where k = # items in the priority queue, and that checking the priority of an item and updating
the priority of an item are both O(1) operations. You should show some work on how you
computed your result (at a minimum, state the number of data structure calls, and the number
of times the main loops are executed).
2. Draw a simple, connected, weighted graph with at least 8 vertices and 16 edges,
each with unique edge weights. Identify one vertex as a “start” vertex, and
illustrate a running of Dijkstra’s algorithm on this graph. At a minimum, you must
show the distance labels on each vertex and their values as they change, as well as
the final single-source shortest path tree at the end of the algorithm.
3. Draw a graph with negative weight edges but no negative weight cycle for which
Dijkstra's Algorithm does not produce a single-source shortest paths tree.
4. Suppose you are given a diagram of a telephone network, which is a graph G
whose vertices represent switching centers, and whose edges represent
communication lines between two centers. The edges are marked by their
bandwidth. The bandwidth of a path of multiple links is the bandwidth of its
lowest bandwidth edge. Give an algorithm that, given a diagram graph G and two
switching centers a and b as input, will find the path of maximum bandwidth
between a and b, and output the bandwidth of this path. Writing high-level
pseudocode is enough for this algorithm. To help you think through this problem,
consider the following graph:
4
8
9
A--------B--------C--------D
\
| _____/
10\
5| / 6
\
|/
E----F
12
In the above graph, the highest bandwidth path from A to D is (A, E, F, C, D) and has
bandwidth 6. The highest bandwidth path from A to B is A,E,F,C,B, which also has
bandwidth 6.
5. Figure out the worst-case run-time cost of your algorithm for the previous
problem (#4), in terms of n, the number of nodes, and m, the number of edges. Be
sure to state assumptions you made (such as "I assumed the graph was stored with
an adjacency matrix")
6. Exercise #6 in section 6.1. Be sure to show how you got your result, and the
assumptions you made in your analysis.
7. Exercise #2 in section 6.6
Extra Credit:
1) Write pseudocode (high-level is fine) for an algorithm that solves the
following problem: Given a free tree T, compute the diameter of T, which is
defined as the length of the longest simple path between any two vertices of
T. The input to the problem is a free tree T, and the output is the diameter of
T. This problem is motivated by the following scenario: The time delay of a
long-distance call can be determined by multiplying a small fixed constant by
the number of communication links on the telephone network between the
caller and callee. Suppose the telephone network of a company named RT&T
is a free tree. The engineers of RT&T want to compute the maximum possible
time delay that may be experienced in a long-distance call. This is simply the
diameter of T multiplied by a constant. Make your algorithm as efficient as
possible (it is possible to get O(n) run-time).
2) Design an efficient algorithm for finding a longest directed path from a vertex
s to a vertex t of an acyclic weighted digraph G. Specify your graph
representation and any auxiliary data structures used. Also analyze the time
complexity of your algorithm.
Download