聚类系数可变无标度网络模型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 ...
随机推荐
- phoenix错误
spark集群与phoenix进行数据请求时报错: 2018-06-15 17:13:30,158 INFO - Starting task 15.3 in stage 116.0 (TID 6832 ...
- 二进制搭建kubernetes多master集群【一、使用TLS证书搭建etcd集群】
上一篇我们介绍了kubernetes集群架构以及系统参数配置,参考:二进制搭建kubernetes多master集群[开篇.集群环境和功能介绍] 下面本文etcd集群才用三台centos7.5搭建完成 ...
- spring boot入门与进阶
视频课程包含: SpringBoot入门.SpringBoot进阶.Spring Cloud微服务.Spring Ecosystem 微服务相关.Spring Boot 入门 IDEA 版本.Spri ...
- Linux服务器部署系列之八—Sendmail篇
Sendmail是目前Linux系统下面用得最广的邮件系统之一,虽然它存在一些不足,不过,目前还是有不少公司在使用它.对它的学习,也能让我们更深的了解邮件系统的运作.下面我们就来看看sendmail邮 ...
- public class 和class 的区别
Java在编写类的时候可以使用两种方式定义类: public class定义类: class定义类: 如果一个类声明的时候使用了public class进行了声明,则类名称必须与 ...
- python编码(七)
本文中,以'哈'来解释作示例解释所有的问题,“哈”的各种编码如下: 1. UNICODE (UTF8-16),C854:2. UTF-8,E59388:3. GBK,B9FE. 一.python中的s ...
- Redis Quick Start [遇到问题解决问题版]
http://redis.io/topics/quickstart make test 时的问题: 问题:gcc: Command not found 解决:yum install gcc [root ...
- WPF 嵌入字体文件
官方说明文档:将字体与应用程序一起打包 https://docs.microsoft.com/zh-cn/dotnet/framework/wpf/advanced/packaging-fonts-w ...
- OpenGL + MFC
OpenGL超级宝典(中文版) 2001年 本书是一本完整而详尽的关于OpenGL的参考书,全书分为四部分:第一部分“OpenGL导言”介绍3D图形学的基本原理,读者将在此学会构造使用OpenGL的程 ...
- UFOV页面 使用canvas
canvas画八边形:cxt.beginPath();cxt.beginPath(); canvas内线条的粗细:cxt.lineWidth = '2'; 鼠标消失: css: html, body ...