ACS 7101/3 – Advanced Algorithm Design

21
ACS 7101/3 – Advanced Algorithm Design November 2008 Finding the maximum matching of a bipartite graph Final Project

description

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. - PowerPoint PPT Presentation

Transcript of ACS 7101/3 – Advanced Algorithm Design

Page 1: ACS 7101/3 – Advanced Algorithm Design

ACS 7101/3 – Advanced Algorithm Design

November 2008

Finding the maximum matching of a bipartite graph

Final Project

Page 2: ACS 7101/3 – Advanced Algorithm Design

ACS 7101 – Advance Data Structures

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

7 8 9 10 11 12

1 2 3 4 5 6

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.

November 2008

Page 3: ACS 7101/3 – Advanced Algorithm Design

ACS 7101 – Advance Data Structures

Finding the maximum matching of a bipartite graph

The general procedure is:

In this example M3 is the maximum matching

M1 G1 G1’ P M1 P = M2

M2 G2 G2’ P1 & P2 M2 P1 P2 = M3

November 2008

Page 4: ACS 7101/3 – Advanced Algorithm Design

ACS 7101 – Advance Data Structures

Finding the maximum matching of a bipartite graph

We start with the graph stored in a text file as pairs u, v

1 7

2 8

2 9

3 9

3 10

4 10

1 11

1 2 3 4 5 6

7 8 9 10 11 12

Graph.txt

Store the file in memory using an Array of Objects

Array of objects A

field size description

M bit matching

L byte Level #

P bit augmenting path

pl ptr to int Points to the list

pm ptr to int Points to the match

November 2008

Page 5: ACS 7101/3 – Advanced Algorithm Design

ACS 7101 – Advance Data Structures

Finding the maximum matching of a bipartite graph

A M L P pl pm

0

i 1 0 -1 0 7

2 1 -1 0 8 9

3 1 -1 0 9 10

4 0 -1 0 10 11

5 0 -1 0 9 11

j-1 6 1 -1 0 7 12

j 7 1 -1 0 1 6

8 0 -1 0 2

9 1 -1 0 2 3 5

10 1 -1 0 3 4

11 0 -1 0 4 5

12 0 -1 0 6

Fig 1.

• count the nodes

• readFile.cpp

• randomMatching.cpp

• or exampleMatching.cpp

• initialize rest of the Array

G1

November 2008

M1 G1

G1 is a directed graph

Page 6: ACS 7101/3 – Advanced Algorithm Design

ACS 7101 – Advance Data Structures

Finding the maximum matching of a bipartite graph

November 2008

G1 G1’ : finding j*

• findLevels.cpp

G2' M L P pl pm

0

i 1 0 0 0 7

2 1 2 0 8 9

3 1 -1 0 9 10

4 1 2 0 10 11

5 0 0 0 9 11

j-1 6 1 2 0 7 12

j 7 1 1 0 1 6

8 0 3 0 2 FN

9 1 1 0 2 3 5

10 1 3 0 3 4

11 1 1 0 4 5

12 0 3 0 6 FN

Fig 5 j*=3

•Level 0: (first half)if M = 0 => L = 0while 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

Page 7: ACS 7101/3 – Advanced Algorithm Design

ACS 7101 – Advance Data Structures

Finding the maximum matching of a bipartite graph

November 2008

G1’ P

• findPaths.cpp

G2' M L P pl pm

0

i 1 0 0 0 7

2 1 2 0 8 9

3 1 -1 0 9 10

4 1 2 0 10 11

5 0 0 0 9 11

j-1 6 1 2 0 7 12

j 7 1 1 0 1 6

8 0 3 0 2 FN

9 1 1 0 2 3 5

10 1 3 0 3 4

11 1 1 0 4 5

12 0 3 0 6 FN

Fig 5 j*=3

•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

Page 8: ACS 7101/3 – Advanced Algorithm Design

ACS 7101 – Advance Data Structures

Finding the maximum matching of a bipartite graph

November 2008

P M1 P = M2

• findPaths.cpp

•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…

G2' M L P pl pm

0

i 1 0 0 1 7

2 1 2 0 8 9

3 1 -1 0 9 10

4 1 2 0 10 11

5 0 0 0 9 11

j-1 6 1 2 1 7 12

j 7 1 1 1 1 6

8 0 3 0 2 FN

9 1 1 0 2 3 5

10 1 3 0 3 4

11 1 1 0 4 5

12 0 3 1 6 FN

Fig 5 j*=3

Page 9: ACS 7101/3 – Advanced Algorithm Design

ACS 7101 – Advance Data Structures

Finding the maximum matching of a bipartite graph

November 2008

P M2 P1 = M3

• findPaths.cpp

Idea behind the symmetric difference:

M2 P1

Edge (1, 7)

In M2 doesn’t belong to a match match

(A[1].M = 0 AND A[7].M = 1)

1 2 3 4 5 6

7 8 9 10 11 12

1 2 3 4 5 6

7 8 9 10 11 12

1 2 3 4 5 6

