Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Kruskal's algorithm find the minimum spanning tree if it's greedy?

Why does Kruskal's algorithm find the minimum spanning tree if it's greedy? Isn't a minimum spanning tree a global optimization problem? Isn't the point of being greedy is that there is a chance you won't find the most optimal solution? So how can Kruskal be able to find the minimum spanning tree while also being greedy?

like image 415
DiderDrogba344 Avatar asked Dec 10 '16 02:12

DiderDrogba344


People also ask

Why is Kruskal algorithm A greedy algorithm?

Also, it picks the edge with a minimum cost at first and the edge with a maximum cost at last. Hence, you can say that the Kruskal algorithm makes a locally optimal choice, intending to find the global optimal solution. That is why it is called a Greedy Algorithm.

Is Kruskal's algorithm A greedy algorithm?

It is a greedy algorithm in graph theory as in each step it adds the next lowest-weight edge that will not form a cycle to the minimum spanning forest.

Which algorithm are available for finding minimum cost spanning tree in greedy method?

Prim's algorithm is a greedy approach to find the minimum spanning tree. In this algorithm, to form a MST we can start from an arbitrary vertex. The function Extract-Min returns the vertex with minimum edge cost.

Is Kruskal greedy or dynamic programming?

Kruskal's algorithm is a greedy algorithm in graph theory that is used to find the Minimum spanning tree (A subgraph of a graph G ( V , E ) G(V,E) G(V,E) which is a tree and includes all the vertices of the given graph such that the sum of the weight of the edges is minimum) of a given connected, weighted, undirected ...


2 Answers

Okay, let's assume that you're right, so Kruskal's algorithm doesn't find the optimal solution. Let the solution find by Kruskal's algorithm S, and the optimal solution T.

There must be an edge e = (u, v) that appears on S but not on T. As T is an spanning tree, there must be a path between node u and node v.

Now, we should notice that at least one edge on the path u-v has weight not smaller than e. Otherwise, Kruskal's algorithm would have chosen all the edges on the path u-v instead of edge e.

That means, if we remove that edge and add e on the solution T, the solution doesn't get worse. And as we assumed that T is optimal, after this change, the tree is still optimal. If we apply this logic repeatedly, we can always make S.

like image 162
miheyan Avatar answered Dec 19 '22 20:12

miheyan


I could comprehend the askings as the following question- Greedy is not always optimal then why Kruskal's algorithm gets the optimal solution? So this question can be answered in two parts-

1. Does Kruskal algorithm gives optimal solution? This is already answered by @miheyan.

2. If greedy always gives optimal solution? In general NO, Greedy doesn't give optimal solution always but there is a set of problems for which Greedy approach gives optimal solution and Kruskal's algorithm lies in that set.

Let's take a problem statement - There are two players(player A and Player B) who are given a pile of money with different denomination. Let's say there are 4 currency notes with values as- 100, 50, 20, 10. Players will choose one currency note at a time and they will pick alternatively. Player A starts the game. Winner will be the person who gets more money.Both players play optimally. Who will win the game? Now try to solve this problem with greedy approach and see if greedy approach gives the optimal solution or not? Take different values, different examples and do your home work.

So the bottom line is - for a set of problems Greedy solution is always optimal but not for all problems. Hope it helps!

like image 23
CNatka Avatar answered Dec 19 '22 20:12

CNatka