1
Advanced Algorithm Assignment 2
Table of Content
Advanced Algorithm Assignment
Introduction
1.0 Dynamic Programming
🔢 1.1 Primitive Calculator – Dynamic Programming 2
🔢 1.2 Minimum Dot Product – Greedy Algorithm 4
🎒 1.3 0/1 Knapsack – Dynamic Programming
4
2.0 Greedy Algorithm
💰 2.1 Fractional Knapsack – Greedy Algorithm 5
🗓 2.2 Activity Selection – Greedy Algorithm 6
🧮 2.3 Huffman Coding – Greedy Algorithm
3.0 Graph
7
🌐 3.1 DFS – Graph Traversal 8
🧭 3.2 Dijkstra's Algorithm – Shortest Paths 9
🌲 3.3 Prim’s Algorithm – Minimum Spanning Tree 10
Final Summary 11
Advanced Algorithm Assignment 3
📘 Introduction
This assignment focuses on implementing foundational and
advanced algorithmic strategies to solve a range of
computational problems using Dynamic Programming,
Greedy Algorithms, and Graph Algorithms.
The tasks involve:
● Designing optimized solutions to classical problems like
the 0/1 Knapsack, Activity Selection, and Huffman
Coding
● Applying graph traversal and shortest path techniques
such as DFS, Dijkstra’s, and Prim’s Algorithms
● Demonstrating efficiency in solving problems with large
constraints through appropriate algorithm design
Each problem is:
● Clearly described
● Solved using an appropriate algorithmic strategy
● Implemented in Python
● Structured to take input and produce output interactively
for testing
The solutions are written with clarity, and correctness is
validated against sample inputs. This report is intended to
serve both as documentation and a learning reference for key
algorithmic paradigms used in computer science.
Advanced Algorithm Assignment 4
1. Dynamic Programming
🔢 1.1 Primitive Calculator – Dynamic Programming
📝 Problem:
Given an integer n, compute the minimum number of
operations to reach it from 1 using:
● Multiply by 2
● Multiply by 3
● Add 1
✅ Approach:
We use dynamic programming to build the
minimal steps for all numbers up to n, tracking how
we got to each number.
📎 Python Pseudocode:
``` def primitive_calculator(n):
ops = [0] * (n+1)
path = [0] * (n+1)
for i in range(2, n+1):
min_ops = ops[i-1]
path[i] = i-1
Advanced Algorithm Assignment 5
if i % 2 == 0 and ops[i//2] < min_ops:
min_ops = ops[i//2]
path[i] = i//2
if i % 3 == 0 and ops[i//3] < min_ops:
min_ops = ops[i//3]
path[i] = i//3
ops[i] = min_ops + 1
result_path = []
while n > 0:
result_path.append(n)
n = path[n]
return ops[result_path[0]], reversed(result_path) ```
📌 Sample Output:
Input: 5 → Output: 3 operations, path: 1 → 2 → 4 →
5
🔢 1.2 Minimum Dot Product – Dynamic
Programming
Advanced Algorithm Assignment 6
📝 Problem:
Find a permutation of B to minimize the dot product with A.
✅ Approach:
Sort A ascending and B descending and multiply
pairwise.
📎 Python Pseudocode:
``` A.sort()
B.sort(reverse=True)
dot_product = sum(A[i] * B[i] for i in range(n)) ```
📌 Sample Output:
Input: A = [1, 3, -5], B = [-2, 4, 1] → Output: -
25
25
🎒 1.3 0/1 Knapsack – Dynamic Programming
📝 Problem:
Given n items with weights and values, select
a subset so that total weight ≤ W and total
value is maximized.
✅ Approach:
Use 1D bottom-up DP to compute max value for
every capacity up to W.
📎 Python Pseudocode:
Advanced Algorithm Assignment 7
``` dp = [0] * (W + 1)
for i in range(n):
for w in range(W, weight[i] - 1, -1):
dp[w] = max(dp[w], dp[w - weight[i]] +
value[i]) ```
📌 Sample Output:
Input: n=3, W=50 → Output: 220
💰 2.1 Fractional Knapsack – Greedy
Algorithm
📝 Problem:
Maximize value of items (fractions allowed) in
a knapsack of capacity W.
✅ Approach:
Sort by value/weight ratio. Take as much as
possible from highest ratio items.
📎 Python Pseudocode:
``` items.sort(key=lambda x: x.value /
x.weight, reverse=True)
for item in items:
if W == 0: break
take = min(W, item.weight)
Advanced Algorithm Assignment 8
total += take * item.value/item.weight
W -= take ```
📌 Sample Output:
Input: [(60,20), (100,50), (120,30)] → Output:
180.0000
🗓 2.2 Activity Selection – Greedy
Algorithm
📝 Problem:
Select max number of non-overlapping activities
based on their start and end times.
✅ Approach:
Sort activities by end time and greedily choose
the next non-conflicting one.
📎 Python Pseudocode:
```activities.sort(key=lambda x: x.end)
last_end = 0
count = 0
for act in activities:
if act.start >= last_end:
count += 1
last_end = act.end```
Advanced Algorithm Assignment 9
📌 Sample Output:
Input: [(1,3), (2,5), (4,6), (6,7), (5,8), (8,9)]
→ Output: 4
🧮 2.3 Huffman Coding – Greedy Algorithm
📝 Problem:
Build a binary tree to encode characters such
that total cost is minimized.
✅ Approach:
Use a min-heap. Repeatedly combine two
lowest-frequency nodes until one tree remains.
📎 Python Pseudocode:
``` heapq.heapify(freq)
while len(freq) > 1:
a = heapq.heappop(freq)
b = heapq.heappop(freq)
heapq.heappush(freq, a + b)
cost += a + b ```
📌 Sample Output:
Input: [5, 9, 12, 13] → Output: 78
🌐 3.1 DFS – Graph Traversal
Advanced Algorithm Assignment 10
📝 Problem:
Perform DFS traversal from a given starting
node and print the visit order.
✅ Approach:
Use recursion with a visited set to avoid cycles.
📎 Python Pseudocode:
``` def dfs(v):
visited.add(v)
order.append(v)
for neighbor in graph[v]:
if neighbor not in visited:
dfs(neighbor)```
📌 Sample Output:
Input: Start 1 → Output: 1 2 5 3 4
🧭 3.2 Dijkstra's Algorithm – Shortest
Paths
📝 Problem:
Find shortest path from a source node to all
others in a weighted graph.
✅ Approach:
Use a min-heap to relax edge weights greedily.
Advanced Algorithm Assignment 11
📎 Python Pseudocode:
``` dist[source] = 0
heapq.heappush(heap, (0, source))
while heap:
d, u = heapq.heappop(heap)
for neighbor, weight in graph[u]:
if dist[u] + weight < dist[neighbor]:
dist[neighbor] = dist[u] + weight
heapq.heappush(heap,
(dist[neighbor], neighbor))```
📌 Sample Output:
Input: Start = 1 → Output: 0 2 3 4 5
🌲 3.3 Prim’s Algorithm – Minimum Spanning
Tree
📝 Problem:
Find the MST of a graph — minimize total edge
cost covering all vertices.
✅ Approach:
Use a priority queue to always add the lightest
edge that connects a new node.
📎 Python Pseudocode:
Advanced Algorithm Assignment 12
``` heapq.heappush(min_heap, (0, start))
while min_heap:
weight, u = heapq.heappop(min_heap)
if u in visited: continue
total_weight += weight
visited.add(u)
for v, w in graph[u]:
if v not in visited:
heapq.heappush(min_heap, (w, v))```
📌 Sample Output:
Input: Edge list → Output: Total MST Cost = 7
Final Summary
● Dynamic Programming: Optimal substructure
used in calculator and knapsack.
● Greedy: Efficient for activity selection,
Huffman coding, dot products, and
fractional items.
● Graphs: DFS explores, Dijkstra finds
shortest paths, and Prim builds MSTs.
Advanced Algorithm Assignment 13