Copyright ©2012 by Pearson Education, Inc. All rights reserved

Slides:



Advertisements
Similar presentations
Graphs and Digraphs Chapter 14.
Advertisements

CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
Graphs Chapter Chapter Contents Some Examples and Terminology Road Maps Airline Routes Mazes Course Prerequisites Trees Traversals Breadth-First.
Graphs. 2 Some Examples and Terminology A graph is a set of vertices (nodes) and a set of edges (arcs) such that each edge is associated with exactly.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 27 Graphs and Applications.
Graphs Chapter 12. Chapter Objectives  To become familiar with graph terminology and the different types of graphs  To study a Graph ADT and different.
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Chapter 8, Part I Graph Algorithms.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 27 Graph Applications.
Graphs Chapter 30 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 excerpts Graphs (breadth-first-search)
Data Structures Using C++
CS 206 Introduction to Computer Science II 11 / 11 / Veterans Day Instructor: Michael Eckmann.
ITEC200 – Week 12 Graphs. 2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study.
Graphs Chapter 30 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Graphs CS-240/341. Uses for Graphs computer networks and routing airline flights geographic maps course prerequisite structures tasks for completing a.
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs.
Fall 2007CS 2251 Graphs Chapter 12. Fall 2007CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs To.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
CISC220 Fall 2009 James Atlas Nov 13: Graphs, Line Intersections.
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
Graph Implementations Chapter 29 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Graphs Chapter 28 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Graph Implementations Chapter 31 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Graphs CS 400/600 – Data Structures. Graphs2 Graphs  Used to represent all kinds of problems Networks and routing State diagrams Flow and capacity.
1 Abstract Data Type We have discussed: List Tree Today we will talk about Graph.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 Graphs.
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,
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
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.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 30 Graphs and Applications.
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.
Graphs Chapter Chapter Contents Some Examples and Terminology Road Maps Airline Routes Mazes Course Prerequisites Trees Traversals Breadth-First.
Graphs A graphs is an abstract representation of a set of objects, called vertices or nodes, where some pairs of the objects are connected by links, called.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
Trees, Binary Search Trees, Balanced Trees, Graphs Graph Fundamentals Telerik Algo Academy
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.
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
Graphs. Contents Terminology Graphs as ADTs Applications of Graphs.
Graphs Chapter 28 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank Carrano.
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 22 Graphs and Applications.
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.
© 2006 Pearson Addison-Wesley. All rights reserved14 B-1 Chapter 14 (continued) Graphs.
Data Structures & Algorithm Analysis lec(8):Graph T. Souad alonazi
Graphs and Graph Algorithms
Graphs Chapter 20.
Graphs.
Unit 10 Graphs (1) King Fahd University of Petroleum & Minerals
Csc 2720 Instructor: Zhuojun Duan
Common final examinations
CS202 - Fundamental Structures of Computer Science II
CS120 Graphs.
Refresh and Get Ready for More
Graph Implementations
Graphs Chapter 13.
Graphs Chapter 11 Objectives Upon completion you will be able to:
Chapter 11 Graphs.
Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013
Chapter 14 Graphs © 2006 Pearson Addison-Wesley. All rights reserved.
Graph Implementation.
GRAPHS G=<V,E> Adjacent vertices Undirected graph
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

Copyright ©2012 by Pearson Education, Inc. All rights reserved Graphs Chapter 28 Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Contents Some Examples and Terminology Road Maps Airline Routes Mazes Course Prerequisites Trees Traversals Breadth-First Traversal Depth-First Traversal Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Contents Topological Order Paths Finding a Path The Shortest Path in an Unweighted Graph The Shortest Path in a Weighted Graph Java Interfaces for the ADT Graph Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Objectives Describe characteristics of a graph, including vertices, edges, paths Give examples of graphs, undirected, directed, unweighted, weighted Give examples of vertices Adjacent and not adjacent For both directed and undirected graphs Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Objectives Give examples of paths, simple paths, cycles, simple cycles Give examples of connected graphs, disconnected graphs, complete graphs Perform depth-first traversal, breadth-first traversal on a given graph List topological order for vertices of a directed graph without cycles Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Objectives Detect whether path exists between two given vertices of a graph Find path with fewest edges that joins one vertex to another Find path with lowest cost that joins one vertex to another in a weighted graph Describe operations for ADT graph Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Examples Not the graphs we study Bar graphs, pie charts, etc. Graphs we study include Trees Networks of nodes connected by paths Could include a road map Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 28-1 A portion of a road map Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Figure 28-2 A directed graph representing a portion of a city’s street map Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Terminology Nodes connected by edges Edges Undirected Directed (digraph) Path between two vertices Sequence of edges Weights Shortest, fastest, cheapest, costs Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 28-3 A weighted graph Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 28-4 Undirected graphs Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Figure 28-5 Vertex A is adjacent to vertex B, but B is not adjacent to A Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 28-6 Airline routes Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 28-7 (a) A maze; (b) its representation as a graph Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Figure 28-8 The prerequisite structure for a selection of courses as a directed graph without cycles Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 28-9 The visitation order of two traversals: (a) depth first; Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 28-9 The visitation order of two traversals: (b) breadth first Copyright ©2012 by Pearson Education, Inc. All rights reserved

Breadth First Traversal Algorithm getBreadthFirstTraversal (originVertex) { traversalOrder = a new queue (or string) VQ = a new queue for visited vertices Mark originVertex as visited and output it VQ.enqueue(originVertex) While (! VQ.isEmpty()) { FrontV = VQ.dequeue() while (FrontV has neighbor) { nextN = next neighbor of FrontV if (nextN is NOT Visited) { Mark nextN as visited and output it VQ.enqueue(nextN) } }… Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved FIGURE 28-10 A trace of a breadth-first traversal beginning at vertex A of a directed graph Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Depth First Traversal Algorithm getDepthFirstTraversal (originVertex) { traversalOrder = a new queue (or string) VS = a new stack for visited vertices Mark originVertex as visited and output it VS.push(originVertex) While (! VS.isEmpty()) { TopV = VS.peek() if (TopV has UNVISITED neighbor) { nextN = next unvisited neighbor of TopV Mark nextN as visited and output it VS.push(nextN) } else VS.pop() … Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Comparison BREADTH FIRST  QUEUE While (! VQ.isEmpty()) { FrontV = VQ.dequeue() while (FrontV has neighbor) { nextN = next neighbor of FrontV if (nextN is NOT Visited) { Mark nextN as visited and output it VQ.enqueue(nextN) } }… DEPTH FIRST  STACK While (! VS.isEmpty()) { TopV = VS.peek() if (TopV has UNVISITED neighbor) { nextN = next unvisited neighbor of TopV Mark nextN as visited and output it VS.push(nextN) } else VS.pop() … Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Figure 28-11 A trace of a depth-first traversal beginning at vertex A of a directed graph Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Figure 28-11 A trace of a depth-first traversal beginning at vertex A of a directed graph Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 28-12 Three topological orders for the graph in Figure 28-8 Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Figure 28-13 An impossible prerequisite structure for three courses, as a directed graph with a cycle Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 28-14 Finding a topological order for the graph in Figure 28-8 Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 28-14 Finding a topological order for the graph in Figure 28-8 Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 28-14 Finding a topological order for the graph in Figure 28-8 Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 28-14 Finding a topological order for the graph in Figure 28-8 Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 28-14 Finding a topological order for the graph in Figure 28-8 Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Figure 28-15 (a) An unweighted graph and (b) the possible paths from vertex A to vertex H Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Figure 28-16 (a) The graph in Figure 28-15a after the shortest-path algorithm has traversed from vertex A to vertex H; (b) the data in a vertex Copyright ©2012 by Pearson Education, Inc. All rights reserved

Shortest Path Algorithm Algorithm getShortestPath (originV, endV, path) { done = False VQ = a new queue - visited Vertices Mark originV as visited (output it) VQ.enqueue(originV) While (!done && ! VQ.isEmpty()) { FrontV = VQ.dequeue() while ((!done && FrontV has neighbor) { nextN = next neighbor of FrontV if (nextN is NOT Visited) { Mark nextN as visited and adjust length and set predecessor VQ.enqueue(nextN) } if (nextN == endV) done = true }… // construct the path … NOTE: In light blue is what is different from the Breadth First Traversal Path.push (endV) Vertex = endV While (Vertex has a predecessor) { Vertex = predecessor of Vertex path.push(vertex) }… Copyright ©2012 by Pearson Education, Inc. All rights reserved Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved NOTE: For printing you may want to remove the blue boxes Figure 28-17 A trace of the traversal in the algorithm to find the shortest path from vertex A to vertex H in an unweighted graph Copyright ©2012 by Pearson Education, Inc. All rights reserved Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved NOTE: For printing you may want to remove the blue boxes Figure 28-17 A trace of the traversal in the algorithm to find the shortest path from vertex A to vertex H in an unweighted graph Copyright ©2012 by Pearson Education, Inc. All rights reserved Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Figure 28-18 (a) A weighted graph and (b) the possible paths from vertex A to vertex H, with their weights Copyright ©2012 by Pearson Education, Inc. All rights reserved

Cheapest Path Algorithm Algorithm getCheapestPath (originV, endV, path) { done = False PQ = a new priority queue Mark originV as visited (output it) PQ.add(originV) While (!done && ! PQ.isEmpty()) { FrontV = PQ.remove() if (FrontV is NOT Visited) { Mark nextN as visited and set cost and predecessor if (FrontV == endV) done = true else while (FrontV has a neighbor) { nextN = next neighbor of FrontV if (nextN is NOT visited) { adjust cost and predecessor PQ.add(nextN with new values) } } // if // while } } // if // while }… // construct the path … Path.push (endV) Vertex = endV While (Vertex has a predecessor) { Vertex = predecessor of Vertex path.push(vertex) }… Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved NOTE: For printing you may want to remove the blue boxes Figure 28-19 A trace of the traversal in the algorithm to find the cheapest path from vertex A to vertex H in a weighted graph Copyright ©2012 by Pearson Education, Inc. All rights reserved Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved NOTE: For printing you may want to remove the blue boxes Figure 28-19 A trace of the traversal in the algorithm to find the cheapest path from vertex A to vertex H in a weighted graph Copyright ©2012 by Pearson Education, Inc. All rights reserved Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved FIGURE 28-20 The graph in Figure 28-18a after finding the cheapest path from vertex A to vertex H Copyright ©2012 by Pearson Education, Inc. All rights reserved

Java Interfaces for the ADT Graph ADT graph different from other ADTs Once instance created, we do not add, remove, or retrieve components Interface to create the graph and to obtain basic information Listing 28-1 Note: Code listing files must be in same folder as PowerPoint files for links to work Copyright ©2012 by Pearson Education, Inc. All rights reserved

Java Interfaces for the ADT Graph Interface to specify operations such as traversals and path searches Listing 28-2 For convenience, a third interface to combine first two Listing 28-3 Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 28-21 A portion of the flight map in Figure 28-6 The following statements create the graph shown Figure 28-21 A portion of the flight map in Figure 28-6 Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved End Chapter 28 Copyright ©2012 by Pearson Education, Inc. All rights reserved