0% found this document useful (0 votes)
14 views3 pages

greedy_algorithms_notes

The document outlines key greedy algorithms including the Fractional Knapsack Problem, Job Sequencing with Deadlines, Coin Change Problem, Huffman Coding, Minimum Spanning Tree (Prim's and Kruskal's algorithms), and Dijkstra's Algorithm. Each algorithm is presented with a brief description and a structured pseudocode for implementation. These algorithms focus on optimizing specific problems by making locally optimal choices at each step.

Uploaded by

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

greedy_algorithms_notes

The document outlines key greedy algorithms including the Fractional Knapsack Problem, Job Sequencing with Deadlines, Coin Change Problem, Huffman Coding, Minimum Spanning Tree (Prim's and Kruskal's algorithms), and Dijkstra's Algorithm. Each algorithm is presented with a brief description and a structured pseudocode for implementation. These algorithms focus on optimizing specific problems by making locally optimal choices at each step.

Uploaded by

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

Greedy Algorithms - Exam Preparation Notes

1. Fractional Knapsack Problem


Maximize profit by taking whole items or fractions. Sort by profit/weight ratio (greedy choice).
BEGIN
FOR i <- 1 TO n DO
x[i] <- 0
ENDFOR
Compute ratio[i] <- p[i] / w[i]
Sort items in decreasing ratio
cp <- 0; remaining <- M
FOR each item i DO
IF w[i] <= remaining THEN
x[i] <- 1
cp <- cp + p[i]
remaining <- remaining - w[i]
ELSE
x[i] <- remaining / w[i]
cp <- cp + p[i] * x[i]
BREAK
ENDIF
ENDFOR
OUTPUT x and cp
END

2. Job Sequencing with Deadlines


Schedule jobs to maximize profit. Sort jobs by profit (greedy), then place each into the latest
available slot before its deadline.
BEGIN
Sort jobs in decreasing profit
Initialize slot[1..max_deadline] <- FREE
FOR each job j in sorted order DO
FOR k <- min(deadline[j], max_deadline) DOWNTO 1 DO
IF slot[k] = FREE THEN
slot[k] <- j
total_profit <- total_profit + profit[j]
BREAK
ENDIF
ENDFOR
ENDFOR
OUTPUT scheduled jobs and total_profit
END

3. Coin Change Problem (Greedy)


Make change using the largest coin denominations first (works for canonical systems like USD).
BEGIN
remaining <- amount
FOR each coin value c in decreasing order DO
count[c] <- remaining // c
remaining <- remaining % c
ENDFOR
OUTPUT count of each coin
END

4. Huffman Coding
Build optimal prefix code by repeatedly merging two smallest-weight nodes (greedy choice).
BEGIN
Create forest: each symbol is a tree with weight = frequency
WHILE more than one tree exists DO
Extract two trees with smallest weights
Create new node with weight = sum of weights
Make the two trees its children
Insert new tree back into forest
ENDWHILE
Assign binary codes by traversing the final tree (0 for left, 1 for right)
OUTPUT codes for all symbols
END

5. Minimum Spanning Tree


a) Prim's Algorithm: Grow a tree by adding the nearest vertex.
b) Kruskal's Algorithm: Add smallest edges without creating cycles.
-- Prim's Algorithm BEGIN
Initialize tree with arbitrary start vertex
FOR count <- 1 TO V-1 DO
Find edge (u, v) with minimum weight such that u in tree and v not in tree
Add v and edge to tree
ENDFOR
OUTPUT MST
END

-- Kruskal's Algorithm BEGIN


Sort all edges in increasing weight
Initialize disjoint sets for each vertex
FOR each edge (u, v) in sorted order DO
IF Find(u) <> Find(v) THEN
Union(u, v)
Add edge to MST
ENDIF
ENDFOR
OUTPUT MST
END

6. Dijkstra's Algorithm
Find shortest paths from source. Greedy: pick the closest unvisited vertex next.
BEGIN
Initialize dist[v] <- INF for all v; dist[source] <- 0
Set S (visited) <- empty
WHILE there are unvisited vertices DO
u <- vertex with minimum dist[] not in S
Add u to S
FOR each neighbor v of u DO
IF dist[u] + weight(u,v) < dist[v] THEN
dist[v] <- dist[u] + weight(u,v)
ENDIF
ENDFOR
ENDWHILE
OUTPUT dist[] for all vertices
END

You might also like