Breadth First Search Yin Tat Lee 1 Degree 1 vertices Claim If G has no cycle then it has a vertex of degree Every tree has a leaf Proof By contradiction Suppose every vertex has degree ID: 729766
Download Presentation The PPT/PDF document "CSE 421: Introduction to Algorithms" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
CSE 421: Introduction to Algorithms
Breadth First Search Yin Tat Lee
1Slide2
Degree 1 vertices
Claim: If G has no cycle, then it has a vertex of degree
(Every tree has a leaf)
Proof
: (By contradiction)Suppose every vertex has degree Start from a vertex and follow a path, when we are at we choose the next vertex to be different from We can do so because The first time that we see a repeated vertex ( we get a cycle. We always get a repeated vertex because has finitely many vertices
2
Slide3
Trees and Induction
Claim: Show that every tree with
vertices has
edges.
Proof: (Induction on .)Base Case: , the tree has no edgeInductive Step: Let be a tree with vertices.So, has a vertex of degree .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 edges and has edges.
3Slide4
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 graphsFinding connected components of a graphTesting bipartiteness 4Slide5
Breadth First Search (BFS)
Completely explore the vertices in order of their distance from
.
Three states of vertices:
UndiscoveredDiscoveredFully-exploredNaturally implemented using a queueThe queue will always have the list of Discovered vertices 5Slide6
BFS implementation
Initialization: mark all vertices "undiscovered"
BFS(
)
mark discoveredqueue = { }while queue not empty = remove_first(queue)for each edge if ( is undiscovered) mark discoveredappend on queuemark fully-explored
6Slide7
7
BFS(1)
1
2
31054912
8
136
7
11
Queue:
1 Slide8
8
BFS(1)
1
2
31054912
8
136
7
11
Queue:
2 3 Slide9
9
BFS(1)
1
2
31054912
8
136
7
11
Queue:
3 4Slide10
10
BFS(1)
1
2
31054912
8
136
7
11
Queue:
4 5 6 7Slide11
11
BFS(1)
1
2
31054912
8
136
7
11
Queue:
5 6 7 8 9Slide12
12
BFS(1)
1
2
31054912
8
136
7
11
Queue:
7 8 9 10Slide13
13
BFS(1)
1
2
31054912
8
136
7
11
Queue:
8 9 10 11Slide14
14
BFS(1)
1
2
31054912
8
136
7
11
Queue:
9 10 11 12 13Slide15
15
BFS(1)
1
2
31054912
8
136
7
11
Queue:Slide16
BFS Analysis
Initialization: mark all vertices "undiscovered"
BFS(
)
mark discoveredqueue = { }while queue not empty = remove_first(queue)for each edge if ( is undiscovered) mark discoveredappend on queuemark fully-explored
16
O(m) times: At most twice per edge
O(n) times:
At most once per vertexSlide17
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 17Slide18
18
BFS Application: Shortest Paths
1
2
31054912
8
136
7
11
BFS Tree gives shortest
paths from 1 to all vertices
0
1
2
3
4
All edges connect same
or adjacent levelsSlide19
19
BFS Application: Shortest Paths
1
2
31054912
8
136
7
11
BFS Tree gives shortest
paths from 1 to all vertices
0
1
2
3
4
All edges connect same
or adjacent levelsSlide20
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 This is because when vertices incident to are considered in the loop, if is still undiscovered, it will be discovered and added to level . 20Slide21
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 treeClaim: If shortest path then Pf: If shortest path , then say
is the shortest path to v.
By previous claim,
So,
This proves the lemma.
21Slide22
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 structureSolve the problem on the treeUse the solution on the tree to find a “good” solution on the graph22Slide23
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 thatFor all in the same connected component, is same.Therefore, question reduces toIf A[u] = A[v]? 23Slide24
Initial State: All vertices undiscovered,
For
=
to
do If state() != fully-explored then Run BFS() Set for each found in BFS() Note: We no longer initialize to undiscovered in the BFS subroutineTotal Cost: In every connected component with vertices and
edges BFS takes time
Note: one can use DFS instead of BFS.
24
BFS Application:
Connected ComponentSlide25
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. 25Slide26
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 edges. So, it has a cycle.Suppose is disconnected. Say connected components of G have vertices where 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.
26Slide27
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=blueStable Matching: men=blue, wom=red 27
a bipartite graphSlide28
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. 28v1v2v3
v
6v5
v4
v7
v
2
v
4
v
5
v
7
v
1
v
3
v
6
a bipartite graph G
another drawing of GSlide29
An Obstruction to Bipartiteness
Lemma: If
is bipartite, then it does not contain an odd length cycle.
Proof
: We cannot -color an odd cycle, let alone . 29
bipartite
(2-colorable)
not bipartite
(not 2-colorable)
?Slide30
A Characterization of Bipartite Graphs
Lemma: Let
be a connected graph, and let
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). 30
Case (i)
L
1
L
2
L
3
Case (ii)
L
1
L
2
L
3Slide31
A Characterization of Bipartite Graphs
Lemma: Let
be a connected graph, and let
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. 31
Case (i)
L
1
L
2
L
3
Bipartition:
blue
= nodes on odd levels,
red
= nodes on even levels.Slide32
A Characterization of Bipartite Graphs
Lemma: Let
be a connected graph, and let
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 , which is odd.
32
z = lca(x, y)Slide33
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. 33
bipartite
(2-colorable)
not bipartite
(not 2-colorable)