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

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

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

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

Graphs

Chapter 28

© 2015 Pearson Education, Inc., Upper Saddle River, NJ.  All rights reserved.

Data Structures and Abstractions with Java, 4e Frank Carrano

Page 2: 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.

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

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.

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

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.

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

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.

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

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

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

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.

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

Connected Graphs

FIGURE 28-4 Undirected graphs

© 2015 Pearson Education, Inc., Upper Saddle River, NJ.  All rights reserved.

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

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

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

Airline Routes

FIGURE 28-6 Airline routes

© 2015 Pearson Education, Inc., Upper Saddle River, NJ.  All rights reserved.

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

Mazes

FIGURE 28-7 (a) A maze; (b) its representation as a graph

© 2015 Pearson Education, Inc., Upper Saddle River, NJ.  All rights reserved.

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

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.

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

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.

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

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.

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

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.

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

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.

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

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.

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

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.

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

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.

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

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.

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

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.

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

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.

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

Topological Order

An algorithm that describes a topological sort

© 2015 Pearson Education, Inc., Upper Saddle River, NJ.  All rights reserved.

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

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.

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

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.

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

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.

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

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.

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

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.

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

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.

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

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.

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

Shortest Path in an Unweighted Graph

Algorithm to find shortest path

© 2015 Pearson Education, Inc., Upper Saddle River, NJ.  All rights reserved.

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

Shortest Path in an Unweighted Graph

Algorithm to find shortest path

© 2015 Pearson Education, Inc., Upper Saddle River, NJ.  All rights reserved.

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

Shortest Path in an Unweighted Graph

Algorithm to find shortest path

© 2015 Pearson Education, Inc., Upper Saddle River, NJ.  All rights reserved.

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

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.

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

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.

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

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.

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

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.

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

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.

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

The Shortest Path in a Weighted Graph

Pseudocode for shortest path (weighted)

© 2015 Pearson Education, Inc., Upper Saddle River, NJ.  All rights reserved.

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

The Shortest Path in a Weighted Graph

Pseudocode for shortest path (weighted)

© 2015 Pearson Education, Inc., Upper Saddle River, NJ.  All rights reserved.

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

The Shortest Path in a Weighted Graph

Pseudocode for shortest path (weighted)

© 2015 Pearson Education, Inc., Upper Saddle River, NJ.  All rights reserved.

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

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.

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

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.

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

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.

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

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.

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

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.

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

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.

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

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.

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

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.

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

End

Chapter 28

© 2015 Pearson Education, Inc., Upper Saddle River, NJ.  All rights reserved.