Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Copyright ©2012 by Pearson Education, Inc. All rights reserved"— Presentation transcript:

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

2 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

3 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

4 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

5 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

6 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

7 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

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

9 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

10 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

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

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

13 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

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

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

16 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

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

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

19 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

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

21 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

22 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

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

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

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

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

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

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

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

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

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

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

33 Copyright ©2012 by Pearson Education, Inc. All rights reserved
Figure (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

34 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

35 Copyright ©2012 by Pearson Education, Inc. All rights reserved
NOTE: For printing you may want to remove the blue boxes Figure 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

36 Copyright ©2012 by Pearson Education, Inc. All rights reserved
NOTE: For printing you may want to remove the blue boxes Figure 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

37 Copyright ©2012 by Pearson Education, Inc. All rights reserved
Figure (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

38 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

39 Copyright ©2012 by Pearson Education, Inc. All rights reserved
NOTE: For printing you may want to remove the blue boxes Figure 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

40 Copyright ©2012 by Pearson Education, Inc. All rights reserved
NOTE: For printing you may want to remove the blue boxes Figure 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

41 Copyright ©2012 by Pearson Education, Inc. All rights reserved
FIGURE 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

42 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

43 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

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

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


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

Similar presentations


Ads by Google