“六度空间”理论又称作“六度分隔(Six Degrees of Separation)”理论。这个理论可以通俗地阐述为:“你和任何一个陌生人之间所间隔的人不会超过六个,也就是说,最多通过五个人你就能够认识任何一个陌生人。”如图1所示。
“六度空间”理论虽然得到广泛的认同,并且正在得到越来越多的应用。但是数十年来,试图验证这个理论始终是许多社会学家努力追求的目标。然而由于历史的原因,这样的研究具有太大的局限性和困难。随着当代人的联络主要依赖于电话、短信、微信以及因特网上即时通信等工具,能够体现社交网络关系的一手数据已经逐渐使得“六度空间”理论的验证成为可能。
假如给你一个社交网络图,请你对每个节点计算符合“六度空间”理论的结点占结点总数的百分比。
输入格式:
输入第1行给出两个正整数,分别表示社交网络图的结点数N(1<N≤103,表示人数)、边数M(≤33×N,表示社交关系数)。随后的M行对应M条边,每行给出一对正整数,分别是该条边直接连通的两个结点的编号(节点从1到N编号)。
输出格式:
对每个结点输出与该结点距离不超过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%
//7-7 六度空间 (30分)
#include <stdio.h>
#include <stdlib.h>
#define MaxVertexNum 1001
int Visited[MaxVertexNum] = {0};
typedef int Vertex;
typedef struct ENode {
Vertex V1;
Vertex V2;
} Edge;
//邻接表定义
typedef struct AdjVNode *PtrToAdjVNode;
struct AdjVNode {
Vertex V;
PtrToAdjVNode Next;
};
typedef struct VNode {
PtrToAdjVNode FirstEdge;
} AdjList;
typedef struct GNode *PtrToGNode;
struct GNode {
int Ne;
int Nv;
AdjList G[MaxVertexNum];
};
typedef PtrToGNode LGraph;
typedef struct QNode {
int Front;
int Tail;
Vertex Data[MaxVertexNum + 1];
} Queue;
LGraph CreatLGraph( int VertexNum );
void InsertEdge( LGraph Graph, Edge* E );
Queue* CreatQueue();
void AddQ( Queue* Q, Vertex V);
int IsEmpty( Queue* Q );
Vertex DeleteQ( Queue* Q );
int BFS( LGraph Graph, Queue* Q, Vertex V );
void ListComponents( LGraph Graph );
void SetZero( int V[], int n );
int main()
{
int N, M;
int i;
scanf("%d", &N);
LGraph Graph = CreatLGraph(N);
scanf("%d", &(Graph->Ne));
if(Graph->Ne != 0) {
Edge* E = (Edge*)malloc(sizeof(Edge));
for(i=0; i<Graph->Ne; i++) {
scanf("%d %d", &(E->V1), &(E->V2));
InsertEdge( Graph, E );
}
}
ListComponents( Graph );
return 0;
}
LGraph CreatLGraph( int VertexNum )
{
Vertex i;
LGraph Graph = (LGraph)malloc(sizeof(struct GNode));
Graph->Nv = VertexNum;
Graph->Ne = 0;
//初始化
for(i=0; i<=Graph->Nv; i++) {
Graph->G[i].FirstEdge = NULL;
}
return Graph;
}
void InsertEdge( LGraph Graph, Edge* E )
{
//新建节点
PtrToAdjVNode NewNode;
NewNode = (PtrToAdjVNode)malloc(sizeof(struct AdjVNode));
NewNode->V = E->V2;
//插入到V1
NewNode->Next = Graph->G[E->V1].FirstEdge;
Graph->G[E->V1].FirstEdge = NewNode;
//新建节点
NewNode = (PtrToAdjVNode)malloc(sizeof(struct AdjVNode));
NewNode->V = E->V1;
//插入到V1
NewNode->Next = Graph->G[E->V2].FirstEdge;
Graph->G[E->V2].FirstEdge = NewNode;
}
int BFS( LGraph Graph, Queue* Q, Vertex V )
{
int cnt = 0;
int level = 0;
//Last 当前曾最后一个顶点
//Tail 下一层最后一个顶点
Vertex Last, Tail;
Last = V;
Visited[V] = 1;
cnt = 1;
AddQ( Q, V );
PtrToAdjVNode temp, flag;
temp = (PtrToAdjVNode)malloc(sizeof(struct AdjVNode));
flag = temp;
while( !IsEmpty(Q) ) {
V = DeleteQ(Q);
temp = Graph->G[V].FirstEdge;
while(temp) {
if(!Visited[temp->V]) {
cnt++;
Visited[temp->V] = 1;
AddQ(Q, temp->V);
Tail = temp->V;
}
temp = temp->Next;
}
if( V == Last ) {
level++;
Last = Tail;
}
if(level == 6) {
break;
}
}
free(flag);
return cnt;
}
void ListComponents( LGraph Graph )
{
Vertex V;
int i;
int cnt;
for(V = 1; V <= Graph->Nv; V++) {
SetZero( Visited, Graph->Nv );
Queue* Q = CreatQueue();
cnt = BFS( Graph, Q, V );
free(Q);
printf("%d: %.2f%%\n", V, cnt*1.0/(Graph->Nv)*100);
}
}
void SetZero( int V[], int n )
{
//重置Visited[]
int i;
for(i=0; i <= n; i++) {
V[i] = 0;
}
}
//队列操作
Queue* CreatQueue()
{
Queue* Q = (Queue*)malloc(sizeof(Queue));
Q->Front = Q->Tail = -1;
return Q;
}
void AddQ( Queue* Q, Vertex V)
{
//队列长度 MaxVertexNum + 1
int MaxSzie = MaxVertexNum + 1;
if((Q->Tail+1)%MaxSzie == Q->Front) {
printf("队列满");
}
else {
Q->Tail = (Q->Tail + 1) % MaxSzie;
Q->Data[Q->Tail] = V;
}
}
int IsEmpty( Queue* Q )
{
return (Q->Front == Q->Tail);
}
Vertex DeleteQ( Queue* Q )
{
//front指向队列头的前一位
int MaxSize = MaxVertexNum + 1;
if( Q->Front == Q->Tail ) {
printf("队列空");
}
else {
Q->Front = (Q->Front + 1) % MaxSize;
return Q->Data[Q->Front];
}
}