|
算法过程: 1.将根节点放入队列中 2.从队列中取出第一个元素,将队列中的第一个元素弹出 3.将所取得元素的全部节点加入队列中 4.判断队列是否为空 a. 若是,则结束 b.若不是,则跳到第二步 - #include <iostream>+ i( z& `0 K# O' \0 u k
- using namespace std;
x) F7 i' v6 {) U4 f7 x1 x4 Z' Y% M
- r7 d- @- H) M1 q* o- const int verMax=9;5 C! T5 [( v( K% A, A) p& _( k
- const int queMax=10; // >=9皆可
1 s7 F/ @/ v$ e( } - v, L2 H* H, E1 |4 m) z! c
- struct node//声明图形顶点结构& A) k# U) u* H# r8 b) l7 t
- {
' t3 B/ l" h: i: u: q - int vertex;& ^7 l1 }3 u; {$ }, g3 H
- struct node *next;
5 M3 ?% c# i- [. o' e - };
" E z# o, ^/ j, _
2 h) K6 k& ~: Z3 t- typedef struct node *graph;+ K( e# b- P4 F6 ? c# u
- struct node head[verMax];//声明顶点结构数组
V! A/ d' n3 t - int que[queMax];2 T# W% e) L& t9 F
- int front=-1;% t. b/ v" _ f |
- int rear=-1;
5 D$ _6 C( h9 V( A/ Z( @ - int visit[verMax];//查找记录//队列的存入& q1 _& E3 Y" v, L3 s* G/ j
4 u# `$ w) L2 T9 ]7 Q; X3 K- int enque(int v), S8 f! @, s7 C- a0 X- u# z
- {
3 M5 l) j" ^- x5 @: p0 \) R - if(rear>=queMax)//队列已满+ X2 a# Y; }$ n6 i& D7 ^% e: @2 V
- return -1;
' Z6 b; k0 r: l7 f: V% U w2 C" B - else
1 j+ Q. c1 C0 t. i1 {2 x - {
- q: n3 k6 ^/ `' ~+ [6 Y - rear++;//对了尾端指针后移
7 x) ^# V R. d( J0 @ - que[rear]=v;//将值存入队列中
, Q Y# ~5 Z2 C% V! d" a( l - return 1;
9 N8 M9 K- m5 \ - }
# c$ w* Z% l; j5 R" Q D - }
9 R. ^; Q: D+ S# }' ?/ F - //队列的取出- W) C6 Q' _9 v+ s# X
- int deque()
5 d' o/ f; v0 }3 p. t5 { - {( G( E* f) T ]$ W3 i8 g( E
- if(front==rear)//队列已空' q3 S) b, G6 p2 k* D u) s
- return -1;' F; T" _5 F, _$ k' ]' T
- else
7 L0 r; [" O5 X1 u$ n: k - {& ]. ^* L% t% b3 n, B2 z) m
- front++;
: @) {0 K( b0 D4 k - return que[front];
/ R; J/ d8 d2 L* i - }
+ [2 d/ \' X! @ - }
1 o4 N h5 p( |- u - //广度优先搜索法2 V1 Z" K6 R2 m, A6 U0 W+ w
- void BFS(int v)7 g f: L, o5 ?- F/ R
- {) A& t6 k$ J5 x' o+ Z _
- graph point;
7 f i/ L8 l' p6 i! ?- F$ A5 p - enque(v);//存入队列
9 S+ [: w+ S& \* C) ~' F - visit[v]=1;//已查找( j% Z0 t9 r, ^! @6 N
- cout<<"["<<v<<"]==>";
; l' P7 e$ d+ s5 d4 f5 H* r - while(front!=rear)
5 }4 j) x0 G$ }' z F - {& ~) f% L8 D- R$ Z2 p8 O
- v=deque();
/ ?$ J% f$ _/ T - point=head[v].next;
" B5 m& F5 h$ S; }/ n6 G - while(point!=NULL)//读入邻接列表所有顶点/ d( m) y9 d1 o5 f3 i8 D
- {# M% b/ |% f! M# {
- if(visit[point->vertex]==0)
1 ^! |- U4 b. X% o2 [2 m; Q8 c - {
& r- V. ~- e, g - enque(point->vertex);//存入队列
% F% ^" D8 S! V4 Y, v- e - visit[point->vertex]=1;//已查找过的顶点4 @0 W- K' |0 x7 a7 m+ L/ w
- cout<<"["<<point->vertex<<"]==>";
/ Q" m( f9 H* [ - }8 n# v+ U$ L9 b6 Z
- point=point->next;
/ U5 A$ v) v5 F; v1 {4 y) j5 I - }
! t' { d4 T# ~' u9 F - }& [1 ~4 ^# x8 X+ _. u/ k! t+ W
- }
* P, s" {$ V+ ?" H - //建立邻接顶点至邻接列表内# B$ W7 u' _3 q6 b
- void create_graph(int v1,int v2)
& x; [+ D5 d" t5 A! P4 K - {- r( [5 }4 P \$ D! x4 o& Y3 m
- graph point,New;
+ V% d. ]* U/ A* c7 Q - New=(graph)malloc(sizeof(struct node));" A& A# r5 ~2 f4 f, r
- if(New!=NULL)
3 V. K5 T* f8 G# t9 s+ K0 o - {
2 R) ~8 j0 q D& L - New->vertex=v2;//邻近顶点
) Q; Y/ K- z8 J; [/ _( J% g+ r - New->next=NULL;
, T% H! H3 x1 ?: P# p - point=&(head[v1]);' e/ r3 [3 }* ]
- while(point->next!=NULL)% v; U% A) c1 g8 K% u& }
- point=point->next;4 S$ g$ W1 |5 B
- point->next=New;//串连在列表尾端
- F5 Q3 ^& s7 F1 m/ V [ - }2 I Z: o; F) A; S. r- H
- }
4 \; t: b# a) W q - //输出邻接列表内数据
0 f; b2 R, H, Q u+ h B0 y7 ^! d+ n9 I6 O - void print_graph(struct node *head)
# q' K- K ]1 i/ j3 t9 k3 ? - {
0 C" Z" c" c/ [" j2 i, T( a S& M - graph point; E1 _! a% m% v6 P. _
- point=head->next;//设置point为首节点
N! D$ l7 f" c% R! c" ` - while(point!=NULL)
' T5 C' f+ V4 ?' L- v7 j f2 o0 i- i7 i - {) g" k9 B/ N6 R9 m& v7 `. ~" l
- cout<<"["<<point->vertex<<"] ";6 l S0 l5 `4 k+ C# r& j
- point=point->next;. X: G" X' @* Z; b
- }2 V% N( B: z' p5 N8 ^
- cout<<endl;
- b! V% S/ C( B - }0 _7 D: W; T0 h% N, q! W" [
- //主程序
+ ^2 o' r7 v* n: Y+ Q - void main()' ?) K, W- M3 z$ W8 j; Z/ F
- {
# q1 @5 M' {8 P8 y5 S) @" ? - int node[20][2]={+ [4 r3 D+ z7 }9 m, s5 K" q
- {1,2},{2,1},{1,3},{3,1},{2,4},{4,2},{2,5},{5,2},{3,6},{6,3},- }- e; f3 G* P6 F, u/ E; z1 ]
- {3,7},{7,3},{4,8},{8,4},{5,8},{8,5},{6,8},{8,6},{7,8},{8,7}};- {: z" e& J/ w' S. q: y
- int i;
6 _6 `. C- v' \" g# H3 [* r - for(i=0;i<verMax;i++)4 `7 k1 D1 d* G! R: c' D" P6 `
- {+ F& g( T. v$ P% l& @
- head[i].vertex=i;: y1 ?8 Y. D: h& v* C
- head[i].next=NULL;
7 \. Z' z0 Z( ]9 h: x7 W% Z - }
/ F! e$ ]2 |4 s. h# \ - for(i=0;i<verMax;i++)/ }! f2 v8 b) g5 A( b3 ~
- visit[i]=0;
1 {8 B4 [& W: H. C
$ Y f5 t% W$ @: ]" K- for(i=0;i<20;i++)4 u5 a, ?, X/ \8 O5 c
- create_graph(node[i][0],node[i][1]);# Z. ~6 F8 @5 m- p$ w
- 1 v: X7 v6 E3 }, S% D& U r
- cout<<"======Graph======"<<endl;, B4 \0 ^2 b) P R% N' F7 c
- for(i=1;i<verMax;i++)
! }3 z! r, e; G [) |/ s& e - {$ _. b6 }7 ^ o/ X" H1 a
- cout<<"Vertex["<<i<<"]: ";
: l n4 G$ {4 A - print_graph(&head[i]);0 P' \, u! C( y% u
- }
/ D; U( n6 R. v3 ~7 f- o E. B - //广度优先搜索( Z- _, X! P- Z3 W# z" O" D& M
- cout<<"Bradth-First-Search:"<<endl<<"&BEGIN==>";
3 c9 h n, o+ @' E - BFS(4);) L7 L$ b8 a I
- cout<<"END&"<<endl;
; M" Q7 B9 X7 g9 { - }
复制代码
6 [) b! j5 G+ f* j- F7 G% n2 b) c) I
) \3 H5 w% B0 s# |: S) C% t8 {/ |& t$ X' L; y {+ Y" R7 N
|
|