COS 226 Programming Assignment 8

Shortest paths

Design and implement a Java applet that can find shortest paths through sparse Euclidean graphs, optimized to examine as few nodes as possible for any given search query. As a starting point, you may use the attached Java code for a Point class, a linked-list Node class, a FIFO Queue class, a Graph class, and a simple driver. This code implements the breadth-first and depth-first search demos that were done in class, on a particular class of sparse graphs formed by generating random points and including edges between points within a certain distance d of one another, with d chosen to make the expected number of edges in the graph a small contant multiple of V.

Note that we cannot use an adjacency-matrix representation for such graphs when V is huge, because we cannot afford a quadratic number of entries, but we can use an adjacency-lists representation, because we only need a constant number of links per node, on the average. The random-graph generator in the attached code is a brute-force one that takes quadratic time, and could be replaced by a better one that takes linear time (see extra credit), but none of our data structures require quadratic space, so we can process sparse graphs. Moreover, none of our graph-processing algorithms should take quadratic time, so that we can use them should a better generator be substituted, or for sparse graphs from some other source.

Your first task is to get oriented in a Java programming environment. You may wish to take this opportunity to download one onto your own computer, or you can use a publicly-available one. If you have never seen a Java program before, you may find the COS 126 Java lectures a useful starting point. This assignment involves just a tiny C-like subset of the language, but it does require that you know about classes. You also will use a tiny subset of the Applications Window Toolkit. You probably will not need to delve into esoteric Java materials, as the code attached to the previous paragraph comprises a skeleton applet that you can edit to complete the assignment. If you like bells and whistles, you are welcome to add menus and interactive data-entry to your applet, but do so on your own time! Our interest in using Java is that it gives us a quick way to see dynamic simulations of our algorithms in action. Your goal is to compile the .java files above with javac, then to view a breadth-first search simulation by making an applet file named 08demoBFS.html that contains

    <title>Graph Searching Example</title>
    <hr>
    <applet code=demo.class width=684 height=684>
    <PARAM NAME=V VALUE=1000>
    <PARAM NAME=algorithm VALUE=2>
    <PARAM NAME=delay VALUE=64>
    </applet>
    <hr>
and firing it up by typing appletviewer 08demoBFS.html. You may also be able to use a browser to see it, though not all browsers are compatible with code of this kind. Do this part of the assignment this week.

Your second task is to add a class that implements a priority queue and to use it to transform breadth-first search into a priority-first search. You can start with the code in the lecture notes, but you will need to modify it to use your own priority-queue interface and to accommodate Java conventions. Also, make the search into a function that takes two points as arguments, stops the search when it has found the shortest path between the two points, and prints the number of nodes examined. Completing this much of the assignment successfully can net you 6 points.

Your third task is to cut down the search time by driving the search toward the goal (that part is simple: change the priority function so that the point chosen from the fringe is the one for which the path through the graph from the source plus the as-the-crow-flies distance to the destination is the smallest) and then by changing the search to use a two-way (symmetric) strategy: keep two priority queues, one for each point, and alternate picking the best point off each fringe until the paths meet in the middle. Completing this much of the assignment successfully can net you 8 points.

Finally, add preprocessing code that can cut the search time even further, using the following idea: Divide the space into and M-by-M grid, and precompute, for each pair i and j of grid cells, the cost of the shortest path from any point in i to any point in j. Then change your two-way search to make use of this information to avoid looking at nodes that cannot be on the shortest path.

Whether you opt for the 6-point, 8-point, or 10-point challenge for this assignment, write a small driver that tests the performance of your method for graphs of V nodes for various values of N, and formulate a hypothesis about the number of nodes that your method examines to find the shortest path between two random nodes, on the average, as a function of V. As usual, submit a readme file that describes your experiments and includes your assessment of the results.

Note. Much of the code that we are providing is not yet mature, so you might encounter bugs. If you do, post what you find on the bug entry form on the course web page. Also, check the course announcements section of the web page for posted bug fixes.

Extra credit. Change the quadratic-time random graph generator to a linear-time one, along the lines of Program 3.20 in the text, and run experiments for V = 10,000 and V = 100,000.

Due: 11:59PM Thursday, April 23.