Update Kruskal's Algorithm.md

This commit is contained in:
Aakash Panchal 2020-01-14 18:13:10 +05:30 committed by GitHub
parent b67acb6a0d
commit 3e5a5aebed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,7 +1,11 @@
## Kruskal's Algorithm
Suppose, You are running a company with several offices in different cities. Now, you want to connect all the offices by phone lines. Different networking companies are asking for different amount of money to connect different pairs of offices.
![enter image description here](https://lh3.googleusercontent.com/644Hn9oGhJPFAkZbyOyXtKx89cAlnim_O2dqWgc5W_YebIHwAHlWrsMpZPzgzbSwENFbsjuVYR0h)
***Comp** is an abbreviation of Company.
Now, how will you figure out the way with minimum cost?
Well, this problem can be solved by using classical algorithms to find the minimum spanning tree for the given graph.
@ -9,7 +13,8 @@ Well, this problem can be solved by using classical algorithms to find the minim
What is the "**Minimum Spanning Tree**" ? and even before that, what is a "**Spanning Tree**"?
### Spanning Tree (ST)
Spanning Tree for a given graph is a subgraph, which is a tree that includes every vertex of a graph with minimum possible number of edges.
Spanning Tree for a given undirected graph is a subgraph, which is a tree that includes every vertex of a graph with minimum possible number of edges.
### Quiz Time
@ -17,20 +22,46 @@ $Q.1$ What is the minimum possible number of edges that can connect all the vert
Answer: $|V|-1$
$Q.2$ Find one ST for the following graph.
$Q.2$ Find one ST for the following graph.![enter image description here](https://lh3.googleusercontent.com/V4UYMyPf_paL45vwYaaZZ1EYzp2WwwqKmzS9NyqZT-WxtTvBrLzP4e7uI0iaarQOt-UkVJ19CHl5)
**Answer:** Dark lines represents the spanning tree which is not unique.
![enter image description here](https://lh3.googleusercontent.com/9EVi1ivLU2S0G7P4h_bbanZbSqoTm4C2eK_18J2c1F7_PNz9JL_2nmVlyi9VHTyc84YrgjywpLjy)
### Minimum Spanning Tree (MST)
Minimum Spanning Tree is the Spanning Tree with minimum cost.
Here the cost has different meanings for different kinds of problem. For example, In the above stated problem, length(or cost of the wire per unit length) of the cable is the cost.
Here the cost has different meanings for different kinds of problem. For example, In the above stated problem, cost is the money asked by different companies.
### Quiz Time
Find the MST for the given graph.
![enter image description here](https://lh3.googleusercontent.com/M-17sSWWCuciZYbp7X3FNOuT8EYObz4Ao0m6_dvrCmUcR5nre5Kdzau5KCLlSA92OE0G3l6xYd6w)
**Answer:**
![enter image description here](https://lh3.googleusercontent.com/IjdTe4v1wNe1CPqweKQdktcnNI7ZT2XRaj01VmhC16orCqGPSJjPTEumQf78NNRanYhOaNDJR0I5)
**Note:** Here we will be talking about undirected graph because directed graph may or may not have a MST, however an undirected graph always have a MST.
**Note**: Here we are talking about an undirected graph because directed graph may or may not have ST. See the image below:
![enter image description here](https://lh3.googleusercontent.com/RZCQAvafhR94_siwLkjLdhtbgmXA4YU3iAmIOBJhh8G3wB615ZIYU6a9xpkKZrqPgcjn0V8jh8-1)
For a directed graph to have a ST, there must be a vertex (say "$root$") from which we can reach to every other vetex by directed paths.
How can you find a MST for an undirected graph?
## Brute Force
One basic idea is to find all the paths which are using exactly $|V| - 1$ edges and including all $|V|$ vertices - find all ST of a graph.
Take the minimum cost path(ST) which will be the MST for a given graph.
This process can lead to exponential time complexity, because in order to find all possible paths we have to spend exponential time in a very dense graph.
We have an elegant algorithm to solve MST finding problem very efficiently.
**Terminologies and notes:**
1. **Connected component** is a subgraph of a graph, which has a path between every pair of vertices in it. And each of the path uses no additional vertices outside of this subgraph.
2. At the start of the algorithm, each vertex is representing different component on their own.
3. Unifying two connected components results into one connected component, having all the vertices of both components.
## Kruskal's Algorithm
@ -41,16 +72,20 @@ Kruskal's Algorithm is a **greedy algorithm**, which chooses the least weighted
1. Sort the array of the edges, by the weights of the edges.
2. Now, loop over this sorted array.
If the edge is connecting two vertices which are not already in the same connected component*, then add that edge in tree. And also unify both of the components.
If the edge is connecting two vertices which are not already in the same connected component, then add that edge in the list of MST edges. And also unify both of the components.
3. When all $|V|$ vertices are added in the tree, then stop the algorithm. Now this tree represents MST for a given graph.
**Connected component** is a subgraph of a graph, which has a path between every pair of vertices in it. And each of the path uses no additional vertices outside of this subgraph.
At the end of the algorithm there will be only one connected component, which includes each vertex of the graph.
**Visualization**
![enter image description here](https://lh3.googleusercontent.com/jKx_yhQnsvEIpn4UIZxgaoM0gtjkFmMqSszqO0A0VDL-UminMn8TCP61_7sXaGafwcwtKgjcdji2)
![enter image description here](https://lh3.googleusercontent.com/xGlG4VqzhxnV-EochhwHC_wr5POGoK5z8BNNWhK_IA4vURTCIUAjaTctrBA4lsJFCakL5Kygcl6t)
![enter image description here](https://lh3.googleusercontent.com/Uds7vSzkS8yehyExYhPnYTFK8QnbfUMa0Kqh6TaCUTG2BoK7ONyulbuVFajKC7cq7AB2rWS_Jt3R)
![same Connected component thing](https://lh3.googleusercontent.com/c6O5HLwwkmAmaPAQBhZoKODGT2HrOwa1kfeXSPZUy7QxfmH8igeP-ZK9QI8v-DfhitJ6Rsoy9_2D)
![enter image description here](https://lh3.googleusercontent.com/LposVdQeQcUXvQak0_v5yl4ob7DwWEInwPgmdI2_QTgSgHwRx8ViOUhJlRBijSE87N0Fr3GnQS7P)
Are you wondering how we will do the step $2$ of the algorithm? Which is to find whether two vertices are already connected.