Lecture 10 Graph Algorithms

Similar documents
Lecture 14: Shortest Paths Steven Skiena

LEC13: SHORTEST PATH. CSE 373 Analysis of Algorithms Fall 2016 Instructor: Prof. Sael Lee. Lecture slide courtesy of Prof.

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

Lecture 13: Minimum Spanning Trees Steven Skiena

Lecture 9 Graph Traversal

Chapter 14 Section 3 - Slide 1

Unweighted Graphs & Algorithms

Chapter 9 Graph Algorithms

Lecture 9 Graph Traversal

Chapter 9 Graph Algorithms

Chapter 9 Graph Algorithms

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

Classic Graph Theory Problems

Data Structures and Algorithms for Engineers

Question Points Score TOTAL 55 TOTAL (Calibrated) 50

Chapter 10. Graph Algorithms

PATH FINDING AND GRAPH TRAVERSAL

Outline. Graphs. Divide and Conquer.

Solving problems on graph algorithms

Lecture 3: Graphs and flows

Department of Computer Applications. MCA 312: Design and Analysis of Algorithms. [Part I : Medium Answer Type Questions] UNIT I

CSE 100 Minimum Spanning Trees Prim s and Kruskal

(Re)Introduction to Graphs and Some Algorithms

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

Introduction to Parallel & Distributed Computing Parallel Graph Algorithms

Graph Algorithms (part 3 of CSC 282),

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

Maximum Flows of Minimum Cost

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

DESIGN AND ANALYSIS OF ALGORITHMS

Greedy Algorithms. COMP 215 Lecture 6

Data Structures and Algorithms for Engineers

Introduction III. Graphs. Motivations I. Introduction IV

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

Theory of Computing. Lecture 10 MAS 714 Hartmut Klauck

Lecture 9 Graph Traversal

Precept 4: Traveling Salesman Problem, Hierarchical Clustering. Qian Zhu 2/23/2011

Theorem 2.9: nearest addition algorithm

CS 310 Advanced Data Structures and Algorithms

r=1 The Binomial Theorem. 4 MA095/98G Revision

COMP 251 Winter 2017 Online quizzes with answers

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

Graph Algorithms (part 3 of CSC 282),

CS4800: Algorithms & Data Jonathan Ullman

Undirected Graphs. Hwansoo Han

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

Artificial Intelligence

Minimum Spanning Trees

15 211: RECITATION7, SECTION M. Hui Han Chin,

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

Pred 8 1. Dist. Pred

Graph. Vertex. edge. Directed Graph. Undirected Graph

Simple graph Complete graph K 7. Non- connected graph

Module 2: NETWORKS AND DECISION MATHEMATICS

Chapter 14. Graphs Pearson Addison-Wesley. All rights reserved 14 A-1

Module 6 NP-Complete Problems and Heuristics

Module 6 P, NP, NP-Complete Problems and Approximation Algorithms

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

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

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

Konigsberg Bridge Problem

Outline. Introduction. Representations of Graphs Graph Traversals. Applications. Definitions and Basic Terminologies

Algorithm Analysis Graph algorithm. Chung-Ang University, Jaesung Lee

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

Lecture 11: Maximum flow and minimum cut

Further Mathematics 2016 Module 2: NETWORKS AND DECISION MATHEMATICS Chapter 9 Undirected Graphs and Networks

CSC Design and Analysis of Algorithms. Lecture 4 Brute Force, Exhaustive Search, Graph Traversal Algorithms. Brute-Force Approach

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

CS1 Lecture 31 Apr. 3, 2019

Math/Stat 2300 Modeling using Graph Theory (March 23/25) from text A First Course in Mathematical Modeling, Giordano, Fox, Horton, Weir, 2009.

Homework Assignment #3 Graph

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

Algorithm Design (8) Graph Algorithms 1/2

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?

Lecture 10. Elementary Graph Algorithm Minimum Spanning Trees

SEVENTH EDITION and EXPANDED SEVENTH EDITION

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

Graph Theory. ICT Theory Excerpt from various sources by Robert Pergl

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

Chapter 5 Graph Algorithms Algorithm Theory WS 2012/13 Fabian Kuhn

Introduction to Approximation Algorithms

Design and Analysis of Algorithms

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

Unit 2: Algorithmic Graph Theory

Eulerian Cycle (2A) Walk : vertices may repeat, edges may repeat (closed or open) Trail: vertices may repeat, edges cannot repeat (open)

Lecture Notes for IEOR 266: Graph Algorithms and Network Flows

How can we lay cable at minimum cost to make every telephone reachable from every other? What is the fastest route between two given cities?

Computational Methods in IS Research Fall Graph Algorithms Network Flow Problems

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

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

Graph and Digraph Glossary

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

Varying Applications (examples)

Introduction to Graph Theory

1. a graph G = (V (G), E(G)) consists of a set V (G) of vertices, and a set E(G) of edges (edges are pairs of elements of V (G))

COMP 355 Advanced Algorithms

Topic 10 Part 2 [474 marks]

PSD1A. DESIGN AND ANALYSIS OF ALGORITHMS Unit : I-V

Introduction to Computer Science

Transcription:

Lecture 10 Graph Algorithms Euiseong Seo (euiseong@skku.edu) 1

Graph Theory Study of the properties of graph structures It provides us with a language with which to talk about graphs Keys to solving problems Identifying the fundamental graph theoretic notion underlying the situation Using classical algorithms to solve the resulting problem 2

Degrees Number of edgesconnected to a vertex For undirected graphs Sum of all degrees = 2 * edges For directed graph Sum of in-degree = sum of out-degree 3

Connectivity A graph is connected if there is an undirected path between every pair of vertices Existence of a spanning tree -> connectivity BFS or DFS connected component algorithms to find connected components Articulation vertex A single vertex whose deletion disconnects the graph Biconnected graphs Any graphs without an articulation vertex Bridge A single edge whose deletion disconnects the graph 4

Connectivity Testing for articulation vertices or bridges Brute force Be sure to add that vertex/edge back before doing the next deletion! Strongly connected components For directed graphs only Partitions the graph into chunks such that there are directed paths between all pairs of vertices within a given chunk Road networks should be strongly connected 5

Cycles in Graphs All non-tree connected graphs contain cycles Eulerian cycle A tour which visits every edge of the graph exactly once Condition for a undirected graph to have an Eulerian cycle? Find an Eulerian cycle by building it once cycle at a time Finding a back edge using DFS Deleting the edges on this cycle leaves each vertex with even degree 6

Cycles in Graphs Hamiltonian cycles A tour which visits every vertex of the graph exactly once Traveling salesman problem = the shortest Hamiltonian cycle on a weighted graph No efficient algorithm to find one Backtracking! 7

Spanning Tree Given a graph G = (V, E) and tree T = (V, E ) E E for all (u, v) in E, u, v V for all connected graph, there exists a spanning tree 2 4 8 1 2 2 4 3 8 s 5 7 0 s 5 1 7 3 3 6 9 3 6 9 1 2 3 A spanning tree can be constructed using DFS or BFS 8

Minimum Spanning Tree A spanning tree whose sum of edge weights is minimal Importance of MSTs When to need connect a set of points by the smallest amount of roadway, wire or pipe Prim s and Kruskal s algorithms 9

Example of MST: Prim s Algorithm 10

1. Vertex D has been chosen as a starting point 1 Vertices A, B, E, F are connected to D through a single edge. 2 A is the nearest to D and thus chosen as the 2 nd vertex along with the edge AD 11

2. The next vertex chosen is the vertex nearest to either D or A. So the vertex F is chosen along with the edge DF 12

3. same as 2, Vertex B is chosen. 13

4. among C, E, G, E is chosen. 14

5. among C, G, C is chosen. 15

6. G is the only remaining vertex. E is chosen. 16

7. The finally obtained minimum spanning tree the total weight is 39 17

Applications of Prim s Maximum spanning trees Minimum product spanning trees Minimum bottleneck spanning tree 18

Weighted Graph Data Type typedef struct { int v; /* neighboring vertex */ int weight; /* edge weight */ } edge; typedef struct { edge edges[maxv+1][maxdegree]; /* adjacency info */ int degree[maxv+1]; /* outdegree of vertex */ int nvertices; /* number of vertices */ int nedges; /* number of edges in graph */ } graph; 19

Prim s Algorithm prim(graph *g, int start) { int i,j; /* counters */ bool intree[maxv]; /* is vertex in the tree yet? */ int distance[maxv]; /* vertex distance from start */ int v; /* current vertex to process */ int w; /* candidate next vertex */ int weight; /* edge weight */ int dist; /* shortest current distance */ for (i=1; i<=g->nvertices; i++) { intree[i] = FALSE; distance[i] = MAXINT; parent[i] = -1; } distance[start] = 0; v = start; while (intree[v] == FALSE) { intree[v] = TRUE; for (i=0; i<g->degree[v]; i++) { w = g->edges[v][i].v; weight = g->edges[v][i].weight; if ((distance[w] > weight) && (intree[w]==false)) { distance[w] = weight; parent[w] = v; } } } v = 1; dist = MAXINT; for (i=2; i<=g->nvertices; i++) if ((intree[i]==false) && (dist > distance[i])) { dist = distance[i]; v = i; } } 20

Shortest Path For unweighted graphs BFS suffices For weighted graphs Dijkstra s algorithm Dijkstra s algorithm is very similar to Prim s 21

Dijkstra s Algorithm s 9 14 2 6 24 18 2 6 3 15 5 20 30 5 11 16 4 6 19 7 44 t 22

Dijkstra s Algorithm dijkstra(graph *g, int start) /* WAS prim(g,start) */ { int i,j; /* counters */ bool intree[maxv]; /* is vertex in the tree yet? */ int distance[maxv]; /* vertex distance from start */ int v; /* current vertex to process */ int w; /* candidate next vertex */ int weight; /* edge weight */ int dist; /* shortest current distance */ for (i=1; i<=g->nvertices; i++) { intree[i] = FALSE; distance[i] = MAXINT; parent[i] = -1; } distance[start] = 0; v = start; while (intree[v] == FALSE) { intree[v] = TRUE; for (i=0; i<g->degree[v]; i++) { w = g->edges[v][i].v; weight = g->edges[v][i].weight; /* CHANGED */ if (distance[w] > (distance[v]+weight)) { /* CHANGED */ distance[w] = distance[v]+weight; parent[w] = v; } } v = 1; dist = MAXINT; for (i=2; i<=g->nvertices; i++) if ((intree[i]==false) && (dist > distance[i])) { dist = distance[i]; v = i; } } } 23

All Pairs Shortest Path When you want to know the center vertex for a new business Dijksta s? We need another one Floyd s all pairs shortest path algorithm 24

All Pairs Shortest Path Floyd s algorithm W(i,j,k) = min(w(i,j,k-1), W(i,k,k-1)+W(k,j,k-1)) 25

Transitive Closure Sometimes we are interested in which vertices are reachable from a given node The Blackmail problem 26

Network Flow Any edge-weighted graph can be thought of as a network of pipes Weight of edge (i, j) measures the capacity of the pipe Network flow problem Given source and sink vertices of a graph The maximum amount of flow which can be sent from source to sink while respecting the maximum capacities of each pipe 27

Network Flow Flow network (flow/capacity) Residual network for the above flow network 28

Ford-Fulkerson Algorithm 29

Bipartite Graphs A graph G is bipartite or two-colorable if the vertices can be divided into two sets A matching in a graph G = (V, E) is a subset of edges E E such that no two edges in E share a vertex Job-worker pairs Marriage graphs The largest possible bipartite matching can be found using network flow 30

Problem: Fire Station A city is served by a number of fire stations. Residents have complained that the distance between certain houses and the nearest station is too far, so a new station is to be built. You are to choose the location of the new station so as to reduce the distance to the nearest station from the houses of the poorest-served residents. The city has up to 500 intersections, connected by road segments of various lengths. No more than 20 road segments intersect at a given intersection. The locations of houses and fire stations alike are considered to be at intersections. Furthermore, we assume that there is at least one house associated with every intersection. There may be more than one fire station per intersection. 31

Problem: Fire Station Input The input begins with a single line indicating the number of test cases, followed by a blank line. There will also be a blank line between each two consecutive inputs. The first line of input contains two positive integers: the number of existing fire stations f (f 100) and the number of intersections i (i 500). Intersections are numbered from 1 to i consecutively. Then f lines follow, each containing the intersection number at which an existing fire station is found. A number of lines follow, each containing three positive integers: the number of an intersection, the number of a different intersection, and the length of the road segment connecting the intersections. All road segments are two-way (at least as far as fire engines are concerned), and there will exist a route between any pair of intersections. Output For each test case, output the lowest intersection number at which a new fire station can be built so as to minimize the maximum distance from any intersection to its nearest fire station. Separate the output of each two consecutive cases by a blank line. 32

Problem: Fire Station Sample Input 1 Sample Output 5 1 6 2 1 2 10 2 3 10 3 4 10 4 5 10 5 6 10 6 1 10 33