Given a weighted directed graph consisting of V vertices and E edges. The task is to print the cyclic path whose sum of weight is negative. If there is no such path present then print “-1”.
Input: V = 5, E = 5, Below is the graph:
Here, for the given negative cycle o/p (1->2->3->4->1) ; In fig there has to be Edge from 4–>1 not from 4–>0
Output: 1 2 3 4 1
Explanation:
Given graph contains a negative cycle, (1->2->3->4->1)Input: V = 5, E = 5, Below is the graph:
Output: 0 1 2 3 4 0
Explanation:
Given graph contains a negative cycle, (0->1->2->3->4->0)
Approach: The idea is to use Bellman-Ford Algorithm which is used to detect a negative cycle or not. To print the negative cycles, perform the Nth iteration of Bellman-Ford and pick a vertex from any edge which is relaxed in this iteration. Using this vertex and its ancestors, the negative cycle can be printed. Below are the steps:
- Perform N-1 iterations of Bellman-Ford algorithm and relax each edge (u, v). Keep track of parent of each vertex and store in an array parent[].
- Now, do one more iteration and if no edge relaxation take place in this Nth iteration, then there is no cycle of negative weight exists in the graph.
- Otherwise take a variable C and store the vertex v from any edge (u, v), which is relaxed in the Nth iteration.
- Now, starting from the C vertex go towards its ancestors until a cycle is found and finally print it.
- This cycle will be the desired cycle of negative weight.
Below is the implementation of the above approach:
C++
| // C++ program for the above approach#include <bits/stdc++.h>usingnamespacestd;// Structure to represent a weighted// edge in graphstructEdge {    intsrc, dest, weight;};// Structure to represent a directed// and weighted graphstructGraph {    // V -> Number of vertices,    // E -> Number of edges    intV, E;    // Graph is represented as an    // array of edges    structEdge* edge;};// Creates a new graph with V vertices// and E edgesstructGraph* createGraph(intV, intE){    structGraph* graph = newGraph;    graph->V = V;    graph->E = E;    graph->edge = newEdge[graph->E];    returngraph;}vector<int> vis;// Function runs Bellman-Ford algorithm// and prints negative cycle(if present)voidNegCycleBellmanFord(structGraph* graph,                        intsrc){    intV = graph->V;    intE = graph->E;    intdist[V];    intparent[V];    // Initialize distances from src    // to all other vertices as INFINITE    // and all parent as -1    for(inti = 0; i < V; i++) {        dist[i] = INT_MAX;        parent[i] = -1;    }    dist[src] = 0;    vis[src] = 0;    // Relax all edges |V| - 1 times.    boolflg = true;    for(inti = 1; i <= V - 1; i++) {      if(flg==false)            break;      flg=false;        for(intj = 0; j < E; j++) {            intu = graph->edge[j].src;            intv = graph->edge[j].dest;            intweight = graph->edge[j].weight;            if(dist[u] != INT_MAX                && dist[u] + weight < dist[v]) {                flg = true;                  vis[v] = 1;                dist[v] = dist[u] + weight;                parent[v] = u;            }        }    }    // Check for negative-weight cycles    intC = -1;    for(inti = 0; i < E; i++) {        intu = graph->edge[i].src;        intv = graph->edge[i].dest;        intweight = graph->edge[i].weight;        if(dist[u] != INT_MAX            && dist[u] + weight < dist[v]) {            // Store one of the vertex of            // the negative weight cycle            C = v;            break;        }    }    if(C != -1) {        for(inti = 0; i < V; i++)            C = parent[C];        // To store the cycle vertex        vector<int> cycle;        for(intv = C;; v = parent[v]) {            cycle.push_back(v);            if(v == C                && cycle.size() > 1)                break;        }        // Reverse cycle[]        reverse(cycle.begin(), cycle.end());        // Printing the negative cycle        for(intv : cycle)            cout << v << ' ';        cout << endl;       return;    }}// Driver Codeintmain(){    // Number of vertices in graph    intV = 5;    // Number of edges in graph    intE = 5;    structGraph* graph = createGraph(V, E);    vis.resize(V,0);    // Given Graph    graph->edge[0].src = 1;    graph->edge[0].dest = 0;    graph->edge[0].weight = 1;    graph->edge[1].src = 1;    graph->edge[1].dest = 2;    graph->edge[1].weight = 2;    graph->edge[2].src = 2;    graph->edge[2].dest = 3;    graph->edge[2].weight = 3;    graph->edge[3].src = 3;    graph->edge[3].dest = 4;    graph->edge[3].weight = -3;    graph->edge[4].src = 4;    graph->edge[4].dest = 1;    graph->edge[4].weight = -3;    graph->edge[5].src = 5;    graph->edge[5].dest = 6;    graph->edge[5].weight = -1;    graph->edge[6].src = 6;    graph->edge[6].dest = 7;    graph->edge[6].weight =-1;    graph->edge[7].src = 7;    graph->edge[7].dest = 5;    graph->edge[7].weight =-1;      // Function Call      for(intsrc = 0;src<V;src++)    {      if(vis[src]==0)         NegCycleBellmanFord(graph, src);    }         cout << "-1\n";    return0;} | 
Java
| // Java program for the above approachimportjava.util.ArrayList;importjava.util.Collections;classGFG{// Structure to represent a weighted// edge in graphstaticclassEdge {    intsrc, dest, weight;}// Structure to represent a directed// and weighted graphstaticclassGraph{        // V. Number of vertices, E.    // Number of edges    intV, E;    // Graph is represented as    // an array of edges.    Edge[] edge;}// Creates a new graph with V vertices// and E edgesstaticGraph createGraph(intV, intE) {    Graph graph = newGraph();    graph.V = V;    graph.E = E;    graph.edge = newEdge[graph.E];    for(inti = 0; i < graph.E; i++)    {        graph.edge[i] = newEdge();    }    returngraph;}// Function runs Bellman-Ford algorithm// and prints negative cycle(if present)staticvoidNegCycleBellmanFord(Graph graph, intsrc){    intV = graph.V;    intE = graph.E;    int[] dist = newint[V];    int[] parent = newint[V];    // Initialize distances from src    // to all other vertices as INFINITE    // and all parent as -1    for(inti = 0; i < V; i++)     {        dist[i] = 1000000;        parent[i] = -1;    }    dist[src] = 0;    // Relax all edges |V| - 1 times.    for(inti = 1; i <= V - 1; i++)    {        for(intj = 0; j < E; j++)        {            intu = graph.edge[j].src;            intv = graph.edge[j].dest;            intweight = graph.edge[j].weight;            if(dist[u] != 1000000&&                 dist[u] + weight < dist[v])            {                dist[v] = dist[u] + weight;                parent[v] = u;            }        }    }    // Check for negative-weight cycles    intC = -1;    for(inti = 0; i < E; i++)     {        intu = graph.edge[i].src;        intv = graph.edge[i].dest;        intweight = graph.edge[i].weight;        if(dist[u] != 1000000&&             dist[u] + weight < dist[v])        {                        // Store one of the vertex of            // the negative weight cycle            C = v;            break;        }    }    if(C != -1)    {        for(inti = 0; i < V; i++)            C = parent[C];        // To store the cycle vertex        ArrayList<Integer> cycle = newArrayList<>();        for(intv = C;; v = parent[v])        {            cycle.add(v);                        if(v == C && cycle.size() > 1)                break;        }        // Reverse cycle[]        Collections.reverse(cycle);        // Printing the negative cycle        for(intv : cycle)            System.out.print(v + " ");                    System.out.println();    }     else        System.out.println(-1);}// Driver Codepublicstaticvoidmain(String[] args){        // Number of vertices in graph    intV = 5;    // Number of edges in graph    intE = 5;    Graph graph = createGraph(V, E);    // Given Graph    graph.edge[0].src = 0;    graph.edge[0].dest = 1;    graph.edge[0].weight = 1;    graph.edge[1].src = 1;    graph.edge[1].dest = 2;    graph.edge[1].weight = 2;    graph.edge[2].src = 2;    graph.edge[2].dest = 3;    graph.edge[2].weight = 3;    graph.edge[3].src = 3;    graph.edge[3].dest = 4;    graph.edge[3].weight = -3;    graph.edge[4].src = 4;    graph.edge[4].dest = 1;    graph.edge[4].weight = -3;    // Function Call    NegCycleBellmanFord(graph, 0);}}// This code is contributed by sanjeev2552 | 
Python3
| # Python3 program for the above approach # Structure to represent a weighted# edge in graphclassEdge:       def__init__(self):        self.src =0        self.dest =0        self.weight =0# Structure to represent a directed# and weighted graphclassGraph:    def__init__(self):                # V. Number of vertices, E.        # Number of edges        self.V =0        self.E =0                # Graph is represented as        # an array of edges.        self.edge =[]     # Creates a new graph with V vertices# and E edgesdefcreateGraph(V, E):    graph =Graph();    graph.V =V;    graph.E =E;    graph.edge =[Edge() fori inrange(graph.E)]    returngraph;  # Function runs Bellman-Ford algorithm# and prints negative cycle(if present)defNegCycleBellmanFord(graph, src):    V =graph.V;    E =graph.E;    dist =[1000000fori inrange(V)]    parent =[-1fori inrange(V)]    dist[src] =0;     # Relax all edges |V| - 1 times.    fori inrange(1, V):        forj inrange(E):                u =graph.edge[j].src;            v =graph.edge[j].dest;            weight =graph.edge[j].weight;             if(dist[u] !=1000000and                dist[u] +weight < dist[v]):                            dist[v] =dist[u] +weight;                parent[v] =u;     # Check for negative-weight cycles    C =-1;        fori inrange(E):           u =graph.edge[i].src;        v =graph.edge[i].dest;        weight =graph.edge[i].weight;         if(dist[u] !=1000000and            dist[u] +weight < dist[v]):                         # Store one of the vertex of            # the negative weight cycle            C =v;            break;             if(C !=-1):               fori inrange(V):                   C =parent[C];         # To store the cycle vertex        cycle =[]               v =C                while(True):            cycle.append(v)            if(v ==C andlen(cycle) > 1):                break;            v =parent[v]         # Reverse cycle[]        cycle.reverse()         # Printing the negative cycle        forv incycle:                   print(v, end =" ");                     print()       else:        print(-1); # Driver Codeif__name__=='__main__':         # Number of vertices in graph    V =5;     # Number of edges in graph    E =5;     graph =createGraph(V, E);     # Given Graph    graph.edge[0].src =0;    graph.edge[0].dest =1;    graph.edge[0].weight =1;     graph.edge[1].src =1;    graph.edge[1].dest =2;    graph.edge[1].weight =2;     graph.edge[2].src =2;    graph.edge[2].dest =3;    graph.edge[2].weight =3;     graph.edge[3].src =3;    graph.edge[3].dest =4;    graph.edge[3].weight =-3;     graph.edge[4].src =4;    graph.edge[4].dest =1;    graph.edge[4].weight =-3;     # Function Call    NegCycleBellmanFord(graph, 0);# This code is contributed by Pratham76 | 
C#
| // C# program for the above approachusingSystem;usingSystem.Collections;usingSystem.Collections.Generic;classGFG {    // Structure to represent a weighted    // edge in graph    classEdge {        publicintsrc, dest, weight;    }    // Structure to represent a directed    // and weighted graph    classGraph {        // V. Number of vertices, E. Number of edges        publicintV, E;        // graph is represented as an array of edges.        publicEdge[] edge;    }    // Creates a new graph with V vertices    // and E edges    staticGraph createGraph(intV, intE)    {        Graph graph = newGraph();        graph.V = V;        graph.E = E;        graph.edge = newEdge[graph.E];        for(inti = 0; i < graph.E; i++) {            graph.edge[i] = newEdge();        }        returngraph;    }    // Function runs Bellman-Ford algorithm    // and prints negative cycle(if present)    staticvoidNegCycleBellmanFord(Graph graph, intsrc)    {        intV = graph.V;        intE = graph.E;        int[] dist = newint[V];        int[] parent = newint[V];        // Initialize distances from src        // to all other vertices as INFINITE        // and all parent as -1        for(inti = 0; i < V; i++) {            dist[i] = 1000000;            parent[i] = -1;        }        dist[src] = 0;        // Relax all edges |V| - 1 times.        for(inti = 1; i <= V - 1; i++) {            for(intj = 0; j < E; j++) {                intu = graph.edge[j].src;                intv = graph.edge[j].dest;                intweight = graph.edge[j].weight;                if(dist[u] != 1000000                    && dist[u] + weight < dist[v]) {                    dist[v] = dist[u] + weight;                    parent[v] = u;                }            }        }        // Check for negative-weight cycles        intC = -1;        for(inti = 0; i < E; i++) {            intu = graph.edge[i].src;            intv = graph.edge[i].dest;            intweight = graph.edge[i].weight;            if(dist[u] != 1000000                && dist[u] + weight < dist[v]) {                // Store one of the vertex of                // the negative weight cycle                C = v;                break;            }        }        if(C != -1) {            for(inti = 0; i < V; i++)                C = parent[C];            // To store the cycle vertex            ArrayList cycle = newArrayList();            for(intv = C;; v = parent[v]) {                cycle.Add(v);                if(v == C && cycle.Count > 1)                    break;            }            // Reverse cycle[]            cycle.Reverse();            // Printing the negative cycle            foreach(intv incycle) Console.Write(v + " ");            Console.WriteLine();        }        else            Console.WriteLine(-1);    }    // Driver Code    publicstaticvoidMain(string[] args)    {        // Number of vertices in graph        intV = 5;        // Number of edges in graph        intE = 5;        Graph graph = createGraph(V, E);        // Given Graph        graph.edge[0].src = 0;        graph.edge[0].dest = 1;        graph.edge[0].weight = 1;        graph.edge[1].src = 1;        graph.edge[1].dest = 2;        graph.edge[1].weight = 2;        graph.edge[2].src = 2;        graph.edge[2].dest = 3;        graph.edge[2].weight = 3;        graph.edge[3].src = 3;        graph.edge[3].dest = 4;        graph.edge[3].weight = -3;        graph.edge[4].src = 4;        graph.edge[4].dest = 1;        graph.edge[4].weight = -3;        // Function Call        NegCycleBellmanFord(graph, 0);    }}// This code is contributed by rutvik_56 | 
Javascript
| <script>// JavaScript program for the above approach// Structure to represent a weighted// edge in graphclass Edge {    constructor()    {        this.src = 0;        this.dest = 0;        this.weight = 0;    }}// Structure to represent a directed// and weighted graphclass Graph {        constructor()    {        // V. Number of vertices, E. Number of edges        this.V = 0;        this.E = 0;        // graph is represented as an array of edges.        this.edge = [];    }}// Creates a new graph with V vertices// and E edgesfunctioncreateGraph(V, E){    vargraph = newGraph();    graph.V = V;    graph.E = E;    graph.edge = Array(graph.E);    for(vari = 0; i < graph.E; i++) {        graph.edge[i] = newEdge();    }    returngraph;}// Function runs Bellman-Ford algorithm// and prints negative cycle(if present)functionNegCycleBellmanFord(graph, src){    varV = graph.V;    varE = graph.E;    vardist = Array(V).fill(0);;    varparent = Array(V).fill(0);;    // Initialize distances from src    // to all other vertices as INFINITE    // and all parent as -1    for(vari = 0; i < V; i++) {        dist[i] = 1000000;        parent[i] = -1;    }    dist[src] = 0;    // Relax all edges |V| - 1 times.    for(vari = 1; i <= V - 1; i++) {        for(varj = 0; j < E; j++) {            varu = graph.edge[j].src;            varv = graph.edge[j].dest;            varweight = graph.edge[j].weight;            if(dist[u] != 1000000                && dist[u] + weight < dist[v]) {                dist[v] = dist[u] + weight;                parent[v] = u;            }        }    }    // Check for negative-weight cycles    varC = -1;    for(vari = 0; i < E; i++) {        varu = graph.edge[i].src;        varv = graph.edge[i].dest;        varweight = graph.edge[i].weight;        if(dist[u] != 1000000            && dist[u] + weight < dist[v]) {            // Store one of the vertex of            // the negative weight cycle            C = v;            break;        }    }    if(C != -1) {        for(vari = 0; i < V; i++)            C = parent[C];        // To store the cycle vertex        varcycle = [];        for(varv = C;; v = parent[v]) {            cycle.push(v);            if(v == C && cycle.length > 1)                break;        }        // Reverse cycle[]        cycle.reverse();        // Printing the negative cycle        for(varv of cycle) document.write(v + " ");        document.write("<br>");    }    else        document.write(-1 + "<br>");}// Driver Code// Number of vertices in graphvarV = 5;// Number of edges in graphvarE = 5;vargraph = createGraph(V, E);// Given Graphgraph.edge[0].src = 0;graph.edge[0].dest = 1;graph.edge[0].weight = 1;graph.edge[1].src = 1;graph.edge[1].dest = 2;graph.edge[1].weight = 2;graph.edge[2].src = 2;graph.edge[2].dest = 3;graph.edge[2].weight = 3;graph.edge[3].src = 3;graph.edge[3].dest = 4;graph.edge[3].weight = -3;graph.edge[4].src = 4;graph.edge[4].dest = 1;graph.edge[4].weight = -3;// Function CallNegCycleBellmanFord(graph, 0);</script> | 
1 2 3 4 1
Time Complexity: O(V*E) 
Auxiliary Space: O(V)
 
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

 
                                    








