0% found this document useful (0 votes)
29 views27 pages

DAA Unit 3 Part 1

The document discusses two algorithmic approaches: Greedy and Dynamic Programming. The Greedy approach makes decisions based on current information without considering future consequences, while Dynamic Programming breaks problems into sub-problems and stores results for optimization. Additionally, it covers Minimum Spanning Trees (MST), their properties, applications, and algorithms like Kruskal's and Prim's for finding MSTs in graphs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views27 pages

DAA Unit 3 Part 1

The document discusses two algorithmic approaches: Greedy and Dynamic Programming. The Greedy approach makes decisions based on current information without considering future consequences, while Dynamic Programming breaks problems into sub-problems and stores results for optimization. Additionally, it covers Minimum Spanning Trees (MST), their properties, applications, and algorithms like Kruskal's and Prim's for finding MSTs in graphs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

Greedy Approach

&
Dynamic Programming Approach
Greedy Approach
• The Greedy method is the simplest and straightforward approach.
• It is not an algorithm, but it is a technique.
• The main function of this approach is that the decision is taken on the basis of the
currently available information.
• Whatever the current information is present, the decision is made without worrying about
the effect of the current decision in future.
• This technique is basically used to determine the feasible solution that may or may not be
optimal.
• The feasible solution is a subset that satisfies the given criteria.
• The optimal solution is the solution which is the best and the most favorable solution in
the subset.
• Some popular Greedy Algorithms are Fractional Knapsack, Dijkstra’s algorithm, Kruskal’s
algorithm, Huffman coding and Prim’s Algorithm
Greedy Approach
How does the Greedy Algorithm work?
1. Start with the initial state of the problem. This is the starting point from where you begin
making choices.

2. Evaluate all possible choices you can make from the current state. Consider all the options
available at that specific moment.

3. Choose the option that seems best at that moment, regardless of future consequences. This is
the “greedy” part – you take the best option available now, even if it might not be the best in
the long run.

4. Move to the new state based on your chosen option. This becomes your new starting point for
the next iteration.

5. Repeat steps 2-4 until you reach the goal state or no further progress is possible. Keep
making the best local choices until you reach the end of the problem or get stuck..
Dynamic Programming Approach
• Dynamic programming is a technique that breaks the problems into sub-problems, and
saves the result for future purposes so that we do not need to compute the result again.
• The main use of dynamic programming is to solve optimization problems. Here,
optimization problems mean that when we are trying to find out the minimum or the
maximum solution of a problem. T
• The dynamic programming guarantees to find the optimal solution of a problem if the
solution exists.
• Wherever we see a recursive solution that has repeated calls for the same inputs, we can
optimize it using Dynamic Programming.
• The idea is to simply store the results of subproblems so that we do not have to re-
compute them when needed later.
Comparison
Comparison
Minimum Spanning Trees
A spanning tree of a graph is a subgraph that:
• Includes all the vertices of the original graph.
• Is connected, meaning there is a path between any two vertices.
• Contains no cycles, so it’s a tree (a connected acyclic graph).

A Minimum Spanning Tree (MST) is a subset of the edges in a connected, weighted,


