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.

Slides:



Advertisements
Similar presentations
CS203 Lecture 15.
Advertisements

Graphs CSC 220 Data Structure. Introduction One of the Most versatile data structures like trees. Terminology –Nodes in trees are vertices in graphs.
Graphs and Digraphs Chapter 14.
Graph Search Methods A vertex u is reachable from vertex v iff there is a path from v to u
Graphs Chapter Chapter Contents Some Examples and Terminology Road Maps Airline Routes Mazes Course Prerequisites Trees Traversals Breadth-First.
Graph Search Methods A vertex u is reachable from vertex v iff there is a path from v to u
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 27 Graphs and Applications.
Abstract Data Types and Subprograms
Breadth-First and Depth-First Search
Chapter 20: Graphs CS Data Structures Mehmet H Gunes Modified from authors’ slides.
Abstract Data Types and Subprograms
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.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 excerpts Graphs (breadth-first-search)
CS 206 Introduction to Computer Science II 11 / 07 / 2008 Instructor: Michael Eckmann.
Graphs By JJ Shepherd. Introduction Graphs are simply trees with looser restrictions – You can have cycles Historically hard to deal with in computers.
Algoritma Traversal Graph. Graph Traversal Algorithms In general, graphs do not have a vertex, like a root, that initiates unique paths to each of the.
Chapter 8 Abstract Data Types and Subprograms. 2 Chapter Goals Distinguish between an array-based visualization and a linked visualization Distinguish.
Graphs CS3240, L. grewe.
Graph.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Graphs.
Graphs. Graph definitions There are two kinds of graphs: directed graphs (sometimes called digraphs) and undirected graphs Birmingham Rugby London Cambridge.
Breadth First Search (BFS) Part 2 COMP171. Graph / Slide 2 Shortest Path Recording * BFS we saw only tells us whether a path exists from source s, 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.
Preorder Traversal with a Stack Push the root onto the stack. While the stack is not empty n pop the stack and visit it.
Computer Science 112 Fundamentals of Programming II Graph Algorithms.
Search Related Algorithms. Graph Code Adjacency List Representation:
Chapter 14 Graphs. © 2004 Pearson Addison-Wesley. All rights reserved Terminology G = {V, E} A graph G consists of two sets –A set V of vertices,
Chapter 9 Priority Queues, Heaps, and Graphs. 2 Goals Describe a priority queue at the logical level and implement a priority queue as a list Describe.
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.
Graphs. Made up of vertices and arcs Digraph (directed graph) –All arcs have arrows that give direction –You can only traverse the graph in the direction.
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.
Chapter 9 Priority Queues, Heaps, Graphs 1 Fall 2010.
Trees : Part 1 Section 4.1 (1) Theory and Terminology (2) Preorder, Postorder and Levelorder Traversals.
Graphs. Graphs Similar to the graphs you’ve known since the 5 th grade: line graphs, bar graphs, etc., but more general. Those mathematical graphs are.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
Chapter 8 Abstract Data Types and Subprograms. 2 Chapter Goals Distinguish between an array-based visualization and a linked visualization Distinguish.
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
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
Breadth-first and depth-first traversal CS1114
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.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 22 Graphs and Applications.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
© 2006 Pearson Addison-Wesley. All rights reserved14 B-1 Chapter 14 (continued) Graphs.
Chapter 9 Priority Queues, Heaps, and Graphs
Graphs Chapter 15 introduces graphs which are probably the most general and commonly-used data structure. This lecture introduces heaps, which are used.
Chapter 28 Graphs and Applications
A vertex u is reachable from vertex v iff there is a path from v to u.
Csc 2720 Instructor: Zhuojun Duan
Priority Queues, Heaps, and Graphs
CSC 172 DATA STRUCTURES.
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Graphs.
Graph Traversals Depth-First Traversals. Algorithms. Example.
Yan Shi CS/SE 2630 Lecture Notes
Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013
COMP171 Depth-First Search.
Graph Implementation.
Chapter 10 The Graph ADT.
Graphs.
Graphs.
Graphs.
Presentation transcript:

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 rather than an innate operation. The basic operations given in our specification allow us to implement different traversals independent of how the graph itself is actually implemented.

