Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import string
- ##from graph import Digraph, Edge, Node
- class Node(object):
- def __init__(self, name):
- self.name = str(name)
- def getName(self):
- return self.name
- def __str__(self):
- return self.name
- def __repr__(self):
- return self.name
- def __eq__(self, other):
- return self.name == other.name
- def __ne__(self, other):
- return not self.__eq__(other)
- class Edge(object):
- def __init__(self, src, dest):
- self.src = src
- self.dest = dest
- def getSource(self):
- return self.src
- def getDestination(self):
- return self.dest
- def __str__(self):
- return str(self.src) + '->' + str(self.dest)
- ##class WeightedEdge(Edge):
- ## def __init__(self,src, dest, dist, weight):
- ## self.src = src
- ## self.dest = dest
- ## self.dist = dist
- ## self.weight = weight
- ## def getDist(self):
- ## return self.dist
- ## def getWeight(self):
- ## return self.weight
- class Digraph(object):
- def __init__(self):
- self.nodes = set([])
- self.edges = {}
- def addNode(self, node):
- if node in self.nodes:
- raise ValueError('Duplicate node')
- else:
- self.nodes.add(node)
- self.edges[node] = []
- def addEdge(self, edge):
- src = edge.getSource()
- print 'src',src
- dest = edge.getDestination()
- print 'dest',dest
- for s in self.nodes:
- print s
- if not(src in self.nodes and dest in self.nodes):
- raise ValueError('Node not in graph')
- self.edges[src].append(dest)
- def childrenOf(self, node):
- return self.edges[node]
- def hasNode(self, node):
- return node in self.nodes
- def __str__(self):
- res = ''
- for k in self.edges:
- for d in self.edges[k]:
- res = res + str(k) + '->' + str(d) + '\n'
- return res[:-1]
- #
- # Problem 2: Building up the Campus Map
- #
- # Write a couple of sentences describing how you will model the
- # problem as a graph)
- #
- mapFileName = 'mit_map.txt'
- def load_map():
- """
- Parses the map file and constructs a directed graph
- Parameters:
- mapFilename : name of the map file
- Assumes:
- Each entry in the map file consists of the following four positive
- integers, separated by a blank space:
- From To TotalDistance DistanceOutdoors
- e.g.
- 32 76 54 23
- This entry would become an edge from 32 to 76.
- Returns:
- a directed graph representing the map
- """
- #TODO
- s = ('32 36 70 0')
- map = Digraph()
- l = s.rsplit()
- n1 = Node(int(l[0]))
- print 'n',n1
- n2 = Node(int(l[1]))
- print "n2",n2
- edge1 = Edge(int(l[0]),int(l[1]))
- map.addNode(n1)
- map.addNode(n2)
- map.addEdge(edge1)
- load_map()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement