简体   繁体   English

邻接矩阵->有向图-> DFS

[英]Adjacency Matrix -> Directed graph -> DFS

This is the code my friends and I have come up with so far after fiddling around. 这是我的朋友和我经过反复研究后提出的代码。 What we are trying to do is read in the adjacency matrix (input.txt), then create a directed graph out of it so we can search it using Depth-First Search. 我们试图做的是读取邻接矩阵(input.txt),然后从中创建一个有向图,以便我们可以使用“深度优先搜索”进行搜索。 We want the output of our program to provide the order of visited nodes. 我们希望程序的输出提供被访问节点的顺序。

The java code: http://pastebin.com/bAzBadxi Java代码: http//pastebin.com/bAzBadxi

The input.txt file: http://pastebin.com/r72J34uA input.txt文件: http//pastebin.com/r72J34uA

My question is, what do we initialize "n" to? 我的问题是,我们将“ n”初始化为什么? (line 32 in the java code) (Java代码中的第32行)

Any help will be appreciated. 任何帮助将不胜感激。

Create a vertex object before you use it. 在使用之前创建一个顶点对象。

Vertex n;    // before g.addVertex(n);

I am not validating your algorithm, just removing compiler error, if your algo is correct it should work fine 我没有验证您的算法,只是删除了编译器错误,如果您的算法正确,它应该可以正常工作

What you are trying to solve is a problem of topological sorting. 您要解决的是拓扑排序问题。

Topological sorting 拓扑排序

In this case, it doesn't matter what n you initialize to, you can simply use the first vertex in the adjacency matrix as the start. 在这种情况下,n初始化为什么无关紧要,您可以简单地使用邻接矩阵中的第一个顶点作为起点。

And adjacency matrix (which should be a square matrix) is a legit representation of a directed graph, you can use the matrix to search the graph directly. 邻接矩阵(应该是方矩阵)是有向图的合法表示,您可以使用矩阵直接搜索图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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