Advertisement
loubot

Untitled

Apr 28th, 2013
570
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.91 KB | None | 0 0
  1. import string
  2. ##from graph import Digraph, Edge, Node
  3. class Node(object):
  4.     def __init__(self, name):
  5.         self.name = str(name)
  6.     def getName(self):
  7.         return self.name
  8.     def __str__(self):
  9.         return self.name
  10.     def __repr__(self):
  11.         return self.name
  12.     def __eq__(self, other):
  13.         return self.name == other.name
  14.     def __ne__(self, other):
  15.         return not self.__eq__(other)
  16.  
  17. class Edge(object):
  18.     def __init__(self, src, dest):
  19.         self.src = src
  20.         self.dest = dest
  21.     def getSource(self):
  22.         return self.src
  23.     def getDestination(self):
  24.         return self.dest
  25.     def __str__(self):
  26.         return str(self.src) + '->' + str(self.dest)
  27.  
  28. ##class WeightedEdge(Edge):
  29. ##    def __init__(self,src, dest, dist, weight):
  30. ##        self.src = src
  31. ##        self.dest = dest
  32. ##        self.dist = dist
  33. ##        self.weight = weight
  34. ##    def getDist(self):
  35. ##        return self.dist
  36. ##    def getWeight(self):
  37. ##        return self.weight
  38.  
  39. class Digraph(object):
  40.     def __init__(self):
  41.         self.nodes = set([])
  42.         self.edges = {}
  43.     def addNode(self, node):
  44.         if node in self.nodes:
  45.             raise ValueError('Duplicate node')
  46.         else:
  47.             self.nodes.add(node)
  48.             self.edges[node] = []
  49.     def addEdge(self, edge):
  50.         src = edge.getSource()
  51.         print 'src',src
  52.         dest = edge.getDestination()
  53.         print 'dest',dest
  54.         for s in self.nodes:
  55.             print s
  56.         if not(src in self.nodes and dest in self.nodes):
  57.                 raise ValueError('Node not in graph')
  58.         self.edges[src].append(dest)
  59.     def childrenOf(self, node):
  60.         return self.edges[node]
  61.     def hasNode(self, node):
  62.         return node in self.nodes
  63.     def __str__(self):
  64.         res = ''
  65.         for k in self.edges:
  66.             for d in self.edges[k]:
  67.                 res = res + str(k) + '->' + str(d) + '\n'
  68.         return res[:-1]
  69.  
  70. #
  71. # Problem 2: Building up the Campus Map
  72. #
  73. # Write a couple of sentences describing how you will model the
  74. # problem as a graph)
  75. #
  76. mapFileName = 'mit_map.txt'
  77. def load_map():
  78.     """
  79.    Parses the map file and constructs a directed graph
  80.  
  81.    Parameters:
  82.        mapFilename : name of the map file
  83.  
  84.    Assumes:
  85.        Each entry in the map file consists of the following four positive
  86.        integers, separated by a blank space:
  87.            From To TotalDistance DistanceOutdoors
  88.        e.g.
  89.            32 76 54 23
  90.        This entry would become an edge from 32 to 76.
  91.  
  92.    Returns:
  93.        a directed graph representing the map
  94.    """
  95.     #TODO
  96.     s = ('32 36 70 0')
  97.     map = Digraph()
  98.    
  99.     l = s.rsplit()
  100.     n1 = Node(int(l[0]))
  101.     print 'n',n1
  102.     n2 = Node(int(l[1]))
  103.     print "n2",n2
  104.     edge1 = Edge(int(l[0]),int(l[1]))
  105.     map.addNode(n1)
  106.     map.addNode(n2)
  107.     map.addEdge(edge1)
  108.  
  109.  
  110.  
  111.  
  112.  
  113. load_map()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement