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()
class OEIS: 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 readTitle(self, title): f = open(title, 'r') lines = f.readlines() for line in lines: tokens = line.split(' ', 1) id = int(tokens[0]) - 1 definition = tokens[1] self.id2name[id] = definition def readEdge(self, edges): f = open(edges, 'r') lines = f.readlines() total = 0 for line in lines: tokens = line.split() start = int(tokens[0]) - 1 count = int(tokens[1]) if count != len(tokens) - 2: raise Exception("not match") total += count if count == 0: continue for end in tokens[2:]: end = int(end) - 1 if end >= len(self.id2name): continue self.graph.addEdge(start, end) def getTitle(self, v): return self.id2name[v]
return self.distTo[v] 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)
import Digraph Di1 = Digraph.digraph(5) Di1.addEdge(0,1) Di1.addEdge(0,5) Di1.addEdge(0,2) Di1.addEdge(3,2) Di1.addEdge(3,4) Di1.addEdge(3,5) Di1.addEdge(3,6) Di1.addEdge(5,2) Di1.addEdge(6,0) print Di1.getNumNodes() print Di1.getEdges(5)
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 topologicalSort di = Digraph.Digraph(7) di.addEdge(0, 1) di.addEdge(0, 5) di.addEdge(0, 2) di.addEdge(1, 4) di.addEdge(5, 2) di.addEdge(3, 2) di.addEdge(3, 5) di.addEdge(3, 4) di.addEdge(3, 6) di.addEdge(6, 0) diDfs = topologicalSort.topologicalSort(di, 0) print diDfs.postorder
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)