Download presentation
Presentation is loading. Please wait.
Published byΞΞ±Ξ½ΞΈΞ―ΟΟΞ· ΞιδαΟΞΊΞ¬Ξ»ΞΏΟ Modified over 6 years ago
1
CSE 421: Introduction to Algorithms
Breadth First Search Yin Tat Lee
2
Degree 1 vertices Claim: If G has no cycle, then it has a vertex of degree β€1 (Every tree has a leaf) Proof: (By contradiction) Suppose every vertex has degree β₯2. Start from a vertex π£ 1 and follow a path, π£ 1 ,β¦, π£ π when we are at π£ π we choose the next vertex to be different from π£ πβ1 . We can do so because deg π£ π β₯2. The first time that we see a repeated vertex ( π£ π = π£ π ) we get a cycle. We always get a repeated vertex because πΊ has finitely many vertices π£ 1 π£ 5 π£ 4 π£ 2 π£ 3
3
Trees and Induction Claim: Show that every tree with π vertices has πβ1 edges. Proof: (Induction on π.) Base Case: π=1, the tree has no edge Inductive Step: Let π be a tree with π vertices. So, π has a vertex π£ of degree 1. Remove π£ and the neighboring edge, and let πβ be the new graph. We claim πβ is a tree: It has no cycle, and it must be connected. So, πβ has πβ2 edges and π has πβ1 edges.
4
Graph Traversal Walk (via edges) from a fixed starting vertex π to all vertices reachable from π . Breadth First Search (BFS): Order nodes in successive layers based on distance from π Depth First Search (DFS): More natural approach for exploring a maze; Applications of BFS: Finding shortest path for unit-length graphs Finding connected components of a graph Testing bipartiteness
5
Breadth First Search (BFS)
Completely explore the vertices in order of their distance from π . Three states of vertices: Undiscovered Discovered Fully-explored Naturally implemented using a queue The queue will always have the list of Discovered vertices
6
BFS implementation Initialization: mark all vertices "undiscovered"
mark π discovered queue = { π } while queue not empty π’ = remove_first(queue) for each edge {π’,π₯} if (π₯ is undiscovered) mark π₯ discovered append π₯ on queue mark π’ fully-explored
7
BFS(1) 1 2 3 4 6 7 9 5 11 mark s "discovered" queue = { s } 12 10
while queue not empty u = remove_first(queue) for each edge {u,x} if (x is undiscovered) mark x discovered append x on queue mark u fully explore 12 10 8 13
8
BFS(1) 1 2 3 4 6 7 9 5 11 mark s "discovered" queue = { s } 12 10
while queue not empty u = remove_first(queue) for each edge {u,x} if (x is undiscovered) mark x discovered append x on queue mark u fully explore 12 10 8 13
9
BFS(1) 1 2 3 4 6 7 9 5 11 mark s "discovered" queue = { s } 12 10
while queue not empty u = remove_first(queue) for each edge {u,x} if (x is undiscovered) mark x discovered append x on queue mark u fully explore 12 10 8 13
10
BFS(1) 1 2 3 4 6 7 9 5 11 mark s "discovered" queue = { s } 12 10
2 3 4 6 7 9 5 11 mark s "discovered" queue = { s } while queue not empty u = remove_first(queue) for each edge {u,x} if (x is undiscovered) mark x discovered append x on queue mark u fully explore 12 10 8 13
11
BFS(1) 1 2 3 4 6 7 9 5 11 mark s "discovered" queue = { s } 12 10
2 3 4 6 7 9 5 11 mark s "discovered" queue = { s } while queue not empty u = remove_first(queue) for each edge {u,x} if (x is undiscovered) mark x discovered append x on queue mark u fully explore 12 10 8 13
12
BFS(1) 1 2 3 4 6 7 9 5 11 mark s "discovered" queue = { s } 12 10
2 3 4 6 7 9 5 11 mark s "discovered" queue = { s } while queue not empty u = remove_first(queue) for each edge {u,x} if (x is undiscovered) mark x discovered append x on queue mark u fully explore 12 10 8 13
13
BFS(1) 1 2 3 4 6 7 9 5 11 mark s "discovered" queue = { s } 12 10
2 3 4 6 7 9 5 11 mark s "discovered" queue = { s } while queue not empty u = remove_first(queue) for each edge {u,x} if (x is undiscovered) mark x discovered append x on queue mark u fully explore 12 10 8 13
14
BFS(1) 1 2 3 4 6 7 9 5 11 mark s "discovered" queue = { s } 12 10
2 3 4 6 7 9 5 11 mark s "discovered" queue = { s } while queue not empty u = remove_first(queue) for each edge {u,x} if (x is undiscovered) mark x discovered append x on queue mark u fully explore 12 10 8 13
15
BFS(1) 1 2 3 4 6 7 9 5 11 mark s "discovered" queue = { s } 12 10
while queue not empty u = remove_first(queue) for each edge {u,x} if (x is undiscovered) mark x discovered append x on queue mark u fully explore 12 10 8 13
16
BFS Analysis Initialization: mark all vertices "undiscovered" BFS(π )
mark π discovered queue = { π } while queue not empty π’ = remove_first(queue) for each edge {π’,π₯} if (π₯ is undiscovered) mark π₯ discovered append π₯ on queue mark π’ fully-explored O(n) times: At most once per vertex O(m) times: At most twice per edge
17
Properties of BFS BFS(π ) visits a vertex π£ if and only if there is a path from π to π£ 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 levels of the tree
18
BFS Application: Shortest Paths
BFS Tree gives shortest paths from 1 to all vertices 1 2 1 3 4 6 2 7 9 5 3 11 mark s "discovered" queue = { s } while queue not empty u = remove_first(queue) for each edge {u,x} if (x is undiscovered) mark x discovered append x on queue mark u fully explore 12 10 8 4 All edges connect same or adjacent levels 13
19
BFS Application: Shortest Paths
BFS Tree gives shortest paths from 1 to all vertices 1 2 3 1 4 5 6 7 2 8 9 10 11 3 mark s "discovered" queue = { s } while queue not empty u = remove_first(queue) for each edge {u,x} if (x is undiscovered) mark x discovered append x on queue mark u fully explore 4 All edges connect same or adjacent levels 12 13
20
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.
21
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.
22
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
23
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 A[u] = A[v]?
24
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.
25
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.
26
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.
27
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
28
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
29
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)
30
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)
31
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)
32
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)
33
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)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.