210 likes | 432 Views
ACS 7101/3 – Advanced Algorithm Design. Finding the maximum matching of a bipartite graph. Final Project. November 2008. Finding the maximum matching of a bipartite graph. From an initial matching. We want to find the maximum matching. 1 2 3 4 5 6.
E N D
ACS 7101/3 – Advanced Algorithm Design Finding the maximum matching of a bipartite graph Final Project November 2008
Finding the maximum matching of a bipartite graph From an initial matching We want to find the maximum matching 1 2 3 4 5 6 1 2 3 4 5 6 7 8 9 10 11 12 7 8 9 10 11 12 • preconditions: • The graph stored in a text file is represented by positive integer numbers. • The nodes must be consecutive and no node should be represented with a number bigger than the total number of nodes. • This code is designed for a balanced bipartite graph. Notes: • Since we are using positive integers to name each node, we are not going to • use the index 0 of the array. ACS 7101 – Advance Data Structures November 2008
Finding the maximum matching of a bipartite graph The general procedure is: M1 G1 G1’ P M1 P = M2 M2 G2 G2’ P1 & P2 M2 P1 P2 = M3 In this example M3 is the maximum matching ACS 7101 – Advance Data Structures November 2008
Finding the maximum matching of a bipartite graph We start with the graph stored in a text file as pairs u, v Store the file in memory using an Array of Objects Graph.txt 1 7 2 8 2 9 3 9 3 10 4 10 11 … … 1 2 3 4 5 6 7 8 9 10 11 12 ACS 7101 – Advance Data Structures November 2008
Finding the maximum matching of a bipartite graph M1 G1 • count the nodes • readFile.cpp • randomMatching.cpp • or exampleMatching.cpp • initialize rest of the Array G1 G1 is a directed graph ACS 7101 – Advance Data Structures November 2008
Finding the maximum matching of a bipartite graph G1 G1’ : finding j* • Level 0: (first half) • if M = 0 => L = 0 • while j* != 0 • Level k odd (we want to go down on the directed graph) • for the first half • assign k to all the nodes minus the one pointed by pm in the list • Level k even (we want to go up) • for the second half • assign k to all the matching node • Note: • when L = current level and M = 0 => j* = L • findLevels.cpp ACS 7101 – Advance Data Structures November 2008
Finding the maximum matching of a bipartite graph G1’ P • Use a stack to keep track of the path • Starting at Level 0: • Push (i) • Read the list while it doesn’t belong to a path (P = 0) AND is not pointed by pm • while level(j) is greater than 0 and <= j*, AND it doesn't belong to a path AND it does belong to a matching: push(j); level ++; 1 2 3 4 5 6 7 8 9 10 11 12 • findPaths.cpp ACS 7101 – Advance Data Structures November 2008
Finding the maximum matching of a bipartite graph P M1 P = M2 • If we found a free node then we found a path => empty the stack, set P to 1, • while doing the symmetric difference and updating the matching… • findPaths.cpp ACS 7101 – Advance Data Structures November 2008
Finding the maximum matching of a bipartite graph P M2 P1 = M3 Idea behind the symmetric difference: 3 edges in the path: (1, 7) (7, 6) (6, 12) 1 2 3 4 5 6 1 2 3 4 5 6 M2 P1 7 8 9 10 11 12 7 8 9 10 11 12 Edge (1, 7) In M2 doesn’t belong to a match match (A[1].M = 0 AND A[7].M = 1) M3 1 2 3 4 5 6 7 8 9 10 11 12 • findPaths.cpp ACS 7101 – Advance Data Structures November 2008
Finding the maximum matching of a bipartite graph P M2 P1 = M3 Idea behind the symmetric difference: 3 edges in the path: (1, 7) (7, 6) (6, 12) 1 2 3 4 5 6 1 2 3 4 5 6 M2 P1 7 8 9 10 11 12 7 8 9 10 11 12 Edge (1, 7) In M2 doesn’t belong to a match match (A[1].M = 0 AND A[7].M = 1) M3 1 2 3 4 5 6 7 8 9 10 11 12 • findPaths.cpp ACS 7101 – Advance Data Structures November 2008
Finding the maximum matching of a bipartite graph P M2 P1 = M3 Idea behind the symmetric difference: 3 edges in the path: (1, 7) (7, 6) (6, 12) 1 2 3 4 5 6 1 2 3 4 5 6 M2 P1 7 8 9 10 11 12 7 8 9 10 11 12 Edge (7, 6) Is a matching in M2 ignore (A[7].M = 1 AND A[6].M = 1) M3 1 2 3 4 5 6 7 8 9 10 11 12 • findPaths.cpp ACS 7101 – Advance Data Structures November 2008
Finding the maximum matching of a bipartite graph P M2 P1 = M3 Idea behind the symmetric difference: 3 edges in the path: (1, 7) (7, 6) (6, 12) 1 2 3 4 5 6 1 2 3 4 5 6 M2 P1 7 8 9 10 11 12 7 8 9 10 11 12 Edge (6, 12) In M2 doesn’t belong to a match match (A[6].M = 1 AND A[12].M = 0) M3 1 2 3 4 5 6 7 8 9 10 11 12 • findPaths.cpp ACS 7101 – Advance Data Structures November 2008
Finding the maximum matching of a bipartite graph P M2 P1 = M3 How does the code work: We only evaluate alternate edges Edge (1, 7) In M2 doesn’t belong to a match match (A[1].M = 0 AND A[7].M = 1) A[1].M = 1 AND A[7].M = 1 … … … Edge (6, 12) In M2 doesn’t belong to a match match (A[6].M = 1 AND A[12].M = 0) A[6].M = 1 AND A[12].M = 1 • findPaths.cpp ACS 7101 – Advance Data Structures November 2008
Finding the maximum matching of a bipartite graph P M2 P1 = M3 After repeating the process: M3 1 2 3 4 5 6 7 8 9 10 11 12 • findPaths.cpp ACS 7101 – Advance Data Structures November 2008
Finding the maximum matching of a bipartite graph writeGraph.txt 1 7 Match 2 8 Match 2 9 3 9 3 10 Match … 7 1 Match 6 2 Match … The final step is to write al the pairs (u, v) back into a text file including the matching edges found ACS 7101 – Advance Data Structures November 2008
Finding the maximum matching of a bipartite graph List of files coded: main.cpp linkedListBG.h graph.txt array.cpp readFile.cpp display.cpp randomMatching.cpp exampleMatching.cpp findLevels.cpp findPaths.cpp writeFile.cpp ACS 7101 – Advance Data Structures November 2008
Finding the maximum matching of a bipartite graph Improvements to be done before testing it with bigger graphs: • Change “pos” to an auxiliary pointer • Store repeated calls to linked list in a variable • Check end of list (now is done with count() should be checking if the pointer points to null) • error control: • Open file • List is empty • … • Finish the random initial matching • … ACS 7101 – Advance Data Structures November 2008
Finding the maximum matching of a bipartite graph Problems encounter during the process: • The implementation was not clear in the book • How to store the graph in memory • How to mark the matching edges • How to find the levels • How to find the symmetric difference • C++ language • All the above plus • First time working with array of objects ever • Plus linked lists and pointers (everywhere) ACS 7101 – Advance Data Structures November 2008
Finding the maximum matching of a bipartite graph Positive actions during the process : • C++ • Hello world • Books – exercises • coding pseudo codes and assignment 2 • design • Discussions with professor • brainstorming the Structure • writing the document ACS 7101 – Advance Data Structures November 2008
Finding the maximum matching of a bipartite graph What is next: • Fix everything mentioned before • Create a function to generate a random graph • Test the code in an incremental way starting maybe with 50 nodes and increment it up to 20.000 nodes ACS 7101 – Advance Data Structures November 2008
Finding the maximum matching of a bipartite graph Questions? ACS 7101 – Advance Data Structures November 2008