Design and Analysis of Algorithms · Unit 4 · 6 hrs
Greedy Algorithms
Exam-focused notes for Greedy Algorithms (Design and Analysis of Algorithms, CSC325): what the TU syllabus asks and how it has actually been tested, with 10 solved past questions from this unit.
Greedy Algorithms in short
A greedy algorithm builds a solution step by step, at each step making the choice that looks best right now and never revisiting it. This unit sets out when that shortcut is provably correct, then applies it to the minimum spanning tree, shortest path, scheduling, and data compression problems that are its classic successes.
What this unit covers
- Optimization Problems and Optimal Solution, Introduction of Greedy Algorithms, Elements of Greedy Strategy
- Greedy Algorithms: Fractional Knapsack, Job sequencing with Deadlines, Kruskal's Algorithm, Prims Algorithm, Dijkstra's Algorithm and their Analysis
- Huffman Coding: Purpose of Huffman Coding, Prefix Codes, Huffman Coding Algorithm and its Analysis
Huffman Coding
2082 · 10 marks
How do you define optimal solution? Does greedy algorithm always guarantee optimal solution? Given the string "SUPER DUPER CSIT", use a Greedy algorithm to build a Huffman tree.[10]
2080 · 5 marks
Suppose that a message contains alphabet frequencies as given below and find Huffman codes for each alphabet.
| Symbol | Frequency |
|---|---|
| a | 30 |
| b | 20 |
| c | 25 |
| d | 15 |
| e | 35 |
[5]
2079 · 5 marks
Generate the prefix code for the string "CYBER CRIME" using Huffman algorithm and find the total number of bits required. [5]
Greedy Algorithms
2081 · 10 marks
Does greedy algorithm guarantee optimal solution? Solve the Fractional knapsack problem to find maximum loot from given information.
| Item | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
|---|---|---|---|---|---|---|---|
| Value | 12 | 10 | 20 | 15 | 2 | 3 | 50 |
| Weight (kgs) | 2 | 1 | 3 | 2 | 12 | 10 | 1 |
[10]
2080 · 5 marks
When greedy strategy provides optimal solution? Write down job sequencing with deadlines algorithm and analyze its complexity. [5]
2079 · 5 marks
Find the MST from following graph using Kruskal's algorithm. [5]
2078 · 5 marks
Explain the algorithm and its complexity for solving job sequencing with deadline problem using greedy strategy. [5]
Show solved answerHide answer
Job Sequencing with Deadlines - Greedy Strategy
Problem Statement
Given n jobs, each with a deadline and a profit (earned only if the job is completed by its deadline), find a feasible sequence of jobs that maximizes total profit. Each job takes unit time to complete and only one machine (processor) is available.
Greedy Strategy
The greedy approach selects jobs in decreasing order of profit (most profitable job first) and schedules each job in the latest available time slot before its deadline.
Algorithm
JobSequencingWithDeadline(D, J, n, k)
1. Sort all jobs in decreasing order of profit P.
2. D(0) = J(0) = 0
3. k = 1
4. J(1) = 1 // Select the first (highest profit) job
5. for i = 2 to n do:
r = k
while D(J(r)) > D(i) and D(J(r)) ≠ r do:
r = r - 1
if D(J(r)) ≤ D(i) and D(i) > r then:
for l = k downto r+1 do:
J(l+1) = J(l)
J(r+1) = i
k = k + 1
6. Output J(1), J(2), ..., J(k) // Feasible job sequence with max profit
Step-by-Step Example
| Job | Deadline | Profit |
|---|---|---|
| J1 | 2 | 100 |
| J2 | 1 | 19 |
| J3 | 2 | 27 |
| J4 | 1 | 25 |
| J5 | 3 | 15 |
After sorting by profit (descending): J1(100), J3(27), J4(25), J2(19), J5(15)
- Select J1 → place at slot 2 → Schedule: [_, J1]
- Select J3 → deadline 2, slot 1 is free → Schedule: [J3, J1]
- Select J4 → deadline 1, slot 1 is taken → skip
- Select J2 → deadline 1, slot 1 is taken → skip
- Select J5 → deadline 3, slot 3 is free → Schedule: [J3, J1, J5]
Result: J3, J1, J5 with total profit = 27 + 100 + 15 = 142
Complexity Analysis
| Phase | Operation | Complexity |
|---|---|---|
| Sorting jobs by profit | Comparison sort | O(n log n) |
| Selecting and inserting each job | Inner while loop (at most k iterations per job) | O(n²) in worst case |
- The outer loop runs n times.
- The inner while loop and shifting loop together run at most O(n) per iteration.
- Therefore, the overall time complexity is:
$$T(n) = O(n^2)$$
The dominant term is the job insertion step, making the overall complexity O(n²).
Summary
| Aspect | Detail |
|---|---|
| Strategy | Greedy (highest profit first) |
| Key Idea | Schedule each job in latest available slot before deadline |
| Time Complexity | O(n²) |
| Optimality | Gives optimal (maximum profit) feasible schedule |
2076 · 5 marks
Explain the greedy algorithm for the fractional knapsack problem with its time complexity. [5]
2076 · 5 marks
Explain Prim’s algorithm for MST problem and analyze its time complexity. [5]
Optimization Problems and Optimal Solution, Introduction of Greedy Algorithms, Elements of Greedy Strategy
2078 · 5 marks
What do you mean by optimization problem? Explain the greedy strategy for algorithm design to solve optimization problems. [5]
Show solved answerHide answer
Optimization Problem and Greedy Strategy
Optimization Problem
An optimization problem is the problem of finding the best solution from all feasible solutions. The goal is either to maximize or minimize some objective function subject to given constraints.
Optimization problems can be divided into two categories:
- Discrete Optimization: Variables are discrete (e.g., integer values). Examples include knapsack problem, shortest path problem, etc.
- Continuous Optimization: Variables are continuous. These include constrained problems and multimodal problems.
Greedy Strategy for Algorithm Design
The greedy strategy (or greedy method) is an algorithmic approach that builds up a solution piece by piece, always choosing the next piece that offers the most immediate or local benefit (the locally optimal choice) at each step, with the hope of finding a global optimum.
Key Idea
At each step, make the choice that looks best at the moment without reconsidering previous choices.
Characteristics of Greedy Strategy
| Property | Description |
|---|---|
| Greedy Choice Property | A globally optimal solution can be arrived at by making locally optimal (greedy) choices at each step. |
| Optimal Substructure | An optimal solution to the problem contains optimal solutions to its subproblems. |
| No Backtracking | Once a choice is made, it is never reconsidered. |
General Steps of Greedy Algorithm
- Initialize an empty solution set.
- Select the best available candidate (greedy choice) from the remaining options.
- Check feasibility: Determine if the selected candidate can be added to the solution without violating constraints.
- Add the candidate to the solution if feasible.
- Repeat steps 2-4 until a complete solution is obtained.
Example: Activity Selection Problem
Given a set of activities with start and finish times, select the maximum number of non-overlapping activities.
Greedy Choice: Always select the activity that finishes earliest among the remaining compatible activities.
Activities sorted by finish time:
A1(1,3), A2(2,5), A3(4,6), A4(6,8)
Step 1: Select A1 (earliest finish = 3)
Step 2: A2 overlaps with A1, skip
Step 3: Select A3 (start=4 >= finish of A1=3)
Step 4: Select A4 (start=6 >= finish of A3=6)
Result: {A1, A3, A4} --> Maximum 3 activities
Advantages of Greedy Strategy
- Simple and easy to implement.
- Generally faster than dynamic programming (often O(n log n) or O(n)).
- Works well when greedy choice property and optimal substructure hold.
Limitations
- Does not always produce a globally optimal solution.
- Cannot be applied to all optimization problems (e.g., 0/1 Knapsack does not yield optimal result with greedy).
Common Problems Solved Using Greedy Strategy
- Fractional Knapsack Problem
- Huffman Coding
- Prim's and Kruskal's Minimum Spanning Tree
- Dijkstra's Shortest Path Algorithm
- Activity Selection Problem
In summary, the greedy strategy solves optimization problems by making the locally best choice at each step, which leads to an efficient and often optimal solution when the problem satisfies the greedy choice property and optimal substructure.
Make Unit 4 stick
Practice CSC325 with flashcards & quizzes