Find the Minimum Spanning Tree with Alternating Colored Edges

The code executes a calculation to discover the least crossing tree by substituting coloured edges. It employs an energetic programming approach to calculate the least expensive toll. The calculation considers all conceivable edges and colours and recursively assesses the cost of counting or barring each edge based on whether it keeps up the substituting colour design. It keeps track of the least severe toll experienced so far by employing the memoization method. The calculation develops the least crossing tree by eagerly selecting edges with the least toll, guaranteeing that adjoining edges have distinctive colours. At last, it returns the least−fetched of the built−in traversing trees.

Methods Used

  • Minimum Cost Spanning Tree (MCST) approach.

Minimum Cost Spanning Tree

The minimum cost−spanning tree could be a chart calculation that points to the tree that interfaces all vertices of a chart with the least amount of toll. It iteratively chooses the lowest−cost edge that does not make a cycle until all vertices are associated. The calculation keeps up a set of gone−by vertices and keeps track of the least−fetched to reach each vertex. It eagerly chooses the edge with the most reduced fetch at each step, extending the tree until all vertices are included. The coming about tree has taken the least toll among all possible traversing trees on the chart.


  • Begin with a purge tree T and a set of gone−by vertices V.

  • Initialise the least fetched for each vertex to boundlessness, except for the beginning vertex, which is set to 0.

  • While V does not incorporate all vertices,

    • Select the vertex u with the least fetch among the vertices not in V.

    • Include U to V.

    • For each neighbouring vertex v of u:

  • If v is not in V and the taken toll of the edge (u, v) is decrease than the present day least taken toll for v,

  • Update the least taken toll for v with the taken toll of the edge (u, v).

  • Add the edges (u, v) to T.

  • Return the tree T, which speaks to the least far−fetched crossing tree.

  • This calculation iteratively chooses the vertex with the least toll among the unvisited vertices and grows the tree by including the lowest−cost edges associated with that vertex. It proceeds with this preparation until all vertices are included within the tree. The coming−about tree will have the least amount to fetch among all conceivable crossing trees on the chart.


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

long long calculateMinCost(int numVertices, int mask, int prevVertex, int prevColor, const std::vector<std::vector<std::pair<int, char>>>& graph, std::vector<std::vector<std::vector<long long>>>& dp) {
    if (mask == ((1 << numVertices) - 1)) {
        return 0;

    if (dp[mask][prevVertex][prevColor] != -1) {
        return dp[mask][prevVertex][prevColor];

    long long ans = LLONG_MAX;

    for (int i = 0; i < numVertices; i++) {
        for (const auto& edge : graph[prevVertex]) {
            int nextVertex = edge.first;
            char nextColor = edge.second;

            if (!(mask & (1 << nextVertex)) && (nextColor != prevColor)) {
                long long z = calculateMinCost(numVertices, mask | (1 << nextVertex), nextVertex, nextColor, graph, dp);
                if (z != LLONG_MAX) {
                    ans = std::min(ans, z);

    return dp[mask][prevVertex][prevColor] = ans;

void makeGraph(const std::vector<std::pair<std::pair<int, int>, std::pair<int, char>>>& edges, int numVertices, std::vector<std::vector<std::pair<int, char>>>& graph) {
    for (const auto& edge : edges) {
        int u = edge.first.first - 1;
        int v = edge.first.second - 1;
        int cost = edge.second.first;
        char color = edge.second.second;
        graph[u].emplace_back(v, color);
        graph[v].emplace_back(u, color);

int getMinimumCost(int numVertices, const std::vector<std::vector<std::pair<int, char>>>& graph) {
    std::vector<std::vector<std::vector<long long>>> dp(1 << numVertices, std::vector<std::vector<long long>>(numVertices, std::vector<long long>(3, -1)));

    long long minCostValue = LLONG_MAX;

    for (int i = 0; i < numVertices; i++) {
        minCostValue = std::min(minCostValue, calculateMinCost(numVertices, 1 << i, i, 'B', graph, dp));
        minCostValue = std::min(minCostValue, calculateMinCost(numVertices, 1 << i, i, 'W', graph, dp));

    if (minCostValue != LLONG_MAX) {
        return static_cast<int>(minCostValue);
    } else {
        return -1;

int main() {
    int numVertices = 3;
    std::vector<std::pair<std::pair<int, int>, std::pair<int, char>>> edges = {
        { { 1, 2 }, { 2, 'B' } },
        { { 1, 2 }, { 3, 'W' } },
        { { 2, 3 }, { 4, 'W' } },
        { { 2, 3 }, { 5, 'B' } }

    std::vector<std::vector<std::pair<int, char>>> graph(numVertices);

    makeGraph(edges, numVertices, graph);
    std::cout << getMinimumCost(numVertices, graph) << '\n';

    return 0;




This article presents an algorithmic approach to finding the least crossing tree (MST) by substituting coloured edges in a given chart. The issue includes selecting edges in such a way that adjoining edges have diverse colours and the entire fetch of the chosen edges is minimised. The calculation utilises energetic programming to calculate the least fetch for each conceivable combination of vertices and colours, maintaining a strategic distance from excess computations with memoization.

The code execution illustrates the calculation and gives an arrangement to a test chart with coloured edges. The article too clarifies the concept of the least toll traversing tree (MCST) and its centrality in the chart hypothesis. By and large, the article serves as a comprehensive guide for understanding and executing the calculation to discover the least traversing tree with rotating coloured edges.

Updated on: 14-Jul-2023


Kickstart Your Career

Get certified by completing the course

Get Started