06-Figure 3 six degrees space, 06-figure six degrees Space

Source: Internet
Author: User

06-Figure 3 six degrees space, 06-figure six degrees Space

I used DEV-C ++ to test the use case, passed, but submitted to PAT is all a paragraph error, today is no way. I spent a whole day asking me to write my thoughts on solving this problem. Sorry, I have a headache.

  1 #include <stdio.h>  2 #include <stdlib.h>  3 #include <stdbool.h>  4 #include <string.h>  5   6 typedef struct node  7 {  8     int v;  9     struct node * next; 10 }Node, * pNode; 11 typedef struct 12 { 13     pNode * adjList; 14     int n; 15     bool * visited; 16 }ALGraph, * pALGraph; 17 typedef struct 18 { 19     int * elem; 20     int front, rear, size; 21 }Queue, * pQueue; 22  23 pALGraph initGraph(int N); 24 void link(pALGraph pG, int v1, int v2); 25 void insert(pNode pN, int vv); 26 int BFS(pALGraph, int vv, int N); 27 pQueue createQueue(int N); 28 bool isEmpty(pQueue pQ); 29 void inQueue(pQueue pQ, int e); 30 int outQueue(pQueue pQ); 31  32 int main() 33 { 34 //    freopen("in.txt", "r", stdin); // for test 35     int i, N, M; 36     scanf("%d%d", &N, &M); 37      38     pALGraph pG; 39     pG = initGraph(N); 40     for(i = 0; i < M; i++) 41     { 42         int v1, v2; 43         scanf("%d%d", &v1, &v2); 44         link(pG, v1, v2); 45     } 46     int count; 47     for(i = 1; i <= N; i++) 48     { 49         count = BFS(pG, i, N); 50         printf("%d: %.2f%%\n", i, (float)count / N * 100); 51     } 52 //    fclose(stdin); // for test 53     return 0; 54 } 55  56 pALGraph initGraph(int N) 57 { 58     pALGraph pG; 59     pG = (pALGraph)malloc(sizeof(ALGraph)); 60     pG->adjList = (pNode *)malloc((N + 1) * sizeof(pNode)); 61     pG->n = N + 1; 62     pG->visited = (bool *)malloc((N + 1) * sizeof(bool)); 63     memset(pG->visited, false, (N + 1) * sizeof(bool)); 64     for(int i = 0; i < N + 1; i++) 65     { 66         pG->adjList[i] = (pNode)malloc(sizeof(Node)); 67         pG->adjList[i]->v = i; 68         pG->adjList[i]->next = NULL; 69     } 70      71     return pG; 72 } 73  74 void link(pALGraph pG, int v1, int v2) 75 { 76     insert(pG->adjList[v1], v2); 77     insert(pG->adjList[v2], v1); 78 } 79  80 void insert(pNode pN, int vv) 81 { 82     pNode tmp = (pNode)malloc(sizeof(Node)); 83     tmp->v = vv; 84     tmp->next = pN->next; 85     pN->next = tmp; 86 //    free(tmp); 87 } 88  89 int BFS(pALGraph pG, int vv, int N) 90 { 91     pQueue pQ; 92     pQ = createQueue(N); 93      94     int i, count, level, last, tail; 95     pG->visited[vv] = true; 96     count = 1; 97     level = 0; 98     last = vv; 99     inQueue(pQ, vv);100     while(!isEmpty(pQ))101     {102         vv = outQueue(pQ);103         pNode pN = pG->adjList[vv]->next;104         while(pN)105         {106             if(!pG->visited[pN->v])107             {108                 pG->visited[pN->v] = true;109                 count++;110                 inQueue(pQ, pN->v);111                 tail = pN->v;112             }113             pN = pN->next;114         }115         if(vv == last)116         {117             level++;118             last = tail;119         }120         if(level == 6)121             break;122     }123     memset(pG->visited, false, (N + 1) * sizeof(bool));124     125     return count;126 }127 128 pQueue createQueue(int N)129 {130     pQueue pQ;131     pQ = (pQueue)malloc(sizeof(Queue));132     pQ->elem = (int *)malloc((N + 1) * sizeof(int));133     pQ->front = pQ->rear = 0;134     pQ->size = N + 1;135 }136 137 bool isEmpty(pQueue pQ)138 {139     if(pQ->front != pQ->rear)140         return false;141     else142         return true;143 }144 145 void inQueue(pQueue pQ, int e)146 {147     pQ->rear = (pQ->rear + 1) % pQ->size;148     pQ->elem[pQ->rear] = e;149 }150 151 int outQueue(pQueue pQ)152 {153     pQ->front = (pQ->front + 1) % pQ->size;154     return pQ->elem[pQ->front];155 }

The Six-degree space theory is also called the Six Degrees of Separation theory. This theory can be explained in plain words: "There are no more than six people separated between you and any stranger. That is to say, you can know any stranger by five people at most ." 6.4.


Figure 6.4 six-degree Space

Although the "Six Degrees space" theory has been widely recognized and is being applied more and more. However, for decades, attempting to verify this theory has always been the goal pursued by many scientists. However, due to historical reasons, such research has too many limitations and difficulties. As modern people rely mainly on tools such as telephone, text messages, and instant messaging on the Internet, the first-hand data that reflects social network relationships has gradually made it possible to verify the "Six Degrees space" theory.

For a social network diagram, calculate the percentage of nodes that comply with the "Six Degrees space" Theory to the total number of nodes.

Input format description:

The input row contains two positive integers, indicating the number of knots N (1 <N <= 1st, indicating the number of workers) and number of edges M (<= 33 * N, number of social relationships ). The subsequent M rows correspond to M edges, and each row gives a positive integer, which is the numbers of the two nodes that the edge is directly connected to (nodes numbered from 1 to N ).

Output format description:

The percentage of nodes with no more than 6 points between each node and the node to the total number of nodes is accurate to the second decimal point. Output a line for each nodule point in the format of "node number: (Space) % ".

Sample input and output:

Serial number Input Output
1
10 91 22 33 44 55 66 77 88 99 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 81 22 33 44 55 66 77 89 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 101 21 31 44 56 56 76 88 98 1010 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 11 2
1: 100.00%2: 100.00%

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.