{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

chap4 - Chapter 4 Paths in graphs 4.1 Distances Depth-rst...

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

Chapter 4 Paths in graphs 4.1 Distances Depth-first search readily identifies all the vertices of a graph that can be reached from a designated starting point. It also finds explicit paths to these vertices, summarized in its search tree (Figure 4.1). However, these paths might not be the most economical ones possi- ble. In the figure, vertex C is reachable from S by traversing just one edge, while the DFS tree shows a path of length 3 . This chapter is about algorithms for finding shortest paths in graphs. Path lengths allow us to talk quantitatively about the extent to which different vertices of a graph are separated from each other: The distance between two nodes is the length of the shortest path between them. To get a concrete feel for this notion, consider a physical realization of a graph that has a ball for each vertex and a piece of string for each edge. If you lift the ball for vertex s high enough, the other balls that get pulled up along with it are precisely the vertices reachable from s . And to find their distances from s , you need only measure how far below s they hang. Figure 4.1 (a) A simple graph and (b) its depth-first search tree. (a) E A S B D C (b) S A B D E C 115

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

View Full Document
116 Algorithms Figure 4.2 A physical model of a graph. B E S D C A S D E C B A In Figure 4.2 for example, vertex B is at distance 2 from S , and there are two shortest paths to it. When S is held up, the strings along each of these paths become taut. On the other hand, edge ( D, E ) plays no role in any shortest path and therefore remains slack. 4.2 Breadth-first search In Figure 4.2, the lifting of s partitions the graph into layers: s itself, the nodes at distance 1 from it, the nodes at distance 2 from it, and so on. A convenient way to compute distances from s to the other vertices is to proceed layer by layer. Once we have picked out the nodes at distance 0 , 1 , 2 , . . . , d , the ones at d + 1 are easily determined: they are precisely the as-yet- unseen nodes that are adjacent to the layer at distance d . This suggests an iterative algorithm in which two layers are active at any given time: some layer d , which has been fully identified, and d + 1 , which is being discovered by scanning the neighbors of layer d . Breadth-first search (BFS) directly implements this simple reasoning (Figure 4.3). Ini- tially the queue Q consists only of s , the one node at distance 0 . And for each subsequent distance d = 1 , 2 , 3 , . . . , there is a point in time at which Q contains all the nodes at distance d and nothing else. As these nodes are processed (ejected off the front of the queue), their as-yet-unseen neighbors are injected into the end of the queue. Let’s try out this algorithm on our earlier example (Figure 4.1) to confirm that it does the right thing. If S is the starting point and the nodes are ordered alphabetically, they get visited in the sequence shown in Figure 4.4. The breadth-first search tree, on the right, contains the edges through which each node is initially discovered. Unlike the DFS tree we saw earlier, it has the property that all its paths from S are the shortest possible. It is therefore a
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern