简体   繁体   中英

How to trace path during algorithm BFS

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:

在此处输入图片说明

  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;


    }

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM