数据结构实验之图论二:基于邻接表的广度优先搜索遍历

 

数据结构实验之图论二:基于邻接表的广度优先搜索遍历

Time Limit: 1000MS Memory limit: 65536K

题目描述

给定一个无向连通图,顶点编号从0到n-1,用广度优先搜索(BFS)遍历,输出从某个顶点出发的遍历序列。(同一个结点的同层邻接点,节点编号小的优先遍历)

输入

输入第一行为整数n(0< n <100),表示数据的组数。
对于每组数据,第一行是三个整数k,m,t(0<k<100,0<m<(k-1)*k/2,0< t<k),表示有m条边,k个顶点,t为遍历的起始顶点。 
下面的m行,每行是空格隔开的两个整数u,v,表示一条连接u,v顶点的无向边。

输出

输出有n行,对应n组输出,每行为用空格隔开的k个整数,对应一组数据,表示BFS的遍历结果。

示例输入

1
6 7 0
0 3
0 4
1 4
1 5
2 3
2 4
3 5

示例输出

0 3 4 2 5 1

提示

用邻接表存储。

标准代码:

代码1:

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstdlib>
 4 #include<cstring>
 5 #include<queue>
 6 using namespace std;
 7 struct vode
 8 {
 9     int v;
10     struct vode *next;
11 };
12 struct vode *f[10000];
13 int m,n,k;
14 int visited[1000001];
15 void paixu(struct vode *head)
16 {
17     struct vode *q,*p;
18     int k=0;
19     while(k=!k)
20     {
21         p=head;
22         q=p->next;
23         while(q)
24         {
25             if(p->v>q->v)
26             {
27                 int t;
28                 t=p->v;
29                 p->v=q->v;
30                 q->v=t;
31                 k=0;
32             }
33             else
34             {
35                 p=p->next;
36                 q=p->next;
37             }
38         }
39     }
40 }
41 void fbs()
42 {
43     struct vode *p;
44     visited[k]=1;
45     queue<int>que;
46     que.push(k);
47     cout<<k;
48     while(!que.empty())
49     {
50         k=que.front();
51         que.pop();
52         p=f[k];
53         paixu(p);
54         while(p)
55         {
56             if(visited[p->v]==0)
57             {
58                 cout<<" "<<p->v;
59                 que.push(p->v);
60                 visited[p->v]=1;
61             }
62             p=p->next;
63         }
64     }
65     cout<<endl;
66 }
67 int main()
68 {
69     int zong;
70     cin>>zong;
71     while(zong--)
72     {
73         memset(visited,0,sizeof(visited));
74         cin>>m>>n>>k;
75         int i;
76         for(i=1;i<=n;i++)
77         {
78             int u,v;
79             cin>>u>>v;
80             struct vode *p;
81             p=(struct vode *)malloc(sizeof(struct vode));
82             p->v=v;
83             p->next=f[u];
84             f[u]=p;
85             p=(struct vode *)malloc(sizeof(struct vode));
86             p->v=u;
87             p->next=f[v];
88             f[v]=p;
89         }
90         fbs();
91     }
92     return 0;
93 }
View Code

代码2:与之前的相比,这个代码中排序方法有所改变。

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<stdlib.h>
 4 #include<iostream>
 5 #include<queue>
 6 
 7 using namespace std;
 8 int vis[101];
 9 int n,m,k;
