日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

图算法之BFS

發(fā)布時(shí)間:2025/5/22 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 图算法之BFS 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

深度優(yōu)先搜索(Breadth? First Search),類似于樹的層序遍歷,搜索模型是隊(duì)列,還是以下面的無向圖為例:

實(shí)驗(yàn)環(huán)境是Ubuntu 14.04 x86

?

偽代碼實(shí)現(xiàn)如下:

其中u 為 v 的先輩或父母。

BFS(G, s)

for each vertex u ∈ V [G] - {s}do color[u] ← WHITEd[u] ← ∞π[u] ← NIL //除了源頂點(diǎn)s之外,第1-4行置每個(gè)頂點(diǎn)為白色,置每個(gè)頂點(diǎn)u的d[u]為無窮大,置每個(gè)頂點(diǎn)的父母為NIL。 color[s] ← GRAY // 將源頂點(diǎn)s置為灰色,這是因?yàn)樵谶^程開始時(shí),源頂點(diǎn)已被發(fā)現(xiàn)。 d[s] ← 0 π[s] ← NIL //將源頂點(diǎn)的父頂點(diǎn)置為NIL。 Q ← ? ENQUEUE(Q, s) //第8、9行,初始化隊(duì)列Q,使其僅含源頂點(diǎn)s。 while Q ≠ ?do u ← DEQUEUE(Q) //確定隊(duì)列頭部Q頭部的灰色頂點(diǎn)u,并將其從Q中去掉。for each v ∈ Adj[u] //for循環(huán)考察u的鄰接表中的每個(gè)頂點(diǎn)vdo if color[v] = WHITEthen color[v] ← GRAY d[v] ← d[u] + 1 π[v] ← u //u記為該頂點(diǎn)的父母ENQUEUE(Q, v) //插入隊(duì)列中color[u] ← BLACK

?

隊(duì)列推薦使用鏈?zhǔn)疥?duì)列。

鄰接表實(shí)現(xiàn):

#include <stdio.h> #include <malloc.h>#define MAX_VERTEX_NUM 50 typedef char vertexType; typedef int edgeType; typedef int QueueElemType;/* 邊表 */ typedef struct ArcNode {int adjIndex;struct ArcNode *nextArc; edgeType weight; }ArcNode;/* 頂點(diǎn)表 */ typedef struct VNode {vertexType data;ArcNode *firstArc; }VNode, AdjList[MAX_VERTEX_NUM];/* 圖結(jié)構(gòu) */ typedef struct {AdjList adjList;int vexNum;int edgeNum; }ALGraph;typedef struct QueueNode {QueueElemType data;struct QueueNode *next; }QueueNode;typedef struct {QueueNode *front;QueueNode *rear; }LinkQueue;int visit[MAX_VERTEX_NUM] = {0};void initLinkQueue(LinkQueue *queue) {QueueNode *head = (QueueNode*)malloc(sizeof(QueueNode));if(head == NULL){printf("malloc failed\n");return;}head->next = NULL;queue->front = queue->rear = head;return; }void insertLinkQueue(LinkQueue *queue, QueueElemType data) {QueueNode *newNode = (QueueNode *)malloc(sizeof(QueueNode));if(NULL == newNode){printf("malloc failed\n");return;}newNode->data = data;newNode->next = NULL;queue->rear->next = newNode;queue->rear = newNode;return; }int deleteLinkQueue(LinkQueue *queue, QueueElemType *data) {QueueNode *p;if (queue->front == queue->rear){printf("no element!\n");return 0;}p = queue->front->next;*data = p->data;queue->front->next = p->next;if (p == queue->rear){queue->rear = queue->front;}free(p);return 1; }void BFSTraverse(ALGraph G) {QueueElemType q;LinkQueue queue;ArcNode *adjEdge;int index = 0;initLinkQueue(&queue);for (int i = 0; i < G.vexNum; i++){if (!visit[i]){visit[i] = 1;printf("%c ", G.adjList[i].data); insertLinkQueue(&queue, i);while (deleteLinkQueue(&queue, &q)){adjEdge = G.adjList[q].firstArc;while (adjEdge){index = adjEdge->adjIndex;if (!visit[index]){visit[index] = 1;printf("%c ", G.adjList[index].data);insertLinkQueue(&queue, index);}adjEdge = adjEdge->nextArc;}}}}} void CreateALGraph(ALGraph *G) {int i, j, k;ArcNode *e;int c;printf("輸入頂點(diǎn)數(shù)和邊數(shù):\n");scanf("%d %d", &G->vexNum, &G->edgeNum);setbuf(stdin, NULL);for (i = 0; i < G->vexNum; i++){printf("輸入頂點(diǎn)信息:\n");scanf("%c", &G->adjList[i].data);G->adjList[i].firstArc = NULL;while((c = getchar()) != '\n' && c != EOF);}for (k = 0; k < G->edgeNum; k++){printf("輸入邊(vi,vj)的頂點(diǎn)序號(hào)i,j:\n");scanf("%d %d", &i, &j);while((c = getchar()) != '\n' && c != EOF);e = (ArcNode*)malloc(sizeof(ArcNode));if (NULL == e){return;}e->adjIndex = j;e->nextArc = G->adjList[i].firstArc;G->adjList[i].firstArc = e;// double direction copye = (ArcNode *)malloc(sizeof(ArcNode));if (NULL == e){return;}e->adjIndex = i;e->nextArc = G->adjList[j].firstArc;G->adjList[j].firstArc = e;} }int main(int argc, char const *argv[]) {ALGraph G; CreateALGraph(&G);BFSTraverse(G);return 0; }

