# -*- coding: cp936 -*-
import random
import networkx as nx
from networkx.generators.classic import empty_graph def powerlaw_cluster_graph(n, m, p, seed=None):
"""Holme and Kim algorithm for growing graphs with powerlaw
degree distribution and approximate average clustering. Parameters
----------
n : int
the number of nodes
m : int
the number of random edges to add for each new node
p : float,
Probability of adding a triangle after adding a random edge
seed : int, optional
Seed for random number generator (default=None). Notes
-----
The average clustering has a hard time getting above a certain
cutoff that depends on ``m``. This cutoff is often quite low. The
transitivity (fraction of triangles to possible triangles) seems to
decrease with network size. It is essentially the Barabási–Albert (BA) growth model with an
extra step that each random edge is followed by a chance of
making an edge to one of its neighbors too (and thus a triangle). This algorithm improves on BA in the sense that it enables a
higher average clustering to be attained if desired. It seems possible to have a disconnected graph with this algorithm
since the initial ``m`` nodes may not be all linked to a new node
on the first iteration like the BA model. Raises
------
NetworkXError
If ``m`` does not satisfy ``1 <= m <= n`` or ``p`` does not
satisfy ``0 <= p <= 1``. References
----------
.. [1] P. Holme and B. J. Kim,
"Growing scale-free networks with tunable clustering",
Phys. Rev. E, 65, 026107, 2002.
""" if m < 1 or n < m:
raise nx.NetworkXError(\
"NetworkXError must have m>1 and m<n, m=%d,n=%d"%(m,n)) if p > 1 or p < 0:
raise nx.NetworkXError(\
"NetworkXError p must be in [0,1], p=%f"%(p))
if seed is not None:
random.seed(seed) G=empty_graph(m) # add m initial nodes (m0 in barabasi-speak)
G.name="Powerlaw-Cluster Graph"
repeated_nodes=G.nodes() # list of existing nodes to sample from
# with nodes repeated once for each adjacent edge
source=m # next node is m
while source<n: # Now add the other n-1 nodes
possible_targets = _random_subset(repeated_nodes,m)
# do one preferential attachment for new node
target=possible_targets.pop()
G.add_edge(source,target)
repeated_nodes.append(target) # add one node to list for each new link
count=1
while count<m: # add m-1 more new links
if random.random()<p: # clustering step: add triangle
neighborhood=[nbr for nbr in G.neighbors(target) \
if not G.has_edge(source,nbr) \
and not nbr==source]
if neighborhood: # if there is a neighbor without a link
nbr=random.choice(neighborhood)
G.add_edge(source,nbr) # add triangle
repeated_nodes.append(nbr)
count=count+1
continue # go to top of while loop
# else do preferential attachment step if above fails
target=possible_targets.pop()
G.add_edge(source,target)
repeated_nodes.append(target)
count=count+1 repeated_nodes.extend([source]*m) # add source node to list m times
source += 1
return G
def _random_subset(seq,m):
""" Return m unique elements from seq. This differs from random.sample which can return repeated
elements if seq holds repeated elements.
:param seq:
:param m:
:return:
"""
targets=set()
while len(targets)<m:
x=random.choice(seq)
targets.add(x)
return targets
if __name__=="__main__":
n=input(" the number of nodes:")
m=input("the number of random edges to add for each new node:")
p=input("Probability of adding a triangle after adding a random edge:")
g=powerlaw_cluster_graph(n, m, p, seed=None)
node = list(g.nodes())
edge = list(g.edges())
# with open('node.pickle', 'wb') as f:
# pickle.dump(node, f)
#with open('edge.pickle', 'wb') as f:
# pickle.dump(edge, f)
#print(node)
#print(edge)
#edge = list(edge)
fil = open('edge.txt', 'w')
for i in edge:
fil.write('{} {}\n'.format(*i))
fil.close()

  生成无标度网络,通过P控制聚类系数

聚类系数可变无标度网络模型Holme-Kim HK模型的更多相关文章

  1. Scale Free Network | 无标度网络

    在看WGCNA的时候看到的一个术语. 先来看一个随机网络:没有中心节点,大部分节点都均匀的连在一起. 再看一下scale free network:大部分的连接都集中在少数的中心 如何检验一个网络是否 ...

  2. 聚类系数(clustering coefficient)计算

    转自http://blog.csdn.net/pennyliang/article/details/6838956 Clustering coefficient的定义有两种:全局的和局部的. 全局的算 ...

  3. 网络模型 —— OSI七层模型,TCP五层模型,以及区分

    1. OSI七层模型 OSI层  介绍 功能 TCP/IP协议 应用层 操作系统或网络应用程序提供访问网络服务的接口. 文件传输.浏览器.电子邮件 HTTP, FTP, TFTP, SNMP, DNS ...

  4. barabasilab-networkScience学习笔记4-无标度特征

    第一次接触复杂性科学是在一本叫think complexity的书上,Allen博士很好的讲述了数据结构与复杂性科学,barabasi是一个知名的复杂性网络科学家,barabasilab则是他所主导的 ...

  5. 聚类 高维聚类 聚类评估标准 EM模型聚类

    高维数据的聚类分析 高维聚类研究方向 高维数据聚类的难点在于: 1.适用于普通集合的聚类算法,在高维数据集合中效率极低 2.由于高维空间的稀疏性以及最近邻特性,高维的空间中基本不存在数据簇. 在高维聚 ...

  6. 机器学习-聚类-k-Means算法笔记

    聚类的定义: 聚类就是对大量未知标注的数据集,按数据的内在相似性将数据集划分为多个类别,使类别内的数据相似度较大而类别间的数据相似度较小,它是无监督学习. 聚类的基本思想: 给定一个有N个对象的数据集 ...

  7. 机器学习 - 算法 - 聚类算法 K-MEANS / DBSCAN算法

    聚类算法 概述 无监督问题 手中无标签 聚类 将相似的东西分到一组 难点 如何 评估, 如何 调参 基本概念 要得到的簇的个数  - 需要指定 K 值 质心 - 均值, 即向量各维度取平均 距离的度量 ...

  8. Sklearn K均值聚类

    ## 版权所有,转帖注明出处 章节 SciKit-Learn 加载数据集 SciKit-Learn 数据集基本信息 SciKit-Learn 使用matplotlib可视化数据 SciKit-Lear ...

  9. 聚类算法——DBSCAN算法原理及公式

    聚类的定义 聚类就是对大量未知标注的数据集,按数据的内在相似性将数据集划分为多个类别,使类别内的数据相似度较大而类别间的数据相似度较小.聚类算法是无监督的算法. 常见的相似度计算方法 闵可夫斯基距离M ...

随机推荐

  1. 2018.09.13 poj2728Desert King(最优比率生成树)

    传送门 01分数规划经典题. 不过用krsukal会T掉. 这题用prim反而更快(毕竟是完全图) 因此直接二分+最小生成树搞定. 代码: #include<iostream> #incl ...

  2. Spring boot 注解简单备忘

    Spring boot 注解简单备忘 1.定义注解 package com.space.aspect.anno;import java.lang.annotation.*; /** * 定义系统日志注 ...

  3. hdu-1251(string+map)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1251 思路:重点是用gets输入,而且用a[20],不能直接输入string类型的. #include ...

  4. spring boot使用java读取配置文件,DateSource测试,BomCP测试,AnnotationConfigApplicationContext的DataSource注入

    一.配置注解读取配置文件         (1)@PropertySource可以指定读取的配置文件,通过@Value注解获取值   实例:           @PropertySource(val ...

  5. Redis配置总结

    一:常用配置 1.bind,格式为bind 127.0.0.1:这个是很重要的配置,如果bind 127.0.0.1则外部网络是访问不了的(如果外部网络要访问还要开放端口) 2.port,格式为por ...

  6. faceswap linux安裝教程

    http://www.mamicode.com/info-detail-2602743.html https://blog.csdn.net/sinat_26918145/article/detail ...

  7. Javascript 中函数的 length 属性

    每个函数都有一个 length属性 (函数名.length), 表示期望接收的函数的个数(而不是实际接收的参数个数) 它与arguments不同. arguments.length 是表示函数实际接收 ...

  8. 洛谷P2633 Count on a tree(主席树上树)

    题目描述 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K小的点权.其中lastans是上一个询问的答案,初始为0,即第一个 ...

  9. 使用VSTS进行单元测试练习

    本次作业要求:练习教科书第22~25页单元测试练习,要求自行安装Visual Studio开发平台,版本至少在2010以上,要求把程序安装过程和练习过程写到博客上,越详细越好,要图文并茂,没有书的同学 ...

  10. Send or receive files via Xshell

    1. install lrzsz $ sudo apt-get install lrzsz 2. If you want to send file from your pc to pi, just d ...