2 Marks DAA
2 Marks DAA
Comparison: For large values of n, the O(n2) function grows significantly faster than the O(n)
function. Therefore, an algorithm with a running time of n(n-1)/2 is much less efficient for large
inputs than an algorithm with a running time of n.
Method: We list all possible tours (Hamiltonian cycles), calculate their total distance, and find
the minimum. Let's start from city 0.
1. Tour 1: 0 → 1 → 2 → 3 → 0
○ Cost: 20 + 15 + 17 + 10 = 62
2. Tour 2: 0 → 1 → 3 → 2 → 0
○ Cost: 20 + 11 + 17 + 12 = 60
3. Tour 3: 0 → 2 → 1 → 3 → 0
○ Cost: 12 + 15 + 11 + 10 = 48
The other possible tours are reversals of these and have the same cost.
Optimal Solution: The minimum cost is 48. The optimal path is 0 → 2 → 1 → 3 → 0 (or its
reverse 0 → 3 → 1 → 2 → 0).
Method:
1. T(n)=T(n−1)+1
2. Substitute for T(n−1): T(n)=[T(n−2)+1]+1=T(n−2)+2
3. Substitute for T(n−2): T(n)=[T(n−3)+1]+2=T(n−3)+3
4. Generalize the pattern after k substitutions: T(n)=T(n−k)+k
5. Let k = n-1 to reach the base case T(1): T(n)=T(n−(n−1))+(n−1) T(n)=T(1)+n−1
6. Using the base case T(1)=1: T(n)=1+n−1=n
[Problem of size n]
/ \
[Sub-problem n/2] [Sub-problem n/2] <-- Divide
/ \ / \
... ... ... ... <-- Conquer (Recursive calls)
\ / \ /
[Solved n/2] [Solved n/2]
\ /
[Solution for size n] <-- Combine
Parameters:
● a=4
● b=2
● f(n)=n2
Analysis:
1. Calculate nlogba=nlog24=n2.
2. Compare f(n) with nlogba. In this case, f(n)=n2 is asymptotically equal to n2.
3. This corresponds to Case 2 of the Master Theorem, where f(n)=Θ(nlogba).
Solution:
Therefore, T(n)=Θ(n2logn).
FAT 2
Key Differences:
Execution Solves only the subproblems that Solves all subproblems, starting
are actually needed. from the smallest.
A negative edge could create a "shortcut" to a vertex that has already been marked as visited,
but the algorithm will not re-evaluate it.
Example:
● Edges: A -> C (cost 5), A -> B (cost 3), B -> C (cost -4).
Dijkstra's running from A would first finalize the path to B with cost 3. It would then see the path
A -> C with cost 5. The algorithm would incorrectly conclude the shortest path to C is 5, because
it will not correctly process the path A -> B -> C which has a total cost of 3 + (-4) = -1.
(55)
/ \
(30) (25)
/ \ / \
(14) E:16 C:12 D:13
/ \
A:5 B:9
| 1 | A | (empty) | 0 |
| 2 | AB | (empty) | 0 |
| 3 | ABA | A | 1 |
| 4 | ABAB | AB | 2 |
| 5 | ABABA | ABA | 3 |
| 6 | ABABAB | ABAB | 4 |
| 7 | ABABABC | (empty) | 0 |
FAT 3
Of course. Here are the answers to the questions from your exam paper.
This transformation is essential for using the Simplex algorithm, which is designed to solve
systems of linear equations rather than inequalities. For each constraint like a1x1+a2x2≤b, a
non-negative slack variable s is added to the left side, resulting in the equation a1x1+a2x2+s=b.
Maximize z=3x1+2x2
Subject to:
x1+4x2≤6
To prepare the objective function for the Simplex method, it is written in a standard canonical
form where all variables are on one side, set equal to zero.
Standard Form:
z−3x1−2x2=0
1. 3x1+2x2≤5
2. x2≤2
3. x1,x2≥0
Steps:
1. Plot the constraints: Draw the lines 3x1+2x2=5 and x2=2.
2. Identify the Feasible Region: The feasible region is the polygon formed by the
intersection of all constraints, including x1≥0 and x2≥0.
3. Find Corner Points: The vertices (corners) of this feasible region are:
○ A = (0, 0)
○ B = (5/3, 0)
○ C = (1/3, 2) ← Intersection of 3x1+2x2=5 and x2=2
○ D = (0, 2)
4. Evaluate Objective Function: Check the value of Z=x1+x2at each corner point:
○ At A(0, 0): Z = 0 + 0 = 0
○ At B(5/3, 0): Z = 5/3 + 0 ≈ 1.67
○ At D(0, 2): Z = 0 + 2 = 2
○ At C(1/3, 2): Z = 1/3 + 2 = 7/3 ≈ 2.33 (Maximum)
Solution: The maximum value of Z is 7/3, which occurs at the point (x1=1/3, x2=2).
Max: z=45x1+60x2
Subject to:
● x1≤45
● x2≤50
● 10x1+10x2≥600 (or x1+x2≥60)
● 25x1+5x2≤750 (or 5x1+x2≤150)
● x1,x2≥0
The feasible region is the geometric space (a polygon) on the 2D plane that satisfies all the
given constraints simultaneously. It is bounded by the lines x1=45, x2=50, x1+x2=60, and
5x1+x2=150, within the first quadrant. Any point within or on the boundary of this polygon
represents a valid solution to the LPP.
5. Line Segment Properties in Computational Geometry
In computational geometry, line segments have several key properties used in algorithms:
● Representation: A line segment is defined by its two endpoints, e.g., P1(x1,y1) and
P2(x2,y2).
● Intersection: A crucial property is whether two line segments intersect. This is
determined algorithmically using orientation tests.
● Orientation: Given an ordered triplet of points (p, q, r), their orientation can be
determined as collinear, clockwise (right turn), or counter-clockwise (left turn). This
is the fundamental building block for intersection tests and convex hull algorithms.
The key difference is that a circuit can be much more compact than an equivalent formula
because it can reuse the output of gates, whereas a formula might have to repeat
sub-expressions.
....Q...
......Q.
.Q......
.....Q..
..Q.....
Q.......
...Q....
.......Q
The queen positions (row, column) are: (0,4), (1,6), (2,1), (3,5), (4,2), (5,0), (6,3), (7,7).
function solveNQueens(N):
// Initialize an N x N board with empty squares
board = create_board(N)
print_solution(board)
// RECURSIVE STEP: Try placing a queen in each row of the current column
for row from 0 to N-1:
// Check if it's safe to place a queen at board[row][col]
if isSafe(board, row, col, N):
// Place the queen
board[row][col] = 'Q'
// isSafe() function checks the row, upper-left diagonal, and lower-left diagonal
The decision version of this problem ("Does a clique of size k exist?") is a classic NP-complete
problem, meaning it is considered computationally hard to solve for large graphs.
PREV SEM
Algorithm Steps:
1. Given two non-negative integers, m and n.
2. If n is 0, the GCD is m.
3. Otherwise, the GCD is the result of gcd(n, m % n), where m % n is the remainder of m
divided by n.
Pseudocode:
function fibonacci(n):
if n <= 1:
return n
else:
return fibonacci(n-1) + fibonacci(n-2)
Recurrence Relation:
6. Closest-Pair Problem
The closest-pair problem is a fundamental problem in computational geometry. Given n points
in a metric space (like a 2D plane), the objective is to find the pair of points that have the
smallest distance between them.
a. T(n) = 4T(n/2) + n
● a=4, b=2, f(n)=n.
● We compare f(n) with nlogba=nlog24=n2.
● Since f(n)=n=O(n2−ϵ) for ϵ=1, this falls into Case 1.
● Order of growth: Θ(n2).
b. T(n) = 4T(n/2) + n²
● a=4, b=2, f(n)=n2.
● We compare f(n) with nlogba=nlog24=n2.
● Since f(n)=n2=Θ(n2), this falls into Case 2.
● Order of growth: Θ(n2logn).
function Huffman(C):
// C is a set of characters with their frequencies
n = number of characters in C
Q = a min-priority queue initialized with characters from C
for i from 1 to n:
for w from 1 to W:
if weights[i-1] <= w:
// Item i can fit. Choose max of taking it vs. not taking it.
V[i][w] = max(values[i-1] + V[i-1][w - weights[i-1]], V[i-1][w])
else:
// Item i cannot fit, so we don't take it.
V[i][w] = V[i-1][w]
return V[n][W]
This network is crucial for algorithms like Ford-Fulkerson to find augmenting paths.
n!>3n>2n>n3>n2>nlogn>n>logn
Time O(n log n) in all cases (best, O(n log n) on average, but
Complexity average, worst). O(n²) in the worst case.
Primary Work Most of the work is done in the Most of the work is done in
merge step (combining). the partition step (dividing).
Using a standard partitioning scheme (like Lomuto's), the goal is to place all elements smaller
than 7 to its left and all larger elements to its right.
○ Air Traffic Control: Detecting aircraft that are too close to each other.
○ Computer Vision: Used in pattern recognition and object clustering.
○ Molecular Modeling: Finding the closest pair of atoms in a molecule.
● Convex Hull Problem Applications:
○ Collision Avoidance: Calculating the path for a robot to move without hitting
obstacles.
○ Image Processing: Identifying the shape or boundary of an object in an image.
○ Geographic Information Systems (GIS): Defining the boundary of a set of
geographical points.
Strategy Builds the MST by adding Builds the MST by adding the
vertices to a single tree. safest edges.
Best For Dense graphs (many edges). Sparse graphs (few edges).
if
/ \
do stop
2.
3.
4. do as root (right-skewed):
do
\
if
\
stop
5.
6.
7. do as root (mixed):
do
\
stop
/
if
8.
9.
10.stop as root (left-skewed):
stop
/
if
/
do
11.
12.
13.stop as root (mixed):
stop
/
do
\
if
14.
15.
The residual network represents the available capacity for sending more flow. The
Ford-Fulkerson algorithm works by repeatedly finding an augmenting path and pushing the
maximum possible flow along it until no more such paths can be found.
Dual Problem:
● Maximize W=3y1+5y2
● Subject to:
○ y1+2y2≤4
○ y1−2y2≤2
○ 2y1+4y2≤−1
● And variable constraints: y1≥0, y2≤0.
4-Queens Solution:
One of the two possible solutions places queens at coordinates (1,0), (3,1), (0,2), and (2,3).
..Q.
Q...
...Q
.Q..
Portion of the State-Space Tree:
The tree shows starting with a queen at (0,0), which leads to a dead end, forcing a backtrack.
(Start)
|
-----------------
| (Q at (0,0)) | (Q at (1,0)) -- (Leads to a solution)
-----------------
|
(Col 1: can't place)
|
(DEAD END)
The Branch and Bound technique solves this by exploring a state-space tree where each node
represents a decision (take or leave an item). It efficiently prunes the tree by:
1. Calculating an upper bound at each node (e.g., by solving the fractional knapsack
problem for the remaining items).
2. If this bound is lower than the best solution found so far, the entire subtree from that
node is discarded.
Key Difference: All NP-Complete problems are NP-Hard, but an NP-Hard problem is not
necessarily in NP (e.g., the Halting Problem). NP-Complete problems are always decision
problems, while NP-Hard problems can be optimization problems.