0% found this document useful (0 votes)
6 views7 pages

DAA

The document discusses various algorithms and techniques in computer science, including recursion methods, the Master Theorem for solving recurrences, tail recursion, Turing machines, Union-Find algorithm, greedy algorithms, dynamic programming, and backtracking algorithms like the N-Queen and 8-Queen problems. It also covers specific problems such as the 0/1 Knapsack problem, Bellman-Ford algorithm, job scheduling, and heap sort. Each section provides an overview of the algorithm's principles, applications, and examples to illustrate their functionality.

Uploaded by

S Mukherjee
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views7 pages

DAA

The document discusses various algorithms and techniques in computer science, including recursion methods, the Master Theorem for solving recurrences, tail recursion, Turing machines, Union-Find algorithm, greedy algorithms, dynamic programming, and backtracking algorithms like the N-Queen and 8-Queen problems. It also covers specific problems such as the 0/1 Knapsack problem, Bellman-Ford algorithm, job scheduling, and heap sort. Each section provides an overview of the algorithm's principles, applications, and examples to illustrate their functionality.

Uploaded by

S Mukherjee
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

1.

Recursion tree method is best than the Substitution method for solving a
recurrence relation because in recursion tree method, we draw a recurrence tree and
calculate the time taken by every level of tree. Finally, we sum the work done at
all levels. To draw the recurrence tree, we start from the given recurrence and
keep drawing till we find a pattern among levels. The pattern is typically a
arithmetic or geometric series but in substitution method we make a guess for the
solution and then we use mathematical induction to prove the guess is correct or
incorrect.

2.Master's Theorem:

The master method is a formula for solving recurrence relations of the form:

T(n) = aT(n/b) + f(n),


where,
n = size of input
a = number of subproblems in the recursion
n/b = size of each subproblem. All subproblems are assumed
to have the same size.
f(n) = cost of the work done outside the recursive call,
which includes the cost of dividing the problem and
cost of merging the solutions

Here, a ≥ 1 and b > 1 are constants, and f(n) is an asymptotically positive


function.

If a ≥ 1 and b > 1 are constants and f(n) is an asymptotically positive function,


then the time complexity of a recursive relation is given by

T(n) = aT(n/b) + f(n)

where, T(n) has the following asymptotic bounds:

1. If f(n) = O(n^logb a-ϵ), then T(n) = Θ(n^logb a).

2. If f(n) = Θ(n^logb a), then T(n) = Θ(n^logb a * log n).

3. If f(n) = Ω(n^logb a+ϵ), then T(n) = Θ(f(n)).

ϵ > 0 is a constant.

3.If a function is calling itself and that recursive call is the last statement in
a function then it is called tail recursion. After that call there is nothing, it
is not performing anything, so, it is called tail recursion.

void fun(int n)
{
if(n>0)
{
printf("%d",n);
fun(n-1);
}
}
fun(3);
The tail recursive functions are considered better than non-tail recursive
functions as tail-recursion can be optimized by the compiler.

4. Turing machines are an important tool for studying the limits of computation and
for understanding the foundations of computer science. They provide a simple yet
powerful model of computation that has been widely used in research and has had a
profound impact on our understanding of algorithms and computation.

5. Union Find Algorithm:

An algorithm that implements find and union operations on a disjoint set data
structure. It finds the root parent of an element and determines whether if two
elements are in the same set or not. If two elements are at different sets, merge
the smaller set to the larger set. At the end, we can get the connected components
in a graph.

We are given 10 individuals say, a, b, c, d, e, f, g, h, i, j

Following are relationships to be added:


a <-> b
b <-> d
c <-> f
c <-> i
j <-> e
g <-> j

Given queries like whether a is a friend of d or not. We basically need to create


following 4 groups and maintain a quickly accessible connection among group items:
G1 = {a, b, d}
G2 = {c, f, i}
G3 = {e, g, j}
G4 = {h}

6. Characteristics of Greedy Algorithms:

~ *Making locally optimal choices*

A greedy algorithm selects the best option available at that specific moment at
each step without taking the decision’s long-term implications into account.

~ *No backtracking*

A greedy algorithm’s decisions are final and cannot be changed or undone after they
have been made. The algorithm keeps going without going back to its earlier
choices.

~ *Iterative process*

Greedy algorithms operate in a succession of iterative phases, each building on the


one before it.

~ *Efficiency of greedy algorithms*


Greedy algorithms frequently have a low number of steps and are consequently
computationally quick, they are often effective in terms of temporal complexity.
Nevertheless, because greedy algorithms don’t always come up with the best feasible
answer, this efficiency could come at the expense of optimality.
7. 0/1 Knapsack Problem:

Given n items where each item has some weight and profit associated with it and
also given a bag with capacity W, [i.e., the bag can hold at most W weight in it].
The task is to put the items into the bag such that the sum of profits associated
with them is the maximum possible.

Note: The constraint here is we can either put an item completely into the bag or
cannot put it at all [It is not possible to put a part of an item into the bag].

Input: W = 4, profit[] = [1, 2, 3], weight[] = [4, 5, 1]


Output: 3

Explanation: There are two items which have weight less than or equal to 4. If we
select the item with weight 4, the possible profit is 1. And if we select the item
with weight 1, the possible profit is 3. So the maximum possible profit is 3. Note
that we cannot put both the items with weight 4 and 1 together as the capacity of
the bag is 4.

Input: W = 3, profit[] = [1, 2, 3], weight[] = [4, 5, 6]


Output: 0

8. The primary reason the greedy algorithm fails for the 0-1 Knapsack problem is
that it does not consider the possibility of excluding certain items to achieve a
better overall solution. The greedy approach only focuses on immediate gains,
choosing items with high value-to-weight ratios without considering the potential
impact on future decisions.

Consider a backpack with a weight capacity of 4, and items with the following
weights and values:

Item | Weight | Value | value/Weight


A 3 1.8 0.6
B 2 1 0.5
C 2 1 0.5

If you apply Greedy on value you will first select item A, so the residual weight
capacity will be 1. Since both items A and B weigh more than that residual value,
you won't be able to add any more items. Now, this is a feasible solution but not
an optimal solution.

Greedy based on value per weight would first choose item A and then quit, as
residual capacity is not enough to accommodate any more item, so the total value
reached is 1.8.

The optimal solution, however, is to choose items B and C, which together exactly
weigh the full capacity and have a combined value of 2.

9. Dynamic Programming is a technique in computer programming that helps to


efficiently solve a class of problems that have overlapping subproblems and optimal
substructure property.

If any problem can be divided into subproblems, which in turn are divided into
smaller subproblems, and if there are overlapping among these subproblems, then the
solutions to these subproblems can be saved for future reference. In this way,
efficiency of the CPU can be enhanced. This method of solving a solution is
referred to as dynamic programming.

Such problems involve repeatedly calculating the value of the same subproblems to
find the optimum solution.

Example: Calculating fibonacci-

Let n be the number of terms.

1. If n = 0, return 0.
2. If n = 1, return 1.
3. Else, return the sum of two preceding numbers.

Hence, we have the sequence 0,1,1, 2, 3. Here, we have used the results of the
previous steps as shown below. This is called a dynamic programming approach.

F(0) = 0
F(1) = 1
F(2) = F(1) + F(0)
F(3) = F(2) + F(1)
F(4) = F(3) + F(2)

10. Matrix chain Multiplication Algoritm:

1: **Initialize:**
- Create a 2D array m where m[i][j] will hold the minimum number of
scalar multiplications needed to compute the
matrix product A_i * A_{i+1} * … * A_j.
- Create a 2D array s where s[i][j] will hold the index of the matrix
after which the optimal split occurs.

2.: **Matrix Dimensions:**


- Let p be an array where p[i] denotes the number of rows in matrix A_i and
p[i+1] denotes the number of columns in matrix A_i.
Hence, p has a length of n + 1.

3: **Base Case:**
- For i = 1 to n, set m[i][i] = 0 (the cost of multiplying one matrix is zero).

4: **Compute Optimal Costs:**


- For chain length l from 2 to n:
- For i = 1 to n - l + 1:
- Set j = i + l - 1
- Initialize m[i][j] to infinity.
- For k = i to j - 1:
- Compute the cost of splitting the product at position k:
q = m[i][k] + m[k+1][j] + p[i-1] * p[k] * p[j]
- If q < m[i][j]:
- Update m[i][j] = q
- Update s[i][j] = k

5: **Return Results:**
- The minimum number of scalar multiplications needed is found in m[1][n].
- The optimal parenthesization can be retrieved from the s array
using a recursive function or a stack-based approach.
11. 8-Queen Problem (BackTracking Algorithm):

Step by step approach:

1: Initialize an empty 8×8 chessboard with all positions set to 0.

2: Start with the first row (row 0) and try placing a queen in each column.

3: For each placement, check if the position is safe (not attacked by any
previously placed queen). A position is unsafe if another queen is in the same
column or on the same diagonal.

4: If a safe position is found, place the queen (set position to 1) and recursively
try to place queens in subsequent rows. Otherwise, backtrack by removing the queen
and trying the next column.

5: If all rows are successfully filled (8 queens placed), a valid solution is


found.

12. Bellman-Ford Algorithm:

1: Set initial distance to zero for the source vertex, and set initial distances to
infinity for all other vertices.

2: For each edge, check if a shorter distance can be calculated, and update the
distance if the calculated distance is shorter.

3:Check all edges (step 2) V−1 times. This is as many times as there are vertices
(V), minus one.

4: Optional: Check for negative cycles.

13. The Union by Rank technique is used to optimize the Union operation by ensuring
that the smaller tree is always attached to the root of the larger tree. This
approach prevents the trees from becoming too imbalanced, which would lead to
inefficient find operations.

example:

Let there be 4 elements: 0, 1, 2, 3

Initially, all elements are in their own subsets: 0 1 2 3

Do Union(0, 1)

1 2 3
/
0

Do Union(1, 2)

2 3
/
1
/
0

Do Union(2, 3)

3
/
2
/
1
/
0

14. N-Queen Problem (BackTracking Algorithm):

1: Place a queen in the first column of the first row.


2: Check if placing the queen in the current position conflicts with any previously
placed queens.
3: If there is no conflict, proceed to the next column. If there is a conflict,
backtrack to the previous column and try placing the queen in the next row.
4: Repeat steps 2 and 3 for all columns.
5: If all queens are placed successfully without conflicts, a solution is found.
6: If no solution is found after exploring all possibilities, backtrack to the
previous column and try placing the queen in the next row.

15. Graph Coloring Algorithm:

1: Create a recursive function that takes the graph, current index, number of
vertices, and color array.

2: If the current index is equal to the number of vertices. Print the color
configuration in the color array.

3: Assign a color to a vertex from the range (1 to m).


For every assigned color, check if the configuration is safe, (i.e. check if the
adjacent vertices do not have the same color) and recursively call the function
with the next index and number of vertices else return false

4: If any recursive function returns true then break the loop and return true
If no recursive function returns true then return false.

16. Job Scheduling Algorithm:

Set of jobs with deadlines and profits are taken as an input with the job
scheduling algorithm and scheduled subset of jobs with maximum profit are obtained
as the final output.

Algorithm:

Step1 − Find the maximum deadline value from the input set
of jobs.
Step2 − Once, the deadline is decided, arrange the jobs
in descending order of their profits.
Step3 − Selects the jobs with highest profits, their time
periods not exceeding the maximum deadline.
Step4 − The selected set of jobs are the output.

17. The heap property:

The heap property says that is the value of Parent is either greater than or equal
to (in a max heap ) or less than or equal to (in a min heap) the value of the
Child.

A heap is described in memory using linear arrays in a sequential manner.

18. Heap Sort algorithm:

1: Build a Max-Heap from the input array.


2: Swap the root (maximum value) with the last element in the array.
3: Reduce the heap size by 1 (excluding the sorted elements at the end).
4: Re-heapify the heap to restore the heap property.

You might also like