Tuesday, November 19, 2024
Google search engine
HomeData Modelling & AIMinimum cost to connect weighted nodes represented as array

Minimum cost to connect weighted nodes represented as array

Given an array of N elements(nodes), where every element is weight of that node.Connecting two nodes will take a cost of product of their weights.You have to connect every node with every other node(directly or indirectly).Output the minimum cost required.
Examples: 
 

Input : a[] = {6, 2, 1, 5}
Output :  13
Explanation : 
Here, we connect the nodes as follows:
connect a[0] and a[2], cost = 6*1 = 6,
connect a[2] and a[1], cost = 1*2 = 2,
connect a[2] and a[3], cost = 1*5 = 5.
every node is reachable from every other node:
Total cost = 6+2+5 = 13.

Input  : a[] = {5, 10}
Output : 50
Explanation : connections:
connect a[0] and a[1], cost = 5*10 = 50,
Minimum cost = 50. 

 

We need to make some observations that we have to make a connected graph with N-1 edges. As the output will be sum of products of two numbers, we have to minimize the product of every term in that sum equation. How can we do it? Clearly, choose the minimum element in the array and connect it with every other. In this way we can reach every other node from a particular node. 
Let, minimum element = a[i], (let it’s index be 0) 
Minimum Cost 
So, answer is product of minimum element and sum of all the elements except minimum element. 
 

C++




// cpp code for Minimum Cost Required to connect weighted nodes
#include <bits/stdc++.h>
using namespace std;
int minimum_cost(int a[], int n)
{
    int mn = INT_MAX;
    int sum = 0;
    for (int i = 0; i < n; i++) {
 
        // To find the minimum element
        mn = min(a[i], mn);
 
        // sum of all the elements
        sum += a[i];
    }
 
    return mn * (sum - mn);
}
 
// Driver code
int main()
{
    int a[] = { 4, 3, 2, 5 };
    int n = sizeof(a) / sizeof(a[0]);
    cout << minimum_cost(a, n) << endl;
    return 0;
}


Java




// Java code for Minimum Cost Required to
// connect weighted nodes
 
import java.io.*;
 
class GFG {
     
    static int minimum_cost(int a[], int n)
    {
         
        int mn = Integer.MAX_VALUE;
        int sum = 0;
         
        for (int i = 0; i < n; i++) {
 
            // To find the minimum element
            mn = Math.min(a[i], mn);
 
            // sum of all the elements
            sum += a[i];
        }
 
        return mn * (sum - mn);
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int a[] = { 4, 3, 2, 5 };
        int n = a.length;
         
        System.out.println(minimum_cost(a, n));
    }
}
 
// This code is contributed by vt_m.


Python 3




# Python 3 code for Minimum Cost
# Required to connect weighted nodes
import sys
 
def minimum_cost(a, n):
 
    mn = sys.maxsize
    sum = 0
    for i in range(n):
 
        # To find the minimum element
        mn = min(a[i], mn)
 
        # sum of all the elements
        sum += a[i]
 
    return mn * (sum - mn)
 
# Driver code
if __name__ == "__main__":
     
    a = [ 4, 3, 2, 5 ]
    n = len(a)
    print(minimum_cost(a, n))
 
# This code is contributed
# by ChitraNayal


C#




// C# code for Minimum Cost Required
// to connect weighted nodes
using System;
 
class GFG {
     
    // Function to calculate minimum cost
    static int minimum_cost(int []a, int n)
    {
         
        int mn = int.MaxValue;
        int sum = 0;
         
        for (int i = 0; i < n; i++)
        {
 
            // To find the minimum element
            mn = Math.Min(a[i], mn);
 
            // sum of all the elements
            sum += a[i];
        }
 
        return mn * (sum - mn);
    }
 
    // Driver code
    public static void Main()
    {
        int []a = {4, 3, 2, 5};
        int n = a.Length;
         
        Console.WriteLine(minimum_cost(a, n));
    }
}
 
// This code is contributed by vt_m.


PHP




<?php
// PHP code for Minimum Cost Required
// to connect weighted nodes
function minimum_cost($a, $n)
{
    $mn = PHP_INT_MAX;
    $sum = 0;
    for ($i = 0; $i < $n; $i++)
    {
 
        // To find the minimum element
        $mn = min($a[$i], $mn);
 
        // sum of all the elements
        $sum += $a[$i];
    }
 
    return $mn * ($sum - $mn);
}
 
// Driver code
$a = array( 4, 3, 2, 5 );
$n = sizeof($a);
echo minimum_cost($a, $n), "\n";
 
// This code is contributed
// by Kiit_Tush
?>


Javascript




<script>
    // Javascript code for Minimum Cost Required
    // to connect weighted nodes
     
    // Function to calculate minimum cost
    function minimum_cost(a, n)
    {
           
        let mn = Number.MAX_VALUE;
        let sum = 0;
           
        for (let i = 0; i < n; i++)
        {
   
            // To find the minimum element
            mn = Math.min(a[i], mn);
   
            // sum of all the elements
            sum += a[i];
        }
   
        return mn * (sum - mn);
    }
     
    let a = [4, 3, 2, 5];
    let n = a.length;
 
    document.write(minimum_cost(a, n));
     
</script>


Output: 
 

  24

Time complexity: O(n).
This article is contributed by Harsha Mogali. If you like neveropen and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the neveropen main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
 

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments