
[update]
剛剛看了 CNET Taiwan, 裡面的作業系統是 Linux。
Algorithm: join(node n)
If (Full(LeftRoutingTable(n)) and
Full(RightRoutingTable(n)) and
((LeftChild(n)==null) or (RightChild(n)==null))
Accept new node as child of n
Else
If ((Not Full(LeftRoutingTable(n))) or
(Not Full(RightRoutingTable(n))))
Forward the JOIN request to parent(n)
Else
m=SomeNodesNotHavingEnoughChildrenIn
(LeftRoutingTable(n), RightRoutingTable(n))
If (there exists such an m)
Forward the JOIN request to m
Else
Forward the JOIN request to one of its
adjacent nodes
End If
End If
End If
def join(child, n):
if Full(n['LeftRoutingTable']) and \
Full(n['RightRoutingTable']) and \
((n['LeftChild'] == None) or (n['RightChild'] == None)):
print ' ' + str(n['Name']) + ' -- ' + str(child['Name']) + ';'
accept(child, n)
else:
if not Full(n['LeftRoutingTable']) or not Full(n['RightRoutingTable']):
join(child, n['Parent'])
else:
m = NodesNotEnoughChildren(n['LeftRoutingTable'], n['RightRoutingTable'])
if(m != None):
join(child, m)
else:
# Forward the JOIN request to one ofits adjacent nodes
if n['LeftAdjacent'] != None:
join(child, n['LeftAdjacent'])
else:
join(child, n['RightAdjacent'])
def node(name):
return {
'Parent' : None,
'LeftChild' : None,
'RightChild' : None,
'LeftRoutingTable' : list(),
'RightRoutingTable' : list(),
'LeftAdjacent' : None,
'RightAdjacent' : None,
'Name' : name,
'Level' : 0
}
def travel(node, tstr):
if node['LeftChild'] != None:
tstr += ' ' + str(node['Name']) + ' -- ' + str(node['LeftChild']['Name']) + ';\n'
tstr = travel(node['LeftChild'], tstr)
if node['RightChild'] != None:
tstr += ' ' + str(node['Name']) + ' -- ' + str(node['RightChild']['Name']) + ';\n'
tstr = travel(node['RightChild'], tstr)
return tstr
root = node('root')
child = node('child')
join(child, root)
for i in range(0, int(sys.argv[1])):
c = node(i)
join(c, root)
print 'Finish\n'
tstr = travel(root, '')
temp = Template('graph G{\n$info\n}\n')
s = temp.substitute(info=tstr)
file = open('temp.dot', 'w')
file.write(s)
file.close()
os.system('dot -Tsvg -o temp.svg temp.dot')
os.system('eog temp.svg')
#!/usr/bin/env python
import random, os, Gnuplot
mean = 3
strdev = 10
ns = [10, 100, 1000, 10000]
g = Gnuplot.Gnuplot()
g('set multiplot')
g('set size 0.5,0.5')
for i in range(0, 4):
gauss = list()
for j in range(0, ns[i]):
gauss.append(list())
gauss[j].append(random.gauss(mean, strdev))
gauss[j].append(random.gauss(mean, strdev))
if i == 0:
g('set origin 0,0.5')
elif i == 1:
g('set origin 0.5,0.5')
elif i == 2:
g('set origin 0,0')
else:
g('set origin 0.5,0')
g.plot(gauss)
g('unset multiplot')
raw_input('Please press return to continue...\n')
#!/usr/bin/env python
import statistics, random, Gnuplot
gauss = list()
gauss2 = list()
gpdf = list()
gpdf2 = list()
g = Gnuplot.Gnuplot()
for i in range(0, 1000):
gauss.append(random.gauss(-2, 1))
gauss2.append(random.gauss(2, 2))
#gauss.append(random.gauss(2, 1))
#gauss2.append(random.gauss(-2, 1))
y, x = statistics.pdf(gauss, kernel = 'Gaussian')
w, z = statistics.pdf(gauss2, kernel = 'Gaussian')
for i in range(0, len(x)):
gpdf.append(list())
gpdf[i].append(x[i])
gpdf[i].append(y[i])
for i in range(0, len(w)):
gpdf2.append(list())
gpdf2[i].append(z[i])
gpdf2[i].append(w[i])
g.plot(gpdf, gpdf2)
raw_input('Please press return to continue...\n')