Download presentation
Presentation is loading. Please wait.
1
Graph Implementations
Chapter 29 Copyright ©2012 by Pearson Education, Inc. All rights reserved
2
Copyright ©2012 by Pearson Education, Inc. All rights reserved
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
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
4
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
5
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
6
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
7
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Question 1Consider the graph in Figure 28-4b of the previous chapter. Number the vertices from 0 through 3, starting at the vertex in the upper left corner and moving in a clockwise direction. What adjacency matrix represents this graph? Copyright ©2012 by Pearson Education, Inc. All rights reserved
8
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Question 1 Consider the graph in Figure 28-4b of the previous chapter. Number the vertices from 0 through 3, starting at the vertex in the upper left corner and moving in a clockwise direction. What adjacency matrix represents this graph? Copyright ©2012 by Pearson Education, Inc. All rights reserved
9
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
10
Figure 29-2 Adjacency lists for the directed graph in Figure 29-1a
Copyright ©2012 by Pearson Education, Inc. All rights reserved
11
Figure 29-2 Adjacency lists for the directed graph in Figure 29-1a
Copyright ©2012 by Pearson Education, Inc. All rights reserved
12
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Question 2 What adjacency lists represent the graph described in Question 1? Copyright ©2012 by Pearson Education, Inc. All rights reserved
13
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Question 2 What adjacency lists represent the graph described in Question 1? 2. Vertex 0 references the list 1, 2, 3. Vertex 1 references the list 0, 2, 3. Vertex 2 references the list 0, 1, 3. Vertex 3 references the list 0, 1, 2. Copyright ©2012 by Pearson Education, Inc. All rights reserved
14
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
15
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 Note: Code listing files must be in same folder as PowerPoint files for links to work Copyright ©2012 by Pearson Education, Inc. All rights reserved
16
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Question 3 Given the interface VertexInterface and the class Vertex , write Java statements that create the vertices and edge s for the following directed, weighted graph. This graph contains three vertices— A , B, and C —and four edges, as follows: A → B, B → C , C → A, A → C . These edges have the weights 2, 3, 4, and 5, respectively. Copyright ©2012 by Pearson Education, Inc. All rights reserved
17
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Question 3 Given the interface VertexInterface and the class Vertex , write Java statements that create the vertices and edge s for the following directed, weighted graph. This graph contains three vertices— A , B, and C —and four edges, as follows: A → B, B → C , C → A, A → C . These edges have the weights 2, 3, 4, and 5, respectively. 3. VertexInterface<String> vertexA = new Vertex<String>("A"); VertexInterface<String> vertexB = new Vertex<String>("B"); VertexInterface<String> vertexC = new Vertex<String>("C"); vertexA.addEdge(vertexB, 2.0); vertexB.addEdge(vertexC, 3.0); vertexC.addEdge(vertexA, 4.0); vertexA.addEdge(vertexC, 5.0); Copyright ©2012 by Pearson Education, Inc. All rights reserved
18
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
19
Class DirectedGraph, Listing 29-5
Figure 29-3 (a) A directed graph and (b) its implementation using adjacency lists Copyright ©2012 by Pearson Education, Inc. All rights reserved
20
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Question 4 Create an instance of the class DirectedGraph for the graph described in Question 3. Copyright ©2012 by Pearson Education, Inc. All rights reserved
21
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Question 4 Create an instance of the class DirectedGraph for the graph described in Question 3. 4. DirectedGraph<String> myGraph = new DirectedGraph<String>(); myGraph.addVertex("A"); myGraph.addVertex("B"); myGraph.addVertex("C"); myGraph.addEdge("A", "B", 2.0); myGraph.addEdge("B", "C", 3.0); myGraph.addEdge("C", "A", 4.0); myGraph.addEdge("A", "C", 5.0); Copyright ©2012 by Pearson Education, Inc. All rights reserved
22
Copyright ©2012 by Pearson Education, Inc. All rights reserved
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
23
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Graph Algorithms Breadth-first traversal, Listing 29-A Depth-first traversal left as exercise Copyright ©2012 by Pearson Education, Inc. All rights reserved
24
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Question 5 Write Java statements that display the vertices in a breadth-first traversal of the graph that you created in Question 4, beginning with vertex A. Copyright ©2012 by Pearson Education, Inc. All rights reserved
25
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Question 5 Write Java statements that display the vertices in a breadth-first traversal of the graph that you created in Question 4, beginning with vertex A. 5. QueueInterface<String> bfs = myGraph.getBreadthFirstTraversal("A"); while (!bfs.isEmpty()) System.out.print(bfs.dequeue() + " "); System.out.println(); Copyright ©2012 by Pearson Education, Inc. All rights reserved
26
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
27
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Question 6 Write Java statements that display the vertices in the shortest path from vertex A to vertex C for the graph that you created in Question 4. Also, display the length of this path. Copyright ©2012 by Pearson Education, Inc. All rights reserved
28
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Question 6 Write Java statements that display the vertices in the shortest path from vertex A to vertex C for the graph that you created in Question 4. Also, display the length of this path. 6. StackInterface<String> path = new LinkedStack<String>(); int pathLength = myGraph.getShortestPath("A", "C", path); System.out.println("The shortest path from A to C has length " + pathLength + " and passes through the following vertices:"); while (!path.isEmpty()) System.out.print(path.pop() + " "); System.out.println(); Copyright ©2012 by Pearson Education, Inc. All rights reserved
29
Copyright ©2012 by Pearson Education, Inc. All rights reserved
End Chapter 29 Copyright ©2012 by Pearson Education, Inc. All rights reserved
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.