Greedy Algorithm LectueNote
Greedy Algorithm LectueNote
Greedy Algorithm LectueNote
GREEDY ALGORITHMS
Sub-topics Outlines
• Characteristics of Greedy Algorithm
• Graph Minimum Spanning Tree(MST)-Kruskal’s And Prim’s Algorithms
• Shortest Paths
• Scheduling
What is Greedy Algorithm?
A greedy algorithm is any algorithm that follows the problem-solving
heuristic of making the locally optimal choice at each stage.
greedy strategy does not produce an optimal solution, but a greedy
heuristic can yield locally optimal solutions that approximate a globally
optimal solution in a reasonable amount of time.
✓Is defined as a method for solving optimization problems by taking decisions that
result in the most evident and immediate benefit irrespective of the final outcome.
✓It works for cases where minimization or maximization leads to the required
solution.
Characteristics of Greedy algorithm
For a problem to be solved using the Greedy approach, it must follow a few major
characteristics:
▪There is an ordered list of resources(profit, cost, value, etc.)
▪Maximum of all the resources(max profit, max value, etc.) are taken.
▪For example, in the fractional knapsack problem, the maximum value/weight is
taken first according to available capacity.
For example consider the Fractional Knapsack problem
Optimal substructure property and can be solved using Greedy approach are:
1) Job sequencing Problem:
Greedily choose the jobs with maximum profit first, by sorting the jobs in decreasing
order of their profit. This would help to maximize the total profit as choosing the job
with maximum profit for every time slot will eventually maximize the total profit
2) Prim’s algorithm to find Minimum Spanning Tree:
It starts with an empty spanning tree. The idea is to maintain two sets of vertices.
The first set contains the vertices already included in the MST, the other set contains
the vertices not yet included. At every step, it considers all the edges that connect
the two sets and picks the minimum weight edge from these edges. After picking the
edge, it moves the other endpoint of the edge to the set containing MST.
What is Graph?
A Graph is a non-linear data structure consisting of vertices and edges.
The vertices are sometimes also referred to as nodes and the edges are lines or arcs
that connect any two nodes in the graph.
More formally a Graph is composed of a set of vertices( V ) and a set of edges( E ).
The graph is denoted by G(E, V).
Components of a Graph
•Vertices: Vertices are the fundamental units of the graph. Sometimes, vertices are
also known as vertex or nodes. Every node/vertex can be labeled or unlabeled.
•Edges: Edges are drawn or used to connect two nodes of the graph. It can be
ordered pair of nodes in a directed graph. Edges can connect any two nodes in any
possible way. There are no rules. Sometimes, edges are also known as arcs. Every
edge can be labeled/unlabeled.
What is a Spanning Tree?
A Spanning tree is a subset of connected graph (G), where all the edges are
connected.
➢one can traverse to any edge from a particular edge with or without
intermediates.
➢Also, a spanning tree must not have any cycle in graph. If there are N vertices in
a connected graph then the number of edges that a spanning tree may have is N-1.
➢Minimum spanning Tree is the sum of edges weights from the source to destination
is minimum.
Maze solving problem
Graph Minimum Spanning Tree (MST)
Prim’s and Kruskal’s Algorithms
•Prim’s and Kruskal’s Algorithm are the famous greedy algorithms.
•They are used for finding the Minimum Spanning Tree (MST) of a given graph.
•To apply these algorithms, the given graph must be weighted, connected and
undirected.
If all the edge weights are distinct, then both the algorithms are guaranteed to find
the same MST.
Step-2
Step-4 Step-6
- Neil Armstrong