Intro DFS Biconnectivity DAGs SCC BFS Paths and Matrices Intro DFS Biconnectivity DAGs SCC BFS Paths and Matrices Overview Overview

Graphs provide a concise representation of a range problems Map coloring – more generally, resource contention problems

CSE 548: (Design and) Analysis of Algorithms FigureNetworks 3.1 (a) A— map communication, and (b) its graph. trac, social, biological, ...

Graphs (a) (b) 5 4 6 3 2 R. Sekar 8

7 1

9 10

12

13 11 1 / 30 2 / 30

necessary to use edges with directions on them. There can be directed edgese fromx toy (writtene=(x, y)), or fromy tox (written(y, x)), or both. A particularly enormous example of a is the graph of all links in the World Wide Web. It has a for each Intro DFS Biconnectivity DAGs SCC BFS Paths and Matrices Overview Introsite DFS on Biconnectivity the Internet, DAGs and SCC a directedBFS Pathsedge and Matrices(u, v) whenever siteu has a link to sitev: in total, billions of nodes and edges! Understanding even the most basic connectivity properties of the Web is of great economic and social interest. Although the size of this problem is daunting, Definition and Representations Depth-Firstwe will soon see that a Searchlot of valuable information (DFS) about the structure of a graph can, happily, be determined in just linear time. A technique for traversing all vertices in the graph A graphG = (V, E), where V is a set of vertices, and E a set of edges. An 3.1.1 How is a graph represented? We can represent a graph by an adjacency matrix; if there aren= V verticesv , . . . , v , this Very versatile, forms the linchpin of many graph| algorithms| 1 n edge e of the form (v1, v2) is said to span vertices v1 and v2. The edges in a is ann n array whose(i, j)th entry is × � directed graph are directed. 1 if there is an edge fromv i tov j dfs(V, E) aij = A G0 = (V 0, E0) is called a subgraph of G if V 0 ⊆ V and E0 includes every 0 otherwise. ∈ [ ] = 0 foreachFor undirectedv graphs,V do thevisited matrix isv symmetricfalse since an edge u, v can be taken in either edge in E between vertices in V . { } foreachdirection. v ∈ V do The biggest convenience of this format is that the presence of a particular edge can be checkedif not invisited constant[ time,v] then with justexplore one memory(V, E access., v) On the other hand the matrix takes Adjacency matrix Adjacency list upO(n 2) space, which is wasteful if the graph does not have very many edges. An alternative representation, with size proportional to the number of edges, is the adja- A graph (V = {v1,..., vn}, E) can be Each vertex v is associated with a explorecency list(V. ItE consistsv) of V linked lists, one per vertex. The linked list for vertexu holds the , , | | names of vertices to whichu has an outgoing edge—that is, verticesv for which(u, v) E. represented by an n × n matrix a, linked list consisting of all vertices u visited[v] = true ∈ 88 where aij = 1 i (vi, vj ) ∈ E such that (v, u) ∈ E. previsit(v) /*A placeholder for now*/ foreach (v, u) ∈ E do Note that adjacency matrix uses O(n2) storage, while adjacency list uses if not visited[u] then explore(G, V, u) O(|V| + |E|) storage. Both can represent directed as well as undirected postvisit(v) /*Another placeholder*/ graphs. 3 / 30 4 / 30 Intro DFS Biconnectivity DAGs SCC BFS Paths and Matrices Intro DFS Biconnectivity DAGs SCC BFS Paths and Matrices Graphs, Mazes and DFS A graph and its DFS

Figure 3.4 The result of explore(A) on the graph of Figure 3.2. Figure 3.2 Exploring a graph is rather like navigating a maze. Figure 3.2 Exploring a graph is rather like navigating a maze. A

L K L F K D A B E H B D B F D A B E H B

G H C F IJ C G E E F G J G H C F IJ C G E K L I J C H I A D L K J I A D FigureIf a maze 3.3 Finding is represented all nodes reachable as a from graph, a particular then DFS node. of the graph amounts DFS uses O(|V|) space and O(|E| + |V|) time. procedureto an exploration explore(G, and v) mapping of the maze. Figure 3.5 Depth-first search. Input:G=(V, E) is a graph;v V ∈ procedure dfs(G) Output: visited(u) is set to true for all nodesu reachable fromv Figure 3.3 Finding all nodes reachable from a particular node. 5 / 30 for allv V: 6 / 30 visited(v)= true procedure explore(G, v) ∈ previsit(v) visited(v)= false for each edge(v, u) E: Input:G=(V, E) is a graph;v V ∈ ∈ if not visited(u): explore(u) Output: visited(foru all) isv V: set to true for all nodesu reachable fromv ∈ postvisit(v) if not visited(v): explore(v) Intro DFS Biconnectivity DAGs SCC BFS Paths and Matrices Intro DFS Biconnectivity DAGs SCC BFS Paths and Matrices visited(v)= true DFS and Connected Components previsit(DFS Numberingv) for each edge(v, u) E: ∈ and whenever you arrive at any junction (vertex) there are a variety of passages (edges) you if not visited(Thisu loop): takes explore( a differentu) amount of time for each vertex, so let’s consider all vertices to- Figurecan follow. 3.6 (a) A careless A 12-node choice graph. of passages (b) DFS might search lead forest. you around in circles or might cause you gether. The total work done in step 1 is thenO( V ). In step 2, over the course of the entire to overlook some accessible part of the maze. Clearly, you need to record some intermediate postvisit(v) | | Associate post andDFS, pre eachnumbers edge x, with y E each is examined visited exactly node bytwice defining, once during explore(x) and once dur- information during exploration. 1,10 11,22 23,24 { }∈ (a) (b) ing explore(y). The overall time for step 2 is thereforeO( E ) and so the depth-first search This classic challenge has amused people for centuries.A Everybody knowsC that all youF previsit and postvisit | | has a running time ofO( V + E ), linear in the size of its input. This is as efficient as we needAB to explore a labyrinthCD is a ball of string and a piece of chalk. The chalk prevents looping, | | | | could possibly hope for, since it takes this long even just to read the adjacency list. by marking the junctions you have already visited.B The stringE 4,9 always takes12,21 D you back to the previsit(v) postvisit(v) starting place, enabling you to return to passages2,3 that you previously saw but did not yet Figure 3.6 shows the outcome of depth-first search on a 12-node graph, once again break- investigate.E F G H andpre whenever[v] = clock you arriveing ties at alphabetically any junctionpost (ignore (vertex)[v] the = pairsclock there of numbersare a variety for the of time passages being). The (edges) outer you loop of DFS I 5,8 13,20 H How can we simulate these two primitives, chalk and string, on a computer? The chalk can follow. A carelesscalls choice explore of passages three times, might onA lead,C, and youfinally aroundF . As in a circles result, there or might are three cause trees, you each marks are easy: for each vertex, maintain a Boolean variable indicating whether it has been toclock overlook++ some accessiblerooted at partone of of these the startingclock maze.++ points. Clearly, Together you need they to constitute record asome forest intermediate. visitedI already.JK As for the ball ofL string, the correct cyberanalogJ is a14,17 stackG. After all,L the exact 6,7 18,19 information during exploration. role of the string is to offer two primitive operations—unwind to get to a new junction (the 3.2.3 Connectivity in undirected graphs stack equivalent is to push the new vertex) and rewind to return to the previous junction (pop PropertyThis classic challenge has amused people for centuries. Everybody knows that all you K the stack). An undirected graph is if there is a path between any pair of vertices. The graph 15,16 needFor any to explore two vertices a labyrinth u and is v, a ballthe intervals of string connected[ andpre[ au] piece, post of[u chalk.]] and The chalk prevents looping, Instead of explicitly maintaining a stack, we will do so implicitly via recursion (which by marking the junctionsof Figure you 3.6 have is notalready connected visited. because, The for instance, string always there is notakes path you from backA toK to. However, the it is implemented using a stack of activation records). The resulting algorithm is shown in [pre[v], post[v]] aredoes either have disjoint, three disjoint or one connected is contained regions, entirelycorresponding within to the following sets of vertices: A connected of a graph is a maximal subgraph where starting place, enabling you to return to passages that you previously saw but did not yet 90 investigate.another. Thesethere regions is path are called between connected any components two vertices: each in of themthe subgraph, is a subgraph i.e., that it is is internally a A,B,E,I,J C,D,G,H,K,L F connected but has no edges to the remaining vertices. When explore is started at a particular How can we simulate these two primitives,{ chalk}{ and string, on a}{ computer?} The chalk vertex,maximal it identiconnectedfies precisely subgraph the connected. component containing that vertex. And each time marks are easy: for each vertex, maintain a Boolean variable92 indicating whether it has been the DFS outer loop calls explore, a new connected component is picked out. 7 / 30 visited already. As for the ball of string, the correct cyberanalog is a stack8 / 30. After all, the exact Thus depth-first search is trivially adapted to check if a graph is connected and, more generally, to assign each nodev an integer ccnum[v] identifying the connected component to role of the string is to offer two primitive operations—unwind to get to a new junction (the which it belongs. All it takes is stack equivalent is to push the new vertex) and rewind to return to the previous junction (pop the stack). procedure previsit(v) ccnum[v] = cc Instead of explicitly maintaining a stack, we will do so implicitly via recursion (which is implemented using a stack of activation records). The resulting algorithm is shown in where cc needs to be initialized to zero and to be incremented each time the DFS procedure calls explore. 90 3.2.4 Previsit and postvisit orderings We have seen how depth-first search—a few unassuming lines of code—is able to uncover the connectivity structure of an undirected graph in just linear time. But it is far more versatile than this. In order to stretch it further, we will collect a little more information during the ex- ploration process: for each node, we will note down the times of two important events, the mo- ment offirst discovery (corresponding to previsit) and that offinal departure (postvisit). Figure 3.6 shows these numbers for our earlier example, in which there are 24 events. The fifth event is the discovery ofI. The 21st event consists of leavingD behind for good. One way to generate arrays preand post with these numbers is to define a simple counter clock, initially set to 1, which gets updated as follows.

