DAA Assignment
DAA Assignment
DAA Assignment
Unit No 1
1.Solve following recurrence using master method.
T(n) 2Tn/2 n
T(n) 2
4Tn/2 n
Solution :
T(n)=2T(n/2) + n T(n)=4T(n/2) + n2
Compare with T(n) = aT(n/b) + f(n) Compare with T(n) = aT(n/b) + f(n)
a=2 , b=2 , f(n) = n a=4 , b=2 , f(n) = n2 , k=2 , p=0
log ba = log 2 2 = 1 a=bk -> 4=22
f(n)= n since, p > -1
Ω(nlog b a+ ε)= Ω(n1+1)= Ω(n2) then, T(n) = θ (nlogba.logp+1n)
f(n)> Ω(nlog b a+ ε) and log ba = log 2 4 = 2
af(n/b) = 2f(n/2)
cf(n) T(n) = θ (n2.log1n)
af(n/2)<cf(n)…… is true
therefore case 3 is applied
T(n) = θ(f(n))
i.e. T(n) = θ(n)
T(n) 2Tn/2 n2
T(n) 2Tn/2 nlogn
Solution:
T(n) = 2T(n/2) + n2 T(n) = 2T(n/2) + nlogn
Compare with T(n) = aT(n/b) + f(n) Compare with T(n) = aT(n/b) + f(n)
a=2 , b=2 , f(n) = n2 , k=2 , p=0 a=2 , b=2 , f(n) = nlogn , k=1 , p=1
a<bk -> 2<22 bk=21=2
case 3 is applied a=bk
since, p >=0 case 2 is applied
then T(n) = θ (nklogpn) since p>-1
log ba = log 2 2 = 1 then T(n) = θ (nlogba.logp+1n)
therefore T(n) = θ (n2log0n) log ba = log 2 2 = 1
T(n) = θ (n2) therefore, T(n) = θ (n1.log1+1n)
i.e. T(n) = θ (n1.log2n)
Unit No 2
1.Explain Asymptotic notation & calculate upper, lower & tight bound of following.
Nagar YuwakShikshanSanstha’s
YeshwantraoChavan College of Engineering
(An Autonomous Institution affiliated to RashtrasantTukadoji Maharaj Nagpur University)
(Accredited 'A++' Grade by NAAC)
Hingna Road, Wanadongri, Nagpur - 441 110
Ph.: 07104-295083, 295085, Website: www.ycce.edu
Solution:
Asymptotic Notations are mathematical tools used to analyze the performance of
algorithms by understanding how their efficiency changes as the input size grows.
These notations provide a concise way to express the behavior of an algorithm’s time
or space complexity as the input size approaches infinity.
Rather than comparing algorithms directly, asymptotic analysis focuses on
understanding the relative growth rates of algorithms’ complexities.
It enables comparisons of algorithms’ efficiency by abstracting away machine-
specific constants and implementation details, focusing instead on fundamental
trends.
Asymptotic analysis allows for the comparison of algorithms’ space and time
complexities by examining their performance characteristics as the input size varies.
By using asymptotic notations, such as Big O, Big Omega, and Big Theta, we can
categorize algorithms based on their worst-case, best-case, or average-case time or
space complexities, providing valuable insights into their efficiency.
In aggregate analysis, there are two steps. First, we must show that a sequence
of 𝑛n operations takes 𝑇(𝑛)T(n) time in the worst case. Then, we show that each
operation takes 𝑇(𝑛)𝑛nT(n) time, on average. Therefore, in aggregate analysis, each
operation has the same cost. In the previous example of cake-making, both operations
would be described as medium, instead of fast and slow.
A common example of aggregate analysis is a modified stack. Stacks are a linear data
structure that have two constant-time operations. push(element) puts an element on the
top of the stack, and pop() takes the top element off of the stack and returns it. These
operations are both constant-time, so a total of 𝑛n operations (in any order) will result
in 𝑂(𝑛)O(n) total time.
Now, a new operation is added to the stack. multipop(k) will either pop the
top 𝑘k elements in the stack, or if it runs out of elements before that, it will pop all of the
elements in the stack and stop. The pseudo-code for multipop(k) would look like this:
multipop(k):
while stack not empty and k > 0:
k = k - 1
stack.pop()
Looking at the pseudo-code, it's easy to see that this is not a constant-time
operation. multipop can run for at most 𝑛n times, where 𝑛n is the size of the stack. So,
the worst-case runtime for multipop is 𝑂(𝑛)O(n). So, in atypical analysis, that means
that 𝑛n multipop operations take 𝑂(𝑛2)O(n2) time.
However, that's not actually the case. Think about multipop and what it's actually
doing. multipop cannot function unless there's been a push to the stack because it
would have nothing to pop off. In fact, any sequence of 𝑛n operations
of multipop , pop and push can take at most 𝑂(𝑛)O(n) time. multipop , the only non-
Nagar YuwakShikshanSanstha’s
YeshwantraoChavan College of Engineering
(An Autonomous Institution affiliated to RashtrasantTukadoji Maharaj Nagpur University)
(Accredited 'A++' Grade by NAAC)
Hingna Road, Wanadongri, Nagpur - 441 110
Ph.: 07104-295083, 295085, Website: www.ycce.edu
For any value of 𝑛n, any sequence of multipop , pop , and push takes 𝑂(𝑛)O(n) time.
So, using aggregate analysis,
𝑇(𝑛)𝑛=𝑂(𝑛)𝑛=𝑂(1).nT(n)=nO(n)=O(1).
So, this stack has an amortized cost of 𝑂(1)O(1) per operation.
1)void fun(int n)
{
if (n < 5)
cout << "ycce";
else {
for (int i = 0; i < n; i++) {
cout << i;
}
}
}
Solution: The time complexity of above code is O(n) as the loop will run for n times.
2) void fun(int n)
{
for (int i = 0; i <= n / 3; i++)
Solution: The time complexity for above code is O(n) as the for loop runs for n times.
Nagar YuwakShikshanSanstha’s
YeshwantraoChavan College of Engineering
(An Autonomous Institution affiliated to RashtrasantTukadoji Maharaj Nagpur University)
(Accredited 'A++' Grade by NAAC)
Hingna Road, Wanadongri, Nagpur - 441 110
Ph.: 07104-295083, 295085, Website: www.ycce.edu
Solution:
The complexity of a method or operation, as defined in Chapter 2 of the text, is the actual
complexity of the method/operation. The actual complexity of an operation is determined by
the step count for that operation, and the actual complexity of a sequence of operations is
determined by the step count for that sequence. The actual complexity of a sequence of
operations may be determined by adding together the step counts for the individual
operations in the sequence. Typically, determining the step count for each operation in the
sequence is quite difficult, and instead, we obtain an upper bound on the step count for the
sequence by adding together the worst-case step count for each operation.
Accounting Method
When we use the accounting method, we must first assign an amortized cost for each month
and then show that this assignment satisifes Equation (3). We have the option to assign a
different amortized cost to each month. In our maintenance contract example, we know the
actual cost by month and could use this actual cost as the amortized cost. It is, however,
easier to work with an equal cost assignment for each month. Later, we shall see examples of
operation sequences that consist of two or more types of operations (for example, when
dealing with lists of elements, the operation sequence may be made up of search, insert, and
remove operations). When dealing with such sequences we often assign a deifferent
amortized cost to operations of different types (however, operations of the same type have the
same amortized cost).
To get the best upper bound on the sum of the actual costs, we must set the amortized
monthly cost to be the smallest number for which Equation (3) is satisifed for all n. From the
above table, we see that using any cost less than $75 will result in P(n) - P(0) < 0 for
some values of n. Therefore, the smallest assignable amortized cost consistent with Equation
(3) is $75.
Generally, when the accounting method is used, we have not computed the aggregate cost.
Therefore, we would not know that $75 is the least assignable amortized cost. So we start by
assigning an amortized cost (obtained by making an educated guess) to each of the different
operation types and then proceed to show that this assignment of amortized costs satisfies
Equation (3). Once we have shown this, we can obtain an upper bound on the cost of any
Nagar YuwakShikshanSanstha’s
YeshwantraoChavan College of Engineering
(An Autonomous Institution affiliated to RashtrasantTukadoji Maharaj Nagpur University)
(Accredited 'A++' Grade by NAAC)
Hingna Road, Wanadongri, Nagpur - 441 110
Ph.: 07104-295083, 295085, Website: www.ycce.edu
where k is the number of different operation types and f(i) is the frequency of operation
type i (i.e., the number of times operations of this type occur in the operation sequence).
For our maintenance contract example, we might try an amortized cost of $70. When we use
this amortized cost, we discover that Equation (3) is not satisifed for n = 12 (for example)
and so $70 is an invalid amortized cost assignment. We might next try $80. By constructing
a table such as the one above, we will observe that Equation (3) is satisfied for all months in
the first 12 month cycle, and then conclude that the equation is satisifed for all n. Now, we
can use $80n as an upper bound on the contract cost for n months.
Unit No 4
1. Generate the optimum sequence for the multiplication of the following matrices
and find the number of operations required for the multiplication.
A= 13 * 5
B= 5 * 89
C= 89 * 3
D= 3 * 34
Solution:
Nagar YuwakShikshanSanstha’s
YeshwantraoChavan College of Engineering
(An Autonomous Institution affiliated to RashtrasantTukadoji Maharaj Nagpur University)
(Accredited 'A++' Grade by NAAC)
Hingna Road, Wanadongri, Nagpur - 441 110
Ph.: 07104-295083, 295085, Website: www.ycce.edu
Unit No 5
Solution:
The graph coloring problem entails assigning colours to specific elements of a graph
while adhering to particular limits and constraints. In other terms, Graph coloring
refers to the act of assigning colours to vertices so that no two neighbouring vertexes
have the same colour.
No two nearby vertices, adjacent edges, or adjacent areas in a graph are coloured with
the same number of colours. This is known as the chromatic number, and the graph is
known as a properly coloured graph.
Colours, order of coloring, colour assignment, and other constraints are established on
the graph while graph coloring. A Color is assigned to a vertex or a specific region.
As a result, vertices or areas with the same colour create independent sets.
EXAM SCHEDULING
Let’s suppose algebra, physics, statistics and calculus are four courses of study in our
college. And let’s say that following pairs have common students :
1. Algebra and statistics
2. Algebra and calculus
3. Statistics and physics
Problem: Say algebra and statistics exam is held on same day then students taking both
courses have to miss at least one exam. They can’t take both at same time. How do we
schedule exams in minimum no of days so that courses having common students are not held
on same day?
Nagar YuwakShikshanSanstha’s
YeshwantraoChavan College of Engineering
(An Autonomous Institution affiliated to RashtrasantTukadoji Maharaj Nagpur University)
(Accredited 'A++' Grade by NAAC)
Hingna Road, Wanadongri, Nagpur - 441 110
Ph.: 07104-295083, 295085, Website: www.ycce.edu
Look at the above graph. It solves our problem. We can conduct exam of courses on
same day if they have same color.
Our solution:
DAY 1: Algebra and Physics
DAY 2: Statistics and Calculus
This solves our problem of scheduling exams so that all students can take exams
without worrying about missing one.
Nagar YuwakShikshanSanstha’s
YeshwantraoChavan College of Engineering
(An Autonomous Institution affiliated to RashtrasantTukadoji Maharaj Nagpur University)
(Accredited 'A++' Grade by NAAC)
Hingna Road, Wanadongri, Nagpur - 441 110
Ph.: 07104-295083, 295085, Website: www.ycce.edu
2. Define Hamiltonian cycle. Find the Hamiltonian cycle for the following graph
Solution:
A Hamiltonian cycle, also called a Hamiltonian circuit is a cycle (i.e., closed loop)
through a graph that visits each node exactly once . A graph possessing a Hamiltonian
cycle is said to be a Hamiltonian graph.
3. Define Hamiltonian cycle. Find the Hamiltonian cycle for the following graph
Solution :
A Hamiltonian cycle, also called a Hamiltonian circuit is a cycle (i.e., closed loop)
through a graph that visits each node exactly once . A graph possessing a Hamiltonian
cycle is said to be a Hamiltonian graph.
Solution:
A Hamiltonian cycle, also called a Hamiltonian circuit is a cycle (i.e., closed loop)
through a graph that visits each node exactly once . A graph possessing a Hamiltonian
cycle is said to be a Hamiltonian graph.
9.For node 3: Explore paths from node 3 to its adjacent unvisited nodes (4 and 6).
10.For node 7: Explore paths from node 7 to its adjacent unvisited node (8).
11.Continue exploring paths and backtracking until a Hamiltonian cycle is found or
until all paths have been explored.
12.If a Hamiltonian cycle is found, return the cycle. Otherwise, return that no
Hamiltonian cycle exists in the graph.
Unit No 6.
Features:
NP:
Nagar YuwakShikshanSanstha’s
YeshwantraoChavan College of Engineering
(An Autonomous Institution affiliated to RashtrasantTukadoji Maharaj Nagpur University)
(Accredited 'A++' Grade by NAAC)
Hingna Road, Wanadongri, Nagpur - 441 110
Ph.: 07104-295083, 295085, Website: www.ycce.edu
Features:
The solutions of the NP class are hard to find since they are being solved by a non-
deterministic machine but the solutions are easy to verify.
Problems of NP can be verified by a Turing machine in polynomial time.
NP Hard:
An NP-hard problem is at least as hard as the hardest problem in NP and it is a class
of problems such that every problem in NP reduces to NP-hard.
Features:
NP-Complete:
A problem is NP-complete if it is both NP and NP-hard. NP-complete problems are
the hard problems in NP.
Features: