【发布时间】:2018-04-03 04:21:13
【问题描述】:
我正在尝试使用邻接矩阵查找起始顶点和结束顶点之间的所有路径。我的图表是未加权且无向的。
我试图遵循这个算法,但是我在每个部分都卡住了。
算法:
procedure FindAllPaths(u, dest)
{
push u to stack;
if(u == dest)
{
print stack;
}
else
{
foreach v that is adjacent with u and not in stack now
{
FindAllPaths(v, dest);
}
}
pop from stack;
}
我的代码:
void AdjacencyMatrix :: Find_All_Paths(int Origin, int Destination)
{
/*
MATRIX:
0 1 0 1 1
0 1 0 1 1
0 0 0 1 0
0 1 1 1 0
1 1 0 1 1
*/
//Push Origin to stack
Push_Vertex.push(Origin);
//Determine if Origin == Destination, if so, print the stack
if(Origin == Destination)
{
while(!Push_Vertex.empty())
{
cout << Push_Vertex.top() << " ";
Push_Vertex.pop();
}//while
cout << endl;
}//if
else
}//Find_All_Paths()
【问题讨论】:
-
这使用了
DFS,但是你没有在循环内调用FindAllPaths。如果您只使用int,您究竟如何检查节点是否相邻。 -
@codekaizer DFS 实际上。
-
@FeiXiang,哦,对了。被
adjacent这个词蒙蔽了双眼 -
我忘了添加检查以查看顶点是否已经在堆栈上,请参阅我编辑的答案。
标签: c++ graph-theory adjacency-matrix