procedure previsit(v) pre[v] = clock clock = clock + 1

93 Tree edges are actually part of the DFS forest. DFS tree lead from a node to a descendant Forward edges nonchild A in the DFS tree.

Forward Back edges lead to an ancestor in the DFS tree.

Back Cross edges lead to neither descendant nor ancestor; they B therefore lead to a node that has already been completely explored (that is, already postvisited). Tree C D Cross

Figure 3.7 has two forward edges, two back edges, and two cross edges. Can you spot them?

Ancestor and descendant relationships, as well as edge types, can be read off directly from pre and post numbers. Because of the depth-first exploration strategy, vertexu is an ancestor of vertexv exactly in those cases whereu is discoveredfirst andv is discovered during explore(u). This is to say pre(u)< pre(v)< post(v)< post(u), which we can depict pictorially as two nested intervals:

Intro DFS Biconnectivity DAGs SCC BFS Paths and Matrices Intro DFS Biconnectivity DAGs SCC BFS Paths and Matrices u v v u DFS of Directed Graph DFSThe and case Edge of descendants Types is symmetric, sinceu is a descendant ofv if and only ifv isanan- cestor ofu. And since edge categories are based entirely on ancestor-descendant relationships, Tree edges are actually part of the DFS forest. it follows that they, too, can be read off from pre and post numbers. Here is a summary of Figure 3.7 DFS on a directed graph. DFS tree the various possibilities for an edge(u, v): lead from a node to a descendant Forward edges nonchild A in the DFS tree. A 1,16 pre/post ordering for(u, v) Edge type

