Presentation is loading. Please wait.

Presentation is loading. Please wait.

Podcast Ch26b Title: Digraph Class Implementation

Similar presentations


Presentation on theme: "Podcast Ch26b Title: Digraph Class Implementation"— Presentation transcript:

1 Podcast Ch26b Title: Digraph Class Implementation
Description: Digraph class design; methods: getNeighbors, addEdge, removeVertex Participants: Barry Kurtz (instructor); John Helfert and Tobie Williams (students) Textbook: Data Structures for Java; William H. Ford and William R. Topp

2 DiGraph Class Design The class provides an availability stack to store indices for the ArrayList. Whenever a vertex is deleted from the map, push the index for the corresponding vInfo element onto the stack. When adding a new vertex, check to see whether an index is available on the stack. If so, pop it and reuse the corresponding vInfo element to store vertex properties, thus insuring more efficient memory management.

3 DiGraph Class Design (cont)

4 DiGraph Class Methods The method getVInfoIndex() takes a vertex as an argument and returns the index of the corresponding element in the ArrayList. The index is in the value field of the vtxMap entry. A return value -1 indicates that the vertex is not in the graph. This method is used by many DiGraph class methods.

5 getVInfoIndex() Private method getVInfoIndex() looks up a vertex in the map and returns the vInfo index of the vertex. // takes vertex v in the map and returns the // index of the corresponding vInfo element // or -1 if v is not a vertex private int getVInfoIndex(Object v) { // get Integer value field of the vtxMap entry Integer indexObj = vtxMap.get(v); // if value is null, there is not entry in // the map; return -1; otherwise, convert // object to an int if (indexObj == null) return -1; else return indexObj; }

6 getNeighbors() The getNeighbors() method scans the adjacency list of a vertex and returns a set containing its neighbors. // returns the vertices that are adjacent to // vertex v in a Set object; if v is not a graph // vertex, throws IllegalArgumentException public Set<T> getNeighbors(T v) { // find the VertexInfo object for index v int index = getVInfoIndex(v); // check for an error and throw exception // if vertices not in graph if (index == -1) throw new IllegalArgumentException( "DiGraph getNeighbors(): vertex not in graph");

7 getNeighbors() (concluded)
// create HashSet object to hold vertices, // obtain the VertexInfo object, and initialize // an iterator to scan the adjacency list of // the VertexInfo object HashSet<T> edgeSet = new HashSet<T>(); VertexInfo<T> vtxInfo = vInfo.get(index); Iterator<Edge> iter = vtxInfo.edgeList.iterator(); Edge e = null; while (iter.hasNext()) { e = iter.next(); edgeSet.add(vInfo.get(e.dest).vertex); } return edgeSet;

8 Student Question Write a method outDegree(), a member function in the Graph class, that is passed in a vertex v and returns the size of its adjacency list. If the vertex is not in the graph, throw an IllegalArgumentException. public int outDegree(T v) { int index = getVInfoIndex(v); if (index == -1) throw new IllegalArgumentException(); return vInfo.get(index).edgeList.size(); }

9 Adding an Edge To add an edge to a graph, obtain the vInfo index of the source vertex and insert an Edge object for the destination vertex in the adjacency list. Update the in-degree of the destination vertex.

10 Adding an Edge (cont) pos1=getVInfoIndex(v1); pos2=getVInfoIndex(v2);
// get VertexInfo objects for vertices v1 and v2 VertexInfo<T> vtxInfo1 = vInfo.get(pos1), vtxInfo2 = vInfo.get(pos2); Edge e = new Edge(pos2, w); boolean returnValue = true; // try to add an Edge reference v1-v2. // if it already exists, just return if (!vtxInfo1.edgeList.contains(e)) { vtxInfo1.edgeList.add(e); // increment inDegree for vertex v2 and // number of edges vtxInfo2.inDegree++; numEdges++; } else returnValue = false; return returnValue;