undirected graph that:
• Connects all vertices without forming any cycles (i.e., it's a tree).
• Has the minimum possible total edge weight, compared to any other subset of edges that
forms a spanning tree.
Minimum Spanning Trees
Properties of Minimum Spanning Trees
• The spanning tree holds the below-mentioned properties:
• The number of vertices (V) in the graph and the spanning tree is the same.

• There is a fixed number of edges in the spanning tree which is equal to one less than the
total number of vertices ( E = V-1 ).

• The spanning tree should not be disconnected, as in there should only be a single
source of component, not more than that.

• The spanning tree should be acyclic, which means there would not be any cycle in the
tree.

• The total cost (or weight) of the spanning tree is defined as the sum of the edge weights
of all the edges of the spanning tree.

• There can be many possible spanning trees for a graph.


Minimum Spanning Trees
Minimum Spanning Trees
Applications
1.Consider n stations are to be linked using a communication network & laying of
communication links between any two stations involves a cost.
The ideal solution would be to extract a subgraph termed as minimum cost spanning tree.
2.Suppose you want to construct highways or railroads spanning several cities then we can
use the concept of minimum spanning trees.
3.Designing Local Area Networks.
4.Laying pipelines connecting offshore drilling sites, refineries and consumer markets.
5.Suppose you want to apply a set of houses with
1. Electric Power
2. Water
3. Telephone lines
4. Sewage lines
Minimum Spanning Trees
Algorithms
1.Kruskal’s Algorithm
2.Prim’s Algorithm

• Kruskal's Algorithm
Minimum Spanning Trees
Kruskal's Algorithm
Steps:
1.Sort all the edges in non-decreasing order of their weight.
2.Pick the smallest edge. Check if it forms a cycle with the spanning tree formed so far. If
the cycle is not formed, include this edge. Else, discard it.
3.Repeat step#2 until there are (V-1) edges in the spanning tree.
Minimum Spanning Trees
Kruskal's Algorithm
Minimum Spanning Trees
Kruskal's Algorithm
Minimum Spanning Trees
Kruskal's Algorithm
Step 5 - After that, pick the edge AE with weight 5. Including this edge will create the cycle,
so discard it.
Step 6 - Pick the edge AC with weight 7. Including this edge will create the cycle, so discard
it.
Step 7 - Pick the edge AD with weight 10. Including this edge will also create the cycle, so
discard it.
Minimum Spanning Trees
Kruskal's Algorithm

The graph contains 9 vertices and 14 edges. So, the minimum spanning tree formed will be
having (9 – 1) = 8 edges.
Minimum Spanning Trees
Kruskal's Algorithm
Minimum Spanning Trees
Prim’s Algorithm
This algorithm always starts with a single node and moves through several adjacent nodes,
in order to explore all of the connected edges along the way.
Minimum Spanning Trees
Prim’s Algorithm
This algorithm always starts with a single node and moves through several adjacent nodes,
in order to explore all of the connected edges along the way.

The working of Prim’s algorithm can be described by using the following steps:
Step 1: Determine an arbitrary vertex as the starting vertex of the MST.
Step 2: Follow steps 3 to 5 till there are vertices that are not included in the MST (known as
fringe vertex).
Step 3: Find edges connecting any tree vertex with the fringe vertices.
Step 4: Find the minimum among these edges.
Step 5: Add the chosen edge to the MST if it does not form any cycle.
Step 6: Return the MST and exit
Minimum Spanning Trees
Prim’s Algorithm
Example: Consider the following graph as an example for which we need to find the
Minimum Spanning Tree (MST).
Minimum Spanning Trees
Prim’s Algorithm
Step 1: Firstly, we select an arbitrary vertex that acts as the starting vertex of the Minimum
Spanning Tree. Here we have selected vertex 0 as the starting vertex.
Minimum Spanning Trees
Prim’s Algorithm
Step 2: All the edges connecting the incomplete MST Step 3: The edges connecting the incomplete MST to
and other vertices are the edges {0, 1} and {0, 7}. other vertices are {0, 7}, {1, 7} and {1, 2}. Among these
Between these two the edge with minimum weight is edges the minimum weight is 8 which is of the edges
{0, 1}. So include the edge and vertex 1 in the MST. {0, 7} and {1, 2}. Let us here include the edge {0, 7} and
the vertex 7 in the MST. [We could have also included
edge {1, 2} and vertex 2 in the MST].
Minimum Spanning Trees
Prim’s Algorithm
Step 4: The edges that connect the incomplete MST Step 5: The connecting edges now are {7, 8}, {1, 2}, {6,
with the fringe vertices are {1, 2}, {7, 6} and {7, 8}. Add 8} and {6, 5}. Include edge {6, 5} and vertex 5 in the
the edge {7, 6} and the vertex 6 in the MST as it has the MST as the edge has the minimum weight (i.e., 2)
least weight (i.e., 1). among them.
Minimum Spanning Trees
Prim’s Algorithm
Step 6: Among the current connecting edges, the edge Step 7: The connecting edges between the incomplete
{5, 2} has the minimum weight. So include that edge MST and the other edges are {2, 8}, {2, 3}, {5, 3} and {5,
and the vertex 2 in the MST. 4}. The edge with minimum weight is edge {2, 8} which
has weight 2. So include this edge and the vertex 8 in
the MST.
Minimum Spanning Trees
Prim’s Algorithm
Step 8: See here that the edges {7, 8} and {2, 3} both Step 9: Only the vertex 4 remains to be included. The
have same weight which are minimum. But 7 is already minimum weighted edge from the incomplete MST to 4
part of MST. So we will consider the edge {2, 3} and is {3, 4}.
include that edge and vertex 3 in the MST.
Minimum Spanning Trees
Prim’s Algorithm
The final structure of the MST is as follows and the weight of the edges of the MST is
(4 + 8 + 1 + 2 + 4 + 2 + 7 + 9) = 37.
Minimum Spanning Trees
Prim’s Algorithm
If we had selected the edge {1, 2} in the third step then the MST would look like the
following.

You might also like