Word

advertisement
ALG0183 Algorithms and Data Structures
Laboratory 9: n (Bellman-Ford algorithm) and a (with topological sort algorithm)
Part A
n negative weights present (Bellman-Ford algorithm)
Rather than use an outer for loop as in the classic description of the Bellman-Ford algorithm, Weiss´s
code makes use of a queue. Nodes are placed on the queue only when a better distance to them is
found. (This should make for more efficient code.) The aim is to discover if the implementations for the
Graph Theory Applet and the JAVENGA applet use an outer for loop (simple or early termination) or a
queue. From the ALG0183 website, run the Graph Theory Applet and the JAVENGA applet on the
following test case.
(Weiss code)
Enter start node:V2
Enter destination node:V1
Enter algorithm (u, d, n, a ): n
(Cost is: -3.0) V2 to V0 to V3 to V4 to V1
Circle your chosen answers.
Graph Theory Applet: underlying implementation?
simple outer for loop
outer for loop with early termination
Graph Theory Applet: correct shortest-path between V2 and V1?
YES
NO
If no, why? Write your explanation below:
JAVENGA Applet: underlying implementation?
simple outer for loop
outer for loop with early termination
JAVENGA Applet: correct shortest-path between V2 and V1?
YES
NO
If no, why? Write your explanation below:
1
queue
queue
ALG0183 Algorithms and Data Structures
Part B
a acyclic with neg. weights (with topological sort algorithm)
Weiss´s code makes use of a queue. Nodes are placed on the queue only when their indegree (number
of incoming edges) has been reduced to zero. In principal, the data structure used does not matter: all
the nodes contained within it have an indegree of zero. (There are typically several valid topological
sorts for an acyclic, directed graph, and only one of them is required to solve the shortest-path
problem). A stack could just as easily be used as a queue. The aim of Part B is to discover the nature of
the underlying topological sort implementations for the David Galles jar and the JAVENGA applet: is a
queue or stack used to manage the list of nodes with an indegree of zero. From the ALG0183 website,
run and test the David Galles jar and the JAVENGA applet. For the Galles tool, use any test case
generated by the tool. For JAVENGA, use the following test case.
V2, V0, V1, V3, V4, V6, V5
Circle your chosen answers.
David Galles jar: underlying implementation?
queue
stack
David Galles jar: correct topological sort? (use any test case generated by the tool)
YES
NO
JAVENGA jar: underlying implementation?
queue
stack
JAVENGA jar: correct topological sort? (use the test case specified above)
YES
NO
2
ALG0183 Algorithms and Data Structures
Part C
n negative weights present (Bellman-Ford algorithm)
In Weiss´s Graph.java, change the Weiss implementation of algorithm n to examine the state of the
queue after each iteration of the inner loop which considers a node´s edges. To allow use of a linked list
iterator, change the queue declaration to: LinkedList<Vertex> q = new LinkedList<Vertex>( );. Use the
following code at the end of the inner loop:
ListIterator<Vertex> listIterator =q.listIterator();
while (listIterator.hasNext()) {
System.out.print(listIterator.next().name +" ");
}
System.out.println();
Now test Weiss´s algorithm n on the graph in Part A. From the ALG0183 website, download the graph
specification NegGraphTest1.txt, remembering to remove the edge (V1,V3).
Record the queue trace below:
start: V2
iteration 1:
iteration 2:
iteration 3:
iteration 4:
iteration 5:
iteration 6:
iteration 7:
iteration 8:
Circle your chosen answer.
This algorithm uses a queue but does it still require V-1 iterations of the outer loop (7-1=6)?
YES
NO
In the classic description of the Bellman-Ford algorithm, the number of edges that would be considered
is 6 * 11 = 66. How many edges are considered in the Weiss implementation which uses a queue to
control the outer for loop? Write your answer below.
Number of edges:
Note: For assessment purposes you should be able to perform such queue traces and edge counts on
small graphs without actual running the software.
3
Download