本文是记录数据结构习题解析与实验指导的课后实验八——基于广度优先搜索的六度空间理论的验证。
1 实验内容
问题描述
“六度空间”理论又称作“六度分隔(Six Degrees of Separation)”理论。这个理论可以通俗地阐述为:“你和任何一个陌生人之间所间隔的人不会超过六个,也就是说,最多通过五个人你就能够认识任何一个陌生人。”如下图所示。
“六度空间”理论虽然得到广泛的认同,并且正在得到越来越多的应用。但是数十年来,试图验证这个理论始终是许多社会学家努力追求的目标。然而由于历史的原因,这样的研究具有太大的局限性和困难。随着当代人的联络主要依赖于电话、短信、微信以及因特网上即时通信等工具,能够体现社交网络关系的一手数据已经逐渐使得“六度空间”理论的验证成为可能。
假如给你一个社交网络图,请你对每个节点计算符合“六度空间”理论的结点占结点总数的百分比。
输入格式:
输入第1行给出两个正整数,分别表示社交网络图的结点数N(1<N≤104,表示人数)、边数M(≤33×N,表示社交关系数)。随后的M行对应M条边,每行给出一对正整数,分别是该条边直接连通的两个结点的编号(节点从1到N编号)。当N和M都为0时,表示结束。
输出格式:
对每个结点输出与该结点距离不超过6的结点数占结点总数的百分比,精确到小数点后2位。每个结节点输出一行,格式为“结点编号:(空格)百分比%”。
输入样例:
10 9
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
输出样例:
1: 70.00%
2: 80.00%
3: 90.00%
4: 100.00%
5: 100.00%
6: 100.00%
7: 100.00%
8: 90.00%
9: 80.00%
10: 70.00%
2 基本思路
这里采用邻接表进行数据存储,由于题目输入的是无向边,所以我们需要对其进行处理。根据题目的输入,建立好树之后,就是利用BFS进行遍历,来寻找范围为6的节点的个数,由于BFS是一个节点一个节点的出队,所以我们要设定一个标识,来表示一层的末尾,当到达末尾时,层数加一,达到六层,就可以退出了,返回节点数(每入一次队,节点数加1)。
3 核心代码
1 数据结构代码:
#include<bits/stdc++.h>
#define MAXVEX 100
using namespace std;
typedef struct EdgeNode
{
int adjvex;
int weight;
EdgeNode *next;
}EdgeNode;
typedef struct
{
int data;
EdgeNode *firstEdge;
}vertexNode, AdjList[MAXVEX];
typedef struct
{
AdjList adjList;
int numVertexs, numEdges;
}GraphAdjList;
2 建树代码:
void CreateAlGraph(GraphAdjList &g)
{
cin>>g.numVertexs>>g.numEdges;
if (g.numVertexs == 0 && g.numEdges == 0)
{
return ;
}
int iStart, iEnd;
for (int i = 0; i < g.numVertexs; ++i)
{
g.adjList[i].data = i + 1;
g.adjList[i].firstEdge = NULL;
}
for (int j = 0; j < g.numEdges; ++j)
{
cin>>iStart>>iEnd;
EdgeNode *n = new EdgeNode;
n->adjvex = iEnd;
n->weight = 1;
n->next = g.adjList[iStart - 1].firstEdge;
g.adjList[iStart - 1].firstEdge = n;
EdgeNode *n2 = new EdgeNode;
n2->adjvex = iStart;
n2->weight = 1;
n2->next = g.adjList[iEnd - 1].firstEdge;
g.adjList[iEnd - 1].firstEdge = n2;
}
}
因为输入的是无向边,所以需要做两次处理。
3 BFS遍历代码:
int BFSTraverse(GraphAdjList &g, int start)
{
queue<int> q;
int cnt = 0;
int level = 0;
int last = start;
int tail = 0;
int visited[g.numVertexs];
for (int i = 0; i < g.numVertexs; ++i)
{
visited[i] = 0;
}
visited[start] = 1;
q.push(start);
cnt++;
EdgeNode *te;
while (!q.empty())
{
int temp = q.front();
q.pop();
te = g.adjList[temp].firstEdge;
while(te != NULL)
{
if (!visited[te->adjvex-1])
{
q.push(te->adjvex - 1);
visited[te->adjvex - 1] = 1;
cnt++;
tail = te->adjvex - 1;
}
te = te->next;
}
if (temp == last)
{
last = tail;
level++;
}
if (level == 6)
{
break;
}
}
return cnt;
}
level用来表示层次,到6的时候break.last表示每一层的最末尾节点。而tail用来寻找下一层最末尾节点。当temp==last时,也就是到达这一层的末尾时,tail也刚好到达下一层的末尾,于是将tail的值赋给last,并且层数加一。
4 全部代码
#include<bits/stdc++.h>
#define MAXVEX 100
using namespace std;
typedef struct EdgeNode
{
int adjvex;
int weight;
EdgeNode *next;
}EdgeNode;
typedef struct
{
int data;
EdgeNode *firstEdge;
}vertexNode, AdjList[MAXVEX];
typedef struct
{
AdjList adjList;
int numVertexs, numEdges;
}GraphAdjList;
void CreateAlGraph(GraphAdjList &g)
{
cin>>g.numVertexs>>g.numEdges;
if (g.numVertexs == 0 && g.numEdges == 0)
{
return ;
}
int iStart, iEnd;
for (int i = 0; i < g.numVertexs; ++i)
{
g.adjList[i].data = i + 1;
g.adjList[i].firstEdge = NULL;
}
for (int j = 0; j < g.numEdges; ++j)
{
cin>>iStart>>iEnd;
EdgeNode *n = new EdgeNode;
n->adjvex = iEnd;
n->weight = 1;
n->next = g.adjList[iStart - 1].firstEdge;
g.adjList[iStart - 1].firstEdge = n;
EdgeNode *n2 = new EdgeNode;
n2->adjvex = iStart;
n2->weight = 1;
n2->next = g.adjList[iEnd - 1].firstEdge;
g.adjList[iEnd - 1].firstEdge = n2;
}
}
int BFSTraverse(GraphAdjList &g, int start)
{
queue<int> q;
int cnt = 0;
int level = 0;
int last = start;
int tail = 0;
int visited[g.numVertexs];
for (int i = 0; i < g.numVertexs; ++i)
{
visited[i] = 0;
}
visited[start] = 1;
q.push(start);
cnt++;
EdgeNode *te;
while (!q.empty())
{
int temp = q.front();
q.pop();
te = g.adjList[temp].firstEdge;
while(te != NULL)
{
if (!visited[te->adjvex-1])
{
q.push(te->adjvex - 1);
visited[te->adjvex - 1] = 1;
cnt++;
tail = te->adjvex - 1;
}
te = te->next;
}
if (temp == last)
{
last = tail;
level++;
}
if (level == 6)
{
break;
}
}
return cnt;
}
void show(GraphAdjList &g)
{
EdgeNode *temp;
for (int i = 0; i < g.numVertexs; ++i)
{
temp = g.adjList[i].firstEdge;
printf("%d",g.adjList[i].data);
while(temp != NULL)
{
printf("-->%d",temp->adjvex);
temp = temp->next;
}
printf("\n");
}
}
int main()
{
freopen("7.txt","r",stdin);
GraphAdjList g;
double result = 0;
CreateAlGraph(g);
for (int i = 0; i < g.numVertexs; ++i)
{
result = BFSTraverse(g,i);
printf("%d: %.2f%%\n",i+1,result/g.numVertexs*100);
}
//show(g);
return 0;
}
其中的7.txt即为题目的输入示例。
到这里这篇文章就结束了。如果有错误,可以在下方评论,或者私聊我😉,我会及时改正的。
如果看了有收获,可以点赞加关注😉,看计算机小白的成长之路。