gpt4 book ai didi

graph - 如何计算图的熵?

转载 作者:行者123 更新时间:2023-12-04 02:15:58 25 4
gpt4 key购买 nike

我有一组随机生成的形式图,我想计算每个的熵。同一个问题换个说法:我有好几个网络,想计算每个网络的信息量。

以下是包含图熵的正式定义的两个来源:
http://www.cs.washington.edu/homes/anuprao/pubs/CSE533Autumn2010/lecture4.pdf (PDF)
http://arxiv.org/abs/0711.4175v1

我正在寻找的代码将图作为输入(作为边列表或邻接矩阵)并输出一些位或其他一些信息内容度量。

因为我在任何地方都找不到它的实现,所以我打算根据正式定义从头开始编写代码。如果有人已经解决了这个问题并愿意分享代码,那将不胜感激。

最佳答案

我最终使用了不同的论文来定义图熵:
复杂网络的信息论:关于进化和架构约束
RV Sole 和 S. Valverde (2004)

基于拓扑结构的网络熵及其对随机网络的计算
B.H.王 W.X.王和T. Zhou

计算每个的代码如下。该代码假设您有一个无向、未加权的图,没有自环。它以邻接矩阵作为输入并返回以位为单位的熵量。它在 R 中实现并使用了 sna package .

graphEntropy <- function(adj, type="SoleValverde") {
if (type == "SoleValverde") {
return(graphEntropySoleValverde(adj))
}
else {
return(graphEntropyWang(adj))
}
}

graphEntropySoleValverde <- function(adj) {
# Calculate Sole & Valverde, 2004 graph entropy
# Uses Equations 1 and 4
# First we need the denominator of q(k)
# To get it we need the probability of each degree
# First get the number of nodes with each degree
existingDegrees = degree(adj)/2
maxDegree = nrow(adj) - 1
allDegrees = 0:maxDegree
degreeDist = matrix(0, 3, length(allDegrees)+1) # Need an extra zero prob degree for later calculations
degreeDist[1,] = 0:(maxDegree+1)
for(aDegree in allDegrees) {
degreeDist[2,aDegree+1] = sum(existingDegrees == aDegree)
}
# Calculate probability of each degree
for(aDegree in allDegrees) {
degreeDist[3,aDegree+1] = degreeDist[2,aDegree+1]/sum(degreeDist[2,])
}
# Sum of all degrees mult by their probability
sumkPk = 0
for(aDegree in allDegrees) {
sumkPk = sumkPk + degreeDist[2,aDegree+1] * degreeDist[3,aDegree+1]
}
# Equivalent is sum(degreeDist[2,] * degreeDist[3,])
# Now we have all the pieces we need to calculate graph entropy
graphEntropy = 0
for(aDegree in 1:maxDegree) {
q.of.k = ((aDegree + 1)*degreeDist[3,aDegree+2])/sumkPk
# 0 log2(0) is defined as zero
if (q.of.k != 0) {
graphEntropy = graphEntropy + -1 * q.of.k * log2(q.of.k)
}
}
return(graphEntropy)
}

graphEntropyWang <- function(adj) {
# Calculate Wang, 2008 graph entropy
# Uses Equation 14
# bigN is simply the number of nodes
# littleP is the link probability. That is the same as graph density calculated by sna with gden().
bigN = nrow(adj)
littleP = gden(adj)
graphEntropy = 0
if (littleP != 1 && littleP != 0) {
graphEntropy = -1 * .5 * bigN * (bigN - 1) * (littleP * log2(littleP) + (1-littleP) * log2(1-littleP))
}
return(graphEntropy)
}

关于graph - 如何计算图的熵?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6950791/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com