1,[a]

ee.usc.edu

1,[a]

CSCI 102Graph Representation, Traversals, &AlgorithmsMark RedekoppMichael Crowley


Origin of Graph Theory‣ In 1736, Euler solved the problem known as the SevenBridges of Königsberg. The city of Königsberg, Prussia onthe Pregel River, included two large islands connected toeach other and the mainland by seven bridges.‣ The problem is to decide whether it is possible to followa path that crosses each bridge exactly once and returns tothe starting point.http://en.wikipedia.org/wiki/Seven_Bridges_of_K%C3%B6nigsberg© Copyright 2013 Brent Nash & Mark Redekopp, All Rights Reserved2


Euler’s Analysis‣ Whenever you enter a non-terminal landmass by a bridgeyou must leave by another‣ The number of times you visit a non-terminal landmassmust be equal to the number of times you leave it‣ Thus every non-terminal landmass must be touching aneven number of bridges– So that you can enter on one bridge and leave on another‣ However, all four of the land masses in the originalproblem are touched by an odd number of bridges (one istouched by 5 bridges, and each of the other three aretouched by 3).© Copyright 2013 Brent Nash & Mark Redekopp, All Rights Reserved3


Explanation Using Graph Theory‣ In "graph-speak", Euler showed that the possibility of awalk through a graph, traversing each edge exactly once,depends on the degrees of the nodes.– The degree of a node is the number of edges touching it.‣ Euler's argument shows that a necessary condition for thewalk of the desired form is that the graph be connectedand have exactly zero or two nodes of odd degree.– If there are 2 nodes of odd degree, then any Eulerian path will startat one of them and end at the other‣ Since the graph corresponding to historical Königsberg hasfour nodes of odd degree, it cannot have an Eulerian path.‣ Such a walk is now called an Eulerian path or Euler walk inhis honor© Copyright 2013 Brent Nash & Mark Redekopp, All Rights Reserved4


List of VerticesAdjacency ListsGraph Representations‣ Graphs are really just a list of lists– List of vertices each having their own list ofadjacent vertices‣ Alternatively, sometimes graphs are alsorepresented with an adjacency matrix– Entry at (i,j) = 1 if there is an edge betweenvertex i and j, 0 otherwiseaebcdfhgabcdefghc,ec,ha,b,d,e,gc,fa,c,fd,e,gc,f,hb,g© Copyright 2013 Brent Nash & Mark Redekopp, All Rights Reserveda b c d e f g ha 0 0 1 0 1 0 0 0b 0 0 1 0 0 0 0 1c 1 1 0 1 1 0 1 0d 0 0 1 0 0 1 0 0e 1 0 1 0 0 1 0 0f 0 0 0 1 1 0 1 0g 0 0 1 0 0 1 0 1h 0 1 0 0 0 0 1 0Adjacency Matrix Representation6


List of VerticesAdjacency ListsGraph Representations‣ Let |V| = # of vertices and |E| = # of edges‣ Adjacency List Representation– O(|V| + |E|) memory storage– Existence of an edge requires searching adjacency list‣ Adjacency Matrix Representation– O(|V| 2 ) storage– Existence of an edge requires O(1) lookup (e.g.matrix[i][j] == 1 )aebcdfhgabcdefghc,ec,ha,b,d,e,gc,fa,c,fd,e,gc,f,hb,g© Copyright 2013 Brent Nash & Mark Redekopp, All Rights Reserveda b c d e f g ha 0 0 1 0 1 0 0 0b 0 0 1 0 0 0 0 1c 1 1 0 1 1 0 1 0d 0 0 1 0 0 1 0 0e 1 0 1 0 0 1 0 0f 0 0 0 1 1 0 1 0g 0 0 1 0 0 1 0 1h 0 1 0 0 0 0 1 0Adjacency Matrix Representation8


List of VerticesAdjacency ListsGraph Representations‣ Can 'a' get to 'b' in two hops?‣ Adjacency List– For each neighbor of a…– Search that neighbor's list for b‣ Adjacency Matrix– Take the dot product of row a & column baebcdfhgabcdefghc,ec,ha,b,d,e,gc,fa,c,fd,e,gc,f,hb,g© Copyright 2013 Brent Nash & Mark Redekopp, All Rights Reserveda b c d e f g ha 0 0 1 0 1 0 0 0b 0 0 1 0 0 0 0 1c 1 1 0 1 1 0 1 0d 0 0 1 0 0 1 0 0e 1 0 1 0 0 1 0 0f 0 0 0 1 1 0 1 0g 0 0 1 0 0 1 0 1h 0 1 0 0 0 0 1 0Adjacency Matrix Representation9


List of VerticesAdjacency ListsGraph Representations‣ Can 'a' get to 'b' in two hops?‣ Adjacency List– For each neighbor of a…– Search that neighbor's list for b‣ Adjacency Matrix– Take the dot product of row a & column baebcdfhgabcdefghc,ec,ha,b,d,e,gc,fa,c,fd,e,gc,f,hb,g© Copyright 2013 Brent Nash & Mark Redekopp, All Rights Reserveda b c d e f g ha 0 0 1 0 1 0 0 0b 0 0 1 0 0 0 0 1c 1 1 0 1 1 0 1 0d 0 0 1 0 0 1 0 0e 1 0 1 0 0 1 0 0f 0 0 0 1 1 0 1 0g 0 0 1 0 0 1 0 1h 0 1 0 0 0 0 1 0Adjacency Matrix Representation10


BC Algorithm Overview‣ What's the most central vertex(es) in the graph below?‣ How do we define "centrality"?‣ Betweeness centrality defines "centrality" as the nodes thatare between the most other pairsSample GraphmacdejihlkbfGraph 1 Graph 2© Copyright 2013 Brent Nash & Mark Redekopp, All Rights Reserved11


BC Algorithm Overview‣ Betweeness centrality (BC) defines "centrality" as the nodesthat are between (i.e. on the path between) the most otherpairs of vertices‣ BC considers betweeness on only "shortest" paths!‣ To compute centrality score for each vertex we need to findshortest paths between all pairs…– Use the Breadth-First Search (BFS) algorithm to do thisSample GraphAre these gray nodes'between' a and e?acdeacdeNo, a-c-d-e is theshortest path?bfbfOriginal 1© Copyright 2013 Brent Nash & Mark Redekopp, All Rights ReservedOriginal w/added path12


BREADTH-FIRST SEARCH© Copyright 2013 Brent Nash & Mark Redekopp, All Rights Reserved13


Breadth-First Search‣ Given a graph with vertices, V, andedges, E, and a starting vertex thatwe'll call u0bch‣ BFS starts at u (‘a’ in the diagram tothe left) and fans-out along theedges to nearest neighbors, then totheir neighbors and so on‣ Goal: Find shortest paths (a.k.a.minimum number of hops or depth)from the start vertex to every othervertexadefDepth 0: ag© Copyright 2013 Brent Nash & Mark Redekopp, All Rights Reserved14


Breadth-First Search‣ Given a graph with vertices, V, andedges, E, and a starting vertex, u‣ BFS starts at u (‘a’ in the diagram tothe left) and fans-out along theedges to nearest neighbors, then totheir neighbors and so on‣ Goal: Find shortest paths (a.k.a.minimum number of hops or depth)from the start vertex to every othervertex0a1eb1cdDepth 0: aDepth 1: c,efhg© Copyright 2013 Brent Nash & Mark Redekopp, All Rights Reserved15


Breadth-First Search‣ Given a graph with vertices, V, andedges, E, and a starting vertex, u‣ BFS starts at u (‘a’ in the diagram tothe left) and fans-out along theedges to nearest neighbors, then totheir neighbors and so on‣ Goal: Find shortest paths (a.k.a.minimum number of hops or depth)from the start vertex to every othervertex0a12eb12cdDepth 0: aDepth 1: c,eDepth 2: b,d,f,g2fhg2© Copyright 2013 Brent Nash & Mark Redekopp, All Rights Reserved16


Breadth-First Search‣ Given a graph with vertices, V, andedges, E, and a starting vertex, u‣ BFS starts at u (‘a’ in the diagram tothe left) and fans-out along theedges to nearest neighbors, then totheir neighbors and so on‣ Goal: Find shortest paths (a.k.a.minimum number of hops or depth)from the start vertex to every othervertex0a12eb12cdDepth 0: aDepth 1: c,eDepth 2: b,d,f,gDepth 3: h2f3hg2© Copyright 2013 Brent Nash & Mark Redekopp, All Rights Reserved17


Developing the Algorithm‣ Key idea: Must explore all nearerneighbors before exploring furtherawayneighbors‣ From ‘a’ we find ‘e’ and ‘c’– Computer can only do one thing at atime so we have to pick either e or c toexplore from– Let’s say we pick e…we will find f– Now what vertex should we explore(i.e. visit neighbors) next?– C!! (if we don’t we won’t find shortestpaths…e.g. d)– Must explore all vertices at depth ibefore any vertices at depth i+1© Copyright 2013 Brent Nash & Mark Redekopp, All Rights Reserved0a1eb1c2dDepth 0: aDepth 1: c,eDepth 2: b,d,f,gDepth 3: hfhg18


Breadth-First SearchAlgorithm:BFS(G,u)1 for each vertex v2 v.pred = -13 Q = new Queue4 Add u to Q, v.pred = 05 while Q is not empty6 x = Q.front(); Q.pop_front()7 foreach neighbor, y, of x:8 if y.pred == -1 // y not found9 Q.push_back(y)10 y.pred = x0a-1Q:-1eb-1c-1-1df-1hg-1a© Copyright 2013 Brent Nash & Mark Redekopp, All Rights Reserved20


Breadth-First SearchAlgorithm:BFS(G,u)1 for each vertex v2 v.pred = -13 Q = new Queue4 Add u to Q, v.pred = 05 while Q is not empty6 x = Q.front(); Q.pop_front()7 foreach neighbor, y, of x:8 if y.pred == -1 // y not found9 Q.push_back(y)10 y.pred = xX =0aaa-1eba-1c-1df-1hg-1Q:ec© Copyright 2013 Brent Nash & Mark Redekopp, All Rights Reserved21


Breadth-First SearchAlgorithm:BFS(G,u)1 for each vertex v2 v.pred = -13 Q = new Queue4 Add u to Q , v.pred = 05 while Q is not empty6 x = Q.front(); Q.pop_front()7 foreach neighbor, y, of x:8 if y.pred == -1 // y not found9 Q.push_back(y)10 y.pred = xX =0aea-1eba-1cedf-1hg-1Q:cf© Copyright 2013 Brent Nash & Mark Redekopp, All Rights Reserved22


Breadth-First SearchAlgorithm:BFS(G,u)1 for each vertex v2 v.pred = -13 Q = new Queue4 Add u to Q , v.pred = 05 while Q is not empty6 x = Q.front(); Q.pop_front()7 foreach neighbor, y, of x:8 if y.pred == -1 // y not found9 Q.push_back(y)10 y.pred = xX =0aaccebaccedf-1chgQ:c fbdg© Copyright 2013 Brent Nash & Mark Redekopp, All Rights Reserved23


Breadth-First SearchAlgorithm:BFS(G,u)1 for each vertex v2 v.pred = -13 Q = new Queue4 Add u to Q , v.pred = 05 while Q is not empty6 x = Q.front(); Q.pop_front()7 foreach neighbor, y, of x:8 if y.pred == -1 // y not found9 Q.push_back(y)10 y.pred = xX =0aafcebaccedf-1chgQ:b cdg© Copyright 2013 Brent Nash & Mark Redekopp, All Rights Reserved24


Breadth-First SearchAlgorithm:BFS(G,u)1 for each vertex v2 v.pred = -13 Q = new Queue4 Add u to Q , v.pred = 05 while Q is not empty6 x = Q.front(); Q.pop_front()7 foreach neighbor, y, of x:8 if y.pred == -1 // y not found9 Q.push_back(y)10 y.pred = xX =0aabcebaccedfbchgQ:d cgh© Copyright 2013 Brent Nash & Mark Redekopp, All Rights Reserved25


Breadth-First SearchAlgorithm:BFS(G,u)1 for each vertex v2 v.pred = -13 Q = new Queue4 Add u to Q , v.pred = 05 while Q is not empty6 x = Q.front(); Q.pop_front()7 foreach neighbor, y, of x:8 if y.pred == -1 // y not found9 Q.push_back(y)10 y.pred = xX =0aadcebaccedfbchgQ:g ch© Copyright 2013 Brent Nash & Mark Redekopp, All Rights Reserved26


Breadth-First SearchAlgorithm:BFS(G,u)1 for each vertex v2 v.pred = -13 Q = new Queue4 Add u to Q , v.pred = 05 while Q is not empty6 x = Q.front(); Q.pop_front()7 foreach neighbor, y, of x:8 if y.pred == -1 // y not found9 Q.push_back(y)10 y.pred = xX =0aagcebaccedfbchgQ:h c© Copyright 2013 Brent Nash & Mark Redekopp, All Rights Reserved27


Breadth-First SearchAlgorithm:BFS(G,u)1 for each vertex v2 v.pred = -13 Q = new Queue4 Add u to Q5 while Q is not empty6 x = Q.front(); Q.pop_front()7 foreach neighbor, y, of x:8 if y.pred == -1 // y not found9 Q.push_back(y)10 y.pred = xX =0aahcebaccedfbchgQ:© Copyright 2013 Brent Nash & Mark Redekopp, All Rights Reserved28


Breadth-First Search‣ Shortest paths can be found bewalking predecessor value from anynode backward‣ Example:– Shortest path from a to h– Start at h– Pred[h] = b (so walk back to b)0aacbaccdbchg– Pred[b] = c (so walk back to c)– Pred[c] = a (so walk back to a)eef– Pred[a] = 0 … no predecessor, Done!!© Copyright 2013 Brent Nash & Mark Redekopp, All Rights Reserved29


BC REVISITED© Copyright 2013 Brent Nash & Mark Redekopp, All Rights Reserved30


BC Algorithm Overview‣ Betweeness-Centrality determines "centrality" as the numberof shortest paths from all-pairs upon which a vertex lies‣ Consider the sample graph below– Each external vertex (a, b, e, f) lies is a member of only the shortestpaths between itself and each other vertex– Vertices c and d lie on greater number of shortest paths and thus willbe scored higherSample GraphacbdefImage each vertex is aball and each edge is achain or string. Whatwould this graph looklike if you picked it upby vertex c? Vertex a?© Copyright 2013 Brent Nash & Mark Redekopp, All Rights Reserved31


BC Implementation‣ Based on Brandes' formulation for unweighted graphs– Perform |V| Breadth-first traversals– Traversals results in a subgraph consisting of shortest paths from rootto all other vertices– Messages are then sent back up the subgraph from "leaf" vertices tothe root summing the percentage of shortest-paths each vertex is amember of– Summing a vertex's score from each traversal yields overall BC resultSample Graph withfinal BC scores55ab19 19c© Copyright 2013 Brent Nash & Mark Redekopp, All Rights Reserveddef55Traversals fromselected rootsand resultingpartial BC scores(in this case, thenumber ofdescendants)ac540b d20 0e fc50 0 2a b d0e f032


BC Implementation‣ As you work down, track # of shortest paths running througha vertex and its predecessor(s)‣ On your way up, sum the nodes beneath1,[c]b50ac1,[-]1,[a]41,[c]d21,[d] 1,[d]e f0 0.5*2© Copyright 2013 Brent Nash & Mark Redekopp, All Rights Reserveda1,[a] 4 1,[a]b c.5*2 .5*22, [b,c]d12,[d]e01,[-]# of shortest paths thru the vertex,[List of predecessor]Score on the way back up (ifmultiple shortest paths, split thescore appropriately)c4Traversals fromselected roots 0b d2and resultingpartial BC scores0 0(in this case, thee fnumber ofdescendants)a5c0 0 2a b d0e5f033


DEPTH FIRST SEARCH© Copyright 2013 Brent Nash & Mark Redekopp, All Rights Reserved34


DFS Application: Topological Sort‣ Breadth-first search doesn'tsolve all our problems.‣ Given a graph ofdependencies (tasks,prerequisities, etc.)topological sort creates aconsistent ordering of tasks(vertices) where nodependencies are violated‣ Many possible validtopological orderings exist– EE 101, EE 201, EE 357,EE 454, EE 457, CS102,PHYS 152, CS 201,…– CS 102, EE 101, MATH 126,EE 201L,…EE 101EE 201LEE 357EE 457 EE 454LCS 102 MATH126CS 201CS 402 CS 377CS 477a CS 460© Copyright 2013 Brent Nash & Mark Redekopp, All Rights Reserved35


Topological Sort‣ Another example– Getting dressed‣ More Examples:– Project managementscheduling– Build order in a Makefile orother compile project– Cooking using a recipe– Instruction execution on anout-of-order pipelined CPU– Production of output values ina simulation of acombinational gate networkSocksShoesUnderwearPantsBeltUndershirtShirtTieJackethttp://www.personal.kent.edu/~rmuhamma/Algorithms/MyAlgorithms/GraphAlgor/topoSort.htm© Copyright 2013 Brent Nash & Mark Redekopp, All Rights Reserved36


Topological Sort‣ Does breadth-firstsearch work?– No. What if we started at MATH126…– We'd go to CS 201L before CS102‣ All parent nodes need to becompleted before any childnode‣ BFS only guarantees someparent has completed beforechild‣ Turns out a Depth-FirstSearch will be part of oursolution© Copyright 2013 Brent Nash & Mark Redekopp, All Rights ReservedEE 101EE 201LEE 357EE 457 EE 454LCS 102 MATH126CS 201CS 402 CS 377CS 477a CS 46037


Depth First Search‣ Explores ALL childrenbefore completing aparent– Note: BFS completes a parentbefore ANY children‣ For DFS let us assign:– A start time when the node isfirst found– A finish time when a node iscompleted‣ If we look at our nodes inreverse order of finish time(i.e. last one to finish back tofirst one to finish) we arrive ata…– Topological ordering!!!© Copyright 2013 Brent Nash & Mark Redekopp, All Rights Reserved123EE 457EE 101EE 201LEE 357104 5 6 7Reverse Finish Time Order1298EE 454L11CS 1021312Start TimeFinish Time22 23 24CS 2011415MATH 12621CS 402 CS 37716 18172019CS 477a CS 460MATH 126, CS 102, CS 201, CS 377, CS 460, CS 477a, CS 402,EE 101, EE 201L, EE 357, EE 454L, EE 45738


DFS Algorithm‣ Visit a node– Mark as visited (started)– For each visited neighbor, visitit and perform DFS on all oftheir children– Only then, mark as finished‣ DFS is recursive!!‣ If cycles in the graph, ensurewe don’t get caught visitingneighbors endlessly– Color them as we go– White = unvisited,– Gray = visited but not finished– Black = finished© Copyright 2013 Brent Nash & Mark Redekopp, All Rights ReservedDFS-All (G)1 for each vertex u2 u.color = WHITE3 finish_list = empty_list4 for each vertex u do5 if u.color == WHITE then6 DFS-Visit (G, u, finish_list)7 return finish_listDFS-Visit (G, u)1 u.color = GRAY2 for each vertex v in Adj(u) do3 if v.color = WHITE then4 DFS-Visit (G, v)5 u.color = BLACK6 finish_list.append(u)39


Depth First-SearchDFS-All (G)1 for each vertex u2 u.color = WHITE3 finish_list = empty_list4 for each vertex u do5 if u.color == WHITE then6 DFS-Visit (G, u, finish_list)7 return finish_listghfedcbaDFS-Visit (G, u)1 u.color = GRAY2 for each vertex v in Adj(u) do3 if v.color = WHITE then4 DFS-Visit (G, v)5 u.color = BLACK6 finish_list.append(u)© Copyright 2013 Brent Nash & Mark Redekopp, All Rights Reserved40


Depth First-SearchDFS-All (G)1 for each vertex u2 u.color = WHITE3 finish_list = empty_list4 for each vertex u do5 if u.color == WHITE then6 DFS-Visit (G, u, finish_list)7 return finish_listghfedcvbauDFS-Visit (G, u)1 u.color = GRAY2 for each vertex v in Adj(u) do3 if v.color = WHITE then4 DFS-Visit (G, v)5 u.color = BLACK6 finish_list.append(u)© Copyright 2013 Brent Nash & Mark Redekopp, All Rights ReservedDFS-Visit(G,a):41


Depth First-SearchDFS-All (G)1 for each vertex u2 u.color = WHITE3 finish_list = empty_list4 for each vertex u do5 if u.color == WHITE then6 DFS-Visit (G, u, finish_list)7 return finish_listghfevducbaDFS-Visit (G, u)1 u.color = GRAY2 for each vertex v in Adj(u) do3 if v.color = WHITE then4 DFS-Visit (G, v)5 u.color = BLACK6 finish_list.append(u)© Copyright 2013 Brent Nash & Mark Redekopp, All Rights ReservedDFS-Visit(G,d):DFS-Visit(G,a):42


Depth First-SearchDFS-All (G)1 for each vertex u2 u.color = WHITE3 finish_list = empty_list4 for each vertex u do5 if u.color == WHITE then6 DFS-Visit (G, u, finish_list)7 return finish_listgvhfeudcbaDFS-Visit (G, u)1 u.color = GRAY2 for each vertex v in Adj(u) do3 if v.color = WHITE then4 DFS-Visit (G, v)5 u.color = BLACK6 finish_list.append(u)© Copyright 2013 Brent Nash & Mark Redekopp, All Rights ReservedDFS-Visit(G,f):DFS-Visit(G,d):DFS-Visit(G,a):43


Depth First-SearchDFS-All (G)1 for each vertex u2 u.color = WHITE3 finish_list = empty_list4 for each vertex u do5 if u.color == WHITE then6 DFS-Visit (G, u, finish_list)7 return finish_listguhfedcbaDFS-Visit (G, u)1 u.color = GRAY2 for each vertex v in Adj(u) do3 if v.color = WHITE then4 DFS-Visit (G, v)5 u.color = BLACK6 finish_list.append(u)DFS-Visit(G,h):© Copyright 2013 Brent Nash & Mark Redekopp, All Rights ReservedDFS-Visit(G,f):DFS-Visit(G,d):DFS-Visit(G,a):44


