Product of Minimum Edge Weight Between all Pairs of a Tree


The product of the least edge weight between all sets of a tree is gotten by finding the least weight edge for each conceivable match of vertices within the tree and, after that, increasing all these least weights together. This esteem speaks to the smallest conceivable toll or weight required to navigate the tree from any vertex to any other vertex. By considering the least weight of each edge, we guarantee that we find the most proficient way between any two vertices within the tree. The item of these least edge weights gives a compact degree of the general network and proficiency of the tree structure.

Methods Used

  • DFS

  • Prim’s algorithm

DFS

The product of the least edge weight between all sets of a tree can be found employing a Depth−first−look (DFS) approach. Beginning from any hub, we navigate the tree in a depth−first way, investigating each department some time ago and recently backtracking. Amid the traversal, we keep up a running item of the least edge weights experienced so far. At whatever point we experience an unused edge, we compare its weight with the current least and overhaul the item in the event that it becomes essential. By visiting all hubs within the tree, we guarantee that we consider all sets of hubs and calculate the ultimate item of the least edge weights proficiently utilising DFS.

Algorithm

  • Begin the DFS traversal from any hub within the tree.

  • Initialise a variable 'product' to 1, speaking to the item with the fewest edge weights experienced so far.

  • While navigating the tree in a depth−first way, keep track of the least edge weight seen on the way from the current hub to its precursor. Upgrade the product' in like manner on the off chance that the unused edge weight is less than the current minimum.

  • Once the DFS traversal is total, 'product' will hold the item with the least edge weights between all pairs of hubs within the tree.

  • Return the 'product' as the ultimate result.

Example

#include <iostream>
#include <vector>
#include <climits>

using namespace std;

// Structure to represent a node in the tree
struct Node {
    int value;
    vector<Node*> children;
};

// Helper function for DFS traversal
void dfs(Node* node, int& product, int minWeight) {
    // Update the product if the minimum weight is smaller
    if (node->value < minWeight) {
        product *= node->value;
        minWeight = node->value;
    }

    // Recursively explore all children nodes
    for (Node* child : node->children) {
        dfs(child, product, minWeight);
    }
}

// Function to calculate the product of minimum edge weights between all pairs of nodes
int calculateProduct(Node* root) {
    int product = 1;
    int minWeight = INT_MAX;

    dfs(root, product, minWeight);

    return product;
}

int main() {
    // Create a sample tree
    Node* root = new Node({4, {}});
    Node* node1 = new Node({2, {}});
    Node* node2 = new Node({6, {}});
    Node* node3 = new Node({3, {}});
    Node* node4 = new Node({5, {}});
    Node* node5 = new Node({1, {}});
    Node* node6 = new Node({7, {}});

    root->children = {node1, node2};
    node1->children = {node3, node4};
    node2->children = {node5, node6};

    // Calculate the product of minimum edge weights
    int result = calculateProduct(root);

    // Print the result
    cout << "Product of minimum edge weights: " << result << endl;

    // Deallocate memory
    delete root;
    delete node1;
    delete node2;
    delete node3;
    delete node4;
    delete node5;
    delete node6;

    return 0;
}

Output

Product of minimum edge weights: 8

Prim’s Algorithm

Prim's algorithm finds the item of the least edge weight between all sets of a tree by beginning with a subjective vertex and over and over including the least weight edge that interfaces a vertex within the tree to a vertex exterior of it. This handler proceeds until all vertices are included within the tree. At each step, the calculation chooses the edge with the least weight that connects the tree to a vertex not within the tree, ensuring that the tree remains associated and the edge weight is minimised. The ultimate item is the increase in weight of all chosen edges.

Algorithm

  • Make a chart representation with vertices and edges and initialise a purge set to store the coming tree.

  • Choose a beginning vertex subjectively and include it in the set of the coming−about tree.

  • While the coming tree does not incorporate all vertices,

  • Find the least−weight edge that interfaces a vertex within the coming−about tree to a vertex exterior to it.

    Add the vertex exterior of the tree to the coming about tree set and include the chosen edge to the tree.

    Compute the item of all edge weights within the coming tree.

Example

#include <iostream>
#include <vector>
#include <climits>

using namespace std;

typedef pair<int, int> pii;
typedef vector<vector<pii>> Graph;

void addEdge(Graph& graph, int src, int dest, int weight) {
    graph[src].push_back(make_pair(dest, weight));
    graph[dest].push_back(make_pair(src, weight));
}

int primMST(Graph& graph) {
    int numVertices = graph.size();

    vector<bool> visited(numVertices, false);
    vector<int> minWeight(numVertices, INT_MAX);

    minWeight[0] = 0; // Start from vertex 0

    int product = 1;

    for (int count = 0; count < numVertices - 1; ++count) {
        int minIndex = -1;
        int minWeightValue = INT_MAX;

        // Find the vertex with the minimum weight
        for (int v = 0; v < numVertices; ++v) {
            if (!visited[v] && minWeight[v] < minWeightValue) {
                minWeightValue = minWeight[v];
                minIndex = v;
            }
        }

        visited[minIndex] = true;

        // Update the minimum weights of adjacent vertices
        for (const auto& neighbor : graph[minIndex]) {
            int v = neighbor.first;
            int weight = neighbor.second;

            if (!visited[v] && weight < minWeight[v]) {
                minWeight[v] = weight;
            }
        }

        product *= minWeightValue;
    }

    return product;
}

int main() {
    int numVertices = 5;
    int numEdges = 7;

    Graph graph(numVertices);

    // Add edges to the graph
    auto addEdge = [&](int src, int dest, int weight) {
        graph[src].push_back({dest, weight});
        graph[dest].push_back({src, weight});
    };

    addEdge(0, 1, 2);
    addEdge(0, 2, 3);
    addEdge(1, 3, 4);
    addEdge(1, 4, 5);
    addEdge(2, 3, 1);
    addEdge(2, 4, 3);
    addEdge(3, 4, 6);

    int minProduct = primMST(graph);

    cout << "Product of minimum edge weights: " << minProduct << endl;

    return 0;
}

Output

Product of minimum edge weights: 0

Conclusion

This article gives a clarification of how to calculate the item with the least edge weight between all sets of a tree. It examines two approaches: DFS (Depth−First Look) and Prim's calculation. The DFS approach includes navigating the tree in a depth−first way, keeping track of the least edge weight experienced, and overhauling the item appropriately. On the other hand, Prim's calculation begins with a chosen vertex and iteratively includes the least weight edge that interfaces the tree to a vertex exterior to it, eventually yielding the item of all chosen edge weights. Both strategies are depicted with calculations and case programmes in C.

Updated on: 14-Jul-2023

99 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements