CS 261 Reachability Problems. The classic Reachability algorithm findReachable (graph g, vertex start) { create a set of reachable vertices, initially.

Slides:



Advertisements
Similar presentations
Jecho and Donatus Depth First and Breadth First Search.
Advertisements

Algorithms (and Datastructures) Lecture 3 MAS 714 part 2 Hartmut Klauck.
Problem solving with graph search
CSE 390B: Graph Algorithms Based on CSE 373 slides by Jessica Miller, Ruth Anderson 1.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
CS261 Data Structures Single Source Reachability - Edge List Representation.
1 Dijkstra’s Minimum-Path Algorithm Minimum Spanning Tree CSE Lectures 20 – Intro to Graphs.
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.
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.
Data Structure and Algorithms (BCS 1223) GRAPH. Introduction of Graph A graph G consists of two things: 1.A set V of elements called nodes(or points or.
Graphs By JJ Shepherd. Introduction Graphs are simply trees with looser restrictions – You can have cycles Historically hard to deal with in computers.
State Space Search Algorithms CSE 472 Introduction to Artificial Intelligence Autumn 2003.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
CS2420: Lecture 37 Vladimir Kulyukin Computer Science Department Utah State University.
CS 261 – Data Structures Graphs. Used in a variety of applications and algorithms Graphs represent relationships or connections Superset of trees (i.e.,
CS 206 Introduction to Computer Science II 11 / 10 / 2008 Instructor: Michael Eckmann.
Using Search in Problem Solving
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 / 12 / 2008 Instructor: Michael Eckmann.
Ch 16. Case Study Containers Timothy Budd Oregon State University.
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.
Blind Search-Part 2 Ref: Chapter 2. Search Trees The search for a solution can be described by a tree - each node represents one state. The path from.
Using Search in Problem Solving
Fall 2007CS 2251 Graphs Chapter 12. Fall 2007CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs To.
Graphs & Graph Algorithms Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
CISC220 Fall 2009 James Atlas Nov 13: Graphs, Line Intersections.
CS261 Data Structures DFS and BFS – Edge List Representation.
Shortest Path Algorithms. Kruskal’s Algorithm We construct a set of edges A satisfying the following invariant:  A is a subset of some MST We start with.
Computer Science 112 Fundamentals of Programming II Graph Algorithms.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Tree Searching Breadth First Search Dept First Search.
CS261 – Data Structures Graphs. Goals Introduction and Motivation Representations.
Representing and Using Graphs
Introduction to search Chapter 3. Why study search? §Search is a basis for all AI l search proposed as the basis of intelligence l inference l all learning.
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.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Common final examinations When: Wednesday, 12/11, 3:30-5:30 PM Where: Ritter Hall - Walk Auditorium 131 CIS 1166 final When: Wednesday, 12/11, 5:45-7:45.
Dijkstra’s Algorithm: single source shortest paths David Kauchak cs62 Spring 2010.
CS261 Data Structures Dijkstra’s Algorithm – Edge List Representation.
CS 206 Introduction to Computer Science II 11 / 16 / 2009 Instructor: Michael Eckmann.
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.
COSC 2007 Data Structures II
A* Path Finding Ref: A-star tutorial.
Main Index Contents 11 Main Index Contents Graph Categories Graph Categories Example of Digraph Example of Digraph Connectedness of Digraph Connectedness.
Section 9.6 Graph Applications. 9.6 Graph Applications Our graph specification does not include traversal operations. We treat traversal as a graph application/algorithm.
Graphs Chapter 15 introduces graphs which are probably the most general and commonly-used data structure. This lecture introduces heaps, which are used.
BackTracking CS255.
CSC317 Shortest path algorithms
CS 106B Homework 7: Trailblazer
Lecture 12 Graph Algorithms
Common final examinations
Graphs Chapter 13.
Graphs Chapter 11 Objectives Upon completion you will be able to:
A* Path Finding Ref: A-star tutorial.
Outline This topic covers Prim’s algorithm:
Shortest Path Algorithms
Lecture 13 Algorithm Analysis
Lecture 13 Algorithm Analysis
Graphs Part 2 Adjacency Matrix
Subgraphs, Connected Components, Spanning Trees
Lecture 13 Algorithm Analysis
Chapter 16 1 – Graphs Graph Categories Strong Components
CSE 417: Algorithms and Computational Complexity
Graphs.
Graphs.
Graphs.
Graphs.
Presentation transcript:

CS 261 Reachability Problems

The classic Reachability algorithm findReachable (graph g, vertex start) { create a set of reachable vertices, initially empty. call this r. create a container for vertices known to be reachable. call this c add start vertex to container c while the container c is not empty { remove first entry from the container c, assign to v if v is not already in the set of reachable vertices r { add v to the reachable set r add the neighbors of v to the container c } return r }

Different containers different algorithms What is interesting about this algorithm is that if you use different containers You get different algorithms

Example Solving a Maze

Represented as a graph

Use a Stack: { 1 }

Use a Stack: { 2, 6 }

Use a Stack: { 7, 3, 6 }

Use a Stack: { 3, 6 } dead end

Try alternative: { 4, 8, 6 }

Keep plowing on: { 5, 9, 8, 6 }

Keep on: { 10, 9, 8, 6 }

Keep on: { 15, 9, 8, 6 }

Keep on: { 14, 20, 9, 8, 6 }

Note duplicate: { 9, 20, 9, 8, 6 }

Opps, 4 again: { 4, 20, 9, 8, 6 }

Pop, try alternative: { 20, 9, 8, 6 }

Keep going: { 19, 9, 8, 6 }

And going: { 24, 9, 8, 6 }

Finished: { 25, 9, 8, 6 }

Depth first search Keep going in one direction as long as possible When you get stuck, back up to last choice, try alternative Did anybody do a Corn Maze last fall? Probably used this technique.

What if we use a queue?

Initial queue: {1}

First neighbors: {2, 6}

Neighbors of 2: {6, 3, 7}

Neighbors of 6: {3, 7, 11}

Neighbors of 3: {7, 11, 4, 8}

Neighbors of 7: {11, 4, 8}

Neighbors of 11: {4, 8, 12, 16}

Neighbors of 4: {8, 12, 16, 5, 9}

Breadth-first search Notice how this search jumps all over the place It’s - you go that way, I’ll go this way (but with a very large group of friends) Or think about spilling ink at the start, and watching it seep through the entire maze

Questions you can ask Which is faster? Which is guaranteed to find a solution? What if the graph is infinite, but there is a finite solution - which is guaranteed to find a solution?

But there is more! If we have a weighted graph, and use a priority queue - then the same technique is called Dijkstra’s algorithm Idea: queue keeps shortest distance from some place you have been to someplace you might not have been

Essence of Dijkstra’s algorithm! Idea: queue keeps shortest distance from some place you have been to someplace you might not have been When you pull an item from the queue, if it is someplace you have seen, toss it out, If it is new, add to destinations, put neighbors into queue

What about Weighted Graphs? Pendleton Pierre Pensacola Princeton Pittsburgh Peoria Pueblo Phoenix Dijkstra’s algorithm. Use a priority queue instead of a stack. Return a map of city, distance pairs. Pqueue orders values on shortest distance Dijkstra (String startCity, Map[String, Map[String, double]] distances) Make empty map of distances into variable reachable Put (StartingCity, 0) into Pqueue While Pqueue not empty pull new city from queue, if not ready in reachable, add to reachable add neighbors to queue, adding weight to distance from starting city When done with loop, return reachable map

Example: What is the distance from Pierre Pendleton Pierre Pensacola Princeton Pittsburgh Peoria Pueblo Phoenix Pierre: Dijkstra (String startCity, Map[String, Map[String, double]] distances) Make empty map of distances into variable reachable Put (StartingCity, 0) into Pqueue While Pqueue not empty pull new city from queue, if not ready in reachable, add to reachable add neighbors to queue, adding weight to distance from starting city When done with loop, return reachable map

Example: What is the distance from Pierre Pendleton Pierre Pensacola Princeton Pittsburgh Peoria Pueblo Phoenix Pierre: Pendeleton: Dijkstra (String startCity, Map[String, Map[String, double]] distances) Make empty map of distances into variable reachable Put (StartingCity, 0) into Pqueue While Pqueue not empty pull new city from queue, if not ready in reachable, add to reachable add neighbors to queue, adding weight to distance from starting city When done with loop, return reachable map

Example: What is the distance from Pierre Pendleton Pierre Pensacola Princeton Pittsburgh Peoria Pueblo Phoenix Pierre: 0, Pendleton: Phoenix: 6, Pueblo: 10 Notice how the distances have been added Dijkstra (String startCity, Map[String, Map[String, double]] distances) Make empty map of distances into variable reachable Put (StartingCity, 0) into Pqueue While Pqueue not empty pull new city from queue, if not ready in reachable, add to reachable add neighbors to queue, adding weight to distance from starting city When done with loop, return reachable map

Example: What is the distance from Pierre Pendleton Pierre Pensacola Princeton Pittsburgh Peoria Pueblo Phoenix Pierre: 0, Pendleton: 2, Phoenix: Pueblo: 9, Peoria: 10, Pueblo: 10, Pittsburgh: 16 Notice how values are stored in the Pqueue in distance order Dijkstra (String startCity, Map[String, Map[String, double]] distances) Make empty map of distances into variable reachable Put (StartingCity, 0) into Pqueue While Pqueue not empty pull new city from queue, if not ready in reachable, add to reachable add neighbors to queue, adding weight to distance from starting city When done with loop, return reachable map

Example: What is the distance from Pierre Pendleton Pierre Pensacola Princeton Pittsburgh Peoria Pueblo Phoenix Pierre: 0, Pendleton: 2, Phoenix: 6, Pueblo: Peoria: 10, Pueblo: 10, Pierre: 13, Pittsburgh: 16 Pierre gets put in queue, although it is known to be reachable Dijkstra (String startCity, Map[String, Map[String, double]] distances) Make empty map of distances into variable reachable Put (StartingCity, 0) into Pqueue While Pqueue not empty pull new city from queue, if not ready in reachable, add to reachable add neighbors to queue, adding weight to distance from starting city When done with loop, return reachable map

Example: What is the distance from Pierre Pendleton Pierre Pensacola Princeton Pittsburgh Peoria Pueblo Phoenix Pierre: 0, Pendleton: 2, Phoenix: 6, Pueblo: 9, Peoria: Pueblo: 10, Pierre: 13, Pueblo: 13, Pittsburgh: 15, Pittsburgh: 16 Duplicates only removed when pulled out of queue Dijkstra (String startCity, Map[String, Map[String, double]] distances) Make empty map of distances into variable reachable Put (StartingCity, 0) into Pqueue While Pqueue not empty pull new city from queue, if not ready in reachable, add to reachable add neighbors to queue, adding weight to distance from starting city When done with loop, return reachable map

Depth first reachability, Breadth first Reachability, Dijkstras Algorithm All three are essentially the same algorithm The only difference is the type of container they use

Bottom line So the key idea from this course is Selecting appropriate data structures is the key to programming You need to know what tools are available to make the best choices And with that, we end this course