CS 206 Introduction to Computer Science II 11 / 10 / 2008 Instructor: Michael Eckmann.

Slides:



Advertisements
Similar presentations
Chapter 9: Graphs Shortest Paths
Advertisements

CS 206 Introduction to Computer Science II 04 / 01 / 2009 Instructor: Michael Eckmann.
CSE 390B: Graph Algorithms Based on CSE 373 slides by Jessica Miller, Ruth Anderson 1.
Topological Sort Topological sort is the list of vertices in the reverse order of their finishing times (post-order) of the depth-first search. Topological.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
Graph Search Methods A vertex u is reachable from vertex v iff there is a path from v to u
Graph Search Methods A vertex u is reachable from vertex v iff there is a path from v to u
Graphs Chapter 12. Chapter Objectives  To become familiar with graph terminology and the different types of graphs  To study a Graph ADT and different.
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.
Graph Search Methods Spring 2007 CSE, POSTECH. Graph Search Methods A vertex u is reachable from vertex v iff there is a path from v to u. A search method.
CS 206 Introduction to Computer Science II 11 / 07 / 2008 Instructor: Michael Eckmann.
Graphs Graphs are the most general data structures we will study in this course. A graph is a more general version of connected nodes than the tree. Both.
Graphs By JJ Shepherd. Introduction Graphs are simply trees with looser restrictions – You can have cycles Historically hard to deal with in computers.
CS 206 Introduction to Computer Science II 11 / 11 / Veterans Day Instructor: Michael Eckmann.
Data Structures & Algorithms Graph Search Richard Newman based on book by R. Sedgewick and slides by S. Sahni.
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.
CS 206 Introduction to Computer Science II 10 / 31 / 2008 Happy Halloween!!! Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 09 / 17 / 2008 Instructor: Michael Eckmann.
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
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.
Graphs. Graphs Many interesting situations can be modeled by a graph. Many interesting situations can be modeled by a graph. Ex. Mass transportation system,
CS 206 Introduction to Computer Science II 11 / 05 / 2008 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.
Fall 2007CS 2251 Graphs Chapter 12. Fall 2007CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs To.
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.
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.
CS 206 Introduction to Computer Science II 09 / 19 / 2008 Instructor: Michael Eckmann.
CS261 Data Structures DFS and BFS – Edge List Representation.
Graphs CS 400/600 – Data Structures. Graphs2 Graphs  Used to represent all kinds of problems Networks and routing State diagrams Flow and capacity.
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
Dijkstras Algorithm Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path problem.
Lecture 12-2: Introduction to Computer Algorithms beyond Search & Sort.
SPANNING TREES Lecture 21 CS2110 – Spring
Minimum Spanning Trees
COSC 2007 Data Structures II Chapter 14 Graphs III.
MA/CSSE 473 Day 15 BFS Topological Sort Combinatorial Object Generation Intro.
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.
Graph Traversal BFS & DFS. Review of tree traversal methods Pre-order traversal In-order traversal Post-order traversal Level traversal a bc d e f g hi.
Graphs. Definitions A graph is two sets. A graph is two sets. –A set of nodes or vertices V –A set of edges E Edges connect nodes. Edges connect nodes.
COMP261 Lecture 6 Dijkstra’s Algorithm. Connectedness Is this graph connected or not? A Z FF C M N B Y BB S P DDGG AA R F G J L EE CC Q O V D T H W E.
CS 206 Introduction to Computer Science II 11 / 16 / 2009 Instructor: Michael Eckmann.
SPANNING TREES Lecture 20 CS2110 – Fall Spanning Trees  Definitions  Minimum spanning trees  3 greedy algorithms (incl. Kruskal’s & Prim’s)
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
1 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency.
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
SPANNING TREES Lecture 21 CS2110 – Fall Nate Foster is out of town. NO 3-4pm office hours today!
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
Graph Searching CSIT 402 Data Structures II. 2 Graph Searching Methodology Depth-First Search (DFS) Depth-First Search (DFS) ›Searches down one path as.
Graph Theory Def: A graph is a set of vertices and edges G={V,E} Ex. V = {a,b,c,d,e} E = {ab,bd,ad,ed,ce,cd} Note: above is a purely mathematical definition.
Spanning Trees Dijkstra (Unit 10) SOL: DM.2 Classwork worksheet Homework (day 70) Worksheet Quiz next block.
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.
Brute Force and Exhaustive Search Brute Force and Exhaustive Search Traveling Salesman Problem Knapsack Problem Assignment Problem Selection Sort and Bubble.
Graphs – Part III CS 367 – Introduction to Data Structures.
A vertex u is reachable from vertex v iff there is a path from v to u.
Short paths and spanning trees
Graphs Chapter 11 Objectives Upon completion you will be able to:
A* Path Finding Ref: A-star tutorial.
Graphs Part 2 Adjacency Matrix
Minimum Spanning Tree Algorithms
CSE 373: Data Structures and Algorithms
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Graphs: Shortest path and mst
INTRODUCTION A graph G=(V,E) consists of a finite non empty set of vertices V , and a finite set of edges E which connect pairs of vertices .
More Graphs Lecture 19 CS2110 – Fall 2009.
Presentation transcript:

CS 206 Introduction to Computer Science II 11 / 10 / 2008 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS Fall 2008 Today’s Topics Questions? Graphs –Traversals Depth First Search Shortest Path algorithms

Graphs You implemented a graph as an adjacency matrix in lab yesterday. Let's look at my Graph class that implements it with an adjacency matrix.

Graphs Graph traversal –Depth first search (DFS)‏ Pick a vertex at which to start Visit one of it's 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.

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, using our BFS code.

Graphs the shortest path could be in terms of minimum weight for weighted graphs (note: 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 returns an 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. 0-> 1(2), 3(1)‏ 1-> 3(3), 4(10)‏ 2-> 0(4), 5(5)‏ 3-> 2(2),4(2),5(8),6(4)‏ 4-> 6(6)‏ 5-> null 6-> 5(1)‏ Dijkstra's algorithm, given a starting vertex will find the minimum weight paths from that starting vertex to all other vertices.

We need code to handle a weighted, directed graph. We need a “minimum” Priority Queue, that is, one that returns the item with the lowest priority at any given remove(). We need a way to 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