graph traversals - school of computer science, carnegie mellon
TRANSCRIPT
![Page 1: Graph Traversals - SCHOOL OF COMPUTER SCIENCE, Carnegie Mellon](https://reader031.vdocument.in/reader031/viewer/2022020703/61fb2f1e2e268c58cd5b262a/html5/thumbnails/1.jpg)
Graph Traversals
Slides by Carl Kingsford
Feb. 1, 2013
Based on/Reading: Chapter 3 of Kleinberg & Tardos
![Page 2: Graph Traversals - SCHOOL OF COMPUTER SCIENCE, Carnegie Mellon](https://reader031.vdocument.in/reader031/viewer/2022020703/61fb2f1e2e268c58cd5b262a/html5/thumbnails/2.jpg)
Breadth-First Search
Breadth-first search explores the nodes of a graph in increasingdistance away from some starting vertex s.
It decomposes the component into layers Li such that the shortestpath from s to each of nodes in Li is of length i .
Breadth-First Search:
1. L0 is the set {s}.2. Given layers L0, L1, . . . , Lj , then Lj+1 is the set of nodes that
are not in a previous layer and that have an edge to somenode in layer Lj .
![Page 3: Graph Traversals - SCHOOL OF COMPUTER SCIENCE, Carnegie Mellon](https://reader031.vdocument.in/reader031/viewer/2022020703/61fb2f1e2e268c58cd5b262a/html5/thumbnails/3.jpg)
BFS Tree
s
1 2 3 4 5
6 7 8 9
10 11 12 13 14 15 16
![Page 4: Graph Traversals - SCHOOL OF COMPUTER SCIENCE, Carnegie Mellon](https://reader031.vdocument.in/reader031/viewer/2022020703/61fb2f1e2e268c58cd5b262a/html5/thumbnails/4.jpg)
BFS Tree Example
A BFS traversal of a graph results in a breadth-first search tree:
2
1
s
1
2
3
3
3
Can we say anything about the non-tree edges?
![Page 5: Graph Traversals - SCHOOL OF COMPUTER SCIENCE, Carnegie Mellon](https://reader031.vdocument.in/reader031/viewer/2022020703/61fb2f1e2e268c58cd5b262a/html5/thumbnails/5.jpg)
BFS Tree Example
A BFS traversal of a graph results in a breadth-first search tree:
2
1
s
1
2
3
3
3
Can we say anything about the non-tree edges?
![Page 6: Graph Traversals - SCHOOL OF COMPUTER SCIENCE, Carnegie Mellon](https://reader031.vdocument.in/reader031/viewer/2022020703/61fb2f1e2e268c58cd5b262a/html5/thumbnails/6.jpg)
Example BFS
0:0
1:-12:-1
3:-1
4:-1
5:-1
6:-1
7:-1
8:-1
9:-1
10:-1 11:-1
12:-1
13:-1
14:-1
![Page 7: Graph Traversals - SCHOOL OF COMPUTER SCIENCE, Carnegie Mellon](https://reader031.vdocument.in/reader031/viewer/2022020703/61fb2f1e2e268c58cd5b262a/html5/thumbnails/7.jpg)
Example BFS
0:0
1:12:-1
3:-1
4:-1
5:1
6:-1
7:1
8:-1
9:-1
10:1 11:1
12:-1
13:1
14:-1
![Page 8: Graph Traversals - SCHOOL OF COMPUTER SCIENCE, Carnegie Mellon](https://reader031.vdocument.in/reader031/viewer/2022020703/61fb2f1e2e268c58cd5b262a/html5/thumbnails/8.jpg)
Example BFS
0:0
1:12:2
3:2
4:-1
5:1
6:-1
7:1
8:-1
9:-1
10:1 11:1
12:-1
13:1
14:-1
![Page 9: Graph Traversals - SCHOOL OF COMPUTER SCIENCE, Carnegie Mellon](https://reader031.vdocument.in/reader031/viewer/2022020703/61fb2f1e2e268c58cd5b262a/html5/thumbnails/9.jpg)
Example BFS
0:0
1:12:2
3:2
4:-1
5:1
6:-1
7:1
8:-1
9:-1
10:1 11:1
12:-1
13:1
14:-1
![Page 10: Graph Traversals - SCHOOL OF COMPUTER SCIENCE, Carnegie Mellon](https://reader031.vdocument.in/reader031/viewer/2022020703/61fb2f1e2e268c58cd5b262a/html5/thumbnails/10.jpg)
Example BFS
0:0
1:12:2
3:2
4:-1
5:1
6:2
7:1
8:-1
9:2
10:1 11:1
12:-1
13:1
14:-1
![Page 11: Graph Traversals - SCHOOL OF COMPUTER SCIENCE, Carnegie Mellon](https://reader031.vdocument.in/reader031/viewer/2022020703/61fb2f1e2e268c58cd5b262a/html5/thumbnails/11.jpg)
Example BFS
0:0
1:12:2
3:2
4:2
5:1
6:2
7:1
8:-1
9:2
10:1 11:1
12:-1
13:1
14:2
![Page 12: Graph Traversals - SCHOOL OF COMPUTER SCIENCE, Carnegie Mellon](https://reader031.vdocument.in/reader031/viewer/2022020703/61fb2f1e2e268c58cd5b262a/html5/thumbnails/12.jpg)
Example BFS
0:0
1:12:2
3:2
4:2
5:1
6:2
7:1
8:-1
9:2
10:1 11:1
12:-1
13:1
14:2
![Page 13: Graph Traversals - SCHOOL OF COMPUTER SCIENCE, Carnegie Mellon](https://reader031.vdocument.in/reader031/viewer/2022020703/61fb2f1e2e268c58cd5b262a/html5/thumbnails/13.jpg)
Example BFS
0:0
1:12:2
3:2
4:2
5:1
6:2
7:1
8:-1
9:2
10:1 11:1
12:-1
13:1
14:2
![Page 14: Graph Traversals - SCHOOL OF COMPUTER SCIENCE, Carnegie Mellon](https://reader031.vdocument.in/reader031/viewer/2022020703/61fb2f1e2e268c58cd5b262a/html5/thumbnails/14.jpg)
Example BFS
0:0
1:12:2
3:2
4:2
5:1
6:2
7:1
8:-1
9:2
10:1 11:1
12:3
13:1
14:2
![Page 15: Graph Traversals - SCHOOL OF COMPUTER SCIENCE, Carnegie Mellon](https://reader031.vdocument.in/reader031/viewer/2022020703/61fb2f1e2e268c58cd5b262a/html5/thumbnails/15.jpg)
Example BFS
0:0
1:12:2
3:2
4:2
5:1
6:2
7:1
8:-1
9:2
10:1 11:1
12:3
13:1
14:2
![Page 16: Graph Traversals - SCHOOL OF COMPUTER SCIENCE, Carnegie Mellon](https://reader031.vdocument.in/reader031/viewer/2022020703/61fb2f1e2e268c58cd5b262a/html5/thumbnails/16.jpg)
Example BFS
0:0
1:12:2
3:2
4:2
5:1
6:2
7:1
8:3
9:2
10:1 11:1
12:3
13:1
14:2
![Page 17: Graph Traversals - SCHOOL OF COMPUTER SCIENCE, Carnegie Mellon](https://reader031.vdocument.in/reader031/viewer/2022020703/61fb2f1e2e268c58cd5b262a/html5/thumbnails/17.jpg)
Example BFS
0:0
1:12:2
3:2
4:2
5:1
6:2
7:1
8:3
9:2
10:1 11:1
12:3
13:1
14:2
![Page 18: Graph Traversals - SCHOOL OF COMPUTER SCIENCE, Carnegie Mellon](https://reader031.vdocument.in/reader031/viewer/2022020703/61fb2f1e2e268c58cd5b262a/html5/thumbnails/18.jpg)
Example BFS
0:0
1:12:2
3:2
4:2
5:1
6:2
7:1
8:3
9:2
10:1 11:1
12:3
13:1
14:2
![Page 19: Graph Traversals - SCHOOL OF COMPUTER SCIENCE, Carnegie Mellon](https://reader031.vdocument.in/reader031/viewer/2022020703/61fb2f1e2e268c58cd5b262a/html5/thumbnails/19.jpg)
Example BFS
0:0
1:12:2
3:2
4:2
5:1
6:2
7:1
8:3
9:2
10:1 11:1
12:3
13:1
14:2
![Page 20: Graph Traversals - SCHOOL OF COMPUTER SCIENCE, Carnegie Mellon](https://reader031.vdocument.in/reader031/viewer/2022020703/61fb2f1e2e268c58cd5b262a/html5/thumbnails/20.jpg)
Example BFS
0:0
1:12:2
3:2
4:2
5:1
6:2
7:1
8:3
9:2
10:1 11:1
12:3
13:1
14:2
![Page 21: Graph Traversals - SCHOOL OF COMPUTER SCIENCE, Carnegie Mellon](https://reader031.vdocument.in/reader031/viewer/2022020703/61fb2f1e2e268c58cd5b262a/html5/thumbnails/21.jpg)
Depth-First Search
DFS keeps walking down a pathuntil it is forced to backtrack.
It backtracks until it finds anew path to go down.
Think: Solving a maze.
It results in a search tree, calledthe depth-first search tree.
In general, the DFS tree will bevery different than the BFStree.
5
1
s
7
2
43
6
![Page 22: Graph Traversals - SCHOOL OF COMPUTER SCIENCE, Carnegie Mellon](https://reader031.vdocument.in/reader031/viewer/2022020703/61fb2f1e2e268c58cd5b262a/html5/thumbnails/22.jpg)
Depth-First Search
5
1
s
7
2
4
3
6
![Page 23: Graph Traversals - SCHOOL OF COMPUTER SCIENCE, Carnegie Mellon](https://reader031.vdocument.in/reader031/viewer/2022020703/61fb2f1e2e268c58cd5b262a/html5/thumbnails/23.jpg)
Example DFS
0:0:-1
1:-1:-1 2:-1:-1
3:-1:-1 4:-1:-1
5:-1:-1
6:-1:-1
7:-1:-1
8:-1:-1
9:-1:-1
10:-1:-1
11:-1:-1
12:-1:-1
13:-1:-1
14:-1:-1
![Page 24: Graph Traversals - SCHOOL OF COMPUTER SCIENCE, Carnegie Mellon](https://reader031.vdocument.in/reader031/viewer/2022020703/61fb2f1e2e268c58cd5b262a/html5/thumbnails/24.jpg)
Example DFS
0:0:-1
1:-1:-1 2:-1:-1
3:-1:-1 4:-1:-1
5:-1:-1
6:-1:-1
7:-1:-1
8:-1:-1
9:-1:-1
10:-1:-1
11:-1:-1
12:-1:-1
13:-1:-1
14:1:-1
![Page 25: Graph Traversals - SCHOOL OF COMPUTER SCIENCE, Carnegie Mellon](https://reader031.vdocument.in/reader031/viewer/2022020703/61fb2f1e2e268c58cd5b262a/html5/thumbnails/25.jpg)
Example DFS
0:0:-1
1:-1:-1 2:-1:-1
3:-1:-1 4:-1:-1
5:2:-1
6:-1:-1
7:-1:-1
8:-1:-1
9:-1:-1
10:-1:-1
11:-1:-1
12:-1:-1
13:-1:-1
14:1:-1
![Page 26: Graph Traversals - SCHOOL OF COMPUTER SCIENCE, Carnegie Mellon](https://reader031.vdocument.in/reader031/viewer/2022020703/61fb2f1e2e268c58cd5b262a/html5/thumbnails/26.jpg)
Example DFS
0:0:-1
1:-1:-1 2:-1:-1
3:-1:-1 4:-1:-1
5:2:-1
6:-1:-1
7:-1:-1
8:-1:-1
9:-1:-1
10:-1:-1
11:-1:-1
12:3:-1
13:-1:-1
14:1:-1
![Page 27: Graph Traversals - SCHOOL OF COMPUTER SCIENCE, Carnegie Mellon](https://reader031.vdocument.in/reader031/viewer/2022020703/61fb2f1e2e268c58cd5b262a/html5/thumbnails/27.jpg)
Example DFS
0:0:-1
1:-1:-1 2:4:-1
3:-1:-1 4:-1:-1
5:2:-1
6:-1:-1
7:-1:-1
8:-1:-1
9:-1:-1
10:-1:-1
11:-1:-1
12:3:-1
13:-1:-1
14:1:-1
![Page 28: Graph Traversals - SCHOOL OF COMPUTER SCIENCE, Carnegie Mellon](https://reader031.vdocument.in/reader031/viewer/2022020703/61fb2f1e2e268c58cd5b262a/html5/thumbnails/28.jpg)
Example DFS
0:0:-1
1:5:-1 2:4:-1
3:-1:-1 4:-1:-1
5:2:-1
6:-1:-1
7:-1:-1
8:-1:-1
9:-1:-1
10:-1:-1
11:-1:-1
12:3:-1
13:-1:-1
14:1:-1
![Page 29: Graph Traversals - SCHOOL OF COMPUTER SCIENCE, Carnegie Mellon](https://reader031.vdocument.in/reader031/viewer/2022020703/61fb2f1e2e268c58cd5b262a/html5/thumbnails/29.jpg)
Example DFS
0:0:-1
1:5:-1 2:4:-1
3:-1:-1 4:-1:-1
5:2:-1
6:-1:-1
7:6:-1
8:-1:-1
9:-1:-1
10:-1:-1
11:-1:-1
12:3:-1
13:-1:-1
14:1:-1
![Page 30: Graph Traversals - SCHOOL OF COMPUTER SCIENCE, Carnegie Mellon](https://reader031.vdocument.in/reader031/viewer/2022020703/61fb2f1e2e268c58cd5b262a/html5/thumbnails/30.jpg)
Example DFS
0:0:-1
1:5:-1 2:4:-1
3:-1:-1 4:-1:-1
5:2:-1
6:7:-1
7:6:-1
8:-1:-1
9:-1:-1
10:-1:-1
11:-1:-1
12:3:-1
13:-1:-1
14:1:-1
![Page 31: Graph Traversals - SCHOOL OF COMPUTER SCIENCE, Carnegie Mellon](https://reader031.vdocument.in/reader031/viewer/2022020703/61fb2f1e2e268c58cd5b262a/html5/thumbnails/31.jpg)
Example DFS
0:0:-1
1:5:-1 2:4:-1
3:-1:-1 4:-1:-1
5:2:-1
6:7:-1
7:6:-1
8:-1:-1
9:-1:-1
10:-1:-1
11:-1:-1
12:3:-1
13:8:-1
14:1:-1
![Page 32: Graph Traversals - SCHOOL OF COMPUTER SCIENCE, Carnegie Mellon](https://reader031.vdocument.in/reader031/viewer/2022020703/61fb2f1e2e268c58cd5b262a/html5/thumbnails/32.jpg)
Example DFS
0:0:-1
1:5:-1 2:4:-1
3:-1:-1 4:-1:-1
5:2:-1
6:7:-1
7:6:-1
8:-1:-1
9:-1:-1
10:-1:-1
11:9:-1
12:3:-1
13:8:-1
14:1:-1
![Page 33: Graph Traversals - SCHOOL OF COMPUTER SCIENCE, Carnegie Mellon](https://reader031.vdocument.in/reader031/viewer/2022020703/61fb2f1e2e268c58cd5b262a/html5/thumbnails/33.jpg)
Example DFS
0:0:-1
1:5:-1 2:4:-1
3:-1:-1 4:-1:-1
5:2:-1
6:7:-1
7:6:-1
8:-1:-1
9:-1:-1
10:10:-1
11:9:-1
12:3:-1
13:8:-1
14:1:-1
![Page 34: Graph Traversals - SCHOOL OF COMPUTER SCIENCE, Carnegie Mellon](https://reader031.vdocument.in/reader031/viewer/2022020703/61fb2f1e2e268c58cd5b262a/html5/thumbnails/34.jpg)
Example DFS
0:0:-1
1:5:-1 2:4:-1
3:-1:-1 4:-1:-1
5:2:-1
6:7:14
7:6:-1
8:15:-1
9:-1:-1
10:10:11
11:9:12
12:3:-1
13:8:13
14:1:-1
![Page 35: Graph Traversals - SCHOOL OF COMPUTER SCIENCE, Carnegie Mellon](https://reader031.vdocument.in/reader031/viewer/2022020703/61fb2f1e2e268c58cd5b262a/html5/thumbnails/35.jpg)
Example DFS
0:0:-1
1:5:18 2:4:19
3:-1:-1 4:23:-1
5:2:21
6:7:14
7:6:17
8:15:16
9:-1:-1
10:10:11
11:9:12
12:3:20
13:8:13
14:1:22
![Page 36: Graph Traversals - SCHOOL OF COMPUTER SCIENCE, Carnegie Mellon](https://reader031.vdocument.in/reader031/viewer/2022020703/61fb2f1e2e268c58cd5b262a/html5/thumbnails/36.jpg)
Example DFS
0:0:-1
1:5:18 2:4:19
3:24:-1 4:23:-1
5:2:21
6:7:14
7:6:17
8:15:16
9:-1:-1
10:10:11
11:9:12
12:3:20
13:8:13
14:1:22
![Page 37: Graph Traversals - SCHOOL OF COMPUTER SCIENCE, Carnegie Mellon](https://reader031.vdocument.in/reader031/viewer/2022020703/61fb2f1e2e268c58cd5b262a/html5/thumbnails/37.jpg)
Example DFS
0:0:-1
1:5:18 2:4:19
3:24:-1 4:23:-1
5:2:21
6:7:14
7:6:17
8:15:16
9:25:-1
10:10:11
11:9:12
12:3:20
13:8:13
14:1:22
![Page 38: Graph Traversals - SCHOOL OF COMPUTER SCIENCE, Carnegie Mellon](https://reader031.vdocument.in/reader031/viewer/2022020703/61fb2f1e2e268c58cd5b262a/html5/thumbnails/38.jpg)
Example DFS
0:0:29
1:5:18 2:4:19
3:24:27 4:23:28
5:2:21
6:7:14
7:6:17
8:15:16
9:25:26
10:10:11
11:9:12
12:3:20
13:8:13
14:1:22
![Page 39: Graph Traversals - SCHOOL OF COMPUTER SCIENCE, Carnegie Mellon](https://reader031.vdocument.in/reader031/viewer/2022020703/61fb2f1e2e268c58cd5b262a/html5/thumbnails/39.jpg)
General Tree Growing (following Gross & Yellen)
We can think of BFS and DFS (and several other algorithms) asspecial cases of tree growing:
I Let T be the current tree T , and
I Maintain a list of frontier edges: the set of edges of G thathave one endpoint in T and one endpoint not in T :
v
I Repeatedly choose a frontier edge (somehow) and add it to T .
![Page 40: Graph Traversals - SCHOOL OF COMPUTER SCIENCE, Carnegie Mellon](https://reader031.vdocument.in/reader031/viewer/2022020703/61fb2f1e2e268c58cd5b262a/html5/thumbnails/40.jpg)
Tree Growing
TreeGrowing(graph G, vertex v, func nextEdge):
T = (v,∅)S = set of edges incident to v
While S is not empty:
e = nextEdge(G, S)
T = T + e // add edge e to T
S = updateFrontier(G, S, e)
return T
I The function nextEdge(G, S) returns a frontier edge from S .
I updateFrontier(G, S, e) returns the new frontier after weadd edge e to T.
![Page 41: Graph Traversals - SCHOOL OF COMPUTER SCIENCE, Carnegie Mellon](https://reader031.vdocument.in/reader031/viewer/2022020703/61fb2f1e2e268c58cd5b262a/html5/thumbnails/41.jpg)
Tree Growing
These algorithms are all special cases / variants of Tree Growing,with different versions of nextEdge:
1. Depth-first search
2. Breadth-first search
3. Prim’s minimum spanning tree algorithm
4. Dijkstra’s shortest path
5. A*
![Page 42: Graph Traversals - SCHOOL OF COMPUTER SCIENCE, Carnegie Mellon](https://reader031.vdocument.in/reader031/viewer/2022020703/61fb2f1e2e268c58cd5b262a/html5/thumbnails/42.jpg)
BFS & DFS as Tree Growing
What’s nextEdge for DFS?
Select a frontier edge whose tree endpoint was discoveredmost recently.
Why? We can use a stack to implement DFS.Runtime: O(|Edges|)
What’s nextEdge for BFS?
Select a frontier edge whose tree endpoint was discovered earliest.
Why? We can use a queue to implement BFS.Runtime: O(|Edges|)
![Page 43: Graph Traversals - SCHOOL OF COMPUTER SCIENCE, Carnegie Mellon](https://reader031.vdocument.in/reader031/viewer/2022020703/61fb2f1e2e268c58cd5b262a/html5/thumbnails/43.jpg)
BFS & DFS as Tree Growing
What’s nextEdge for DFS?
Select a frontier edge whose tree endpoint was discoveredmost recently.
Why? We can use a stack to implement DFS.Runtime: O(|Edges|)
What’s nextEdge for BFS?
Select a frontier edge whose tree endpoint was discovered earliest.
Why? We can use a queue to implement BFS.Runtime: O(|Edges|)
![Page 44: Graph Traversals - SCHOOL OF COMPUTER SCIENCE, Carnegie Mellon](https://reader031.vdocument.in/reader031/viewer/2022020703/61fb2f1e2e268c58cd5b262a/html5/thumbnails/44.jpg)
BFS & DFS as Tree Growing
What’s nextEdge for DFS?
Select a frontier edge whose tree endpoint was discoveredmost recently.
Why? We can use a stack to implement DFS.Runtime: O(|Edges|)
What’s nextEdge for BFS?
Select a frontier edge whose tree endpoint was discovered earliest.
Why? We can use a queue to implement BFS.Runtime: O(|Edges|)
![Page 45: Graph Traversals - SCHOOL OF COMPUTER SCIENCE, Carnegie Mellon](https://reader031.vdocument.in/reader031/viewer/2022020703/61fb2f1e2e268c58cd5b262a/html5/thumbnails/45.jpg)
Prim’s Algorithm
Prim’s Algorithm: Run TreeGrowing starting with any rootnode, adding the frontier edge with the smallest weight.
Theorem. Prim’s algorithm produces a minimum spanning tree.
ur
v
e
S = set of nodes already in
the tree when e is added
![Page 46: Graph Traversals - SCHOOL OF COMPUTER SCIENCE, Carnegie Mellon](https://reader031.vdocument.in/reader031/viewer/2022020703/61fb2f1e2e268c58cd5b262a/html5/thumbnails/46.jpg)
Implementations of BFS and DFS
![Page 47: Graph Traversals - SCHOOL OF COMPUTER SCIENCE, Carnegie Mellon](https://reader031.vdocument.in/reader031/viewer/2022020703/61fb2f1e2e268c58cd5b262a/html5/thumbnails/47.jpg)
BFS implementation
procedure bfs(G, s):
Q := queue containing only s
while Q not empty
v := Q.front(); Q.remove front()
for w ∈ G.neighbors(v):
if w not seen:
mark w seen
Q.enqueue(w)
![Page 48: Graph Traversals - SCHOOL OF COMPUTER SCIENCE, Carnegie Mellon](https://reader031.vdocument.in/reader031/viewer/2022020703/61fb2f1e2e268c58cd5b262a/html5/thumbnails/48.jpg)
Recursive implementation of DFS
procedure dfs(G, u):
while u has an unvisited neighbor in G
v := an unvisited neighbor of u
mark v visited
dfs(G, v)
![Page 49: Graph Traversals - SCHOOL OF COMPUTER SCIENCE, Carnegie Mellon](https://reader031.vdocument.in/reader031/viewer/2022020703/61fb2f1e2e268c58cd5b262a/html5/thumbnails/49.jpg)
Stack-based implementation of DFS
procedure dfs(G, s):
S := stack containing only s
while S not empty
v := S.pop()
if v not visited:
mark v visited
for w ∈ G.neighbors(v): S.push(w)
![Page 50: Graph Traversals - SCHOOL OF COMPUTER SCIENCE, Carnegie Mellon](https://reader031.vdocument.in/reader031/viewer/2022020703/61fb2f1e2e268c58cd5b262a/html5/thumbnails/50.jpg)
Properties of BFS and DFS
![Page 51: Graph Traversals - SCHOOL OF COMPUTER SCIENCE, Carnegie Mellon](https://reader031.vdocument.in/reader031/viewer/2022020703/61fb2f1e2e268c58cd5b262a/html5/thumbnails/51.jpg)
Property of Non-BFS-Tree Edges
Theorem. Choose x ∈ Li and y ∈ Lj such that {x , y} is an edgein undirected graph G . Then i and j differ by at most 1.
In other words, edges of G that do not appear in the tree connectnodes either in the same layer or adjacent layer.
Proof. Suppose not, and that i < j − 1.
All the neighbors of x will be found by layer i + 1.
Therefore, the layer of y is less than i + 1, so j ≤ i + 1, whichcontradicts i < j − 1.
![Page 52: Graph Traversals - SCHOOL OF COMPUTER SCIENCE, Carnegie Mellon](https://reader031.vdocument.in/reader031/viewer/2022020703/61fb2f1e2e268c58cd5b262a/html5/thumbnails/52.jpg)
A property of Non-DFS-Tree Edges
Theorem. Let x and y be nodes in the DFS tree TG such that{x , y} is an edge in undirected graph G . Then one of x or y is anancestor of the other in TG .
Proof. Suppose, wlog, x is reached first in the DFS.
All the nodes that are marked explored between first encounteringx and leaving x for the last time are descendants of x in TG .
When we reach x , node y must not yet have been explored.
It must become explored before leaving x for the last time(otherwise, we should add {x , y} to TG ). Hence, y is a descendentof x in TG .