gpt4 book ai didi

c++ - 使用带两个链表的邻接表的深度优先搜索 C++ 实现

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:15:55 26 4
gpt4 key购买 nike

我是一名大学生,这是我第一次在这里提问。首先感谢您抽出时间阅读。我们有一个关于图形的项目。到目前为止,我通过 matrix 和 adjacency list 创建了图形。我的邻接列表版本由 2 个链表组成(只是为了允许添加尽可能多的顶点)。现在我正在尝试进行 Depth First Search 。我做了一些研究,认真地我不太了解,所以我按照自己的想法行事,但我不确定它是否正确,或者复杂性是否为 O(V+E) v 指的是顶点,E 指的是edges.. 任何人都可以阅读它并告诉我它是否正确?如果是,那么复杂度 O(V+E) 是多少?PS:请只使用 c++,因为它是我唯一知道的语言。非常感谢您的帮助

我在DFS做的是

1-执行遍历所有顶点并在每个顶点上调用 DFS 的 while 循环2- 在每个顶点上,调用 DFS,顶点的颜色将为灰色 ('g')3-另一个遍历目标顶点(边)子节点的 while 循环4-在每个 children 上,另一个 while 循环将通过 search-> value == child->value
搜索相应的边5-当它找到顶点时它会再次调用 DFS(所以重复步骤 2 到 5)6-完成后,一个接一个地给顶点涂上黑色

代码如下:

边缘节点.h

class EdgeNode
{
public:
int value;
EdgeNode *Next;
};

顶点节点.h

#include "EdgeNode.h"
class VertexNode
{
public:
int value;
int parentIndex;
char color;
EdgeNode *Child;
VertexNode *Next;
};

图.h

#include "VertexNode.h"

class Graph{
public:

int NbVertex;
int time;
VertexNode *s;
Graph();
void AddVertex(int value);
bool AddEdge(int parentIndex, int value);
void PrintVertex();
void PrintGraph();
void DepthFirstSearch();
void DFS(VertexNode *V);


};

图表.cpp

#include <iostream>
#include "Graph.h"
#include <string>
using namespace std;


Graph::Graph()
{
s = NULL;
NbVertex = 0;
}

void Graph::AddVertex(int value)
{
if (NbVertex == 0)
{
s = new VertexNode;
s->value = value;
s->parentIndex = NbVertex+1;
s->color = 'w';
s->Child = NULL;
s->Next = NULL;
}

else
{
VertexNode *z = s;
while (z->Next != NULL)
{
z = z->Next;
}
z->Next = new VertexNode;
z->Next->parentIndex = NbVertex+1;
z->Next->color = 'w';
z->Next->value = value;
z->Next->Child = NULL;
z->Next->Next = NULL;
}


NbVertex++;
}


bool Graph::AddEdge(int parentIndex, int value)
{
if (NbVertex == 0)
cout << "Graph is empty" << endl;

VertexNode *z = s;

while (z != NULL)
{
if (z->parentIndex == parentIndex)
{


if (z->Child == NULL)
{
EdgeNode *y = new EdgeNode;
y->value = value;
y->Next = NULL;
z->Child = y;

}

else
{
EdgeNode *y = z->Child;
while (y->Next != NULL)
{
if (y->value == value)
{
cout << "The edge already exists"<<endl;
return false;
}

y = y->Next;
}
y->Next = new EdgeNode;
y->Next->value = value;
y->Next->Next = NULL;

}

return true;

}

z = z->Next;

}

cout << "The index was not found" << endl;
return false;

}


void Graph :: PrintVertex()
{
VertexNode *z = s;
int count = 1;
while (z != NULL)
{
cout << "vertex " << count << " : " << endl;
cout << "Value : " << z->value << endl;
cout << "Index : " << z->parentIndex << endl;
cout << "Next : " << z->Next << endl;
cout << "Child : " << z->Child << endl;
cout << "Color : " << z->color << endl;
cout << endl;
count++;
z = z->Next;
}

cout << "time is : " << time;
}


void Graph::PrintGraph()
{
VertexNode *z = s;
int countVertex = 1;
while (z != NULL)
{
cout << "vertex " << countVertex << " : " << endl;
cout << "Value : " << z->value << endl;
cout << "Index : " << z->parentIndex << endl;
cout << "Next : " << z->Next << endl;
cout << "Child : " << z->Child << endl;
cout << endl;
countVertex++;
if (z->Child != NULL)
{
int countChild=1;
EdgeNode *y = z->Child;
while (y != NULL)
{
cout << "Child " << countChild << " : " << endl;
cout << "Value : " << y->value << endl;
cout <<"Next : " << y->Next << endl;
cout << "-------------------"<<endl;
countChild++;
y = y->Next;
}

}

cout << "*************************************"<<endl;

z = z->Next;
}
}



void Graph :: DepthFirstSearch()
{
time = 0;
VertexNode *z = s;
while (z != NULL)
{
if (z->color == 'w')
{
DFS(z);
}
z = z->Next;
}

}
void Graph::DFS(VertexNode *V)
{
cout << "(" << V->value;
V->color = 'g';
time++;

EdgeNode *Y = V->Child;
while (Y != NULL)
{
VertexNode *Search = s;
while (Search != NULL)
{
if (Search->value == Y->value)
{
if (Search->color == 'w')
{
DFS(Search);
}
/*else
{
Search->color = 'b';
}*/
}

Search = Search->Next;

}

Y = Y->Next;
}

V->color = 'b';
time++;
cout << V->value << ")";
}

这是DFS

void Graph :: DepthFirstSearch()
{
time = 0;
VertexNode *z = s;
while (z != NULL)
{
if (z->color == 'w')
{
DFS(z);
}
z = z->Next;
}

}
void Graph::DFS(VertexNode *V)
{
cout << "(" << V->value;
V->color = 'g';
time++;

EdgeNode *Y = V->Child;
while (Y != NULL)
{
VertexNode *Search = s;
while (Search != NULL)
{
if (Search->value == Y->value)
{
if (Search->color == 'w')
{
DFS(Search);
}
/*else
{
Search->color = 'b';
}*/
}

Search = Search->Next;

}

Y = Y->Next;
}

V->color = 'b';
time++;
cout << V->value << ")";
}

最佳答案

我不知道你的项目应该如何组织,但是用邻接表构建的图的代码大约需要 10 行。

快速查看一下,您的 DFS 似乎是正确的。理论上,DFS(或 BFS)时间是 O(V + E),因为它会通过所有顶点和所有边(总计 O(|V| + |E|)。

如果您关心代码阅读和组织,请继续,但如果您更喜欢更少的代码,我相信您可以找到更小的图形实现。

有一个 5 行的 DFS 实现可以正常工作。

void DFS(graph G, int v){
mark[v] = true;
cout<<"Visiting "<<v<<endl;
for(auto u : G[v]) //this means "for all u in the adjacence list of v"
if(!mark[u]) DFS(G, u);
}

关于c++ - 使用带两个链表的邻接表的深度优先搜索 C++ 实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41068265/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com