Let's talk about Assignment 6 today.
The input for this assignment is a set of 3 files containing data that describes a street map of Pittsburgh. The following examples are taken directly from the assignment handout (small map). The comments are mine, and don't actually appear in the files. (You probably don't have to write all of this down.)
8 // number of places Ali_Baba 404 S_Craig_St // name of place 0, num. on street, name of street Baker_Hall 4850 Frew_St ...
For this assignment, only the first line of this file is needed. We'll look at the rest of the file in assignment seven.
19 // number of intersections 631912 499709 // x and y coordinates of intersection 0 632883 485117 // x and y coordinates of intersection 1 ...
Streets are listed alphabetically.
12 // number of streets 5th_Ave 2 // first street, alphabetically, 2 blocks 7 5000 5099 982 8 // first block on 5th_Ave, from node 7 to 8 8 5100 5199 419 1 // second block on 5th_ave, from node 8 to 10 Amberson_Ave 1 9 834 999 356 8 ...The street map can be thought of as a graph, with intersections corresponding to nodes, and blocks corresponding to edges. (See figure on handout.)
The program is to perform 4 tasks:
If the name does not exactly match a street name, look for a partial match. If there is more than one partial match, list them all. If there is one exact match or partial match, check to see if the address lies on any of the blocks on the streets.
In addition to writing the code that solves these four tasks, you will also have to write the code that reads in the input data and builds your internal data structures.
Before writing any code to solve any of the tasks, it makes sense to spend some time thinking about what data structures are most appropriate for this assignment. The first of these is easy:
Now let's brainstorm a little bit about what data structures we could use to represent the places and streets, and what algorithms we might use to solve the tasks. Try to think of some alternative yourself before looking at what we suggest.
This won't be used until Task 4.
A queue will be needed for Tasks 3 and 4.
Now let's take a step back and look at the different objects for which we may want to define a struct:
intersection/node (maybe) block/edge (probably) street (probably) place (probably)We'll want several global structures, too:
graph (yes) list of places (not globally, but one list per block) list of streets (yes) list of intersections (no, already part of adjacency list rep of graph)
Normally we think of this algorithm as performing a search from a single node to all of the other nodes in the graph. In Task 4, however, there are two important differences. First, we are going to search from a block, or edge, to the rest of the graph. One way to do this is to put both endpoints of the edge in the queue initially. Second, instead of listing the nodes (intersections) as we visit them, we want to list the places that we see, which are located on the edges. This is an important difference, because breadth-first search normally ignores an edge if it leads to an already visited node, which we can't necessarily do.
Example: (nodes listed in order they are removed from queue.)
0 1
o----o 4 o
| /|
| / |
| 3 / | /~\
o------------o | |_| Anja & Jason's House
2 /~\ \ |
|_| \ |
Bruce's \|
House 5 o
Even though 5 is already in the queue when the edge from 4 to 5 is
examined, we have to list Anja & Jason's House