Open In App

Minimum cost to reverse edges such that there is path between every pair of nodes

Given a connected, directional graph. Each node is connected to exactly two other nodes. There is a weight associated with each edge denoting the cost to reverse its direction. The task is to find the minimum cost to reverse some edges of the graph such that it is possible to go from each node to every other node.

Examples: 



Input: 
5
1 2 7
5 1 8
5 4 5
3 4 1
3 2 10
Output: 15

Input:
6
1 5 4
5 3 8
2 4 15
1 6 16
2 3 23
4 6 42
Output: 39

Approach:



Below is the implementation of the above approach: 




// C++ code to find
// the minimum cost to
// reverse the edges
#include <bits/stdc++.h>
using namespace std;
 
// Function to calculate
// min cost for reversing
// the edges
int minCost(vector<vector<int> >& graph, int n)
{
 
    int cost1 = 0, cost2 = 0;
    // bool array to mark
    // start and end node
    // of a graph
    bool start[n + 1] = { false };
    bool end[n + 1] = { false };
 
    for (int i = 0; i < n; i++) {
 
        int a = graph[i][0];
        int b = graph[i][1];
        int c = graph[i][2];
 
        // This edge must
        // start from b and end at a
        if (start[a] || end[b]) {
            cost2 += c;
            start[b] = true;
            end[a] = true;
        }
 
        // This edge must
        // start from a and end at b
        else {
            cost1 += c;
            start[a] = true;
            end[b] = true;
        }
    }
 
    // Return minimum of
    // both possibilities
    return min(cost1, cost2);
}
 
// Driver code
int main()
{
    int n = 5;
    // Adjacency list representation
    // of a graph
    vector<vector<int> > graph = {
        { 1, 2, 7 },
        { 5, 1, 8 },
        { 5, 4, 5 },
        { 3, 4, 1 },
        { 3, 2, 10 }
    };
 
    int ans = minCost(graph, n);
    cout << ans << '\n';
 
    return 0;
}




// Java code to find the minimum cost to
// reverse the edges
class GFG
{
 
// Function to calculate min cost for
// reversing the edges
static int minCost(int[][] graph, int n)
{
 
    int cost1 = 0, cost2 = 0;
     
    // bool array to mark start and
    // end node of a graph
    boolean []start = new boolean[n + 1];
    boolean []end = new boolean[n + 1];
 
    for (int i = 0; i < n; i++)
    {
        int a = graph[i][0];
        int b = graph[i][1];
        int c = graph[i][2];
 
        // This edge must start from b
        // and end at a
        if (start[a] || end[b])
        {
            cost2 += c;
            start[b] = true;
            end[a] = true;
        }
 
        // This edge must start from a
        // and end at b
        else
        {
            cost1 += c;
            start[a] = true;
            end[b] = true;
        }
    }
 
    // Return minimum of both possibilities
    return Math.min(cost1, cost2);
}
 
// Driver code
public static void main(String[] args)
{
    int n = 5;
     
    // Adjacency list representation
    // of a graph
    int [][]graph = {{ 1, 2, 7 },
                     { 5, 1, 8 },
                     { 5, 4, 5 },
                     { 3, 4, 1 },
                     { 3, 2, 10 }};
 
    int ans = minCost(graph, n);
    System.out.println(ans);
}
}
 
// This code is contributed by Rajput-Ji




# Python code to find the minimum cost to
# reverse the edges
 
# Function to calculate min cost for
# reversing the edges
def minCost(graph, n):
 
    cost1, cost2 = 0, 0;
     
    # bool array to mark start and
    # end node of a graph
    start = [False]*(n + 1);
    end = [False]*(n + 1);
 
    for i in range(n):
        a = graph[i][0];
        b = graph[i][1];
        c = graph[i][2];
 
        # This edge must start from b
        # and end at a
        if (start[a] or end[b]):
            cost2 += c;
            start[b] = True;
            end[a] = True;
 
        # This edge must start from a
        # and end at b
        else:
            cost1 += c;
            start[a] = True;
            end[b] = True;
 
    # Return minimum of both possibilities
    return min(cost1, cost2);
 
# Driver code
if __name__ == '__main__':
    n = 5;
     
    # Adjacency list representation
    # of a graph
    graph = [[ 1, 2, 7 ],
                    [ 5, 1, 8 ],
                    [ 5, 4, 5 ],
                    [ 3, 4, 1 ],
                    [ 3, 2, 10 ]];
 
    ans = minCost(graph, n);
    print(ans);
     
# This code is contributed by 29AjayKumar




// C# code to find the minimum cost to
// reverse the edges
using System;
     
class GFG
{
 
// Function to calculate min cost for
// reversing the edges
static int minCost(int[,] graph, int n)
{
    int cost1 = 0, cost2 = 0;
     
    // bool array to mark start and
    // end node of a graph
    Boolean []start = new Boolean[n + 1];
    Boolean []end = new Boolean[n + 1];
 
    for (int i = 0; i < n; i++)
    {
        int a = graph[i, 0];
        int b = graph[i, 1];
        int c = graph[i, 2];
 
        // This edge must start from b
        // and end at a
        if (start[a] || end[b])
        {
            cost2 += c;
            start[b] = true;
            end[a] = true;
        }
 
        // This edge must start from a
        // and end at b
        else
        {
            cost1 += c;
            start[a] = true;
            end[b] = true;
        }
    }
 
    // Return minimum of both possibilities
    return Math.Min(cost1, cost2);
}
 
// Driver code
public static void Main(String[] args)
{
    int n = 5;
     
    // Adjacency list representation
    // of a graph
    int [,]graph = {{ 1, 2, 7 },
                    { 5, 1, 8 },
                    { 5, 4, 5 },
                    { 3, 4, 1 },
                    { 3, 2, 10 }};
 
    int ans = minCost(graph, n);
    Console.WriteLine(ans);
}
}
 
// This code is contributed by PrinciRaj1992




<script>
 
// JavaScript code to find
// the minimum cost to
// reverse the edges
 
 
// Function to calculate
// min cost for reversing
// the edges
function minCost(graph, n) {
 
    let cost1 = 0, cost2 = 0;
    // bool array to mark
    // start and end node
    // of a graph
    let start = new Array(n + 1).fill(false);
    let end = new Array(n + 1).fill(false);
 
    for (let i = 0; i < n; i++) {
 
        let a = graph[i][0];
        let b = graph[i][1];
        let c = graph[i][2];
 
        // This edge must
        // start from b and end at a
        if (start[a] || end[b]) {
            cost2 += c;
            start[b] = true;
            end[a] = true;
        }
 
        // This edge must
        // start from a and end at b
        else {
            cost1 += c;
            start[a] = true;
            end[b] = true;
        }
    }
 
    // Return minimum of
    // both possibilities
    return Math.min(cost1, cost2);
}
 
// Driver code
 
let n = 5;
// Adjacency list representation
// of a graph
let graph = [
    [1, 2, 7],
    [5, 1, 8],
    [5, 4, 5],
    [3, 4, 1],
    [3, 2, 10]
];
 
let ans = minCost(graph, n);
document.write(ans + '<br>');
 
</script>

Output: 
15

 

Time Complexity: O(n)
Auxiliary Space: O(n)


Article Tags :