B A C Forward Tree/forward Back edges lead to an ancestor in the DFS tree. u v v u B 2,11 C 12,15 Back Back Cross edges lead to neither descendant nor ancestor; they B v u u v E thereforeF leadD to a node that has alreadyE 3,10 beenD completely13,14 Cross explored (that is, already postvisited). Tree vu uv C D You canCross confirm each of these characterizations by consulting the diagram of edge types. Do 4,7 F H 8,9 you see why no other orderings are possible? G H No cross edges in undirected graphs! 5,6 G 3.3.2 Directed acyclic graphs Figure 3.7 has two forward edges, two back edges, and two cross edges. Can youBack spot and them? forward edges merge A in a directed graph is a circular pathv v v v v . Figure 3.7 has 0 → 1 → 2 →···→ k → 0 9 / 30 quite a few of them, for example,B E F B. A graph without cycles10 / 30 is acyclic. It turns Ancestor and descendant relationships, as well as edge types, can be read off directly → → → procedure postvisit(v) out we can test for acyclicity in linear time, with a single depth-first search. post[v] =from clock pre and post numbers. Because of the depth-first exploration strategy, vertexu is an 95 clockIntro = DFS clock ancestorBiconnectivity + DAGs 1 of SCC vertexBFS Paths andv Matricesexactly in those cases whereu is discoveredfirstIntro DFS and Biconnectivityv is discoveredDAGs SCC BFS Paths and Matrices during explore(u). This is to say pre(u)< pre(v)< post(v)< post(u), which we can Biconnectivitydepict pictorially in Undirected as two nested Graphs intervals: Illustration of Biconnected Components These timings will soon take on larger significance. Meanwhile, you might have noticed from Figure 3.4 that:

Property For any nodes u and v, the two intervals [pre(u),upost(u)] vand [pre v(v), post u (v)] are either disjointDefinition or one is (Biconnected contained within Components) the other. The case of descendants is symmetric, sinceu is a descendant ofv if and only ifv isanan- Why? Because[pre(u), post(u)] is essentially the time during which vertexu was on the An articulationu point in a graph is any vertex a that lies on every stack. The last-in,cestorfirst-out of behavior. And since of a stack edge explains categories the rest. are based entirely on ancestor-descendant relationships, pathit from follows between that vertices they, too, v, w. can be read off from pre and post numbers. Here is a summary of (u, v) 3.3 Depth-A biconnectedthefirst various search graph possibilities in contains directed no for articulation an graphs edge points.: A of a graph is a maximal subgraph that 3.3.1 Types of edges pre/post ordering for(u, v) Edge type is biconnected. Our depth-first search algorithm can be run verbatim on directed graphs, taking careTree/forward to tra- verse edges only in their prescribed directions. Figureu 3.7 v shows an v exampleu and the search tree that results when vertices are considered in lexicographic order. Each biconnected component given a dierent color In further analyzing the directed case, it helps to have terminology for important relation-Back Articulation points have multiple colors ships between nodes of a tree.Aisthe root of the searchv tree; u everything u else v is its descendant. Similarly,E has descendantsF,G, andH, and conversely, is an ancestor of these three11 / 30 nodes. 12 / 30 The family analogy is carried further:C isthe parent ofD, which is its child. Cross vu uv For undirected graphs we distinguished between tree edges and nontree edges. In the directed case, there is a slightly more elaborate taxonomy: You can confirm each of these characterizations by consulting the diagram of edge types. Do you see why no other orderings are possible? 94 3.3.2 Directed acyclic graphs A cycle in a directed graph is a circular pathv v v v v . Figure 3.7 has 0 → 1 → 2 →···→ k → 0 quite a few of them, for example,B E F B. A graph without cycles is acyclic. It turns → → → out we can test for acyclicity in linear time, with a single depth-first search.

95 Intro DFS Biconnectivity DAGs SCC BFS Paths and Matrices Intro DFS Biconnectivity DAGs SCC BFS Paths and Matrices Biconnected Graphs Finding articulation points during DFS

During DFS visit of vertex v, we want to decide if it is an articulation Biconnected components are not disjoint. point or not. Divide V into to disjoint sets:

Articulation points are duplicated across them. Vi includes nodes inside the DFS tree rooted at v, and V = V − V of nodes outside the DFS tree rooted at v. Articulation points ≡ single-points of failure o i Graph is disconnected if any of them go down. Observation (1) Suppose that every inside node v (i.e., v ∈ V − {v}) can reach some Between any two u, v ∈ V in a there are two i i i outside node v (i.e., v ∈ V ) without going through v. Then v is not node-disjoint paths (and hence a cycle). o o o an articulation point.

13 / 30 14 / 30

Intro DFS Biconnectivity DAGs SCC BFS Paths and Matrices Intro DFS Biconnectivity DAGs SCC BFS Paths and Matrices Finding articulation points during DFS (2) Finding articulation points during DFS (3)

Proof:

Paths within Vo: By properties of DFS tree, any two vertices in Vo can reach each other without going through v Observation (2)

Paths between vi and Vo: Once vi can reach any node in Vo, it can reach If Observation (1) does not hold then v is an articulation point.

all other nodes in Vo without having to go through v. This means that there exists some vi that cannot reach an outside Paths within V : Consider nodes v and v0 in V − {v}. By our assumption i i i i node vo without going through v. By definition, this means that v is 0 there is a path from vi to some vo ∈ Vo, and another path from vi to an articulation point. 0 0 0 some vo ∈ Vo, neither involving v. Consider the path vi to vo to vo to vi , which does not pass through v.

Thus, for every pair of vertices, there is a path between them that avoids v, and hence v is not an articulation point. 15 / 30 16 / 30 Intro DFS Biconnectivity DAGs SCC BFS Paths and Matrices Intro DFS Biconnectivity DAGs SCC BFS Paths and Matrices Finding articulation points during DFS (3) Finding articulation points during DFS (4)

We combine and slightly strengthen Observations (1), (2), while Note than any such path from x to y will follow zero or more tree omitting the proof, as it is essentially the same as before. edges down, followed by a back edge. Observation (3) If this path bypasses v, this is going to occur due to a single Let Y denote the set of ancestors of v and X its immediate children back-edge that goes to an ancestor of v. So there is no need to in the DFS tree. Now, v is not an articulation point i each x ∈ X has consider paths with multiple back-edges. a path to some y ∈ Y without going through v. Note: Pre-number increases while following tree edges down, while By focusing on just the children and ancestors of v, this makes it it decreases when a back edge is followed. easier to decide on articulation points during DFS. Key Idea: During DFS, for each vertex x, maintain the highest ancestor that can be reached from x by following tree edges down Note than any such path from x to y will follow zero or more tree and then a back edge. edges down, followed by a back edge.

17 / 30 18 / 30

Intro DFS Biconnectivity DAGs SCC BFS Paths and Matrices Intro DFS Biconnectivity DAGs SCC BFS Paths and Matrices Finding articulation points during DFS (5) Finding articulation points during DFS (6)

Key Idea: During DFS, maintain the highest ancestor reachable from x by Key Idea: Maintain low during DFS following tree edges down and then a back edge. when visiting x, initialize low[x] to pre[x]. This info is maintained in the array low. when a DFS call on a child x0 of x returns, check if low[x] ≤ low[x0] for every child x0 of x in DFS tree. low[x0] ≥ pre[x]. This captures the fact that we can follow the tree edge down from x to If so, the highest ancestor x0 can reach is not higher than x, i.e., x0 0 0 x , then go to whichever ancestor is reachable from x . cannot go to ancestors of x without going through x. 0 0 Algorithmically, let low[x] = min(low[x], low[x ]) when explore(x ) So, mark x as articulation point returns If an adjacent vertex x00 is already visited when x considers it, set [ ] ≤ [ 00] 00 low x pre x for x adjacent to x but not a parent or child in the 00 00 DFS tree. low[x] = min(low[x], pre[x ]) unless x is parent of x Algorithmically, set low[x] = min(low[x], pre[x00]) All these can be done during a DFS, while increasing the cost of each By properties of DFS, x00 is either a descendant or ancestor of x. step by only a constant amount — so, overall complexity is 00 As we are taking min, statement eective only if x is an ancestor. O(|E| + |V|) 19 / 30 20 / 30 Intro DFS Biconnectivity DAGs SCC BFS Paths and Matrices Intro DFS Biconnectivity DAGs SCC BFS Paths and Matrices Directed Acyclic Graphs (DAGs) Strongly Connected Components (SCC)

Analog of connected components for undirected graphs A directed graph that contains no cycles. Definition (SCC) Often used to represent (acyclic) dependencies, partial orders,... Two vertices u and v in a directed graph are connected if there is Property (DAGs and DFS) a path from u to v and vice-versa. A directed graph has a cycle i its DFS reveals a back edge. A directed graph is strongly connected if any pair of vertices in the In a dag, every edge leads to a vertex with lower post number. graph are connected. Every dag has at least one source and one sink. A subgraph of a directed graph is said to be an SCC if it is a maximal subgraph that is strongly connected.

SCCs are also similar to biconnected components!

21 / 30 22 / 30

Intro DFS Biconnectivity DAGs SCC BFS Paths and Matrices Intro DFS Biconnectivity DAGs SCC BFS Paths and Matrices

FigureSCC 3.9 (a)Example A directed graph and its strongly connected components. (b) The meta-graph. Breadth-first Search (BFS)

(a) (b) A B C

Traverse the graph by “levels” D E F A B,E C,F BFS(v) visits v first Then it visits all immediate children of v G H G,H,I D then it visits children of children of v, and so on. J,K,L

I J K As compared to DFS, BFS uses a queue (rather than a stack) to remember vertices that still need to be explored

L

The textbook describes an algorithm for computing SCC in 3.4.2linear-time An efficient using algorithm DFS. 23 / 30 24 / 30 The decomposition of a directed graph into its strongly connected components is very infor- mative and useful. It turns out, fortunately, that it can be found in linear time by making further use of depth-first search. The algorithm is based on some properties we have already seen but which we will now pinpoint more closely.

Property 1 If the explore subroutine is started at node u, then it will terminate precisely when all nodes reachable from u have been visited. Therefore, if we call explore on a node that lies somewhere in a sink strongly connected component (a strongly connected component that is a sink in the meta-graph), then we will retrieve exactly that component. Figure 3.9 has two sink strongly connected components. Starting explore at nodeK, for instance, will completely traverse the larger of them and then stop. This suggests a way offinding one strongly connected component, but still leaves open two major problems: (A) how do wefind a node that we know for sure lies in a sink strongly con- nected component and (B) how do we continue once thisfirst component has been discovered? Let’s start with problem (A). There is not an easy, direct way to pick out a node that is guaranteed to lie in a sink strongly connected component. But there is a way to get a node in a source strongly connected component. Property 2 The node that receives the highest post number in a depth-first search must lie in a source strongly connected component. This follows from the following more general property.

Property 3 If C and C� are strongly connected components, and there is an edge from a node

98 Intro DFS Biconnectivity DAGs SCC BFS Paths and Matrices Intro DFS Biconnectivity DAGs SCC BFS Paths and Matrices Figure 4.2 A physical model of a graph. BFS Algorithm BFS Algorithm Illustration S

