0

I'm looking for a way to trace a path for my bfs-algorithm, but I don't know how. Maybe I'm wrong, but I think I do need a duble ArrayList, to save every possible way. That means in the following example:

enter image description here

  1. arraylist: 6-3-1
  2. arraylist: 6-3-5
  3. arraylist: 5-9-7
  4. arraylist: 6-9-10

And this would even look worse, if there would be a connection from 3 to 9.

    public List<E> BFS(N source, N target)
    {
        ArrayList<ArrayList<N>> nodes_paths = new ArrayList<ArrayList<N>>();
        ArrayList<N> nodes_list = new ArrayList<N>(graph.getNodes());
        N current_node;

        Queue <N> list = new LinkedList<N>();
        HashMap<N, Boolean> already_visited = new HashMap<N, Boolean>();
//      mark all nodes in HashMap as not visited
        for(int i=0; i<nodes_list.size(); i++)
        {
            already_visited.put(nodes_list.get(i), false);
        }

        ArrayList<N> successors = new ArrayList<N>();
        list.add(source);

        while(!list.isEmpty())
        {
            current_node = list.poll();
            already_visited.put(current_node, true);
          //need to add the node to any path, but don't know to which?
            if(current_node.equals(target))
            {

            }
            else
            {
                successors = new ArrayList<N>(graph.getSuccessors(current_node));
                for(int j=0; j<successors.size(); j++)
                {
                    if(!already_visited.get(successors.get(j)))
                    {
                        already_visited.put(successors.get(j), true);
                        list.add(successors.get(j));
                    }
                }
            }
        }


//      need the path array here!
        ArrayList<E> edges_list = new ArrayList<E>(graph.getEdges());
        ArrayList<E> path_edges = new ArrayList<E>();
        for(int k=0; k<path.size()-1; k++)  //If there are path.size nodes, so therer are only path.size-1 edges
        {
            for(int l=0; l<edges_list.size(); l++)
            {
                if(path.get(k).equals(edges_list.get(l).getSource()) && path.get(k+1).equals(edges_list.get(l).getTarget()))
                {
                    path_edges.add(edges_list.get(l));
                }
            }
        }

        return path_edges;


    }
Johnny
  • 146
  • 14

1 Answers1

2

This approach would get complex managing, you could store a map:V->V [from vertices to vertices], which will map from each node v, the vertex u that "discovered" v.

You will populate this map during the iterations of BFS.

Later - you can reconstruct the path by simply going from the target node [in the map] - up until you get back to the source. This map can be implemented as an array, if you enumerate the vertices.

Rishi
  • 1,163
  • 7
  • 12
  • Also known as a "predecessor" or "parent" array. You can find it in CLRS in the section on BFS (section 22.2 in 2nd and 3rd editions). – beaker Mar 14 '16 at 14:30