def __init__(self, edges, title): self.id2name = dict() self.readTitle(title) self.graph = Digraph(len(self.id2name)) self.readEdge(edges) #sanity check print "# of vertices ", self.graph.numOfVertices() print "# of edges ", self.graph.numOfEdges()
def main(): graph = Digraph(4) graph.addArc(0,1,23) graph.addArc(0,2,82) graph.addArc(0,20,8) graph.addArc(2,20,92) graph.addArc(2,1,2) graph.addArc(20,1,18) lista = TravelAgent.path(graph,0) print (lista)
from Digraph import * from Exercise1 import shortest from nReinas import nReinas from time import time_ns if __name__ == "__main__": graph = Digraph(6) graph.addArc(0, 5, 1) graph.addArc(0, 2, 4) graph.addArc(0, 3, 2) graph.addArc(0, 1, 20) graph.addArc(2, 1, 30) graph.addArc(3, 2, 1) graph.addArc(3, 5, 400) graph.addArc(1, 4, 15) weigth = shortest(graph, 0, 4) print(weigth, "\n") initial = time_ns() nReinas(4) final = time_ns() print("tiempo: ", final - initial, " ns")
import Digraph import directedDfs di = Digraph.Digraph(7) di.addEdge(0, 1) di.addEdge(0, 5) di.addEdge(2, 0) di.addEdge(2, 3) di.addEdge(3, 2) di.addEdge(3, 5) di.addEdge(4, 2) di.addEdge(4, 3) di.addEdge(5, 4) di.addEdge(6, 0) di.addEdge(6, 4) print di.getEdges(1) diDfs = directedDfs.directedDfs(di, 0) print diDfs.path
def pathTo(self, v): if not self.hasPathTo(v): return None path = [] x = v while self.distTo[x] != 0: path.append(x) x = self.edgeTo[x] path.append(x) path.reverse() return path if __name__ == "__main__": graph = Digraph(5) graph.addEdge(0, 1) graph.addEdge(1, 2) graph.addEdge(2, 3) source = 1 bfs = BFS(graph, source) for i in range(graph.numOfVertices()): if bfs.hasPathTo(i): print "%d to %d (%d)" % (source, i, bfs.distanceTo(i)) for x in bfs.pathTo(i): if x == source: print x, else: print "-> " + str(x), print "\n" else: print "%d to %d (-) : not connected\n" % (source, i)