運(yùn)行結(jié)果: A F B G E I C H D

?

鄰接矩陣實(shí)現(xiàn):

#include <stdio.h> #include <malloc.h>#define MAX_VERTEX_NUM 50 typedef char vertexType; typedef int edgeType; typedef int QueueElemType;typedef struct {vertexType vexs[MAX_VERTEX_NUM];edgeType arc[MAX_VERTEX_NUM][MAX_VERTEX_NUM];int vexNum;int edgeNum; }Graph;typedef struct QueueNode {QueueElemType data;struct QueueNode *next; }QueueNode;typedef struct {QueueNode *front;QueueNode *rear; }LinkQueue;int visit[MAX_VERTEX_NUM] = {0};void initLinkQueue(LinkQueue *queue) {QueueNode *head = (QueueNode*)malloc(sizeof(QueueNode));if(head == NULL){printf("malloc failed\n");return;}head->next = NULL;queue->front = queue->rear = head;return; }void insertLinkQueue(LinkQueue *queue, QueueElemType data) {QueueNode *newNode = (QueueNode *)malloc(sizeof(QueueNode));if(NULL == newNode){printf("malloc failed\n");return;}newNode->data = data;newNode->next = NULL;queue->rear->next = newNode;queue->rear = newNode;return; }int deleteLinkQueue(LinkQueue *queue, QueueElemType *data) {QueueNode *p;if (queue->front == queue->rear){printf("no element!\n");return 0;}p = queue->front->next;*data = p->data;queue->front->next = p->next;if (p == queue->rear){queue->rear = queue->front;}free(p);return 1; } void CreateALGraph(Graph *G) {int i, j, k;int c;printf("輸入頂點(diǎn)數(shù)和邊數(shù):\n");scanf("%d %d", &G->vexNum, &G->edgeNum);setbuf(stdin, NULL);for (i = 0; i < G->vexNum; i++){printf("輸入第%d個(gè)頂點(diǎn)信息:\n", i + 1);scanf("%c", &G->vexs[i]);while((c = getchar()) != '\n' && c != EOF);}for (i = 0; i < G->vexNum; i++){for (j = 0; j < G->vexNum; j++){G->arc[i][j] = 0;}}for (k = 0; k < G->edgeNum; k++){printf("輸入邊(vi,vj)的下標(biāo)i,j:\n");scanf("%d %d", &i, &j);while((c = getchar()) != '\n' && c != EOF);// set a default weightG->arc[i][j] = G->arc[j][i] = 1; } }void BFSTraverse(Graph G) {LinkQueue queue;QueueElemType q;initLinkQueue(&queue);for(int i = 0; i < G.vexNum; i++){if (!visit[i]){visit[i] = 1;printf("%c ", G.vexs[i]);insertLinkQueue(&queue, i);while (deleteLinkQueue(&queue, &q)){for (int j = 0; j < G.vexNum; j++){if (G.arc[q][j] == 1 && !visit[j]){visit[j] = 1;printf("%c ", G.vexs[j]);insertLinkQueue(&queue, j);}}}}} }int main(int argc, char const *argv[]) {Graph G; CreateALGraph(&G);BFSTraverse(G);return 0; }

運(yùn)行結(jié)果:A B F C G I E D H

轉(zhuǎn)載于:https://www.cnblogs.com/freedreamnight/p/3906047.html

總結(jié)

以上是生活随笔為你收集整理的图算法之BFS的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。