CS 206 Introduction to Computer Science II 04 / 01 / 2009 Instructor: Michael Eckmann.

Slides:



Advertisements
Similar presentations
Problem solving with graph search
Advertisements

Single Source Shortest Paths
CSE 390B: Graph Algorithms Based on CSE 373 slides by Jessica Miller, Ruth Anderson 1.
CSC 2300 Data Structures & Algorithms April 13, 2007 Chapter 9. Graph Algorithms.
The Greedy Approach Chapter 8. The Greedy Approach It’s a design technique for solving optimization problems Based on finding optimal local solutions.
1 Minimum Spanning Tree Prim-Jarnik algorithm Kruskal algorithm.
CS 206 Introduction to Computer Science II 03 / 04 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 23 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 22 / 2008 Instructor: Michael Eckmann.
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.
CS 206 Introduction to Computer Science II 11 / 07 / 2008 Instructor: Michael Eckmann.
CSCI 3160 Design and Analysis of Algorithms Tutorial 2 Chengyu Lin.
Shortest Paths and Dijkstra's Algorithm CS 110: Data Structures and Algorithms First Semester,
CS 206 Introduction to Computer Science II 11 / 11 / Veterans Day Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 06 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 31 / 2008 Happy Halloween!!! Instructor: Michael Eckmann.
CS2420: Lecture 37 Vladimir Kulyukin Computer Science Department Utah State University.
CS 206 Introduction to Computer Science II 11 / 04 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 20 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 10 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 09 / 17 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 20 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 14 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 12 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 03 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 26 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 29 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 16 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 25 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 12 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 09 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 16 / 2009 Instructor: Michael Eckmann.
CSC 2300 Data Structures & Algorithms April 3, 2007 Chapter 9. Graph Algorithms.
CS 206 Introduction to Computer Science II 09 / 19 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 02 / 25 / 2009 Instructor: Michael Eckmann.
The Shortest Path Problem
CS 206 Introduction to Computer Science II 10 / 28 / 2009 Instructor: Michael Eckmann.
1 Shortest Path Calculations in Graphs Prof. S. M. Lee Department of Computer Science.
CS 146: Data Structures and Algorithms July 21 Class Meeting
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Graph Algorithms Shortest-Path.
Computer Science 112 Fundamentals of Programming II Graph Algorithms.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
1 Chapter 28 Weighted Graph Applications. 2 Objectives F To represent weighted edges using adjacency matrices and priority queues (§28.2). F To model.
Dijkstras Algorithm Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path problem.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 28 Weighted Graph.
SPANNING TREES Lecture 21 CS2110 – Spring
COSC 2007 Data Structures II Chapter 14 Graphs III.
Data Structures and Algorithms Ver. 1.0 Session 17 Objectives In this session, you will learn to: Implement a graph Apply graphs to solve programming problems.
CS 206 Introduction to Computer Science II 02 / 23 / 2009 Instructor: Michael Eckmann.
Shortest Path in Weighted Graph : Dijkstra’s Algorithm.
Dijkstra’s Algorithm: single source shortest paths David Kauchak cs62 Spring 2010.
CS 206 Introduction to Computer Science II 11 / 16 / 2009 Instructor: Michael Eckmann.
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.
SPANNING TREES Lecture 20 CS2110 – Fall Spanning Trees  Definitions  Minimum spanning trees  3 greedy algorithms (incl. Kruskal’s & Prim’s)
SPANNING TREES Lecture 21 CS2110 – Fall Nate Foster is out of town. NO 3-4pm office hours today!
Spanning Trees Dijkstra (Unit 10) SOL: DM.2 Classwork worksheet Homework (day 70) Worksheet Quiz next block.
Graphs – Part III CS 367 – Introduction to Data Structures.
Short paths and spanning trees
Graphs Chapter 11 Objectives Upon completion you will be able to:
CSE 373: Data Structures and Algorithms
Minimum Spanning Tree Algorithms
Shortest Path Algorithm for Weighted Non-negative Undirected Graphs
CSE 417: Algorithms and Computational Complexity
Dijkstra's Shortest Path Algorithm
Richard Anderson Spring 2016
Graphs: Shortest path and mst
More Graphs Lecture 19 CS2110 – Fall 2009.
Presentation transcript:

CS 206 Introduction to Computer Science II 04 / 01 / 2009 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS Spring 2009 Today’s Topics Questions? Comments? Graphs –Shortest path (for unwieghted graphs)‏ –Dijkstra's algorithm (solve minimum weight path w/ weighted graph – all edge weights are non-negative)‏

Graphs Shortest path algorithms –problem is to find the shortest path from one given vertex to each of the other vertices. –output is a list of paths from given vertex to all other vertices –what real world examples might ever want to find the shortest path?

Graphs Shortest path algorithms –problem is to find the shortest path from one given vertex to each of the other vertices. –output is a list of paths from given vertex to all other vertices –the shortest path could be in terms of path length (number of edges between vertices e.g. a direct flight has path length 1, flights with connecting flights have path length > 1 –the shortest path could be in terms of minimum weight for weighted graphs (example on the board.)‏ e.g. finding the lowest cost flights Dijkstra's algorithm solves this problem

Graphs the shortest path could be in terms of path length (number of edges between vertices) e.g. a direct flight has path length 1, flights with connecting flights have path length > 1 –Initialize all lengths to infinity –Can process the graph in a BFS starting at the given vertex –When visit a node, replace it's length with the current length. –The BFS uses a queue. –Let's implement this.

Graphs the shortest path could be in terms of minimum weight for weighted graphs (note well: weights are non-negative)‏ e.g. finding the lowest cost flights Dijkstra's algorithm solves this problem –It attempts to minimize the weight at each step. Dijkstra's algorithm is a greedy algorithm. That is, its strategy is to locally minimize the weight, hoping that's the best way to get the minimum weight of the whole graph. –Sometimes the local minimum weight is not the correct choice for the overall problem. In that case, the algorithm will still work, but the initial guess was wrong. –Dijkstra's algorithm works in a similar way to BFS but instead of a queue, use a “minimum” priority queue. That is, a priority queue that when we dequeue an item, we get the item whose priority is least among the items in the priority queue. –Let's see an example on the board and come up with pseudocode for this algorithm.

Graphs Example on the board and then pseudocode for this algorithm. vi means vertex i, and (j) means weight j v0-> v1(2), v3(1)‏ v1-> v3(3), v4(10)‏ v2-> v0(4), v5(5)‏ v3-> v2(2), v4(2), v5(8), v6(4)‏ v4-> v6(6)‏ v5-> null v6-> v5(1)‏ Dijkstra's algorithm, given a starting vertex will find the minimum weight paths from that starting vertex to all other vertices.

We can reuse the code we wrote to set vertices as visited or unvisited. We can reuse our code to handle a directed graph, but we must add the ability for it to be a weighted graph. We need a “minimum” Priority Queue, that is, one that returns the item with the lowest priority at any given dequeue(). We need a way to store all the path lengths. Also, we need to initially set all the minimum path lengths to Integer.MAX_VALUE (this is the initial value we want to use for the path lengths, because if we ever calculate a lesser weight path, then we store this lesser weight path.)‏

Dijkstra's algorithm pseudocode (given a startV)‏ set all vertices to unvisited and all to have pathLen MAX set pathLen from startV to startV to be 0 add (item=startV, priority=0) to PQ while (PQ !empty) { v = remove the lowest priority vertex from PQ (do this until we get an unvisited vertex out)‏ set v to visited for all unvisited adjacent vertices (adjV) to v { if ( current pathLen from startV to adjV ) > ( weight of the edge from v to adjV + pathLen from startV to v ) then { set adjV's pathLen from startV to adjV to be weight of the edge from v to adjV + pathLen from startV to v add (item=v, priority=pathLen just calculated) to PQ } // end if } // end for } // end while

What if we wanted to not only display the minimum weight path to each vertex, but also the actual path (with the intervening vertices)? e.g. Minimum weight from v0 to v5 is 8, and the path is: v0 to v3 to v2 to v5

What if we wanted to not only display the minimum weight path to each vertex, but also the actual path (with the intervening vertices)? Notice that the minimum path from v0 to v3 is: v0 to v3 with a weight of 1 Notice that the minimum path from v0 to v2 is: v0 to v3 to v2 with a weight of 3 Notice that the minimum path from v0 to v5 is: v0 to v3 to v2 to v5 with a weight of 8 See how all these just extend each other by one more edge. For example if we end up at v5 from v2, we get to v2 the same way we would have gotten to v2 (w/ the minimum weight.)‏