-1

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
}
Calmen Chia
  • 325
  • 1
  • 8
  • Where are those classes: DSALinkedList, DSAGraphVertex? What does the getAdjacent(..) method do? Could you post a functionality complete example? – Paolof76 Oct 16 '20 at 04:13
  • DSALinkedList is equivalent to the 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

3 Answers3

0

It's not an issue in the recursion logic, otherwise you'd get a stackoverflow exception instead of full heap. It sounds like you're traversing the same nodes of the graph in cycles.

Without seeing the dataset and the whole logic, is difficult to understand the problem, but try to implement a check to avoid cycles during the walk (if a node is aready been visited, stop) and see if this solves the issue.

Paolof76
  • 889
  • 1
  • 9
  • 23
0

I also face this kind of issue while collecting large amount of html as string. I think you should use StringBuilder Class instead of String because Everytime you append String new Object is created.

If your code breaks in production/testing Environment (Not in Local), then you should check Memory and CPU allocation of server as well as which kind of Garbage Collector is used. (I used G1GC).

0

Do you have any clue how many possibilities exist for a vertex having 1000 bounds? Try to get an answer to that question and rethink about your application.

paladin
  • 765
  • 6
  • 13