Directed Graph Algorithms CSE 373 Data Structures Lecture 14.

Slides:



Advertisements
Similar presentations
CSE 390B: Graph Algorithms Based on CSE 373 slides by Jessica Miller, Ruth Anderson 1.
Advertisements

Part A - Terminology and Traversals
Topological Sort and Hashing
Graph Algorithms: Topological Sort The topological sorting problem: given a directed, acyclic graph G = (V, E), find a linear ordering of the vertices.
§2 Topological Sort 〖 Example 〗 Courses needed for a computer science degree at a hypothetical university How shall we convert this list into a graph?
CSE 373 Graphs 1: Concepts, Depth/Breadth-First Search
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
CSE 101- Winter ‘15 Discussion Section January 26th 2015.
Graph Searching CSE 373 Data Structures Lecture 20.
Chapter 8, Part I Graph Algorithms.
1 Graphs: Traversal Searching/Traversing a graph = visiting the vertices of a graph by following the edges in a systematic way Example: Given a highway.
Graphs COL 106 Slide Courtesy : Douglas W. Harder, U Waterloo.
1 CSE 326: Data Structures: Graphs Lecture 19: Monday, Feb 24, 2003.
CSE332: Data Abstractions Lecture 16: Topological Sort / Graph Traversals Tyler Robison Summer
CSE332: Data Abstractions Lecture 16: Topological Sort / Graph Traversals Dan Grossman Spring 2010.
Graph & BFS.
CS 311 Graph Algorithms. Definitions A Graph G = (V, E) where V is a set of vertices and E is a set of edges, An edge is a pair (u,v) where u,v  V. If.
CSE 326: Data Structures Lecture #19 Graphs I Alon Halevy Spring Quarter 2001.
CSE 326 Graphs David Kaplan Dept of Computer Science & Engineering Autumn 2001.
Chapter 9: Graphs Summary Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
CSE 326: Data Structures Graphs Ben Lerner Summer 2007.
Graph & BFS Lecture 22 COMP171 Fall Graph & BFS / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D.
Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs.
CSE 373: Data Structures and Algorithms Lecture 18: Graphs II 1.
CSE 780 Algorithms Advanced Algorithms Graph Algorithms Representations BFS.
CSE 326: Data Structures Lecture #17 Trees and DAGs and Graphs, Oh MY! Bart Niswonger Summer Quarter 2001.
More Graph Algorithms Weiss ch Exercise: MST idea from yesterday Alternative minimum spanning tree algorithm idea Idea: Look at smallest edge not.
Review of Graphs A graph is composed of edges E and vertices V that link the nodes together. A graph G is often denoted G=(V,E) where V is the set of vertices.
1 Graphs Algorithms Sections 9.1, 9.2, and Graphs v1v1 v2v2 v5v5 v7v7 v8v8 v3v3 v6v6 v4v4 A graph G = (V, E) –V: set of vertices (nodes) –E: set.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
1 Chapter 9 Graph Algorithms Real-life graph problems Algorithms for some graph problems Choice of data structures for graph problems.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
CSE 326: Data Structures First Post-Schism Lecture Lecture #22 SteganoGRAPHy Steve Wolfman Winter Quarter 2000.
CSE 326: Data Structures Lecture #20 Graphs I.5 Alon Halevy Spring Quarter 2001.
CSE332: Data Abstractions Lecture 15: Introduction to Graphs Tyler Robison 2010 Summer 1.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
1 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency.
Topological Sort: Definition
DATA STRUCTURES AND ALGORITHMS Lecture Notes 10 Prepared by İnanç TAHRALI.
Graphs Upon completion you will be able to:
CSE 421 Algorithms Richard Anderson Winter 2009 Lecture 5.
Graph Searching CSIT 402 Data Structures II. 2 Graph Searching Methodology Depth-First Search (DFS) Depth-First Search (DFS) ›Searches down one path as.
CSE 421 Algorithms Richard Anderson Autumn 2015 Lecture 5.
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
CSE373: Data Structures & Algorithms Lecture 14: Topological Sort / Graph Traversals Dan Grossman Fall 2013.
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.
1 CSE 332: Graphs Richard Anderson Spring Announcements This week and next week – Graph Algorithms Reading, Monday and Wednesday, Weiss
CSE 373: Data Structures and Algorithms Lecture 21: Graphs V 1.
Topological Sorting.
Graphs Representation, BFS, DFS
I206: Lecture 15: Graphs Marti Hearst Spring 2012.
More Graph Algorithms.
Graphs Representation, BFS, DFS
Graphs Chapter 11 Objectives Upon completion you will be able to:
CSE 373 Data Structures and Algorithms
Topological Sort CSE 373 Data Structures Lecture 19.
Graphs.
Directed Graph Algorithms
Directed Graph Algorithms
CSE 373 Graphs 4: Topological Sort reading: Weiss Ch. 9
Richard Anderson Autumn 2016 Lecture 5
Graph Algorithms "A charlatan makes obscure what is clear; a thinker makes clear what is obscure. " - Hugh Kingsmill CLRS, Sections 22.2 – 22.4.
CE 221 Data Structures and Algorithms
CSE 373 Data Structures and Algorithms
CSE 417: Algorithms and Computational Complexity
CSE 373: Data Structures and Algorithms
Richard Anderson Winter 2019 Lecture 6
Richard Anderson Winter 2019 Lecture 5
Richard Anderson Autumn 2015 Lecture 6
Presentation transcript:

Directed Graph Algorithms CSE 373 Data Structures Lecture 14

5/9/03Digraphs - Lecture 142 Readings Reading ›Sections 9.2, 9.3 and

5/9/03Digraphs - Lecture 143 Topological Sort Problem: Find an order in which all these courses can be taken. Example: 142  143  378  370  321  341  322  326  421  401 In order to take a course, you must take all of its prerequisites first 142

5/9/03Digraphs - Lecture 144 Given a digraph G = (V, E), find a linear ordering of its vertices such that: for any edge (v, w) in E, v precedes w in the ordering A B C F D E Topological Sort

5/9/03Digraphs - Lecture 145 A B C F D E EADF BC Any linear ordering in which all the arrows go to the right is a valid solution Topo sort - good example Note that F can go anywhere in this list because it is not connected. Also the solution is not unique.

5/9/03Digraphs - Lecture 146 A B C F D E DAEF BC Any linear ordering in which an arrow goes to the left is not a valid solution Topo sort - bad example NO!

5/9/03Digraphs - Lecture 147 Paths and Cycles Given a digraph G = (V,E), a path is a sequence of vertices v 1,v 2, …,v k such that: ›(v i,v i+1 ) in E for 1 < i < k ›path length = number of edges in the path ›path cost = sum of costs of each edge A path is a cycle if : ›k > 1; v 1 = v k G is acyclic if it has no cycles.

5/9/03Digraphs - Lecture 148 Only acyclic graphs can be topo. sorted A directed graph with a cycle cannot be topologically sorted. A B C F D E

5/9/03Digraphs - Lecture 149 Step 1: Identify vertices that have no incoming edges The “in-degree” of these vertices is zero A B C F D E Topo sort algorithm - 1

5/9/03Digraphs - Lecture 1410 Step 1: Identify vertices that have no incoming edges If no such vertices, graph has only cycle(s) (cyclic graph) Topological sort not possible – Halt. A B C D Example of a cyclic graph Topo sort algorithm - 1a

5/9/03Digraphs - Lecture 1411 Step 1: Identify vertices that have no incoming edges Select one such vertex A B C F D E Select Topo sort algorithm - 1b

5/9/03Digraphs - Lecture 1412 A B C F D E Step 2: Delete this vertex of in-degree 0 and all its outgoing edges from the graph. Place it in the output. Topo sort algorithm - 2 A

5/9/03Digraphs - Lecture 1413 A B C F D E Repeat Step 1 and Step 2 until graph is empty Select Continue until done

5/9/03Digraphs - Lecture 1414 A B C F D E B Select B. Copy to sorted list. Delete B and its edges. B

5/9/03Digraphs - Lecture 1415 A C F D E BC Select C. Copy to sorted list. Delete C and its edges. C

5/9/03Digraphs - Lecture 1416 A F D E BC D Select D. Copy to sorted list. Delete D and its edges. D

5/9/03Digraphs - Lecture 1417 A F E BC D EF Select E. Copy to sorted list. Delete E and its edges. Select F. Copy to sorted list. Delete F and its edges. E, F

5/9/03Digraphs - Lecture 1418 ABC D EF Done A B C F D E

5/9/03Digraphs - Lecture 1419 A B C F D E Assume adjacency list representation Implementation ABCDEF Translation array value next

5/9/03Digraphs - Lecture In-Degree array; or add a field to array A Calculate In-degrees AD

5/9/03Digraphs - Lecture 1421 Calculate In-degrees for i = 1 to n do D[i] := 0; endfor for i = 1 to n do x := A[i]; while x  null do D[x.value] := D[x.value] + 1; x := x.next; endwhile endfor

5/9/03Digraphs - Lecture 1422 Key idea: Initialize and maintain a queue (or stack) of vertices with In-Degree 0 1 Queue Maintaining Degree 0 Vertices AD

5/9/03Digraphs - Lecture 1423 After each vertex is output, when updating In-Degree array, enqueue any vertex whose In-Degree becomes zero 1 Queue 6 Output 2 dequeue enqueue Topo Sort using a Queue (breadth-first) AD

5/9/03Digraphs - Lecture 1424 Topological Sort Algorithm 1.Store each vertex’s In-Degree in an array D 2.Initialize queue with all “in-degree=0” vertices 3.While there are vertices remaining in the queue: (a) Dequeue and output a vertex (b) Reduce In-Degree of all vertices adjacent to it by 1 (c) Enqueue any of these vertices whose In-Degree became zero 4.If all vertices are output then success, otherwise there is a cycle.

5/9/03Digraphs - Lecture 1425 Some Detail Main Loop while notEmpty(Q) do x := Dequeue(Q) Output(x) y := A[x]; while y  null do D[y.value] := D[y.value] – 1; if D[y.value] = 0 then Enqueue(Q,y.value); y := y.next; endwhile

5/9/03Digraphs - Lecture 1426 Topological Sort Analysis Initialize In-Degree array: O(|V| + |E|) Initialize Queue with In-Degree 0 vertices: O(|V|) Dequeue and output vertex: ›|V| vertices, each takes only O(1) to dequeue and output: O(|V|) Reduce In-Degree of all vertices adjacent to a vertex and Enqueue any In-Degree 0 vertices: › O(|E|) For input graph G=(V,E) run time = O(|V| + |E|) ›Linear time!

5/9/03Digraphs - Lecture 1427 After each vertex is output, when updating In-Degree array, push any vertex whose In-Degree becomes zero 1 Stack 2 Output 6 pop push Topo Sort using a Stack (depth-first) AD

5/9/03Digraphs - Lecture 1428 Recall Path cost,Path length Path cost: the sum of the costs of each edge Path length: the number of edges in the path ›Path length is the unweighted path cost Seattle San Francisco Dallas Chicago Salt Lake City length(p) = 5 cost(p) = 11

5/9/03Digraphs - Lecture 1429 Shortest Path Problems Given a graph G = (V, E) and a “source” vertex s in V, find the minimum cost paths from s to every vertex in V Many variations: ›unweighted vs. weighted ›cyclic vs. acyclic ›pos. weights only vs. pos. and neg. weights ›etc

5/9/03Digraphs - Lecture 1430 Why study shortest path problems? Traveling on a budget: What is the cheapest airline schedule from Seattle to city X? Optimizing routing of packets on the internet: ›Vertices are routers and edges are network links with different delays. What is the routing path with smallest total delay? Shipping: Find which highways and roads to take to minimize total delay due to traffic etc.

5/9/03Digraphs - Lecture 1431 Unweighted Shortest Path Problem: Given a “source” vertex s in an unweighted directed graph G = (V,E), find the shortest path from s to all vertices in G A C B D FH G E Source Only interested in path lengths

5/9/03Digraphs - Lecture 1432 Breadth-First Search Solution Basic Idea: Starting at node s, find vertices that can be reached using 0, 1, 2, 3, …, N-1 edges (works even for cyclic graphs!) A C B D FH G E

5/9/03Digraphs - Lecture 1433 Breadth-First Search Alg. Uses a queue to track vertices that are “nearby” source vertex is s Distance[s] := 0 Enqueue(Q,s); Mark(s)//After a vertex is marked once // it won’t be enqueued again while queue is not empty do X := Dequeue(Q); for each vertex Y adjacent to X do if Y is unmarked then Distance[Y] := Distance[X] + 1; Previous[Y] := X;//if we want to record paths Enqueue(Q,Y); Mark(Y); Running time = O(|V| + |E|)

5/9/03Digraphs - Lecture 1434 Example: Shortest Path length A C B D FH G E 0 Queue Q = C

5/9/03Digraphs - Lecture 1435 Example (ct’d) A C B D FH G E 0 Queue Q = A D E Previous pointer Indicates the vertex is marked

5/9/03Digraphs - Lecture 1436 Example (ct’d) A C B D FH G E 0 Q = D E B

5/9/03Digraphs - Lecture 1437 Example (ct’d) A C B D FH G E 0 Q = B G

5/9/03Digraphs - Lecture 1438 Example (ct’d) A C B D FH G E 0 Q = F

5/9/03Digraphs - Lecture 1439 Example (ct’d) A C B D FH G E 0 Q = H

5/9/03Digraphs - Lecture 1440 What if edges have weights? Breadth First Search does not work anymore ›minimum cost path may have more edges than minimum length path A C B D FH G E Shortest path (length) from C to A: C  A (cost = 9) Minimum Cost Path = C  E  D  A (cost = 8)

5/9/03Digraphs - Lecture 1441 Dijkstra’s Algorithm for Weighted Shortest Path Classic algorithm for solving shortest path in weighted graphs (without negative weights) A greedy algorithm (irrevocably makes decisions without considering future consequences) Each vertex has a cost for path from initial vertex

5/9/03Digraphs - Lecture 1442 Basic Idea of Dijkstra’s Algorithm Find the vertex with smallest cost that has not been “marked” yet. Mark it and compute the cost of its neighbors. Do this until all vertices are marked. Note that each step of the algorithm we are marking one vertex and we won’t change our decision: hence the term “greedy” algorithm