Chapter 2 Final
Chapter 2 Final
Advantages:
Efficiency: Greedy algorithms are often faster and simpler compared to other
methods like dynamic programming.
Practicality: They are easy to implement for many real-world problems.
Limitations:
Non-optimal Solutions: Not all problems are amenable to greedy strategies (e.g., 0/1
Knapsack Problem).
Problem-Specific: Requires the problem to have the greedy-choice property and
optimal substructure.
The Knapsack Problem is a classic optimization problem in which the goal is to maximize
the value of items placed in a knapsack while adhering to its weight capacity. There are two
primary variations of the problem:
In this variation, you can take fractions of items, meaning you can divide an item to fit it into
the knapsack.
Problem Statement:
You are given n items,36 each with a weight wi_ and a value vi.
The knapsack has a weight capacity W.
Maximize the total value of items in the knapsack while not exceeding the capacity W.
Approach:
Time Complexity:
NUMERICAL
Example
Let us consider a set of given jobs as shown in the following table. We have to find a
sequence of jobs, which will be completed within their deadlines and will give maximum
profit. Each job is associated with a deadline and profit.
Job J1 J2 J3 J4 J5
Deadline 2 1 3 2 1
Profit 60 100 20 40 20
Solution
To solve this problem, the given jobs are sorted according to their profit in a descending
order. Hence, after sorting, the jobs are ordered as shown in the following table.
Job J2 J1 J4 J3 J5
Deadline 1 2 2 3 1
Profit 100 60 40 20 20
From this set of jobs, first we select J2, as it can be completed within its deadline and
contributes maximum profit.
Analysis
In this algorithm, we are using two loops, one is within another. Hence, the complexity of
this algorithm is O(n2).
Characteristics of MST:
1. Spanning Tree: It is a tree that includes all the vertices of the graph with
the minimum number of edges (V - 1, where V is the number of
vertices).
2. Weight Minimization: The total weight of the edges in the MST is
minimized.
3. No Cycles: An MST is a tree and cannot contain any cycles.
SPANNING TREE
The total number of spanning trees with n vertices that can be created
from a complete graph is equal to n
(n-2)
.
If we have n = 4, the maximum number of possible spanning trees is
equal to 44-2 = 16. Thus, 16 spanning trees can be formed from a
complete graph with 4 vertices.
Example of a Spanning Tree
Normal graph
Some of the possible spanning trees that can be created from the above graph are:
A minimum spanning tree is a spanning tree in which the sum of the weight of the edges is as
minimum as possible.
Let's understand the above definition with the help of the example below.
Weighted graph
Minimum spanning tree - 1 Minimum spanning tree - 2 Minimum spanning tree - 3 Minimum spanning tree - 4
The minimum spanning tree from the above spanning trees is:
Steps:
Input Graph:
The graph contains 9 vertices and 14 edges. So, the minimum spanning tree formed will be
having (9 – 1) = 8 edges.
After sorting:
1 7 6
2 8 2
2 6 5
4 0 1
4 2 5
6 8 6
7 2 3
7 7 8
8 0 7
8 1 2
9 3 4
10 5 4
11 1 7
14 3 5
Now pick all edges one by one from the sorted list of edges
Step 6: Pick edges 8-6. Since including this edge results in the cycle, discard it. Pick edge 2-
3: No cycle is formed, include it.
Add edge 2-3 in the MST
Step 7: Pick edge 7-8. Since including this edge results in the cycle, discard it. Pick edge 0-7.
No cycle is formed, include it.
Step 8: Pick edge 1-2. Since including this edge results in the cycle, discard it. Pick edge 3-4.
No cycle is formed, include it.
Note: Since the number of edges included in the MST equals to (V – 1), so the algorithm
stops here
Start from an arbitrary vertex and grow the MST one edge at a time by adding the smallest
edge that connects a vertex in the MST to a vertex outside it.
Steps:
1. Initialize all vertices as not visited.
2. Choose any vertex as the starting point and mark it as visited.
3. Add the smallest edge that connects a visited vertex to an unvisited vertex.
4. Repeat until all vertices are included in the MST.
The Single Source Shortest Path (SSSP) problem aims to find the shortest paths from a
given source vertex to all other vertices in a weighted graph. The graph can be directed or
undirected.
Common Algorithms for SSSP:
1. Dijkstra’s Algorithm
Type: Greedy
Input: Weighted graph with non-negative weights.
Output: Shortest distance from the source to all other vertices.
Steps:
1. Initialize the distance of the source vertex to itself as 0 and all others as infinity (∞\infty).
2. Use a priority queue (min-heap) to keep track of the vertices with the smallest known
distance.
3. Extract the vertex with the smallest distance, mark it as visited, and update the distances of
its neighbors.
4. Repeat until all vertices are processed or the queue is empty.
Time Complexity:
1. Purpose
Dijkstra's Algorithm is used to find the shortest path from a single source vertex to all other
vertices in a graph.
It works for graphs with non-negative edge weights only.
2. Approach
It is a greedy algorithm, which means it selects the shortest (locally optimal) path at each
step with the hope of finding the globally optimal solution.
3. Steps
Initialization: Start with the source vertex, set its distance to 00, and all others to ∞\infty
(infinity).
Priority Queue: Use a min-heap to efficiently get the next vertex with the smallest known
distance.
Relaxation: For each vertex, update the distances of its neighbors if a shorter path is found
through the current vertex.
4. Time Complexity
5. Limitations
Does not work with graphs containing negative weight edges, as the greedy approach
cannot guarantee correct results.
For graphs with negative weights, Bellman-Ford Algorithm is used instead.
COMPLEXITY CHART