Graph Traversal We look at two types of graph traversal: –The strategy of going down a branch to its deepest point and moving up is called a depth-first strategy. –Another systematic way to visit each vertex in a tree is to visit each vertex on level 0 (the root), then each vertex on level 1, then each vertex on level 2, and so on. Visiting each vertex by level in this way is called a breadth-first strategy. We discuss algorithms for employing both strategies within the context of determining if two cities are connected in our airline example.

Can we get from Austin to Washington?

Depth first search - erroneous IsPathDF (graph, startVertex, endVertex): returns boolean set found to false create a new stack push startVertex onto the stack while stack is not empty AND found is false pop a vertex from the stack if vertex equals endVertex set found to true else push all adjacent vertices onto stack return found This approach could result in an infinite loop!

Depth first search - corrected IsPathDF (graph, startVertex, endVertex): returns boolean set found to false create a new stack clear all marks on the graph push startVertex onto the stack while stack is not empty AND found is false pop a vertex from the stack if vertex equals endVertex set found to true else if vertex is not marked mark vertex push all adjacent vertices onto stack return found

Breadth first search – use queue IsPathBF (graph, startVertex, endVertex): returns boolean set found to false create a new queue clear all marks on the graph add startVertex to the queue while queue is not empty AND found is false remove a vertex from the queue if vertex equals endVertex set found to true else if vertex is not marked mark vertex add all adjacent vertices to the queue return found

The Single-Source Shortest- Paths Algorithm An algorithm that displays the shortest path from a designated starting city to every other city in the graph In our example graph if the starting point is Washington we should get Last Vertex Destination Distance Washington Washington 0 Washington Atlanta 600 Washington Dallas 1300 Atlanta Houston 1400 Dallas Austin 1500 Dallas Denver 2080 Dallas Chicago 2200

ShortestPaths algorithm shortestPaths(graph, startVertex): returns QueueInterface clear all marks from the graph create a new result queue of Flight objects create a new vertex queue of String objects create a new priority queue of Flight objects create a new flight(startVertex, startVertex, 0) add the flight to the priority queue while the priority queue is not empty grab (remove) a flight from the priority queue if flight.getToVertex is not marked mark flight.getToVertex add the flight to the result queue flight.setFromVertex(flight.getToVertex()) set minDistance to flight.getDistance() set vertex queue to vertices adjacent from flight.getFromVertex() while more vertices in vertex queue get next vertex from vertex queue if vertex not marked flight.setToVertex(vertex) flight.setDistance(minDistance + graph.weightIs(flight.getFromVertex(), vertex)) add the flight to the priority queue return the result queue But there is a problem down here!

Notes The algorithm for the shortest-path traversal is similar to those we used for the depth-first and breadth-first searches, but there are three major differences: –We use a priority queue rather than a FIFO queue or stack –We stop only when there are no more cities to process; there is no destination –It is incorrect if we use a reference-based priority queue improperly!

The Incorrect Part of the Algorithm while more vertices in vertex queue get next vertex from vertex queue if vertex not marked flight.setToVertex(vertex) flight.setDistance(minDistance + graph.weightIs(flight.getFromVertex(), vertex)) add flight to the priority queue This part of the algorithm walks through the queue of vertices adjacent to the current vertex, and adds Flights objects onto the priority queue pq based on the information. The flight variable is actually a reference to a Flights object. Suppose the queue of adjacent vertices has information in it related to the cities Atlanta and Houston. The first time through this loop we insert information related to Atlanta in flight and add it in pq. But the next time through the loop we make changes to the Flights object referenced by flight. We update it to contain information about Houston. And we again add it in pq. So now pq loses the information it had about Atlanta.

Correcting the Algorithm while more vertices in vertex queue get next vertex from vertex queue if vertex not marked set newDistance to minDistance + graph.weightIs(flight.getFromVertex(), vertex) create newFlight(flight.getFromVertex(), vertex, newDistance) add newFlight to the priority queue

Unreachable Vertices With this new graph we cannot fly from Washington to Austin, Chicago, Dallas, or Denver