11 Student Question In the DiGraph class, the algorithm for addEdge(v1, v2, w) involves updates to the in-degree and edgeList fields of selected elements in the vInfo array list. Describe the updates. In the vInfo element for vertex v1, add the Edge (v2, w) to the edgeList field; in the vInfo element for vertex v2, increment the in-degree field.

12 Student Question In the DiGraph class, the algorithm for removeEdge(v1, v2) involves updates to the in-degree and edgeList fields of selected elements in the vInfo array list. Describe the updates. In the vInfo element for vertex v1, remove the Edge (v2, w) from the edgeList field; in the vInfo element for vertex v2, decrement the in-degree field.

13 Student Question In the DiGraph class implementation of addVertex(v), a corresponding VertexInfo element in the vInfo must be accessed. Describe the process to obtain this element. An availability stack stores VertexInfo elements that are not currently in use. First attempt to access (pop) an element from the stack. If the stack is empty, create a new VertexInfo object and add it to the array list.

14 Erasing a Vertex Task 1: Get the VertexInfo object in vInfo that corresponds to the index. Set the occupied field to false and then push the index onto an availability stack for use by a vertex that might be added later. // iterator scans Edge objects in adjacency lists Iterator<Edge> iter = null; Edge e = null; VertexInfo<T> vtxInfo = vInfo.get(index), edgeVtxInfo; vtxInfo.occupied = false; availStack.push(index);

15 Erasing a Vertex (cont)
Task 2: Delete all edges that terminate in v. These edges have the form (vi, v) and are found by scanning all of the elements in vInfo. Extract the adjacency list for each vertex and use an iterator to scan the element, removing the Edge object that indicates v is an adjacent vertex. Decrement of the variable numEdges with each edge deletion.

16 Erasing a Vertex (cont)
// remove all the edges that terminate at the // vertex being removed. use a loop to check all // of the VertexInfo elements in vInfo for which // occupied is true; these correspond to // actual vertices in the map for (int i = 0; i < vInfo.size(); i++) { // get the VertexInfo object for index i edgeVtxInfo = vInfo.get(i);

17 Erasing a Vertex (continued)
// check if vertex is valid if (edgeVtxInfo.occupied) { // obtain iterator to scan the adjacency list iter = edgeVtxInfo.edgeList.iterator(); while (iter.hasNext()) { // get the Edge object and check if the // dest field has value index which // identifies vertex v; if so // remove it and decrement numEdges e = iter.next(); if (e.dest == index) { iter.remove(); numEdges--; break; }

18 Erasing a Vertex (cont)
Task 3: Delete all edges that emanate from v. These edges constitute the adjacency list for the vertex. First, determine the number of edges and decrement numEdges. Then, search the list of Edge objects and decrement the in-degree for each adjacent vertex. Delete each edge during the scan.

19 Erasing a Vertex (concluded)
// reduce numEdges by # of elements in adjacency list numEdges -= vtxInfo.edgeList.size(); // scan the adjacency list for vertex v and // decrement the in-degree for each adjacent vertex iter = vtxInfo.edgeList.iterator(); while (iter.hasNext()){ e = iter.next(); edgeVtxInfo = vInfo.get(e.dest); iter.remove(); edgeVtxInfo.inDegree--; }

20 Efficiency of addEdge() and removeVertex() Methods
The running time for adding a vertex is O(E) and for removing a vertex is O(V + E).

21 Student Question In the DiGraph class, the algorithm for removeVertex(v) involves updates to vtxMap which is the map that stores vertex-index pairs. It also involves updates to the in-degree, edgeList, and occupied fields of selected elements in the vInfo array list. Describe the updates. Remove the vertex v from vtxMap; identify the vInfo element for vertex v, mark the occupied field "false", and push the element on the availability stack; for each "occupied" element in the vInfo array list, check whether v is a destination vertex; if so, remove the edge that has v as the destination.


Download ppt "Podcast Ch26b Title: Digraph Class Implementation"

Similar presentations


Ads by Google