Aad End Sem
Aad End Sem
Design
Answer:
An algorithm is a finite set of well-defined instructions used to solve a particular problem or
perform a computation. It takes some input, processes it, and produces an output.
Algorithms are the foundation of computer programs.
5. Effectiveness: All operations must be basic enough to be done manually if required.
6. Generality: Should work for a set of inputs, not just one case.
Advantages of algorithms:
● Debugging & Maintenance: Easier to test and update compared to raw code.
Example:
Binary Search Algorithm is an efficient algorithm used to search for a value in a sorted
array.
3. Design Automation: Generative design software like Autodesk Fusion uses
algorithms to create and evaluate design options.
4. User Flow Optimization: Algorithms analyze user behavior to streamline UI/UX
flows.
Real-life example:
In a fitness app, algorithms track user activity and suggest workouts using data like age,
weight, and goal.
(4 marks)
Answer:
Machine Learning (ML) in product design is used to build intelligent and adaptive
systems that learn from user data and improve the user experience automatically.
1. User Behavior Prediction: ML models analyze how users interact with a product to
suggest features or improvements.
2. Image and Voice Recognition: Products like Face ID or Alexa use ML for biometric
access or voice commands.
3. Sentiment Analysis: Analyzing user feedback or reviews to guide future design
decisions.
4. Augmented Design Tools: ML-powered tools help designers generate layouts, color
palettes, or UX flows automatically.
Example:
Instagram uses ML to reorder your feed based on your interaction patterns — likes, time
spent on posts, and comments.
Answer:
A recurrence relation is a mathematical expression that defines each term of a sequence
based on one or more previous terms.
General form:
T(n) = aT(n/b) + f(n)
Where:
1. Linear Recurrence: Involves only the previous one or two terms.
E.g., Fibonacci: F(n) = F(n-1) + F(n-2)
Example:
In Merge Sort, the recurrence relation is:
T(n) = 2T(n/2) + O(n)
Using the Master Theorem, this gives O(n log n) complexity.
SECTION 2: Divide and Conquer & Greedy Technique
🔹 Q5: Explain the Divide and Conquer technique with a real-life and
algorithmic example.
(4 marks)
Answer:
Divide and Conquer is an algorithmic paradigm that solves a problem by recursively
dividing it into smaller sub-problems, solving each sub-problem independently, and then
combining the results.
Steps:
3. Combine: Merge the solutions of sub-problems to form the solution of the original
problem.
Real-life example:
Sorting books by category: First divide by genre, then by author within each genre, then sort
titles alphabetically within author groups. Finally, combine them into a complete organized
library.
Answer:
A Greedy Algorithm builds a solution step-by-step by choosing the locally optimal option at
each stage, assuming this will lead to the global optimum.
Key idea:
● Does not guarantee the best solution for all problems but works well for some.
Note: This works only if coin denominations are canonical (standard). Otherwise, Greedy
may not be optimal.
Other Examples:
Answer:
Strategy Divide problem recursively Take best local option at each step
Complexity Often higher (e.g. O(n log n)) Usually faster (e.g. O(n log n) for sort)
Summary:
Divide and Conquer is powerful for solving large problems with recursive structure, while
Greedy works best when local choices lead to a global solution.
SECTION 3: Dynamic Programming, Knapsack,
Fibonacci
(4 marks)
Answer:
Dynamic Programming (DP) is a technique for solving problems by breaking them into
overlapping subproblems, solving each subproblem just once, and storing its result for future
reference (memoization or tabulation).
Key Characteristics:
1. Optimal Substructure: Solution of the problem can be built from optimal solutions of
subproblems.
2. Overlapping Subproblems: The same subproblems are solved multiple times.
Real-life analogy:
Climbing stairs: To reach the 5th step, you can come from the 4th or 3rd step. If you already
know ways to reach 4 and 3, you can build the result for 5 quickly.
Answer:
In the 0/1 Knapsack problem, we are given:
● A knapsack of capacity W
● Objective: Maximize the value without exceeding the weight, and you either pick an
item or leave it (0/1 choice).
DP Approach:
● Define a DP table dp[i][j] = max value using first i items and capacity j
Recurrence relation:
Example:
Items: (value, weight) → (60, 10), (100, 20), (120, 30)
Capacity = 50
Max value = 220 by picking (100, 20) and (120, 30)
(4 marks)
Answer:
Selection Either take whole item or Can take a fraction of the item
not
Optimality with Not always optimal with Always optimal with greedy
Greedy greedy
Time Complexity O(nW) O(n log n) (after sorting by
value/weight)
Example:
Answer:
Memoization is a top-down dynamic programming technique where results of subproblems
are stored in a dictionary or array to avoid repeated calculations.
if n <= 1:
return n
if n not in memo:
return memo[n]
Benefits:
Answer:
Prim’s Algorithm is a greedy algorithm used to find the Minimum Spanning Tree (MST) of
a connected, weighted graph. It builds the MST by growing one vertex at a time, always
choosing the edge with the minimum weight that connects a vertex inside the MST to a
vertex outside.
Steps:
3. At each step, pick the smallest-weight edge that connects to an unvisited node.
4. Add that node to the MST and repeat until all vertices are included.
Time Complexity:
● O(E log V) using priority queue and adjacency list (with Min Heap)
Example:
For graph with edges:
Answer:
Kruskal’s Algorithm is a greedy algorithm to find the Minimum Spanning Tree (MST) by
always choosing the smallest weight edge that doesn’t form a cycle.
Steps:
3. For each edge, use Disjoint Set (Union-Find) to check if adding it forms a cycle.
Time Complexity:
Example:
Edges:
Answer:
Conclusion:
Both give MST, but use different approaches. Prim is vertex-based; Kruskal is edge-based.
Answer:
Dijkstra’s Algorithm finds the shortest path from a single source to all other vertices in a
weighted graph with non-negative weights.
Steps:
2. Use a priority queue (min-heap) to extract vertex with the minimum distance.
3. For each neighbor of current vertex, update the distance if a shorter path is found.
Time Complexity:
Example:
Graph:
● A → B = 2
● A → C = 3 (via B)
● A → D = 6 (via C)
Answer:
Floyd-Warshall Algorithm is a Dynamic Programming algorithm used to find shortest
paths between all pairs of vertices in a weighted graph (positive or negative weights, but
no negative cycles).
Steps:
Example:
Initial matrix:
A B C
A 0 3 ∞
B ∞ 0 1
C ∞ ∞ 0
(4 marks)
Answer:
The Activity Selection Problem involves selecting the maximum number of
non-overlapping activities that a single person or machine can perform, given their start and
finish times. The Greedy Approach solves this by always choosing the activity that finishes
the earliest.
Steps:
3. For the rest, pick only those whose start time is ≥ finish time of last selected activity.
Time Complexity:
Example:
Activities (start, finish):
A(1, 3), B(2, 5), C(4, 6), D(6, 8), E(5, 9), F(8, 9)
Sorted by finish time → A, B, C, D, E, F
Selected: A, C, D, F
Max activities: 4
(4 marks)
Answer:
The Greedy method is used in the Activity Selection problem because:
1. Local Optimal → Global Optimal:
Choosing the activity that ends earliest ensures more time is left for future activities.
4. No Overlaps:
Selecting earliest finish time avoids overlaps and maximizes count of selected
activities.
Conclusion:
Greedy works here because the problem has optimal substructure and greedy choice
property.
SECTION 6: Backtracking
Answer:
Backtracking is a problem-solving technique that builds a solution step by step and
abandons a path as soon as it determines that it will not lead to a valid solution. It’s a
depth-first search over possible solution spaces.
Used when:
Key Steps:
Answer:
The 4-Queens Problem requires placing 4 queens on a 4×4 board so that no two queens
threaten each other.
Steps:
2. Move to next row, place queen in a column not attacked by previous queens.
One Solution:
[. Q . .]
[. . . Q]
[Q . . .]
[. . Q .]
Answer:
The Subset Sum Problem asks whether a subset of a given set of integers adds up to a
given target sum.
Using Backtracking:
Example:
Set = [3, 34, 4, 12, 5, 2], Target = 9
Subset = [4, 5] is a valid solution.
Steps:
(4 marks)
Answer:
Dynamic Programming (DP) is a technique used to solve optimization problems by
breaking them down into overlapping subproblems and solving each subproblem only
once, storing the result for reuse.
2. Optimal Substructure – Optimal solution can be formed from optimal sub-solutions.
(4 marks)
Answer:
In the 0/1 Knapsack problem, you are given weights and profits of items, and a knapsack
with a max weight capacity W. The goal is to maximize profit without exceeding the weight,
and each item can be taken or not (0/1).
DP Approach:
Recurrence Relation:
matlab
Copy code
= dp[i-1][w] otherwise
Initialization:
dp[0][w] = 0 for all w (0 items)
dp[i][0] = 0 for all i (0 weight)
● Time: O(nW)
● Space: O(nW)
Answer:
Create a 2D table dp[4][51]. Fill based on recurrence:
Answer:
Matrix Chain Multiplication finds the most efficient way to multiply a chain of matrices by
determining the optimal parenthesization that minimizes the number of scalar
multiplications.
Key Idea:
Matrix multiplication is associative, so order matters for cost but not result.
DP Approach:
Initialization:
m[i][i] = 0 (one matrix, no multiplication)
● Time: O(n³)
● Space: O(n²)
Example:
X = "ABCDGH", Y = "AEDFHR" → LCS = "ADH", Length = 3
DP Table Construction:
Recurrence:
if X[i-1] == Y[j-1]:
dp[i][j] = 1 + dp[i-1][j-1]
else:
Initialization:
dp[0][j] = 0, dp[i][0] = 0 (if one string is empty, LCS is 0)
● Time: O(m×n)
● Space: O(m×n)
SECTION 5: Graph Algorithms
Answer:
Breadth-First Search (BFS) is a graph traversal algorithm that explores vertices level by
level, using a queue. It starts at a source node and visits all neighbors before moving to the
next level.
How it works:
Example (Graph):
/ \
B C
/ \
D E
Applications:
Answer:
Depth-First Search (DFS) is a traversal method that explores as deep as possible along
each branch before backtracking. It uses a stack (or recursion).
How it works:
Example (Graph):
/ \
B C
/ \
D E
Applications:
● Topological sort
● Cycle detection
● O(V + E)
(4 marks)
Answer:
Prim’s Algorithm finds the Minimum Spanning Tree (MST) of a connected, weighted
graph, using a greedy approach.
Steps:
2. Add the minimum weight edge that connects a visited node to an unvisited node.
Example:
Graph edges:
Time Complexity:
Answer:
Kruskal’s Algorithm builds a Minimum Spanning Tree (MST) by always adding the
smallest edge that doesn’t form a cycle, using a greedy approach.
Steps:
2. Use Disjoint Set (Union-Find) to check if adding an edge forms a cycle.
Kruskal’s Prim’s
Time Complexity:
Answer:
Dijkstra’s Algorithm finds the shortest path from a source vertex to all other vertices in a
weighted graph with non-negative weights.
Steps:
2. Use Priority Queue to pick the node with minimum distance.
Example:
Graph:
Time Complexity:
Applications:
● GPS systems
● Network routing