Applications of DFS and BFS
Transcript of Applications of DFS and BFS
![Page 1: Applications of DFS and BFS](https://reader031.fdocuments.us/reader031/viewer/2022021416/5891bfd91a28ab93088bee6f/html5/thumbnails/1.jpg)
Applications of DFS and BFS
Slides by Carl Kingsford
Feb. 3, 2014
Based on/Reading: Chapter 3 of Kleinberg & Tardos
![Page 2: Applications of DFS and BFS](https://reader031.fdocuments.us/reader031/viewer/2022021416/5891bfd91a28ab93088bee6f/html5/thumbnails/2.jpg)
An Application of BFS
![Page 3: Applications of DFS and BFS](https://reader031.fdocuments.us/reader031/viewer/2022021416/5891bfd91a28ab93088bee6f/html5/thumbnails/3.jpg)
Testing Bipartiteness
A graph is bipartite if you can divide its nodes into 2 parts (S , |V | − S)(a cut) so that every edge goes between the two parts S and |V | − S andnot within a single part.
Problem: Determine if a graph G is bipartite.
Bipartite graphs can’t contain odd cycles:
2
3 4
5
6
7
1
![Page 4: Applications of DFS and BFS](https://reader031.fdocuments.us/reader031/viewer/2022021416/5891bfd91a28ab93088bee6f/html5/thumbnails/4.jpg)
Bipartite Testing
How can we test if G is bipartite?
I Do a BFS starting from some node s.
I Color even levels “blue” and odd levels “red.”
I Check each edge to see if any edge has both endpoints thesame level.
Notice: Nodes in adjacent levels must get different colors becauseby construction there are edges between adjacent levels.
Therefore, the coloring above, is the only possible coloring (up torenaming the colors).
![Page 5: Applications of DFS and BFS](https://reader031.fdocuments.us/reader031/viewer/2022021416/5891bfd91a28ab93088bee6f/html5/thumbnails/5.jpg)
Bipartite Testing
How can we test if G is bipartite?
I Do a BFS starting from some node s.
I Color even levels “blue” and odd levels “red.”
I Check each edge to see if any edge has both endpoints thesame level.
Notice: Nodes in adjacent levels must get different colors becauseby construction there are edges between adjacent levels.
Therefore, the coloring above, is the only possible coloring (up torenaming the colors).
![Page 6: Applications of DFS and BFS](https://reader031.fdocuments.us/reader031/viewer/2022021416/5891bfd91a28ab93088bee6f/html5/thumbnails/6.jpg)
Bipartite Testing
How can we test if G is bipartite?
I Do a BFS starting from some node s.
I Color even levels “blue” and odd levels “red.”
I Check each edge to see if any edge has both endpoints thesame level.
Notice: Nodes in adjacent levels must get different colors becauseby construction there are edges between adjacent levels.
Therefore, the coloring above, is the only possible coloring (up torenaming the colors).
![Page 7: Applications of DFS and BFS](https://reader031.fdocuments.us/reader031/viewer/2022021416/5891bfd91a28ab93088bee6f/html5/thumbnails/7.jpg)
Proof of Correctness for Bipartite Testing
One of two cases happen:
1. There is no edge of G between two nodes of the same layer.In this case, every edge just connects two nodes in adjacentlayers. But adjacent layers are oppositely colored, so G mustbe bipartite.
2. There is an edge of G joining two nodes x and y of the samelayer Lj . Let z ∈ Li be the least common ancestor of x and yin the BFS tree T .z − x − y − z is a cycle of length 2(j − i) + 1, which is odd, soG is not bipartite.
![Page 8: Applications of DFS and BFS](https://reader031.fdocuments.us/reader031/viewer/2022021416/5891bfd91a28ab93088bee6f/html5/thumbnails/8.jpg)
Correctness, Picture
z
x y
Li
Lj
j - i
x − z − y − x is a cycle of length 2(j − i) + 1, which is an oddnumber.
![Page 9: Applications of DFS and BFS](https://reader031.fdocuments.us/reader031/viewer/2022021416/5891bfd91a28ab93088bee6f/html5/thumbnails/9.jpg)
An Application of DFS
![Page 10: Applications of DFS and BFS](https://reader031.fdocuments.us/reader031/viewer/2022021416/5891bfd91a28ab93088bee6f/html5/thumbnails/10.jpg)
DAGs
I A directed, acyclic graph (DAG) is a graph that contains nodirected cycles. (After leaving any node u you can never getback to u by following edges along the arrows.)
I DAGs are very useful in modeling project dependencies: Taski has to be done before task j and k which have to be donebefore m.
i
j
k
m
g
f
e
h
![Page 11: Applications of DFS and BFS](https://reader031.fdocuments.us/reader031/viewer/2022021416/5891bfd91a28ab93088bee6f/html5/thumbnails/11.jpg)
Topological Sort
Given a DAG D representing dependencies, how do you order thejobs so that when a job is started, all its dependencies are done?
Topological Sort. Given a DAG D = (V ,E ), find a mapping f fromV to {1, . . . , |V |}), so that for every edge (u, v) ∈ E , f (u) < f (v),and each integer 1 ≤ i ≤ |V | is mapped to exactly once.
e a m g i h b k1 2 3 4 5 6 7 8
f(e) = 1f(a) = 2
...
![Page 12: Applications of DFS and BFS](https://reader031.fdocuments.us/reader031/viewer/2022021416/5891bfd91a28ab93088bee6f/html5/thumbnails/12.jpg)
Topological Sort, II
Theorem. Every DAG contains a vertex with no incoming edges.
Proof. Suppose not.
Then keep following edges backward and in fewer than n + 1 stepsyou’ll reach a node you’ve already visited.
This is a directed cycle, contradicting that the graph is a DAG.
How can we turn this into an algorithm?
![Page 13: Applications of DFS and BFS](https://reader031.fdocuments.us/reader031/viewer/2022021416/5891bfd91a28ab93088bee6f/html5/thumbnails/13.jpg)
Topological Sort, II
Theorem. Every DAG contains a vertex with no incoming edges.
Proof. Suppose not.
Then keep following edges backward and in fewer than n + 1 stepsyou’ll reach a node you’ve already visited.
This is a directed cycle, contradicting that the graph is a DAG.
How can we turn this into an algorithm?
![Page 14: Applications of DFS and BFS](https://reader031.fdocuments.us/reader031/viewer/2022021416/5891bfd91a28ab93088bee6f/html5/thumbnails/14.jpg)
Topological Sort, II
Theorem. Every DAG contains a vertex with no incoming edges.
Proof. Suppose not.
Then keep following edges backward and in fewer than n + 1 stepsyou’ll reach a node you’ve already visited.
This is a directed cycle, contradicting that the graph is a DAG.
How can we turn this into an algorithm?
![Page 15: Applications of DFS and BFS](https://reader031.fdocuments.us/reader031/viewer/2022021416/5891bfd91a28ab93088bee6f/html5/thumbnails/15.jpg)
Topological Sort Algorithm
Topological sort:
1. Let i = 1
2. Find a node u with no incoming edges, and let f (u) = i
3. Delete u from the graph
4. Increment i
Implementation: Maintain
I Income[w] = number of incoming edges for node w
I a list S of nodes that currently have no incoming edges.
When we delete a node u, we decrement Income[w] for allneighbors w of u. If Income[w] becomes 0, we add w to S .
![Page 16: Applications of DFS and BFS](https://reader031.fdocuments.us/reader031/viewer/2022021416/5891bfd91a28ab93088bee6f/html5/thumbnails/16.jpg)
Can we get stuck in this case?
u
v
w
x
all neighbors of u havein-degree = 2
othernodes with
in-degree ≥ 1u is the only node with in-degree = 0
No, because the above can’t happen: after removing u, theremaining graph is still a DAG.
![Page 17: Applications of DFS and BFS](https://reader031.fdocuments.us/reader031/viewer/2022021416/5891bfd91a28ab93088bee6f/html5/thumbnails/17.jpg)
Can we get stuck in this case?
u
v
w
x
all neighbors of u havein-degree = 2
othernodes with
in-degree ≥ 1u is the only node with in-degree = 0
No, because the above can’t happen: after removing u, theremaining graph is still a DAG.
![Page 18: Applications of DFS and BFS](https://reader031.fdocuments.us/reader031/viewer/2022021416/5891bfd91a28ab93088bee6f/html5/thumbnails/18.jpg)
Topological Sort Running Time
1. Initialize Income[w] array:I Count the number of edges adjacent to each node.I Implement via BFS or DFS.I Total running time O(|V |+ |E |).
2. For each node that currently has no incoming edges,decrement an entry in the income array, and possibly addsomething to S
I This executes O(|V |) times.I Handling each neighbor (decrement, add to S) takes O(1)
time.I So this entire step takes O(|V |+ |E |) time.
Total running time is: O(|V |+ |E |) +O(|V |+ |E |) = O(|V |+ |E |).This is O(|E |) for connected graphs.
![Page 19: Applications of DFS and BFS](https://reader031.fdocuments.us/reader031/viewer/2022021416/5891bfd91a28ab93088bee6f/html5/thumbnails/19.jpg)
Discovery and Finishing Times
DFS can be used to associate 2 numbers with each node of agraph G :
I discovery time: d [u] = the time at which u is first visited
I finishing time: f [u] = the time at which all u and all itsneighbors have been visited.
Clearly d [u] ≤ f [u].
u
v
d[u] f[u]
d[v] f[v] v
u
![Page 20: Applications of DFS and BFS](https://reader031.fdocuments.us/reader031/viewer/2022021416/5891bfd91a28ab93088bee6f/html5/thumbnails/20.jpg)
Non-DFS-Trees Edges of a DAG
Let (u, v) be an edge of a DAG D. What can we say about therelationship between f [u] and f [v ]?
Back edge
tree edgeforwardedge
Right-Left EdgeLeft-Right
Edge
Back edges and Left-Right edges cannot occur=⇒ f [v ] < f [u] if (u, v) ∈ D.
![Page 21: Applications of DFS and BFS](https://reader031.fdocuments.us/reader031/viewer/2022021416/5891bfd91a28ab93088bee6f/html5/thumbnails/21.jpg)
Non-DFS-Trees Edges of a DAG
Let (u, v) be an edge of a DAG D. What can we say about therelationship between f [u] and f [v ]?
Back edge
tree edgeforwardedge
Right-Left EdgeLeft-Right
Edge
Back edges and Left-Right edges cannot occur=⇒ f [v ] < f [u] if (u, v) ∈ D.
![Page 22: Applications of DFS and BFS](https://reader031.fdocuments.us/reader031/viewer/2022021416/5891bfd91a28ab93088bee6f/html5/thumbnails/22.jpg)
Topological Sort Via Finishing Times
Another Topological Sort Algorithm:
Every edge (u, v) in a DAG has f [v ] < f [u].
If we list nodes from largest f [u] to smallest f [u] then every edgegoes from left to right.
Exactly a topological sort.
So: as each node is finished, add it to the front of a linked list.