前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >06-图3 六度空间 (30分)

06-图3 六度空间 (30分)

作者头像
AI那点小事
发布2020-04-18 20:19:48
6060
发布2020-04-18 20:19:48
举报
文章被收录于专栏:AI那点小事AI那点小事

“六度空间”理论又称作“六度分隔(Six Degrees of Separation)”理论。这个理论可以通俗地阐述为:“你和任何一个陌生人之间所间隔的人不会超过六个,也就是说,最多通过五个人你就能够认识任何一个陌生人。”如图1所示。

这里写图片描述
这里写图片描述

图1 六度空间示意图 “六度空间”理论虽然得到广泛的认同,并且正在得到越来越多的应用。但是数十年来,试图验证这个理论始终是许多社会学家努力追求的目标。然而由于历史的原因,这样的研究具有太大的局限性和困难。随着当代人的联络主要依赖于电话、短信、微信以及因特网上即时通信等工具,能够体现社交网络关系的一手数据已经逐渐使得“六度空间”理论的验证成为可能。

假如给你一个社交网络图,请你对每个节点计算符合“六度空间”理论的结点占结点总数的百分比。

输入格式:

输入第1行给出两个正整数,分别表示社交网络图的结点数NN(1

代码语言:javascript
复制
#include <iostream>
#include <stdio.h>
#include <queue> 
using namespace std;

class Graph {
    private:
        int Nv;             //顶点数
        int Ne;             //边数
        int** G;            //邻接矩阵
        int* isVisited;     //访问数组
    public:
        //构造函数
        Graph(int nv,int ne) {
            this->Nv = nv;
            this->Ne = ne;
            this->isVisited = new int[nv];
            this->G = new int*[nv];
            for ( int i = 0 ; i < nv ; i++) {
                this->G[i] = new int[nv];
                this->isVisited[i] = 0;
            }
            for ( int i = 0 ; i  < nv ; i++){
                for ( int j = 0 ; j < nv ; j++){
                    this->G[i][j] = 0;
                    this->G[i][i] = 1; 
                }
            }

            for ( int i = 0 ; i < this->Ne ; i++){
                int a,b;
                cin>>a>>b;
                this->G[a-1][b-1] = 1;
                this->G[b-1][a-1] = 1;
            }
        }

        int BFS(int start) {
            queue<int> que;
            int cnt = 1;
            int level = 0;
            int last = start;
            int tail;
            this->isVisited[start] = 1;
            que.push(start);
            while(!que.empty()) {
                int neiborgh = que.front();
                que.pop();
                for ( int i = 0 ; i < this->Nv ; i++) {
                    if ( this->G[neiborgh][i] && !this->isVisited[i]) {
                        que.push(i);
                        this->isVisited[i] = 1;
                        cnt++;  
                        tail = i;
                    }
                }
                if (neiborgh == last){
                    level++;
                    last = tail;
                }
                if ( level == 6){
                    break;
                }
            }
            return cnt;
        }

    void SVD(){
        for ( int i = 0 ; i < this->Nv ; i++ ){
            MemSet_Visited();
            int cnt = BFS(i);
            double percent = cnt * 1.0 / this->Nv;
            printf("%d: %.2f%%\n",i+1,percent*100);
        }
    }

    void MemSet_Visited(){
        for(int i = 0 ; i < this->Nv ; i++){
            this->isVisited[i] = 0;
        } 
    } 
};

int main()
{
    int nv,ne;
    cin>>nv>>ne;
    Graph graph(nv,ne); 
    graph.SVD();

    return 0;
 } 

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
短信
腾讯云短信(Short Message Service,SMS)可为广大企业级用户提供稳定可靠,安全合规的短信触达服务。用户可快速接入,调用 API / SDK 或者通过控制台即可发送,支持发送验证码、通知类短信和营销短信。国内验证短信秒级触达,99%到达率;国际/港澳台短信覆盖全球200+国家/地区,全球多服务站点,稳定可靠。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档