Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 28

Greedy Algorithms

MINIMUM SPANNING TREE


Definition
• A tree is a connected graph without cycles.
• A graph is a tree if and only if there is one and only one
path joining any two of its vertices.
• A connected graph is a tree if and only if it has N vertices
and N-1 edges.
• A subgraph that spans (reaches out to) all vertices of a
graph is called a spanning subgraph.
• A subgraph that is a tree and that spans (reaches out to) all
vertices of the original graph is called a spanning tree.
• Among all the spanning trees of a weighted and connected
graph, the one (possibly more) with the least total weight is
called a minimum spanning tree (MST).
Greedy Choice
We will show two ways to build a minimum
spanning tree.
• KRUSKAL’S ALGORITHM

• PRIM’S ALGORITHM

Greedy/Prims 3
Kruskal's algorithm
• In graph theory this algo finds a minimum
spanning tree for a connected weighted
graph.
• This means it finds a subset of the edges that
forms a tree that includes every vertex, where
the total weight of all the edges in the tree is
minimized.
Kruskal’s algorithm
• In Kruskal’s algorithm, the set A is a forest. The safe edge added
to A is always a least-weight edge in the graph that connects two
distinct components.

• It. finds a safe edge to add to the growing forest by finding, of all
the edges that connect any two trees in the forest, an edge (u, v)
of least weight.

• Kruskal’s algorithm is a greedy algorithm, because at each step it


adds to the forest an edge of least possible weight.
The combining of trees is accomplished by the UNION procedure
Explanation

• Kruskal’s logic is a greedy logic.


• We first arrange the edges in ascending order of
weights.
• We also make ‘n’ sets of one node each.
• We then start the iterations. We pick up the edge with
the edge with the lowest weight and we check whether
the nodes of the edge belong to the same set. If the do
we make the union of the two sets. Else we move on to
the next edge in the queue.
• In this way, when all the nodes belong to the same set,
our minimum spanning tree is ready.
Prim’s Algorithms
In this case we start with a source node, say ‘s’. We maintain an array of the
distance of each node from the MST being built.
We also maintain an array of the predecessor of each node. When a node has
not yet been selected into the MST it’s predecessor is NULL.
Initially we set all distances as infinity except that of the source which is set to
zero.
We now start the iterations.
We select the node that has the least distance, say ‘u’. In the first loop, the
source is selected because its distance is zero. In subsequent iterations this
process ensures that the minimum weight edge in the cut of the current tree is
selected. We now include the node in the tree.
Then we relax all the nodes connected to the node. This means that we see for
each node v connected to u whether the distance of v is less than the weight of
edge (u,v). If it is, we assign w(u,v) to the distance of v.
This step ensures that after each new node is added to the tree, the distance of
the remaining nodes from the tree is updated.
Thus when we have all the edges in the tree, our minimum spanning tree is
ready.
PRIM’S Algorithm
PRIM(G, w, r)
for each u belongs to V [G]
do key[u] ← ∞
π[u] ← NIL
key[r] ← 0
Q ← V [G]
while Q ≠ Ø
do u ← EXTRACT-MIN(Q)
for each v belongs to Adj[u]
do if v belongs to Q and w(u, v) < key[v]
then π[v] ← u
key[v] ← w(u, v)
while Q ≠ φ v Adj {u} For each V ε Adj u
if ∏{V} Key {V}
1 T a b,h T a key[b] = 4, key[h] = 8
2 T b c,h T b key[c] = 8
3 T c d,f,i T c key[d] = 7, key[f] = 4, key[i] = 2
4 T i g,h T i key[g] = 6, key [h] = 7
5 T f d,e,g T f key[e] = 10, key [g] = 2
6 T g f,h,i T g key [h] = 1
7 T h a,b,g,i F - -
8 T d c,e,f T d key [e] = 9
9 T e d,f F - -

while loop terminated


A = |V-Q| = vertices in the MST = {a,b,c,i,f,g,h,d,e}
Shortest Path Problems
• In general, in graph theory the shortest path
between two vertices in a graph is a path
between those two vertices in such way that
the sum of weights of edges in that path is the
smallest than the sum of weights of edges in
any path between those two vertices provided
the graph is a weighted graph. So the shortest
path problem is to find such a path between the
given vertices.
Types
• The two types of finding such shortest paths

are

1) Single source shortest path,

2) All pair shortest path.


Types Cont….

Single Source Shortest Path Problems (sssp): In these types of


problems we fix a vertex (or node), which is called source, and we find the
shortest path from this source to every other node.e.g
1 Dijkstras Algorithm[weights of edges are nonnegative]
2 Bellman-Ford Algorithm: This algorithm is used to solve the SSSP
problems when the edges may be negative.
3 A* search algorithm

All pair shortest path

All pair shortest path problems are problems of finding shortest paths between
every pair of vertices.
Floyd-Warshall algorithm
Johnson's algorithm
Example 1
Final Solution

You might also like