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

Similar documents
Data structures and libraries

INF280 Graph Traversals & Paths

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

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

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

Graph Algorithms (part 3 of CSC 282),


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

Lecture 13. Reading: Weiss, Ch. 9, Ch 8 CSE 100, UCSD: LEC 13. Page 1 of 29

Problem solving paradigms

Minimum Spanning Trees

Minimum Spanning Tree

2. True or false: even though BFS and DFS have the same space complexity, they do not always have the same worst case asymptotic time complexity.

Algorithms (IX) Yijia Chen Shanghai Jiaotong University

Chapter 9 Graph Algorithms

11/22/2016. Chapter 9 Graph Algorithms. Introduction. Definitions. Definitions. Definitions. Definitions

Graph Algorithms (part 3 of CSC 282),

Analysis of Algorithms, I

Shortest path algorithms

Shortest path problems

Dynamic Programming. Bjarki Ágúst Guðmundsson Tómas Ken Magnússon. School of Computer Science Reykjavík University

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

DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI. Department of Computer Science and Engineering CS6301 PROGRAMMING DATA STRUCTURES II

CS521 \ Notes for the Final Exam

Graphs. Part II: SP and MST. Laura Toma Algorithms (csci2200), Bowdoin College

Chapter 9 Graph Algorithms

CS161 - Minimum Spanning Trees and Single Source Shortest Paths

Unweighted Graphs & Algorithms

Info 2950, Lecture 16

CSE 100 Minimum Spanning Trees Prim s and Kruskal

Title. Ferienakademie im Sarntal Course 2 Distance Problems: Theory and Praxis. Nesrine Damak. Fakultät für Informatik TU München. 20.

Chapter 9 Graph Algorithms

Unit #9: Graphs. CPSC 221: Algorithms and Data Structures. Will Evans 2012W1

Addis Ababa University, Amist Kilo July 20, 2011 Algorithms and Programming for High Schoolers. Lecture 12

Total Points: 60. Duration: 1hr

Weighted Graph Algorithms Presented by Jason Yuan

CSE 21: Mathematics for Algorithms and Systems Analysis

The Shortest Path Problem

PATH FINDING AND GRAPH TRAVERSAL

Introduction to Algorithms / Algorithms I Lecturer: Michael Dinitz Topic: Shortest Paths Date: 10/13/15

Problem 1. Which of the following is true of functions =100 +log and = + log? Problem 2. Which of the following is true of functions = 2 and =3?

Dijkstra s algorithm for shortest paths when no edges have negative weight.

Practice Problems for the Final

CSE 100: GRAPH SEARCH

CIS 121 Data Structures and Algorithms Minimum Spanning Trees

UNIT 3. Greedy Method. Design and Analysis of Algorithms GENERAL METHOD

CS 310 Advanced Data Structures and Algorithms

Computer Science 302 Spring 2017 (Practice for) Final Examination, May 10, 2017

Pred 8 1. Dist. Pred

Lecture 10: Analysis of Algorithms (CS ) 1

Shortest Paths. Nishant Mehta Lectures 10 and 11

CS 125 Section #5 Graph Traversal and Linear Programs October 6, 2016

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

CS4800: Algorithms & Data Jonathan Ullman

CS490 Quiz 1. This is the written part of Quiz 1. The quiz is closed book; in particular, no notes, calculators and cell phones are allowed.

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

Shortest Paths. Nishant Mehta Lectures 10 and 11

4/8/11. Single-Source Shortest Path. Shortest Paths. Shortest Paths. Chapter 24

CSE373: Data Structures & Algorithms Lecture 17: Minimum Spanning Trees. Dan Grossman Fall 2013

Spanning Trees 4/19/17. Prelim 2, assignments. Undirected trees

Spanning Trees. Lecture 22 CS2110 Spring 2017

COP 4531 Complexity & Analysis of Data Structures & Algorithms

CS 125 Section #6 Graph Traversal and Linear Programs 10/13/14

CMPSCI611: The SUBSET-SUM Problem Lecture 18

Lecture 3: Graphs and flows

Problem Score Maximum MC 34 (25/17) = 50 Total 100

Shortest Paths and Minimum Spanning Trees

Algorithms (V) Path in Graphs. Guoqiang Li. School of Software, Shanghai Jiao Tong University

Data Structures Brett Bernstein


Graphs. CSE 2320 Algorithms and Data Structures Alexandra Stefan and Vassilis Athitsos University of Texas at Arlington

UNIT Name the different ways of representing a graph? a.adjacencymatrix b. Adjacency list

Matching 4/21/2016. Bipartite Matching. 3330: Algorithms. First Try. Maximum Matching. Key Questions. Existence of Perfect Matching

SPANNING TREES. Lecture 21 CS2110 Spring 2016

CS 3410 Ch 14 Graphs and Paths

Exam 3 Practice Problems

Data Structures and Algorithms for Engineers

More Graph Algorithms: Topological Sort and Shortest Distance

Lecture 10 Graph Algorithms

Lecture 14: Shortest Paths Steven Skiena

CHAPTER 13 GRAPH ALGORITHMS

Shortest Path Problem

Dynamic-Programming algorithms for shortest path problems: Bellman-Ford (for singlesource) and Floyd-Warshall (for all-pairs).

COMP 355 Advanced Algorithms

Lecture 10. Graphs Vertices, edges, paths, cycles Sparse and dense graphs Representations: adjacency matrices and adjacency lists Implementation notes

Algorithms for Data Science

CS473-Algorithms I. Lecture 13-A. Graphs. Cevdet Aykanat - Bilkent University Computer Engineering Department

Senior Computer Team Lectures : Shortest Path Just use variables that mean something!! - G. Galperin. by Gary Sivek

CSE 373 MAY 10 TH SPANNING TREES AND UNION FIND

TIE Graph algorithms

Shortest Paths. CSE 373 Data Structures Lecture 21

10/31/18. About A6, Prelim 2. Spanning Trees, greedy algorithms. Facts about trees. Undirected trees

Spanning Trees, greedy algorithms. Lecture 20 CS2110 Fall 2018

Topological Sort. Here a topological sort would label A with 1, B and C with 2 and 3, and D with 4.

Unit-5 Dynamic Programming 2016

UNIT 5 GRAPH. Application of Graph Structure in real world:- Graph Terminologies:

Dijkstra s Algorithm Last time we saw two methods to solve the all-pairs shortest path problem: Min-plus matrix powering in O(n 3 log n) time and the

This course is intended for 3rd and/or 4th year undergraduate majors in Computer Science.

(Re)Introduction to Graphs and Some Algorithms

CS350: Data Structures Dijkstra s Shortest Path Alg.

Transcription:

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

Today we re going to cover Minimum spanning tree Shortest paths Some known graph problems Special graphs Trees Directed acyclic graphs Bipartite graphs 2

Weighted graphs Now the edges in our graphs may have weights, which could represent the distance of the road represented by the edge the cost of going over the edge some capacity of the edge We can use a modified adjacency list to represent weighted graphs

Weighted graphs struct edge { int u, v; int weight; }; edge(int _u, int _v, int _w) { u = _u; v = _v; weight = _w; } -4 2 2.9 4

Weighted graphs vector<edge> adj[4]; adj[].push_back(edge(,, )); adj[].push_back(edge(, 2, -4)); adj[].push_back(edge(,, )); adj[].push_back(edge(, 2, )); -4 2 adj[2].push_back(edge(2,, -4)); adj[2].push_back(edge(2,, )); adj[2].push_back(edge(2,, 2.9)); 2.9 adj[].push_back(edge(, 2, 2.9)); 5

Minimum spanning tree We have an undirected weighted graph The vertices along with a subset of the edges in the graph is called a spanning tree if it forms a tree (i.e. does not contain a cycle) and the tree spans all vertices (all vertices can reach all other vertices) The weight of a spanning tree is the sum of the weights of the edges in the subset We want to find a minimum spanning tree 6

Minimum spanning tree Several greedy algorithms work Go through the edges in the graph in increasing order of weight Greedily pick an edge if it doesn t form a cycle (Union-Find can be used to keep track of when we would get a cycle) When we ve gone through all edges, we have a minimum spanning tree This is Kruskal s algorithm Time complexity is O(E log E) 7

Minimum spanning tree bool edge_cmp(const edge &a, const edge &b) { return a.weight < b.weight; } vector<edge> mst(int n, vector<edge> edges) { union_find uf(n); sort(edges.begin(), edges.end(), edge_cmp); vector<edge> res; for (int i = ; i < edges.size(); i++) { int u = edges[i].u, v = edges[i].v; } if (uf.find(u)!= uf.find(v)) { uf.unite(u, v); res.push_back(edges[i]); } } return res; 8

Shortest paths We have a weighted graph (undirected or directed) Given two vertices u, v, what is the shortest path from u to v? If all weights are the same, this can be solved with breadth-first search Of course, this is usually not the case... 9

Shortest paths There are many known algorithms to find shortest paths Like breadth-first search, these algorithms usually find the shortest paths from a given start vertex to all other vertices Let s take a quick look at Dijkstra s algorithm, the Bellman-Ford algorithm and the Floyd-Warshall algorithm

Dijkstra s algorithm vector<edge> adj[]; vector<int> dist(, INF); void dijkstra(int start) { dist[start] = ; priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > pq; pq.push(make_pair(dist[start], start)); while (!pq.empty()) { int u = pq.top().second, d = pq.top().first; pq.pop(); if (d > dist[u]) continue; for (int i = ; i < adj[u].size(); i++) { int v = adj[u][i].v, w = adj[u][i].weight; if (w + dist[u] < dist[v]) { dist[v] = w + dist[u]; pq.push(make_pair(dist[v], v)); } } } }

Dijkstra s algorithm Time complexity is O(V log E) Note that this only works for non-negative weights 2

Bellman-Ford algorithm void bellman_ford(int n, int start) { dist[start] = ; } for (int i = ; i < n - ; i++) { for (int u = ; u < n; u++) { for (int j = ; j < adj[u].size(); j++) { int v = adj[u][j].v; int w = adj[u][j].weight; dist[v] = min(dist[v], w + dist[u]); } } }

Bellman-Ford algorithm Time complexity is O(V E) Can be used to detect negative-weight cycles 4

Floyd-Warshall algorithm What about using dynamic programming to compute shortest paths? Let sp(k, i, j) be the shortest path from i to j if we re only allowed to travel through the vertices,..., k Base case: sp(k, i, j) = if i = j Base case: sp(, i, j) = weight[a][b] if (i, j) E Base case: sp(, i, j) = { sp(k, i, k) + sp(k, k, j) sp(k, i, j) = min sp(k, i, j) 5

Floyd-Warshall algorithm int dist[][]; int weight[][]; void floyd_warshall(int n) { for (int i = ; i < n; i++) { for (int j = ; j < n; j++) { dist[i][j] = i == j? : weight[i][j]; } } } for (int k = ; k < n; k++) { for (int i = ; i < n; i++) { for (int j = ; j < n; j++) { dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]); } } } 6

Floyd-Warshall algorithm Computes all-pairs shortest paths Time complexity is clearly O(n ) Very simple to code 7

Known graph problems The problems we re dealing with very often ask us to solve some well known graph problem But usually it s well hidden in the problem statement Let s take a look at a few examples 8

Minimum vertex cover We have an unweighted undirected graph A vertex cover is a subset of the vertices S, such that for each edge (u, v) in the graph, either u or v (or both) are in S 4 2 5 9

Minimum vertex cover We have an unweighted undirected graph A vertex cover is a subset of the vertices S, such that for each edge (u, v) in the graph, either u or v (or both) are in S 4 2 5 9

Minimum vertex cover We have an unweighted undirected graph A vertex cover is a subset of the vertices S, such that for each edge (u, v) in the graph, either u or v (or both) are in S 4 2 5 We want to find a vertex cover of minimum size 9

Minimum vertex cover We have an unweighted undirected graph A vertex cover is a subset of the vertices S, such that for each edge (u, v) in the graph, either u or v (or both) are in S 4 2 5 We want to find a vertex cover of minimum size 9

Minimum vertex cover We have an unweighted undirected graph A vertex cover is a subset of the vertices S, such that for each edge (u, v) in the graph, either u or v (or both) are in S 4 2 5 We want to find a vertex cover of minimum size NP-hard problem in general graphs 9

Maximum independent set We have an unweighted undirected graph An independent set is a subset of the vertices S, such that no two vertices u, v in S are adjacent in the graph 4 2 5 2

Maximum independent set We have an unweighted undirected graph An independent set is a subset of the vertices S, such that no two vertices u, v in S are adjacent in the graph 4 2 5 2

Maximum independent set We have an unweighted undirected graph An independent set is a subset of the vertices S, such that no two vertices u, v in S are adjacent in the graph 4 2 5 We want to find an independent set of maximum size 2

Maximum independent set We have an unweighted undirected graph An independent set is a subset of the vertices S, such that no two vertices u, v in S are adjacent in the graph 4 2 5 We want to find an independent set of maximum size 2

Maximum independent set We have an unweighted undirected graph An independent set is a subset of the vertices S, such that no two vertices u, v in S are adjacent in the graph 4 2 5 We want to find an independent set of maximum size NP-hard problem in general graphs 2

Relation between MVC and MIS The previous two problems are very related A subset of the vertices is a vertex cover if and only if the complement of the set is an independent set 4 2 5 2

Relation between MVC and MIS The previous two problems are very related A subset of the vertices is a vertex cover if and only if the complement of the set is an independent set 4 2 5 2

Relation between MVC and MIS The previous two problems are very related A subset of the vertices is a vertex cover if and only if the complement of the set is an independent set 4 2 5 2

Relation between MVC and MIS The previous two problems are very related A subset of the vertices is a vertex cover if and only if the complement of the set is an independent set 4 2 5 The size of a minimum vertex cover plus the size of a maximum independent set is equal to the number of vertices 2

Maximum matching We have an unweighted undirected graph A matching is a subset of the edges such that each vertex is adjacent to at most one edge in the subset 4 2 5 6 22

Maximum matching We have an unweighted undirected graph A matching is a subset of the edges such that each vertex is adjacent to at most one edge in the subset 4 2 5 6 22

Maximum matching We have an unweighted undirected graph A matching is a subset of the edges such that each vertex is adjacent to at most one edge in the subset 4 2 5 6 We want to find a matching of maximum size 22

Maximum matching We have an unweighted undirected graph A matching is a subset of the edges such that each vertex is adjacent to at most one edge in the subset 4 2 5 6 We want to find a matching of maximum size 22

Maximum matching We have an unweighted undirected graph A matching is a subset of the edges such that each vertex is adjacent to at most one edge in the subset 4 2 5 6 We want to find a matching of maximum size There exists an O(V 4 ) algorithm for general graphs, but is pretty complex 22

Graph coloring We have an unweighted undirected graph A coloring of the graph is an assignment of colors to the vertices such that adjacent vertices have different colors 4 2 5 2

Graph coloring We have an unweighted undirected graph A coloring of the graph is an assignment of colors to the vertices such that adjacent vertices have different colors 4 2 5 2

Graph coloring We have an unweighted undirected graph A coloring of the graph is an assignment of colors to the vertices such that adjacent vertices have different colors 4 2 5 We want to find a coloring that uses the minimum number of distinct colors 2

Graph coloring We have an unweighted undirected graph A coloring of the graph is an assignment of colors to the vertices such that adjacent vertices have different colors 4 2 5 We want to find a coloring that uses the minimum number of distinct colors 2

Graph coloring We have an unweighted undirected graph A coloring of the graph is an assignment of colors to the vertices such that adjacent vertices have different colors 4 2 5 We want to find a coloring that uses the minimum number of distinct colors NP-hard in general graphs 2

Special graphs All of these problems are hard (in some sense) in general graphs But what if we re working with special kinds of graphs? Let s look at a few examples 24

Bipartite graphs A graph is bipartite if the vertices can be partitioned into two sets such that for each edge (u, v) u and v are in different sets 4 2 5 6 How do we check if a graph is bipartite? 25

Bipartite graphs A graph is bipartite if the vertices can be partitioned into two sets such that for each edge (u, v) u and v are in different sets 4 2 5 6 How do we check if a graph is bipartite? 25

Bipartite graphs A graph is bipartite if the vertices can be partitioned into two sets such that for each edge (u, v) u and v are in different sets 6 4 2 5 How do we check if a graph is bipartite? 25

Bipartite graphs A graph is bipartite if the vertices can be partitioned into two sets such that for each edge (u, v) u and v are in different sets 6 2 5 4 How do we check if a graph is bipartite? 25

Bipartite graphs A graph is bipartite if the vertices can be partitioned into two sets such that for each edge (u, v) u and v are in different sets 6 2 5 4 How do we check if a graph is bipartite? 25

Bipartite graphs We want to check if we can split the vertices into these two groups Take any vertex, and assume that it s in the first group Then all of his neighbors must be in the second group And then all of their neighbors must be in the first group And so on... We can do this with a simple depth-first search If we ever find a contradiction (i.e. a vertex must both be in the first and second set), then the graph is not bipartite 26

Bipartite graphs vector<int> adj[]; vector<int> side(, -); bool is_bipartite = true; void check_bipartite(int u) { for (int i = ; i < adj[u].size(); i++) { int v = adj[u][i]; if (side[v] == -) { side[v] = - side[u]; check_bipartite(v); } else if (side[u] == side[v]) { is_bipartite = false; } } } for (int u = ; u < n; u++) { if (side[u] == -) { side[u] = ; check_bipartite(u); } } 27

Coloring bipartite graphs What if we want to find the minimum graph coloring of a bipartite graph? 6 2 5 4 28

Coloring bipartite graphs What if we want to find the minimum graph coloring of a bipartite graph? 6 2 5 4 Simple, one side can be colored with one color, and the second side can be colored with a second color 28

Bipartite matching Finding a maximum matching in bipartite graphs is very common see example Soon we ll see an efficient algorithm for finding the maximum matching in a bipartite graph 29

König s theorem König s theorem states that the size of a minimum vertex cover in a bipartite graph is equal to the size of the maximum matching in that graph To find the minimum vertex cover in a bipartite graph, we just find the maximum matching with our efficient algorithm, and we have our answer And since the size of the maximum independent set is just the number of vertices minus the size of the minimum vertex cover, we can also compute the maximum independent set for a bipartite graph efficiently

Trees An undirected graph is a tree if it has no cycles Easy to check if a graph is a tree by checking if there are any backward edges in the depth-first search tree (see previous lecture) A connected tree with n vertices has exactly n edges Between each pair of vertices u, v in the tree, there exists exactly one simple path, which can be found with depth-first search (or breadth-first search)

Trees What if we look at these problems for trees? How do we find the minimum number of colors needed to color a tree? 2

Trees What if we look at these problems for trees? How do we find the minimum number of colors needed to color a tree? Well, trees are actually bipartite graphs... Why? Pick some vertex and make it the root of the tree. Then vertices at even heights in the tree can be put on one side, and vertices at odd heights can be put on the other side So all the efficient algorithms for bipartite graphs also work for trees 2

Trees Trees are also well suited for dynamic programming, so many problems become simpler here because of that

Directed acyclic graphs A directed graph is a directed acyclic graph if it doesn t contain any cycles Easy to check if a graph is a DAG by checking if there are any backward edges in the depth-first search tree (see previous lecture) Many problems are simple on DAGs, since it s easy to do dynamic programming over DAGs counting number of simple paths from u to v longest simple path from u to v 4