java - 通过邻接表进行前序遍历

标签 java graph depth-first-search adjacency-list preorder

我正在尝试将我的EdgeList转换为邻接列表,然后预序遍历通过它。我非常确定到邻接列表的转换工作正常,但我在预序遍历时遇到了麻烦。我尝试使用 DFS 来完成此操作,但它给出了错误的结果。

这是我的优势:

{index1=0, index2=2}
{index1=3, index2=4}
{index1=1, index2=4}
{index1=0, index2=5}
{index1=2, index2=6}
{index1=1, index2=5}
{index1=2, index2=7}

这就是链接列表的样子。

0 - 2 - 5
1 - 4 - 5
2 - 6 - 7
3 - 4

或者

linked[2, 5]
linked[4, 5]
linked[6, 7]
linked[4]

现在我想预先排序遍历图表,以获得结果0 - 2 - 6 - 7 - 5 - 1 - 4 - 3

          0
      2       5
   6    7       1
                  4
                   3

我尝试在我的 AdjacencyListGraph 中使用 DFS,但这是我使用以下代码得到的 0 2 6 7 5 的结果:

public class AdjacencyListGraph {

    private int V;   // No. of vertices

    // Array  of lists for Adjacency List Representation
    private LinkedList<Integer> adj[];

    // Constructor
    AdjacencyListGraph(int v) {
        V = v;
        adj = new LinkedList[v];
        for (int i=0; i<v; ++i)
            adj[i] = new LinkedList();
    }

    //Function to add an edge into the graph
    void addEdge(int v, int w) {
        adj[v].add(w);  // Add w to v's list.
    }


    // A function used by DFS
    void DFSUtil(int v,boolean visited[])
    {
        // Mark the current node as visited and print it
        visited[v] = true;
        System.out.print(v+" ");

        // Recur for all the vertices adjacent to this vertex
        Iterator<Integer> i = adj[v].listIterator();
//        System.out.println(i.toString());
        while (i.hasNext())
        {
            int n = i.next();
            if (!visited[n])
                DFSUtil(n,visited);
        }
    }

    // The function to do DFS traversal. It uses recursive DFSUtil()
    void DFS(int v)
    {

        // Mark all the vertices as not visited(set as
        // false by default in java)
        boolean visited[] = new boolean[V];

        // Call the recursive helper function to print DFS traversal
        // starting from all vertices one by one

        DFSUtil(v, visited);
    }

    public static void main(String[] args) {
        AdjacencyListGraph graph = new AdjacencyListGraph(8);

        graph.addEdge(0, 2);
        graph.addEdge(0, 5);
        graph.addEdge(1, 4);
        graph.addEdge(1, 5);
        graph.addEdge(2, 6);
        graph.addEdge(2, 7);
        graph.addEdge(3, 4);

        graph.DFS(0);
    }
}

我也尝试过使用:

void DFS() 
{ 
    // Mark all the vertices as not visited(set as 
    // false by default in java) 
    boolean visited[] = new boolean[V]; 

    // Call the recursive helper function to print DFS traversal 
    // starting from all vertices one by one 
    for (int i=0; i<V; ++i) 
        if (visited[i] == false) 
            DFSUtil(i, visited); 
} 

它不是上面提到的,而是遍历所有节点,但它以错误的顺序返回结果。

我做错了什么?我应该如何或使用什么来获得所需的结果?

最佳答案

尝试使用此图:

graph.addEdge(0, 2);
graph.addEdge(0, 5);
graph.addEdge(1, 4);
graph.addEdge(5, 1); // different direction than original
graph.addEdge(2, 6);
graph.addEdge(2, 7);
graph.addEdge(4, 3); // different direction than original

或者,对程序中的 addEdge 方法进行轻微修改:

//Function to add an edge into the graph
void addEdge(int v, int w) {
    adj[v].add(w);  // Add w to v's list.
    adj[w].add(v);  // Add v to w's list.
}

关于java - 通过邻接表进行前序遍历,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53612919/

相关文章:

graph - IDA Pro 5.0 - 图形太大,超过 1000 个节点)无法显示在屏幕上

python - DFS 中(非常慢的)深度复制的任何替代方法?

java - 我使用 DFS 的 hasCycle() 方法出了什么问题?

java - android.content.res.Resources$NotFoundException : Resource ID #0x88a6fd

java - 如何执行我的 java 桌面应用程序的单个实例?

java - 树的分区压缩以及如何将节点压缩到根

java - 使用深度优先搜索查找到节点的唯一路由数

java - JSP 输入到 servlet 为空

java - 如何连接其他机器安装Elasticsearch服务器?

r - ggplotly如何更改工具提示和右侧的文本