#include<iostream>
#include<fstream>
#include<vector>
#include<algorithm>
#include<string>
#include<map>
#include<queue>
#include<functional>
using namespace std;
ifstream inFile("C:\\Users\\DELL\\Desktop\\in.txt", ios::in);
const int MaxVertexNum = 100;
typedef int Vertex;
typedef int WeightType;
typedef char DatatType;
//边的定义
typedef struct ENode *PtrToENode;
struct ENode{
Vertex V1, V2;
WeightType Weight;
};
typedef PtrToENode Edge;
//邻接点的定义
typedef struct AdjVNode *PtrToAdjVNode;
struct AdjVNode{
WeightType Weight;
Vertex AdjV;
PtrToAdjVNode Next;
};
//顶点表头结点的定义
typedef struct VNode{
PtrToAdjVNode FirstEdge;
DatatType Data;
}AdjList[MaxVertexNum];
typedef struct GNode* PtrToGNode;
struct GNode{
int Nv;
int Ne;
AdjList G;
};
typedef PtrToGNode LGraph;
LGraph CreateGraph(int VertexNum)
{
LGraph Graph;
Graph = new GNode;
Graph->Ne = 0;
Graph->Nv = VertexNum;
for(int i=0;i<Graph->Nv;++i){
Graph->G[i].FirstEdge = nullptr;
}
return Graph;
}
void InsertEdge(LGraph Graph, ENode Edge)
{
PtrToAdjVNode NewNode = new AdjVNode;
NewNode->AdjV = Edge.V2;
NewNode->Weight = Edge.Weight;
NewNode->Next = Graph->G [Edge.V1 ].FirstEdge ;
Graph->G[Edge.V1].FirstEdge = NewNode;
PtrToAdjVNode NewNode1 = new AdjVNode;
NewNode1->AdjV = Edge.V1;
NewNode1->Weight = Edge.Weight;
NewNode1->Next = Graph->G[Edge.V2].FirstEdge;
Graph->G[Edge.V2].FirstEdge = NewNode1;
}
LGraph Build()
{
LGraph Graph;
Vertex V;
ENode E;
inFile >> V;
Graph = CreateGraph(V);
for(int i=0;i<V;++i){
inFile >> Graph->G[i].Data;
}
inFile >> Graph->Ne ;
if(Graph->Ne ){
for(int i=0;i<Graph->Ne ;++i){
inFile >> E.V1 >> E.V2 >> E.Weight;
InsertEdge(Graph, E);
}
}
return Graph;
}
void Print(LGraph Graph)
{
if(Graph->Nv !=0){
for(int i=0;i<Graph->Nv ;++i){
PtrToAdjVNode It = Graph->G[i].FirstEdge;
cout << Graph->G[i].Data << " ";
while(It){
cout << Graph->G [It->AdjV].Data << " ";
It = It->Next;
}
cout << endl;
}
}
}
void Visit(Vertex V)
{
printf("正在访问顶点%d\n", V);
}
/* Visited[]为全局变量,已经初始化为false */
void DFS(LGraph Graph, bool* Visited, Vertex V)
{ /* 以V为出发点对邻接表存储的图Graph进行DFS搜索 */
PtrToAdjVNode W;
Visit(V); /* 访问第V个顶点 */
Visited[V] = true; /* 标记V已访问 */
for (W = Graph->G[V].FirstEdge; W; W = W->Next) /* 对V的每个邻接点W->AdjV */
if (!Visited[W->AdjV]) /* 若W->AdjV未被访问 */
DFS(Graph, Visited, W->AdjV); /* 则递归访问之 */
}
void BFS(LGraph Graph,bool* Visited,int index)
{
queue<PtrToAdjVNode> Q;
Visited[index] = true;
Visit(index);
PtrToAdjVNode P = new AdjVNode;
P->AdjV = index;
P->Next = nullptr;
Q.push(P);
while(Q.size()){
int a = Q.front()->AdjV;
PtrToAdjVNode it = Graph->G [Q.front()->AdjV ].FirstEdge ;
Q.pop();
while(it){
if(Visited[it->AdjV ]==false){
Q.push(it);
Visited[it->AdjV] = true;
Visit(it->AdjV);
}
it = it->Next;
}
}
}
void PrintBFS(LGraph Graph, bool* Visited)
{
cout << "广度优先搜索" << endl;
for (int i = 0; i<Graph->Nv; ++i) {
if (Visited[i] == false) {
BFS(Graph, Visited, i);
}
}
}
void PrintDFS(LGraph Graph, bool* Visited)
{
cout << "深度优先搜索" << endl;
for (int i = 0; i<Graph->Nv; ++i) {
if (Visited[i] == false) {
DFS(Graph, Visited, i);
}
}
}
int main()
{
LGraph Graph = Build();
Print(Graph);
bool Visited[MaxVertexNum], Visited1[MaxVertexNum];
for(int i=0;i<Graph->Nv ;++i){
Visited[i] = false;
Visited1[i]= false;
}
PrintBFS(Graph, Visited);
cout << endl << endl;
cout << endl << endl;
PrintDFS(Graph, Visited1);
delete Graph;
inFile.close();
system("pause");
return 0;
}
图的建立和BFS、DFS
最新推荐文章于 2022-05-28 12:58:08 发布
