Package ziggy :: Package GraphReduce :: Module PBFS_mapper
[hide private]
[frames] | no frames]

Source Code for Module ziggy.GraphReduce.PBFS_mapper

 1  #! /usr/bin/env python 
 2  ''' 
 3  Created on Aug 12, 2010 
 4   
 5  @author: dwmclary 
 6  ''' 
 7  import sys 
 8  import re 
 9   
10 -class BFSNode(object):
11 - def __init__(self, nID=None, distance=None, adj=None, path=None):
12 if nID != None: 13 self.nID = nID 14 self.distance = distance 15 self.adj = adj 16 self.path = path
17
18 - def write(self):
19 s = str(self.nID)+ " "+ " ".join(map(str,self.adj)) +\ 20 " d: "+str(self.distance) + " path: " + " ".join(map(str,self.path)) 21 return s
22
23 - def __str__(self):
24 return self.write()
25 26
27 -def read_adjacency(file, comment="#", sep=None):
28 for line in file: 29 line = line.rstrip() 30 if line[0] != comment: 31 entry = line.split(sep) 32 nid = entry[0] 33 if "d:" in entry: 34 d_index = entry.index("d:") 35 distance = float(entry[d_index+1]) 36 p_index = entry.index("path:") 37 path = entry[p_index+1:] 38 adj = entry[1:d_index] 39 else: 40 distance = float('inf') 41 path = [] 42 adj = entry[1:] 43 44 node = BFSNode(nid,distance,adj, path) 45 yield node
46 47 48 #def main(n): 49 # data = read_adjacency(sys.stdin) 50 # M = {} 51 # N = {} 52 # for node in data: 53 # if node.nID == n: 54 # node.distance = 0.0 55 # 56 # d = node.distance 57 # 58 # print node.write() 59 # 60 # if node.distance < float('inf'): 61 # M[node.nID] = node 62 # else: 63 # N[node.nID] = node 64 # 65 # for key in M.keys(): 66 # n = M[key] 67 # for m in n.adj: 68 # if m in N: 69 # print N[m].nID + " pathlen: " + str(n.distance+1) 70
71 -def main(n):
72 data = read_adjacency(sys.stdin) 73 for node in data: 74 if node.nID == n: 75 node.distance = 0.0 76 print n + " pathlen: 0.0" + " path: " 77 d = node.distance 78 79 print node 80 81 for m in node.adj: 82 path = [node.nID] + node.path 83 print m + " pathlen: " + str(d+1) + " path: " + ",".join(path)
84 85 86 87 88 89 if __name__ == "__main__": 90 try: 91 import psyco 92 psyco.full() 93 except: 94 pass 95 source = open("pbfs_source").readlines() 96 source = source[0].strip() 97 main(source) 98