Various Graph Algorithms

Slides:



Advertisements
Similar presentations
Single Source Shortest Paths
Advertisements

Weighted graphs Example Consider the following graph, where nodes represent cities, and edges show if there is a direct flight between each pair of cities.
CSE 390B: Graph Algorithms Based on CSE 373 slides by Jessica Miller, Ruth Anderson 1.
Midwestern State University Department of Computer Science Dr. Ranette Halverson CMPS 2433 – CHAPTER 4 GRAPHS 1.
1 Discrete Structures & Algorithms Graphs and Trees: III EECE 320.
Midterm <  70 3.
Lectures on Network Flows
1 Discrete Structures & Algorithms Graphs and Trees: II EECE 320.
3 -1 Chapter 3 The Greedy Method 3 -2 The greedy method Suppose that a problem can be solved by a sequence of decisions. The greedy method has that each.
Chapter 26 of CLSR Bipartite Matching By Dr. M. Sakalli, Sources: Levitin and many other CSE, Marmara Univ. May/2009.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
Lecture 11. Matching A set of edges which do not share a vertex is a matching. Application: Wireless Networks may consist of nodes with single radios,
Week 11 - Friday.  What did we talk about last time?  Exam 2 post mortem  Cycle detection  Connectivity.
Chapter 10: Iterative Improvement Simplex Method The Design and Analysis of Algorithms.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Greedy methods Prudence Wong
© The McGraw-Hill Companies, Inc., Chapter 3 The Greedy Method.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Lecture 12-2: Introduction to Computer Algorithms beyond Search & Sort.
CSE, IIT KGP Matchings and Factors. CSE, IIT KGP Matchings A matching of size k in a graph G is a set of k pairwise disjoint edges.A matching of size.
Section 2.1 “Matching in bipartite graphs” in Graph Theory Handout for reading seminar.
CSE 331: Review. Main Steps in Algorithm Design Problem Statement Algorithm Real world problem Problem Definition Precise mathematical def “Implementation”
All Pair Shortest Path IOI/ACM ICPC Training June 2004.
Bipartite Matching. Unweighted Bipartite Matching.
Network Flows Chun-Ta, Yu Graduate Institute Information Management Dept. National Taiwan University.
Graph Algorithms Maximum Flow - Best algorithms [Adapted from R.Solis-Oba]
Introduction to Graph Theory
Indian Institute of Technology Kharagpur PALLAB DASGUPTA Graph Theory: Matchings and Factors Pallab Dasgupta, Professor, Dept. of Computer Sc. and Engineering,
1 GRAPHS – Definitions A graph G = (V, E) consists of –a set of vertices, V, and –a set of edges, E, where each edge is a pair (v,w) s.t. v,w  V Vertices.
Trees.
Tirgul 12 Solving T4 Q. 3,4 Rehearsal about MST and Union-Find
Hamiltonian Graphs Graphs Hubert Chan (Chapter 9.5)
IOI/ACM ICPC Training 4 June 2005.
Chapter 10 Iterative Improvement
COMP108 Algorithmic Foundations Greedy methods
Bipartite Matching Lecture 8: Oct 7.
CSC317 Shortest path algorithms
CSE 2331/5331 Topic 9: Basic Graph Alg.
Hamiltonian Graphs Graphs Hubert Chan (Chapter 9.5)
Bipartite Graphs What is a bipartite graph?
Algorithms and Networks
Lecture 16 Bipartite Matching
Maximum Flow - Best algorithms
Lectures on Network Flows
Lecture 12 Algorithm Analysis
Introduction to Graphs
Ellen Walker CPSC 201 Data Structures Hiram College
Greedy Algorithms / Minimum Spanning Tree Yin Tat Lee
Chapter 5. Optimal Matchings
CS202 - Fundamental Structures of Computer Science II
More Graph Algorithms.
CSE 421: Introduction to Algorithms
Instructor: Shengyu Zhang
Analysis of Algorithms
Algorithms (2IL15) – Lecture 5 SINGLE-SOURCE SHORTEST PATHS
Lectures on Graph Algorithms: searching, testing and sorting
Graphs.
Algorithms and Data Structures Lecture XIII
Lecture 12 Algorithm Analysis
CSE 421: Introduction to Algorithms
Bipartite Graph 1. A graph G is bipartite if the node set V can be partitioned into two sets V1 and V2 in such a way that no nodes from the same set are.
Algorithms (2IL15) – Lecture 7
Lecture 14 Shortest Path (cont’d) Minimum Spanning Tree
Text Book: Introduction to algorithms By C L R S
Lecture 12 Algorithm Analysis
Richard Anderson Winter 2019 Lecture 6
Lecture 13 Shortest Path (cont’d) Minimum Spanning Tree
GRAPH – Definitions A graph G = (V, E) consists of
INTRODUCTION A graph G=(V,E) consists of a finite non empty set of vertices V , and a finite set of edges E which connect pairs of vertices .
GRAPH TRAVERSAL.
Presentation transcript:

Various Graph Algorithms

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.

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.

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

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

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

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

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

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

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

Idea k j i

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

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?

Topological sort

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

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

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;

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 ]

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

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;

Bipartite Matching

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

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

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

Definition Note that maximum matching is not unique.

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!

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

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

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

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’

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’

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

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.

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

Labelling Algorithm Start with arbitrary matching

Labelling Algorithm Pick a free vertex in the bottom

Labelling Algorithm Run BFS

Labelling Algorithm Alternate unmatched/matched edges

Labelling Algorithm Until a augmenting path is found

Flip!

Repeat Pick another free vertex in the bottom

Repeat Run BFS

Repeat Flip

Answer Since we cannot find any augmenting path, stop!

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.

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

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

Weighted Bipartite Graph 3 4 6 6

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

Maximum Weighted Matching Score: 6+1+1+1+4=13 3 4 6 6

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!

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.

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.

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

Stable Marriage Problem ON YOU OWN

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.

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

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?

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

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

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

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

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