1

I am using depth first search on an undirected graph to find paths from a starting node to an ending node. All the nodes along the path are stored in an ArrayList. But since there can be multiple paths going from start to end, I decided to store each of the ArrayLists that contain the nodes along the paths to another ArrayList, So the second ArrayList should contain all the solution ArrayLists.

However, when I tested the code out, it always ends up with the ArrayLists been empty for some reason, and I am not sure when that is. This is my code for the DFS traversal:

private void pathDepthFirstSearch(Graph graph, GraphNode u, GraphNode v) throws GraphException {
    listOfNodes.add(u); //Adds the starting node to the ArrayList
    u.setMark(true); //Marks it as visited.
    if(u.getName() == v.getName()) { //Managed to get to the end node.(.getName() returns int values).
        listOfSolutions.add(listOfNodes); //Add this Path solution to the ArrayList that contains a list of solutions.
        numOfSolutions++; //Number of solutions increment by 1.
    } else {
        for (Iterator<GraphEdge> iter = graph.incidentEdges(u); iter.hasNext();) {
            GraphEdge nextEdge = iter.next();
            GraphNode secondEndPoint = nextEdge.secondEndpoint();
            if(secondEndPoint.getMark() == false) { //Checks if the end point of the edge has been visited or not, if not, then call the method recursively.
                pathDepthFirstSearch(graph,secondEndPoint, v);
            }
        }
    }
    listOfNodes.remove(u); //Finished exploring u, so remove it from the Solution ArrayList.
    u.setMark(false); //Mark as un-visited.
}

And the code for calling the function with startNode and endNode specified:

private List<GraphNode> listOfNodes = new ArrayList<GraphNode>(); //The ArrayList to store one path solution.
private List<List<GraphNode>> listOfSolutions = new ArrayList<>(); //The ArrayList to store all the ArrayList path solutions.
public Iterator<GraphNode> solve() throws GraphException {
    GraphNode startNode = new GraphNode(startLoc); //Creates the starting node.
    GraphNode endNode = new GraphNode(endLoc); //Creates the end node.
    pathDepthFirstSearch(graph, startNode, endNode);
    //returns one of the solutions from the ArrayList of solutions (listOfSolutions) 
    //depending on if we want the longest or shortest path. If the list is empty, 
    //then null is returned instead.
}

After running this in my main function that prints out the nodes from the ArrayList that is returned from the solve() method, I get the message "no solution is found", which I set it to only happen when the method returned null.

How come the ArrayList is empty, even though in the DFS method, nodes should have been added to it, and then the list is added to the listOfSolutions?

halfer
  • 19,824
  • 17
  • 99
  • 186
Pengibaby
  • 373
  • 2
  • 11
  • 1
    i guress `u.getName()` returns a string? In this case use `equals` not `==` – Jens Nov 30 '18 at 09:05
  • @Jens sorry, I should have specified that. the nodes store integer values, which is why I stuck with "==". – Pengibaby Nov 30 '18 at 09:07
  • int or integer? – Roy Shahaf Nov 30 '18 at 09:08
  • @RoyShahaf int. – Pengibaby Nov 30 '18 at 09:09
  • @Pengibaby Have you tried to debug the code? – Jens Nov 30 '18 at 09:10
  • With partial code it can be difficult to help but I would point out you only seem to create a list once. That is: after finding a solution you add the list to the list of solutions but you don't create a new list. you later remove elements from the list. so basically you add the same list for each solution (so you practically add one list to the list of solutions) and you empty it at the end. – Roy Shahaf Nov 30 '18 at 09:15
  • @Jens I used some print function to help me out but didn't find anything wrong :/ – Pengibaby Nov 30 '18 at 09:16
  • 1
    @Pengibaby Using a Debugger is more helpful – Jens Nov 30 '18 at 09:17
  • @RoyShahaf Where would I create the new list? would it be in the DFS function? Also I thought the remove part only removed the nodes that does not reach the end destination? – Pengibaby Nov 30 '18 at 09:17
  • 1
    change listOfSolutions.add(listOfNodes); to listOfSolutions.add(new ArrayList<>(listOfNodes)); – Roy Shahaf Nov 30 '18 at 09:19
  • @Jens I will try doing that and see if i can find anything. Thank you :) – Pengibaby Nov 30 '18 at 09:26
  • @RoyShahaf I will try that. what about the remove(u) part? Do I need to do anything with that? – Pengibaby Nov 30 '18 at 09:27
  • 1
    if you do what i said, listOfNodes becomes a representation of the current path and so you should still remove nodes from it when going "back" – Roy Shahaf Nov 30 '18 at 09:40
  • @RoyShahaf Thank you for the explanation and help. However I still get the same thing after making the change. So I am really confused. I am assuming from the DFS code I have written, the order in which the nodes are added to the list is the same order as the path would take right? So each consecutive node would be adjacent with each other? That's how I intended for it to work any way. – Pengibaby Nov 30 '18 at 09:50
  • Note we prefer a technical style of writing here. We gently discourage greetings, hope-you-can-helps, thanks, advance thanks, notes of appreciation, regards, kind regards, signatures, please-can-you-helps, chatty material and abbreviated txtspk, pleading, how long you've been stuck, voting advice, meta commentary, etc. Just explain your problem, and show what you've tried, what you expected, and what actually happened. – halfer Dec 01 '18 at 09:22

0 Answers0