I am implementing a graph traversal to look for all possible paths, and it works fine with a small sample graph. But when I bring it to a larger graph (having 1000 vertex), the program crashes with OutOfMemoryError: Java heap space. The error occurred at str += src + " -> ";
at line 13.
Is there any better solution for this? Thanks!
This is the code for traversal
private void displayPathRec( String src, String dest, String str )
{
DSALinkedList ll = null;
DSAGraphVertex v = null;
ll = getAdjacent( src );
Iterator iter = ll.iterator();
// Base Case: found endLabel
if( !src.equals( dest ) )
{
str += src + " -> ";
while( iter.hasNext() )
{
v = (DSAGraphVertex) iter.next();
displayPathRec( v.getLabel(), dest, str );
}
}
else
System.out.println(str + dest);
}
Sample of my graph
graph.addVertex("A", 25);
graph.addVertex("B", 60);
graph.addVertex("C", 45);
graph.addVertex("D", 75);
graph.addVertex("E", 95);
graph.addVertex("F", 85);
graph.addVertex("T", 115);
graph.addVertex("G", 105);
graph.addEdge("A", "B", "AB", "");
graph.addEdge("A", "D", "AD", "");
graph.addEdge("A", "C", "AC", "");
graph.addEdge("A", "E", "AE", "");
graph.addEdge("B", "E", "BE", "");
graph.addEdge("E", "F", "EF", "");
graph.addEdge("E", "G", "EG", "");
graph.addEdge("D", "C", "DC", "");
graph.addEdge("D", "F", "DF", "");
graph.addEdge("F", "T", "FT", "");
graph.addEdge("F", "G", "FG", "");
graph.addEdge("T", "G", "TG", "");
graph.addEdge("C", "F", "CF", "");
DSALinkedList adjList = null;
DSAGraphVertex v = null;
String src = "A", dest = "G";
graph.displayPath( src, dest );
Output
A -> B -> E -> F -> T -> G
A -> B -> E -> F -> G
A -> B -> E -> G
A -> D -> C -> F -> T -> G
A -> D -> C -> F -> G
A -> D -> F -> T -> G
A -> D -> F -> G
A -> C -> F -> T -> G
A -> C -> F -> G
A -> E -> F -> T -> G
A -> E -> F -> G
A -> E -> G
This is the working example, but when I tried to use it with larger graph, the program crashes.
This is my getAdjacent, given the label for the vertex, it will return the adjacency list for the vertex.
public DSALinkedList getAdjacent( String inLabel )
{
DSAGraphVertex v = null;
if( !hasVertex(inLabel) ) // If inLabel does not exists means vertex is not exists
throw new IllegalArgumentException("Label does not exist");
else
{
boolean found = false;
Iterator iter = vertices.iterator(); // Iterate through vertices becase we want to get
// link list of vertex and link list is fields of vertex
// ASSERTION: Iterate over vertices (to search for the desired vertex)
while( iter.hasNext() && !found )
{
v = (DSAGraphVertex)iter.next();
found = v.getLabel().equals(inLabel);
}
}
return v.getAdjacent(); // Return vertex list
}
in java. DSAGraphVertex represents the vertex for every vertices. Kindly refer to my updated question for what getAdjacent method will do
– Calmen Chia Oct 16 '20 at 04:32