Data Structures, Algorithms, & Applications in Java
Chapter 17, Exercise 37
First we develop the class GraphArrayLinearList,
which is the counterpart of the class GraphChain.
This class is given below.
public class GraphArrayLinearList extends ArrayLinearList
{
/** delete element with element.vertex = theVertex
* @return deleted element */
public Object removeElement(int theVertex)
{
// search for matching element
for (int i = 0; i < size; i++)
if (((EdgeNode) element[i]).vertex == theVertex)
{// found matching element
// save matching element
Object theElement = element[i];
// remove matching element
System.arraycopy(element, i + 1, element, i, size - i - 1);
size--;
return theElement;
}
// no matching element
return null;
}
}
Now we can develop the code for the class ArrayDigraph
by making minor changes (shown in red) to
the code for the class LinkedDigraph.
The code is shown below.
public class ArrayDigraph extends Graph
{
// data members
int n; // number of vertices
int e; // number of edges
GraphArrayLinearList [] aList; // adjacency lists
// constructors
public ArrayDigraph(int theVertices)
{
// validate theVertices
if (theVertices < 0)
throw new IllegalArgumentException
("number of vertices must be >= 0");
n = theVertices;
aList = new GraphArrayLinearList [n + 1];
for (int i = 1; i <= n; i++)
aList[i] = new GraphArrayLinearList();
// default value of e is 0
}
// default is a 0-vertex graph
public ArrayDigraph()
{this(0);}
// Graph methods
/** @return number of vertices */
public int vertices()
{return n;}
/** @return number of edges */
public int edges()
{return e;}
/** @return true iff (i,j) is an edge */
public boolean existsEdge(int i, int j)
{
if (i < 1 || j < 1 || i > n || j > n
|| aList[i].indexOf(new EdgeNode(j)) == -1)
return false;
else
return true;
}
/** put theEdge into the digraph
* @throws IllegalArgumentException when
* theEdge is invalid */
public void putEdge(Object theEdge)
{
Edge edge = (Edge) theEdge;
int v1 = edge.vertex1;
int v2 = edge.vertex2;
if (v1 < 1 || v2 < 1 || v1 > n || v2 > n || v1 == v2)
throw new IllegalArgumentException
("(" + v1 + "," + v2 + ") is not a permissible edge");
if (aList[v1].indexOf(new EdgeNode(v2)) == -1) // new edge
{
// put v2 at end of aList[v1]
aList[v1].add(aList[v1].size(), new EdgeNode(v2));
e++;
}
}
/** remove the edge (i,j) */
public void removeEdge(int i, int j)
{
if (i >= 1 && j >= 1 && i <= n && j <= n)
{
Object v = aList[i].removeElement(j);
if (v != null) // edge (i,j) did exist
e--;
}
}
/** this method is undefined for directed graphs
* @throws NoSuchMethodError */
public int degree(int i)
{throw new NoSuchMethodError();}
/** @return out-degree of vertex i
* @throws IllegalArgumentException when
* i is an invalid vertex */
public int outDegree(int i)
{
if (i < 1 || i > n)
throw new IllegalArgumentException("no vertex " + i);
return aList[i].size();
}
/** @return in-degree of vertex i
* @throws IllegalArgumentException when
* i is an invalid vertex */
public int inDegree(int i)
{
if (i < 1 || i > n)
throw new IllegalArgumentException("no vertex " + i);
// count in edges at vertex i
int sum = 0;
for (int j = 1; j <= n; j++)
if (aList[j].indexOf(new EdgeNode(i)) != -1)
sum++;
return sum;
}
/** output the graph */
public void output()
{
for (int i = 1; i <= n; i++)
System.out.println("Vertex " + i + " = " + aList[i]);
}
/** create and return an iterator for vertex i
* @throws IllegalArgumentException when
* i is an invalid vertex */
public Iterator iterator(int i)
{
if (i < 1 || i > n)
throw new IllegalArgumentException("no vertex " + i);
return aList[i].iterator();
}
}