聚类系数可变无标度网络模型Holme-Kim HK模型
# -*- 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模型的更多相关文章
- Scale Free Network | 无标度网络
在看WGCNA的时候看到的一个术语. 先来看一个随机网络:没有中心节点,大部分节点都均匀的连在一起. 再看一下scale free network:大部分的连接都集中在少数的中心 如何检验一个网络是否 ...
- 聚类系数(clustering coefficient)计算
转自http://blog.csdn.net/pennyliang/article/details/6838956 Clustering coefficient的定义有两种:全局的和局部的. 全局的算 ...
- 网络模型 —— OSI七层模型,TCP五层模型,以及区分
1. OSI七层模型 OSI层 介绍 功能 TCP/IP协议 应用层 操作系统或网络应用程序提供访问网络服务的接口. 文件传输.浏览器.电子邮件 HTTP, FTP, TFTP, SNMP, DNS ...
- barabasilab-networkScience学习笔记4-无标度特征
第一次接触复杂性科学是在一本叫think complexity的书上,Allen博士很好的讲述了数据结构与复杂性科学,barabasi是一个知名的复杂性网络科学家,barabasilab则是他所主导的 ...
- 聚类 高维聚类 聚类评估标准 EM模型聚类
高维数据的聚类分析 高维聚类研究方向 高维数据聚类的难点在于: 1.适用于普通集合的聚类算法,在高维数据集合中效率极低 2.由于高维空间的稀疏性以及最近邻特性,高维的空间中基本不存在数据簇. 在高维聚 ...
- 机器学习-聚类-k-Means算法笔记
聚类的定义: 聚类就是对大量未知标注的数据集,按数据的内在相似性将数据集划分为多个类别,使类别内的数据相似度较大而类别间的数据相似度较小,它是无监督学习. 聚类的基本思想: 给定一个有N个对象的数据集 ...
- 机器学习 - 算法 - 聚类算法 K-MEANS / DBSCAN算法
聚类算法 概述 无监督问题 手中无标签 聚类 将相似的东西分到一组 难点 如何 评估, 如何 调参 基本概念 要得到的簇的个数 - 需要指定 K 值 质心 - 均值, 即向量各维度取平均 距离的度量 ...
- Sklearn K均值聚类
## 版权所有,转帖注明出处 章节 SciKit-Learn 加载数据集 SciKit-Learn 数据集基本信息 SciKit-Learn 使用matplotlib可视化数据 SciKit-Lear ...
- 聚类算法——DBSCAN算法原理及公式
聚类的定义 聚类就是对大量未知标注的数据集,按数据的内在相似性将数据集划分为多个类别,使类别内的数据相似度较大而类别间的数据相似度较小.聚类算法是无监督的算法. 常见的相似度计算方法 闵可夫斯基距离M ...
随机推荐
- Linux应用开发入门(转)
今天偶然看到这篇文章,做个入门了解还是不错的. 前一阵子在QQ上和朋友聊天的时候,总会看到有人说Linux上的应用程序开发是高手才可以完成的,而且这种“迷信”在目前似乎还很普遍.然而,情况并不是这样的 ...
- 2018.07.01洛谷P2617 Dynamic Rankings(带修主席树)
P2617 Dynamic Rankings 题目描述 给定一个含有n个数的序列a[1],a[2],a[3]--a[n],程序必须回答这样的询问:对于给定的i,j,k,在a[i],a[i+1],a[i ...
- day8 异常处理
异常和错误 part1:程序中难免出现错误,而错误分成两种 1.语法错误(这种错误,根本过不了python解释器的语法检测,必须在程序执行前就改正) 2.逻辑错误(逻辑错误) part2:什么是异常 ...
- hibernate从浅至深
hibernate在开发中的位置 web------>struts2--------->hibernate ORM框架 Hibernate是一个数据持久化层的ORM框架. Object:对 ...
- Quartus II中使用脚本转换sof到rbf文件
1. 新建一个文本文件,保存为任意但有意义的名字,如:sof_to_rbf.bat,注意,保存时请不要使用默认的格式,应该手动从.txt切换为all files 2. 在文本中输入以下内容: %Q ...
- 快速学会在JSP中使用EL表达式
在没有学会EL表达式之前,我们想在JSP文件中获取servlet或者其他JSP页面传来的值,通常都是在JSP页面中编写java代码来实现.而在jsp页面编写Java 代码,这种做法时不规范的,将会产生 ...
- Lucene 中自定义排序的实现
使用Lucene来搜索内容,搜索结果的显示顺序当然是比较重要的.Lucene中Build-in的几个排序定义在大多数情况下是不适合我们使用的.要适合自己的应用程序的场景,就只能自定义排序功能,本节我们 ...
- System.Windows.Freezable 在未被引用的程序集中定义
System.Windows.Freezable 在未被引用的程序集中定义 解决方法 添加windowsbase.dll 引用
- 4.C#WebAPI多版本管理介绍及实现方案详解
1.什么是 API 的多版本? 说白了就是多版本共存的问题.为方便大家理解我就举个例子吧,大家想必都用过Jquery吧,它的1.*版本做到了对低版本IE的支持:2.*版本还保留着ajax,但是不再支持 ...
- .Net Core in Docker - 在容器内编译发布并运行
Docker可以说是现在微服务,DevOps的基础,咱们.Net Core自然也得上Docker..Net Core发布到Docker容器的教程网上也有不少,但是今天还是想来写一写. 你搜.Net c ...