ES A bfs(V, E, s) A C DE foreach u ∈ V do visited[u] = false DC B q = {s}; visited[s] = true Figure 4.4 The result of breadth-first search on the graph of FigureB 4.1. while q is nonempty do u = deque(q) Order Queue contents Breadth-of visitationfirst search.after processing node S foreach edge (u, v) ∈ E do Figure 4.3 [S] if not visited[v] then procedure bfs(G,S s) [ACDE] Input: GraphAG=(V, E),[CDEB directed] or undirected; vertexs V queue(q, v); visited[v] = true A C ∈ D E Output: For allC vertices[Du E Breachable] froms, dist(u) is set D [E B] to theE distance[ fromB] s tou. B [] B for allu V: 25 / 30 ∈ 26 / 30 dist(u) = ∞ right thing. IfS is the starting point and the nodes are ordered alphabetically, they get visited dist(s)=0 in the sequence shown in Figure 4.4. The breadth-first search tree, on the right, contains the Intro DFS Biconnectivity DAGs SCC BFS Paths and Matrices Q=[s] (queueedgesIntro containing DFS through Biconnectivity which DAGs justeach SCC BFS nodes )Paths is and initially Matrices discovered. Unlike the DFS tree we saw earlier, it has the property that all its paths fromS are the shortest possible. It is therefore a shortest- whileQ is notpath tree empty:. Shortest Paths and BFS u= eject(QGraph) paths and Boolean Matrices for allCorrectness edges(u, v) andE: efficiency ∈ BFS automatically computes shortest paths! if dist(v)= : We have developed∞ the basic intuition behind breadth-first search. In order to check that injectthe algorithm(Q, v) works correctly, we need to make sure that it faithfully executes this intuition. bfs(V, E, s) distWhatA( graphv)= we expect, dist and precisely,its(u) boolean + 1 is that matrix representation foreach u ∈ V do dist[u] = ∞ For eachd=0,1,2,..., there is a moment at which (1) all nodes at distance d ≤ q = {s}; dist[s] = 0 froms have their distances correctly set; (2) all other nodes have their distances set to ; and (3) the queue contains exactly the nodes at distanced. while q is nonempty do 1 ∞ 2 3 4.2 Breadth-This hasfi beenrst phrased search with an inductive argument in mind. We 0 have 1 already 0 0  discussed both u = deque(q) the base case and the inductive step. Can youfill in the details?  1 0 1 1    foreach edge (u, v) ∈ E do In Figure 4.2, the lifting ofs partitions the graph into layers:A =s itself, the nodes at distance The overall running time of this algorithm is linear,O( V + 0E ), 0 for 0 exactly 1 the same | | | |  if dist[v] = ∞ then 1 from it, the nodesreasons at as depth- distancefirst search.2 from Each it, and vertex so is on. put on A the convenient queue exactly0 way 0 once, 0 to when0 compute it isfirst distances en- froms to the othercountered, vertices so there is are to2 proceedV queue operations. layer by The layer. rest Once of the work we have is done picked in the algorithm’s out the nodes queue(q, v); dist[v] = dist[u] + 1 | | 4 at distance0,1,innermost2,...,d, theloop. ones Over at thed+1 course are of easily execution, determined: this loop looks they at each are edge precisely once (in the directed as-yet- graphs) or twice (in undirected graphs), and therefore takesO( E ) time. But not all paths are created equal! We would like to compute unseen nodes that are adjacent to the layer at distanced. This suggests| | an iterative algorithm in which two layers are active at any given time: some layerd, which has been fully identified, shortest weighted path — a topic of future lecture. Now that we have both BFS and DFS before us: how do their exploration styles compare? andd+1, whichDepth- is beingfirst search discovered makes deep by incursions scanning into the a graph, neighbors retreating of layer only whend. it runs out of new 27 / 30 Breadth-firstnodes search to visit. (BFS) This strategy directly gives implements it the wonderful, this subtle, simple and reasoning extremely useful (Figure28 properties / 30 4.3). Ini- we saw in the Chapter 3. But it also means that DFS can end up taking a long and convoluted tially the queuerouteQ consists to a vertex only that is of actuallys, the very one close node by, at as distance in Figure 4.1.0. And Breadth- forfi eachrst search subsequent makes distanced=1,2sure,3,... to, visit there vertices is a in point increasing in time order at of which their distanceQ contains from the all starting the nodes point. at This distance is a d and nothingbroader, else.shallower As these search, nodes rather are like processed the propagation (ejected of a wave off the upon front water. of And the it is queue), achieved their as-yet-unseenusing neighbors almost exactly are injected the same into code the as DFS—but end of the with queue. a queue in place of a stack. Let’s try out this algorithm on our earlier example111 (Figure 4.1) to confirm that it does the

110 Intro DFS Biconnectivity DAGs SCC BFS Paths and Matrices Intro DFS Biconnectivity DAGs SCC BFS Paths and Matrices Graph paths and Boolean Matrices Shortest paths and Matrix Operations

 0 1 0 0   1 0 1 1    A =   Let A be the adjacency matrix for a  0 0 0 1  0 0 0 0 graph G, and B = A × A. Now, Bij = 1 i there is path in the graph of length Redefine operations on matrix elements so that + becomes min,  1 0 1 1  2 from vi to vj  0 1 0 1  and ∗ becomes integer addition. A2 =    0 0 0 0  ∗ Let C = A + B. Then Cij = 1 i there is   D = A then Dij = k i the shortest path from vj to vi is of length 0 0 0 0 path of length ≤ 2 between vi and vj k

Define A∗ = A0 + A1 + A2 + ··· . If  0 1 0 1  ∗  1 0 1 1  D = A then Dij = 1 i vj is reachable A3 =    0 0 0 0  from vi.   0 0 0 0

29 / 30 30 / 30