Slides06 - Connected Components, Bipartite Testingsheldon/teaching/mhc... · Logistics Quizzes Quiz...
Transcript of Slides06 - Connected Components, Bipartite Testingsheldon/teaching/mhc... · Logistics Quizzes Quiz...
![Page 1: Slides06 - Connected Components, Bipartite Testingsheldon/teaching/mhc... · Logistics Quizzes Quiz 5: 74% Other quizzes: ~98% I’ll drop lowest 2 quizzes HW HW 2 back: average 44.9/50,](https://reader036.fdocuments.us/reader036/viewer/2022071018/5fd239ac5c4655153e5e4745/html5/thumbnails/1.jpg)
LogisticsQuizzes
Quiz 5: 74%Other quizzes: ~98%I’ll drop lowest 2 quizzes
HWHW 2 back: average 44.9/50, 5.2 hoursHW 3 dueHW 4 out tonight
No reading for Thursday
Questions?
![Page 2: Slides06 - Connected Components, Bipartite Testingsheldon/teaching/mhc... · Logistics Quizzes Quiz 5: 74% Other quizzes: ~98% I’ll drop lowest 2 quizzes HW HW 2 back: average 44.9/50,](https://reader036.fdocuments.us/reader036/viewer/2022071018/5fd239ac5c4655153e5e4745/html5/thumbnails/2.jpg)
Today
BFS/DFS
Connected components
Bipartite testing
Directed Graphs
![Page 3: Slides06 - Connected Components, Bipartite Testingsheldon/teaching/mhc... · Logistics Quizzes Quiz 5: 74% Other quizzes: ~98% I’ll drop lowest 2 quizzes HW HW 2 back: average 44.9/50,](https://reader036.fdocuments.us/reader036/viewer/2022071018/5fd239ac5c4655153e5e4745/html5/thumbnails/3.jpg)
Connected Components
Definitions, example, proof on board
![Page 4: Slides06 - Connected Components, Bipartite Testingsheldon/teaching/mhc... · Logistics Quizzes Quiz 5: 74% Other quizzes: ~98% I’ll drop lowest 2 quizzes HW HW 2 back: average 44.9/50,](https://reader036.fdocuments.us/reader036/viewer/2022071018/5fd239ac5c4655153e5e4745/html5/thumbnails/4.jpg)
“Meta”-BFS algorithm
while there is an unexplored node s
BFS(s)
end
Example
Running time? argue O(m+n) running time on board
![Page 5: Slides06 - Connected Components, Bipartite Testingsheldon/teaching/mhc... · Logistics Quizzes Quiz 5: 74% Other quizzes: ~98% I’ll drop lowest 2 quizzes HW HW 2 back: average 44.9/50,](https://reader036.fdocuments.us/reader036/viewer/2022071018/5fd239ac5c4655153e5e4745/html5/thumbnails/5.jpg)
Representing Graphs: Adjacency List
Adjacency list. Each node keeps a (linked) list of neighbors.
Find all edges incident to u: O(nu)
1
3
5 4
212345
2 4 51 3 42 51 21 3
![Page 6: Slides06 - Connected Components, Bipartite Testingsheldon/teaching/mhc... · Logistics Quizzes Quiz 5: 74% Other quizzes: ~98% I’ll drop lowest 2 quizzes HW HW 2 back: average 44.9/50,](https://reader036.fdocuments.us/reader036/viewer/2022071018/5fd239ac5c4655153e5e4745/html5/thumbnails/6.jpg)
Running Time?Set explored[u] to be false for all uA = { s } // set of discovered but not explored nodeswhile A is not empty
Take a node u from Aif explored[u] is false
set explored[u] = truefor each edge (u,v) incident to u
add v to Aend
endend
Same reasoning we just did: but now “charge” each line of code to either a node or an edge
O(n)
O(m)
O(m)O(m)
O(n)
![Page 7: Slides06 - Connected Components, Bipartite Testingsheldon/teaching/mhc... · Logistics Quizzes Quiz 5: 74% Other quizzes: ~98% I’ll drop lowest 2 quizzes HW HW 2 back: average 44.9/50,](https://reader036.fdocuments.us/reader036/viewer/2022071018/5fd239ac5c4655153e5e4745/html5/thumbnails/7.jpg)
Graph Traversal: Summary
BFS/DFS: O(n+m)Is G connected?Find connected components of GFind distance of every vertex from sourceGet BFS/DFS trees (useful in some other problems)
BFS: explore by distance, layers, queueDFS: explore deeply, recursive, stack
![Page 8: Slides06 - Connected Components, Bipartite Testingsheldon/teaching/mhc... · Logistics Quizzes Quiz 5: 74% Other quizzes: ~98% I’ll drop lowest 2 quizzes HW HW 2 back: average 44.9/50,](https://reader036.fdocuments.us/reader036/viewer/2022071018/5fd239ac5c4655153e5e4745/html5/thumbnails/8.jpg)
Application of BFS:Bipartite Testing
![Page 9: Slides06 - Connected Components, Bipartite Testingsheldon/teaching/mhc... · Logistics Quizzes Quiz 5: 74% Other quizzes: ~98% I’ll drop lowest 2 quizzes HW HW 2 back: average 44.9/50,](https://reader036.fdocuments.us/reader036/viewer/2022071018/5fd239ac5c4655153e5e4745/html5/thumbnails/9.jpg)
Bipartite GraphsA bipartite graph is an undirected graph G = (V, E) in which the nodes can be colored red or blue such that every edge has one red and one blue end.
is a bipartite graph
is NOT a bipartite graph
Examples? How can we check if a given graph is bipartite?
![Page 10: Slides06 - Connected Components, Bipartite Testingsheldon/teaching/mhc... · Logistics Quizzes Quiz 5: 74% Other quizzes: ~98% I’ll drop lowest 2 quizzes HW HW 2 back: average 44.9/50,](https://reader036.fdocuments.us/reader036/viewer/2022071018/5fd239ac5c4655153e5e4745/html5/thumbnails/10.jpg)
Simple Observation: Odd Cycles
Lemma. If G has a cycle of odd length, then G is not bipartite
Proof on board
![Page 11: Slides06 - Connected Components, Bipartite Testingsheldon/teaching/mhc... · Logistics Quizzes Quiz 5: 74% Other quizzes: ~98% I’ll drop lowest 2 quizzes HW HW 2 back: average 44.9/50,](https://reader036.fdocuments.us/reader036/viewer/2022071018/5fd239ac5c4655153e5e4745/html5/thumbnails/11.jpg)
BFS and Bipartite GraphsLemma. Let G be a connected graph, and let L0, …, Lk be the layers produced by BFS starting at node s. Exactly one of the following holds:(i) No edge of G joins two nodes of the same layer, and G is
bipartite.(ii) An edge of G joins two nodes of the same layer, and G
contains an odd-length cycle (and hence is not bipartite).
![Page 12: Slides06 - Connected Components, Bipartite Testingsheldon/teaching/mhc... · Logistics Quizzes Quiz 5: 74% Other quizzes: ~98% I’ll drop lowest 2 quizzes HW HW 2 back: average 44.9/50,](https://reader036.fdocuments.us/reader036/viewer/2022071018/5fd239ac5c4655153e5e4745/html5/thumbnails/12.jpg)
Layer 1 Layer 2 Layer 3 Layer 4Layer 0
BFS and Bipartite GraphsLemma. Let G be a connected graph, and let L0, …, Lk be the layers produced by BFS starting at node s. Exactly one of the following holds:(i) No edge of G joins two nodes of the same layer, and G is
bipartite.(ii) An edge of G joins two nodes of the same layer, and G
contains an odd-length cycle (and hence is not bipartite).
![Page 13: Slides06 - Connected Components, Bipartite Testingsheldon/teaching/mhc... · Logistics Quizzes Quiz 5: 74% Other quizzes: ~98% I’ll drop lowest 2 quizzes HW HW 2 back: average 44.9/50,](https://reader036.fdocuments.us/reader036/viewer/2022071018/5fd239ac5c4655153e5e4745/html5/thumbnails/13.jpg)
BFS and Bipartite Graphs
Layer 1 Layer 2 Layer 3 Layer 4Layer 0
Lemma. Let G be a connected graph, and let L0, …, Lk be the layers produced by BFS starting at node s. Exactly one of the following holds:(i) No edge of G joins two nodes of the same layer, and G is
bipartite.(ii) An edge of G joins two nodes of the same layer, and G
contains an odd-length cycle (and hence is not bipartite).
![Page 14: Slides06 - Connected Components, Bipartite Testingsheldon/teaching/mhc... · Logistics Quizzes Quiz 5: 74% Other quizzes: ~98% I’ll drop lowest 2 quizzes HW HW 2 back: average 44.9/50,](https://reader036.fdocuments.us/reader036/viewer/2022071018/5fd239ac5c4655153e5e4745/html5/thumbnails/14.jpg)
Algorithm for Bipartite-Testing
Run BFSCheck each non-tree edge
If any has endpoints in same layer, then G is not bipartiteOtherwise, G is bipartite
Running Time?