我一直在尝试在业余时间了解有关图遍历的更多信息,并且我尝试使用深度优先搜索来查找无向,强连接图中的起始节点和结束节点之间的所有简单路径。到目前为止,我一直在使用Print all paths from a given source to a destination中的代码,该代码仅用于有向图。
使用递归DFS的主算法发生在以下两个函数中:
void Graph::printAllPaths(int s, int d)
{
// Mark all the vertices as not visited
bool *visited = new bool[V];
// Create an array to store paths
int *path = new int[V];
int path_index = 0; // Initialize path[] as empty
// Initialize all vertices as not visited
for (int i = 0; i < V; i++)
visited[i] = false;
// Call the recursive helper function to print all paths
printAllPathsUtil(s, d, visited, path, path_index);
}
// A recursive function to print all paths from 'u' to 'd'.
// visited[] keeps track of vertices in current path.
// path[] stores actual vertices and path_index is current
// index in path[]
void Graph::printAllPathsUtil(int u, int d, bool visited[],
int path[], int &path_index)
{
// Mark the current node and store it in path[]
visited[u] = true;
path[path_index] = u;
path_index++;
// If current vertex is same as destination, then print
// current path[]
if (u == d)
{
for (int i = 0; i<path_index; i++)
cout << path[i] << " ";
cout << endl;
}
else // If current vertex is not destination
{
// Recur for all the vertices adjacent to current vertex
list<int>::iterator i;
for (i = adj[u].begin(); i != adj[u].end(); ++i)
if (!visited[*i])
printAllPathsUtil(*i, d, visited, path, path_index);
}
// Remove current vertex from path[] and mark it as unvisited
path_index--;
visited[u] = false;
}
哪个适用于有向图,但不适用于无向,强连接的图。
我想知道他们是否有办法调整此代码以适用于无向图?我有一种感觉需要更多的回溯来探索更多可能的路径,但我不确定如何处理这个问题。
任何帮助将不胜感激。
答案 0 :(得分:2)
RoadRunner,你确定你所展示的代码中有“无向问题”吗?乍一看看起来还不错。也许这个错误来自于你没有修复addEdge
来创建你创建的图表,例如:
void Graph::addEdge(int u, int v)
{
adj[u].push_back(v);
adj[v].push_back(u); // Fix: add back edge as well!
}
更新(C代码,但非常难看)
好的,这是我尝试将代码转换为纯C.显然代码风格很难看,根本没有错误检查,但是你可以改进它,因为我希望你能更精通C语言。另外,我只是滚动我的简单自定义链接列表的图形节点有一个奇怪的名称NodeListNode
,即包含图形节点的ListNode。
<强> Graph.h
#pragma once
#ifdef __cplusplus
extern "C" { // only need to export C interface if
// used by C++ source code
#endif
typedef struct tagNodeListNode {
struct tagNodeListNode* next;
int index;
} NodeListNode;
typedef struct tagGraph {
int nodesCount;
NodeListNode** adjArr;
} Graph;
typedef void(*GraphPathVisitorFunc)(NodeListNode const* const path);
Graph GraphCreate(int nodesCount);
void GraphDestroy(Graph gr);
void GraphAddEdge(Graph gr, int u, int v);
void GraphVisitAllPaths(Graph gr, int s, int d, GraphPathVisitorFunc visitor);
void GraphPrintAllPaths(Graph gr, int s, int d);
#ifdef __cplusplus
}
#endif
<强> Graph.c
#include "Graph.h"
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
Graph GraphCreate(int nodesCount)
{
// calloc ensures zeroing array
NodeListNode** adjArr = (NodeListNode**)calloc(nodesCount, sizeof(NodeListNode*));
Graph gr = { nodesCount, adjArr };
return gr;
}
void GraphDestroy(Graph gr)
{
for (int i = 0; i < gr.nodesCount; i++)
{
for (NodeListNode* adj = gr.adjArr[i]; adj != NULL;)
{
NodeListNode* tmp = adj;
adj = adj->next; //first move on the free
free(tmp);
}
}
free(gr.adjArr);
}
void GraphAddEdgeImplFirst(Graph gr, int from, int to)
{
NodeListNode* adj = gr.adjArr[from];
NodeListNode* n = (NodeListNode*)malloc(sizeof(NodeListNode));
n->next = adj;
n->index = to;
gr.adjArr[from] = n;
}
void GraphAddEdgeImplLast(Graph gr, int from, int to)
{
NodeListNode* adj = gr.adjArr[from];
NodeListNode* n = (NodeListNode*)malloc(sizeof(NodeListNode));
n->next = NULL;
n->index = to;
if(adj == NULL)
{
gr.adjArr[from] = n;
}
else
{
while (adj->next != NULL)
adj = adj->next;
adj->next = n;
}
}
void GraphAddEdge(Graph gr, int u, int v)
{
GraphAddEdgeImplFirst(gr, u, v);
GraphAddEdgeImplFirst(gr, v, u);
// closer to https://ideone.com/u3WoIJ but slower and thus makes no sense
//GraphAddEdgeImplLast(gr, u, v);
//GraphAddEdgeImplLast(gr, v, u);
}
void GraphVisitAllPathsImpl(Graph gr, int cur, int dst, GraphPathVisitorFunc visitor, NodeListNode* pathFst, NodeListNode* pathLst, bool* visited)
{
if (cur == dst)
{
visitor(pathFst);
return;
}
NodeListNode* adj = gr.adjArr[cur];
for (NodeListNode const* tmp = adj; tmp != NULL; tmp = tmp->next)
{
int next = tmp->index;
if (visited[next])
continue;
visited[next] = true;
NodeListNode nextNode = { NULL,next };
pathLst->next = &nextNode;
GraphVisitAllPathsImpl(gr, next, dst, visitor, pathFst, &nextNode, visited);
pathLst->next = NULL;
visited[next] = false;
}
}
void GraphVisitAllPaths(Graph gr, int start, int dst, GraphPathVisitorFunc visitor)
{
bool* visited = calloc(gr.nodesCount, sizeof(bool));
visited[start] = true;
NodeListNode node = { NULL,start };
GraphVisitAllPathsImpl(gr, start, dst, visitor, &node, &node, visited);
free(visited);
}
void PrintPath(NodeListNode const* const path)
{
for (NodeListNode const* tmp = path; tmp != NULL; tmp = tmp->next)
{
printf("%d ", tmp->index);
}
printf("\n");
}
void GraphPrintAllPaths(Graph gr, int s, int d)
{
GraphVisitAllPaths(gr, s, d, PrintPath);
}
使用示例,其图形与您的ideaone示例相同。请注意,要获得匹配的输出,您应该使用GraphAddEdgeImplLast
而不是GraphAddEdgeImplFirst
,否则结果将按相反的顺序排列。
void testGraph()
{
Graph gr = GraphCreate(20);
GraphAddEdge(gr, 0, 1);
GraphAddEdge(gr, 0, 7);
GraphAddEdge(gr, 1, 2);
GraphAddEdge(gr, 1, 6);
GraphAddEdge(gr, 1, 5);
GraphAddEdge(gr, 2, 3);
GraphAddEdge(gr, 2, 5);
GraphAddEdge(gr, 3, 4);
GraphAddEdge(gr, 3, 5);
GraphAddEdge(gr, 4, 5);
GraphAddEdge(gr, 4, 10);
GraphAddEdge(gr, 4, 11);
GraphAddEdge(gr, 5, 6);
GraphAddEdge(gr, 5, 10);
GraphAddEdge(gr, 5, 11);
GraphAddEdge(gr, 6, 7);
GraphAddEdge(gr, 6, 8);
GraphAddEdge(gr, 6, 9);
GraphAddEdge(gr, 6, 10);
GraphAddEdge(gr, 7, 8);
GraphAddEdge(gr, 8, 9);
GraphAddEdge(gr, 8, 13);
GraphAddEdge(gr, 9, 10);
GraphAddEdge(gr, 9, 13);
GraphAddEdge(gr, 9, 12);
GraphAddEdge(gr, 10, 12);
GraphAddEdge(gr, 11, 12);
GraphAddEdge(gr, 12, 13);
GraphAddEdge(gr, 12, 14);
GraphAddEdge(gr, 12, 16);
GraphAddEdge(gr, 13, 14);
GraphAddEdge(gr, 14, 15);
GraphAddEdge(gr, 16, 17);
GraphAddEdge(gr, 15, 17);
GraphAddEdge(gr, 15, 19);
GraphAddEdge(gr, 17, 18);
GraphAddEdge(gr, 17, 19);
GraphAddEdge(gr, 18, 19);
GraphPrintAllPaths(gr, 12, 4);
GraphDestroy(gr);
}
希望这有帮助