Unformatted text preview: COMP271H COMP271H
Tutorial 4 Finding bipartite graph Bipartite graph  Math definition Bipartite Bipartite graph  Drawing Bipartite Set V1 Set V2 Not a bipartite graph Not Reason: This edge inside the same subset of V Set V1 Set V2 Is this bipartite graph? Is How about this? How How about this then? How Application – Marriage problem Application Each link represent Each the man and the woman happy to marry with each other marry Find an assignment of Find marriage to make most people happy most Assumption: No homosexual No marriage is allowed marriage Today’s questions Today’s
Prove that a bipartite graph has no cycle Prove of odd length of 2. Design a BFSbased algorithm to Design determine a graph is bipartite determine 3. Justify the correctness of our algorithm
1. Q1  Why no odd length cycle? Q1
I1=Ik+1 I3 I2 All red vertices are even index Ik Let the cycle C = {I1, I2, …, Ik,I1} Length of cycle C = number of vertices of C  1 = k (even)! Algorithm: DFS v.s. BFS Algorithm: DFS(v) BFS(v) Initialize graph Stack s color[v]=gray s.push(v); While (s is not empty)
• • • v = s.top() for each w adj (v) do If color[w]==white then Initialize graph Queue q color[v]=gray q.enqueue(v); While (q is not empty)
• • • v = q.dequeue() for each w adj (v) do If color[w]==white then color[w] = gray s.push(w) color[w] = gray q.enqueue(w) • color[v] = black • color[v] = black Q2 – BFSbased algorithm Q2 Idea: Idea: Color the root vertex as red Color red Alternatively color each level by blue and red Alternatively blue red using BFS using Pass all cases: Report that it is bipartite If we detect: Report that it is NOT bipartite Algorithm: Run(1) Algorithm:
Pick this as start vertex Algorithm: Run(2) Algorithm: Algorithm: Run(3) Algorithm: Algorithm: Run(4) Algorithm: Algorithm: Run(5) Algorithm: Algorithm: Run(6) Algorithm: Blueblue pair detected! Report NOTbipartite! Q2  BFSbased algorithm Q2 Pseudocode: Pseudocode: See tutorial solution How we can prove this algorithm is correct? Can we make use of properties of BFS tree? Can we make use of part 1’s result?
• A bipartite graph has no cycle of odd length Correctness: Property of BFS tree Property Consider an arbitrary vertex v in a BFS Consider tree T tree For each vertex w belongs to adj(v), they are For either these 3 cases: either
• Same level as v (Redred or blueblue pair edge) blueblue • 1 level above v (Redblue pair edge) • 1 level below v (Redblue pair edge) For the cases of 1 level above/below v, it For satisfies our definition of bipartite graph satisfies How about same level case ? Illustrative example (1) Illustrative Illustrative example (2) Illustrative If there exists a blueblue pair Illustrative example (3) Illustrative What is number of edges on this cycle? Q3 – Correctness Q3 Correctness proof: You can see it in the tutorial solution It is better to understand the proof, not the It memorize it memorize The way to understand is to draw it, think The about it, and then derive some examples to convince yourself your algorithm is CORRECT!! CORRECT!! Q&A ...
View
Full
Document
This note was uploaded on 12/09/2010 for the course ENGLISH 1303 taught by Professor May during the Spring '10 term at HKU.
 Spring '10
 may
 English

Click to edit the document details