CS 206 Introduction to Computer Science II

Similar documents
CS 206 Introduction to Computer Science II

CS 206 Introduction to Computer Science II

CS 206 Introduction to Computer Science II

CSE 100: GRAPH ALGORITHMS

CS 206 Introduction to Computer Science II

CSE 100: GRAPH SEARCH

LECTURE 17 GRAPH TRAVERSALS

CS 206 Introduction to Computer Science II

Algorithms and Theory of Computation. Lecture 3: Graph Algorithms

Understand graph terminology Implement graphs using

Homework Assignment #3 Graph

The Shortest Path Problem

Chapter 14. Graphs Pearson Addison-Wesley. All rights reserved 14 A-1

CSC 172 Data Structures and Algorithms. Lecture 24 Fall 2017

CS350: Data Structures Dijkstra s Shortest Path Alg.

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

CS 206 Introduction to Computer Science II

CS 206 Introduction to Computer Science II

CS 206 Introduction to Computer Science II

Module 11: Additional Topics Graph Theory and Applications

CS 106X, Lecture 23 Dijkstra and A* Search

The ADT priority queue Orders its items by a priority value The first item removed is the one having the highest priority value

CS 206 Introduction to Computer Science II

Algorithm Design and Analysis

Graphs & Digraphs Tuesday, November 06, 2007

MA/CSSE 473 Day 12. Questions? Insertion sort analysis Depth first Search Breadth first Search. (Introduce permutation and subset generation)

Algorithm Design and Analysis

Mystery Algorithm! ALGORITHM MYSTERY( G = (V,E), start_v ) mark all vertices in V as unvisited mystery( start_v )

This is a set of practice questions for the final for CS16. The actual exam will consist of problems that are quite similar to those you have

Scribes: Romil Verma, Juliana Cook (2015), Virginia Williams, Date: May 1, 2017 and Seth Hildick-Smith (2016), G. Valiant (2017), M.

lecture27: Graph Traversals

Graph Traversals. CS200 - Graphs 1

Spanning Trees 4/19/17. Prelim 2, assignments. Undirected trees

Spanning Trees. Lecture 22 CS2110 Spring 2017

Depth-First Search Depth-first search (DFS) is another way to traverse the graph.

Graph Algorithms. Textbook reading. Chapter 3 Chapter 4. CSci 3110 Graph Algorithms 1/41

Fundamental Graph Algorithms Part Four

Graphs. The ultimate data structure. graphs 1

Graphs: Graph Data Structure:

CSE 100 Minimum Spanning Trees Prim s and Kruskal

Graphs - II. Announcements. Where did I leave that book? Where did I leave that book? Where did I leave that book? CS 2110, Fall 2016

csci 210: Data Structures Graph Traversals

Introduction to Graphs. CS2110, Spring 2011 Cornell University

CS 1114: Implementing Search. Last time. ! Graph traversal. ! Two types of todo lists: ! Prof. Graeme Bailey.

22.3 Depth First Search

Algorithm Design and Analysis

CSE 373: Data Structures and Algorithms

Spanning trees. Suppose you have a connected undirected graph

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

CS61BL. Lecture 5: Graphs Sorting

Graphs Data Structures

CS/COE 1501 cs.pitt.edu/~bill/1501/ Graphs

Graph Algorithms. Definition

Plan. CMPSCI 311: Introduction to Algorithms. Recall. Adjacency List Representation. DFS Descriptions. BFS Description

Graph Search Methods. Graph Search Methods

Graph Search Methods. Graph Search Methods

Graph: representation and traversal

Graphs - II CS 2110, Spring 2016

(Dijkstra s Algorithm) Consider the following positively weighted undirected graph for the problems below: 8 7 b c d h g f

Announcements Problem Set 4 is out!

CSE 373: Data Structures and Algorithms

Some major graph problems

Computer Science 385 Design and Analysis of Algorithms Siena College Spring Lab 8: Greedy Algorithms Due: Start of your next lab session

CS 310 Advanced Data Structures and Algorithms

CS/COE

CS171 Final Practice Exam

CS302 - Data Structures using C++

Outlines: Graphs Part-2

Graph Representations and Traversal

CSE373: Data Structures & Algorithms Lecture 28: Final review and class wrap-up. Nicki Dell Spring 2014

Spanning Trees, greedy algorithms. Lecture 22 CS2110 Fall 2017

EECS 281 Homework 4 Key Fall 2004

Outline. Graphs. Divide and Conquer.

CS 161 Lecture 11 BFS, Dijkstra s algorithm Jessica Su (some parts copied from CLRS) 1 Review

Computer Science 385 Design and Analysis of Algorithms Siena College Spring Topic Notes: Brute-Force Algorithms

R13. II B. Tech I Semester Supplementary Examinations, May/June DATA STRUCTURES (Com. to ECE, CSE, EIE, IT, ECC)

Graphs - II CS 2110, Spring 2016

csci 210: Data Structures Graph Traversals

Data Structures. 1. Each entry in a linked list is a called a (a)link (b)node (c)data structure (d)none of the above

Stacks CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2008

CSS 343 Data Structures, Algorithms, and Discrete Math II. Graphs II. Yusuf Pisan

Week 12: Minimum Spanning trees and Shortest Paths

COT 6405 Introduction to Theory of Algorithms

CS 106 Introduction to Computer Science I

Shortest Path Problem

CSC263 Week 8. Larry Zhang.

CS/COE

CS171 Final Practice Exam

TIE Graph algorithms

CS302 Data Structures using C++

Breadth First Search. cse2011 section 13.3 of textbook

12 Abstract Data Types

CS200: Graphs. Rosen Ch , 9.6, Walls and Mirrors Ch. 14

(Re)Introduction to Graphs and Some Algorithms

18: GRAPH DATA STRUCTURES. Introduction

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of MCA

DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI. Department of Computer Science and Engineering CS6301 PROGRAMMING DATA STRUCTURES II

Do you remember what is the most powerful tool for managing complexity?

Algorithms and Data Structures Lesson 4

Lecture 24 Notes Search in Graphs

Transcription:

CS 206 Introduction to Computer Science II 04 / 13 / 2018 Instructor: Michael Eckmann

Today s Topics Questions? Comments? Graphs Depth First Search (DFS) Shortest path Shortest weight path (Dijkstra's algorithm) Michael Eckmann - Skidmore College - CS 206 - Spring 2018

Graph traversal Depth first search (DFS) Pick a vertex at which to start Visit one of its adjacent vertices then visit one of that one's adjacent vertices, and so on until there is no unvisited adjacent vertex of the one we're working on. Then backtrack one level and visit another adjacent vertex from that one and repeat. Do this until we're at the start vertex and there's no more unvisited adjacent vertices Do not visit a vertex more than once Only vertices that are reachable from the start vertex will be visited Those vertices that are adjacent to a vertex can be visited in any order. Example of DFS on the board.

Recall that the BFS used a Queue. DFS Any thoughts on how DFS could be implemented? What data structure allows us to backtrack?

DFS set all vertices to UNVISITED push start vertex visit start vertex - set start vertex to visited while (stack is not empty) peek to get vertex at top of stack try to get an unvisited adjacent vertex to the peeked one if there isn't one, pop else push it visit it - set it to visited

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?

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

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 Process the graph in a BFS order starting at the given vertex but when visit a node, also replace its length with the current length. Example on the board This is just BFS while also keeping track of path lengths.

Example on the board and then pseudocode for Dijkstra's 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) > (pathlen from startv to v + weight of the edge from v to adjv) 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=adjv, 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.)