DAA Unit-4 Notes
DAA Unit-4 Notes
AcademicArk
https://academicark-mvp8.onrender.com/
Contents
1 Dynamic Programming 4
rk
1 academicark
BCS503 - DAA Unit-4 AKTU
5 Backtracking 12
5.1 General Framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
5.2 Key Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
6 N-Queens Problem 13
6.1 Problem Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
6.2 Constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
6.3 Backtracking Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
6.4 Time Complexity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2 academicark
BCS503 - DAA Unit-4 AKTU
16 Practice Problems 31
16.1 Problem 1: Modified Knapsack . . . . . . . . . . . . . . . . . . . . . . . 31
16.2 Problem 2: Floyd-Warshall . . . . . . . . . . . . . . . . . . . . . . . . . . 31
16.3 Problem 3: N-Queens . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
16.4 Problem 4: TSP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
16.5 Problem 5: Graph Coloring . . . . . . . . . . . . . . . . . . . . . . . . . 31
16.6 Problem 6: Sum of Subsets . . . . . . . . . . . . . . . . . . . . . . . . . . 31
16.7 Problem 7: Resource Allocation . . . . . . . . . . . . . . . . . . . . . . . 31
3 academicark
BCS503 - DAA Unit-4 AKTU
1 Dynamic Programming
Definition
Dynamic Programming is an algorithmic paradigm that solves complex prob-
lems by breaking them down into simpler subproblems and storing the results of
subproblems to avoid redundant computations. It is particularly useful when the
problem exhibits overlapping subproblems and optimal substructure properties.
store results.
ac
4 academicark
BCS503 - DAA Unit-4 AKTU
Objective: Maximize the total value of items in the knapsack without exceeding
its capacity, where each item can be either included (1) or excluded (0).
0
if i = 0 or w = 0
ica
max(dp[i − 1][w], vi + dp[i − 1][w − wi ]) if wi ≤ w
e
ad
Algorithm
ac
5 academicark
BCS503 - DAA Unit-4 AKTU
Example
• Knapsack Capacity: W = 5 kg
Item/Weight 0 1 2 3 4 5
0 (No items) 0 0 0 0 0 0
1 (w=2, v=3) 0 0 3 3 3 3
2 (w=3, v=4) 0 0 3 4 4 7
3 (w=4, v=5) 0 0 3 4 5 7
4 (w=5, v=6) 0 0 3 4 5 7
Important Note
e m
Algorithm
6 academicark
BCS503 - DAA Unit-4 AKTU
Algorithm
rk
Floyd-Warshall Algorithm:
ica
1: procedure FloydWarshall(graph[][], n)
m
3: for i ← 1 to n do
ad
4: dist[i][i] ← 0
ac
5: end for
6: for k ← 1 to n do ▷ Intermediate vertex
7: for i ← 1 to n do ▷ Source vertex
8: for j ← 1 to n do ▷ Destination vertex
9: if dist[i][k] ̸= ∞ AND dist[k][j] ̸= ∞ then
10: dist[i][j] ← min(dist[i][j], dist[i][k] + dist[k][j])
11: end if
12: end for
13: end for
14: end for
15: return dist[][]
16: end procedure
7 academicark
BCS503 - DAA Unit-4 AKTU
Example
Example: Floyd-Warshall Algorithm
Consider a graph with 4 vertices and the following adjacency matrix:
Initial Distance Matrix:
1 2 3 4
1 0 4 ∞ 5
2 ∞ 0 1 ∞
3 2 ∞ 0 3
4 ∞ ∞ 1 0
1 2 3 4
1 0 4 5 5
2 3 0 1 4
3 2 6 0 3
4 3 7 1 0
Important Note
ica
3.3 Applications
• Network routing protocols
8 academicark
BCS503 - DAA Unit-4 AKTU
Algorithm
Warshall’s Algorithm:
1: procedure Warshall(adjM atrix[][], n)
2: Initialize reach[][] ← adjM atrix[][]
3: for i ← 1 to n do
4: reach[i][i] ← 1 ▷ Vertex reachable to itself
5: end for
6: for k ← 1 to n do ▷ Intermediate vertex
7: for i ← 1 to n do
8: for j ← 1 to n do
rk
Example
Example: Warshall’s Algorithm
Given directed graph with adjacency matrix:
Initial Adjacency Matrix:
1 2 3 4
1 1 1 0 0
2 0 1 1 0
3 0 0 1 1
4 0 0 0 1
9 academicark
BCS503 - DAA Unit-4 AKTU
j=0,1,...,x
ac
Example
Example: Resource Allocation Problem
Suppose 4 doctors are to be allocated to 3 hospitals. The table shows patients
served per hour:
10 academicark
BCS503 - DAA Unit-4 AKTU
rk
ica
e m
ad
ac
11 academicark
BCS503 - DAA Unit-4 AKTU
5 Backtracking
Definition
Backtracking is an algorithmic technique that systematically searches for solu-
tions by building candidates incrementally and abandoning a candidate (backtrack-
ing) as soon as it is determined that the candidate cannot lead to a valid solution.
11: end if
end for
m
12:
e
12 academicark
BCS503 - DAA Unit-4 AKTU
6 N-Queens Problem
6.1 Problem Statement
Definition
Place N queens on an N × N chessboard such that no two queens attack each
other. Queens attack along rows, columns, and diagonals.
6.2 Constraints
• No two queens in the same row
2: if row == n then
ica
end if
e
5:
ad
6: for col ← 0 to n − 1 do
ac
13 academicark
BCS503 - DAA Unit-4 AKTU
rk
ica
e m
ad
ac
14 academicark
BCS503 - DAA Unit-4 AKTU
Example
Example: 4-Queens Problem
Solution 1:
. Q . .
. . . Q
Q . . .
. . Q .
• Level 1 (Row 1): For each valid placement in row 0, try columns in row 1
Steps:
7. Solution found!
15 academicark
BCS503 - DAA Unit-4 AKTU
8:
9: if GraphColoring(graph, m, color, v + 1, V ) then
ica
11: end if
e
ad
13:
14: end for
15: return false
16: end procedure
17:
18: procedure isSafe(graph[][], v, color[], c, V )
19: for i ← 0 to V − 1 do
20: if graph[v][i] == 1 AND color[i] == c then
21: return false ▷ Adjacent vertex has same color
22: end if
23: end for
24: return true
25: end procedure
Example
Example: Graph Coloring with 3 Colors
Consider a graph with 4 vertices:
Adjacency Matrix:
16 academicark
BCS503 - DAA Unit-4 AKTU
0 1 2 3
0 0 1 1 1
1 1 0 1 0
2 1 1 0 1
3 1 0 1 0
Solution:
• Vertex 0: Color 1
• Vertex 1: Color 2
• Vertex 2: Color 3
• Vertex 3: Color 2
Chromatic number = 3
rk
ica
m
e
ad
ac
17 academicark
BCS503 - DAA Unit-4 AKTU
8: end if
for v ← 1 to V − 1 do
ica
9:
10: if isSafe(v, graph, path, pos) then
m
11: path[pos] ← v
e
ad
18 academicark
BCS503 - DAA Unit-4 AKTU
Example
Example: Hamiltonian Cycle
Graph with 5 vertices:
Edges: (0,1), (1,2), (2,3), (3,4), (4,0), (0,2), (2,4)
Hamiltonian Cycle: 0 → 1 → 2 → 3 → 4 → 0
Path Construction:
1. Start at vertex 0
19 academicark
BCS503 - DAA Unit-4 AKTU
9:
10: SumOfSubsets(w, subset, sum + w[i], remaining − w[i], i + 1, n, M )
ica
Example
Example: Sum of Subsets
Given:
• Set: S = {3, 5, 6, 7, 8}
• Target sum: M = 15
Solutions:
20 academicark
BCS503 - DAA Unit-4 AKTU
rk
ica
em
ad
ac
21 academicark
BCS503 - DAA Unit-4 AKTU
Algorithm
ac
22 academicark
BCS503 - DAA Unit-4 AKTU
rk
ica
me
ad
ac
23 academicark
BCS503 - DAA Unit-4 AKTU
Algorithm
rk
3:
4: minCost ← ∞
ac
24 academicark
BCS503 - DAA Unit-4 AKTU
Example
Example: TSP using Branch and Bound
Cost Matrix:
A B C D
A ∞ 10 15 20
B 10 ∞ 35 25
C 15 35 ∞ 30
D 20 25 30 ∞
Total reduction = 55
Step 2: Column Reduction After row reduction, check columns. No further
reduction needed.
Step 3: Branching Start from A, explore edges A→B, A→C, A→D with their
rk
respective bounds.
ica
Important Note
11.4 Applications
• Vehicle routing
• DNA sequencing
• Network design
25 academicark
BCS503 - DAA Unit-4 AKTU
Important Note
Algorithm
Knapsack Branch and Bound:
1: procedure Knapsack BB(W, wt[], val[], n)
2: Sort items by value/weight ratio (descending)
3: Initialize queue with root node
4: maxP rof it ← 0
5: while queue not empty do
6: node ← extract maximum bound node
7: if node.bound > maxP rof it then
8: if node.level == n then
9: Update maxP rof it
rk
10: else
ica
Example
26 academicark
BCS503 - DAA Unit-4 AKTU
rk
ica
m
e
ad
ac
27 academicark
BCS503 - DAA Unit-4 AKTU
• Memory is limited
28 academicark
BCS503 - DAA Unit-4 AKTU
14.3 Backtracking
• Systematic search with pruning of non-promising branches
rk
ica
29 academicark
BCS503 - DAA Unit-4 AKTU
15.2.2 Floyd-Warshall
dist[i][j](k) = min(dist[i][j](k−1) , dist[i][k](k−1) + dist[k][j](k−1) )
rk
ica
j=0,1,...,x
ad
ac
30 academicark
BCS503 - DAA Unit-4 AKTU
16 Practice Problems
16.1 Problem 1: Modified Knapsack
A thief has a knapsack of capacity 15 kg. There are 4 items with the following weights
and values: (w=5,v=10), (w=4,v=40), (w=6,v=30), (w=3,v=50). Find the maximum
value the thief can carry using dynamic programming.
Color a graph with 6 vertices using minimum colors. The graph represents a map where
m
31 academicark
BCS503 - DAA Unit-4 AKTU
32 academicark
BCS503 - DAA Unit-4 AKTU
rk
ica
em
ad
ac
33 academicark
BCS503 - DAA Unit-4 AKTU
https://academicark-mvp8.onrender.com/
rk
ica
m
e
ad
ac
34 academicark