Presentation is loading. Please wait.

Presentation is loading. Please wait.

Various Graph Algorithms

Similar presentations


Presentation on theme: "Various Graph Algorithms"— Presentation transcript:

1 Various Graph Algorithms

2 All Pair Shortest Path Problem
Find the Shortest path between every pair of vertices in a graph ONE SOLUTION: apply Dijkstra’s algorithm mutliple times, each time a different vertex in the graph is the source. O(EV + V3 ) time if very connected or O(V(V+E) logV) if not many edges Another example of dynamic programming is all-pair shortest path where we are interested in calculating the shortest path between every pair of vertices. One obvious solution is to execute Dijkstra’s algorithm from different source vertices. But the running time would be O((V+E)V log V). A dynamic programming solution runs in only O(V3) time.

3 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) Another example of dynamic programming is all-pair shortest path where we are interested in calculating the shortest path between every pair of vertices. One obvious solution is to execute Dijkstra’s algorithm from different source vertices. But the running time would be O((V+E)V log V). A dynamic programming solution runs in only O(V3) time.

4 Floyd-Warshall Algorithm
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 The idea behind Floyd-Warshall algorithm is this: Let the vertices be labeled from 1 to n. We find shortest paths between any two vertices with the restriction that the vertices on the shortest path (excluding the end points) can only consists of vertices from 1 to k. We then relax (nothing to do with Dijkstra’s relax() operation!) the restriction so that the shortest paths can include vertices from 1 to (k+1). j i

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

6 Example 2 4 2 1 4 1 1 3 3 5 1 3 5 1

7 i = 4, j = 5, k = 0 2 4 2 1 4 1 1 3 3 5 1 3 5 1

8 i = 4, j = 5, k = 1 2 4 2 1 4 1 1 3 3 5 1 3 5 1

9 i = 4, j = 5, k = 2 2 4 2 1 4 1 1 3 3 5 1 3 5 1

10 i = 4, j = 5, k = 3 4 1 2 3 5

11 Idea k j i

12 The tables i i j j k k=4 k=5 D44,3 D45,3 D54,3 D44,5 D54,3
To fill out an entry in the table k, we make use of entries for table k-1, For example, to calculate D54,3, (column 4 row 3 in table 5), we look at D44,3, and the sum of D44,5 and D45,3. We take the smaller of the two values and fill in D54,3. D44,5 k=4 k=5 D54,3 = MIN {going vertex 4 to 3 using 1-4 vertices (k=4)} OR {going from vertex 4 to 5 using 1-4 vertices (k=4 )+ going from vertex 5 to3 using 1-4 vertices (k=4)}

13 The code for i = 1 to |V| for j = 1 to |V| a[i][j][0] = cost(i,j) for k = 1 to |V| a[i][j][k] = min( a[i][j][k-1], a[i][k][k-1] + a[k][j][k-1]) The pseudo code above only gives us the distances of the shortest path? How can you modify the code so that we can recover the shortest paths?

14 Topological sort

15 Topological order b d a c e
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 e c a

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

17 Topological sort algorithm
Algorithm topSort1 n = |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;

18 Example b d e c a b e c a b c a b a a R[] = [ _ _ _ _ _ ]
d has no successor! Choose d! Both b and e have no successor! Choose e! b e c a R[] = [ _ _ _ e d ] Both b and c have no successor! Choose c! b c a Only b has no successor! Choose b! b a a Choose a! The topological order is a,b,c,e,d R[] = [ _ _ c e d ] R[] = [ _ b c e d ] R[] = [ a b c e d ]

19 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|).

20 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; return aList;

21 Bipartite Matching

22 Unweighted Bipartite Matching
bipartite graph (or bigraph) is a graph whose vertices can be divided into two disjoint sets U and V and such that every edge connects a vertex in U to one in V U V

23 Definitions A matching in a graph is a subset of its edges, no two of which share an endpoint Matching Free Vertex

24 Definitions Maximum Matching: matching with the largest number of edges (or for Maximum Weighted Matching largest sum of weight values of the retained Edges)

25 Definition Note that maximum matching is not unique.

26 Intuition Let the top set of vertices be Blue
Let the bottom set of vertices be Yellow Suppose each edge represents a pair of Blue and Yellow who like each other Maximum matching tries to maximize the number of Blue-Yellow couples!

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

28 How can we get a bipartite match? First some terms
G = (V, E): bipartite graph with bipartition {A, B} V=A  B, A  B = . a, a’,… represent vertices in A. No edge between a, a’. b, b’,… represent vertices in B. No edge between b, b’. b ab a A B A B A B G Examples of matchings

29 Matching and augmenting paths (1/3)
How can we find a (large) matching? By expanding a small one with augmenting paths. Augmenting path Starts an unmatched vertex in A, and ends at an unmatched one in B. Vertices except 1st and last in the path are attached to edges in M. In finding an augmenting path, the followings are repeated: Move from A to B through an edge in E – M. Move from B to A through an edge in M. A B A B Matching M Augmenting path P

30 Matching and augmenting paths (2/3)
How can we find a (large) matching? By expanding a small one with augmenting paths. Suppose M and an augmenting path P are given. M’ = (E(P)  M) (M  E(P)) is also a matching. Each vertex on P is incident with just one edge in E(P)  M. Then E(P)  M is a matching. M  E(P) is obviously a matching. These sets do not share any vertex, so the union is also a matching. A B A B A B Matching M Augmenting path P Matching M’

31 Matching and augmenting paths (3/3)
How can we find a matching? By expanding a small one with augmenting paths. Two new vertices (the first and the last vertices in the path) are included in the new matching with one expansion. THUS M’ is better than M Alternating path Augmenting one w/o the condition on the end vertex. The end is not necessarily an unmatched vertex in B. A B A B A B Matching M Augmenting path P Matching M’

32 Idea Theorem (Berge 1975): A matching M in G is maximum iff (if and only if) There 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!)

33 Proof for the other direction
() Suppose M is not maximum. Let M’ be a maximum matching such that |M’|>|M|. 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’ > 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.

34 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

35 Labelling Algorithm Start with arbitrary matching

36 Labelling Algorithm Pick a free vertex in the bottom

37 Labelling Algorithm Run BFS

38 Labelling Algorithm Alternate unmatched/matched edges

39 Labelling Algorithm Until a augmenting path is found

40 Flip!

41 Repeat Pick another free vertex in the bottom

42 Repeat Run BFS

43 Repeat Flip

44 Answer Since we cannot find any augmenting path, stop!

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

46 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|)

47 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|)

48 Weighted Bipartite Graph
3 4 6 6

49 Weighted Matching Score: 6+3+1=10 3 4 6 6

50 Maximum Weighted Matching
Score: =13 3 4 6 6

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

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

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

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

55 Stable Marriage Problem
ON YOU OWN

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

57 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

58 Naïve solution Starting from a feasible solution.
Check if it is stable. If yes, done! If not, remove an unstable couple. Will this work?

59 Naïve solution (2) Does not work! E.g. A1 B3 C2 D4 E5 A2 B3 C1 D4 E5

60 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 α is not engaged Pair up (X, α). Then, set X=next man and goto 1. If α prefers X more than her fiancee Y, Pair up (X, α). Then, set X=Y and goto 1. Goto 1

61 Example A B C D E 2 1 2 1 5 5 2 3 3 3 1 3 5 2 A B C D E 2 5 1 3 4 4

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

63 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; }


Download ppt "Various Graph Algorithms"

Similar presentations


Ads by Google