AaDS Report
AaDS Report
Table of Content
Introduction
3.0 Graph
7
🌐 3.1 DFS – Graph Traversal 8
Final Summary 11
Advanced Algorithm Assignment 3
📘 Introduction
1. Dynamic Programming
✅ 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:
ops[i] = min_ops + 1
result_path = []
while n > 0:
result_path.append(n)
n = path[n]
📌 Sample Output:
📝 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
📎 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
📎 Python Pseudocode:
if W == 0: break
W -= take ```
📌 Sample Output:
✅ 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
count += 1
last_end = act.end```
Advanced Algorithm Assignment 9
📌 Sample Output:
📎 Python Pseudocode:
``` heapq.heapify(freq)
a = heapq.heappop(freq)
b = heapq.heappop(freq)
heapq.heappush(freq, a + b)
cost += a + b ```
📌 Sample Output:
📝 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:
visited.add(v)
order.append(v)
dfs(neighbor)```
📌 Sample Output:
📎 Python Pseudocode:
``` dist[source] = 0
while heap:
d, u = heapq.heappop(heap)
heapq.heappush(heap,
(dist[neighbor], neighbor))```
📌 Sample Output:
📎 Python Pseudocode:
Advanced Algorithm Assignment 12
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:
📌 Sample Output:
Final Summary
● Dynamic Programming: Optimal substructure
used in calculator and knapsack.