In graph theory, the reverse-delete Algorithm is a method for creating a minimum spanning tree from a connected, edge-weighted graph. When compared to Kruskal's Algorithm, which also appears in the same work, it initially appeared in Kruskal. This approach will locate a minimum spanning tree for each disconnected portion of the graph if it is disconnected. Every vertex in the graph is contained within the collection of these minimal-spanning trees, known as a minimum-spanning forest.
Reverse Delete Algorithm
By weight, order the edges
Set up MST with all edges.
The highest weight edge should be removed.
Restore the edge if removing it causes the graph to become disconnected.
Otherwise, keep going
Example
The values close to the edge show their edge weight.
This graph's minimum spanning tree is:
Let's use the graph to perform the Reverse Delete Algorithm:
We begin with Edges 3 to 4, which have the highest weight.
Since removing edges 3 and 4 does not cause the graph to become disconnected, the edge may be deleted.
Pick the edge 5-6 with weight 11 next. Since removing edges 5 and 6 does not cause the graph to become disconnected, the edge may be deleted.
Pick the 1-3 edge with weight 9. Since removing edges 1-3 does not cause the graph to become disconnected, the edge may be deleted.
Choose the edge 4-6 with weight nine next. The graph will become disconnected if this edge is removed because Node 6 will be split apart. We, therefore, do not remove the edge.
Choose edge 2 with weight 8 next. Since removing edges 1-2 does not cause the graph to become disconnected, the edge may be deleted.
Code
Now let’s see the code in different languages like Java, C++, and Python.
Java Code
import java.util.arrlist;
class Edge
{
public int w;
public int u;
public int v;
public Edge n;
public Edge(int w, int u, int v)
{
this.w = w;
this.u = u;
this.v = v;
this.n = null;
}
}
public class grp
{
public int ver;
public arrlist < arrlist < Integer >> adgeList;
public Edge edge;
public int edgecounter;
public grp(int ver)
{
this.ver = ver;
this.adgeList = new arrlist < arrlist < Integer >> (ver);
this.edge = null;
this.edgecounter = 0;
int i;
for (i = 0; i < this.ver; ++i)
{
this.adgeList.add(new arrlist < Integer > ());
}
}
public boolean connected()
{
boolean[] reached = new boolean[this.ver];
// Set the initial reached ver
int i;
for (i = 0; i < this.ver; ++i)
{
reached[i] = false;
}
this.DFS(0, reached);
for (i = 1; i < this.ver; i++)
{
if (reached[i] == false)
{
return false;
}
}
return true;
}
public void add_edge_1(int u, int v, int w)
{
if (u < 0 || u >= this.ver || v < 0 || v >= this.ver)
{
return;
}
adgeList.get(u).add(v);
adgeList.get(v).add(u);
Edge e = new Edge(w, u, v);
if (this.edge == null)
{
this.edge = e;
}
else if (this.edge.w <= e.w)
{
e.n = this.edge;
this.edge = e;
}
else
{
Edge temp = this.edge;
while (temp.n != null && temp.n.w > e.w)
{
temp = temp.n;
}
e.n = temp.n;
temp.n = e;
}
this.edgecounter = this.edgecounter + 1;
}
public void printgrp()
{
int i;
System.out.print("\n grp Adjacency List ");
for (i = 0; i < this.ver; ++i)
{
System.out.print(" \n [" + i + "] :");
for (int j = 0; j < this.adgeList1.get(i).size(); j++)
{
System.out.print(" " + this.adgeList1.get(i).get(j));
}
}
}
public void DFS_1(int v, boolean[] reached)
{
reached[v] = true;
for (int i = 0; i < this.adgeList1.get(v).size(); i++)
{
if (reached[this.adgeList1.get(v).get(i)] == false)
{
DFS_1(this.adgeList1.get(v).get(i), reached);
}
}
}
public void rev_dlt_MST()
{
int result = 0;
Edge pt = this.edge;
System.out.println("\n\n nodes are nonnected by edge in mst");
while (pt != null)
{
adgeList.get(pt.u).remove(new Integer(pt.v));
adgeList.get(pt.v).remove(new Integer(pt.u));
if (connected() == false)
{
adgeList.get(pt.u).add(pt.v);
adgeList.get(pt.v).add(pt.u);
result += pt.weight;
System.out.print(" (" + pt.u + ", " + pt.v + ") \n");
}
pt = pt.n;
}
System.out.println("Calculated total weight of MST " + result);
}
public static void main(String[] args)
{
grp g = new grp(8);
g.addEdge(0, 1, 5);
g.addEdge(0, 3, 3);
g.addEdge(1, 2, 3);
g.addEdge(1, 6, 7);
g.addEdge(1, 7, 9);
g.addEdge(2, 5, 9);
g.addEdge(2, 7, 4);
g.addEdge(3, 4, 11);
g.addEdge(3, 7, 8);
g.addEdge(4, 5, 8);
g.addEdge(4, 6, 14);
g.addEdge(4, 7, 10);
g.addEdge(5, 6, 11);
g.printgrp();
g.rev_dlt_MST();
}
}
You can also try this code with Online Java Compiler
Using big-O notation, the time complexity is O(E logV (log logV)3), where E is the number of edges present and V is the number of vertices.
Space Complexity: O(V+E)
Frequently asked questions
How does Kruskal's Algorithm work?
The Kruskal algorithm arranges all the edges according to rising edge weights and only keeps adding nodes to the tree if the selected edge does not constitute a cycle.
What is the complexity of the Kruskal algorithm?
The time complexity of the Kruskal algorithm is O(E log V), where V is the number of vertices.
Is the Kruskal algorithm greedy?
In the graph theory, it is known as a greedy method since it continuously adds the next lowest-weight edge that won't cycle to the minimal spanning forest.
Why do we use the Prim Algorithm?
The minimal spanning tree from a graph is found using Prim's Approach, a greedy algorithm.
Why Prims and Kruskal Cannot be applied on directed graphs?
All vertices are presumed to be connected in Prim's Algorithm. However, every node in a directed graph cannot be reached from every other node. Due to this, Prim's Algorithm is flawed.
Conclusion
To conclude this blog, here we discussed the Reverse Delete Algorithm, examples, and code. We also Algorithm. In the end, we saw the time complexities.