04/03/03 Lecture 20 1

Size: px
Start display at page:

Download "04/03/03 Lecture 20 1"

Transcription

1 Figure 14.2 Adjacency list representation of the graph shown in Figure 14.1; the nodes in list i represent vertices adjacent to i and the cost of the connecting edge. 04/03/03 Lecture 20 1

2 Shortest Paths Suppose we are interested in the shortest paths (and their lengths) from vertex Miami to all other vertices in the graph. We need to augment the data structure to store this information. 04/03/03 Lecture 20 2

3 Figure 14.4 An abstract scenario of the data structures used in a shortest-path calculation, with an input graph taken from a file. The shortest weighted path from A to C is A to B to E to D to C (cost is 76). 04/03/03 Lecture 20 3

4 Vertex Object // Represents a vertex in the graph. class Vertex { public String name; // Vertex name public List adj; // Adjacent vertices public double dist; // Cost public Vertex prev; // Previous vertex on shortest path public int scratch;// Extra variable used in algorithm public Vertex( String nm ) { name = nm; adj = new LinkedList( ); reset( ); public void reset( ) { dist = Graph.INFINITY; prev = null; pos = null; scratch = 0; public PriorityQueue.Position pos; // Used for dijkstra2 04/03/03 Lecture 20 4

5 Edge Object // Represents an edge in the graph. class Edge { public Vertex dest; // Second vertex in Edge public double cost; // Edge cost public Edge( Vertex d, double c ) { dest = d; cost = c; 04/03/03 Lecture 20 5

6 Graph Object // Graph class: evaluate shortest paths. // // CONSTRUCTION: with no parameters. // // ******************PUBLIC OPERATIONS********* // void addedge( String v, String w, double cvw ) // --> Add additional edge // void printpath( String w ) --> Print path after alg is run // void unweighted( String s ) --> Single-source unweighted // void dijkstra( String s ) --> Single-source weighted // void negative( String s ) --> Single-source negative weighted // void acyclic( String s ) --> Single-source acyclic 04/03/03 Lecture 20 6

7 getvertex Method public class Graph { public static final double INFINITY = Double.MAX_VALUE; private Map vertexmap = new HashMap( ); // Maps String to Vertex /** If vertexname is not present, add it to vertexmap. * * In either case, return the Vertex. */ private Vertex getvertex( String vertexname ) { Vertex v = (Vertex) vertexmap.get( vertexname ); if( v == null ) { v = new Vertex( vertexname ); vertexmap.put( vertexname, v ); return v; 04/03/03 Lecture 20 7

8 addedge Method /** * Add a new edge to the graph. */ public void addedge( String sourcename, String destname, double cost ) { Vertex v = getvertex( sourcename ); Vertex w = getvertex( destname ); v.adj.add( new Edge( w, cost ) ); 04/03/03 Lecture 20 8

9 printpath Method /** * Recursive routine to print shortest path to dest * after running shortest path algorithm. The path * is known to exist. */ private void printpath( Vertex dest ) { if( dest.prev!= null ) { printpath( dest.prev ); System.out.print( " to " ); System.out.print( dest.name ); 04/03/03 Lecture 20 9

10 /** clearall Method * Initializes the vertex output info prior to running * any shortest path algorithm. */ private void clearall( ) { for( Iterator itr = vertexmap.values( ).iterator( ); itr.hasnext( ); ) ( (Vertex)itr.next( ) ).reset( ); 04/03/03 Lecture 20 10

11 /** Unweighted Shortest Path Problem * A main routine that: * 1. Reads a file containing edges (supplied as a commandline parameter); * 2. Forms the graph; * 3. Repeatedly prompts for two vertices and * runs the shortest path algorithm. * The data file is a sequence of lines of the format * source destination. */ 04/03/03 Lecture 20 11

12 Figure 14.5 Data structures used in a shortest-path calculation, with an input graph taken from a file; the shortest weighted path from A to C is A to B to E to D to C (cost is 76). Legend: Dark-bordered boxes are Vertex objects. The unshaded portion in each box contains the name and adjacency list and does not change when shortest-path computation is performed. Each adjacency list entry contains an Edge that stores a reference to another Vertex object and the edge cost. Shaded portion is dist and prev, filled in after shortest path computation runs. Dark arrows emanate from vertexmap. Light arrows are adjacency list entries. Dashed arrows are the prev data member that results from a shortest-path computation. 04/03/03 Lecture 20 12

13 Figure The graph, after the starting vertex has been marked as reachable in zero edges 04/03/03 Lecture 20 13

14 Figure The graph, after all the vertices whose path length from the starting vertex is 1 have been found 04/03/03 Lecture 20 14

15 Figure The graph, after all the vertices whose shortest path from the starting vertex is 2 have been found 04/03/03 Lecture 20 15

16 Figure The final shortest paths 04/03/03 Lecture 20 16

17 Figure If w is adjacent to v and there is a path to v, there also is a path to w 04/03/03 Lecture 20 17

18 Figure 14.21A Searching the graph in the unweighted shortest-path computation. The darkest-shaded vertices have already been completely processed, the lightest-shaded vertices have not yet been used as v, and the mediumshaded vertex is the current vertex, v. The stages proceed left to right, top to bottom, as numbered (continued). 04/03/03 Lecture 20 18

19 Figure 14.21B Searching the graph in the unweighted shortest-path computation. The darkest-shaded vertices have already been completely processed, the lightest-shaded vertices have not yet been used as v, and the mediumshaded vertex is the current vertex, v. The stages proceed left to right, top to bottom, as numbered. 04/03/03 Lecture 20 19

20 public static void main( String [ ] args ) { Graph g = new Graph( ); BufferedReader graphfile=new BufferedReader(FileReader( args[0] ) ); // Read the edges and insert String line; while( ( line = graphfile.readline( ) )!= null ) { StringTokenizer st = new StringTokenizer( line ); if( st.counttokens( )!= 3 ) { // some error message String source = st.nexttoken( ); String dest = st.nexttoken( ); int cost = Integer.parseInt( st.nexttoken( ) ); g.addedge( source, dest, cost ); // Read the queries BufferedReader in = new BufferedReader( new InputStreamReader( System.in ) ); while( processrequest( in, g ) ) ; // while loop body is empty 04/03/03 Lecture 20 20

21 processrequest Method public static boolean processrequest( BufferedReader in, Graph g ) { String startname = null, destname = null, alg = null; System.out.print( "Enter start node:" ); if( ( startname = in.readline( ) ) == null ) return false; System.out.print( "Enter destination node:" ); if( ( destname = in.readline( ) ) == null ) return false; g.unweighted( startname ); // changes with algorithm g.printpath( destname ); return true; 04/03/03 Lecture 20 21

22 SP unweighted graphs public void unweighted( String startname ) { clearall( ); Vertex start = (Vertex) vertexmap.get( startname ); LinkedList q = new LinkedList( ); q.addlast( start ); start.dist = 0; while(!q.isempty( ) ) { Vertex v = (Vertex) q.removefirst( ); for( Iterator itr = v.adj.iterator( ); itr.hasnext( ); ) { Edge e = (Edge) itr.next( ); Vertex w = e.dest; if( w.dist == INFINITY ) { w.dist = v.dist + 1; w.prev = v; q.addlast( w ); 04/03/03 Lecture 20 22

23 Figure The eyeball is at v and w is adjacent, so D w should be lowered to 6. 04/03/03 Lecture 20 23

24 Figure If D v is minimal among all unseen vertices and if all edge costs are nonnegative, D v represents the shortest path. 04/03/03 Lecture 20 24

25 Figure 14.25A Stages of Dijkstra s algorithm. The conventions are the same as those in Figure (continued). 04/03/03 Lecture 20 25

26 Figure 14.25B Stages of Dijkstra s algorithm. The conventions are the same as those in Figure /03/03 Lecture 20 26

27 Class Path // Represents an entry in the priority queue for Dijkstra's algorithm. class Path implements Comparable { public Vertex dest; // w public double cost; // d(w) public Path( Vertex d, double c ) { dest = d; cost = c; public int compareto( Object rhs ) { double othercost = ((Path)rhs).cost; return cost < othercost? -1 : cost > othercost? 1 : 0; 04/03/03 Lecture 20 27

28 public void dijkstra( String startname ) { PriorityQueue pq = new BinaryHeap( ); Vertex start = (Vertex) vertexmap.get( startname ); clearall( ); pq.insert( new Path( start, 0 ) ); start.dist = 0; int nodesseen = 0; while(!pq.isempty( ) && nodesseen < vertexmap.size( ) ) { Path vrec = (Path) pq.deletemin( ); Vertex v = vrec.dest; if( v.scratch!= 0 ) continue; // already processed v v.scratch = 1; nodesseen++; for( Iterator itr = v.adj.iterator( ); itr.hasnext( ); ) { Edge e = (Edge) itr.next( ); Vertex w = e.dest; double cvw = e.cost; if( w.dist > v.dist + cvw ) { w.dist = v.dist +cvw; w.prev = v; pq.insert( new Path( w, w.dist ) ); 04/03/03 Lecture 20 28

29 Figure 14.30A A topological sort. The conventions are the same as those in Figure (continued). 04/03/03 Lecture 20 29

30 Figure 14.30B A topological sort. The conventions are the same as those in Figure /03/03 Lecture 20 30

31 Figure 14.31A The stages of acyclic graph algorithm. The conventions are the same as those in Figure (continued). 04/03/03 Lecture 20 31

32 Figure 14.31B The stages of acyclic graph algorithm. The conventions are the same as those in Figure /03/03 Lecture 20 32

33 Figure An activity-node graph 04/03/03 Lecture 20 33

34 Figure An event-node graph 04/03/03 Lecture 20 34

35 Figure Earliest completion times 04/03/03 Lecture 20 35

36 Figure Latest completion times 04/03/03 Lecture 20 36

37 Figure Earliest completion time, latest completion time, and slack (additional edge item) 04/03/03 Lecture 20 37

38 Figure Worst-case running times of various graph algorithms 04/03/03 Lecture 20 38

COP 3530, Course Outcomes

COP 3530, Course Outcomes COP 3530, Course Outcomes be familiar with basic techniques of algorithm analysis master analyzing simple non-recursive algorithms be familiar with analyzing recursive algorithms master the implementation

More information

Graphs. 04/01/03 Lecture 20 1

Graphs. 04/01/03 Lecture 20 1 Graphs Graphs model networks of various kinds: roads, highways, oil pipelines, airline routes, dependency relationships, etc. Graph G(V,E) VVertices or Nodes EEdges or Links: pairs of vertices Directed

More information

Data Structures. Giri Narasimhan Office: ECS 254A Phone: x-3748

Data Structures. Giri Narasimhan Office: ECS 254A Phone: x-3748 Data Structures Giri Narasimhan Office: ECS 254A Phone: x-3748 giri@cs.fiu.edu Lecture 18 03/11/04 Vertex and Edge classes Class Edge { public Vertex dest; public double weight; public Edge (Vertex d,

More information

CS 3410 Ch 14 Graphs and Paths

CS 3410 Ch 14 Graphs and Paths CS 3410 Ch 14 Graphs and Paths Sections 14.1-14.3 Pages 527-552 Exercises 1,2,5,7-9 14.1 Definitions 1. A vertex (node) and an edge are the basic building blocks of a graph. Two vertices, (, ) may be connected

More information

Lecture 10. Graphs Vertices, edges, paths, cycles Sparse and dense graphs Representations: adjacency matrices and adjacency lists Implementation notes

Lecture 10. Graphs Vertices, edges, paths, cycles Sparse and dense graphs Representations: adjacency matrices and adjacency lists Implementation notes Lecture 10 Graphs Vertices, edges, paths, cycles Sparse and dense graphs Representations: adjacency matrices and adjacency lists Implementation notes Reading: Weiss, Chapter 9 Page 1 of 24 Midterm exam

More information

CSE 100 Minimum Spanning Trees Prim s and Kruskal

CSE 100 Minimum Spanning Trees Prim s and Kruskal CSE 100 Minimum Spanning Trees Prim s and Kruskal Your Turn The array of vertices, which include dist, prev, and done fields (initialize dist to INFINITY and done to false ): V0: dist= prev= done= adj:

More information

CS 146 Spring 2017 Homework 4 Sections 5 and 6. The homework assignment is available at:

CS 146 Spring 2017 Homework 4 Sections 5 and 6. The homework assignment is available at: The homework assignment is available at: http://www.jennylam.cc/courses/16-s17/homework0.html a c d 1 7 e 7 6 f h i 0. b 10 g (a) Illustrate the execution of breadth-first search on the graph above, treating

More information

Basic Graph Definitions

Basic Graph Definitions CMSC 341 Graphs Basic Graph Definitions A graph G = (V,E) consists of a finite set of vertices, V, and a finite set of edges, E. Each edge is a pair (v,w) where v, w V. V and E are sets, so each vertex

More information

The Collections API. Lecture Objectives. The Collections API. Mark Allen Weiss

The Collections API. Lecture Objectives. The Collections API. Mark Allen Weiss The Collections API Mark Allen Weiss Lecture Objectives To learn how to use the Collections package in Java 1.2. To illustrate features of Java that help (and hurt) the design of the Collections API. Tuesday,

More information

The Shortest Path Problem

The Shortest Path Problem The Shortest Path Problem 1 Shortest-Path Algorithms Find the shortest path from point A to point B Shortest in time, distance, cost, Numerous applications Map navigation Flight itineraries Circuit wiring

More information

CMSC 132: Object-Oriented Programming II. Shortest Paths

CMSC 132: Object-Oriented Programming II. Shortest Paths CMSC 13: Object-Oriented Programming II Shortest Paths 1 Quiz 1 One advantage of adjacency list representation over adjacency matrix representation of a graph is that in adjacency list representation,

More information

18: GRAPH DATA STRUCTURES. Introduction

18: GRAPH DATA STRUCTURES. Introduction 18: GRAPH DATA STRUCTURES Introduction... 1 Describing graphs... Directed Graphs... 3 Traversing a graph... EXERCISE: Traversal... 8 Implementing a Graph... 9 EXERCISE: Looking at a Graph... 1 EXERICISE:

More information

CS 314 Final Fall 2011

CS 314 Final Fall 2011 Points off 1 2A 2B 2C 3 4 5 Total off Net Score CS 314 Final Fall 2011 Your Name_ Your UTEID Instructions: 1. There are 5 questions on this test. 2. You have 3 hours to complete the test. 3. You may not

More information

Graphs & Digraphs Tuesday, November 06, 2007

Graphs & Digraphs Tuesday, November 06, 2007 Graphs & Digraphs Tuesday, November 06, 2007 10:34 PM 16.1 Directed Graphs (digraphs) like a tree but w/ no root node & no guarantee of paths between nodes consists of: nodes/vertices - a set of elements

More information

CSE 100: GRAPH ALGORITHMS

CSE 100: GRAPH ALGORITHMS CSE 100: GRAPH ALGORITHMS 2 Graphs: Example A directed graph V5 V = { V = E = { E Path: 3 Graphs: Definitions A directed graph V5 V6 A graph G = (V,E) consists of a set of vertices V and a set of edges

More information

SPANNING TREES. Lecture 21 CS2110 Spring 2016

SPANNING TREES. Lecture 21 CS2110 Spring 2016 1 SPANNING TREES Lecture 1 CS110 Spring 016 Spanning trees What we do today: Calculating the shortest path in Dijkstra s algorithm Look at time complexity of shortest path Definitions Minimum spanning

More information

Unweighted Graphs & Algorithms

Unweighted Graphs & Algorithms Unweighted Graphs & Algorithms Zachary Friggstad Programming Club Meeting References Chapter 4: Graph (Section 4.2) Chapter 22: Elementary Graph Algorithms Graphs Features: vertices/nodes/dots and edges/links/lines

More information

Week 9. CS 400 Programming III

Week 9. CS 400 Programming III Week 9 p3b: final submission due before 10pm on Thursday, 3/28 p4: available soon x4: due before 10pm on Monday 4/1 x5: meet with x-team coach for design review 4/1-4/8 Module: Week 9 (and start on week

More information

CS350: Data Structures Dijkstra s Shortest Path Alg.

CS350: Data Structures Dijkstra s Shortest Path Alg. Dijkstra s Shortest Path Alg. James Moscola Department of Engineering & Computer Science York College of Pennsylvania James Moscola Shortest Path Algorithms Several different shortest path algorithms exist

More information

Priority Queues. 04/10/03 Lecture 22 1

Priority Queues. 04/10/03 Lecture 22 1 Priority Queues It is a variant of queues Each item has an associated priority value. When inserting an item in the queue, the priority value is also provided for it. The data structure provides a method

More information

CSE 100 Practice Final Exam

CSE 100 Practice Final Exam CSE 100 Practice Final Exam Please note that this practice final only serves as a template and does not exhaustively cover all the topics that are on the actual final. The final exam will cover all the

More information

Prelim 2. CS 2110, November 20, 2014, 7:30 PM Extra Total Question True/False Short Answer

Prelim 2. CS 2110, November 20, 2014, 7:30 PM Extra Total Question True/False Short Answer Prelim 2 CS 2110, November 20, 2014, 7:30 PM 1 2 3 4 5 Extra Total Question True/False Short Answer Complexity Induction Trees Graphs Extra Credit Max 20 10 15 25 30 5 100 Score Grader The exam is closed

More information

2. True or false: even though BFS and DFS have the same space complexity, they do not always have the same worst case asymptotic time complexity.

2. True or false: even though BFS and DFS have the same space complexity, they do not always have the same worst case asymptotic time complexity. 1. T F: Consider a directed graph G = (V, E) and a vertex s V. Suppose that for all v V, there exists a directed path in G from s to v. Suppose that a DFS is run on G, starting from s. Then, true or false:

More information

Single Source Shortest Paths

Single Source Shortest Paths Single Source Shortest Paths Given a connected weighted directed graph G(V, E), associated with each edge u, v E, there is a weight w(u, v). The single source shortest paths (SSSP) problem is to find a

More information

CPSC 211. SOLUTIONS to PRACTICE EXERCISES II

CPSC 211. SOLUTIONS to PRACTICE EXERCISES II CPSC 211 SOLUTIONS to PRACTICE EXERCISES II Recursion 1. The output of cheers(3) is: Hip Hip Hurrah 2. public static void cheers(int n) { if ( n

More information

CPSC 211. SOLUTIONS to PRACTICE EXERCISES II

CPSC 211. SOLUTIONS to PRACTICE EXERCISES II Last Updated:11/29/2010 7:57 PM Recursion 1. The output of cheers(3) is: Hip Hip Hurrah CPSC 211 SOLUTIONS to PRACTICE EXERCISES II 2. public static void cheers(int n) { if ( n

More information

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

Graph Applications. Topological Sort Shortest Path Problems Spanning Trees. Data Structures 1 Graph Applications 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,

More information

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

Computer Science & Engineering 423/823 Design and Analysis of Algorithms Computer Science & Engineering 423/823 Design and Analysis of Algorithms Lecture 07 Single-Source Shortest Paths (Chapter 24) Stephen Scott and Vinodchandran N. Variyam sscott@cse.unl.edu 1/36 Introduction

More information

CSE 100: GRAPH SEARCH

CSE 100: GRAPH SEARCH CSE 100: GRAPH SEARCH Announcements PA3 released Checkpoint due Tuesday, May 5 @10:00pm Final submission due Thursday, May 14 @10:00PM Start early! Start early! Start early! Start early! Start early! I

More information

This course is intended for 3rd and/or 4th year undergraduate majors in Computer Science.

This course is intended for 3rd and/or 4th year undergraduate majors in Computer Science. Lecture 9 Graphs This course is intended for 3rd and/or 4th year undergraduate majors in Computer Science. You need to be familiar with the design and use of basic data structures such as Lists, Stacks,

More information

CS 512: Comments on Graph Search 16:198:512 Instructor: Wes Cowan

CS 512: Comments on Graph Search 16:198:512 Instructor: Wes Cowan CS 512: Comments on Graph Search 16:198:512 Instructor: Wes Cowan 1 General Graph Search In general terms, the generic graph search algorithm looks like the following: def GenerateGraphSearchTree(G, root):

More information

Implementations. Priority Queues. Heaps and Heap Order. The Insert Operation CS206 CS206

Implementations. Priority Queues. Heaps and Heap Order. The Insert Operation CS206 CS206 Priority Queues An internet router receives data packets, and forwards them in the direction of their destination. When the line is busy, packets need to be queued. Some data packets have higher priority

More information

Agenda. Simulation Discrete event simulation Carwash simulation Call bank simulation

Agenda. Simulation Discrete event simulation Carwash simulation Call bank simulation Applications II 1 Agenda Simulation Discrete event simulation Carwash simulation Call bank simulation Graphs Terminology Representation Traversal Shortest path Topological sorting Problem complexity 2

More information

Input from Files. Buffered Reader

Input from Files. Buffered Reader Input from Files Buffered Reader Input from files is always text. You can convert it to ints using Integer.parseInt() We use BufferedReaders to minimize the number of reads to the file. The Buffer reads

More information

Notes on access restrictions

Notes on access restrictions Notes on access restrictions A source code file MyClass.java is a compilation unit and can contain at most one public class. Furthermore, if there is a public class in that file, it must be called MyClass.

More information

(f) Given what we know about linked lists and arrays, when would we choose to use one data structure over the other?

(f) Given what we know about linked lists and arrays, when would we choose to use one data structure over the other? CSM B Hashing & Heaps Spring 0 Week 0: March 0, 0 Motivation. (a) In the worst case, how long does it take to index into a linked list? Θ(N) (b) In the worst case, how long does it take to index into an

More information

CS 314 Final Spring 2013 SOLUTION - SOLUTION - SOLUTION - SOLUTION - SOLUTION - SOLUTION - SOLUTION

CS 314 Final Spring 2013 SOLUTION - SOLUTION - SOLUTION - SOLUTION - SOLUTION - SOLUTION - SOLUTION Points off 1 2 3 4 5 6 Total off Net Score CS 314 Final Spring 2013 SOLUTION - SOLUTION - SOLUTION - SOLUTION - SOLUTION - SOLUTION - SOLUTION Your UTEID Instructions: 1. There are 6 questions on this

More information

Prelim 2 Solutions. CS 2110, November 20, 2014, 7:30 PM Extra Total Question True/False Short Answer

Prelim 2 Solutions. CS 2110, November 20, 2014, 7:30 PM Extra Total Question True/False Short Answer Prelim 2 Solutions CS 2110, November 20, 2014, 7:30 PM 1 2 3 4 5 Extra Total Question True/False Short Answer Complexity Induction Trees Graphs Extra Credit Max 20 10 15 25 30 5 100 Score Grader The exam

More information

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 20. Example. Shortest Paths Definitions

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 20. Example. Shortest Paths Definitions Taking Stock IE170: Algorithms in Systems Engineering: Lecture 20 Jeff Linderoth Department of Industrial and Systems Engineering Lehigh University March 19, 2007 Last Time Minimum Spanning Trees Strongly

More information

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

22 Elementary Graph Algorithms. There are two standard ways to represent a VI Graph Algorithms Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths All-Pairs Shortest Paths 22 Elementary Graph Algorithms There are two standard ways to represent a graph

More information

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

1 Shortest Paths. 1.1 Breadth First Search (BFS) CS 124 Section #3 Shortest Paths and MSTs 2/13/2018 CS 4 Section # Shortest Paths and MSTs //08 Shortest Paths There are types of shortest paths problems: Single source single destination Single source to all destinations All pairs shortest path In today

More information

Points off Total off Net Score. CS 314 Final Exam Spring 2016

Points off Total off Net Score. CS 314 Final Exam Spring 2016 Points off 1 2 3 4 5 6 Total off Net Score CS 314 Final Exam Spring 2016 Your Name Your UTEID Instructions: 1. There are 6 questions on this test. 100 points available. Scores will be scaled to 300 points.

More information

CS 361 Data Structures & Algs Lecture 15. Prof. Tom Hayes University of New Mexico

CS 361 Data Structures & Algs Lecture 15. Prof. Tom Hayes University of New Mexico CS 361 Data Structures & Algs Lecture 15 Prof. Tom Hayes University of New Mexico 10-12-2010 1 Last Time Identifying BFS vs. DFS trees Can they be the same? Problems 3.6, 3.9, 3.2 details left as homework.

More information

Shortest Path Problem

Shortest Path Problem Shortest Path Problem For weighted graphs it is often useful to find the shortest path between two vertices Here, the shortest path is the path that has the smallest sum of its edge weights Dijkstra s

More information

Today s Outline. Graph ADT? Graph Definitions. Trees as Graphs. Graphs Chapter 9 in Weiss. More Definitions: Simple Paths and Cycles.

Today s Outline. Graph ADT? Graph Definitions. Trees as Graphs. Graphs Chapter 9 in Weiss. More Definitions: Simple Paths and Cycles. Today s Outline Graphs Chapter 9 in Weiss CSE 6 Data Structures Ruth Anderson /6/00 Announcements Written Homework #6 due NOW Project Code due Mon March by pm Project Benchmarking & Written due Thurs March

More information

CSC 172 Data Structures and Algorithms. Lecture 24 Fall 2017

CSC 172 Data Structures and Algorithms. Lecture 24 Fall 2017 CSC 172 Data Structures and Algorithms Lecture 24 Fall 2017 ANALYSIS OF DIJKSTRA S ALGORITHM CSC 172, Fall 2017 Implementation and analysis The initialization requires Q( V ) memory and run time We iterate

More information

CSE 100: GRAPH ALGORITHMS

CSE 100: GRAPH ALGORITHMS CSE 100: GRAPH ALGORITHMS Dijkstra s Algorithm: Questions Initialize the graph: Give all vertices a dist of INFINITY, set all done flags to false Start at s; give s dist = 0 and set prev field to -1 Enqueue

More information

Introduction. I Given a weighted, directed graph G =(V, E) with weight function

Introduction. I Given a weighted, directed graph G =(V, E) with weight function ntroduction Computer Science & Engineering 2/82 Design and Analysis of Algorithms Lecture 05 Single-Source Shortest Paths (Chapter 2) Stephen Scott (Adapted from Vinodchandran N. Variyam) sscott@cse.unl.edu

More information

import java.util.scanner; import java.io.*;

import java.util.scanner; import java.io.*; // Driver.java /** * Programmer: Kenneth Eaton * Course: COSC 439 * Professor: Dr. Tehranipour * Date: 12/11/2007 * Project: Project 3, Routing Table with Dijkstra's Algorithm * * Purpose: This program

More information

CS 340 Homework Alternative 6. Due 11:59 PM Wednesday December 12

CS 340 Homework Alternative 6. Due 11:59 PM Wednesday December 12 CS 340 Homework Alternative 6 Due 11:59 PM Wednesday December 12 Alternative Homework 6 Implement the Dijkstra class and the BinaryHeap class shown on the following slide. The Dijkstra class already includes

More information

1.204 Lecture 7. Data structures: graphs, sets. Graphs and Networks. Graphs can be directed or undirected <0, 1> (0, 1) <1, 0>

1.204 Lecture 7. Data structures: graphs, sets. Graphs and Networks. Graphs can be directed or undirected <0, 1> (0, 1) <1, 0> .0 Lecture 7 Data structures: graphs, sets Graphs and Networks A graph contains: Nodes Arcs, or pairs of nodes ij, i!=j Graphs can be directed or undirected 0 0 0 Directed Undirected (0, )

More information

CmpSci 187: Programming with Data Structures Spring 2015

CmpSci 187: Programming with Data Structures Spring 2015 CmpSci 187: Programming with Data Structures Spring 2015 Lecture #22, More Graph Searches, Some Sorting, and Efficient Sorting Algorithms John Ridgway April 21, 2015 1 Review of Uniform-cost Search Uniform-Cost

More information

CS 61B Data Structures and Programming Methodology. Aug 5, 2008 David Sun

CS 61B Data Structures and Programming Methodology. Aug 5, 2008 David Sun CS 61B Data Structures and Programming Methodology Aug 5, 2008 David Sun Graph Traversal Many algorithms on graphs depend on traversing all or some subset of the verfces. Unlike tree traversals, straight

More information

Data Structures Brett Bernstein

Data Structures Brett Bernstein Data Structures Brett Bernstein Final Review 1. Consider a binary tree of height k. (a) What is the maximum number of nodes? (b) What is the maximum number of leaves? (c) What is the minimum number of

More information

CS61B, Fall 2002 Discussion #15 Amir Kamil UC Berkeley 12/5/02

CS61B, Fall 2002 Discussion #15 Amir Kamil UC Berkeley 12/5/02 CS61B, Fall 2002 Discussion #15 Amir Kamil UC Berkeley 12/5/02 Topics: Graph Algorithms 1 Graph Algorithms There are many algorithms that can be applied to graphs. Many of these are actually used in the

More information

10/31/18. About A6, Prelim 2. Spanning Trees, greedy algorithms. Facts about trees. Undirected trees

10/31/18. About A6, Prelim 2. Spanning Trees, greedy algorithms. Facts about trees. Undirected trees //8 About A, Prelim Spanning Trees, greedy algorithms Lecture CS Fall 8 Prelim : Thursday, November. Visit exams page of course website and read carefully to find out when you take it (: or 7:) and what

More information

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

Graph Algorithms. Chapter 22. CPTR 430 Algorithms Graph Algorithms 1 Graph Algorithms Chapter 22 CPTR 430 Algorithms Graph Algorithms Why Study Graph Algorithms? Mathematical graphs seem to be relatively specialized and abstract Why spend so much time and effort on algorithms

More information

Spanning Trees, greedy algorithms. Lecture 20 CS2110 Fall 2018

Spanning Trees, greedy algorithms. Lecture 20 CS2110 Fall 2018 1 Spanning Trees, greedy algorithms Lecture 20 CS2110 Fall 2018 1 About A6, Prelim 2 Prelim 2: Thursday, 15 November. Visit exams page of course website and read carefully to find out when you take it

More information

Topological Sort. Here a topological sort would label A with 1, B and C with 2 and 3, and D with 4.

Topological Sort. Here a topological sort would label A with 1, B and C with 2 and 3, and D with 4. Topological Sort The goal of a topological sort is given a list of items with dependencies, (ie. item 5 must be completed before item 3, etc.) to produce an ordering of the items that satisfies the given

More information

Introduction. I Given a weighted, directed graph G =(V, E) with weight function

Introduction. I Given a weighted, directed graph G =(V, E) with weight function ntroduction Computer Science & Engineering 2/82 Design and Analysis of Algorithms Lecture 06 Single-Source Shortest Paths (Chapter 2) Stephen Scott (Adapted from Vinodchandran N. Variyam) sscott@cse.unl.edu

More information

COS 226 Algorithms and Data Structures Practice Design Questions

COS 226 Algorithms and Data Structures Practice Design Questions COS 226 Algorithms & Data Structures p. 1/5 COS 226 Algorithms and Data Structures Practice Design Questions Version: May 1, 2017 Exercise 1 Shortest-Princeton-Path (Final Spring 2011) Consider the following

More information

1. Graph and Representation

1. Graph and Representation Chapter Graphs Tree Root Direction: parent-child relationships No cycles Graph Vertices + Edges No tree restrictions Examples World Wide Web Maps. Graph and Representation A graph G: a pair (V, E) vertices

More information

Total Score /15 /20 /30 /10 /5 /20 Grader

Total Score /15 /20 /30 /10 /5 /20 Grader NAME: NETID: CS2110 Fall 2009 Prelim 2 November 17, 2009 Write your name and Cornell netid. There are 6 questions on 8 numbered pages. Check now that you have all the pages. Write your answers in the boxes

More information

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

Graph Representation DFS BFS Dijkstra A* Search Bellman-Ford Floyd-Warshall Iterative? Non-iterative? MST Flow Edmond-Karp Charles Lin Graph Representation DFS BFS Dijkstra A* Search Bellman-Ford Floyd-Warshall Iterative? Non-iterative? MST Flow Edmond-Karp Adjacency Matrix bool way[100][100]; cin >> i >> j; way[i][j] = true;

More information

1 Dijkstra s Algorithm

1 Dijkstra s Algorithm Lecture 11 Dijkstra s Algorithm Scribes: Himanshu Bhandoh (2015), Virginia Williams, and Date: November 1, 2017 Anthony Kim (2016), G. Valiant (2017), M. Wootters (2017) (Adapted from Virginia Williams

More information

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

Total Score /1 /20 /41 /15 /23 Grader NAME: NETID: CS2110 Spring 2015 Prelim 2 April 21, 2013 at 5:30 0 1 2 3 4 Total Score /1 /20 /41 /15 /23 Grader There are 5 questions numbered 0..4 on 8 pages. Check now that you have all the pages. Write

More information

Priority queue ADT part 1

Priority queue ADT part 1 http://www.mainjava.com/java/core-java/priorityqueue-class-in-java-with-programming-example/ Priority queue ADT part 1 CS 146 - Spring 2017 Today Dijkstra s algorithm The minimum spanning tree problem

More information

Homework Assignment #3 Graph

Homework Assignment #3 Graph CISC 4080 Computer Algorithms Spring, 2019 Homework Assignment #3 Graph Some of the problems are adapted from problems in the book Introduction to Algorithms by Cormen, Leiserson and Rivest, and some are

More information

Advanced Java Concepts Unit 9: Graph Exercises

Advanced Java Concepts Unit 9: Graph Exercises dvanced Java oncepts Unit : Graph Exercises. Which one of the following data structures would be best suited for representing a street map for pedestrians? graph with... a) undirected, unweighted edges.

More information

Algorithms. Algorithms 4.4 SHORTEST PATHS. APIs properties Bellman Ford algorithm Dijkstra s algorithm ROBERT SEDGEWICK KEVIN WAYNE

Algorithms. Algorithms 4.4 SHORTEST PATHS. APIs properties Bellman Ford algorithm Dijkstra s algorithm ROBERT SEDGEWICK KEVIN WAYNE Algorithms ROBERT SEDGEWICK KEVIN WAYNE 4.4 SHORTEST PATHS Algorithms F O U R T H E D I T I O N APIs properties Bellman Ford algorithm Dijkstra s algorithm ROBERT SEDGEWICK KEVIN WAYNE https://algs4.cs.princeton.edu

More information

Points off A 4B 5 Total off Net Score. CS 314 Final Exam Spring 2015

Points off A 4B 5 Total off Net Score. CS 314 Final Exam Spring 2015 Points off 1 2 3 4A 4B 5 Total off Net Score CS 314 Final Exam Spring 2015 Your Name Your UTEID Instructions: 1. There are 5 questions on this test. 100 points available. 2. You have 3 hours to complete

More information

Algorithm Design and Analysis

Algorithm Design and Analysis Algorithm Design and Analysis LECTURE 7 Greedy Graph Algorithms Topological sort Shortest paths Adam Smith The (Algorithm) Design Process 1. Work out the answer for some examples. Look for a general principle

More information

Points off Total off Net Score. CS 314 Final Exam Fall 2016

Points off Total off Net Score. CS 314 Final Exam Fall 2016 Points off 1 2 3 4 5 6 Total off Net Score CS 314 Final Exam Fall 2016 Your Name Your UTEID Instructions: 1. There are 6 questions on this test. 100 points available. Scores will be scaled to 300 points.

More information

More Graph Algorithms: Topological Sort and Shortest Distance

More Graph Algorithms: Topological Sort and Shortest Distance More Graph Algorithms: Topological Sort and Shortest Distance Topological Sort The goal of a topological sort is given a list of items with dependencies, (ie. item 5 must be completed before item 3, etc.)

More information

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

1 Shortest Paths. 1.1 Breadth First Search (BFS) CS 124 Section #3 Shortest Paths and MSTs 2/13/2018 CS Section # Shortest Paths and MSTs //08 Shortest Paths There are types of shortest paths problems: Single source single destination Single source to all destinations All pairs shortest path In today

More information

CS61B Spring 2016 Guerrilla Section 6 Worksheet

CS61B Spring 2016 Guerrilla Section 6 Worksheet Spring 2016 5 May 2016 Directions: In groups of 4-5, work on the following exercises. Do not proceed to the next exercise until everyone in your group has the answer and understands why the answer is what

More information

A Parallel Algorithm for the Single-Source Shortest Path Problem

A Parallel Algorithm for the Single-Source Shortest Path Problem A Parallel Algorithm for the Single-Source Shortest Path Problem Kevin Kelley, Tao Schardl May 12, 2010 Outline The Problem Dijkstra s Algorithm Gabow s Scaling Algorithm Optimizing Gabow Parallelizing

More information

CS Algorithms and Complexity

CS Algorithms and Complexity CS 350 - Algorithms and Complexity Graph Theory, Midterm Review Sean Anderson 2/6/18 Portland State University Table of contents 1. Graph Theory 2. Graph Problems 3. Uninformed Exhaustive Search 4. Informed

More information

Computer Science CS221 Test 4 Name

Computer Science CS221 Test 4 Name Computer Science CS221 Test 4 Name 1. We looked at two different concrete classes that implemented the Queue interface, QueueAr which used an array as the underlying data structure, and QueueLi which used

More information

CSE 373: Data Structures and Algorithms

CSE 373: Data Structures and Algorithms CSE 373: Data Structures and Algorithms Lecture 17: Finish Dijkstra s Algorithm, Preserving Abstractions (Software Design), Spanning Trees Instructor: Lilian de Greef Quarter: Summer 2017 Today Wrap up

More information

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

ROOT: A node which doesn't have a parent. In the above tree. The Root is A. TREE: A tree is a finite set of one or more nodes such that there is a specially designated node called the Root, and zero or more non empty sub trees T 1, T 2...T k, each of whose roots are connected

More information

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

4/8/11. Single-Source Shortest Path. Shortest Paths. Shortest Paths. Chapter 24 /8/11 Single-Source Shortest Path Chapter 1 Shortest Paths Finding the shortest path between two nodes comes up in many applications o Transportation problems o Motion planning o Communication problems

More information

Compiling expressions

Compiling expressions E H U N I V E R S I T Y T O H F R G Compiling expressions E D I N B U Javier Esparza Computer Science School of Informatics The University of Edinburgh The goal 1 Construct a compiler for arithmetic expressions

More information

CIS 121 Data Structures and Algorithms Minimum Spanning Trees

CIS 121 Data Structures and Algorithms Minimum Spanning Trees CIS 121 Data Structures and Algorithms Minimum Spanning Trees March 19, 2019 Introduction and Background Consider a very natural problem: we are given a set of locations V = {v 1, v 2,..., v n }. We want

More information

Description of The Algorithm

Description of The Algorithm Description of The Algorithm Dijkstra s algorithm works by solving the sub-problem k, which computes the shortest path from the source to vertices among the k closest vertices to the source. For the dijkstra

More information

Pred 8 1. Dist. Pred

Pred 8 1. Dist. Pred CS Graph Algorithms, Page Shortest Path Problem Task: given a graph G, find the shortest path from a vertex u to a vertex v. ffl If all edges are of the same length, then BFS will do. ffl But some times

More information

Single Source Shortest Path

Single Source Shortest Path Single Source Shortest Path A directed graph G = (V, E) and a pair of nodes s, d is given. The edges have a real-valued weight W i. This time we are looking for the weight and the shortest path from s

More information

CS61BL. Lecture 5: Graphs Sorting

CS61BL. Lecture 5: Graphs Sorting CS61BL Lecture 5: Graphs Sorting Graphs Graphs Edge Vertex Graphs (Undirected) Graphs (Directed) Graphs (Multigraph) Graphs (Acyclic) Graphs (Cyclic) Graphs (Connected) Graphs (Disconnected) Graphs (Unweighted)

More information

CSE 101. Algorithm Design and Analysis Miles Jones Office 4208 CSE Building Lecture 5: SCC/BFS

CSE 101. Algorithm Design and Analysis Miles Jones Office 4208 CSE Building Lecture 5: SCC/BFS CSE 101 Algorithm Design and Analysis Miles Jones mej016@eng.ucsd.edu Office 4208 CSE Building Lecture 5: SCC/BFS DECOMPOSITION There is a linear time algorithm that decomposes a directed graph into its

More information

Directed Graphs. DSA - lecture 5 - T.U.Cluj-Napoca - M. Joldos 1

Directed Graphs. DSA - lecture 5 - T.U.Cluj-Napoca - M. Joldos 1 Directed Graphs Definitions. Representations. ADT s. Single Source Shortest Path Problem (Dijkstra, Bellman-Ford, Floyd-Warshall). Traversals for DGs. Parenthesis Lemma. DAGs. Strong Components. Topological

More information

Spanning Trees, greedy algorithms. Lecture 22 CS2110 Fall 2017

Spanning Trees, greedy algorithms. Lecture 22 CS2110 Fall 2017 1 Spanning Trees, greedy algorithms Lecture 22 CS2110 Fall 2017 1 We demo A8 Your space ship is on earth, and you hear a distress signal from a distance Planet X. Your job: 1. Rescue stage: Fly your ship

More information

An Early Problem in Graph Theory

An Early Problem in Graph Theory raphs Topic 2 " Hopefully, you've played around a bit with The Oracle of acon at Virginia and discovered how few steps are necessary to link just about anybody who has ever been in a movie to Kevin acon,

More information

CS420/520 Algorithm Analysis Spring 2009 Lecture 14

CS420/520 Algorithm Analysis Spring 2009 Lecture 14 CS420/520 Algorithm Analysis Spring 2009 Lecture 14 "A Computational Analysis of Alternative Algorithms for Labeling Techniques for Finding Shortest Path Trees", Dial, Glover, Karney, and Klingman, Networks

More information

Algorithms and Data Structures 2014 Exercises and Solutions Week 9

Algorithms and Data Structures 2014 Exercises and Solutions Week 9 Algorithms and Data Structures 2014 Exercises and Solutions Week 9 November 26, 2014 1 Directed acyclic graphs We are given a sequence (array) of numbers, and we would like to find the longest increasing

More information

CSE 373: Data Structures and Algorithms

CSE 373: Data Structures and Algorithms CSE 373: Data Structures and Algorithms Lecture 6: Dijkstra s Algorithm (Graphs) Instructor: Lilian de Greef Quarter: Summer 207 Today Announcements Graph Traversals Continued Remarks on DFS & BFS Shortest

More information

EXAMINATIONS 2011 Trimester 2, MID-TERM TEST. COMP103 Introduction to Data Structures and Algorithms SOLUTIONS

EXAMINATIONS 2011 Trimester 2, MID-TERM TEST. COMP103 Introduction to Data Structures and Algorithms SOLUTIONS T E W H A R E W Ā N A N G A O T E Ū P O K O O T E I K A A M Ā U I VUW V I C T O R I A UNIVERSITY OF WELLINGTON Student ID:....................... EXAMINATIONS 2011 Trimester 2, MID-TERM TEST COMP103 Introduction

More information

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

CS 561, Lecture 10. Jared Saia University of New Mexico CS 561, Lecture 10 Jared Saia University of New Mexico Today s Outline The path that can be trodden is not the enduring and unchanging Path. The name that can be named is not the enduring and unchanging

More information

Shortest Path Algorithm

Shortest Path Algorithm Shortest Path Algorithm What is the Shortest Path Problem? Is the shortest path problem well defined? The Dijkstra's Algorithm for Shortest Path Problem. Implementation Dijkstra's Algorithm What is the

More information

1. Introduction. Lecture Content

1. Introduction. Lecture Content Lecture 6 Queues 1 Lecture Content 1. Introduction 2. Queue Implementation Using Array 3. Queue Implementation Using Singly Linked List 4. Priority Queue 5. Applications of Queue 2 1. Introduction A queue

More information

CS 125 Section #6 Graph Traversal and Linear Programs 10/13/14

CS 125 Section #6 Graph Traversal and Linear Programs 10/13/14 CS 125 Section #6 Graph Traversal and Linear Programs 10/13/14 1 Depth first search 1.1 The Algorithm Besides breadth first search, which we saw in class in relation to Dijkstra s algorithm, there is one

More information