I have looked in several different data structures and algorithms books, but I have yet to find any of them useful when it comes to researching how to implement the operations of fetching, deleting, and updating an vertex and edge for a graph in java? Here is my starting program. Any help would be much appreciated or any suggestions to any books that would assist me in this matter?
Code:
public class SimpleGraph // a directed graph
{ Listing vertex[ ]; //the reference to the vertex array
int edge[ ][ ]; // reference to the adjacency matrix array
int max, numberOfVertices;
public SimpleGraph(int n)
{ vertex = new Listing[n]; // allocation of the vertex array
edge = new int[n][n]; // adjacency matrix with all elements set to 0
max = n; numberOfVertices = 0;
}
public boolean insertVertex(int vertexNumber, Listing newListing)
{ if (vertexNumber >= max) //the graph is full
return false;
vertex[vertexNumber] = newListing.deepCopy(); numberOfVertices++;
return true;
}
public boolean insertEdge(int fromVertex, int toVertex)
{ if(vertex[fromVertex] == null || vertex[toVertex] == null) // non-existent vertex
return false;
edge[fromVertex][toVertex] = 1;
return true;
}
public Listing fetchVertex(int vertex)
{
}
public Listing fetchEdge(int vertex1, int vertex2)
{
}
public boolean deleteVertex(int vertex)
{
}
public boolean deleteEdge(int vertex1, int vertex2)
{
}
public boolean updateVertex(int vertex1)
{
// not really sure if this method would have a second parameter.
// My guess is yes it would be because you
// need to know which other vertex you want to update the first one
// to
}
public boolean updateEdge(int vertex1, int vertex2)
{
}
public void showVertex(int vertexNumber)
{ System.out.println(vertex[vertexNumber]);
}
public void showEdges(int vertexNumber) //edges emanating from vertexNumber
{ for(int column=0; column<numberOfVertices; column++)
{ if(edge[vertexNumber][column] == 1) // there is an edge
System.out.println(vertexNumber + "," + column);
}
}
}// end of SimpleGraph class
// definition of a hub city (a vertex)
public class Listing
{ private String name;
public Listing(String n)
{ name=n;
}
public String toString()
{ return("Name is " + name);
}
public Listing deepCopy()
{ Listing clone = new Listing(name);
return clone;
}
}
Comment