Lecture20: Graph IV Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)

Slides:



Advertisements
Similar presentations
CS16: Introduction to Data Structures & Algorithms
Advertisements

IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
Spring 2007Shortest Paths1 Minimum Spanning Trees JFK BOS MIA ORD LAX DFW SFO BWI PVD
Shortest Paths and Dijkstra's Algorithm CS 110: Data Structures and Algorithms First Semester,
1 prepared from lecture material © 2004 Goodrich & Tamassia COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material.
Shortest Paths1 C B A E D F
CSC 213 Lecture 23: Shortest Path Algorithms. Weighted Graphs Each edge in weighted graph has numerical weight Weights can be distances, building costs,
1 Graphs ORD DFW SFO LAX Many slides taken from Goodrich, Tamassia 2004.
1 Directed Graphs CSC401 – Analysis of Algorithms Lecture Notes 15 Directed Graphs Objectives: Introduce directed graphs and weighted graphs Present algorithms.
Directed Graphs1 JFK BOS MIA ORD LAX DFW SFO. Directed Graphs2 Outline and Reading (§6.4) Reachability (§6.4.1) Directed DFS Strong connectivity Transitive.
Minimum Spanning Trees1 JFK BOS MIA ORD LAX DFW SFO BWI PVD
© 2004 Goodrich, Tamassia Shortest Paths1 Shortest Paths (§ 13.6) Given a weighted graph and two vertices u and v, we want to find a path of minimum total.
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
Minimum Spanning Trees1 JFK BOS MIA ORD LAX DFW SFO BWI PVD
CSC311: Data Structures 1 Chapter 13: Graphs II Objectives: Graph ADT: Operations Graph Implementation: Data structures Graph Traversals: DFS and BFS Directed.
Shortest Paths1 C B A E D F
© 2004 Goodrich, Tamassia Shortest Paths1 C B A E D F
CSC 213 Lecture 24: Minimum Spanning Trees. Announcements Final exam is: Thurs. 5/11 from 8:30-10:30AM in Old Main 205.
Graphs – Shortest Path (Weighted Graph) ORD DFW SFO LAX
Weighted Graphs In a weighted graph, each edge has an associated numerical value, called the weight of the edge Edge weights may represent, distances,
Minimum Spanning Trees
© 2010 Goodrich, Tamassia Shortest Paths1 C B A E D F
1 Shortest Path Problem Topic 11 ITS033 – Programming & Algorithms C B A E D F Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program,
Shortest Paths C B A E D F
Graphs Part 2. Shortest Paths C B A E D F
Lecture17: Graph I Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
CSC 213 – Large Scale Programming. Today’s Goals  Discuss what is meant by weighted graphs  Where weights placed within Graph  How to use Graph ’s.
Graphs. Data Structure for Graphs. Graph Traversals. Directed Graphs. Shortest Paths. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013.
Minimum-Cost Spanning Tree CS 110: Data Structures and Algorithms First Semester,
Shortest Paths 1 Chapter 7 Shortest Paths C B A E D F
Lecture 16. Shortest Path Algorithms
Shortest Paths and Dijkstra’s Algorithm CS 105. SSSP Slide 2 Single-source shortest paths Given a weighted graph G and a source vertex v in G, determine.
1 Dijkstra’s Algorithm Dr. Ying Lu RAIK 283 Data Structures & Algorithms.
Lecture19: Graph III Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
CSC 213 – Large Scale Programming. Today’s Goals  Discuss what is meant by weighted graphs  Where weights placed within Graph  How to use Graph ’s.
Paths in a Graph : A Brief Tutorial Krishna.V.Palem Kenneth and Audrey Kennedy Professor of Computing Department of Computer Science, Rice University 1.
© 2010 Goodrich, Tamassia Minimum Spanning Trees1 JFK BOS MIA ORD LAX DFW SFO BWI PVD
1 Prim’s algorithm. 2 Minimum Spanning Tree Given a weighted undirected graph G, find a tree T that spans all the vertices of G and minimizes the sum.
1 Weighted Graphs CSC401 – Analysis of Algorithms Lecture Notes 16 Weighted Graphs Objectives: Introduce weighted graphs Present shortest path problems,
Data Structures and Algorithms1 Data Structures and algorithms (IS ZC361) Weighted Graphs – Shortest path algorithms – MST S.P.Vimal BITS-Pilani
CHAPTER 13 GRAPH ALGORITHMS ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA.
© 2010 Goodrich, Tamassia Shortest Paths1 C B A E D F
© 2010 Goodrich, Tamassia Minimum Spanning Trees1 JFK BOS MIA ORD LAX DFW SFO BWI PVD
1 COMP9024: Data Structures and Algorithms Week Twelve: Graphs (II) Hui Wu Session 1, 2014
Graphs 10/24/2017 6:47 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
Directed Graphs Directed Graphs Shortest Path 12/7/2017 7:10 AM BOS
Shortest Paths C B A E D F Shortest Paths
Graphs Part 2.
COMP9024: Data Structures and Algorithms
Shortest Paths C B A E D F Shortest Paths
Shortest Path 6/18/2018 4:22 PM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Shortest Paths C B A E D F Shortest Paths 1
Shortest Paths C B A E D F Shortest Paths
Shortest Paths C B A E D F Shortest Paths
Directed Graphs Directed Graphs 1 Shortest Path Shortest Path
Shortest Paths C B A E D F
Shortest Paths C B A E D F Shortest Paths
Shortest Paths C B A E D F Shortest Paths
Minimum Spanning Trees
Chapter 13 Graph Algorithms
Graphs Part 2.
Graph Algorithms shortest paths, minimum spanning trees, etc.
Minimum Spanning Trees
Single-source shortest paths
Copyright © Aiman Hanna All rights reserved
Shortest Paths.
Minimum Spanning Trees
Weighted Graphs C B A E D F Sequences
CSE 417: Algorithms and Computational Complexity
Presentation transcript:

