ziggy.GraphReduce.PBFS_reducer

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

# Source Code for Module ziggy.GraphReduce.PBFS_reducer

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