0% found this document useful (0 votes)
2 views1 page

05_greedy_algorithms

The document discusses greedy algorithms, which make locally optimal choices to find a global optimum, though they do not always guarantee a global optimal solution. It outlines conditions under which greedy algorithms work, such as the greedy choice property and optimal substructure, and provides examples of classic problems like Activity Selection, Huffman Coding, Minimum Spanning Tree, and Dijkstra's Shortest Path. Additionally, it includes practice problems to reinforce understanding of greedy methods.

Uploaded by

omar.ali3898
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views1 page

05_greedy_algorithms

The document discusses greedy algorithms, which make locally optimal choices to find a global optimum, though they do not always guarantee a global optimal solution. It outlines conditions under which greedy algorithms work, such as the greedy choice property and optimal substructure, and provides examples of classic problems like Activity Selection, Huffman Coding, Minimum Spanning Tree, and Dijkstra's Shortest Path. Additionally, it includes practice problems to reinforce understanding of greedy methods.

Uploaded by

omar.ali3898
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

# Greedy Algorithms

Date: May 4, 2025

## Concept
- Make locally optimal choices at each step
- Goal: Find global optimum through local optima
- Does not always lead to global optimal solution
- Often simpler and more efficient than DP when applicable

## When Greedy Works


- Greedy choice property: local optimal choice leads to global optimal solution
- Optimal substructure: optimal solution contains optimal solutions to subproblems
- Must prove correctness (often by contradiction or exchange argument)

## Classic Greedy Problems

### Activity Selection


- Choose maximum number of non-overlapping activities
- Sort activities by end time
- Time complexity: O(n log n) for sorting

### Huffman Coding


- Prefix-free variable-length encoding
- Builds optimal binary tree for compression
- Uses frequencies to assign shorter codes to common symbols
- Time complexity: O(n log n)

### Minimum Spanning Tree


- Kruskal's Algorithm:
* Sort edges by weight
* Add edges if they don't create a cycle
* Uses Union-Find data structure
* Time complexity: O(E log E) or O(E log V)

- Prim's Algorithm:
* Start from any vertex
* Add minimum weight edge to expand tree
* Uses priority queue
* Time complexity: O(E log V) with binary heap

### Dijkstra's Shortest Path


- Greedy approach to shortest path
- Always chooses nearest unvisited vertex
- Cannot handle negative weights
- Time complexity: O(E log V) with binary heap

## Interval Scheduling and Partitioning


- Scheduling: sort by end time, select non-overlapping
- Partitioning: find minimum number of resources needed

## Practice Problems:
1. Fractional knapsack (greedy) vs. 0/1 knapsack (DP)
2. Minimum number of coins for change (when greedy works)
3. Job sequencing with deadlines
4. Implementing Huffman coding for text compression

You might also like