all pair shortest path ioi/acm icpc training june 2004

61
All Pair Shortest Path IOI/ACM ICPC Training June 2004

Upload: hector-thornton

Post on 04-Jan-2016

227 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: All Pair Shortest Path IOI/ACM ICPC Training June 2004

All Pair Shortest Path

IOI/ACM ICPC Training

June 2004

Page 2: All Pair Shortest Path IOI/ACM ICPC Training June 2004

All Pair Shortest Path

Note: Dijkstra’s Algorithm takes O((V+E)logV) time

All Pair Shortest Path Problem can be solved by executing Dijkstra’s Algorithm |V| times

Running Time: O(V(V+E)log V)

Floyd-Warshall Algorithm: O(V3)

Page 3: All Pair Shortest Path IOI/ACM ICPC Training June 2004

Idea

Label the vertices with integers 1..n

Restrict the shortest paths from i to j to consist of vertices 1..k only (except i and j)

Iteratively relax k from 1 to n.

i

k

j

Page 4: All Pair Shortest Path IOI/ACM ICPC Training June 2004

Definition

Find shortest distance from i to j using vertices 1 .. k only

i

k

j

Page 5: All Pair Shortest Path IOI/ACM ICPC Training June 2004

Example

1

2

4

3 5

2 4

1

1

53

1

1

3

Page 6: All Pair Shortest Path IOI/ACM ICPC Training June 2004

i = 4, j = 5, k = 0

1

2

4

3 5

2 4

1

1

53

1

1

3

Page 7: All Pair Shortest Path IOI/ACM ICPC Training June 2004

i = 4, j = 5, k = 1

1

2

4

3 5

2 4

1

1

53

1

1

3

Page 8: All Pair Shortest Path IOI/ACM ICPC Training June 2004

i = 4, j = 5, k = 2

1

2

4

3 5

2 4

1

1

53

1

1

3

Page 9: All Pair Shortest Path IOI/ACM ICPC Training June 2004

i = 4, j = 5, k = 3

1

2

4

3 5

2 4

1

1

51

1

1

3

Page 10: All Pair Shortest Path IOI/ACM ICPC Training June 2004

Idea

k

j

i

0 k for

