CS112_46_Assignment8A - Assignment 8A Problem 1:...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Assignment 8A Problem 1: Understanding graph algorithms Those of you who have played Clue will recognize the following undirected graph, which shows the connections between the various rooms on the game board: The numbers on the various arcs show the distance (measured in spaces on the board) between pairs of rooms. For example, the distance from the Hall to the Lounge is 4 steps, and the distance from the Ball Room to the Billiard Room is 6 steps. In this problem, the secret passages ” that connect the rooms at the corners of the board (the Kitchen-Study and Lounge-Conservatory arcs) are arbitrarily assumed to have distance 3. 1a) Indicate the order of traversal for a depth-first search starting at the Lounge. Assume that iteration over a set chooses nodes in alphabetical order. Thus, the first step in the depth-first search will be to the Conservatory, rather than to the Dining Room or Hall, which come later in the alphabet. 1b) Indicate the order of traversal for a breadth-first search starting at the Kitchen. As before, assume that nodes in any set are processed in alphabetical order. 1c) Trace the operation of Dijkstra ’ s algorithm to find the minimum path from the Lounge to the Library. For convenience, Dijkstra ’ s algorithm is shown below.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Figure 1. Code for Dijkstra’s algorithm static pathADT FindShortestPath(nodeADT start, nodeADT finish) { queueADT queue; pathADT path, newPath; arcADT arc; queue = NewQueue(); path = NewPath(); while (start != finish) { if (!IsDistanceFixed(start)) { FixNodeDistance(start, TotalPathDistance(path)); foreach (arc in ArcsFrom(start)) { if (!IsDistanceFixed(EndOfArc(arc))) { newPath = NewExtendedPath(path, arc); PriorityEnqueue(queue, newPath, TotalPathDistance(newPath)); } } } if (QueueIsEmpty(queue)) return (NULL); path = Dequeue(queue); start = EndOfPath(path); } return (path); } Problem 2: Traversal Strategies for Graphs The DepthFirstSearch and BreadthFirstSearch traversal functions given in chapter 16 of your textbook are written to emphasize the structure of the underlying algorithms. If you wanted to include these traversal strategies as part of the graph package, you would need to reimplement the functions so that they no longer depended on a client-supplied Visit function. One approach is to implement these two algorithms as the following mapping functions: void MapDFS(nodeFnT fn, nodeADT start, void *clientData); void MapBFS(nodeFnT fn, nodeADT start, void *clientData); In each case, the functions should call fn(node, clientData) for every node reachable from start in the specified traversal order. To make these functions even more general, write the code for MapDFS MapBFS so that the mapping function itself keeps track of the nodes that have been visited—presumably by
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 7

CS112_46_Assignment8A - Assignment 8A Problem 1:...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online