Download presentation
Presentation is loading. Please wait.
1
Graph Implementations Chapter 31 Slides by Steve Armstrong LeTourneau University Longview, TX 2007, Prentice Hall
2
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X Chapter 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 An Implementation of the ADT Graph Basic Operations Graph Algorithms
3
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X The Adjacency Matrix For a graph with n vertices, has n rows and n columns Each row, each column corresponds to a vertex in the graph Numbered 0 through n – 1 Element a ij indicates whether an edge exists between vertex i and vertex j Elements of the matrix contain Boolean for unweighted graph Edge weights for weighted graph
4
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X The Adjacency Matrix Fig. 31-1 (a) A directed graph and (b) its adjacency matrix.
5
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X The Adjacency Matrix Adjacency matrix uses fixed amount of space Depends on number of vertices Does not depend on number of edges Typically the matrix will be sparse Presence of an edge between two vertices can be known immediately All neighbors of a vertex found by scanning entire row for that vertex
6
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X The Adjacency List Represents only edges that originate from the vertex Space not reserved for edges that do not exist Uses less memory than corresponding adjacency matrix Thus more often used than adjacency matrix
7
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X The Adjacency List Fig. 31-2 Adjacency lists for the directed graph in Fig. 31-1a.
8
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X Vertices and Edges Objects for implementing the ADT graph Vertex Edge These are interrelated Vertex has edges that leave it Edge defined by vertices at its ends Vertex is like a node in a tree
9
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X Specifying the Class Vertex Identify each vertex with an object A data field of class Vertex Operations for class Vertex Mark a vertex Test whether marked Remove mark View interface for vertices in a graphView interface
10
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X Specifying the Class Vertex Define a class Edge Instances placed in adjacency lists Lists contain edges that leave the vertex Each edge references the vertex that ends the edge and indicates its weight (if any) Iterator provided for list access Path operations set get Test operations
11
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X The Inner Class Edge Instances of class Edge in a vertex's adjacency list Indicates edges that originate in that vertex Edge must record Vertex that ends it Edge's weight (if any) Vertex is only class that uses Edge Thus will be an inner class View source code of class Edgesource code
12
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X Implementing the Class Vertex Class Vertex placed in a package to hide it from clients of the graph Implemented with LinkedListWithIterator as specified in Chapter 8 LinkedListWithIterator View source code of class Vertexsource code Note inner private class neighborIterator neighborIterator
13
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X An Implementation of the ADT Graph Use an adjacency list to implement directed graph Can be weighted or unweighted Must have container for the vertices List used if integers used to identify the vertices Dictionary used if strings are used to identify the vertices
14
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X An Implementation of the ADT Graph Fig. 31-3 (a) A directed graph and (b) its implementation using adjacency lists.
15
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X An Implementation of the ADT Graph Beginning of the class Must identify vertices – string used here Dictionary used as the container for vertices View source code of class DirectedGraph DirectedGraph
16
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X Efficiency Fig. 31-4 The performance of basic operations of the ADT graph when implemented b y using adjacency lists
17
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X Graph Algorithms View Java implementation of breadth-first traversalbreadth-first traversal View implementation of algorithm to find shortest path shortest path
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.