简体   繁体   中英

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. We want the output of our program to provide the order of visited nodes.

The java code: http://pastebin.com/bAzBadxi

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

My question is, what do we initialize "n" to? (line 32 in the java code)

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.

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.

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