Download presentation
Presentation is loading. Please wait.
1
CSE 421: Introduction to Algorithms
Breadth First Search Yin Tat Lee
2
Properties of BFS Claim: All nontree edges join vertices on the same or adjacent levels of the tree Proof: Consider an edge {π₯,π¦} Say π₯ is first discovered and it is added to level π. We show y will be at level π or π+1 This is because when vertices incident to π₯ are considered in the loop, if π¦ is still undiscovered, it will be discovered and added to level π+1.
3
Properties of BFS Lemma: All vertices at level π of BFS(π ) have shortest path distance π to π . Claim: If πΏ π£ =π then shortest path β€π Pf: Because there is a path of length π from π to π£ in the BFS tree Claim: If shortest path =π then πΏ π£ β€π Pf: If shortest path =π, then say π = π£ 0 , π£ 1 ,β¦, π£ π =π£ is the shortest path to v. By previous claim, πΏ π£ 1 β€πΏ π£ 0 +1 πΏ π£ 2 β€πΏ π£ 1 +1 β¦ πΏ π£ π β€πΏ π£ πβ1 +1 So, πΏ π£ π β€π. This proves the lemma.
4
Why Trees? Trees are simpler than graphs
Many statements can be proved on trees by induction So, computational problems on trees are simpler than general graphs This is often a good way to approach a graph problem: Find a "nice" tree in the graph, i.e., one such that non-tree edges have some simplifying structure Solve the problem on the tree Use the solution on the tree to find a βgoodβ solution on the graph
5
CSE 421: Introduction to Algorithms
Application of BFS Yin Tat Lee
6
BFS Application: Connected Component
We want to answer the following type questions (fast): Given vertices π’,π£ is there a path from π’ to π£ in πΊ? Idea: Create an array π΄ such that For all π’ in the same connected component, π΄[π’] is same. Therefore, question reduces to If π΄ π’ =π΄[π£]?
7
BFS Application: Connected Component
Initial State: All vertices undiscovered, π = 0 For π£ = 1 to π do If state(π£) != fully-explored then Run BFS(π£) Set π΄ π’ βπ for each π’ found in BFS(π£) π=π+1 Note: We no longer initialize to undiscovered in the BFS subroutine Total Cost: π(π+π) In every connected component with π π vertices and π π edges BFS takes time π π π + π π . Note: one can use DFS instead of BFS.
8
Connected Components Lesson: We can execute any algorithm on disconnected graphs by running it on each connected component. We can use the previous algorithm to detect connected components. There is no overhead, because the algorithm runs in time π(π+π). So, from now on, we can (almost) always assume the input graph is connected.
9
Cycles in Graphs Claim: If an π vertices graph πΊ has at least π edges, then it has a cycle. Proof: If πΊ is connected, then it cannot be a tree. Because every tree has πβ1 edges. So, it has a cycle. Suppose πΊ is disconnected. Say connected components of G have π 1 ,β¦, π π vertices where π 1 +β¦+ π π =π Since πΊ has β₯π edges, there must be some π such that a component has π π vertices with at least π π edges. Therefore, in that component we do not have a tree, so there is a cycle.
10
Bipartite Graphs Definition: An undirected graph πΊ=(π,πΈ) is bipartite
if you can partition the node set into 2 parts (say, blue/red or left/right) so that all edges join nodes in different parts i.e., no edge has both ends in the same part. Application: Scheduling: machine=red, jobs=blue Stable Matching: men=blue, wom=red a bipartite graph
11
Testing Bipartiteness
Problem: Given a graph πΊ, is it bipartite? Many graph problems become: Easier/Tractable if the underlying graph is bipartite (matching) Before attempting to design an algorithm, we need to understand structure of bipartite graphs. v2 v2 v3 v1 v4 v6 v5 v4 v3 v5 v6 v7 v1 v7 a bipartite graph G another drawing of G
12
An Obstruction to Bipartiteness
Lemma: If πΊ is bipartite, then it does not contain an odd length cycle. Proof: We cannot 2-color an odd cycle, let alone πΊ. ? bipartite (2-colorable) not bipartite (not 2-colorable)
13
A Characterization of Bipartite Graphs
Lemma: Let πΊ be a connected graph, and let πΏ0, β¦, πΏ π be the layers produced by BFS(π ). Exactly one of the following holds. (i) No edge of πΊ joins two nodes of the same layer, and πΊ is bipartite. (ii) An edge of πΊ joins two nodes of the same layer, and πΊ contains an odd-length cycle (and hence is not bipartite). L1 L2 L3 L1 L2 L3 Case (i) Case (ii)
14
A Characterization of Bipartite Graphs
Lemma: Let πΊ be a connected graph, and let πΏ0, β¦, πΏ π be the layers produced by BFS(π ). Exactly one of the following holds. (i) No edge of πΊ joins two nodes of the same layer, and πΊ is bipartite. (ii) An edge of πΊ joins two nodes of the same layer, and πΊ contains an odd-length cycle (and hence is not bipartite). Proof. (i) Suppose no edge joins two nodes in the same layer. By previous lemma, all edges join nodes on adjacent levels. Bipartition: blue = nodes on odd levels, red = nodes on even levels. L1 L2 L3 Case (i)
15
A Characterization of Bipartite Graphs
Lemma: Let πΊ be a connected graph, and let πΏ0, β¦, πΏ π be the layers produced by BFS(π ). Exactly one of the following holds. (i) No edge of πΊ joins two nodes of the same layer, and πΊ is bipartite. (ii) An edge of πΊ joins two nodes of the same layer, and πΊ contains an odd-length cycle (and hence is not bipartite). Proof. (ii) Suppose {π₯, π¦} is an edge & π₯,π¦ in same level πΏ π . Let π§= their lowest common ancestor in BFS tree. Let πΏ π be level containing π§. Consider cycle that takes edge from π₯ to π¦, then tree from π¦ to π§, then tree from π§ to π₯. Its length is 1+ πβπ +(πβπ), which is odd. z = lca(x, y)
16
Obstruction to Bipartiteness
Corollary: A graph πΊ is bipartite if and only if it contains no odd length cycles. Furthermore, one can test bipartiteness using BFS. bipartite (2-colorable) not bipartite (not 2-colorable)
17
Summary of last lecture
BFS(π ) implemented using queue. Edges into then-undiscovered vertices define a tree β the βBreadth First spanning treeβ of πΊ Level π in the tree are exactly all vertices π£ s.t., the shortest path (in πΊ) from the root π to π£ is of length π All nontree edges join vertices on the same or adjacent layers of the tree Applications: Shortest Path Connected component Test bipartiteness / 2-coloring
18
Depth First Search Yin Tat Lee
CSE 421 Depth First Search Yin Tat Lee
19
Depth First Search Follow the first path you find as far as you can go; back up to last unexplored edge when you reach a dead end, then go as far you can Naturally implemented using recursive calls or a stack
20
DFS(s) β Recursive version
Initialization: mark all vertices undiscovered DFS(π£) Mark π£ discovered for each edge {π£,π₯} if (π₯ is undiscovered) Mark π₯ discovered π₯βparent=π’ DFS(π₯) Mark π£ fully-discovered
21
Non-Tree Edges in DFS BFS tree β DFS tree, but, as with BFS, DFS has found a tree in the graph s.t. non-tree edges are "simple" in some way. All non-tree edges join a vertex and one of its descendants/ancestors in the DFS tree
22
DFS(A) A,1 B J C G H K L D F I M E Color code: undiscovered discovered
fully-explored DFS(A) A,1 Suppose edge lists at each vertex are sorted alphabetically Call Stack (Edge list): A (B,J) B J C G H K L D F I M st[] = {1} E
23
DFS(A) A,1 B,2 J C G H K L D F I M E Color code: undiscovered
fully-explored DFS(A) A,1 Call Stack: (Edge list) A (B,J) B (A,C,J) B,2 J C G H K L D F I M st[] = {1,2} E
24
DFS(A) A,1 B,2 J C,3 G H K L D F I M E Color code: undiscovered
fully-explored DFS(A) A,1 Call Stack: (Edge list) A (B,J) B (A,C,J) C (B,D,G,H) B,2 J C,3 G H K L D F I M st[] = {1,2,3} E
25
DFS(A) A,1 B,2 J C,3 G H K L D,4 F I M E Color code: undiscovered
fully-explored DFS(A) A,1 Call Stack: (Edge list) A (B,J) B (A,C,J) C (B,D,G,H) D (C,E,F) B,2 J C,3 G H K L D,4 F I M st[] = {1,2,3,4} E
26
DFS(A) A,1 B,2 J C,3 G H K L D,4 F I M E,5 Color code: undiscovered
fully-explored DFS(A) A,1 Call Stack: (Edge list) A (B,J) B (A,C,J) C (B,D,G,H) D (C,E,F) E (D,F) B,2 J C,3 G H K L D,4 F I M st[] = {1,2,3,4,5} E,5
27
DFS(A) A,1 B,2 J C,3 G H K L D,4 F,6 I M E,5 Color code: undiscovered
fully-explored DFS(A) A,1 Call Stack: (Edge list) A (B,J) B (A,C,J) C (B,D,G,H) D (C,E,F) E (D,F) F (D,E,G) B,2 J C,3 G H K L D,4 F,6 I M st[] = {1,2,3,4,5,6} E,5
28
DFS(A) A,1 B,2 J C,3 G,7 H K L D,4 F,6 I M E,5 Color code:
undiscovered discovered fully-explored DFS(A) A,1 Call Stack: (Edge list) A (B,J) B (A,C,J) C (B,D,G,H) D (C,E,F) E (D,F) F (D,E,G) G (C,F) B,2 J C,3 G,7 H K L D,4 F,6 I M st[] = {1,2,3,4,5,6,7} E,5
29
DFS(A) A,1 B,2 J C,3 G,7 H K L D,4 F,6 I M E,5 Color code:
undiscovered discovered fully-explored DFS(A) A,1 Call Stack: (Edge list) A (B,J) B (A,C,J) C (B,D,G,H) D (C,E,F) E (D,F) F (D,E,G) G (C,F) B,2 J C,3 G,7 H K L D,4 F,6 I M st[] = {1,2,3,4,5,6,7} E,5
30
DFS(A) A,1 B,2 J C,3 G,7 H K L D,4 F,6 I M E,5 Color code:
undiscovered discovered fully-explored DFS(A) A,1 Call Stack: (Edge list) A (B,J) B (A,C,J) C (B,D,G,H) D (C,E,F) E (D,F) F (D,E,G) B,2 J C,3 G,7 H K L D,4 F,6 I M st[] = {1,2,3,4,5,6} E,5
31
DFS(A) A,1 B,2 J C,3 G,7 H K L D,4 F,6 I M E,5 Color code:
undiscovered discovered fully-explored DFS(A) A,1 Call Stack: (Edge list) A (B,J) B (A,C,J) C (B,D,G,H) D (C,E,F) E (D,F) B,2 J C,3 G,7 H K L D,4 F,6 I M st[] = {1,2,3,4,5} E,5
32
DFS(A) A,1 B,2 J C,3 G,7 H K L D,4 F,6 I M E,5 Color code:
undiscovered discovered fully-explored DFS(A) A,1 Call Stack: (Edge list) A (B,J) B (A,C,J) C (B,D,G,H) D (C,E,F) B,2 J C,3 G,7 H K L D,4 F,6 I M st[] = {1,2,3,4} E,5
33
DFS(A) A,1 B,2 J C,3 G,7 H K L D,4 F,6 I M E,5 Color code:
undiscovered discovered fully-explored DFS(A) A,1 Call Stack: (Edge list) A (B,J) B (A,C,J) C (B,D,G,H) B,2 J C,3 G,7 H K L D,4 F,6 I M st[] = {1,2,3} E,5
34
DFS(A) A,1 B,2 J C,3 G,7 H,8 K L D,4 F,6 I M E,5 Color code:
undiscovered discovered fully-explored DFS(A) A,1 Call Stack: (Edge list) A (B,J) B (A,C,J) C (B,D,G,H) H (C,I,J) B,2 J C,3 G,7 H,8 K L D,4 F,6 I M st[] = {1,2,3,8} E,5
35
DFS(A) A,1 B,2 J C,3 G,7 H,8 K L D,4 F,6 I,9 M E,5 Color code:
undiscovered discovered fully-explored DFS(A) A,1 Call Stack: (Edge list) A (B,J) B (A,C,J) C (B,D,G,H) H (C,I,J) I (H) B,2 J C,3 G,7 H,8 K L D,4 F,6 I,9 M st[] = {1,2,3,8,9} E,5
36
DFS(A) A,1 B,2 J C,3 G,7 H,8 K L D,4 F,6 I,9 M E,5 Color code:
undiscovered discovered fully-explored DFS(A) A,1 Call Stack: (Edge list) A (B,J) B (A,C,J) C (B,D,G,H) H (C,I,J) B,2 J C,3 G,7 H,8 K L D,4 F,6 I,9 M st[] = {1,2,3,8} E,5
37
DFS(A) A,1 B,2 J,10 C,3 G,7 H,8 K L D,4 F,6 I,9 M E,5 Color code:
undiscovered discovered fully-explored DFS(A) A,1 Call Stack: (Edge list) A (B,J) B (A,C,J) C (B,D,G,H) H (C,I,J) J (A,B,H,K,L) B,2 J,10 C,3 G,7 H,8 K L D,4 F,6 I,9 M st[] = {1,2,3,8, 10} E,5
38
DFS(A) A,1 B,2 J,10 C,3 G,7 H,8 K,11 L D,4 F,6 I,9 M E,5 Color code:
undiscovered discovered fully-explored DFS(A) A,1 Call Stack: (Edge list) A (B,J) B (A,C,J) C (B,D,G,H) H (C,I,J) J (A,B,H,K,L) K (J,L) B,2 J,10 C,3 G,7 H,8 K,11 L D,4 F,6 I,9 M st[] = {1,2,3,8,10,11} E,5
39
Color code: undiscovered discovered fully-explored DFS(A) A,1 Call Stack: (Edge list) A (B,J) B (A,C,J) C (B,D,G,H) H (C,I,J) J (A,B,H,K,L) K (J,L) L (J,K,M) B,2 J,10 C,3 G,7 H,8 K,11 L,12 D,4 F,6 I,9 M st[] = {1,2,3,8,10,11,12} E,5
40
Color code: undiscovered discovered fully-explored DFS(A) A,1 Call Stack: (Edge list) A (B,J) B (A,C,J) C (B,D,G,H) H (C,I,J) J (A,B,H,K,L) K (J,L) L (J,K,M) M (L) B,2 J,10 C,3 G,7 H,8 K,11 L,12 D,4 F,6 I,9 M,13 st[] = {1,2,3,8,10,11,12,13} E,5
41
Color code: undiscovered discovered fully-explored DFS(A) A,1 Call Stack: (Edge list) A (B,J) B (A,C,J) C (B,D,G,H) H (C,I,J) J (A,B,H,K,L) K (J,L) L (J,K,M) B,2 J,10 C,3 G,7 H,8 K,11 L,12 D,4 F,6 I,9 M,13 st[] = {1,2,3,8,10,11,12} E,5
42
Color code: undiscovered discovered fully-explored DFS(A) A,1 Call Stack: (Edge list) A (B,J) B (A,C,J) C (B,D,G,H) H (C,I,J) J (A,B,H,K,L) K (J,L) B,2 J,10 C,3 G,7 H,8 K,11 L,12 D,4 F,6 I,9 M,13 st[] = {1,2,3,8,10,11} E,5
43
Color code: undiscovered discovered fully-explored DFS(A) A,1 Call Stack: (Edge list) A (B,J) B (A,C,J) C (B,D,G,H) H (C,I,J) J (A,B,H,K,L) B,2 J,10 C,3 G,7 H,8 K,11 L,12 D,4 F,6 I,9 M,13 st[] = {1,2,3,8, 10} E,5
44
Color code: undiscovered discovered fully-explored DFS(A) A,1 Call Stack: (Edge list) A (B,J) B (A,C,J) C (B,D,G,H) H (C,I,J) J (A,B,H,K,L) B,2 J,10 C,3 G,7 H,8 K,11 L,12 D,4 F,6 I,9 M,13 st[] = {1,2,3,8, 10} E,5
45
Color code: undiscovered discovered fully-explored DFS(A) A,1 Call Stack: (Edge list) A (B,J) B (A,C,J) C (B,D,G,H) H (C,I,J) B,2 J,10 C,3 G,7 H,8 K,11 L,12 D,4 F,6 I,9 M,13 st[] = {1,2,3,8} E,5
46
Color code: undiscovered discovered fully-explored DFS(A) A,1 Call Stack: (Edge list) A (B,J) B (A,C,J) C (B,D,G,H) B,2 J,10 C,3 G,7 H,8 K,11 L,12 D,4 F,6 I,9 M,13 st[] = {1,2,3} E,5
47
Color code: undiscovered discovered fully-explored DFS(A) A,1 Call Stack: (Edge list) A (B,J) B (A,C,J) B,2 J,10 C,3 G,7 H,8 K,11 L,12 D,4 F,6 I,9 M,13 st[] = {1,2} E,5
48
Color code: undiscovered discovered fully-explored DFS(A) A,1 Call Stack: (Edge list) A (B,J) B (A,C,J) B,2 J,10 C,3 G,7 H,8 K,11 L,12 D,4 F,6 I,9 M,13 st[] = {1,2} E,5
49
Color code: undiscovered discovered fully-explored DFS(A) A,1 Call Stack: (Edge list) A (B,J) B,2 J,10 C,3 G,7 H,8 K,11 L,12 D,4 F,6 I,9 M,13 st[] = {1} E,5
50
Color code: undiscovered discovered fully-explored DFS(A) A,1 Call Stack: (Edge list) A (B,J) B,2 J,10 C,3 G,7 H,8 K,11 L,12 D,4 F,6 I,9 M,13 st[] = {1} E,5
51
Color code: undiscovered discovered fully-explored DFS(A) A,1 Call Stack: (Edge list) TA-DA!! B,2 J,10 C,3 G,7 H,8 K,11 L,12 D,4 F,6 I,9 M,13 st[] = {} E,5
52
DFS(A) Edge code: Tree edge Back edge A,1 B,2 J,10 C,3 G,7 H,8 K,11 L,12 D,4 F,6 I,9 M,13 E,5
53
A,1 Edge code: Tree edge Back edge No Cross Edges! DFS(A) B,2 C,3 H,8 D,4 J,10 E,5 I,9 By the way, suppose Iβve run dfs and each node has a dfs# Suppose I then hand you an edge and tell you the dfs#βs of the two endpoints, how do you know which of the endpoints is the ancestor? K,11 F,6 L,12 G,7 M,13
54
Properties of (undirected) DFS
Like BFS(π ): DFS(π ) visits π₯ iff there is a path in G from π to π₯ So, we can use DFS to find connected components Edges into then-undiscovered vertices define a tree β the "depth first spanning tree" of G Unlike the BFS tree: The DF spanning tree isn't minimum depth Its levels don't reflect min distance from the root Non-tree edges never join vertices on the same or adjacent levels
55
Non-Tree Edges in DFS Lemma: For every edge {π₯,π¦}, if {π₯,π¦} is not in DFS tree, then one of π₯ or π¦ is an ancestor of the other in the tree. Proof: Suppose that π₯ is visited first. Therefore DFS(π₯) was called before DFS(π¦) Since {π₯,π¦} is not in DFS tree, π¦ was visited when the edge {π₯,π¦} was examined during DFS(π₯) Therefore π¦ was visited during the call to DFS(π₯) so π¦ is a descendant of π₯.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.