深度优先搜索(Depth-First Search,DFS)是一种图遍历算法,它从起始节点开始,沿着一条路径尽可能深入,直到达到最深的节点,然后回溯到前一节点,继续探索下一条路径。DFS通常使用递归或栈(非递归)来实现。
以下是DFS算法的基本步骤:
function DFS(node):
if node is not visited:
mark node as visited
for each neighbor of node:
DFS(neighbor)
DFS的非递归实现通常使用栈来模拟递归调用的过程,具体步骤如下:
注意:在处理连通图时可能导致栈溢出,因此在实际应用中可能需要注意栈深度的问题。
伪代码如下:
function DFS_non_recursive(start):
stack = empty stack
push start onto stack
while stack is not empty:
current = pop from stack
if current is not visited:
mark current as visited
for each neighbor of current:
push neighbor onto stack
由于可能存在非常多的路径,我们设置一个maxLength,表示限制经过的节点个数。
void Graph::DFS(int current, int end, std::unordered_set<int>& visited, std::vector<int>& path, int maxLength)
{
static int currentLength = 0;
static int i = 1;
visited.insert(current);
path.push_back(current);
if (path.size() <= maxLength)
{
if (current == end)
{
// 生成路径
for (int node : path)
{
std::cout<<node<<' ';
}
std::cout<<"路径总长度为:"<<currentLength<<std::endl;
}
else
{
for (int neighbor = 0; neighbor < V; ++neighbor)
{
if (adjMatrix[current][neighbor] != INF && visited.find(neighbor) == visited.end())
{
int edgeWeight = adjMatrixLength[current][neighbor];
currentLength += edgeWeight;
DFS(neighbor, end, visited, path, maxLength);
currentLength -= edgeWeight; // 回溯时减去当前边的长度
}
}
}
}
visited.erase(current);
path.pop_back();
}