Assignment 3
Assignment 3
Q2. Would a greedy algorithm always work? Can you think of a scenario in which it
would miserably fail?
It is not always the case that the greedy algorithm will give the best optimal solution, as it
makes the choice too early preventing the algorithm to find the best possible solution.
Example: Finding the path with the largest sum
Consider a binary search tree of 3 levels (0 to 2), our goal is to choose the path with the
largest sum at the leaf nodes. The greedy algorithm will make a greedy choice and choose the
path on the right side but the best answer happens to be on the opposite right path.
Q3. Can we use any other algorithm, besides the “greedy algorithm” to find the
minimum spanning tree (MST)?
There are other algorithms as well to find the minimum spanning tree. They are mentioned as
follows:
Boruvka's Algorithm
Boruvka's algorithm is a specific algorithm for finding the MST. It follows a divide-and-
conquer strategy by repeatedly selecting the minimum-weight edge that connects different
components.
Reverse-Delete Algorithm
A reverse-delete algorithm is a different approach to finding the MST. It starts with all edges
included and iteratively removes edges in decreasing order of their weights, ensuring that the
remaining graph remains connected.
Q4. Do the weights of the edges in the graph have an impact on the number of MSTs we
get from the graph? If yes, explain with the help of an example.
Yes, the weights of the edges in the graph have does have an impact on the number of
minimum spanning trees we get. If the weight of each edge will be different and unique then
there will be only one resulting minimum spanning tree. But if some edges have identical
weights then there can be more than one MST generated, because then it will depend on the
edge that we decide to choose.
For example:
B
3
5
A C
3
In this case, there will be two different MSTs, one will be A-B (weight 5), and A-C (weight
3) and the total minimum weight will be 8. Another MST will be B=C (weight 3) and A-
C(weight 5) and a total minimum weight of 8.
Q5. Discuss the case where the Kruskal algorithm forms a minimum-spanning forest
instead of a minimum-spanning tree with the help of an example.
During the implementation of the Kruskal algorithm, the algorithm starts off by forming a
forest instead of a MST, it’s because in this algorithm each edge is added separately and one
by one. Thus it forms a forest at each step and finishes by combining all these forests into a
minimum-spanning tree.
A 4 B
5 3
C 2 D
Arranging the weights in their increasing order:
C-D=2, B-D=3, A-B=4, A-C=5
Applying Kruskal’s algorithm:
Adding the first edge
A B A B
C 2 D C 2 D
A 4 B A 4 B
3 3
C 2 D C 2 D
Not adding the last edge weighing 5 as it forms a cycle.
As it is evident from the example mentioned above Kruskal’s algorithm results in a forest
when the graph is disconnected, and every edge is added at each given step.
Resulting minimum spanning tree:
A 4 B
C 2 D