7 8 9 10 11 12

M3

3 edges in the path:

(1, 7)

(7, 6)

(6, 12)

Page 10: ACS 7101/3 – Advanced Algorithm Design

ACS 7101 – Advance Data Structures

Finding the maximum matching of a bipartite graph

November 2008

P M2 P1 = M3

• findPaths.cpp

Idea behind the symmetric difference:

M2 P1

Edge (1, 7)

In M2 doesn’t belong to a match match

(A[1].M = 0 AND A[7].M = 1)

1 2 3 4 5 6

7 8 9 10 11 12

1 2 3 4 5 6

7 8 9 10 11 12

1 2 3 4 5 6

7 8 9 10 11 12

M3

3 edges in the path:

(1, 7)

(7, 6)

(6, 12)

Page 11: ACS 7101/3 – Advanced Algorithm Design

ACS 7101 – Advance Data Structures

Finding the maximum matching of a bipartite graph

November 2008

P M2 P1 = M3

• findPaths.cpp

Idea behind the symmetric difference:

M2 P1

Edge (7, 6)

Is a matching in M2 ignore

(A[7].M = 1 AND A[6].M = 1)

1 2 3 4 5 6

7 8 9 10 11 12

1 2 3 4 5 6

7 8 9 10 11 12

1 2 3 4 5 6

7 8 9 10 11 12

3 edges in the path:

(1, 7)

(7, 6)

(6, 12)

M3

Page 12: ACS 7101/3 – Advanced Algorithm Design

ACS 7101 – Advance Data Structures

Finding the maximum matching of a bipartite graph

November 2008

P M2 P1 = M3

• findPaths.cpp

Idea behind the symmetric difference:

M2 P11 2 3 4 5 6

7 8 9 10 11 12

1 2 3 4 5 6

7 8 9 10 11 12

1 2 3 4 5 6

7 8 9 10 11 12

3 edges in the path:

(1, 7)

(7, 6)

(6, 12)

M3Edge (6, 12)

In M2 doesn’t belong to a match match

(A[6].M = 1 AND A[12].M = 0)

Page 13: ACS 7101/3 – Advanced Algorithm Design

ACS 7101 – Advance Data Structures

Finding the maximum matching of a bipartite graph

November 2008

P M2 P1 = M3

• findPaths.cpp

How does the code work:We only evaluate alternate edges

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

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

M L P pl

0

1 1 0 1

2 1 2 0

3 1 -1 0

4 1 2 0

5 0 0 0

6 1 2 0

7 1 1 1

8 0 3 0

9 1 1 0

10 1 3 0

11 1 1 0

12 1 2 0

Page 14: ACS 7101/3 – Advanced Algorithm Design

ACS 7101 – Advance Data Structures

Finding the maximum matching of a bipartite graph

November 2008

P M2 P1 = M3

• findPaths.cpp

After repeating the process:

M3 M L P pl pm

0

i 1 1 0 1 7

2 1 2 2 8 9

3 1 -1 0 9 10

4 1 2 0 10 11

5 1 0 2 9 11

j-1 6 1 2 1 7 12

j 7 1 1 1 1 6

8 1 3 2 2

9 1 1 2 2 3 5

10 1 3 0 3 4

11 1 1 0 4 5

12 1 2 1 6

Fig 7

1 2 3 4 5 6

7 8 9 10 11 12

M3

Page 15: ACS 7101/3 – Advanced Algorithm Design

ACS 7101 – Advance Data Structures

Finding the maximum matching of a bipartite graph

November 2008

The final step is to write al the pairs (u, v) back into a text file including the matching edges found

1 7 Match

2 8 Match

2 9

3 9

3 10 Match

7 1 Match

1 6

2 2 Match

writeGraph.txt

Page 16: ACS 7101/3 – Advanced Algorithm Design

ACS 7101 – Advance Data Structures

Finding the maximum matching of a bipartite graph

November 2008

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

Page 17: ACS 7101/3 – Advanced Algorithm Design

ACS 7101 – Advance Data Structures

Finding the maximum matching of a bipartite graph

November 2008

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 fileList is empty…

Finish the random initial matching …

Page 18: ACS 7101/3 – Advanced Algorithm Design

ACS 7101 – Advance Data Structures

Finding the maximum matching of a bipartite graph

November 2008

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)

Page 19: ACS 7101/3 – Advanced Algorithm Design

ACS 7101 – Advance Data Structures

Finding the maximum matching of a bipartite graph

November 2008

C++ Hello world Books – exercises coding pseudo codes and

assignment 2

design Discussions with professor brainstorming the Structure writing the document

Positive actions during the process :

Page 20: ACS 7101/3 – Advanced Algorithm Design

ACS 7101 – Advance Data Structures

Finding the maximum matching of a bipartite graph

November 2008

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

What is next:

Page 21: ACS 7101/3 – Advanced Algorithm Design

ACS 7101 – Advance Data Structures

Finding the maximum matching of a bipartite graph

November 2008

Questions?