tut4_ppt(update) - COMP271H COMP271H Tutorial 4 Finding...

Info icon This preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

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 BFS-based 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 – BFS-based 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: Blue-blue pair detected! Report NOT-bipartite! Q2 - BFS-based 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 (Red-red or blue-blue pair edge) blue-blue • 1 level above v (Red-blue pair edge) • 1 level below v (Red-blue 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 blue-blue 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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern