Graph Applications. Topological Sort Shortest Path Problems Spanning Trees. Data Structures 1 Graph Applications

Similar documents
Graphs. Data Structures 1 Graphs

ECE4050 Data Structures and Algorithms. Lecture 8: Graphs

11.1 Terminology and Representations

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

Graph Traversals. CS200 - Graphs 1

The ADT priority queue Orders its items by a priority value The first item removed is the one having the highest priority value

// Source code example for "A Practical Introduction to Data Structures and // Algorithm Analysis" by Clifford A. Shaffer, Prentice Hall, 1998.

The Shortest Path Problem

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

Graph Algorithms. Chapter 22. CPTR 430 Algorithms Graph Algorithms 1

Spanning Trees, greedy algorithms. Lecture 22 CS2110 Fall 2017

CSE 100 Minimum Spanning Trees Prim s and Kruskal

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

Data Structure Lecture#24: Graphs 2 (Chapter 11) U Kang Seoul National University

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

CS 310 Advanced Data Structures and Algorithms

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

Homework Assignment #3 Graph

(Dijkstra s Algorithm) Consider the following positively weighted undirected graph for the problems below: 8 7 b c d h g f

Undirected Graphs. Hwansoo Han

L22-23: Graph Algorithms

Unit 2: Algorithmic Graph Theory

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

Spanning Trees. Lecture 22 CS2110 Spring 2017


Pred 8 1. Dist. Pred

CS/COE

Data Structures Brett Bernstein

Minimum Spanning Trees

CSci 231 Final Review

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

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

Week 5. 1 Analysing BFS. 2 Depth-first search. 3 Analysing DFS. 4 Dags and topological sorting. 5 Detecting cycles. CS 270 Algorithms.

CS61BL. Lecture 5: Graphs Sorting

CSE 373 NOVEMBER 20 TH TOPOLOGICAL SORT

CS 270 Algorithms. Oliver Kullmann. Breadth-first search. Analysing BFS. Depth-first. search. Analysing DFS. Dags and topological sorting.

Week 12: Minimum Spanning trees and Shortest Paths

Shortest path problems

Graphs & Digraphs Tuesday, November 06, 2007

CS 270 Algorithms. Oliver Kullmann. Analysing BFS. Depth-first search. Analysing DFS. Dags and topological sorting.

Chapter 9. Priority Queue

Outline. Graphs. Divide and Conquer.

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

Unweighted Graphs & Algorithms

Week 11: Minimum Spanning trees

23.2 Minimum Spanning Trees

CSE 100: GRAPH ALGORITHMS

Minimum Spanning Trees Ch 23 Traversing graphs

CS Algorithms and Complexity

Graph. Vertex. edge. Directed Graph. Undirected Graph

tree follows. Game Trees

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?

Algorithms and Data Structures 2014 Exercises and Solutions Week 9

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

Elementary Graph Algorithms: Summary. Algorithms. CmSc250 Intro to Algorithms

ROOT: A node which doesn't have a parent. In the above tree. The Root is A.

COMP 251 Winter 2017 Online quizzes with answers

Minimum Spanning Trees

R13. II B. Tech I Semester Supplementary Examinations, May/June DATA STRUCTURES (Com. to ECE, CSE, EIE, IT, ECC)

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

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

SELF-BALANCING SEARCH TREES. Chapter 11

Minimum Spanning Trees My T. UF

Introduction to Parallel & Distributed Computing Parallel Graph Algorithms

Info 2950, Lecture 16

Directed Graphs (II) Hwansoo Han

Graphs. Computer Science E-119 Harvard Extension School Fall 2012 David G. Sullivan, Ph.D. What is a Graph? b d f h j

CSE 100: GRAPH ALGORITHMS

Total Score /1 /20 /41 /15 /23 Grader

Konigsberg Bridge Problem

Graph Algorithms. Andreas Klappenecker. [based on slides by Prof. Welch]

Lecture 9 Graph Traversal

Basic Graph Definitions

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

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

Lecture 4: Graph Algorithms

CS 561, Lecture 10. Jared Saia University of New Mexico

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

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

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

Lecture 10. Elementary Graph Algorithm Minimum Spanning Trees

CS 161 Lecture 11 BFS, Dijkstra s algorithm Jessica Su (some parts copied from CLRS) 1 Review

INDIAN STATISTICAL INSTITUTE

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

CSC 8301 Design & Analysis of Algorithms: Kruskal s and Dijkstra s Algorithms

Graph Representations and Traversal

GRAPHS Lecture 17 CS2110 Spring 2014

Graph Algorithms. Definition

CMPSC 250 Analysis of Algorithms Spring 2018 Dr. Aravind Mohan Shortest Paths April 16, 2018

CS200: Graphs. Rosen Ch , 9.6, Walls and Mirrors Ch. 14

CS 3410 Ch 14 Graphs and Paths

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

CSE373: Data Structures & Algorithms Lecture 28: Final review and class wrap-up. Nicki Dell Spring 2014

Data Structure Lecture#23: Graphs (Chapter 11) U Kang Seoul National University

Graph Traversal CSCI Algorithms I. Andrew Rosenberg

MST worksheet By Jim Xu

CS 206 Introduction to Computer Science II

Outlines: Graphs Part-2

CSC 172 Data Structures and Algorithms. Lecture 24 Fall 2017

Transcription:

Graph Applications Topological Sort Shortest Path Problems Spanning Trees Data Structures 1 Graph Applications

Application: Topological Sort Given a set of jobs, courses, etc. with prerequisite constraints, output the jobs in an order that does not violate any of the prerequisites. J4 J8 J1 J2 J7 J9 J3 J5 Model the problem as a DAG - relationships are directed, acyclic since cycles will cause conflicting set of prereqs. Topological sort can be performed using a DFS traversal (reverse order is generated), or by employing a queue. Order Generated Using DFS: J8, J4, J9, J7, J2, J5, J3, J1 Order Generated Using Queue:J1, J2, J3, J4, J5, J7, J8, J9 Data Structures 2 Graph Applications

