Upload
sharlene-lloyd
View
216
Download
0
Embed Size (px)
DESCRIPTION
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.
Citation preview
Graphs
Chapter 28
© 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
Data Structures and Abstractions with Java, 4e Frank Carrano
Road Maps
FIGURE 28-1 A portion of a road map
© 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
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.
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.
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.
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
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.
Connected Graphs
FIGURE 28-4 Undirected graphs
© 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
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
Airline Routes
FIGURE 28-6 Airline routes
© 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
Mazes
FIGURE 28-7 (a) A maze; (b) its representation as a graph
© 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
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.
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.
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.
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.
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.
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.
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.
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.
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.
Topological Order
FIGURE 28-12 Three topological orders for thegraph in Figure 28-8
© 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
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.
Topological Order
An algorithm that describes a topological sort
© 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
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.
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.
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.
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.
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.
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.
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.
Shortest Path in an Unweighted Graph
Algorithm to find shortest path
© 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
Shortest Path in an Unweighted Graph
Algorithm to find shortest path
© 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
Shortest Path in an Unweighted Graph
Algorithm to find shortest path
© 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
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.
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.
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.
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.
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.
The Shortest Path in a Weighted Graph
Pseudocode for shortest path (weighted)
© 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
The Shortest Path in a Weighted Graph
Pseudocode for shortest path (weighted)
© 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
The Shortest Path in a Weighted Graph
Pseudocode for shortest path (weighted)
© 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
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.
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.
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.
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.
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.
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.
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.
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.
End
Chapter 28
© 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.