Depth First-SearchDFS-All (G)1 for each vertex u2 u.color = WHITE3 finish_list = empty_list4 for each vertex u do5 if u.color == WHITE then6 DFS-Visit (G, u, finish_list)7 return finish_listguhfedcbaDFS-Visit (G, u)1 u.color = GRAY2 for each vertex v in Adj(u) do3 if v.color = WHITE then4 DFS-Visit (G, v)5 u.color = BLACK6 finish_list.append(u)Finish_list:hDFS-Visit(G,h):DFS-Visit(G,f):DFS-Visit(G,d):© Copyright 2013 Brent Nash & Mark Redekopp, All Rights ReservedDFS-Visit(G,a):45


Depth First-SearchDFS-All (G)1 for each vertex u2 u.color = WHITE3 finish_list = empty_list4 for each vertex u do5 if u.color == WHITE then6 DFS-Visit (G, u, finish_list)7 return finish_listghvfeudcbaDFS-Visit (G, u)1 u.color = GRAY2 for each vertex v in Adj(u) do3 if v.color = WHITE then4 DFS-Visit (G, v)5 u.color = BLACK6 finish_list.append(u)Finish_list:h© Copyright 2013 Brent Nash & Mark Redekopp, All Rights ReservedDFS-Visit(G,f):DFS-Visit(G,d):DFS-Visit(G,a):46


Depth First-SearchDFS-All (G)1 for each vertex u2 u.color = WHITE3 finish_list = empty_list4 for each vertex u do5 if u.color == WHITE then6 DFS-Visit (G, u, finish_list)7 return finish_listghufedcbaDFS-Visit (G, u)1 u.color = GRAY2 for each vertex v in Adj(u) do3 if v.color = WHITE then4 DFS-Visit (G, v)5 u.color = BLACK6 finish_list.append(u)Finish_list:hDFS-Visit(G,g):DFS-Visit(G,f):DFS-Visit(G,d):© Copyright 2013 Brent Nash & Mark Redekopp, All Rights ReservedDFS-Visit(G,a):47


Depth First-SearchDFS-All (G)1 for each vertex u2 u.color = WHITE3 finish_list = empty_list4 for each vertex u do5 if u.color == WHITE then6 DFS-Visit (G, u, finish_list)7 return finish_listghufedcbaDFS-Visit (G, u)1 u.color = GRAY2 for each vertex v in Adj(u) do3 if v.color = WHITE then4 DFS-Visit (G, v)5 u.color = BLACK6 finish_list.append(u)Finish_list:h,gDFS-Visit(G,g):DFS-Visit(G,f):DFS-Visit(G,d):© Copyright 2013 Brent Nash & Mark Redekopp, All Rights ReservedDFS-Visit(G,a):48


Depth First-SearchDFS-All (G)1 for each vertex u2 u.color = WHITE3 finish_list = empty_list4 for each vertex u do5 if u.color == WHITE then6 DFS-Visit (G, u, finish_list)7 return finish_listghfeudcbaDFS-Visit (G, u)1 u.color = GRAY2 for each vertex v in Adj(u) do3 if v.color = WHITE then4 DFS-Visit (G, v)5 u.color = BLACK6 finish_list.append(u)Finish_list:h,g,f© Copyright 2013 Brent Nash & Mark Redekopp, All Rights ReservedDFS-Visit(G,f):DFS-Visit(G,d):DFS-Visit(G,a):49


Depth First-SearchDFS-All (G)1 for each vertex u2 u.color = WHITE3 finish_list = empty_list4 for each vertex u do5 if u.color == WHITE then6 DFS-Visit (G, u, finish_list)7 return finish_listghfeducbaDFS-Visit (G, u)1 u.color = GRAY2 for each vertex v in Adj(u) do3 if v.color = WHITE then4 DFS-Visit (G, v)5 u.color = BLACK6 finish_list.append(u)Finish_list:h,g,f,d© Copyright 2013 Brent Nash & Mark Redekopp, All Rights ReservedDFS-Visit(G,d):DFS-Visit(G,a):50


Depth First-SearchDFS-All (G)1 for each vertex u2 u.color = WHITE3 finish_list = empty_list4 for each vertex u do5 if u.color == WHITE then6 DFS-Visit (G, u, finish_list)7 return finish_listghfedcvbauDFS-Visit (G, u)1 u.color = GRAY2 for each vertex v in Adj(u) do3 if v.color = WHITE then4 DFS-Visit (G, v)5 u.color = BLACK6 finish_list.append(u)Finish_list:h,g,f,d© Copyright 2013 Brent Nash & Mark Redekopp, All Rights ReservedDFS-Visit(G,a):51


Depth First-SearchDFS-All (G)1 for each vertex u2 u.color = WHITE3 finish_list = empty_list4 for each vertex u do5 if u.color == WHITE then6 DFS-Visit (G, u, finish_list)7 return finish_listghfevdcubaDFS-Visit (G, u)1 u.color = GRAY2 for each vertex v in Adj(u) do3 if v.color = WHITE then4 DFS-Visit (G, v)5 u.color = BLACK6 finish_list.append(u)Finish_list:h,g,f,d© Copyright 2013 Brent Nash & Mark Redekopp, All Rights ReservedDFS-Visit(G,c):DFS-Visit(G,a):52


Depth First-SearchDFS-All (G)1 for each vertex u2 u.color = WHITE3 finish_list = empty_list4 for each vertex u do5 if u.color == WHITE then6 DFS-Visit (G, u, finish_list)7 return finish_listghfeudcbaDFS-Visit (G, u)1 u.color = GRAY2 for each vertex v in Adj(u) do3 if v.color = WHITE then4 DFS-Visit (G, v)5 u.color = BLACK6 finish_list.append(u)Finish_list:h,g,f,d© Copyright 2013 Brent Nash & Mark Redekopp, All Rights ReservedDFS-Visit(G,e):DFS-Visit(G,c):DFS-Visit(G,a):53


Depth First-SearchDFS-All (G)1 for each vertex u2 u.color = WHITE3 finish_list = empty_list4 for each vertex u do5 if u.color == WHITE then6 DFS-Visit (G, u, finish_list)7 return finish_listghfeudcbaDFS-Visit (G, u)1 u.color = GRAY2 for each vertex v in Adj(u) do3 if v.color = WHITE then4 DFS-Visit (G, v)5 u.color = BLACK6 finish_list.append(u)Finish_list:h,g,f,d.e© Copyright 2013 Brent Nash & Mark Redekopp, All Rights ReservedDFS-Visit(G,e):DFS-Visit(G,c):DFS-Visit(G,a):54


Depth First-SearchDFS-All (G)1 for each vertex u2 u.color = WHITE3 finish_list = empty_list4 for each vertex u do5 if u.color == WHITE then6 DFS-Visit (G, u, finish_list)7 return finish_listghfedcubaDFS-Visit (G, u)1 u.color = GRAY2 for each vertex v in Adj(u) do3 if v.color = WHITE then4 DFS-Visit (G, v)5 u.color = BLACK6 finish_list.append(u)Finish_list:h,g,f,d.e,c© Copyright 2013 Brent Nash & Mark Redekopp, All Rights ReservedDFS-Visit(G,c):DFS-Visit(G,a):55


