資料結構 鄰接矩陣寬度優先搜尋

2021-09-17 20:44:24 字數 1917 閱讀 3139

本實驗實現無向圖的鄰接矩陣儲存,並輸出鄰接矩陣, 以及該無向圖的廣度優先遍歷序列。

建立如圖所示的無向圖g的鄰接矩陣,並輸出該矩陣。

輸出廣度優先遍歷序列

code

#include

#include

#include

#define max_size 20

#define ok 1

#define no 0

#define inf 0x3f3f3f3f

typedef

int status;

typedef

int elemtype;

typedef

char infotype;

typedef

struct

arccell,imagetable[max_size]

[max_size]

;typedef

struct

mgraph;

//以下鏈佇列

typedef

struct qnode

queuenode,

*queuepoint;

typedef

struct

queuehead,

*queueheadpoint;

queueheadpoint initqueue()

status queueempty

(queueheadpoint head)

elemtype getfront

(queueheadpoint *head)

status queuepush

(queueheadpoint *head,elemtype data)

p->data=data;

p->next=

(*head)

->rear->next;

(*head)

->rear->next=p;

(*head)

->rear=p;

(*head)

->queuesize++

;return ok;

}status queuepop

(queueheadpoint *head)

status queuedelete

(queueheadpoint *head)

//以上為鏈佇列

status creatudg

(mgraph *node)

while

(scanf

("%d%d"

,&v1,

&v2)

!=eof

&& v1>=

0&& v2>=0)

else

else

if(node->vexs[v1]

.vertex==0)

else

if(node->vexs[v2]

.vertex==0)

else

continue

; node->arcnum++;}

}return ok;

}status bfs

(mgraph *node,elemtype start)}}

printf

("\n");

queuedelete

(&head)

;return ok;

}int

main()

/**input:

0 10 2

0 30 5

1 22 5

3 54 5

-1 -1

*/

廣度優先搜尋 鄰接矩陣

需要了解的是,圖的廣度搜尋遍歷類似於二叉樹的層次遍歷,用到了隊的操作 如下 include include define ok 1 define error 0 define true 1 define false 0 define maxvex 100typedef int datatype 設定...

深度優先遍歷(鄰接矩陣)

problem description 無向連通圖的頂點值為字元型且互不相等,採用鄰接矩陣儲存。請從儲存下標為0的頂點開始深度優先遍歷,在選取下乙個未被訪問的鄰接點時,優先選擇儲存下標小的頂點,輸出該遍歷序列。input 有多組資料,每組第一行為兩個整數n和e,表示n個頂點和e條邊 0using n...

廣度優先搜尋鄰接矩陣表示 (C語言)

在圖論演算法中圖的表示主要有有鄰接矩陣和鄰接表兩種表示法,在這篇文章之前,已經發布了dijkstra演算法的鄰接表表示法 dijkstra演算法的鄰接表表示 兩種表示法的演算法思路是一樣的,只是表示的方式不同而帶來了 表示上的細微區別。個人認為鄰接矩陣的表示法更為簡潔,有鄰接表有些繁瑣。由於思路相似...