Graph Representation, DFS and BFS
A classical problem Now I have N friends, but I have the phone numbers of some of them only I want to deliver a message to Sarah by repeated calling My memory is so good that I remember who can be contacted by my N friends Give me a calling plan for me and my friends
A classical problem This is actually a graph! Winnie Ryan Gary Ada Joyce Ryan John Ada Sarah Winnie
What is a graph? A set of vertices (or nodes) linked by edges 1 3 Mathematically, we often write G = (V,E) V: set of vertices, so |V| = number of vertices E: set of edges, so |E| = number of edges 1 3 2 5 4
What is a Graph? a b c d e A graph G = (V,E) is composed of: V: set of vertices E: set of edges connecting the vertices in V An edge e = (u,v) is a pair of vertices Example: a b V= {a,b,c,d,e} E= {(a,b),(a,c),(a,d), (b,e),(c,d),(c,e), (d,e)} c d e
Why do we need graphs? To present the relationships between different objects/elements in a mathematical way Examples: Friendship Local Area Network (LAN) Map of a country What could the vertices and edges represent in the above examples?
Applications electronic circuits CS16 electronic circuits networks (roads, flights, communications) LAX JFK DFW STL HNL FTL
Various types of graphs Directed (digraphs) /undirected graphs You may treat each undirected edge as two directed edges in opposite directions Undirected: edge (u, v) = (v, u), (No self loops.) Directed: (u, v) is edge from u to v, denoted as u v. (Self loops are allowed).
Various types of graphs Weighted/unweighted graphs You may treat unweighted edges to be weighted edges of equal weights 5 -2 7 4
Adjacent and Incident If (v0, v1) is an edge in an undirected graph, v0 and v1 are adjacent The edge (v0, v1) is incident on vertices v0 and v1 If <v0, v1> is an edge in a directed graph v0 is adjacent to v1, and v1 is adjacent from v0 The edge <v0, v1> is incident on v0 and v1
Adjacent and Incident Vertices “u” and “v” are said to be adjacent if there is an edge e = {u, v} joining them. the edge “e” is said to be incident on each of its vertices u and v. If (v0, v1) is an edge in an undirected graph, v0 and v1 are adjacent The edge (v0, v1) is incident on vertices v0 and v1 If <v0, v1> is an edge in a directed graph v0 is adjacent to v1, and v1 is adjacent from v0 The edge <v0, v1> is incident on v0 and v1
Examples of Graphs V={0,1,2,3,4} When (x,y) is an edge, we say that x is adjacent to y, and y is adjacent from x. 0 is adjacent to 1. 1 is not adjacent to 0. 2 is adjacent from 1. 2 4 3
Examples Vertex set = {v1, v2, v3, v4, v5, v6} Edge set = {e1, e2, e3, e4, e5, e6, e7} e1, e2, and e3 are incident on v1 v2 and v3 are adjacent to v1 e6 and e7 are loops e2 and e3 are parallel v4 is an isolated vertex v4 v6 v5 e5 e7 e6 e3 v2 v1 v3 e2 e1 e4
Representation of Graphs Two standard ways. Adjacency Lists. Adjacency Matrix. a d c b a d c b 1 2 3 4 1 2 3 4 1 0 1 1 1 2 1 0 1 0 3 1 1 0 1 4 1 0 1 0
Graph representation – undirected Adjacency list Adjacency matrix
Graph representation – directed Adjacency list Adjacency matrix
Adjacency Lists Representation A graph of n nodes is represented by a one-dimensional array L of linked lists, where L[i] is the linked list containing all the nodes adjacent from node i. The nodes in the list L[i] are in no particular order a d c b CS 103
Adjacency matrix Use a 2D array 1 3 4 2 5 -2 7 s\t 1 2 3 4 5 -2 7
Adjacency list N vertices, N linked lists Each list stores its adjacent vertices 1 5|-2 3|5 1 3 4 2 5 -2 7 2 1|0 3|7 3 4 2|4 5
Adjacency Lists Representation A graph of n nodes is represented by a one-dimensional array L of linked lists, where L[i] is the linked list containing all the nodes adjacent from node i. The nodes in the list L[i] are in no particular order a d c b
Pros and Cons of Adjacency Lists Space-efficient. Saves on space (memory): the representation takes as many memory words as there are nodes and edge. Total storage: (V+E) Cons: It can take up to O(n) time to determine if a pair of nodes (i,j) is an edge: one would have to search the linked list L[i], which takes time proportional to the length of L[i]. (V) in the worst case.
Adjacency Matrix Represented by a two dimensional array. |V| |V| matrix A. A is then given by: a d c b 1 2 3 4 1 2 3 4 1 0 1 1 1 2 1 0 1 0 3 1 1 0 1 4 1 0 1 0
Pros and Cons of Adjacency Matrices Simple to implement Easy and fast to tell if a pair (i,j) is an edge: simply check if A[i][j] is 1 or 0 Preferred when graph is dense |E| is close to |V|2 Cons: No matter how few edges the graph has, the matrix takes O(n2) in memory-Space: (V2).
Adjacency Matrix The amount of memory required is O(V2) For undirected graph to cut down needed memory only entries on and above diagonal are saved In an undirected graph, (u, v) and (v, u) represents the same edge, adjacency matrix A of an undirected graph is its own transpose A = AT It can be adapted to represent weighted graphs.
Degree of a Vertex For undirected graph The degree of a vertex is the number of edges incident to that vertex. Sum of the degrees of the vertices of a graph G is equal to twice the number of edges in G. A vertex is said to be even or odd according as its degree is an even or an odd number. A vertex of degree zero is called an isolated vertex. For directed graph, the in-degree of a vertex v is the number of edges coming to have v as the head the out-degree of a vertex v is the number of edges that have v as the tail A node with indegree 0 is a root. The sum of the outdegrees of the vertices of a digraph G equals the sum of the indegrees of the vertices, which equals the number of edges in G.
Examples 3 2 1 2 3 3 3 1 2 3 3 4 5 6 G2 3 1 1 1 1 3 G1 in:1, out: 1 directed graph in-degree out-degree 1 in: 1, out: 2 2 in: 1, out: 0 G3
Adjacency matrix of digraph X Y Z W X Y Z W No. of 1’s in A is equal to no. of edges. Row total and col total gives the indegree and outdegree, resp.
Graph Terminology simple path: no repeated vertices cycle: simple path, except that the last vertex is the same as the first vertex a b b e c c d e
Graph Terminology For Undirected Graph cycle: simple path, except that the last vertex is the same as the first vertex For Digraph Loop: An edge terminates at the same vertex from which it has started a b adca c d e
Connected Graph-Undirectedtivity An undirected graph is said to be connected if there is a path between every pair of nodes. Otherwise, the graph is disconnected. A forest is an acyclic graph, and a tree is a connected acyclic graph. Informally, an undirected graph is connected if it hangs in one piece Connected Disconnected
Even More Terminology connected not connected connected graph: any two vertices are connected by some path connected not connected connected component: maximal connected subgraph. E.g., the graph below has 3 connected components.
Connected Graphs-Directed A graph is connected if there is a path from every vertex to every other vertex. A directed graph with this property is strongly connected. If a directed graph is not strongly connected, but underlying undirected graph is connected then the directed graph is weakly connected.
A directed graph is weakly connected if there is an undirected path between any pair of vertices, and strongly connected if there is a directed path between every pair of vertices. A digraph that is not weakly connected is said to be disconnected. Strongly Connected Weakly Connected Disconnected
Complete Graph-Undirected Let n = #vertices, and m = #edges A complete graph: one in which all pairs of vertices are adjacent How many total edges in a complete graph? m = n(n -1)/2. Therefore, if a graph is not complete, m < n(n -1)/2
Complete Graph-Directed Let n = #vertices, and m = #edges A complete Digraph: has directed edge from every vertex to every other vertex . How many total edges in a complete graph? m = n(n -1). Therefore, if a graph is not complete, m < n(n -1) a b m=4(4-1)=12 c d
Digraphs and relations The relation R is reflexive if every node has a loop. The relations R is symmetric if arcs are bidirectional. The relation R is transitive if for any sequence of consecutive arcs, there is a single arc from the first to the last node.
Hamiltonian Path and Hamiltonian Cycle( Circuit) A Hamiltonian path (or traceable path) is a path in an undirected or directed graph that visits each vertex exactly once A Hamiltonian cycle (or Hamiltonian circuit) is a graph cycle (i.e., closed loop) that visits each vertex exactly once (except for the vertex that is both the start and end, which is visited twice). A graph that contains a Hamiltonian cycle is called a Hamiltonian graph. Determining whether such paths and cycles exist in graphs is the Hamiltonian path problem, which is NP-complete. Hamilton Path is A,F, B, C, G, D, E Hamilton circuit is A,F, B, C, G, D, E, A
Euler Path and Hamiltonian Cycle( Circuit) An Euler path is a path that uses every edge of a graph exactly once. An Euler circuit is a circuit that uses every edge of a graph exactly once. An Euler path starts and ends at different vertices. An Euler circuit starts and ends at the same vertex.
Eulerian circuit traverses every edge exactly once, but may repeat vertices, while a Hamiltonian circuit visits each vertex exactly once but may repeat edges.
Special graphs Tree: either one of the followings is the definition A connected graph with |V|-1 edges A connected graph without cycles A graph with exactly one path between every pair of vertices
Special graphs Tree edges could be directed or undirected For trees with directed edges, a root usually exists
Special graphs Tree - connected graph without cycles Forest - All connected components are trees. Forest is a collection of trees. How many trees are there in the following forest?
More… tree - connected graph without cycles forest - collection of trees
Graph-searching Algorithms Searching a graph means systematically following the edges of the graph so as to visit the vertices. Standard graph-searching algorithms. Breadth-first Search (BFS). Depth-first Search (DFS).
Breadth First Search
Breadth First Search (BFS) Go to all nearest nodes first The data structure “queue” is used to store the visited nodes Expand from visited (but not dead) nodes.
Breadth First Search (BFS) Queue 1 6 2 5 7 3 4 The path has been found! But let’s complete the BFS… 7 2 1 3 5 6 4
Breadth-first Search Expands between discovered and undiscovered vertices uniformly across the breadth. A vertex is “discovered” the first time it is encountered during the search. A vertex is “finished” if all vertices adjacent to it have been discovered. Colors the vertices to keep track of progress. White – Undiscovered. Gray – Discovered but not finished. Black – Finished. Colors are required only to reason about the algorithm. Can be implemented without colors.
Breadth-first Search Breadth-first search constructs a breadth-first tree, initially containing only its root, which is the source vertex s. Whenever the search discovers a white vertex in the course of scanning the adjacency list of an already discovered vertex u, the vertex v and the edge (u,v) are added to the tree. We say that u is the predecessor or parent of v in the breadth-first tree. Ancestor and descendant relationships in the breadth-first tree are defined relative to the root s. If u is on the simple path in the tree from the root s to vertex v , then u is an ancestor of v and v is a descendant of u.
Breadth First Search It is assumed that input graph G (V, E) is represented using adjacency list. Additional attributes are attached to each vertex in the graph: color[u] – stores color of each vertex π[u] – stores predecessor of u d[u] – stores distance from source s to vertex u
Algorithm: Breadth First Search BFS(G, s) 1 for each vertex u Є G: V [G] – {s} 2 do color [u] ← WHITE 3 d [u] ← ∞ 4 π[u] ← NIL 5 color[s] ← GRAY 6 d [s] ← 0 7 π[s] ← NIL 8 Q ← Ø /* Q always contains the set of GRAY vertices */ 9 ENQUEUE (Q, s) 10 while Q ≠ Ø 11 do u ← DEQUEUE (Q) 12 for each v Є G. Adj [u] 13 do if color [v] = WHITE /* For undiscovered vertex. */ 14 then color [v] ← GRAY 15 d [v] ← d [u] + 1 16 π[v] ← u 17 ENQUEUE(Q, v) 18 color [u] ← BLACK
Example (BFS) Q Except root node, s u Except root node, s For each vertex u Є V(G) color [u] ← WHITE d[u] ← ∞ π [s] ← NIL v w x y Q
Example (BFS) Q Considering s as root node u Considering s as root node color[s] ← GRAY d[s] ← 0 π [s] ← NIL ENQUEUE (Q, s) v w x y s Q
Example (BFS) r s t u 1 1 v w x y w Q r DEQUEUE s from Q Adj[s] = w, r color [w] = WHITE color [w] ← GRAY d[w]←d[s]+1 =0+1 = 1 π[w] ← s ENQUEUE (Q, w) color [r] = WHITE color [r] ← GRAY d [r] ← d [s] + 1 = 0 + 1 = 1 π[r] ← s ENQUEUE (Q, r) color [s] ← BLACK r s t u 1 1 v w x y w Q r
Example (BFS) 1 2 1 2 Q r t x DEQUEUE w from Q Adj[w] = s, t, x color [s] ≠ WHITE color [t] = WHITE color [t] ← GRAY d [t] ← d [w] + 1 = 1 + 1 = 2 π[t] ← w ENQUEUE (Q, t) color [x] = WHITE color [x] ← GRAY d [x] ← d [w] + 1 = 1 + 1 = 2 π[x] ← w ENQUEUE (Q, x) color [w] ← BLACK r s t u 1 2 1 2 v w x y r Q t x
Example (BFS) 1 2 2 1 2 Q t x v DEQUEUE r from Q Adj[r] = s, v color [s] ≠ WHITE color [v] = WHITE color [v] ← GRAY d [v] ←d [r] +1 =1+1 = 2 π[v] ← r ENQUEUE (Q, v) color [r] ← BLACK r s t u 1 2 2 1 2 v w x y Q t x v
Example (BFS) DEQUEUE t from Q Adj[t] = u, w, x color [u] = WHITE color [u] ← GRAY d [u] ← d [t]+1 = 2+1 =3 π[u] ← t ENQUEUE (Q, u) color [w] ≠ WHITE color [x] ≠ WHITE color [t] ← BLACK 1 2 3 r s t u v w x y Q
Example (BFS) DEQUEUE x from Q Adj[x] = t, u, w, y color [t] ≠ WHITE color [u] ≠ WHITE color [w] ≠ WHITE color [y] = WHITE color [y] ← GRAY d [y] ← d [x] + 1 =2+1 =3 π[y] ← x ENQUEUE (Q, y) color [x] ← BLACK 1 2 3 r s t u v w x y Q
Example (BFS) DEQUEUE v from Q Adj[v] = r color [r] ≠ WHITE color [v] ← BLACK 1 2 3 r s t u v w x y Q
Example (BFS) DEQUEUE u from Q Adj[u] = t, x, y color [t] ≠ WHITE color [x] ≠ WHITE color [y] ≠ WHITE color [u] ← BLACK 1 2 3 r s t u v w x y Q
Example (BFS) DEQUEUE y from Q Adj[y] = u, x color [u] ≠ WHITE color [x] ≠ WHITE color [y] ← BLACK 1 2 3 r s t u v w x y Q
Example (BFS) r s t u 1 2 3 2 1 2 3 v w x y BFS Tree
Analysis of BFS Total Running Time of BFS = O(V+E) Initialization takes O(V). Traversal Loop After initialization, each vertex is enqueued and dequeued at most once, and each operation takes O(1). So, total time for queuing is O(V). The adjacency list of each vertex is scanned at most once. The sum of lengths of all adjacency lists is (E). Summing up over all vertices => total running time of BFS is O(V+E), linear in the size of the adjacency list representation of graph. Total Running Time of BFS = O(V+E)
Analysis: Breadth First Search BFS(G, s) 1 for each vertex u Є G: V [G] – {s} 2 do color [u] ← WHITE 3 d [u] ← ∞ 4 π[u] ← NIL 5 color[s] ← GRAY 6 d [s] ← 0 7 π[s] ← NIL 8 Q ← Ø /* Q always contains the set of GRAY vertices */ 9 ENQUEUE (Q, s) 10 while Q ≠ Ø 11 do u ← DEQUEUE (Q) 12 for each v Є G. Adj [u] 13 do if color [v] = WHITE /* For undiscovered vertex. */ 14 then color [v] ← GRAY 15 d [v] ← d [u] + 1 16 π[v] ← u 17 ENQUEUE(Q, v) 18 color [u] ← BLACK O(V) O(V+E) O(E) Total Running Time = O(V + E)
Shortest Paths The shortest-path-distance δ (s, v) from s to v as the minimum number of edges in any path from vertex s to vertex v. if there is no path from s to v, then δ (s, v) = ∞ A path of length δ (s, v) from s to v is said to be a shortest path from s to v. Breadth First search finds the distance to each reachable vertex in the graph G (V, E) from a given source vertex s є V. The field d, for distance, of each vertex is used.
Algorithm: Breadth First Search BFS(G, s) 1 for each vertex u Є G: V [G] – {s} 2 do color [u] ← WHITE 3 d [u] ← ∞ 4 π[u] ← NIL 5 color[s] ← GRAY 6 d [s] ← 0 7 π[s] ← NIL 8 Q ← Ø /* Q always contains the set of GRAY vertices */ 9 ENQUEUE (Q, s) 10 while Q ≠ Ø 11 do u ← DEQUEUE (Q) 12 for each v Є G. Adj [u] 13 do if color [v] =WHITE /* For undiscovered vertex. */ 14 then color [v] ← GRAY 15 d [v] ← d [u] + 1 16 π[v] ← u 17 ENQUEUE(Q, v) 18 color [u] ← BLACK
Algorithm: Shortest Path BFS-Shortest-Paths (G, s) 1 for each vertex u є V [G] – {s} 2 d [u] ← ∞ 3 d [s] ← 0 4 ENQUEUE (Q, s) 5 while Q ≠ φ 6 do u ← DEQUEUE(Q) 7 for each v in Adj[u] 8 do if d [v] = ∞ 9 then d [v] ← d [u] +1 10 ENQUEUE (Q, v)
Print Path PRINT-PATH (G, s, v) 1 if v == s 2 print s 3 else if π[v] == NIL 4 print “no path from s to v exists 5 else PRINT-PATH (G, s, π[v]) 6 print v
Depth First Search
Depth First Search (DFS) Let’s use recursion!: Go as far as you can If it is a blind end, go back and walk through another edge 7 2 1 3 5 6 4
Let’s use recursion! Initialize all nodes as unvisited (white) DFS (node u) { mark u as discovered for each adjacent node v from u if (v is white) DFS (v) }
Depth First Search (DFS) Let’s review the graph, and note the color changes White: Unvisited Grey: Discovered Black: Finished (No more unvisited adjacent nodes) 7 2 1 3 5 6 4
Depth First Search (DFS) Each vertex is initially white It is grayed when it is discovered in the search, and It is blackened when it is finished, that is, when its adjacency list has been examined completely.
Discovery and Finish Times It guarantees that each vertex ends up in exactly one depth-first tree, so that these trees are disjoint. It timestamps each vertex The first timestamp d[v] records when v is first discovered (and grayed) The second timestamp f [v] records when the search finishes examining v's adjacency list (and blackens v). For every vertex u d[u] < f[u]
Algorithm: Depth First Search DFS-Visit(u) 1 color [u] ← GRAY time ← time + 1 d [u] ← time 4 for each v є Adj [u] 5 if color [v]= WHITE 6 then π[v] ← u 7 DFS-Visit (v) 8 color [u] ← BLACK 9 f[u] ← time ← time + 1 DFS(G) 1 for each vertex u є V [G] 2 do color [u] ← WHITE 3 π[u] ← NIL 4 time ← 0 //global variable 5 for each vertex u є V [G] 6 do if color [u] = WHITE then DFS-Visit (u)
Example (DFS) For each vertex u є V(G) color [u] ← WHITE π [u] ← NIL time ← 0 u v w x y z
Example (DFS) 1/ Considering white vertex u color [u] ← GRAY d[u] ← time + 1 = 0 + 1 = 1 Adj[u] = v, x If color [v] = WHITE π[v] ← u DFS-VISIT (v) u v w 1/ x y z
Example (DFS) 2/ 1/ color [v] ← GRAY d[v] ← time + 1 = 1 + 1 = 2 Adj[v] = y If color [y] =WHITE π[y] ← v DFS-VISIT (y) u v w 1/ 2/ x y z
Example (DFS) 2/ 1/ 3/ color [y] ← GRAY d[y] ← time + 1 = 2 + 1 = 3 Adj[y] = x If color [x] = WHITE π[x] ← y DFS-VISIT (x) u v w 1/ 2/ 3/ x y z
Example (DFS) 1/ 2/ 4/ 3/ color [x] ← GRAY d[x] ← time + 1 = 3 + 1 = 4 Adj[x] = v color [v] ≠ WHITE u v w 1/ 2/ 4/ 3/ x y z
Example (DFS) 1/ 2/ 4/ 3/ The edge (x, v) is a back edge that is a non tree edge and is labeled as B u v w 1/ 2/ B 4/ 3/ x y z Back Edges: Those edges (u,v), connecting a vertex u to an ancestor v. Self loops in directed graphs are also considered as back edges. Forward Edges: Those non-tree edges (u,v), connecting a vertex u to an descendant v. Cross Edges: All other edges, as long as one vertex is not an ancestor of the other.
Example (DFS) 1/ 2/ 4/5 3/ The vertex x is finished. color [x] ← BLACK f[x] ← time + 1 = 4 + 1 = 5 u v w 1/ 2/ B 4/5 3/ x y z
Example (DFS) 1/ 2/ 4/5 3/6 The vertex y is finished. color [y] ← BLACK f[y] ← time + 1 = 5 + 1 = 6 u v w 1/ 2/ B 4/5 3/6 x y z
Example (DFS) 1/ 2/7 4/5 3/6 The vertex v is finished. color [v] ← BLACK f[v] ← time + 1 = 6 + 1 = 7 u v w 1/ 2/7 B 4/5 3/6 x y z
Example (DFS) 1/ 2/7 4/5 3/6 The edge (u, x) is a forward edge that is a non tree edge and is labeled as F u v w 1/ 2/7 B F 4/5 3/6 x y z Back Edges: Those edges (u,v), connecting a vertex u to an ancestor v. Self loops in directed graphs are also considered as back edges. Forward Edges: Those non-tree edges (u,v), connecting a vertex u to an descendant v. Cross Edges: All other edges, as long as one vertex is not an ancestor of the other.
Example (DFS) 1/8 2/7 4/5 3/6 The vertex u is finished. color [u] ← BLACK f[u] ← time + 1 = 7 + 1 = 8 u v w 1/8 2/7 B F 4/5 3/6 x y z
Example (DFS) 1/8 2/7 9/ 4/5 3/6 Considering white vertex w color [w] ← GRAY d[w] ← time +1= 8+1 = 9 Adj[w] = y, z color [y] ≠ WHITE If color [z]= WHITE π[z] ← w DFS-VISIT (z) u v w 1/8 2/7 9/ B F 4/5 3/6 x y z
Example (DFS) 1/8 2/7 9/ 4/5 3/6 The edge (w, y) is a cross edge that is a non tree edge and is labeled as C u v w 1/8 2/7 9/ B C F 4/5 3/6 x y z Back Edges: Those edges (u,v), connecting a vertex u to an ancestor v. Self loops in directed graphs are also considered as back edges. Forward Edges: Those non-tree edges (u,v), connecting a vertex u to an descendant v. Cross Edges: All other edges, as long as one vertex is not an ancestor of the other.
Example (DFS) 1/8 2/7 9/ 4/5 3/6 10/ color [z] ← GRAY d[z] ← time + 1= 9+1 =10 Adj[z] = z color [z] ≠ WHITE u v w 1/8 2/7 9/ B C F 4/5 3/6 10/ x y z
Example (DFS) 1/8 2/7 9/ 4/5 3/6 10/ The edge (z, z) is a back edge that is a non tree edge and is labeled as B u v w 1/8 2/7 9/ B C F B 4/5 3/6 10/ x y z Back Edges: Those edges (u,v), connecting a vertex u to an ancestor v. Self loops in directed graphs are also considered as back edges. Forward Edges: Those non-tree edges (u,v), connecting a vertex u to an descendant v. Cross Edges: All other edges, as long as one vertex is not an ancestor of the other.
Example (DFS) 1/8 2/7 9/ 4/5 3/6 The vertex z is finished. color [z] ← BLACK f[z]← time + 1 =10+1 = 11 u v w 1/8 2/7 9/ B C F B 4/5 3/6 10/11 x y z
Example (DFS) 1/8 2/7 9/12 4/5 3/6 The vertex w is finished. color [w] ← BLACK f[w] ← time +1 =11+1 =12 u v w 1/8 2/7 9/12 B C F B 4/5 3/6 10/11 x y z
Analysis: Depth First Search DFS-Visit(u) 1 color [u] ← GRAY time ← time + 1 d [u] ← time 4 for each v є Adj [u] 5 if color [v]= WHITE 6 then π[v] ← u 7 DFS-Visit (v) 8 color [u] ← BLACK 9 f[u] ← time ← time + 1 DFS(G) 1 for each vertex u є V [G] 2 do color [u] ← WHITE 3 π[u] ← NIL 4 time ← 0 5 for each vertex u є V [G] 6 do if color [u] = WHITE then DFS-Visit (u) O(V) O(V) O(E) Total Running Time = O(V + E)
Analysis of DFS Initialization takes O(V). DFS-Visit is called once for each white vertex vV when it’s painted gray the first time. (E) Total running time of DFS is (V+E).