Depth First-SearchDFS-All (G)1 for each vertex u2 u.color = WHITE3 finish_list = empty_list4 for each vertex u do5 if u.color == WHITE then6 DFS-Visit (G, u, finish_list)7 return finish_listghfedcbauDFS-Visit (G, u)1 u.color = GRAY2 for each vertex v in Adj(u) do3 if v.color = WHITE then4 DFS-Visit (G, v)5 u.color = BLACK6 finish_list.append(u)Finish_list:h,g,f,d.e,c,a© Copyright 2013 Brent Nash & Mark Redekopp, All Rights ReservedDFS-Visit(G,a):56


Depth First-SearchDFS-All (G)1 for each vertex u2 u.color = WHITE3 finish_list = empty_list4 for each vertex u do5 if u.color == WHITE then6 DFS-Visit (G, u, finish_list)7 return finish_listMay iteratethrough manycomplete verticesbefore finding b tolaunch a newsearch fromghfedcbuaDFS-Visit (G, u)1 u.color = GRAY2 for each vertex v in Adj(u) do3 if v.color = WHITE then4 DFS-Visit (G, v)5 u.color = BLACK6 finish_list.append(u)Finish_list:h,g,f,d.e,c,a© Copyright 2013 Brent Nash & Mark Redekopp, All Rights ReservedDFS-Visit(G,b):57


Depth First-SearchDFS-All (G)1 for each vertex u2 u.color = WHITE3 finish_list = empty_list4 for each vertex u do5 if u.color == WHITE then6 DFS-Visit (G, u, finish_list)7 return finish_listghfedcbuaDFS-Visit (G, u)1 u.color = GRAY2 for each vertex v in Adj(u) do3 if v.color = WHITE then4 DFS-Visit (G, v)5 u.color = BLACK6 finish_list.append(u)Finish_list:h,g,f,d.e,c,a,b© Copyright 2013 Brent Nash & Mark Redekopp, All Rights ReservedDFS-Visit(G,b):58


Depth First-SearchDFS-All (G)1 for each vertex u2 u.color = WHITE3 finish_list = empty_list4 for each vertex u do5 if u.color == WHITE then6 DFS-Visit (G, u, finish_list)7 return finish_listghfedcbuaDFS-Visit (G, u)1 u.color = GRAY2 for each vertex v in Adj(u) do3 if v.color = WHITE then4 DFS-Visit (G, v)5 u.color = BLACK6 finish_list.append(u)Finish_list:h,g,f,d.e,c,a,b© Copyright 2013 Brent Nash & Mark Redekopp, All Rights Reserved59


With Cycles in the graphANOTHER EXAMPLE© Copyright 2013 Brent Nash & Mark Redekopp, All Rights Reserved60


Depth First-SearchToposort(G)1 for each vertex u2 u.color = WHITE3 finish_list = empty_list4 for each vertex u do5 if u.color == WHITE then6 DFS-Visit (G, u, finish_list)aebcdfhgDFS-Visit (G, u)1 u.color = GRAY2 for each vertex v in Adj(u) do3 if v.color = WHITE then4 DFS-Visit (G, v)5 u.color = BLACK6 finish_list.append(u)© Copyright 2013 Brent Nash & Mark Redekopp, All Rights Reserved61


Depth First-SearchToposort(G)1 for each vertex u2 u.color = WHITE3 finish_list = empty_list4 for each vertex u do5 if u.color == WHITE then6 DFS-Visit (G, u, finish_list)uaebcvdfhgDFS-Visit (G, u)1 u.color = GRAY2 for each vertex v in Adj(u) do3 if v.color = WHITE then4 DFS-Visit (G, v)5 u.color = BLACK6 finish_list.append(u)© Copyright 2013 Brent Nash & Mark Redekopp, All Rights ReservedDFS-Visit(G,a):62


Depth First-SearchToposort(G)1 for each vertex u2 u.color = WHITE3 finish_list = empty_list4 for each vertex u do5 if u.color == WHITE then6 DFS-Visit (G, u, finish_list)avebcudfhgDFS-Visit (G, u)1 u.color = GRAY2 for each vertex v in Adj(u) do3 if v.color = WHITE then4 DFS-Visit (G, v)5 u.color = BLACK6 finish_list.append(u)© Copyright 2013 Brent Nash & Mark Redekopp, All Rights ReservedDFS-Visit(G,c):DFS-Visit(G,a):63


Depth First-SearchToposort(G)1 for each vertex u2 u.color = WHITE3 finish_list = empty_list4 for each vertex u do5 if u.color == WHITE then6 DFS-Visit (G, u, finish_list)auebcdfhgvDFS-Visit (G, u)1 u.color = GRAY2 for each vertex v in Adj(u) do3 if v.color = WHITE then4 DFS-Visit (G, v)5 u.color = BLACK6 finish_list.append(u)© Copyright 2013 Brent Nash & Mark Redekopp, All Rights ReservedDFS-Visit(G,b):DFS-Visit(G,c):DFS-Visit(G,a):64


Depth First-SearchToposort(G)1 for each vertex u2 u.color = WHITE3 finish_list = empty_list4 for each vertex u do5 if u.color == WHITE then6 DFS-Visit (G, u, finish_list)aebcdfhguvDFS-Visit (G, u)1 u.color = GRAY2 for each vertex v in Adj(u) do3 if v.color = WHITE then4 DFS-Visit (G, v)5 u.color = BLACK6 finish_list.append(u)DFS-Visit(G,h):DFS-Visit(G,b):DFS-Visit(G,c):© Copyright 2013 Brent Nash & Mark Redekopp, All Rights ReservedDFS-Visit(G,a):65


Depth First-SearchToposort(G)1 for each vertex u2 u.color = WHITE3 finish_list = empty_list4 for each vertex u do5 if u.color == WHITE then6 DFS-Visit (G, u, finish_list)aebcvdfhguDFS-Visit (G, u)1 u.color = GRAY2 for each vertex v in Adj(u) do3 if v.color = WHITE then4 DFS-Visit (G, v)5 u.color = BLACK6 finish_list.append(u)DFS-Visit(G,g):DFS-Visit(G,h):DFS-Visit(G,b):DFS-Visit(G,c):© Copyright 2013 Brent Nash & Mark Redekopp, All Rights ReservedDFS-Visit(G,a):66


Depth First-SearchToposort(G)1 for each vertex u2 u.color = WHITE3 finish_list = empty_list4 for each vertex u do5 if u.color == WHITE then6 DFS-Visit (G, u, finish_list)DFS-Visit (G, u)1 u.color = GRAY2 for each vertex v in Adj(u) do3 if v.color = WHITE then4 DFS-Visit (G, v)5 u.color = BLACK6 finish_list.append(u)aebcdfvDFS-Visit(G,g):DFS-Visit(G,h):hguDFS-Visit(G,b):DFS-Visit(G,c):© Copyright 2013 Brent Nash & Mark Redekopp, All Rights ReservedDFS-Visit(G,a):67


Depth First-SearchToposort(G)1 for each vertex u2 u.color = WHITE3 finish_list = empty_list4 for each vertex u do5 if u.color == WHITE then6 DFS-Visit (G, u, finish_list)DFS-Visit (G, u)1 u.color = GRAY2 for each vertex v in Adj(u) do3 if v.color = WHITE then4 DFS-Visit (G, v)5 u.color = BLACK6 finish_list.append(u)aebcdfvuDFS-Visit(G,f):DFS-Visit(G,g):DFS-Visit(G,h):hgDFS-Visit(G,b):DFS-Visit(G,c):© Copyright 2013 Brent Nash & Mark Redekopp, All Rights ReservedDFS-Visit(G,a):68


Depth First-SearchToposort(G)1 for each vertex u2 u.color = WHITE3 finish_list = empty_list4 for each vertex u do5 if u.color == WHITE then6 DFS-Visit (G, u, finish_list)aebcvdfuhgDFS-Visit (G, u)1 u.color = GRAY2 for each vertex v in Adj(u) do3 if v.color = WHITE then4 DFS-Visit (G, v)5 u.color = BLACK6 finish_list.append(u)DFS-Visit(G,d):DFS-Visit(G,f):DFS-Visit(G,g):DFS-Visit(G,h):DFS-Visit(G,b):DFS-Visit(G,c):© Copyright 2013 Brent Nash & Mark Redekopp, All Rights ReservedDFS-Visit(G,a):69


Depth First-SearchToposort(G)1 for each vertex u2 u.color = WHITE3 finish_list = empty_list4 for each vertex u do5 if u.color == WHITE then6 DFS-Visit (G, u, finish_list)aebcdfuhgDFS-Visit (G, u)1 u.color = GRAY2 for each vertex v in Adj(u) do3 if v.color = WHITE then4 DFS-Visit (G, v)5 u.color = BLACK6 finish_list.append(u)DFSQ:dDFS-Visit(G,d):DFS-Visit(G,f):DFS-Visit(G,g):DFS-Visit(G,h):DFS-Visit(G,b):DFS-Visit(G,c):© Copyright 2013 Brent Nash & Mark Redekopp, All Rights ReservedDFS-Visit(G,a):70


Depth First-SearchToposort(G)1 for each vertex u2 u.color = WHITE3 finish_list = empty_list4 for each vertex u do5 if u.color == WHITE then6 DFS-Visit (G, u, finish_list)DFS-Visit (G, u)1 u.color = GRAY2 for each vertex v in Adj(u) do3 if v.color = WHITE then4 DFS-Visit (G, v)5 u.color = BLACK6 finish_list.append(u)DFSQ:daebvcdfuDFS-Visit(G,f):DFS-Visit(G,g):DFS-Visit(G,h):hgDFS-Visit(G,b):DFS-Visit(G,c):© Copyright 2013 Brent Nash & Mark Redekopp, All Rights ReservedDFS-Visit(G,a):71


Depth First-SearchToposort(G)1 for each vertex u2 u.color = WHITE3 finish_list = empty_list4 for each vertex u do5 if u.color == WHITE then6 DFS-Visit (G, u, finish_list)avebuvcdfhgDFS-Visit (G, u)1 u.color = GRAY2 for each vertex v in Adj(u) do3 if v.color = WHITE then4 DFS-Visit (G, v)5 u.color = BLACK6 finish_list.append(u)DFSQ:dDFS-Visit(G,e):DFS-Visit(G,f):DFS-Visit(G,g):DFS-Visit(G,h):DFS-Visit(G,b):DFS-Visit(G,c):© Copyright 2013 Brent Nash & Mark Redekopp, All Rights ReservedDFS-Visit(G,a):72


Depth First-SearchToposort(G)1 for each vertex u2 u.color = WHITE3 finish_list = empty_list4 for each vertex u do5 if u.color == WHITE then6 DFS-Visit (G, u, finish_list)aebucdfhgDFS-Visit (G, u)1 u.color = GRAY2 for each vertex v in Adj(u) do3 if v.color = WHITE then4 DFS-Visit (G, v)5 u.color = BLACK6 finish_list.append(u)DFSQ:deDFS-Visit(G,e):DFS-Visit(G,f):DFS-Visit(G,g):DFS-Visit(G,h):DFS-Visit(G,b):DFS-Visit(G,c):© Copyright 2013 Brent Nash & Mark Redekopp, All Rights ReservedDFS-Visit(G,a):73


Depth First-SearchToposort(G)1 for each vertex u2 u.color = WHITE3 finish_list = empty_list4 for each vertex u do5 if u.color == WHITE then6 DFS-Visit (G, u, finish_list)DFS-Visit (G, u)1 u.color = GRAY2 for each vertex v in Adj(u) do3 if v.color = WHITE then4 DFS-Visit (G, v)5 u.color = BLACK6 finish_list.append(u)DFSQ:defaebcdfuDFS-Visit(G,f):DFS-Visit(G,g):DFS-Visit(G,h):hgDFS-Visit(G,b):DFS-Visit(G,c):© Copyright 2013 Brent Nash & Mark Redekopp, All Rights ReservedDFS-Visit(G,a):74


Depth First-SearchToposort(G)1 for each vertex u2 u.color = WHITE3 finish_list = empty_list4 for each vertex u do5 if u.color == WHITE then6 DFS-Visit (G, u, finish_list)uaebcdfhgDFS-Visit (G, u)1 u.color = GRAY2 for each vertex v in Adj(u) do3 if v.color = WHITE then4 DFS-Visit (G, v)5 u.color = BLACK6 finish_list.append(u)DFSQ:defghbc© Copyright 2013 Brent Nash & Mark Redekopp, All Rights ReservedDFS-Visit(G,a):75


Depth First-SearchToposort(G)1 for each vertex u2 u.color = WHITE3 finish_list = empty_list4 for each vertex u do5 if u.color == WHITE then6 DFS-Visit (G, u, finish_list)aebcdfhgDFS-Visit (G, u)1 u.color = GRAY2 for each vertex v in Adj(u) do3 if v.color = WHITE then4 DFS-Visit (G, v)5 u.color = BLACK6 finish_list.append(u)DFSQ:defghbca© Copyright 2013 Brent Nash & Mark Redekopp, All Rights Reserved76


BFS vs. DFS Algorithm‣ BFS and DFS are more similar than you think– Do we use a FIFO/Queue (BFS) or LIFO/Stack (DFS)to store vertices as we find themBFS-Visit (G, start_node)1 for each vertex u2 u.color = WHITE3 u.pred = nil4 bfsq = new deque5 bfsq.push_back(start_node)6 while bfsq not empty7 u = bfsq.pop_front()8 if u.color == WHITE9 u.color = GRAY10 foreach vertex v in Adj(u) do11 bfsq.push_back(v)DFS-Visit (G, start_node)1 for each vertex u2 u.color = WHITE3 u.pred = nil4 dfsq = new deque5 dfsq.push_back(start_node)6 while dfsq not empty7 u = dfsq.pop_back()8 if u.color == WHITE9 u.color = GRAY10 foreach vertex v in Adj(u) do11 dfsq.push_back(v)© Copyright 2013 Brent Nash & Mark Redekopp, All Rights Reserved77


ITERATIVE VERSION© Copyright 2013 Brent Nash & Mark Redekopp, All Rights Reserved78


Depth First-SearchDFS (G,s)1 for each vertex u2 u.color = WHITE3 dfsq = new deque4 dfsq.push_back(s)5 while dfsq not empty6 u = dfsq.back()abcdhg7 if u.color == WHITE then8 u.color = GRAY9 foreach vertex v in Adj(u) doef10 if v.color == WHITE11 dfsq.push_back(v)12 else if u.color == GRAYDFSQ:a13 u.color = BLACK14 dfsq.pop_back()© Copyright 2013 Brent Nash & Mark Redekopp, All Rights Reserved79


Depth First-SearchDFS (G,s)1 for each vertex u2 u.color = WHITE3 dfsq = new deque4 dfsq.push_back(s)5 while dfsq not empty6 u = dfsq.back()abcdhg7 if u.color == WHITE then8 u.color = GRAY9 foreach vertex v in Adj(u) doef10 if v.color == WHITE11 dfsq.push_back(v)12 else if u.color == GRAYDFSQ:a c e13 u.color = BLACK14 dfsq.pop_back()© Copyright 2013 Brent Nash & Mark Redekopp, All Rights Reserved80


Depth First-SearchDFS (G,s)1 for each vertex u2 u.color = WHITE3 dfsq = new deque4 dfsq.push_back(s)5 while dfsq not empty6 u = dfsq.back()abcdhg7 if u.color == WHITE then8 u.color = GRAY9 foreach vertex v in Adj(u) doef10 if v.color == WHITE11 dfsq.push_back(v)12 else if u.color == GRAYDFSQ: a c e c f13 u.color = BLACK14 dfsq.pop_back()© Copyright 2013 Brent Nash & Mark Redekopp, All Rights Reserved81


Depth First-SearchDFS (G,s)1 for each vertex u2 u.color = WHITE3 dfsq = new deque4 dfsq.push_back(s)5 while dfsq not empty6 u = dfsq.back()abcdhg7 if u.color == WHITE then8 u.color = GRAY9 foreach vertex v in Adj(u) doef10 if v.color == WHITE11 dfsq.push_back(v)12 else if u.color == GRAYDFSQ: a c e c f d g13 u.color = BLACK14 dfsq.pop_back()© Copyright 2013 Brent Nash & Mark Redekopp, All Rights Reserved82


Depth First-SearchDFS (G,s)1 for each vertex u2 u.color = WHITE3 dfsq = new deque4 dfsq.push_back(s)5 while dfsq not empty6 u = dfsq.back()abcdhg7 if u.color == WHITE then8 u.color = GRAY9 foreach vertex v in Adj(u) doef10 if v.color == WHITE11 dfsq.push_back(v)12 else if u.color == GRAYDFSQ: a c e c f d g c h13 u.color = BLACK14 dfsq.pop_back()© Copyright 2013 Brent Nash & Mark Redekopp, All Rights Reserved83


Depth First-SearchDFS (G,s)1 for each vertex u2 u.color = WHITE3 dfsq = new deque4 dfsq.push_back(s)5 while dfsq not empty6 u = dfsq.back()abcdhg7 if u.color == WHITE then8 u.color = GRAY9 foreach vertex v in Adj(u) doef10 if v.color == WHITE11 dfsq.push_back(v)12 else if u.color == GRAYDFSQ: a c e c f d g c h b13 u.color = BLACK14 dfsq.pop_back()© Copyright 2013 Brent Nash & Mark Redekopp, All Rights Reserved84


Depth First-SearchDFS (G,s)1 for each vertex u2 u.color = WHITE3 dfsq = new deque4 dfsq.push_back(s)5 while dfsq not empty6 u = dfsq.back()7 if u.color == WHITE then8 u.color = GRAY9 foreach vertex v in Adj(u) do10 if v.color == WHITE11 dfsq.push_back(v)12 else if u.color == GRAY13 u.color = BLACK14 dfsq.pop_back()bcadefDFSQ: a c e c f d g c h b chg© Copyright 2013 Brent Nash & Mark Redekopp, All Rights Reserved85


Depth First-SearchDFS (G,s)1 for each vertex u2 u.color = WHITE3 dfsq = new deque4 dfsq.push_back(s)5 while dfsq not empty6 u = dfsq.back()7 if u.color == WHITE then8 u.color = GRAY9 foreach vertex v in Adj(u) do10 if v.color == WHITE11 dfsq.push_back(v)12 else if u.color == GRAY13 u.color = BLACK14 dfsq.pop_back()bcadefDFSQ: a c e c f d g c h b c dhg© Copyright 2013 Brent Nash & Mark Redekopp, All Rights Reserved86


Depth First-SearchDFS (G,s)1 for each vertex u2 u.color = WHITE3 dfsq = new deque4 dfsq.push_back(s)5 while dfsq not empty6 u = dfsq.back()7 if u.color == WHITE then8 u.color = GRAY9 foreach vertex v in Adj(u) do10 if v.color == WHITE11 dfsq.push_back(v)12 else if u.color == GRAY13 u.color = BLACK14 dfsq.pop_back()bcadefDFSQ: a c e c f d g c h b c dhg© Copyright 2013 Brent Nash & Mark Redekopp, All Rights Reserved87


Depth First-SearchDFS (G,s)1 for each vertex u2 u.color = WHITE3 dfsq = new deque4 dfsq.push_back(s)5 while dfsq not empty6 u = dfsq.back()7 if u.color == WHITE then8 u.color = GRAY9 foreach vertex v in Adj(u) do10 if v.color == WHITE11 dfsq.push_back(v)12 else if u.color == GRAY13 u.color = BLACK14 dfsq.pop_back()bcadefDFSQ: a c e c f d g c h b c dhg© Copyright 2013 Brent Nash & Mark Redekopp, All Rights Reserved88


Depth First-SearchDFS (G,s)1 for each vertex u2 u.color = WHITE3 dfsq = new deque4 dfsq.push_back(s)5 while dfsq not empty6 u = dfsq.back()7 if u.color == WHITE then8 u.color = GRAY9 foreach vertex v in Adj(u) do10 if v.color == WHITE11 dfsq.push_back(v)12 else if u.color == GRAY13 u.color = BLACK14 dfsq.pop_back()bcadefDFSQ: a c e c f d g c h b chg© Copyright 2013 Brent Nash & Mark Redekopp, All Rights Reserved89


Depth First-SearchDFS (G,s)1 for each vertex u2 u.color = WHITE3 dfsq = new deque4 dfsq.push_back(s)5 while dfsq not empty6 u = dfsq.back()abcdhg7 if u.color == WHITE then8 u.color = GRAY9 foreach vertex v in Adj(u) doef10 if v.color == WHITE11 dfsq.push_back(v)12 else if u.color == GRAYDFSQ: a c e c f d g c h b13 u.color = BLACK14 dfsq.pop_back()© Copyright 2013 Brent Nash & Mark Redekopp, All Rights Reserved90


Depth First-SearchDFS (G,s)1 for each vertex u2 u.color = WHITE3 dfsq = new deque4 dfsq.push_back(s)5 while dfsq not empty6 u = dfsq.back()abcdhg7 if u.color == WHITE then8 u.color = GRAY9 foreach vertex v in Adj(u) doef10 if v.color == WHITE11 dfsq.push_back(v)12 else if u.color == GRAYDFSQ:a13 u.color = BLACK14 dfsq.pop_back()© Copyright 2013 Brent Nash & Mark Redekopp, All Rights Reserved91


Dijkstra's AlgorithmSINGLE-SOURCE SHORTESTPATH (SSSP)© Copyright 2013 Brent Nash & Mark Redekopp, All Rights Reserved92


