Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

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

2 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 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 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 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 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 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 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 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 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 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 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

20 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

21 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

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

23 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

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

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

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

27 Figure 28-14 Finding a topological order for the graph in Figure 28-8Figure 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-8Figure 28-8 Copyright ©2012 by Pearson Education, Inc. All rights reserved

29 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

30 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 vertexFigure 28-15a Copyright ©2012 by Pearson Education, Inc. All rights reserved

31 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

32 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

33 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

34 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

35 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

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

37 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 Listing 28-1 Copyright ©2012 by Pearson Education, Inc. All rights reserved Note: Code listing files must be in same folder as PowerPoint files for links to work Note: Code listing files must be in same folder as PowerPoint files for links to work

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

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

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


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

Similar presentations


Ads by Google