Exemplo n.º 1
0
 def findNegativeCycle(self):
     V = len(self.edgeTo)
     spt = EdgeWeightedDigraph(V)
     for v in range(V):
         if self.edgeTo[v] != None:
             spt.addEdge(self.edgeTo[v])
     finder = EdgeWeightedDirectedCycle(spt)
     self.cycle = finder.Cycle()
Exemplo n.º 2
0
    def main(self, f):
        utils = Utils()
        N, I, Tij = utils.read_file(f)
        G = EdgeWeightedDigraph(N + 2)
        s = N
        t = N + 1
        for i in range(N):
#             G.addEdge(DirectedEdge(s, i, 0))
            G.addEdge(DirectedEdge(i, t, 0))
            
            for j in range(len(Tij[i])):
                G.addEdge(DirectedEdge(I[i][j], i, -( Tij[i][j] ) ) )     # Negate edge weight
                G.addEdge(DirectedEdge(s, I[i][j], 0))
#                 G.addEdge(DirectedEdge(I[i][j], t, 0))
                
        bfsp = BellmanFordSP(G, s)
        if bfsp.hasNegativeCycle():
            bfsp = None
        utils.write_file(bfsp)