图的遍历DFS与BFS(邻接表)
生活随笔
收集整理的這篇文章主要介紹了
图的遍历DFS与BFS(邻接表)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <queue>
#include <Windows.h>using namespace std;#define INFINITY INT_MAX
#define MAX_VERTEX_NUM 20 //頂點最多個數
#define LENGTH 5 //頂點字符長度//*********************************鄰接表***********************************begin
typedef char VertexType[LENGTH];
typedef struct ArcNode
{int adjvex;struct ArcNode* nextarc;int weight;
}ArcNode;typedef struct VNode
{VertexType data;ArcNode *firstarc;
}VNode, AdjList[MAX_VERTEX_NUM];typedef struct
{AdjList vertices;int vexnum;int arcnum;
}ALGraph;int LocateVex(const ALGraph & g, char name[LENGTH])
{for (int i = 0; i < g.vexnum; i++){if (0 == strcmp(g.vertices[i].data, name)){return i;}}return -1;
}//圖的建造
void CreateGraph(ALGraph &g)
{ifstream fcin(_T("graph.txt"));fcin>>g.vexnum;for (int i = 0; i < g.vexnum; i++){fcin>>g.vertices[i].data;g.vertices[i].firstarc = NULL;}fcin>>g.arcnum;char arcHead[LENGTH];char arcTail[LENGTH];int weight;ArcNode *p, *q;ArcNode *pTmp;for (int i = 0; i < g.arcnum; i++){memset(arcHead, 0, LENGTH);memset(arcTail, 0, LENGTH);fcin>>arcTail>>arcHead>>weight;int x = LocateVex(g, arcHead);int y = LocateVex(g, arcTail);p = new ArcNode;q = new ArcNode;p->adjvex = y;p->nextarc = NULL;p->weight = weight;q->adjvex = x;q->nextarc = NULL;q->weight = weight;if (NULL == g.vertices[x].firstarc){ g.vertices[x].firstarc = p;}else{for (pTmp = g.vertices[x].firstarc; NULL != pTmp->nextarc; pTmp = pTmp->nextarc){;}pTmp->nextarc = p;}if (NULL == g.vertices[y].firstarc){ g.vertices[y].firstarc = q;}else{for (pTmp = g.vertices[y].firstarc; NULL != pTmp->nextarc; pTmp = pTmp->nextarc){;}pTmp->nextarc = q;}/*p->adjvex = y;p->nextarc = g.vertices[x].firstarc;p->weight = weight;g.vertices[x].firstarc = p;q->adjvex = x;q->nextarc = g.vertices[y].firstarc;q->weight = weight;g.vertices[y].firstarc = q;*/}
}//v的第一個鄰接點
int FirstAdjVex(const ALGraph &g, int v)
{if ( NULL != g.vertices[v].firstarc){return g.vertices[v].firstarc->adjvex;}return -1;
}//v相對于w的下一個鄰接點
int NextAdjVex(const ALGraph &g, int v, int w)
{ArcNode *p;for (p = g.vertices[v].firstarc; NULL != p; p = p->nextarc){if (p->adjvex == w && p->nextarc != NULL){return p->nextarc->adjvex;}}return -1;
}//*********************************鄰接表***********************************end//深度優先遍歷
bool visit[MAX_VERTEX_NUM];void DFS (ALGraph& g, int v)
{visit[v] = true;cout<<g.vertices[v].data<<'\t';for (int w = FirstAdjVex(g, v); w >= 0; w = NextAdjVex(g, v, w)){if (!visit[w]){DFS(g, w);}}
}void DFSTraverse(ALGraph &g)
{for (int v = 0; v < g.vexnum; v++){visit[v] = false;}for (int v = 0; v < g.vexnum; v++){if (!visit[v]){DFS(g, v);}}cout<<endl;
}//廣度優先遍歷
void BFSTraverse(ALGraph &g, char vName[LENGTH])
{int pos = LocateVex(g, vName);for (int v = 0; v < g.vexnum; v++){visit[v] = false;}queue<int> q;if (!visit[pos]){cout<<g.vertices[pos].data<<'\t';visit[pos] = true;}q.push(pos);while (!q.empty()){int v = q.front();q.pop();for (int w = FirstAdjVex(g, v); w >= 0; w = NextAdjVex(g, v, w)){if (!visit[w]){cout<<g.vertices[w].data<<'\t';visit[w] = true;q.push(w);}}}cout<<endl;
}//輔助函數,設置控制臺的顏色
void SetConsoleTextColor(WORD dwColor)
{HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);if (INVALID_HANDLE_VALUE == handle){return;}SetConsoleTextAttribute(handle, dwColor);
}int _tmain(int argc, _TCHAR* argv[])
{ALGraph graph;CreateGraph(graph);SetConsoleTextColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY);cout<<"**************************DFS****************************"<<endl<<endl;DFSTraverse(graph);SetConsoleTextColor(FOREGROUND_GREEN | FOREGROUND_INTENSITY);cout<<"**************************BFS****************************"<<endl<<endl;BFSTraverse(graph, "V1");return 0;
}
界面運行如下:
建造圖的graph.txt文本內容如下:
8 V1 V2 V3 V4 V5 V6 V7 V8 10 V1 V2 10 V1 V3 50 V2 V4 30 V3 V5 40 V3 V6 99 V4 V5 2 V4 V7 60 V5 V7 80 V6 V8 22 V7 V8 70總結
以上是生活随笔為你收集整理的图的遍历DFS与BFS(邻接表)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: PHP各种魔术方法测试
- 下一篇: textView 加入链接