Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # Diego Ceccarelli, 2012
- from multiprocessing import Pool
- def mapFunction(value):
- # work on values
- return (value[0],len(value[1]))
- def partition(tuples):
- # marshalll tuples into a dictionary of lists of tuples
- mapping = {}
- for t in tuples:
- try:
- mapping[t[1]].append (t)
- except KeyError:
- mapping[t[1]] = [t]
- return mapping
- def reduceFunction(mapping):
- #print mapping
- count = 0
- for node_degree in mapping[1]:
- count += 1
- return (mapping[0],count)
- if __name__ == '__main__':
- pool = Pool(processes=10)
- graph = [ (1, [2,3,4,5]),
- (3, [6,10,5]),
- (10,[1,3,4]),
- (2,[3]),
- (4,[3]),
- (5,[10]),
- (6,[1]),
- (7,[1,2,3,4,5,6,8,9,10]),
- (8,[1,2,3,4,5,6,7,9,10]),
- (9,[1])
- ]
- print "GRAPH:"
- for node in graph :
- print node[0],"->",node[1]
- tuples = pool.map(mapFunction, graph)
- mapping = partition(tuples)
- results = pool.map(reduceFunction, mapping.items())
- print "------"
- print "DEGREEs"
- for res in results:
- print "Degree: ",res[0], "\tFrequency",res[1]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement