05-3. 六度空间 (PAT) - 图的遍历问题
“六度空间”理论又称作“六度分隔(Six Degrees of Separation)”理论。这个理论可以通俗地阐述为:“你和任何一个陌生人之间所间隔的人不会超过六个,也就是说,最多通过五个人你就能够认识任何一个陌生人。”如图6.4所示。
图6.4 六度空间示意图
“六度空间”理论虽然得到广泛的认同,并且正在得到越来越多的应用。但是数十年来,试图验证这个理论始终是许多社会学家努力追求的目标。然而由于历史的原因,这样的研究具有太大的局限性和困难。随着当代人的联络主要依赖于电话、短信、微信以及因特网上即时通信等工具,能够体现社交网络关系的一手数据已经逐渐使得“六度空间”理论的验证成为可能。
假如给你一个社交网络图,请你对每个节点计算符合“六度空间”理论的结点占结点总数的百分比。
输入格式说明:
输入第1行给出两个正整数,分别表示社交网络图的结点数N (1<N<=104,表示人数)、边数M(<=33*N,表示社交关系数)。随后的M行对应M条边,每行给出一对正整数,分别是该条边直接连通的两个结点的编号(节点从1到N编号)。
输出格式说明:
对每个结点输出与该结点距离不超过6的结点数占结点总数的百分比,精确到小数点后2位。每个结节点输出一行,格式为“结点编号:(空格)百分比%”。
样例输入与输出:
序号 | 输入 | 输出 |
1 |
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 |
10 8 1 2 2 3 3 4 4 5 5 6 6 7 7 8 9 10 |
1: 70.00% 2: 80.00% 3: 80.00% 4: 80.00% 5: 80.00% 6: 80.00% 7: 80.00% 8: 70.00% 9: 20.00% 10: 20.00% |
3 |
11 10 1 2 1 3 1 4 4 5 6 5 6 7 6 8 8 9 8 10 10 11 |
1: 100.00% 2: 90.91% 3: 90.91% 4: 100.00% 5: 100.00% 6: 100.00% 7: 100.00% 8: 100.00% 9: 100.00% 10: 100.00% 11: 81.82% |
4 |
2 1 1 2 |
1: 100.00% 2: 100.00% |
题意:统计每个顶点六层范围内所有顶点数占总顶点数比例
解题思路:对每个顶点进行层序遍历(BFS),在遍历过程中,记录顶点所在层数
记录层数利用两个标记变量tail,lastest
tail用于记录当前层的最后一个顶点;在遍历与某个顶点相连的顶点时,lastest用于记录所遍历的当前顶点
当tail == 出队列的顶点时,说明本层已经遍历完毕,则使tail = 当前的lastest,使其等于下一层的最后一个顶点
#include <iostream> #include <queue> #include <iomanip> using namespace std; typedef struct listNode { int data; listNode *next; }*plist, nlist; typedef struct { plist *listArray; int graphSize; bool *visited; float *sixDegreePercent; }*pGraph, nGraph; pGraph CreateGraph(int size); void ConnectGraphVertex(pGraph pG, int vStart, int vEnd); void InsertListNode(plist pL, int vertex); int BFS(pGraph pG, int vertex); void DestoryGraph(pGraph pG); pGraph CreateGraph(int size) { pGraph pG = new nGraph; pG->graphSize = size; pG->listArray = new plist[size]; pG->visited = new bool[size]; pG->sixDegreePercent = new float[size]; for (int i = 0; i < size; i++) { pG->listArray[i] = new nlist; pG->listArray[i]->data = i; pG->listArray[i]->next = NULL; pG->visited[i] = false; pG->sixDegreePercent[i] = 0; } return pG; } void DestoryGraph(pGraph pG) { plist tempListHead; plist tempListNode; for (int i = 0; i < pG->graphSize; i++) { tempListHead = pG->listArray[i]; while ( tempListHead != NULL ) { tempListNode = tempListHead; tempListHead = tempListHead->next; delete tempListNode; } } delete[]pG->listArray; delete[]pG->visited; delete[]pG->sixDegreePercent; delete pG; } void InsertListNode(plist pL, int vertex) { plist temp = new nlist; temp->data = vertex; temp->next = pL->next; pL->next = temp; return; } void ConnectGraphVertex(pGraph pG, int vStart, int vEnd) { if ( pG == NULL || vStart < 0 || vEnd < 0 || vStart == vEnd ) { return; } //将vEnd插入vStart链表 InsertListNode(pG->listArray[vStart], vEnd); //将vStart插入vEnd链表 InsertListNode(pG->listArray[vEnd], vStart); return; } int BFS(pGraph pG, int vertex) { queue<int> que; int source; plist listIter; int lastest; int tail = vertex; int level = 0; int sum = 1; //将初始结点也算在内 int maxLayer = 6; if ( pG->visited[vertex] == false ) { que.push(vertex); pG->visited[vertex] = true; } while ( que.empty() != true && level < maxLayer ) { source = que.front(); que.pop(); //遍历与vertex相连的点 listIter = pG->listArray[source]->next; while ( listIter != NULL ) { if ( pG->visited[listIter->data] == false ) { pG->visited[listIter->data] = true; sum++; que.push(listIter->data); lastest = listIter->data; } listIter = listIter->next; } if ( tail == source ) //判断是否已经遍历完该层所有结点 { level++; tail = lastest; } } //完成遍历后,要清空visited的状态 for ( int i = 0; i < pG->graphSize; i++) { pG->visited[i] = false; } return sum; } int main() { int N, E; cin >> N >> E; pGraph pG; pG = CreateGraph(N + 1); int i; int edgeStart, edgeEnd; for (i = 0; i < E; i++) { cin >> edgeStart >> edgeEnd; edgeStart; edgeEnd; ConnectGraphVertex(pG, edgeStart, edgeEnd); } float percentage; int count; for (i = 1; i <= N; i++) { count = BFS(pG, i); percentage = (float)count / N * 100; cout << fixed << setprecision(2); cout << i << ": " << percentage << '%' << endl; } DestoryGraph(pG); return 0; }