10 queue<int>q;
11 
12 struct node
13 {
14     int u,v;
15     struct node *next;
16 }*head[110];
17 
18 void add(int u, int v)
19 {
20    struct node *p = (struct node*)malloc(sizeof(struct node));
21    p->u = u;
22    p->v = v;
23    p->next = head[u];
24    head[u] = p;
25 };
26 
27 int cmp(const void *a,const void *b)
28 {
29     return *(int *)a-*(int *)b;
30 }
31 
32 void bfs(int t)
33 {
34    int i,x,a[110],j,b[110],y;
35    q.push(t);//元素t入队列
36    vis[t]=1; j=0;
37    while(!q.empty())//如果队列非空
38    {
39        x=q.front();
40        a[++j]=x;
41        q.pop();//删除队首元素
42        y=0;
43            for(struct node *p=head[x]; p!=NULL; p=p->next)
44            {
45                if(vis[p->v]==0)
46                {
47                    b[y++]=p->v;
48                    vis[p->v]=1;
49                }
50            }
51         if(y>=1)
52         qsort(b,y,sizeof(b[0]),cmp);//快速排序,使得同一层次的节点按照从小到大排序
53         for(i=0; i<=y-1; i++)
54         q.push(b[i]);//让b数组中的元素依次由小到达入队列
55    }
56    for(i=1; i<=j-1; i++)
57    printf("%d ",a[i]);
58    printf("%d\n",a[i]);
59 };
60 
61 int main()
62 {
63     int t,i,u,v;
64     scanf("%d",&n);
65     while(n--)
66     {
67         memset(head,NULL,sizeof(head));
68         memset(vis,0,sizeof(vis));
69         cin>>k>>m>>t;
70         for(i=0; i<m; i++)
71         {
72             cin>>u>>v;
73             add(u,v);
74             add(v,u);
75         }
76         bfs(t);
77     }
78 }
View Code
邻接表的结构示意:
 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<stdlib.h>
 4 int m,n;
 5 int visited[300];
 6 struct vode
 7 {
 8     int v;
 9     struct vode *next;
10 };
11 int que[100];
12 struct vode *f[300];
13 void fbs(int k)
14 {
15     int i;
16     for(i=0;i<=m-1;i++)
17     {
18         printf("%d : ",i);
19         struct vode *p=f[i];
20         for(;p;p=p->next)
21         printf("%d ",p->v);
22         printf("\n");
23     }
24 }
25 int main()
26 {
27     int sum;
28     scanf("%d",&sum);
29     while(sum--)
30     {
31         memset(f,0,sizeof(f));
32         memset(visited,0,sizeof(visited));
33         memset(que,-1,sizeof(que));
34         int k;
35         scanf("%d%d%d",&m,&n,&k);
36         int i;
37         for(i=1;i<=n;i++)
38         {
39             int u,v;
40             scanf("%d%d",&u,&v);
41             struct vode *p;
42             p=(struct vode *)malloc(sizeof(struct vode));
43             p->v=v;
44             p->next=f[u];
45             f[u]=p;
46             p=(struct vode *)malloc(sizeof(struct vode));
47             p->v=u;
48             p->next=f[v];
49             f[v]=p;
50         }
51         fbs(k);
52     }
53     return 0;
54 }
View Code

 以上是一种算法,也可以用类似邻接矩阵的bfs算法遍历邻接表:

代码3:

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<stdlib.h>
 4 struct vode
 5 {
 6     int v;
 7     struct vode *next;
 8 };
 9 int m,n;
10 int visited[101]={0};
11 struct vode *f[101];
12 int stack[101];
13 void paixu(struct vode *head)
14 {
15     int k=0;
16     struct vode *p,*q;
17     while(k=!k)
18     {
19         p=head;
20         q=p->next;
21         while(q!=NULL)
22         {
23             if(p->v>q->v)
24             {
25                 int t;
26                 t=p->v;
27                 p->v=q->v;
28                 q->v=t;
29                 k=0;
30             }
31             else
32             {
33                 p=p->next;
34                 q=p->next;
35             }
36         }
37     }
38 }
39 void bfs(int k)
40 {
41     int r=0,l=0;
42     int i;
43     stack[r++]=k;
44     visited[k]=1;
45     for(i=1;i<=m-1;i++)
46     {
47         k=stack[l++];
48         struct vode *p;
49         paixu(f[k]);
50         p=f[k];
51         for(;p!=NULL;p=p->next)
52         if(visited[p->v]==0)
53         {
54             visited[p->v]=1;
55             stack[r++]=p->v;
56         }
57     }
58     for(i=0;i<=m-1;i++)
59     if(i==0)printf("%d",stack[i]);
60     else printf(" %d",stack[i]);
61     printf("\n");
62 }
63 int main()
64 {
65     int q;
66     scanf("%d",&q);
67     while(q--)
68     {
69         int k;
70         scanf("%d%d%d",&m,&n,&k);
71         memset(visited,0,sizeof(visited));
72         int i;
73         for(i=1;i<=n;i++)
74         {
75             int u,v;
76             scanf("%d%d",&u,&v);
77             struct vode *p;
78             p=(struct vode *)malloc(sizeof(struct vode));
79             p->v=v;
80             p->next=f[u];
81             f[u]=p;
82             p=(struct vode *)malloc(sizeof(struct vode));
83             p->v=u;
84             p->next=f[v];
85             f[v]=p;
86         }
87         bfs(k);
88     }
89 }
View Code

 

posted @ 2013-08-06 19:19  狂盗一枝梅  阅读(371)  评论(0编辑  收藏  举报