# Reverse Delete Algorithm for Minimum Spanning Tree

Reverse Delete algorithm is closely related to Kruskal’s algorithm. In Kruskal’s algorithm what we do is : Sort edges by increasing order of their weights. After sorting, we one by one pick edges in increasing order. We include current picked edge if by including this in spanning tree not form any cycle until there are V-1 edges in spanning tree, where V = number of vertices.

In Reverse Delete algorithm, we sort all edges in decreasing order of their weights. After sorting, we one by one pick edges in decreasing order. We include current picked edge if excluding current edge causes disconnection in current graph. The main idea is delete edge if its deletion does not lead to disconnection of graph.

The Algorithm

```1) Sort all edges of graph in non-increasing order of
edge weights.

2) Initialize MST as original graph and remove extra
edges using step 3.

3) Pick highest weight edge from remaining edges and
check if deleting the edge disconnects the graph
or not.
If disconnects, then we don't delete the edge.
Else we delete the edge and continue. ```

Illustration:
Let us understand with the following example:

If we delete highest weight edge of weight 14, graph doesn’t become disconnected, so we remove it.

Next we delete 11 as deleting it doesn’t disconnect the graph.

Next we delete 10 as deleting it doesn’t disconnect the graph.

Next is 9. We cannot delete 9 as deleting it causes disconnection.

We continue this way and following edges remain in final MST.

```Edges in MST
(3, 4)
(0, 7)
(2, 3)
(2, 5)
(0, 1)
(5, 6)
(2, 8)
(6, 7) ```

Note :
In case of same weight edges, we can pick any edge of the same weight edges.

## Recommended: Please solve it on “PRACTICE” first, before moving on to the solution.

Below is C++ implementation of above steps.

```// C++ program to find Minimum Spanning Tree
// of a graph using Reverse Delete Algorithm
#include<bits/stdc++.h>
using namespace std;

// Creating shortcut for an integer pair
typedef  pair<int, int> iPair;

// Graph class represents a directed graph
class Graph
{
int V;    // No. of vertices
vector< pair<int, iPair> > edges;
void DFS(int v, bool visited[]);

public:
Graph(int V);   // Constructor

// function to add an edge to graph
void addEdge(int u, int v, int w);

// Returns true if graph is connected
bool isConnected();

void reverseDeleteMST();
};

Graph::Graph(int V)
{
this->V = V;
}

void Graph::addEdge(int u, int v, int w)
{
edges.push_back({w, {u, v}});
}

void Graph::DFS(int v, bool visited[])
{
// Mark the current node as visited and print it
visited[v] = true;

// Recur for all the vertices adjacent to
// this vertex
list<int>::iterator i;
if (!visited[*i])
DFS(*i, visited);
}

// Returns true if given graph is connected, else false
bool Graph::isConnected()
{
bool visited[V];
memset(visited, false, sizeof(visited));

// Find all reachable vertices from first vertex
DFS(0, visited);

// If set of reachable vertices includes all,
// return true.
for (int i=1; i<V; i++)
if (visited[i] == false)
return false;

return true;
}

// This function assumes that edge (u, v)
// exists in graph or not,
void Graph::reverseDeleteMST()
{
// Sort edges in increasing order on basis of cost
sort(edges.begin(), edges.end());

int mst_wt = 0;  // Initialize weight of MST

cout << "Edges in MST\n";

// Iterate through all sorted edges in
// decreasing order of weights
for (int i=edges.size()-1; i>=0; i--)
{
int u = edges[i].second.first;
int v = edges[i].second.second;

// Remove edge from undirected graph

// Adding the edge back if removing it
// causes disconnection. In this case this
// edge becomes part of MST.
if (isConnected() == false)
{

// This edge is part of MST
cout << "(" << u << ", " << v << ") \n";
mst_wt += edges[i].first;
}
}

cout << "Total weight of MST is " << mst_wt;
}

// Driver code
int main()
{
// create the graph given in above fugure
int V = 9;
Graph g(V);

//  making above shown graph

g.reverseDeleteMST();
return 0;
}
```

Output :

```Edges in MST
(3, 4)
(0, 7)
(2, 3)
(2, 5)
(0, 1)
(5, 6)
(2, 8)
(6, 7)
Total weight of MST is 37```

Notes :

1. The above implementation is a simple/naive implementation of Reverse Delete algorithm and can be optimized to O(E log V (log log V)3) [Source : Wiki]. But this optimized time complexity is still less than Prim and Kruskal Algorithms for MST.
2. The above implementation modifies the original graph. We can create a copy of the graph if original graph must be retained.

This article is contributed by Antra Purohit. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

# GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.
3.6 Average Difficulty : 3.6/5.0
Based on 5 vote(s)