CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/.../day14.pdf · Breadth-First Search...
Transcript of CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/.../day14.pdf · Breadth-First Search...
![Page 1: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/.../day14.pdf · Breadth-First Search (BFS) •Definition: the distancebetween !,#is the number of edges on the shortest](https://reader033.fdocuments.in/reader033/viewer/2022052007/601c3b8c6cbc99638e0a2ed1/html5/thumbnails/1.jpg)
CS3000: Algorithms & DataPaul Hand
Lecture 14:
• Bipartite Graphs and 2-coloring• Depth First Search
Mar 11, 2019
![Page 2: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/.../day14.pdf · Breadth-First Search (BFS) •Definition: the distancebetween !,#is the number of edges on the shortest](https://reader033.fdocuments.in/reader033/viewer/2022052007/601c3b8c6cbc99638e0a2ed1/html5/thumbnails/2.jpg)
Recap: Graphs/BFS
![Page 3: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/.../day14.pdf · Breadth-First Search (BFS) •Definition: the distancebetween !,#is the number of edges on the shortest](https://reader033.fdocuments.in/reader033/viewer/2022052007/601c3b8c6cbc99638e0a2ed1/html5/thumbnails/3.jpg)
Breadth-First Search (BFS)
• Definition: the distance between !, # is the number of edges on the shortest path from ! to #• Thm: BFS finds distances from ! to other nodes• $% contains all nodes at distance & from !• Nodes not in any layer are not reachable from !
![Page 4: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/.../day14.pdf · Breadth-First Search (BFS) •Definition: the distancebetween !,#is the number of edges on the shortest](https://reader033.fdocuments.in/reader033/viewer/2022052007/601c3b8c6cbc99638e0a2ed1/html5/thumbnails/4.jpg)
Bipartiteness / 2-Coloring
![Page 5: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/.../day14.pdf · Breadth-First Search (BFS) •Definition: the distancebetween !,#is the number of edges on the shortest](https://reader033.fdocuments.in/reader033/viewer/2022052007/601c3b8c6cbc99638e0a2ed1/html5/thumbnails/5.jpg)
2-Coloring
• Problem: Team Forming• Need to form two teams !,#• Some people don’t want to be on the same team as
certain other people• Input: Undirected graph $ = &, '• (, ) ∈ ' means (, ) wont be on the same team
• Output: Split & into two sets !,# so that no pair in either set is connected by an edge
![Page 6: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/.../day14.pdf · Breadth-First Search (BFS) •Definition: the distancebetween !,#is the number of edges on the shortest](https://reader033.fdocuments.in/reader033/viewer/2022052007/601c3b8c6cbc99638e0a2ed1/html5/thumbnails/6.jpg)
2-Coloring (Bipartiteness)
• Equivalent Problem: Is the graph ! bipartite?• A graph ! is bipartite if I can split " into two sets # and $ such that all edges %, ' ∈ ) go between # and $
2 1
3 4
L R
5
![Page 7: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/.../day14.pdf · Breadth-First Search (BFS) •Definition: the distancebetween !,#is the number of edges on the shortest](https://reader033.fdocuments.in/reader033/viewer/2022052007/601c3b8c6cbc99638e0a2ed1/html5/thumbnails/7.jpg)
Activity: Is the following graph bipartite?
![Page 8: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/.../day14.pdf · Breadth-First Search (BFS) •Definition: the distancebetween !,#is the number of edges on the shortest](https://reader033.fdocuments.in/reader033/viewer/2022052007/601c3b8c6cbc99638e0a2ed1/html5/thumbnails/8.jpg)
Activity:
Give an example of a bipartite graph that is not connected
Suppose a graph of 10 nodes is bipartite. What is the maximum number of edgesit can have?
![Page 9: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/.../day14.pdf · Breadth-First Search (BFS) •Definition: the distancebetween !,#is the number of edges on the shortest](https://reader033.fdocuments.in/reader033/viewer/2022052007/601c3b8c6cbc99638e0a2ed1/html5/thumbnails/9.jpg)
Activity: Is the following graph bipartite?Activity
• Determine if there is path between nodes 1 and 2All omitted entries are zero
A 1 2 3 4 5 6 7 8 9 101 1 12 1 1 13 1 1 14 1 1 15 1 1 16 1 1 17 1 18 1 1 1 19 1 110 1
![Page 10: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/.../day14.pdf · Breadth-First Search (BFS) •Definition: the distancebetween !,#is the number of edges on the shortest](https://reader033.fdocuments.in/reader033/viewer/2022052007/601c3b8c6cbc99638e0a2ed1/html5/thumbnails/10.jpg)
Designing an Algorithm to determine if a graph is bipartite
• Key Fact: If ! contains a cycle of odd length, then !is not 2-colorable/bipartite
![Page 11: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/.../day14.pdf · Breadth-First Search (BFS) •Definition: the distancebetween !,#is the number of edges on the shortest](https://reader033.fdocuments.in/reader033/viewer/2022052007/601c3b8c6cbc99638e0a2ed1/html5/thumbnails/11.jpg)
Designing the Algorithm
• Idea for the algorithm: • BFS the graph, coloring nodes as you find them• Color nodes in layer ! purple if ! even, red if ! odd• See if you have succeeded or failed
![Page 12: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/.../day14.pdf · Breadth-First Search (BFS) •Definition: the distancebetween !,#is the number of edges on the shortest](https://reader033.fdocuments.in/reader033/viewer/2022052007/601c3b8c6cbc99638e0a2ed1/html5/thumbnails/12.jpg)
Designing the Algorithm
• Claim: If BFS 2-colored the graph successfully, the graph has been 2-colored successfully• Key Question: Suppose you have not 2-colored the
graph successfully, maybe someone else can do it?
![Page 13: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/.../day14.pdf · Breadth-First Search (BFS) •Definition: the distancebetween !,#is the number of edges on the shortest](https://reader033.fdocuments.in/reader033/viewer/2022052007/601c3b8c6cbc99638e0a2ed1/html5/thumbnails/13.jpg)
Designing the Algorithm
• Claim: If BFS fails, then G contains an odd cycle• If G contains an odd cycle then G can’t be 2-colored!
![Page 14: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/.../day14.pdf · Breadth-First Search (BFS) •Definition: the distancebetween !,#is the number of edges on the shortest](https://reader033.fdocuments.in/reader033/viewer/2022052007/601c3b8c6cbc99638e0a2ed1/html5/thumbnails/14.jpg)
Depth-First Search (DFS)
![Page 15: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/.../day14.pdf · Breadth-First Search (BFS) •Definition: the distancebetween !,#is the number of edges on the shortest](https://reader033.fdocuments.in/reader033/viewer/2022052007/601c3b8c6cbc99638e0a2ed1/html5/thumbnails/15.jpg)
Exploring a Graph
• Problem: Is there a path from ! to "?• Idea: Explore all nodes reachable from !.
• Two different search techniques:• Breadth-First Search: explore nearby nodes before
moving on to farther away nodes• Depth-First Search: follow a path until you get stuck,
then go back
![Page 16: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/.../day14.pdf · Breadth-First Search (BFS) •Definition: the distancebetween !,#is the number of edges on the shortest](https://reader033.fdocuments.in/reader033/viewer/2022052007/601c3b8c6cbc99638e0a2ed1/html5/thumbnails/16.jpg)
Depth-First Search
G = (V,E) is a graphexplored[u] = 0 ∀u
DFS(u):explored[u] = 1
for ((u,v) in E):if (explored[v]=0):parent[v] = uDFS(v)
u b
a c
![Page 17: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/.../day14.pdf · Breadth-First Search (BFS) •Definition: the distancebetween !,#is the number of edges on the shortest](https://reader033.fdocuments.in/reader033/viewer/2022052007/601c3b8c6cbc99638e0a2ed1/html5/thumbnails/17.jpg)
Depth-First Search
u b
a c
• Fact: The parent-child edges form a (directed) tree• Each edge has a type:• Tree edges: (", $), (", &), (&, ')
• These are the edges that explore new nodes• Forward edges: (", ')
• Ancestor to descendant• Backward edges: $, "
• Descendant to ancestor• Cross edges: (&, $)
• No ancestral relation
![Page 18: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/.../day14.pdf · Breadth-First Search (BFS) •Definition: the distancebetween !,#is the number of edges on the shortest](https://reader033.fdocuments.in/reader033/viewer/2022052007/601c3b8c6cbc99638e0a2ed1/html5/thumbnails/18.jpg)
Activity
a b
e f
• DFS this graph starting from node !• Search in alphabetical order• Label edges as { tree , forward , backward , cross}
c d
g h
• Each edge has a type:• Tree edges: (#, !), (#, &), (&, ')
• Edges that explore new nodes• Forward edges: (#, ')
• Ancestor to descendant• Backward edges: !, #
• Descendant to ancestor• Cross edges: (&, !)
• No ancestral relation
![Page 19: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/.../day14.pdf · Breadth-First Search (BFS) •Definition: the distancebetween !,#is the number of edges on the shortest](https://reader033.fdocuments.in/reader033/viewer/2022052007/601c3b8c6cbc99638e0a2ed1/html5/thumbnails/19.jpg)
Pre-OrderingG = (V,E) is a graphexplored[u] = 0 ∀u
DFS(u):explored[u] = 1
pre-visit(u)
for ((u,v) in E):if (explored[v]=0):parent[v] = uDFS(v)
u b
a c
• Maintain a counter clock, initially set clock = 1• pre-visit(u):
set preorder[u]=clock, clock=clock+1
Vertex Pre-Order
• Order the vertices by when they were first visited by DFS
![Page 20: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/.../day14.pdf · Breadth-First Search (BFS) •Definition: the distancebetween !,#is the number of edges on the shortest](https://reader033.fdocuments.in/reader033/viewer/2022052007/601c3b8c6cbc99638e0a2ed1/html5/thumbnails/20.jpg)
Post-OrderingG = (V,E) is a graphexplored[u] = 0 ∀u
DFS(u):explored[u] = 1
for ((u,v) in E):if (explored[v]=0):parent[v] = uDFS(v)
post-visit(u)
u b
a c
• Maintain a counter clock, initially set clock = 1• post-visit(u):
set postorder[u]=clock, clock=clock+1
Vertex Post-Order
• Order the vertices by when they were last visited by DFS
![Page 21: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/.../day14.pdf · Breadth-First Search (BFS) •Definition: the distancebetween !,#is the number of edges on the shortest](https://reader033.fdocuments.in/reader033/viewer/2022052007/601c3b8c6cbc99638e0a2ed1/html5/thumbnails/21.jpg)
Preorder versus postorder
Pre-order: F, B, A, D, C, E, G, I, H. Post-order: A, C, E, D, B, H, I, G, F.
![Page 22: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/.../day14.pdf · Breadth-First Search (BFS) •Definition: the distancebetween !,#is the number of edges on the shortest](https://reader033.fdocuments.in/reader033/viewer/2022052007/601c3b8c6cbc99638e0a2ed1/html5/thumbnails/22.jpg)
Activity
a b
e f
• Compute the post-order of this graph• DFS from !, search in alphabetical order
c d
g h
Vertex a b c d e f g hPost-Order
![Page 23: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/.../day14.pdf · Breadth-First Search (BFS) •Definition: the distancebetween !,#is the number of edges on the shortest](https://reader033.fdocuments.in/reader033/viewer/2022052007/601c3b8c6cbc99638e0a2ed1/html5/thumbnails/23.jpg)
Ask the Audience
a b
e f
• Compute the post-order of this graph• DFS from !, search in alphabetical order
c d
g h
Vertex a b c d e f g hPost-Order 8 7 5 4 6 1 2 3