mirror of
https://github.com/dholerobin/Lecture_Notes.git
synced 2025-07-01 13:06:29 +00:00
Update Kruskal's Algorithm.md
This commit is contained in:
parent
b67acb6a0d
commit
3e5a5aebed
@ -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.
|
||||
|
||||

|
||||
***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.
|
||||
**Answer:** Dark lines represents the spanning tree which is not unique.
|
||||

|
||||
|
||||
### 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.
|
||||

|
||||
|
||||
**Answer:**
|
||||

|
||||
|
||||
**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:
|
||||
|
||||

|
||||
|
||||
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**
|
||||

|
||||
|
||||

|
||||

|
||||
|
||||

|
||||

|
||||
|
||||
Are you wondering how we will do the step $2$ of the algorithm? Which is to find whether two vertices are already connected.
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user