0 k for ),min(

only {1..k} involving to from distanceShortest :

,

1,

1,

1,

,

,

ji

kjk

kki

kjik

ji

kji

w

DDDD

jiD

Page 11: All Pair Shortest Path IOI/ACM ICPC Training June 2004

The tables

i

j

k=4

k=5

i

j

k

k

Page 12: All Pair Shortest Path IOI/ACM ICPC Training June 2004

The code

for i = 1 to |V| for j = 1 to |V| a[i][j][0] = cost(i,j)for k = 1 to |V| for i = 1 to |V| for j = 1 to |V| a[i][j][k] = min( a[i][j][k-1],

a[i][k][k-1] + a[k][j][k-1])

Page 13: All Pair Shortest Path IOI/ACM ICPC Training June 2004

Topological sort

IOI/ACM ICPC Training

June 2004

Page 14: All Pair Shortest Path IOI/ACM ICPC Training June 2004

Topological order

Consider the prerequisite structure for courses:

Each node x represents a course x (x, y) represents that course x is a prerequisite to course y Note that this graph should be a directed graph without cycles. A linear order to take all 5 courses while satisfying all prerequisites is called a

topological order. E.g.

a, c, b, e, d c, a, b, e, d

b d

ec

a

Page 15: All Pair Shortest Path IOI/ACM ICPC Training June 2004

Topological sort

Arranging all nodes in the graph in a topological order

Applications:Schedule tasks associated with a project

Page 16: All Pair Shortest Path IOI/ACM ICPC Training June 2004

Topological sort algorithm

Algorithm topSort1n = |V|;Let R[0..n-1] be the result array;for i = 1 to n {

select a node v that has no successor;R[n-i] = v;delete node v and its edges from the graph;

}return R;

Page 17: All Pair Shortest Path IOI/ACM ICPC Training June 2004

Example

b d

ec

a

1. d has no successor! Choose d!

a

5. Choose a!The topological

order is a,b,c,e,d

2. Both b and e have no successor! Choose e!

b

ec

a

3. Both b and c have no successor! Choose c!

b

c

a

4. Only b has no successor! Choose b!

ba

Page 18: All Pair Shortest Path IOI/ACM ICPC Training June 2004

Time analysis

Finding a node with no successor takes O(|V|+|E|) time.

We need to repeat this process |V| times. Total time = O(|V|2 + |V| |E|).

We can implement the above process using DFS. The time can be improved to O(|V| + |E|).

Page 19: All Pair Shortest Path IOI/ACM ICPC Training June 2004

Algorithm based on DFSAlgorithm topSort2s.createStack();for (all nodes v in the graph) {

if (v has no predecessors) {s.push(v);mark v as visited;

}}while (s is not empty) {

let v be the node on the top of the stack s;if (no unvisited nodes are children to v) { // i.e. v has no unvisited successor

aList.add(1, v);s.pop(); // blacktrack

} else {select an unvisited child u of v;s.push(u);mark u as visited;

}}return aList;

Page 20: All Pair Shortest Path IOI/ACM ICPC Training June 2004

Bipartite Matching

IOI/ACM ICPC Training

June 2004

Page 21: All Pair Shortest Path IOI/ACM ICPC Training June 2004

Unweighted Bipartite Matching

Page 22: All Pair Shortest Path IOI/ACM ICPC Training June 2004

Definitions

Matching

Free Vertex

Page 23: All Pair Shortest Path IOI/ACM ICPC Training June 2004

Definitions

Maximum Matching: matching with the largest number of edges

Page 24: All Pair Shortest Path IOI/ACM ICPC Training June 2004

Definition

Note that maximum matching is not unique.

Page 25: All Pair Shortest Path IOI/ACM ICPC Training June 2004

Intuition

Let the top set of vertices be men Let the bottom set of vertices be women Suppose each edge represents a pair of

man and woman who like each other

Maximum matching tries to maximize the number of couples!

Page 26: All Pair Shortest Path IOI/ACM ICPC Training June 2004

Applications

Matching has many applications. For examples,Comparing Evolutionary TreesFinding RNA structure…

This lecture lets you know how to find maximum matching.

Page 27: All Pair Shortest Path IOI/ACM ICPC Training June 2004

Alternating Path Alternating between matching and non-matching edges.

a b c d e

f g h i j

d-h-e: alternating patha-f-b-h-d-i: alternating path starts and ends with free verticesf-b-h-e: not alternating pathe-j: alternating path starts and ends with free vertices

Page 28: All Pair Shortest Path IOI/ACM ICPC Training June 2004

Idea

“Flip” augmenting path to get better matching

Note: After flipping, the number of matched edges will increase by 1!

Page 29: All Pair Shortest Path IOI/ACM ICPC Training June 2004

Idea

Theorem (Berge 1975):A matching M in G is maximum iffThere is no augmenting path

Proof: () If there is an augmenting path, clearly not

maximum. (Flip matching and non-matching edges in that path to get a “better” matching!)

Page 30: All Pair Shortest Path IOI/ACM ICPC Training June 2004

Proof for the other direction

() Suppose M is not maximum. Let M’ be a maximum matching such that |M’|>|M|.

Consider H = MM’ = (MM’)-(MM’)i.e. a set of edges in M or M’ but not both

H has two properties: Within H, number of edges belong to M’ > number of edges

belong to M. H can be decomposed into a set of paths. All paths should

be alternating between edges in M and M’. There should exist a path with more edges from M’.

Also, it is alternating.

Page 31: All Pair Shortest Path IOI/ACM ICPC Training June 2004

Idea of Algorithm

Start with an arbitrary matching While we still can find an augmenting path

Find the augmenting path PFlip the edges in P

Page 32: All Pair Shortest Path IOI/ACM ICPC Training June 2004

Labelling Algorithm

Start with arbitrary matching

Page 33: All Pair Shortest Path IOI/ACM ICPC Training June 2004

Labelling Algorithm

Pick a free vertex in the bottom

Page 34: All Pair Shortest Path IOI/ACM ICPC Training June 2004

Labelling Algorithm

Run BFS

Page 35: All Pair Shortest Path IOI/ACM ICPC Training June 2004

Labelling Algorithm

Alternate unmatched/matched edges

Page 36: All Pair Shortest Path IOI/ACM ICPC Training June 2004

Labelling Algorithm

Until a augmenting path is found

Page 37: All Pair Shortest Path IOI/ACM ICPC Training June 2004

Augmenting Tree

Page 38: All Pair Shortest Path IOI/ACM ICPC Training June 2004

Flip!

Page 39: All Pair Shortest Path IOI/ACM ICPC Training June 2004

Repeat

Pick another free vertex in the bottom

Page 40: All Pair Shortest Path IOI/ACM ICPC Training June 2004

Repeat

Run BFS

Page 41: All Pair Shortest Path IOI/ACM ICPC Training June 2004

Repeat

Flip

Page 42: All Pair Shortest Path IOI/ACM ICPC Training June 2004

Answer

Since we cannot find any augmenting path, stop!

Page 43: All Pair Shortest Path IOI/ACM ICPC Training June 2004

Overall algorithm

Start with an arbitrary matching (e.g., empty matching) Repeat forever

For all free vertices in the bottom, do bfs to find augmenting paths

If found, then flip the edges If fail to find, stop and report the maximum matching.

Page 44: All Pair Shortest Path IOI/ACM ICPC Training June 2004

Time analysis

We can find at most |V| augmenting paths (why?)

To find an augmenting path, we use bfs! Time required = O( |V| + |E| )

Total time: O(|V|2 + |V| |E|)

Page 45: All Pair Shortest Path IOI/ACM ICPC Training June 2004

Improvement

We can try to find augmenting paths in parallel for all free nodes in every iteration.

Using such approach, the time complexity is improved to O(|V|0.5 |E|)

Page 46: All Pair Shortest Path IOI/ACM ICPC Training June 2004

Weighted Bipartite Graph

46 63

Page 47: All Pair Shortest Path IOI/ACM ICPC Training June 2004

Weighted Matching

46 6

3

Score: 6+3+1=10

Page 48: All Pair Shortest Path IOI/ACM ICPC Training June 2004

Maximum Weighted Matching

46 6

3

Score: 6+1+1+1+4=13

Page 49: All Pair Shortest Path IOI/ACM ICPC Training June 2004

Augmenting Path (change of definition) Any alternating path such that total score of unmatched

edges > that of matched edges The score of the augmenting path is

Score of unmatched edges – that of matched edges

46 6

3

Note: augmenting path need not start and end at free vertices!

Page 50: All Pair Shortest Path IOI/ACM ICPC Training June 2004

Idea for finding maximum weight matching Theorem: Let M be a matching of

maximum weight among matchings of size |M|.

If P is an augmenting path for M of maximum weight,

Then, the matching formed by augmenting M by P is a matching of maximum weight among matchings of size |M|+1.

Page 51: All Pair Shortest Path IOI/ACM ICPC Training June 2004

Overall Algorithm

Start with an empty matching Repeat forever

Find an augmenting path P with maximum score If the score > 0, then flip the edges Otherwise, stop and report the maximum weight

matching.

Page 52: All Pair Shortest Path IOI/ACM ICPC Training June 2004

Time analysis

The same! Time required = O(|V|2 + |V| |E|)

Page 53: All Pair Shortest Path IOI/ACM ICPC Training June 2004

Stable Marriage Problem

IOI/ACM ICPC Training

June 2004

Page 54: All Pair Shortest Path IOI/ACM ICPC Training June 2004

Stable Marriage Problem

Given N men and N women, each person list in order of preference all the people of the opposite sex who would like to marry.

Problem:Engage all the women to all the men in such a

way as to respect all their preferences as much as possible.

Page 55: All Pair Shortest Path IOI/ACM ICPC Training June 2004

Stable?

A set of marriages is unstable if two people who are not married both prefer each other than their

spouses

E.g. Suppose we have A1 B3 C2 D4 E5. This is unstable since A prefer 2 more than 1 2 prefer A more than C

A B C D E

2

5

1

3

4

1

2

3

4

5

2

3

5

4

1

1

3

2

4

5

5

3

2

1

4

1

E

A

D

B

C

2

D

E

B

A

C

3

A

D

B

C

E

4

C

B

D

A

E

5

D

B

C

E

A

Page 56: All Pair Shortest Path IOI/ACM ICPC Training June 2004

Naïve solution

Starting from a feasible solution. Check if it is stable.

If yes, done! If not, remove an unstable couple.

Is this work?

Page 57: All Pair Shortest Path IOI/ACM ICPC Training June 2004

Naïve solution (2)

Does not work! E.g.

A1 B3 C2 D4 E5 A2 B3 C1 D4 E5 A3 B2 C1 D4 E5 A3 B1 C2 D4 E5

A B C D E

2

5

1

3

4

1

2

3

4

5

2

3

5

4

1

1

3

2

4

5

5

3

2

1

4

1

E

A

D

B

C

2

D

E

B

A

C

3

A

D

B

C

E

4

C

B

D

A

E

5

D

B

C

E

A

Page 58: All Pair Shortest Path IOI/ACM ICPC Training June 2004

Solution

1. Let X be the first man.2. X proposes to the best woman in the

remaining on his list. (Initially, the first woman on his list!)

3. If α is not engaged Pair up (X, α). Then, set X=next man and goto 1.

4. If α prefers X more than her fiancee Y, Pair up (X, α). Then, set X=Y and goto 1.

5. Goto 1

Page 59: All Pair Shortest Path IOI/ACM ICPC Training June 2004

Example

A B C D E

2

5

1

3

4

1

2

3

4

5

2

3

5

4

1

1

3

2

4

5

5

3

2

1

4

1

E

A

D

B

C

2

D

E

B

A

C

3

A

D

B

C

E

4

C

B

D

A

E

5

D

B

C

E

A

A B C D E

2

5

1

1

2

3

4

2

3

5

1

3

5

3

2

Page 60: All Pair Shortest Path IOI/ACM ICPC Training June 2004

Time analysis

If there are N men and N women,O(N2) time

Page 61: All Pair Shortest Path IOI/ACM ICPC Training June 2004

Algorithm

prefer[m][s]=w means the woman w is on the s-th position in the preference list of the man m

Let next[m] be the current best woman in his remaining list. (Initially, next[m]=0)

fiancee[w]=m means the man m engaged to woman w. (Initially, fiancee[w]=0)

Let rank[w][m] is the ranking of the man m in the preference list of the woman w.

For(m=1;m<=N;m++) {For(s=m;s!=0;

}