cs3000: algorithms & data paul handkhoury.northeastern.edu/.../day14.pdf · breadth-first...

23
CS3000: Algorithms & Data Paul Hand Lecture 14: Bipartite Graphs and 2-coloring Depth First Search Mar 11, 2019

Upload: others

Post on 30-Sep-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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