Topological Sort(Using a Queue) void t o p s o r t ( Graph G) { Queue<Integer > Q = new AQueue<Integer >(G. n ( ) ) ; i n t [ ] Count = new i n t [G. n ( ) ] ; i n t v ; for ( v =0; v<g. n ( ) ; v++) Count [ v ] = 0; / / I n i t i a l i z e for ( v =0; v<g. n ( ) ; v++) / / Process every edge for ( i n t w = G. f i r s t ( v ) ; w < G. n ( ) ; w = G. next ( v, w) ) Count [w] + + ; / / Add to v2 s prereq count for ( v =0; v<g. n ( ) ; v++) / / I n i t i a l i z e Queue i f ( Count [ v ] == 0) / / V has no p r e r e q u i s i t e s Q. enqueue ( v ) ; while (Q. l ength ( ) > 0) { / / Process the v e r t i c e s v = Q. dequeue ( ). i n t V a l u e ( ) ; p r i n t o u t ( v ) ; / / P r e V i s i t f o r Vertex V for ( i n t w = G. f i r s t ( v ) ; w < G. n ( ) ; w = G. next ( v, w) ) { Count [w] ; / / One less p r e r e q u i s i t e i f ( Count [w] == 0) / / This vertex i s now f r e e Q. enqueue (w ) ; Data Structures 3 Graph Applications

Topological Sort(Using Recursion) void t o p s o r t ( Graph G) { / / Topo s o r t : r e c u r s i v e for ( i n t i =0; i <G. n ( ) ; i ++) / / I n i t i a l i z e Mark array G. setmark ( i, UNVISITED ) ; for ( i n t i =0; i <G. n ( ) ; i ++) / / Process a l l v e r t i c e s i f (G. getmark ( i ) == UNVISITED ) tophelp (G, i ) ; / / C a l l helper f u n c t i o n void tophelp ( Graph G, i n t v ) { / / Topsort helper G. setmark ( v, VISITED ) ; for ( Edge w = G. f i r s t ( v ) ; G. isedge (w ) ; w = G. next (w) ) i f (G. getmark (G. v2 (w) ) == UNVISITED ) tophelp (G, G. v2 (w ) ) ; p r i n t o u t ( v ) ; / / P o s t V i s i t f o r Vertex v Data Structures 4 Graph Applications

Shortest Paths Problems 10 B 20 5 D A 2 11 3 C 15 E Numerous applications where weighted graphs are used for optimization; shortest path problems are an instance. Example: road maps (cities and distances) Problems are non-trivial since the shortest path can be via a long path of edges, rather than the straight edge from source to destination. Data Structures 5 Graph Applications

Single-Source Shortest Paths Given a vertex s in graph G, find the shortest path from s to every other vertex in G B 20 10 5 D A 2 11 3 C 15 E Closely related problem is to find the shortest (minimum cost) path between two nodes of a graph. This problem is no more complicated than the algorithm to find all of the shortest (reachable) paths from a single source. Application-Networking: Find least expensive routes to broadcast messages to other computers in the network. Unweighted graphs - can use BFS; for weighted graphs, this will Data Structures 6 Graph Applications not work.

Dijkstra s Shortest Paths Algorithm Goal: Given start vertex s, find the shortest path from s to all other vertices Visit vertices in some order, compute shortest paths for all vertices seen(visited) so far, then add shortest path to the next vertex x. Problem: Shortest path to a vertex already processed might go through x. Solution: Process vertices in order of distance from s. Data Structures 7 Graph Applications

Dijkstra s Algorithm: Example 10 B 20 5 D A 2 11 3 C 15 E A B C D E Initial 0 Process A 0 10 3 20 Process C 0 5 3 20 18 Process B 0 5 3 10 18 Process D 0 5 3 10 18 Process E 0 5 3 10 18 Data Structures 8 Graph Applications

Dijkstra s Algorithm: Implementation / / Compute s h o r t e s t path distances from s, s t o r e i n D s t a t i c void D i j k s t r a ( Graph G, i n t s, i n t [ ] D) { for ( i n t i =0; i <G. n ( ) ; i ++) / / I n i t i a l i z e D[ i ] = I n t e g e r.max VALUE; D[ s ] = 0; for ( i n t i =0; i <G. n ( ) ; i ++) { / / Process v e r t i c e s i n t v = minvertex (G, D ) ; / / Get next c l o s e s t v ertex G. setmark ( v, VISITED ) ; i f (D[ v ] == I n t e g e r.max VALUE) return ; for (w = G. f i r s t ( v ) ; w < G. n ( ) ; w = G. next (w) ) i f (D[w] > (D[ v ] + G. weight ( v, w ) ) ) D[w] = D[ v ] + G. weight ( v, w ) ; s t a t i c i n t minvertex ( Graph G, i n t [ ] D) { i n t v = 0; / / I n i t i a l i z e v to any u n v i s i t e d vertex ; for ( i n t i =0; i <G. n ( ) ; i ++) i f (G. getmark ( i ) == UNVISITED ) { v = i ; break ; for ( i n t i =0; i <G. n ( ) ; i ++) / / Find s m a l l e s t value i f ( (G. getmark ( i ) == UNVISITED ) && (D[ i ] < D[ v ] ) ) v = i ; return v ; Data Structures 9 Graph Applications

Dijkstra s Algorithm: Analysis minvertex() costs V, and this is done ( V + E ) times (Dijkstra()), resulting in a complexity Θ( V 2 + E ) minvertex() can be improved using a min-heap, ordered by D values. Removing a minimum value, can be done in Θ(log V ) time (need to reheap after root element is removed). Modifications to D values, can delete and reinsert the new values - can be done in Θ(log V ), as well. Using a min-heap, the complexity becomes Θ( V + E )log E. Data Structures 10 Graph Applications

Minimum-Cost Spanning Trees A Minimum-Cost-Spanning Tree (MST) of Graph G is the graph containing the vertices of G and subset of its weighted edges such that the edges have minimum cost (sum of weights of all edges) vertices are connected. Application Soldering the shortest set of edges to connect a set of terminals Connecting cities for a telephone network to minimize cable length. Data Structures 11 Graph Applications

Minimum Spanning Tree: Prim s Algorithm B 5 D A 10 2 20 11 3 C 15 E Pick any vertex N to be the start vertex. Pick the least cost outgoing edge from N; if this edge is connected vertex M, add M and edge NM to the MST. Pick the least outgoing edge from the MST (N or M) and the edge vertex is not visited, add to MST, and so on. Data Structures 12 Graph Applications

Minimum Spanning Tree: Prim s Algorithm / Compute a minimal cost spanning t r e e / s t a t i c void Prim ( Graph G, i n t s, i n t [ ] D, i n t [ ] V) { for ( i n t i =0; i <G. n ( ) ; i ++) / / I n i t i a l i z e D[ i ] = I n t e g e r.max VALUE; D[ s ] = 0; for ( i n t i =0; i <G. n ( ) ; i ++) { / / Process the v e r t i c e s i n t v = minvertex (G, D ) ; G. setmark ( v, VISITED ) ; i f ( v!= s ) AddEdgetoMST (V [ v ], v ) ; i f (D[ v ] == I n t e g e r.max VALUE) return ; / / Unreachable for ( i n t w = G. f i r s t ( v ) ; w < G. n ( ) ; w = G. next ( v, w) ) i f (D[w] > G. weight ( v, w) ) { D[w] = G. weight ( v, w ) ; V [w] = v ; Data Structures 13 Graph Applications

Minimum Spanning Tree: Prim s Algorithm: Example Can use a priority queue to pick the minimum weight edge at eacn iteration. Data Structures 14 Graph Applications