java - 通过邻接表的前序遍历
问题描述
我正在尝试将 my 转换EdgeList
为 aAdjacency List
然后preorder traverse
通过它。我很确定转换Adjacency List
为正常工作,但我在preorder traversing
通过它时遇到了麻烦。我试图用 来做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
我试过DFS
在我的 中使用AdjacencyListGraph
,但这是我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);
}
而不是上面提到的,它遍历所有的nodes
,但它以错误的顺序返回结果。
我做错了什么,我应该如何或应该使用什么来获得预期的结果?
解决方案
试试这张图:
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.
}
推荐阅读
- c# - SQL 数据库未保存对表的更改
- javascript - 使用 angular-chart.js 时如何增加图例和图表之间的空间?
- html - TranslateY 在某些浏览器上滚动闪烁
- sql - 仅在特定字段上连接两个 SQL 表
- python - 每次在 for 循环中循环时如何遍历不同的字典
- twilio - 有没有办法在 Twilio 中禁用我可以用未经验证的号码拨打电话?
- python - 使用 spacy 从文档中删除命名实体
- c++ - 如何将 CNG 密钥转换为 OpenSSL EVP_PKEY(反之亦然)?
- ios - 试图创建一个彩色圆圈 UIImage,但它总是以方形结束。为什么?
- django - Django Query 获取 ArrayField 列的所有不同值的计数