Guest User

Untitled

a guest
Oct 18th, 2012
114
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python
  2.  
  3. # Diego Ceccarelli, 2012
  4.  
  5. from multiprocessing import Pool
  6.  
  7.  
  8. def mapFunction(value):
  9.     # work on values
  10.     return (value[0],len(value[1]))
  11.  
  12. def partition(tuples):
  13.     # marshalll tuples into a dictionary of lists of tuples
  14.     mapping = {}
  15.     for t in tuples:
  16.         try:
  17.             mapping[t[1]].append (t)
  18.         except KeyError:
  19.             mapping[t[1]] = [t]
  20.     return mapping
  21.  
  22. def reduceFunction(mapping):
  23.  
  24.     #print mapping
  25.     count = 0
  26.     for node_degree in mapping[1]
  27.         count += 1
  28.     return (mapping[0],count)
  29.  
  30. if __name__ == '__main__':
  31.     pool = Pool(processes=10)
  32.     graph = [   (1, [2,3,4,5]),
  33.                 (3, [6,10,5]),
  34.                 (10,[1,3,4]),
  35.                 (2,[3]),
  36.                 (4,[3]),
  37.                 (5,[10]),
  38.                 (6,[1]),
  39.                 (7,[1,2,3,4,5,6,8,9,10]),
  40.                 (8,[1,2,3,4,5,6,7,9,10]),
  41.                 (9,[1])
  42.             ]
  43.     print "GRAPH:"
  44.     for node in graph :
  45.         print node[0],"->",node[1]
  46.    
  47.    
  48.     tuples = pool.map(mapFunction, graph)
  49.     mapping = partition(tuples)
  50.     results = pool.map(reduceFunction, mapping.items())
  51.     print "------"
  52.     print "DEGREEs"
  53.     for res in results:
  54.         print "Degree: ",res[0], "\tFrequency",res[1]
RAW Paste Data