[英]All simple paths of an undirected, strongly connected graph
我一直在嘗試在業余時間了解有關圖遍歷的更多信息,並且嘗試使用深度優先搜索來查找無向,強連接圖中起點和終點之間的所有簡單路徑。 到目前為止,我一直在使用“ 打印從給定源到目標的所有路徑”中的此代碼,這僅用於有向圖。
使用遞歸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;
}
對於有向圖,但對於無向,強連接圖,該方法很好用。
我想知道他們是否是一種方法來調整此代碼,使其也可用於無向圖? 我感覺需要更多的回溯來探索更多可能的路徑,但是不確定如何實現這一點。
任何幫助,將不勝感激。
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。
圖
#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
圖
#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);
}
希望這可以幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.