All%20Pair%20Shortest%20Path - PowerPoint PPT Presentation

About This Presentation
Title:

All%20Pair%20Shortest%20Path

Description:

All Pair Shortest Path. Note: Dijkstra's Algorithm takes O((V E)logV) time ... Theorem (Berge 1975): A matching M in G is maximum. iff. There is no augmenting path ... – PowerPoint PPT presentation

Number of Views:117
Avg rating:3.0/5.0
Slides: 62
Provided by: ooiwei
Category:

less

Transcript and Presenter's Notes

Title: All%20Pair%20Shortest%20Path


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

2
All Pair Shortest Path
  • Note Dijkstras Algorithm takes O((VE)logV)
    time
  • All Pair Shortest Path Problem can be solved by
    executing Dijkstras Algorithm V times
  • Running Time O(V(VE)log V)
  • Floyd-Warshall Algorithm O(V3)

3
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.

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

k
j
i
5
Example
2
4
2
4
1
1
1
3
3
5
1
3
5
1
6
i 4, j 5, k 0
2
4
2
4
1
1
1
3
3
5
1
3
5
1
7
i 4, j 5, k 1
2
4
2
4
1
1
1
3
3
5
1
3
5
1
8
i 4, j 5, k 2
2
4
2
4
1
1
1
3
3
5
1
3
5
1
9
i 4, j 5, k 3
2
4
2
4
1
1
1
1
3
5
1
3
5
1
10
Idea
k
j
i
11
The tables
i
k
i
j
j
k
k4
k5
12
The code
  • for i 1 to V
  • for j 1 to V
  • aij0 cost(i,j)
  • for k 1 to V
  • for i 1 to V
  • for j 1 to V
  • aijk min( aijk-1,
  • aikk-1 akjk-1)

13
Topological sort
  • IOI/ACM ICPC Training
  • June 2004

14
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

15
Topological sort
  • Arranging all nodes in the graph in a topological
    order
  • Applications
  • Schedule tasks associated with a project

16
Topological sort algorithm
  • Algorithm topSort1
  • n V
  • Let R0..n-1 be the result array
  • for i 1 to n
  • select a node v that has no successor
  • Rn-i v
  • delete node v and its edges from the graph
  • return R

17
Example
18
Time analysis
  • Finding a node with no successor takes O(VE)
    time.
  • We need to repeat this process V times.
  • Total time O(V2 V E).
  • We can implement the above process using DFS. The
    time can be improved to O(V E).

19
Algorithm based on DFS
  • Algorithm topSort2
  • s.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

20
Bipartite Matching
  • IOI/ACM ICPC Training
  • June 2004

21
Unweighted Bipartite Matching
22
Definitions
Matching
Free Vertex
23
Definitions
  • Maximum Matching matching with the largest
    number of edges

24
Definition
  • Note that maximum matching is not unique.

25
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!

26
Applications
  • Matching has many applications. For examples,
  • Comparing Evolutionary Trees
  • Finding RNA structure
  • This lecture lets you know how to find maximum
    matching.

27
Alternating Path
  • Alternating between matching and non-matching
    edges.

a
b
c
d
e
f
g
h
i
j
d-h-e alternating path a-f-b-h-d-i alternating
path starts and ends with free vertices f-b-h-e
not alternating path e-j alternating path starts
and ends with free vertices
28
Idea
  • Flip augmenting path to get better matching
  • Note After flipping, the number of matched edges
    will increase by 1!

?
29
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!)

30
Proof for the other direction
  • (?) Suppose M is not maximum. Let M be a maximum
    matching such that MgtM.
  • Consider H M?M (M?M)-(M?M)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 gt 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.

31
Idea of Algorithm
  • Start with an arbitrary matching
  • While we still can find an augmenting path
  • Find the augmenting path P
  • Flip the edges in P

32
Labelling Algorithm
  • Start with arbitrary matching

33
Labelling Algorithm
  • Pick a free vertex in the bottom

34
Labelling Algorithm
  • Run BFS

35
Labelling Algorithm
  • Alternate unmatched/matched edges

36
Labelling Algorithm
  • Until a augmenting path is found

37
Augmenting Tree
38
Flip!
39
Repeat
  • Pick another free vertex in the bottom

40
Repeat
  • Run BFS

41
Repeat
  • Flip

42
Answer
  • Since we cannot find any augmenting path, stop!

43
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.

44
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(V2 V E)

45
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(V0.5 E)

46
Weighted Bipartite Graph
3
4
6
6
47
Weighted Matching
Score 63110
3
4
6
6
48
Maximum Weighted Matching
Score 6111413
3
4
6
6
49
Augmenting Path (change of definition)
  • Any alternating path such that total score of
    unmatched edges gt that of matched edges
  • The score of the augmenting path is
  • Score of unmatched edges that of matched edges

3
4
6
6
Note augmenting path need not start and end at
free vertices!
50
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 M1.

51
Overall Algorithm
  • Start with an empty matching
  • Repeat forever
  • Find an augmenting path P with maximum score
  • If the score gt 0, then flip the edges
  • Otherwise, stop and report the maximum weight
    matching.

52
Time analysis
  • The same!
  • Time required O(V2 V E)

53
Stable Marriage Problem
  • IOI/ACM ICPC Training
  • June 2004

54
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.

55
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

56
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?

57
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

58
Solution
  • Let X be the first man.
  • X proposes to the best woman in the remaining on
    his list. (Initially, the first woman on his
    list!)
  • If a is not engaged
  • Pair up (X, a). Then, set Xnext man and goto 1.
  • If a prefers X more than her fiancee Y,
  • Pair up (X, a). Then, set XY and goto 1.
  • Goto 1

59
Example
A
B
C
D
E
2
1
2
1
5
5
2
3
3
3
1
3
5
2
4
60
Time analysis
  • If there are N men and N women,
  • O(N2) time

61
Algorithm
  • prefermsw means the woman w is on the s-th
    position in the preference list of the man m
  • Let nextm be the current best woman in his
    remaining list. (Initially, nextm0)
  • fianceewm means the man m engaged to woman w.
    (Initially, fianceew0)
  • Let rankwm is the ranking of the man m in the
    preference list of the woman w.
  • For(m1mltNm) For(sms!0
Write a Comment
User Comments (0)
About PowerShow.com