INF280 Graph Algorithms

Similar documents
INF280 Graph Traversals & Paths

Undirected Graphs. DSA - lecture 6 - T.U.Cluj-Napoca - M. Joldos 1

CS 161: Design and Analysis of Algorithms

22c:31 Algorithms. Kruskal s Algorithm for MST Union-Find for Disjoint Sets

CS F-14 Disjoint Sets 1

Unweighted Graphs & Algorithms

Graph Representation DFS BFS Dijkstra A* Search Bellman-Ford Floyd-Warshall Iterative? Non-iterative? MST Flow Edmond-Karp

CS 310 Advanced Data Structures and Algorithms

CSE 373 MAY 10 TH SPANNING TREES AND UNION FIND

COMP4128 Programming Challenges

COMP Analysis of Algorithms & Data Structures

Graph Traversal. Section Dr. Chris Mayfield. Oct 24, Department of Computer Science James Madison University

Introduction to Parallel & Distributed Computing Parallel Graph Algorithms

Agenda. Graph Representation DFS BFS Dijkstra A* Search Bellman-Ford Floyd-Warshall Iterative? Non-iterative? MST Flow Edmond-Karp

CS490: Problem Solving in Computer Science Lecture 6: Introductory Graph Theory

COMP 251 Winter 2017 Online quizzes with answers

Building a network. Properties of the optimal solutions. Trees. A greedy approach. Lemma (1) Lemma (2) Lemma (3) Lemma (4)

Chapter 5. Greedy algorithms

Algorithms (IX) Yijia Chen Shanghai Jiaotong University

Data structures. Bjarki Ágúst Guðmundsson Tómas Ken Magnússon. Árangursrík forritun og lausn verkefna. School of Computer Science Reykjavík University

& ( D. " mnp ' ( ) n 3. n 2. ( ) C. " n

CS S-14 Disjoint Sets 1

CSE 100: GRAPH ALGORITHMS

Lecture 3: Graphs and flows

CMSC 341 Lecture 20 Disjointed Sets

CSci 231 Final Review

Chapter 23. Minimum Spanning Trees

and 6.855J March 6, Maximum Flows 2

Maximum Flow. Flow Networks. Flow Networks Ford-Fulkerson Method Edmonds-Karp Algorithm Push-Relabel Algorithms. Example Flow Network

Graph Algorithms. Parallel and Distributed Computing. Department of Computer Science and Engineering (DEI) Instituto Superior Técnico.

CS521 \ Notes for the Final Exam

Graphs and Network Flows ISE 411. Lecture 7. Dr. Ted Ralphs

Advanced Algorithms Class Notes for Monday, October 23, 2012 Min Ye, Mingfu Shao, and Bernard Moret

Lecture 6 Basic Graph Algorithms

Source. Sink. Chapter 10: Iterative Programming Maximum Flow Problem. CmSc250 Intro to Algorithms

22 Elementary Graph Algorithms. There are two standard ways to represent a

Minimum-Spanning-Tree problem. Minimum Spanning Trees (Forests) Minimum-Spanning-Tree problem

22 Elementary Graph Algorithms. There are two standard ways to represent a

Minimum Spanning Tree (undirected graph)

1 Shortest Paths. 1.1 Breadth First Search (BFS) CS 124 Section #3 Shortest Paths and MSTs 2/13/2018

CMSC 341 Lecture 20 Disjointed Sets

looking ahead to see the optimum

Solutions to Exam Data structures (X and NV)

COP 4531 Complexity & Analysis of Data Structures & Algorithms

UC Berkeley CS 170: Efficient Algorithms and Intractable Problems Handout 8 Lecturer: David Wagner February 20, Notes 8 for CS 170

Tutorial. Question There are no forward edges. 4. For each back edge u, v, we have 0 d[v] d[u].

Minimum Spanning Trees and Union Find. CSE 101: Design and Analysis of Algorithms Lecture 7

Graphs and Network Flows IE411. Lecture 13. Dr. Ted Ralphs

Network Design and Optimization course

Decreasing a key FIB-HEAP-DECREASE-KEY(,, ) 3.. NIL. 2. error new key is greater than current key 6. CASCADING-CUT(, )

Tutorial for Algorithm s Theory Problem Set 5. January 17, 2013

n 2 ( ) ( ) + n is in Θ n logn

Homework Assignment #3 Graph

Graphs. Bjarki Ágúst Guðmundsson Tómas Ken Magnússon Árangursrík forritun og lausn verkefna. School of Computer Science Reykjavík University

1 Shortest Paths. 1.1 Breadth First Search (BFS) CS 124 Section #3 Shortest Paths and MSTs 2/13/2018

Graphs 2: Networks. Outside the Box PU2

Lecture 13: Minimum Spanning Trees Steven Skiena

GRAPHICAL ALGORITHMS. UNIT _II Lecture-12 Slides No. 3-7 Lecture Slides No Lecture Slides No

CMSC 341 Lecture 20 Disjointed Sets

Lecture 4 Greedy algorithms

CIS 121 Data Structures and Algorithms Minimum Spanning Trees

Graphs. Edges may be directed (from u to v) or undirected. Undirected edge eqvt to pair of directed edges

Priority queue ADT part 1

Spanning trees. Suppose you have a connected undirected graph

CISC 320 Midterm Exam

Reference Sheet for CO142.2 Discrete Mathematics II

Week 12: Minimum Spanning trees and Shortest Paths

Algorithm Design (8) Graph Algorithms 1/2

Info 2950, Lecture 16

Index. stack-based, 400 A* algorithm, 325

Disjoint Sets. Based on slides from previous iterations of this course.

Graphs and Network Flows IE411. Lecture 21. Dr. Ted Ralphs

Local Algorithms for Sparse Spanning Graphs

Lecture 4: Graph Algorithms

Minimum Spanning Trees

CSE373: Data Structures & Algorithms Lecture 11: Implementing Union-Find. Lauren Milne Spring 2015

Homework 5: Graphs, Minimum Spanning Trees, and Dijkstra Shortest-Path

Lecture 13: Minimum Spanning Trees Steven Skiena. Department of Computer Science State University of New York Stony Brook, NY

Lecture 10 Graph Algorithms

Lecture 10. Elementary Graph Algorithm Minimum Spanning Trees

Weighted Graph Algorithms Presented by Jason Yuan

Minimum Spanning Trees

Solving problems on graph algorithms

Data structures and libraries

Lecture Summary CSC 263H. August 5, 2016

CS/COE 1501 cs.pitt.edu/~bill/1501/ Network Flow

GRAPHS (Undirected) Graph: Set of objects with pairwise connections. Why study graph algorithms?

Part VI Graph algorithms. Chapter 22 Elementary Graph Algorithms Chapter 23 Minimum Spanning Trees Chapter 24 Single-source Shortest Paths

6.1 Minimum Spanning Trees

2.1 Greedy Algorithms. 2.2 Minimum Spanning Trees. CS125 Lecture 2 Fall 2016

Representations of Weighted Graphs (as Matrices) Algorithms and Data Structures: Minimum Spanning Trees. Weighted Graphs

CS 220: Discrete Structures and their Applications. graphs zybooks chapter 10

CSE 431/531: Algorithm Analysis and Design (Spring 2018) Greedy Algorithms. Lecturer: Shi Li

Test 1 Last 4 Digits of Mav ID # Multiple Choice. Write your answer to the LEFT of each problem. 2 points each t 1

Union Find and Greedy Algorithms. CSE 101: Design and Analysis of Algorithms Lecture 8

Parallel Breadth First Search

Jessica Su (some parts copied from CLRS / last quarter s notes)

CS 341: Algorithms. Douglas R. Stinson. David R. Cheriton School of Computer Science University of Waterloo. February 26, 2019

Outline. Graphs. Divide and Conquer.

Exam 1 Solutions. Jonathan Turner 10/4/01 9/27/01. CS 541 Algorithms and Programs. Be neat and concise, but complete.

Transcription:

INF280 Graph Algorithms Florian Brandner 21 March 2017 1/19

Contents Union-Find Minimum Spanning Trees Flows in Graphs Assignment Problems and Matchings 2/19

Union-Find A data structure to track equivalence relations between elements: Elements are partitioned into non-overlapping sets Initially only pairwise relations are known (i.e., X and Y are in the same set) From pairwise relations, deduce the global partitioning step-wise Basic idea: Represent partitions as trees Merge trees when a new pairwise relation is discovered 3/19

Union-Find (2) Two operations allow to update/query the union-find data structure: Union(x,y): Add a new pairwise relation between x and y and update the Union-Find structure to put them in the same set Find(x): Get the (current) representative of the set for element x https://visualgo.net/ufds 4/19

Union-Find using Ranks and Path Compression map<int, pair<int, unsigned int> > Sets; // map to parent & rank void MakeSet(int x) { Sets.insert(make_pair(x, make_pair(x, 0))); int Find(int x) { if (Sets[x].first == x) return x; // Parent == x? else return Sets[x].first = Find(Sets[x].first); // Get Parent void Union(int x, int y) { int parentx = Find(x), parenty = Find(y); int rankx = Sets[parentX].second, ranky = Sets[parentY].second; if (parentx == parenty) return; else if (rankx < ranky) Sets[parentX].first = parenty; else Sets[parentY].first = parentx; if (rankx == ranky) Sets[parentX].second++;; 5/19

Contents Union-Find Minimum Spanning Trees Flows in Graphs Assignment Problems and Matchings 6/19

Spanning Trees Subgraph of undirected connected graph that forms a tree The subgraph contains each node of the original graph Computing spanning trees Many possible spanning trees exist Any depth-first traversal gives a spanning tree In weighted graphs one might need a minimum spanning tree A spanning tree as defined above Require that the total sum of edge weights is minimal (i.e., no other spanning tree with a lower total sum exists) 7/19

Example: Minimum Spanning Trees 4 1 3 2 7 2 8 3 8 https://commons.wikimedia.org/wiki/file:minimum_spanning_tree.svg, Dcoetzee, public domain 8/19

Kruskal s Algorithm vector<pair<int, pair<int, int> > > Edges; set<pair<int,int> > A; // Final minimum spanning tree void Kruskal() { for(int u=0; u < N; u++) MakeSet(u); sort(edges.begin(), Edges.end()); // Initialize Union-Find DS // Sort edges by weight for(auto tmp : Edges) { auto edge = tmp.second; if (Find(edge.first)!= Find(edge.second)) { Union(edge.first, edge.second); // update Union-Find DS A.insert(edge); // include edge in MST https://visualgo.net/mst 9/19

Contents Union-Find Minimum Spanning Trees Flows in Graphs Assignment Problems and Matchings 10/19

Flow Networks Flow networks are weighted directed graphs Edge weights denote the capacity of edges Current in an electric circuit Water in pipes Trains on a railroad Question: What is the maximum flow between nodes s and t? Assign flow to each edge respecting the edge s capacity For each node, the combined in/out flows must be equal 11/19

Maximum Flow / Minimum Cut Maximum flow is limited by a cut separating s and t Basic idea behind cuts: Partition the network s nodes into two sets S and T S contains s while T contains t Edges (u, v) with u S and v T are in the cut between S and T The edges in the cut completely separate the two sets = Removing those edges gives a maximum flow of zero Link between cuts and flows: The combined edge weights of a cut bound the flow from s to t The maximum flow is thus limited by a minimum cut 12/19

Ford-Fulkerson Algorithm // find path from s to t in G, return true if such a path exists bool DFS(int G[MAXN][MAXN], int s, int t, int Predecessor[MAXN]); int FordFulkerson(int G[MAXN][MAXN], int s, int t) { int GRes[MAXN][MAXN]; // residual graph copy_n((int*)g, MAXN*MAXN, (int*)gres); // copy original graph int Predecessor[MAXN]; int Maxflow = 0; while (DFS(GRes, s, t, Predecessor)) { // find residual path int Bottleneck = MAXFLOW; // get minimal flow of residual path for(int v = t, u = Predecessor[t]; v!= s; v = u, u = Predecessor[u]) Bottleneck = min(bottleneck, GRes[u][v]); for(int v = t, u = Predecessor[t]; v!= s; v = u, u = Predecessor[u]) GRes[u][v] -= Bottleneck; // decrease capacity along residual path GRes[v][u] += Bottleneck; Maxflow += Bottleneck; return Maxflow; https://visualgo.net/maxflow 13/19

Contents Union-Find Minimum Spanning Trees Flows in Graphs Assignment Problems and Matchings 14/19

Assignment Problems and Matchings Represented as bipartite graphs: Nodes are partitioned into two disjoint sets X and Y Edges always connect nodes from both sets (G = (X Y, E), where E X Y) Basic idea: Search the best assignment of elements in X to elements in Y Each element may appear only in one assignment Problem variants Maximize matching cardinality (Hopcroft-Karp on next slide) Maximize matching cost in weighted graphs 15/19

Hopcroft-Karp (data) // Artificial node (unused otherwise) -- end of augmenting path #define NIL 0 // "Infinity", i.e., value larger than min( X, Y ) #define INF numeric_limits<unsigned int>::max() // Partitions X and Y vector<int> X, Y; // Neighbors in Y of nodes in X vector<int> Adj[MAXX]; // Matching X-Y and Y-X int PairX[MAXX]; int PairY[MAXY]; // Augmenting path lengths unsigned int Dist[MAXX]; 16/19

Hopcroft-Karp (main) int HopcroftKarp() { fill_n(pairx, X.size(), NIL); fill_n(pairy, Y.size(), NIL); // initialize: empty matching int Matching = 0; // count number of edges in matching while (BFS()) { // find all shortest augmenting paths for(auto x : X) // update matching cardinality if (PairX[x] == NIL && // node not yet in matching? DFS(x)) // does an augmenting path start at x? Matching++; return Matching; 17/19

Hopcroft-Karp (BFS) bool BFS() { queue<int> Q; Dist[NIL] = INF; for(auto x : X) // start from nodes that are not yet matched Dist[x] = (PairX[x] == NIL)? 0 : INF; if (PairX[x] == NIL) Q.push(x); while (!Q.empty()) { // find all shortest paths to NIL int x = Q.front(); Q.pop(); if (Dist[x] < Dist[NIL]) // can this become a shorter path? for (auto y : Adj[x]) if (Dist[PairY[y]] == INF) { Dist[PairY[y]] = Dist[x] + 1; // update path length Q.push(PairY[y]); return Dist[NIL]!= INF; // any shortest path to NIL found? 18/19

Hopcroft-Karp (DFS) bool DFS(int x) { if (x!= NIL) { for (auto y : Adj[x]) if (Dist[PairY[y]] == Dist[x] + 1 && DFS(PairY[y])) { // follow trace of BFS PairX[x] = y; // add edge from x to y to matching PairY[y] = x; return true; Dist[x] = INF; return false; // no augmenting path found return true; // reached NIL 19/19