我使用基於鄰接的多變量列表的列表製作圖。我銷售了所有創建數據結構的特定函數,但我無法實現在兩點之間搜索所有可能的路線((我想了很多,但無法弄清楚如何獲得所有方法,而不僅僅是一個。我知道我必須使用BFS,但我不能想出什麼(如何搜索鏈接列表中兩個節點之間的所有路徑圖
#include <iostream>
#include <string>
using namespace::std;
typedef string graphElement;
typedef struct vertexTag {
graphElement data;
int visited;
struct edgeTag* edges;
struct vertexTag* next;
struct vertexTag* prev;
} vertexT;
typedef struct edgeTag {
struct vertexTag* connectsTo;
struct edgeTag* next;
} edgeT;
class graph {
private:
vertexT* head;
vertexT* tail;
vertexT curr;
int count_vertex;
queue<vertexT*> queue;
void BFS(graphElement destenetion, vertexT* startP);
vertexT* FindVertex(graphElement data);
public:
graph();
~graph();
vertexT* AddVertex(graphElement data);
void DeleteVertex(graphElement data);
edgeT* AddEdge(graphElement source, graphElement destination);
void DeleteEdge(graphElement source, graphElement destination);
void PrintGraph();
void DiskIn();
void DiskOut();
void FindAllPaths(graphElement source, graphElement destenetion);
};
是BFS我tryed作出((我知道它不是那麼好:(
void graph::FindAllPaths(graphElement source, graphElement destenetion) {
vertexT *vertP;
vertexT *startP = NULL;
for (vertP = head; vertP != NULL; vertP = vertP->next) {
vertP->visited = 0;
if (vertP->data == source)
startP = vertP;
}
if (startP == NULL)
{
cout << "No such vertex";
return;
}
else
{
BFS(destenetion, startP);
}
}
void graph::BFS(graphElement destenetion, vertexT* startP) {
vertexT* current;
edgeT* edgeP;
vector<string>path;
queue.push(startP);
//startP->visited = true;
while (!queue.empty()) {
current = queue.front();
queue.pop();
if (current->data == destenetion)
copy(path.begin(), path.end(), ostream_iterator<string>(cout, " "));
for (edgeP = startP->edges; edgeP != NULL; edgeP = edgeP->next) {
//if (!edgeP->connectsTo->visited) {
queue.push(edgeP->connectsTo);
edgeP->connectsTo->visited = true;
path.push_back(edgeP->connectsTo->data);
// }
}
}
}
看起來你的標題是誤導性的,你的問題是關於圖上的搜索,而不是刪除邊緣,對吧? – code11
oops yap你說得對,這是老問題,我自己完成了:) –
你可以使用DFS而不是BFS ,每次到達想要的頂點時保存該路徑,每條路徑都是唯一的。 –