CS 146: Data Structures and Algorithms July 21 Class Meeting

Slides:



Advertisements
Similar presentations
Lecture 15. Graph Algorithms
Advertisements

Cpt S 223 – Advanced Data Structures Graph Algorithms: Introduction
Chapter 9: Graphs Shortest Paths
Graph Theory Arnold Mesa. Basic Concepts n A graph G = (V,E) is defined by a set of vertices and edges v3 v1 v2Vertex (v1) Edge (e1) A Graph with 3 vertices.
§3 Shortest Path Algorithms Given a digraph G = ( V, E ), and a cost function c( e ) for e  E( G ). The length of a path P from source to destination.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
CSC 2300 Data Structures & Algorithms April 13, 2007 Chapter 9. Graph Algorithms.
§2 Topological Sort 〖 Example 〗 Courses needed for a computer science degree at a hypothetical university How shall we convert this list into a graph?
Graphs Chapter 12. Chapter Objectives  To become familiar with graph terminology and the different types of graphs  To study a Graph ADT and different.
CS 206 Introduction to Computer Science II 11 / 07 / 2008 Instructor: Michael Eckmann.
1 9.3 Shortest Path Algorithms Improvement –Use a queue to keep all vertices whose shortest distance to s is known. –Initialize d v to  for all vertices,
1 Shortest Path Algorithms Given a graph G = (V, E) and a distinguished vertex s, find the shortest weighted path from s to every other vertex in G. weighted.
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.
Chapter 9: Graphs Summary Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College.
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
Shortest Path Algorithm By Weston Vu CS 146. What is Shortest Paths? Shortest Paths is a part of the graph algorithm. It is used to calculate the shortest.
Fall 2007CS 2251 Graphs Chapter 12. Fall 2007CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs To.
CSC 2300 Data Structures & Algorithms April 3, 2007 Chapter 9. Graph Algorithms.
1 Minimum Spanning Trees Longin Jan Latecki Temple University based on slides by David Matuszek, UPenn, Rose Hoberman, CMU, Bing Liu, U. of Illinois, Boting.
Minimum Spanning Trees. Subgraph A graph G is a subgraph of graph H if –The vertices of G are a subset of the vertices of H, and –The edges of G are a.
1 Minimum Spanning Trees Longin Jan Latecki Temple University based on slides by David Matuszek, UPenn, Rose Hoberman, CMU, Bing Liu, U. of Illinois, Boting.
Minimum Spanning Tree in Graph - Week Problem: Laying Telephone Wire Central office.
CS 146: Data Structures and Algorithms July 14 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
CS 46B: Introduction to Data Structures July 30 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Graph Algorithms: Minimum.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Graph Algorithms Shortest-Path.
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.
1 WEEK 9-10 Graphs II Unweighted Shortest Paths Dijkstra’s Algorithm Graphs with negative costs Acyclic Graphs Izmir University of Economics.
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.
CS 146: Data Structures and Algorithms June 23 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
COSC 2007 Data Structures II Chapter 14 Graphs III.
Dijkstra’s Algorithm. Announcements Assignment #2 Due Tonight Exams Graded Assignment #3 Posted.
Dijkstra’s Algorithm. This algorithm finds the shortest path from a source vertex to all other vertices in a weighted directed graph without negative.
CS 146: Data Structures and Algorithms July 23 Class Meeting
CSE 326: Data Structures Lecture #20 Graphs I.5 Alon Halevy Spring Quarter 2001.
CS 146: Data Structures and Algorithms July 14 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
CS 146: Data Structures and Algorithms July 16 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
Graphs. Graphs Similar to the graphs you’ve known since the 5 th grade: line graphs, bar graphs, etc., but more general. Those mathematical graphs are.
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
CSC2100B Tutorial 10 Graph Jianye Hao.
CSCI2100 Data Structures Tutorial 12
Graphs Upon completion you will be able to:
Graphs Chapter 28 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank Carrano.
CS 146: Data Structures and Algorithms July 28 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
Graph Revisited Fei Chen CSCI2100B Data Structures Tutorial 12 1.
Graphs and Paths Data Structures & Problem Solving Using JAVA Second Edition Mark Allen Weiss Chapter 14 © 2002 Addison Wesley.
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
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.
CSE 326: Data Structures Lecture #23 Dijkstra and Kruskal (sittin’ in a graph) Steve Wolfman Winter Quarter 2000.
© 2006 Pearson Addison-Wesley. All rights reserved14 B-1 Chapter 14 (continued) Graphs.
Minimum Spanning Trees
CMPE Data Structures and Algorithms in C++ May 3 Class Meeting
Short paths and spanning trees
Graph Algorithm.
Minimum Spanning Trees
CSE 373 Data Structures and Algorithms
Shortest Path Algorithms
Chapter 15 Graphs © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
CSCI2100 Data Structures Tutorial
Minimum Spanning Trees
Fundamental Structures of Computer Science II
CE 221 Data Structures and Algorithms
Data Structures and Algorithm Analysis Graph Algorithms
CSE 417: Algorithms and Computational Complexity
CE 221 Data Structures and Algorithms
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Chapter 9: Graphs Shortest Paths
Presentation transcript:

CS 146: Data Structures and Algorithms July 21 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak www.cs.sjsu.edu/~mak

Graph Representation Represent a directed graph with an adjacency list. For each vertex, keep a list of all adjacent vertices. Data Structures and Algorithms in Java, 3rd ed. by Mark Allen Weiss Pearson Education, Inc., 2012 ISBN 0-13-257627-9

Topological Sort We can use a graph to represent the prerequisites in a course of study. A directed edge from Course A to Course B means that Course A is a prerequisite for Course B. Data Structures and Algorithms in Java, 3rd ed. by Mark Allen Weiss Pearson Education, Inc., 2012 ISBN 0-13-257627-9

Topological Sort, cont’d A topological sort of a directed graph is an ordering of the vertices such that if there is a path from vi to vj, then vi comes before vj in the ordering. The order is not necessarily unique. Data Structures and Algorithms in Java, 3rd ed. by Mark Allen Weiss Pearson Education, Inc., 2012 ISBN 0-13-257627-9

Topological Sort Topological sort example using a queue. Start with vertex v1. On each pass, remove the vertices with indegree = 0. Subtract 1 from the indegree of the adjacent vertices. Data Structures and Algorithms in Java, 3rd ed. by Mark Allen Weiss Pearson Education, Inc., 2012 ISBN 0-13-257627-9

Topological Sort Pseudocode to perform a topological sort. O(|E| + |V |) time

Shortest Path Algorithms Assume there is a cost associated with each edge. The cost of a path is the sum of the cost of each edge on the path. Find the least-cost path from a “distinguished” vertex s to every other vertex in the graph. Data Structures and Algorithms in Java, 3rd ed. by Mark Allen Weiss Pearson Education, Inc., 2012 ISBN 0-13-257627-9

Shortest Path Algorithms, cont’d A negative cost results in a negative-cost cycle. Make a path’s cost arbitrarily small by looping. Data Structures and Algorithms in Java, 3rd ed. by Mark Allen Weiss Pearson Education, Inc., 2012 ISBN 0-13-257627-9

Unweighted Shortest Path Minimize the lengths of paths. Assign a weight of 1 to each edge. In this example, let the distinguished vertex s be v3. Data Structures and Algorithms in Java, 3rd ed. by Mark Allen Weiss Pearson Education, Inc., 2012 ISBN 0-13-257627-9

Unweighted Shortest Path, cont’d The path from s to itself has length (cost) 0. Data Structures and Algorithms in Java, 3rd ed. by Mark Allen Weiss Pearson Education, Inc., 2012 ISBN 0-13-257627-9

Unweighted Shortest Path, cont’d Find vertices v1 and v6 that are distance 1 from v3: Data Structures and Algorithms in Java, 3rd ed. by Mark Allen Weiss Pearson Education, Inc., 2012 ISBN 0-13-257627-9

Unweighted Shortest Path, cont’d Find all vertices that are distance 2 from v3. Begin with the vertices adjacent to v1 and v6. Data Structures and Algorithms in Java, 3rd ed. by Mark Allen Weiss Pearson Education, Inc., 2012 ISBN 0-13-257627-9

Unweighted Shortest Path, cont’d Find all vertices that are distance 3 from v3. Begin with the vertices adjacent to v2 and v4. Now we have the shortest paths from v3 to every other vertex. Data Structures and Algorithms in Java, 3rd ed. by Mark Allen Weiss Pearson Education, Inc., 2012 ISBN 0-13-257627-9

Unweighted Shortest Path, cont’d Keep the tentative distance from vertex v3 to another vertex in the dv column. Keep track of the path in the pv column. A vertex becomes known after it has been processed. Don’t reprocess a known vertex. No cheaper path can be found. Set all dv = ∞. Enqueue the distinquished vertex s and set ds = 0. During each iteration, dequeue a vertex v. Mark v as known. For each vertex w adjacent to v whose dw = ∞ Set its distance dw to dv + 1 Set its path pw to v. Enqueue w.

Unweighted Shortest Path, cont’d

Unweighted Shortest Path, cont’d

Break

Weighted Least Cost Path Dijkstra’s Algorithm Example of a greedy algorithm. Greedy algorithm At each stage, do what appears to be the best at that stage. May not always work. Keep the same information for each vertex: Either known or unknown Tentative distance dv Path information pv

Dijkstra’s Algorithm At each stage: Select an unknown vertex v that has the smallest dv. Declare that the shortest path from s to v is known. For each vertex w adjacent to v: Set its distance dw to the dv + costv,w Set its path pw to v. Data Structures and Algorithms in Java, 3rd ed. by Mark Allen Weiss Pearson Education, Inc., 2012 ISBN 0-13-257627-9