List of VerticesAdjacency ListsSSSP‣ Let us associate a 'weight' with eachedge– Could be physical distance, cost of usingthe link, etc.‣ Find the shortest path from a sourcenode, 'a' to all other nodesabcdefgh(c,13),(e,4)(c,5),(h,6)(a,13),(b,5),(d,2),(e,8),(g,7)(c,2),(f,1)(a,4),(c,8),(f,3)(d,1),(e,3),(g,4)(c,7),(f,4),(h,14)(b,6),(g,14)Edge weightsaeb5138436c2df174h14g© Copyright 2013 Brent Nash & Mark Redekopp, All Rights Reserved93


List of VerticesAdjacency ListsList of VerticesSSSP‣ What is the shortest distance from ato all other vertices?‣ How would you go about computingthose distances?aeb1348536c2df174h14gabcdefgh(c,13),(e,4)(c,5),(h,6)(a,13),(b,5),(d,2),(e,8),(g,7)(c,2),(f,1)(a,4),(c,8),(f,3)(d,1),(e,3),(g,4)(c,7),(f,4),(h,14)(b,6),(g,14)© Copyright 2013 Brent Nash & Mark Redekopp, All Rights ReservedVertabcdefghDist094


List of VerticesDijkstra's Algorithm‣ Dijkstra's algorithm is similar to a BFSbut pulls out the smallest distancevertex (from the source) rather thanpulling vertices out in FIFO order (as inBFS)a4b13856c2d17h14g‣ Maintain a data structure that you canidentify shortlye3f4– We'll show it as a table of all vertices withtheir currently 'known' distance from thesource• Initially, a has dist=0• All others = infinite distanceVertabcdefghDist0infinfinfinfinfinfinf© Copyright 2013 Brent Nash & Mark Redekopp, All Rights Reserved95


List of VerticesDijkstra's Algorithm1. SSSP(G, s)2. Q = empty Q3. s.dist = 0; s.pred = NULL4. Q.insert(s)5. For all v in vertices6. if v != s then v.dist = inf; Q.insert(v)a4b13e8536c2df174h14g7. while Q is not emptyVertDist8. v = Q.remove_smallest_dist_vert()9. for u in neighbors(v)10. w = weight(v,u)11. if(v.dist + w < u.dist)12. u.pred = v13. u.dist = v.dist + w (and update Q)abcdefgh0infinfinfinfinfinfinf© Copyright 2013 Brent Nash & Mark Redekopp, All Rights Reserved96


List of VerticesDijkstra's Algorithm1. SSSP(G, s)2. Q = empty Q3. s.dist = 0; s.pred = NULL4. Q.insert(s)5. For all v in vertices6. if v != s then v.dist = inf; Q.insert(v)a4b13e8536c2df174h14g7. while Q is not emptyVertDist8. v = Q.remove_smallest_dist_vert()9. for u in neighbors(v)10. w = weight(v,u)11. if(v.dist + w < u.dist)12. u.pred = v13. u.dist = v.dist + w (and update Q)abcdefgh0infinfinfinfinfinfinf134v=a© Copyright 2013 Brent Nash & Mark Redekopp, All Rights Reserved97


List of VerticesDijkstra's Algorithm1. SSSP(G, s)2. Q = empty Q3. s.dist = 0; s.pred = NULL4. Q.insert(s)5. For all v in vertices6. if v != s then v.dist = inf; Q.insert(v)a4b13e8536c2df174h14g7. while Q is not emptyVertDist8. v = Q.remove_smallest_dist_vert()9. for u in neighbors(v)10. w = weight(v,u)11. if(v.dist + w < u.dist)12. u.pred = v13. u.dist = v.dist + w (and update Q)abcdefgh0inf13inf4infinfinf127v=e© Copyright 2013 Brent Nash & Mark Redekopp, All Rights Reserved98


List of VerticesDijkstra's Algorithm1. SSSP(G, s)2. Q = empty Q3. s.dist = 0; s.pred = NULL4. Q.insert(s)5. For all v in vertices6. if v != s then v.dist = inf; Q.insert(v)a4b13e8536c2df174h14g7. while Q is not emptyVertDist8. v = Q.remove_smallest_dist_vert()9. for u in neighbors(v)10. w = weight(v,u)11. if(v.dist + w < u.dist)12. u.pred = v13. u.dist = v.dist + w (and update Q)abcdefgh0inf12inf47infinf811v=f© Copyright 2013 Brent Nash & Mark Redekopp, All Rights Reserved99


List of VerticesDijkstra's Algorithm1. SSSP(G, s)2. Q = empty Q3. s.dist = 0; s.pred = NULL4. Q.insert(s)5. For all v in vertices6. if v != s then v.dist = inf; Q.insert(v)a4b13e8536c2df174h14g7. while Q is not emptyVertDist8. v = Q.remove_smallest_dist_vert()9. for u in neighbors(v)10. w = weight(v,u)11. if(v.dist + w < u.dist)12. u.pred = v13. u.dist = v.dist + w (and update Q)abcdefgh0inf1284711inf10v=d© Copyright 2013 Brent Nash & Mark Redekopp, All Rights Reserved100


List of VerticesDijkstra's Algorithm1. SSSP(G, s)2. Q = empty Q3. s.dist = 0; s.pred = NULL4. Q.insert(s)5. For all v in vertices6. if v != s then v.dist = inf; Q.insert(v)a4b13e8536c2df174h14g7. while Q is not emptyVertDist8. v = Q.remove_smallest_dist_vert()9. for u in neighbors(v)10. w = weight(v,u)11. if(v.dist + w < u.dist)12. u.pred = v13. u.dist = v.dist + w (and update Q)abcdefgh0inf1084711inf15v=c© Copyright 2013 Brent Nash & Mark Redekopp, All Rights Reserved101


List of VerticesDijkstra's Algorithm1. SSSP(G, s)2. Q = empty Q3. s.dist = 0; s.pred = NULL4. Q.insert(s)5. For all v in vertices6. if v != s then v.dist = inf; Q.insert(v)a4b13e8536c2df174h14g7. while Q is not emptyVertDist8. v = Q.remove_smallest_dist_vert()9. for u in neighbors(v)10. w = weight(v,u)11. if(v.dist + w < u.dist)12. u.pred = v13. u.dist = v.dist + w (and update Q)abcdefgh0151084711inf25v=g© Copyright 2013 Brent Nash & Mark Redekopp, All Rights Reserved102


List of VerticesDijkstra's Algorithm1. SSSP(G, s)2. Q = empty Q3. s.dist = 0; s.pred = NULL4. Q.insert(s)5. For all v in vertices6. if v != s then v.dist = inf; Q.insert(v)a4b13e8536c2df174h14g7. while Q is not emptyVertDist8. v = Q.remove_smallest_dist_vert()9. for u in neighbors(v)10. w = weight(v,u)11. if(v.dist + w < u.dist)12. u.pred = v13. u.dist = v.dist + w (and update Q)abcdefgh01510847112521v=b© Copyright 2013 Brent Nash & Mark Redekopp, All Rights Reserved103


List of VerticesDijkstra's Algorithm1. SSSP(G, s)2. Q = empty Q3. s.dist = 0; s.pred = NULL4. Q.insert(s)5. For all v in vertices6. if v != s then v.dist = inf; Q.insert(v)a4b13e8536c2df174h14g7. while Q is not emptyVertDist8. v = Q.remove_smallest_dist_vert()9. for u in neighbors(v)10. w = weight(v,u)11. if(v.dist + w < u.dist)12. u.pred = v13. u.dist = v.dist + w (and update Q)abcdefgh015108471121v=h© Copyright 2013 Brent Nash & Mark Redekopp, All Rights Reserved104

More magazines by this user
Similar magazines