Presentation is loading. Please wait.

Presentation is loading. Please wait.

Graphs Chapter 28 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank Carrano.

Similar presentations


Presentation on theme: "Graphs Chapter 28 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank Carrano."— Presentation transcript:

1 Graphs Chapter 28 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank Carrano

2 Road Maps FIGURE 28-1 A portion of a road map © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

3 Graphs A road map is a graph Definition of a graph:  A collection of distinct vertices and distinct edges In the map of Figure 28-1  The circles are vertices or nodes  The lines are called edges A subgraph is a portion of a graph that is itself a graph © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

4 Graphs FIGURE 28-2 A directed graph representing a portion of a city’s street map © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

5 Paths A path between two vertices in a graph is a sequence of edges A path in a directed graph must consider the direction of the edges  Called a directed path The length of a path is the number of edges that it comprises. A cycle is a path that begins and ends at the same vertex © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

6 Weights Weighted graph, has values on its edges  Values are called either weights or costs © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. FIGURE 28-3 A weighted graph

7 Connected Graphs A graph that has a path between every pair of distinct vertices is connected A complete graph has an edge between every pair of distinct vertices. Undirected graphs can be  Connected  Complete or  Disconnected © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

8 Connected Graphs FIGURE 28-4 Undirected graphs © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

9 Adjacent Vertices FIGURE 28-5 Vertex A is adjacent to vertex B, but B is not adjacent to A © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Vertices are adjacent in an undirected graph if they are joined by an edge

10 Airline Routes FIGURE 28-6 Airline routes © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

11 Mazes FIGURE 28-7 (a) A maze; (b) its representation as a graph © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

12 Course Prerequisites FIGURE 28-8 The prerequisite structure for a selection of courses as a directed graph without cycles © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

13 Traversals FIGURE 28-9 The visitation order of two traversals: (a) depth first; (b) breadth first © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

14 Breadth-First Traversal Algorithm that performs a breadth-first traversal of a nonempty graph beginning at a given vertex © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

15 Breadth-First Traversal Algorithm that performs a breadth-first traversal of a nonempty graph beginning at a given vertex © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

16 Breadth-First Traversal FIGURE 28-10 A trace of a breadth-first traversal beginning at vertex A of a directed graph © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

17 Depth-First Traversal Algorithm that performs a depth-first traversal of a nonempty graph, beginning at a given vertex: © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

18 Depth-First Traversal Algorithm that performs a depth-first traversal of a nonempty graph, beginning at a given vertex: © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

19 Depth-First Traversal FIGURE 28-11 A trace of a depth-first traversal beginning at vertex A of a directed graph © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

20 Depth-First Traversal FIGURE 28-11 A trace of a depth-first traversal beginning at vertex A of a directed graph © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

21 Topological Order FIGURE 28-12 Three topological orders for the graph in Figure 28-8 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

22 Topological Order FIGURE 28-13 An impossible prerequisite structure for three courses, as a directed graph with a cycle © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. In a topological order of the vertices in a directed graph without cycles, vertex a precedes vertex b whenever a directed edge exists from a to b.

23 Topological Order An algorithm that describes a topological sort © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

24 Topological Order FIGURE 28-14 Finding a topological order for the graph in Figure 28-8 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

25 Topological Order FIGURE 28-14 Finding a topological order for the graph in Figure 28-8 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

26 Topological Order FIGURE 28-14 Finding a topological order for the graph in Figure 28-8 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

27 Topological Order FIGURE 28-14 Finding a topological order for the graph in Figure 28-8 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

28 Topological Order FIGURE 28-14 Finding a topological order for the graph in Figure 28-8 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

29 Shortest Path in an Unweighted Graph FIGURE 28-15 (a) An unweighted graph and (b) the possible paths from vertex A to vertex H © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

30 Shortest Path in an Unweighted Graph 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 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

31 Shortest Path in an Unweighted Graph Algorithm to find shortest path © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

32 Shortest Path in an Unweighted Graph Algorithm to find shortest path © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

33 Shortest Path in an Unweighted Graph Algorithm to find shortest path © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

34 Shortest Path in an Unweighted Graph 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 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

35 The Shortest Path in a Weighted Graph FIGURE 28-18 (a) A weighted graph and (b) the possible paths from vertex A to vertex H, with their weights © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

36 The Shortest Path in a Weighted Graph 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 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

37 The Shortest Path in a Weighted Graph 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 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

38 The Shortest Path in a Weighted Graph FIGURE 28-20 The graph in Figure 28-18a after finding the cheapest path from vertex A to vertex H © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

39 The Shortest Path in a Weighted Graph Pseudocode for shortest path (weighted) © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

40 The Shortest Path in a Weighted Graph Pseudocode for shortest path (weighted) © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

41 The Shortest Path in a Weighted Graph Pseudocode for shortest path (weighted) © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

42 Java Interfaces for the ADT Graph LISTING 28-1 An interface of basic graph operations © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

43 Java Interfaces for the ADT Graph LISTING 28-1 An interface of basic graph operations © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

44 Java Interfaces for the ADT Graph LISTING 28-1 An interface of basic graph operations © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

45 Java Interfaces for the ADT Graph FIGURE 28-21 A portion of the flight map in Figure 28-6 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

46 Java Interfaces for the ADT Graph LISTING 28-2 An interface of operations on an existing graph © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

47 Java Interfaces for the ADT Graph LISTING 28-2 An interface of operations on an existing graph © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

48 Java Interfaces for the ADT Graph LISTING 28-2 An interface of operations on an existing graph © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

49 Java Interfaces for the ADT Graph LISTING 28-3 An interface for the ADT graph © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

50 End Chapter 28 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.


Download ppt "Graphs Chapter 28 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank Carrano."

Similar presentations


Ads by Google