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