06-图3 六度空间 (30 分)
“六度空间”理论又称作“六度分隔(Six Degrees of Separation)”理论。这个理论可以通俗地阐述为:“你和任何一个陌生人之间所间隔的人不会超过六个,也就是说,最多通过五个人你就能够认识任何一个陌生人。”如图1所示。
图1 六度空间示意图
“六度空间”理论虽然得到广泛的认同,并且正在得到越来越多的应用。但是数十年来,试图验证这个理论始终是许多社会学家努力追求的目标。然而由于历史的原因,这样的研究具有太大的局限性和困难。随着当代人的联络主要依赖于电话、短信、微信以及因特网上即时通信等工具,能够体现社交网络关系的一手数据已经逐渐使得“六度空间”理论的验证成为可能。
假如给你一个社交网络图,请你对每个节点计算符合“六度空间”理论的结点占结点总数的百分比。
输入格式:
输入第1行给出两个正整数,分别表示社交网络图的结点数N(1<N≤104,表示人数)、边数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%
这一题我使用的是三角矩阵的方式来压缩存储无向图
但是发现由于三角矩阵用起来比较麻烦,在很多细节上有很多不同的地方要考虑清晰,所以导致程序看起来很不友好(例如遍历时不能用定点数来作为跳出条件,要通过总共存了多少空间来作为跳出条件,但是有些数组又要用顶点数来作为跳出条件,例如visited数组)
所以如果要用三角矩阵感觉要建立一个类才行
#include <iostream>
#include <deque>
#include <iomanip>
#define ElementType int
#define Vertex int
typedef struct _ENode
{
Vertex V1;
Vertex V2;
}Edge;
typedef struct _GNode
{
int num_of_array;//下三角矩阵存储,所以和一般的二维不一样
int Nv;//顶点数
int Ne;//边数
ElementType *G;//下三角临接矩阵,一维代替二维
}Graph;
using namespace std;
void creat_Graph(Graph *, int num_v, int num_e);
void insert_Edge(Graph *, Edge);
void Six_Degrees(Graph *);
double Calculate_Six_Percent(Graph *, Vertex, int degrees, bool visited[]);
void print(Graph *);
double *percent;
int main()
{
int N, M;
cin >> N >> M;
Graph *graph = new Graph;
creat_Graph(graph, N, M);
for (int i = 0; i < M; i++)
{
Edge edge;
cin >> edge.V1 >> edge.V2;
insert_Edge(graph, edge);
}
percent = new double[graph->num_of_array];
Six_Degrees(graph);
print(graph);
return 0;
}
void creat_Graph(Graph *graph, int num_v, int num_e)
{
graph->num_of_array = num_v * (num_v + 1) / 2;
graph->Nv = num_v;
graph->Ne = num_e;
graph->G = new ElementType[graph->num_of_array];
for (int i = 0; i < graph->num_of_array; i++) //下三角临接矩阵初始化
graph->G[i] = 0;
}
void insert_Edge(Graph *graph, Edge edge)
{
edge.V1 -= 1;//输出的是从1开始计数,而此程序矩阵从0开始
edge.V2 -= 1;
if (edge.V1 < edge.V2)
graph->G[edge.V2*(edge.V2 + 1) / 2 + edge.V1] = 1;
else
graph->G[edge.V1*(edge.V1 + 1) / 2 + edge.V2] = 1;
}
void Six_Degrees(Graph *graph)
{
bool *visited = new bool[graph->Nv];
for (Vertex i = 0; i < graph->Nv; i++)
{
for (int i = 0; i < graph->Nv; i++)//初始化visited,visited只有Nv个数据
visited[i] = 0;
percent[i] = Calculate_Six_Percent(graph, i, 1, visited);//percent也只有Nv个数据
}
}
double Calculate_Six_Percent(Graph *graph, Vertex start_V, int degrees, bool visited[])
{
Vertex *queue = new Vertex[graph->num_of_array + 1];
int front = 0, rear = 0, last = 0;
int num_six_degrees = 0;//不超过6度的顶点数
Vertex V = start_V;
visited[V] = 1;
queue[rear++] = V;//入队
while (front != rear && degrees <= 6)
{
V = queue[front++];//出队
Vertex target_V = 0;//由于三角矩阵的关系,i不能表示顶点,遂用 target_V 表示要与顶点 V 做判断的顶点
Vertex start_TLine = V * (V + 1) / 2;
Vertex start_VLine = start_TLine + V;
for (Vertex i = start_TLine; i < start_TLine + V; i++) //遍历横向顶点
{
if (graph->G[i] == 1 && visited[target_V] == 0) { //有边且未被访问过
queue[rear++] = target_V;
visited[target_V] = 1;
num_six_degrees++;
}
target_V++;
}
int k = 0;
for (Vertex i = start_VLine; i < graph->num_of_array; i = i + V + k)//遍历竖向顶点
{
k++;
if (graph->G[i] == 1 && visited[target_V] == 0) {
queue[rear++] = target_V;
visited[target_V] = 1;
num_six_degrees++;
}
target_V++;
}
if (last == front - 1) {
degrees++;
last = rear - 1;
}
}
return (double)(num_six_degrees + 1) / graph->Nv;
}
void print(Graph *graph)
{
int flag = 1;
cout << setprecision(2) << setiosflags(ios::fixed);
for (int i = 0; i < graph->Nv; i++)
{
if (flag)
flag = 0;
else
cout << endl;
cout << i+1 << ": " << percent[i] * 100 << "%";
}
}