Download presentation
Presentation is loading. Please wait.
1
Podcast Ch26a Title: Representing Graphs
Description: Adjacency matrix; adjacency list; VertexInfo class; vertex map; exploring the adjacency list Participants: Barry Kurtz (instructor); John Helfert and Tobie Williams (students) Textbook: Data Structures for Java; William H. Ford and William R. Topp
2
Representing Graphs Adjacency matrix representation of a graph uses a matrix whose entries give the weight of an edge (vi,vj) or 0 if there is no edge from vi to vj.
3
Draw the adjacency matrix for the following graph G.
4
Use the following digraph; a assume the weights are all 1.
(a) Identify the adjacency matrix for the graph. (i) (ii) (iii) (iv)
5
Representing Graphs (concluded)
An Adjacency list representation of a graph includes the vertex with the list of its neighbors.
6
Give the adjacency list representation of the graph.
Vertex Edges
7
Edge Class class Edge { // index of the destination vertex in the
// ArrayList vInfo of vertex properties public int dest; // weight of this edge public int weight; public Edge(int dest, int weight) { this.dest = dest; this.weight = weight; } public boolean equals(Object obj) { return ((Edge)obj).dest == this.dest;
8
Representing Vertex Information
The VertexInfo class has a reference to the vertex value, the in-degree of the vertex, a list of edges originating at the vertex and other information used by graph algorithms.
9
VertexInfo Class // maintains vertex properties, including
// its set of Edges class VertexInfo<T> { // vertex reference back to the vertex in the map public T vertex; // list of Edge objects (adjacent vertices) // for the current vertex public LinkedList<Edge> edgeList; // maintains the in-degree of the vertex public int inDegree; // indicates whether the object currently // represents a vertex public boolean occupied;
10
VertexInfo Class (continued)
// indicates vertex color for use in algorithms // that traverse the vertices of a Graph public VertexColor color; // available to algorithms for storing relevant // data values public int dataValue; // available to Graph algorithms; holds parent // which is a vertex that has an edge terminating // in the current vertex public T parent;
11
VertexInfo Class (concluded)
// constructor creates an object with initial // values for the vertex, edgeList, inDegree, // and occupied fields public VertexInfo(T v) { vertex = v; edgeList = new LinkedList<Edge>(); inDegree = 0; occupied = true; }
12
VertexColor Class The VertexColor class maintains the color of a vertex. The colors of WHITE, GRAY, and BLACK are values of the enumerated type. public enum VertexColor { WHITE, GRAY, BLACK }
13
Vertex Map & VertexInfo ArrayList
The vertex map contains key‑value pairs where the key is a reference to the vertex and the value is an index into an ArrayList of VertexInfo objects.
14
Vertex Map & VertexInfo ArrayList
15
For the following graph G, assume that the vInfo index of A: 0. B: 1
For the following graph G, assume that the vInfo index of A: 0 B: 1 C: 2 D: 3 Demonstrate the graph class representation for G in the map vtxMap and the array list vInfo containing a list of VertexInfo object. In vtxMap, fill-in the key and value components of the key-value pair. In vInfo, fill-in the in-degree and the edge set for each vertex. Recall that each entry of the edge list is of the form {dest vInfo index, weight}. For instance, the edge (A, C) is represented in the edge set of A by {2, 3}.
16
Exploring the Adjacency List
A VertexInfo object allows access to all the neighbors of the vertex. VertexInfo<T> vtxInfo = vInfo.get(i); // declare an iterator for edgeList and position it // at the first element in the adjacency list Iterator<Edge> iter = vtxInfo.edgeList.iterator(); // extract the first element and output its weight Edge e = iter.next(); System.out.println(e.weight); // use the dest field to access the vInfo element // that corresponds to the adjacent vertex; the // index of the element is e.dest; // output the name of the vertex VertexInfo<T> edgeVtxInfo = vInfo.get(e.dest); System.out.println(edgeVtxInfo.vertex);
17
Exploring the Adjacency List of a Vertex (continued)
Let us look at a code segment that scans the adjacency list looking for the edge of minimum weight. In order to output the edge, we must maintain the vInfo index of the destination vertex. // declare variables for the minimum dest index and // minimum weight int minDest, minWeight = Integer.MAX_VALUE; // VertexInfo objects for elements in vInfo; // current vertex element in vInfo is vtxInfo; // vInfo element for a neighbor is edgeVtxInfo VertexInfo<T> vtxInfo = null, edgeVtxInfo = null; ...
18
Exploring the Adjacency List of a Vertex (continued)
// iterator scans adjacency list for current vertex Iterator<Edge> iter = vtxInfo.edgeList.iterator(); while (iter.hasNext) { // extract next element (Edge object) from // adjacency list Edge e = iter.next(); // update if new minimum weight is discovered if (e.weight < minWeight) minWeight = e.weight; minDest = e.dest; }
19
Exploring the Adjacency List of a Vertex (continued)
// output the edge that has minimum weight; // first get the corresponding vInfo element // and then access the vertex field edgeVtxInfo = vInfo.get(minDest); System.out.println("Edge (" + vtxInfo.vertex ", " + edgeVtxInfo.vertex + ") has weight edgeVtxInfo.weight);
20
The occupied Field Occupied field of a VertexInfo object indicates if the object corresponds to an existing vertex entry in the map. for (i = 0; i < vInfo.size(); i++) { VertexInfo<T> vtxInfo = vInfo.get(i); if (vtxInfo.occupied) < vInfo element corresponds to an actual vertex> }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.