graph implementations chapter 29 copyright ©2012 by pearson education, inc. all rights reserved
TRANSCRIPT
Contents
• An Overview of Two Implementations The Adjacency Matrix The Adjacency List
• Vertices and Edges Specifying the Class Vertex The Inner Class Edge Implementing the Class Vertex
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Contents
• An Implementation of the ADT Graph Basic Operations Graph Algorithms
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Objectives
• Describe adjacency matrix
• Describe adjacency list
• Specify and implement classes that represent vertices and edges of a graph
• Implement ADT graph by using adjacency lists
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Adjacency Matrix
• For a graph of n vertices
• Has n rows, n columns
• Rows and columns numbered 0 to n -1
• Each row and column corresponds to vertex of graph
• Note: Sparse graph wastes space
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Figure 29-1 (a) An unweighted, directed graph and (b) its adjacency matrix
Copyright ©2012 by Pearson Education, Inc. All rights reserved
The Adjacency List
• Represents only those edges that originate from vertex
• Space not reserved for edges that do not exist
• Use less memory than corresponding adjacency matrix
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Figure 29-2 Adjacency lists for the directed graph in Figure 29-1a
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Figure 29-2 Adjacency lists for the directed graph in Figure 29-1a
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Specifying the Class Vertex
• Need a way to identify vertices
• Operations that mark a vertex as visited
• Adjacency list indicates its neighbors
• Path operations: Get Set Test
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Specifying the Class Vertex
• Note interface, Listing 29-1
• Inner class Edge, Listing 29-2
• Class Vertex, Listing 29-3
• Private class neighborIterator,Listing 29-4
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Note: Code listing filesmust be in same folder
as PowerPoint filesfor links to work
Note: Code listing filesmust be in same folder
as PowerPoint filesfor links to work
Implementation of the ADT Graph
• With either implementation Need container for the graph’s vertices
• Could be list or dictionary Depending on how object is identified
• We will use dictionary Identify objects with a string
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Figure 29-3 (a) A directed graph and (b) its implementation using adjacency lists
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Class DirectedGraph, Listing 29-5
Figure 29-4 The performance of basic operations of the ADT graph when implemented by using adjacency lists
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Graph Algorithms
• Breadth-first traversal, Listing 29-A Depth-first traversal left as exercise
• Shortest path, Listing 29-B Implementation of method getCheapestPath for weighted graph left as an exercise
Copyright ©2012 by Pearson Education, Inc. All rights reserved