def main(): g = graph.UndirectedGraph() g.addVertex("1.1.1.1") g.addVertex("1.1.1.2") g.addVertex("1.1.2.3") g.addVertex("1.4.4.4") g.addVertex("0.0.0.0") g.addVertex("0.1.1.1") g.addVertex("0.0.1.1") g.addEdge("1.1.1.1", "1.1.1.2", 5) g.addEdge("1.1.1.1", "1.1.2.3", 7) g.addEdge("1.4.4.4", "1.1.1.1", 12) g.addEdge("1.4.4.4", "1.1.1.2", 2) g.addEdge("1.1.1.1", "0.0.0.0", 1) g.addEdge("0.0.0.0", "0.1.1.1", 1) g.addEdge("0.1.1.1", "0.0.1.1", 1) g.addEdge("0.0.1.1", "1.4.4.4", 1) # f = open("graph.pkl", "rb") # g = pickle.load(f) # f.close() g.print_graph() start = "1.1.1.1" dest = "1.4.4.4" res = graph.dijsktra(g, start) distances = res[0] print "DIST: "+str(distances[dest]) paths = res[1] # print "Distances: " # print distances path = graph.get_path(start, dest, paths) graph.print_path(start, dest, g, path) # g.print_graph() f = open(r"graph.pickle", "wb") pickle.dump(g, f, pickle.HIGHEST_PROTOCOL) f.close() stored_obj = open(r"graph.pickle", "rb") g2 = pickle.load(stored_obj) stored_obj.close() print "Printing g2 loaded from pickled file" g2.print_graph()
import graph, cPickle as pickle, sys try: ip1 = str(sys.argv[1]) ip2 = str(sys.argv[2]) with open("python/edges.pickle", "rb") as input_file: e = pickle.load(input_file) with open("python/vertices.pickle", "rb") as input_file: v = pickle.load(input_file) g = graph.UndirectedGraph() g.newEdges(e) g.newVertices(v) if ip1 not in g.vertices(): print ip1 + " has no measurements yet." elif ip2 not in g.vertices(): print ip2 + " has no measurements yet." else: res = graph.dijsktra(g, ip1) dist_ret = res[0] dist = dist_ret.get(ip2) print ip1 + " -> " + ip2 + ": " + str(dist) + "(s)" except Exception, e: print "Python Error: " + str(e)
def receive_message_dvr(self, msg): if msg['type'] in ('normal', 'chat'): # parceo string to json message = get_dict(msg["body"]) message_type = message['type'] message_from = message['from'] if message_type == 'message': message_to = message['to'] if message['to'] != self.jid: closest = self.get_closest(message_to) # send message self.send_individual_message( message_to, message['msg'], message['hops'] + 1, message['origin'], closest['path'], closest['distance'] + message['distance']) else: print(message) elif message_type == 'connection': # update neighbors changes = False nodes = message['table'] for n in nodes: if n not in self.table: changes = True self.table[message_from] = nodes[n] elif len(nodes[n]) > len(self.table[n]): changes = True self.table[message_from] = nodes[n] # fill empties for node in self.table: if node != self.jid: i = 0 for their_neighbor in self.table[node]: for my_neighbor in self.table[self.jid]: if their_neighbor[0] == my_neighbor[0]: i = i + 1 if i == 0 and their_neighbor[0] != self.jid: changes = True self.table[self.jid].append( (their_neighbor[0], None)) # fill distance # generate graph # (nodeA, nodeB, distance) graph = Graph() edges = [] for node in self.table: for connection in self.table[node]: if connection[1] is not None: edges.append((node, connection[0], connection[1])) for edge in edges: graph.add_edge(*edge) # apply to everyone for index, neighbor in enumerate(self.table[self.jid]): name = neighbor[0] path = dijsktra(graph, self.jid, name) distance = 0 for i in range(1, len(path)): previus = self.table[path[i - 1]] for p in previus: if p[0] == path[i]: distance = distance + p[1] if distance < neighbor[1]: self.table[self.jid][index] = (path[i], distance) if True: connection_msg = make(self.jid, self.table) for n in self.neighbors: self.send_message(mto=n[0], mbody=connection_msg) else: # Error print('Error') print(msg['body'])