Lecture20: Graph IV Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Weighted Graph Properties  Each edge has an associated numerical value, weight of the edge  Edge weights may represent, distances, costs, etc. Example:  In a flight route graph, the weight of an edge represents the distance in miles between the endpoint airports 2 ORD PVD MIA DFW SFO LAX LGA HNL

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Shortest Paths What is the shortest path?  A path of minimum total weight between two vertices  Length of a path is the sum of the weights of its edges. Applications  Internet packet routing  Flight reservations  Driving directions 3 ORD PVD MIA DFW SFO LAX LGA HNL

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Shortest Paths Properties  A subpath of a shortest path is itself a shortest path  There is a tree of shortest paths from a start vertex to all the other vertices Example:  Tree of shortest paths from Providence 4 ORD PVD MIA DFW SFO LAX LGA HNL

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Dijkstra’s Algorithm Problem definition  Find the shortest path from a starting vertex to all other vertices. Assumptions:  The graph is connected.  The edges are undirected.  The edge weights are nonnegative. Methodology  We grow a “cloud” of vertices, beginning with a starting vertex and eventually covering all the vertices.  Solve for vertices close to starting vertex: Neighbors are easy to determine.  Add an edge one by one Find the path to each vertex one by one Iteratively expand the set of nodes where the shortest path is known. 5

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Dijkstra’s Algorithm 6

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Edge Relaxation 7 10

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 C B A E D F Example 8 CB A E D F C B A E D F C B A E D F

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Example 9 CB A E D F CB A E D F

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Dijkstra’s Algorithm Heap-based priority queue  Stores the vertices outside the cloud  Key: distance  Value: vertex  replaceKey(l,k) : changes the key of entry l We store two labels with each vertex:  Distance  Entry in priority queue 10 Algorithm DijkstraDistances(G, s) Q  new heap-based priority queue for all v  G.vertices() if v = s v.setDistance(0) else v.setDistance(  ) l  Q.insert(v.getDistance(), v) v.setEntry(l) while  Q.empty() l  Q.removeMin() u  l.getValue() for all e  u.incidentEdges() // relax e z  e.opposite(u) r  u.getDistance() + e.weight() if r < z.getDistance() z.setDistance(r) Q.replaceKey(z.getEntry(), r)

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Analysis of Dijkstra’s Algorithm 11

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Shortest Paths Tree Extension of Dijkstra’s algorithm  Return a tree of shortest paths from the start vertex to all other vertices  Store a third label: parent edge in the shortest path tree  In the edge relaxation step, we update the parent label. 12 Algorithm DijkstraShortestPathsTree(G, s) … for all v  G.vertices() … v.setParent(  ) … for all e  u.incidentEdges() // relax edge e z  e.opposite(u) r  u.getDistance() + e.weight() if r < z.getDistance() z.setDistance(r) z.setParent(e) Q.replaceKey(z.getEntry(),r)

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Why Dijkstra’s Algorithm Work 13 CB A E D F

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Why It Doesn’t Work for Negative-Weight Edges Greedy algorithm: It adds vertices by increasing distance.  If a node with a negative incident edge were to be added late to the cloud, it could mess up distances for vertices already in the cloud. 14 CB A E D F C’s true distance is 1, but it is already in the cloud with d(C)=5!

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Bellman-Ford Algorithm 15 Algorithm BellmanFord(G, s) for all v  G.vertices() if v = s v.setDistance(0) else v.setDistance(  ) for i  1 to n - 1 do for each e  G.edges() // relax edge e u  e.origin() z  e.opposite(u) r  u.getDistance() + e.weight() if r < z.getDistance() z.setDistance(r)

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Bellman-Ford Example  0     4  

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Bellman-Ford Example

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Directed Acyclic Graph (DAG) 18

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 DAG Applications 19 Procedure of a particular task Hasse diagram

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Topological Sort 20 Algorithm TopologicalSort(G) H  G // Temporary copy of G n  G.numVertices() while H is not empty do Let v be a vertex with no outgoing edges Label v  n n  n - 1 Remove v from H

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 An Example of Topological Sort 21

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 An Example of Topological Sort 22 9

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 An Example of Topological Sort

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 An Example of Topological Sort

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 An Example of Topological Sort

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 An Example of Topological Sort

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 An Example of Topological Sort

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 An Example of Topological Sort

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 An Example of Topological Sort

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 An Example of Topological Sort

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 DAG-based Shortest Path Algorithm 31 Algorithm DagDistances(G, s) for all v  G.vertices() if v = s v.setDistance(0) else v.setDistance(  ) // Perform a topological sort of the vertices for u  1 to n do // in topological order for each e  u.outEdges() // relax edge e z  e.opposite(u) r  u.getDistance() + e.weight() if r < z.getDistance() z.setDistance(r) Why is this faster?

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 DAG Example 32  0     4 

33