Dijkstra’s Algorithm, cont’d Start with s = v1 Data Structures and Algorithms in Java, 3rd ed. by Mark Allen Weiss Pearson Education, Inc., 2012 ISBN 0-13-257627-9

Dijkstra’s Algorithm, cont’d Set v1 to known. v2 and v4 are unknown and adjacent to v1: Set d2 and d4 to their costs + cost of v1 Set p2 and p4 to v1. Data Structures and Algorithms in Java, 3rd ed. by Mark Allen Weiss Pearson Education, Inc., 2012 ISBN 0-13-257627-9

Dijkstra’s Algorithm, cont’d Set v4 to known. v3, v5, v6, and v7 are unknown and adjacent to v4: Set their dw to their costs + cost of v4 Set their pw to v4. Data Structures and Algorithms in Java, 3rd ed. by Mark Allen Weiss Pearson Education, Inc., 2012 ISBN 0-13-257627-9

Dijkstra’s Algorithm, cont’d Set v2 to known. v5 is unknown and adjacent: d5 is already 3 which is less than 2+10=12, so v5 is not changed Data Structures and Algorithms in Java, 3rd ed. by Mark Allen Weiss Pearson Education, Inc., 2012 ISBN 0-13-257627-9

Dijkstra’s Algorithm, cont’d Set v5 to known. v7 is unknown and adjacent. Do not adjust since 5 < 3+6. Set v3 to known. v6 is unknown and adjacent. Set d6 to 3+5=8 which is less than its previous value of 9. Set p6 to v3. Data Structures and Algorithms in Java, 3rd ed. by Mark Allen Weiss Pearson Education, Inc., 2012 ISBN 0-13-257627-9

Dijkstra’s Algorithm, cont’d Set v7 to known. v6 is unknown and adjacent. Set d6 to 5+1=6 which is less than its previous value of 8. Set p6 to v7. Data Structures and Algorithms in Java, 3rd ed. by Mark Allen Weiss Pearson Education, Inc., 2012 ISBN 0-13-257627-9

Dijkstra’s Algorithm, cont’d Set v6 to known. The algorithm terminates. Data Structures and Algorithms in Java, 3rd ed. by Mark Allen Weiss Pearson Education, Inc., 2012 ISBN 0-13-257627-9

Assignment #6 In this assignment, you will write programs to: Perform a topological sort Find the shortest unweighted path Find the shortest weighted path

Assignment #6, cont’d Write a Java program to perform a topological sort using a queue. Use Figure 9.81 (p. 417 and on the next slide) in the textbook as input. Print the sorting table, similar to Figure 9.6 (p. 364), except that instead of generating a new column after each dequeue operation, you can print the column as a row instead. Print the nodes in sorted order, starting with vertex s.

Assignment #6, cont’d Figure 9.81 for the topological sort program. Data Structures and Algorithms in Java, 3rd ed. by Mark Allen Weiss Pearson Education, Inc., 2012 ISBN 0-13-257627-9

Assignment #6, cont’d Write a Java program to find the unweighted shortest path from a given vertex to all other vertices. Use Figure 9.82 (page 418 and the next slide) as input. Vertex A is distinguished. Print the intermediate tables (such as Figure 9.19). Print the final path.

Assignment #6, cont’d Write a Java program to find the weighted shortest path from a given vertex to all other vertices. Use Figure 9.82 (page 418 and the next slide) as input. Vertex A is distinguished. Print the intermediate tables (such as Figures 9.21-9.27). Print the final path.

Assignment #6, cont’d Figure 9.82 for the shortest path programs. Vertex A is distinguished. Data Structures and Algorithms in Java, 3rd ed. by Mark Allen Weiss Pearson Education, Inc., 2012 ISBN 0-13-257627-9

Assignment #6, cont’d You may choose a partner to work with you on this assignment. Both of you will receive the same score. Email your answers to ron.mak@sjsu.edu Subject line: CS 146 Assignment #6: Your Name(s) CC your partner’s email address so I can “reply all”. Due Friday, July 30 at 11:59 PM.

Minimum Spanning Tree (MST) Suppose you’re wiring a new house. What’s the minimum length of wire you need to purchase? Represent the house as an undirected graph. Each electrical outlet is a vertex. The wires between the outlets are the edges. The cost of each edge is the length of the wire.

Minimum Spanning Tree (MST), cont’d Create a tree formed from the edges of an undirected graph that connects all the vertices at the lowest total cost.

Minimum Spanning Tree (MST), cont’d The MST Is an acyclic tree. Spans (includes) every vertex. Has |V |-1 edges. Has minimum total cost. Data Structures and Algorithms in Java, 3rd ed. by Mark Allen Weiss Pearson Education, Inc., 2012 ISBN 0-13-257627-9

Minimum Spanning Tree (MST), cont’d Add each edge to an MST in such a way that: It does not create a cycle. Is the least cost addition. A greedy algorithm! Data Structures and Algorithms in Java, 3rd ed. by Mark Allen Weiss Pearson Education, Inc., 2012 ISBN 0-13-257627-9