Given a graph with N nodes and M edges where each edge has a color (either black or green) and a cost associated with it. Find the minimum spanning tree of the graph such that every path in the tree is made up of alternating colored edges.
Input: N = 3, M = 4
Input: N = 4, M = 6
- The first observation we make here is every such kind of spanning tree will be a chain. To prove it, suppose we have a tree that is not a chain and every path in it is made up of alternating edges. Then we can deduce that at least 1 node has a degree of 3. Out of these 3 edges, at least 2 will have the same color. The path using these 2 edges will never follow the conditions and Hence, such kind of tree is always a chain.
- Now we can find a chain with minimum cost and alternating edges using bitmask-dp,
dp[mask(2^n)][Node(n)][col_of_last_edge(2)] where the mask is the bitmask of nodes we’ve added to the chain. Node is the last node we added to the chain.col_of_last_edge is the color of edge use to connect Node.
- To transition from 1 state to another state we visit the adjacency list of the last node and use those edges which have color != col_of_last_edge.
Below is the implementation of the above approach:
Time Complexity: O(2^N * (M + N))
- Find the weight of the minimum spanning tree
- Minimum Product Spanning Tree
- Kruskal's Minimum Spanning Tree using STL in C++
- Applications of Minimum Spanning Tree Problem
- Boruvka's algorithm for Minimum Spanning Tree
- Minimum spanning tree cost of given Graphs
- Reverse Delete Algorithm for Minimum Spanning Tree
- Prim’s Minimum Spanning Tree (MST) | Greedy Algo-5
- Kruskal’s Minimum Spanning Tree Algorithm | Greedy Algo-2
- Minimum Spanning Tree using Priority Queue and Array List
- Querying the number of distinct colors in a subtree of a colored tree using BIT
- Spanning Tree With Maximum Degree (Using Kruskal's Algorithm)
- Levelwise Alternating OR and XOR operations in Segment Tree
- Maximum Possible Edge Disjoint Spanning Tree From a Complete Graph
- Right sibling of each node in a tree given as array of edges
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : sanjeev2552