Print negative weight cycle in a Directed Graph | Structure to represent a weighted edge in graph ; Structure to represent a directed and weighted graph ; V . Number of vertices , E . Number of edges ; Graph is represented as an array of edges . ; Creates a new graph with V vertices and E edges ; Function runs Bellman - Ford algorithm and prints negative cycle ( if present ) ; Relax all edges | V | - 1 times . ; Check for negative - weight cycles ; Store one of the vertex of the negative weight cycle ; To store the cycle vertex ; Reverse cycle [ ] ; Printing the negative cycle ; Driver Code ; Number of vertices in graph ; Number of edges in graph ; Given Graph ; Function Call
class Edge : NEW_LINE INDENT def __init__ ( self ) : NEW_LINE INDENT self . src = 0 NEW_LINE self . dest = 0 NEW_LINE self . weight = 0 NEW_LINE DEDENT DEDENT class Graph : NEW_LINE INDENT def __init__ ( self ) : NEW_LINE INDENT self . V = 0 NEW_LINE self . E = 0 NEW_LINE self . edge = [ ] NEW_LINE DEDENT DEDENT def createGraph ( V , E ) : NEW_LINE INDENT graph = Graph ( ) ; NEW_LINE graph . V = V ; NEW_LINE graph . E = E ; NEW_LINE graph . edge = [ Edge ( ) for i in range ( graph . E ) ] NEW_LINE return graph ; NEW_LINE DEDENT def NegCycleBellmanFord ( graph , src ) : NEW_LINE INDENT V = graph . V ; NEW_LINE E = graph . E ; NEW_LINE dist = [ 1000000 for i in range ( V ) ] NEW_LINE parent = [ - 1 for i in range ( V ) ] NEW_LINE dist [ src ] = 0 ; NEW_LINE for i in range ( 1 , V ) : NEW_LINE INDENT for j in range ( E ) : NEW_LINE INDENT u = graph . edge [ j ] . src ; NEW_LINE v = graph . edge [ j ] . dest ; NEW_LINE weight = graph . edge [ j ] . weight ; NEW_LINE if ( dist [ u ] != 1000000 and dist [ u ] + weight < dist [ v ] ) : NEW_LINE INDENT dist [ v ] = dist [ u ] + weight ; NEW_LINE parent [ v ] = u ; NEW_LINE DEDENT DEDENT DEDENT C = - 1 ; NEW_LINE for i in range ( E ) : NEW_LINE INDENT u = graph . edge [ i ] . src ; NEW_LINE v = graph . edge [ i ] . dest ; NEW_LINE weight = graph . edge [ i ] . weight ; NEW_LINE if ( dist [ u ] != 1000000 and dist [ u ] + weight < dist [ v ] ) : NEW_LINE INDENT C = v ; NEW_LINE break ; NEW_LINE DEDENT DEDENT if ( C != - 1 ) : NEW_LINE INDENT for i in range ( V ) : NEW_LINE INDENT C = parent [ C ] ; NEW_LINE DEDENT cycle = [ ] NEW_LINE v = C NEW_LINE while ( True ) : NEW_LINE INDENT cycle . append ( v ) NEW_LINE if ( v == C and len ( cycle ) > 1 ) : NEW_LINE INDENT break ; NEW_LINE DEDENT v = parent [ v ] NEW_LINE DEDENT cycle . reverse ( ) NEW_LINE for v in cycle : NEW_LINE INDENT print ( v , end = " ▁ " ) ; NEW_LINE DEDENT print ( ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( - 1 ) ; NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT V = 5 ; NEW_LINE E = 5 ; NEW_LINE graph = createGraph ( V , E ) ; NEW_LINE graph . edge [ 0 ] . src = 0 ; NEW_LINE graph . edge [ 0 ] . dest = 1 ; NEW_LINE graph . edge [ 0 ] . weight = 1 ; NEW_LINE graph . edge [ 1 ] . src = 1 ; NEW_LINE graph . edge [ 1 ] . dest = 2 ; NEW_LINE graph . edge [ 1 ] . weight = 2 ; NEW_LINE graph . edge [ 2 ] . src = 2 ; NEW_LINE graph . edge [ 2 ] . dest = 3 ; NEW_LINE graph . edge [ 2 ] . weight = 3 ; NEW_LINE graph . edge [ 3 ] . src = 3 ; NEW_LINE graph . edge [ 3 ] . dest = 4 ; NEW_LINE graph . edge [ 3 ] . weight = - 3 ; NEW_LINE graph . edge [ 4 ] . src = 4 ; NEW_LINE graph . edge [ 4 ] . dest = 1 ; NEW_LINE graph . edge [ 4 ] . weight = - 3 ; NEW_LINE NegCycleBellmanFord ( graph , 0 ) ; NEW_LINE DEDENT