Presentation is loading. Please wait.

Presentation is loading. Please wait.

Graph Implementations Chapter 29 Copyright ©2012 by Pearson Education, Inc. All rights reserved.

Similar presentations


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

1 Graph Implementations Chapter 29 Copyright ©2012 by Pearson Education, Inc. All rights reserved

2 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

3 Contents An Implementation of the ADT Graph  Basic Operations  Graph Algorithms Copyright ©2012 by Pearson Education, Inc. All rights reserved

4 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

5 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

6 Figure 29-1 (a) An unweighted, directed graph and (b) its adjacency matrix Copyright ©2012 by Pearson Education, Inc. All rights reserved

7 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

8 Figure 29-2 Adjacency lists for the directed graph in Figure 29-1aFigure 29-1a Copyright ©2012 by Pearson Education, Inc. All rights reserved

9 Figure 29-2 Adjacency lists for the directed graph in Figure 29-1aFigure 29-1a Copyright ©2012 by Pearson Education, Inc. All rights reserved

10 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

11 Specifying the Class Vertex Note interface, Listing 29-1Listing 29-1 Inner class Edge, Listing 29-2Listing 29-2 Class Vertex, Listing 29-3Listing 29-3 Private class neighborIterator, Listing 29-4 Listing 29-4 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

12 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

13 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-5Listing 29-5

14 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

15 Graph Algorithms Breadth-first traversal, Listing 29-AListing 29-A  Depth-first traversal left as exercise Shortest path, Listing 29-BListing 29-B  Implementation of method getCheapestPath for weighted graph left as an exercise Copyright ©2012 by Pearson Education, Inc. All rights reserved

16 End Chapter 29 Copyright ©2012 by Pearson Education, Inc. All rights reserved


Download ppt "Graph Implementations Chapter 29 Copyright ©2012 by Pearson Education, Inc. All rights reserved."

Similar presentations


Ads by Google