看起来不太可能有一个小于O(n*|V|^2)
的解决方案,所以这里提供了一种用Python实现的方法,看起来还不错。
def lineE(N):
return(set((i,i+1) for i in range(N-1)))
def ringE(N):
return(lineE(N).union([(0,N-1)]))
def fullE(N):
return(set([(i,j) for i in range(N) for j in range(i)]))
def propagate(V, curr, x, y, d):
nexty = set()
for cx in curr[x]:
if not cx in V[y]["seen"]:
V[y]["seen"].add(cx)
V[y]["foaf"][d] = V[y]["foaf"].get(d,0) + 1
nexty.add(cx)
return(nexty)
def mingle(N, E, D):
V = dict((i, {"seen":set([i]), "foaf":{0:1}}) for i in range(N))
curr = dict((i, set([i])) for i in range(N))
for d in range(1, min(D+1, N)):
next = dict((i, set()) for i in range(N))
for (h, t) in E:
next[t] = next[t].union(propagate(V, curr, h, t, d))
next[h] = next[h].union(propagate(V, curr, t, h, d))
curr = next
return(V)
使用 10 个节点和距离为 3 进行尝试,
N=10
D=3
for (topology, E) in [("line", lineE(N)), ("ring", ringE(N)), ("full", fullE(N))]:
V = mingle(N, E, D)
print "\n", topology
for v in V:
print v, V[v]["foaf"]
我们得到
line
0 {0: 1, 1: 1, 2: 1, 3: 1}
1 {0: 1, 1: 2, 2: 1, 3: 1}
2 {0: 1, 1: 2, 2: 2, 3: 1}
3 {0: 1, 1: 2, 2: 2, 3: 2}
4 {0: 1, 1: 2, 2: 2, 3: 2}
5 {0: 1, 1: 2, 2: 2, 3: 2}
6 {0: 1, 1: 2, 2: 2, 3: 2}
7 {0: 1, 1: 2, 2: 2, 3: 1}
8 {0: 1, 1: 2, 2: 1, 3: 1}
9 {0: 1, 1: 1, 2: 1, 3: 1}
ring
0 {0: 1, 1: 2, 2: 2, 3: 2}
1 {0: 1, 1: 2, 2: 2, 3: 2}
2 {0: 1, 1: 2, 2: 2, 3: 2}
3 {0: 1, 1: 2, 2: 2, 3: 2}
4 {0: 1, 1: 2, 2: 2, 3: 2}
5 {0: 1, 1: 2, 2: 2, 3: 2}
6 {0: 1, 1: 2, 2: 2, 3: 2}
7 {0: 1, 1: 2, 2: 2, 3: 2}
8 {0: 1, 1: 2, 2: 2, 3: 2}
9 {0: 1, 1: 2, 2: 2, 3: 2}
full
0 {0: 1, 1: 9}
1 {0: 1, 1: 9}
2 {0: 1, 1: 9}
3 {0: 1, 1: 9}
4 {0: 1, 1: 9}
5 {0: 1, 1: 9}
6 {0: 1, 1: 9}
7 {0: 1, 1: 9}
8 {0: 1, 1: 9}
9 {0: 1, 1: 9}
看起来是正确的。此外,在我的笔记本电脑上运行距离100的简单拓扑结构,有100000个节点大约需要一分钟的时间。当然,如果你有一个密集的图形(如fullE
),这将会爆炸。
N=100000
D=100
for (topology, E) in [("line", lineE(N)), ("ring", ringE(N))]:
V = mingle(N, E, D)