Daa 5 Marks Answers

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

DAA 5 MARKS ANSWERS

1: Explain in detail about asymptotic notations ?

 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.
 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
2: Explain the divide and conquer method with an example
In Assignment
3: Write an algorithm for binary search == In Copy
4: What is principle difference between dynamic programming and
divide and Conquer techniques?

Divide and Conquer Method Dynamic Programming

1.It deals (involves) three steps at 1.It involves the sequence of four
each level of recursion: steps:
Divide the problem into a number
o Characterize the structure of
of subproblems.
Conquer the subproblems by optimal solutions.
solving them recursively.
o Recursively defines the values
Combine the solution to the
of optimal solutions.
subproblems into the solution for
original subproblems. o Compute the value of optimal
solutions in a Bottom-up
minimum.
o Construct an Optimal Solution
from computed information.

2. It is Recursive. 2. It is non Recursive.

3. It does more work on 3. It solves subproblems only once


subproblems and hence has more and then stores in the table.
time consumption.

4. It is a top-down approach. 4. It is a Bottom-up approach.

5. In this subproblems are 5. In this subproblems are


independent of each other. interdependent.

6. For example: Merge Sort & 6. For example: Matrix


Binary Search etc. Multiplication.
5: Describe in detail graph coloring using back tracking?
=> Graph coloring refers to the problem of coloring vertices of a graph in
such a way that no two adjacent vertices have the same color. This is also
called the vertex coloring problem.
The minimum number of colors needed to color a graph is called its
chromatic number. For example, the following can be colored a minimum
of 2 colors.

Algorithm of Graph Coloring using Backtracking:


 Assign colors one by one to different vertices, starting from vertex 0.
 Before assigning a color, check if the adjacent vertices have the
same color or not.
 If there is any color assignment that does not violate the conditions,
mark the color assignment as part of the solution.
 If no assignment of color is possible then backtrack and return false.
6: Describe in detail 8-queens problem using back tracking?
The 8-queens problem involves placing eight queens on an 8x8
chessboard such that no two queens threaten each other. Backtracking is
an effective method to solve this problem by exploring all possible
placements and abandoning configurations that violate constraints.
Steps of the Algorithm:
Initialize the Board: Start with an empty 8x8 chessboard.
Utility Functions:
isSafe: Checks if a queen can be safely placed at a given position without
being attacked by another queen. It verifies the current row, the upper
diagonal on the left, and the lower diagonal on the left.
printSolution: Optionally prints or stores the board configuration when a
solution is found.

 Place queens column by column starting from the leftmost column.


 For each column, iterate through each row and use isSafe to check if
placing a queen there is safe.
 If it is safe, place the queen and recursively attempt to place the
remaining queens in the next columns.
 If placing the queen leads to a dead-end, remove the queen
(backtrack) and try the next row.
 If no valid position is found for the current column, return false to
backtrack further.
This process continues until all queens are placed without conflicts or all
configurations are exhausted. Backtracking ensures that only valid
configurations are explored by abandoning partial solutions that violate
constraints early in the process.

Prim’s Algorithm Kruskal’s Algorithm

This algorithm begins to construct This algorithm begins to construct


the shortest spanning tree from any the shortest spanning tree from the
vertex in the graph. vertex having the lowest weight in
the graph.

To obtain the minimum distance, it It crosses one node only one time.
traverses one node more than one
time.

The time complexity of Prim’s The time complexity of Kruskal’s


algorithm is O(V2). algorithm is O(E log V).

In Prim’s algorithm, all the graph Kruskal’s algorithm may have


elements must be connected. disconnected graphs.

When it comes to dense graphs, the When it comes to sparse graphs,


Prim’s algorithm runs faster. Kruskal’s algorithm runs faster.

It prefers list data structure. It prefers the heap data structure.

32: Why do we perform topological sorts only on DAGs? Explain (5M)


=> No Cycles:
Definition: Topological sort requires no cycles. Cycles prevent a
consistent ordering of vertices.
DAG Property: Directed Acyclic Graphs (DAGs) are, by definition, free of
cycles.
Linear Order:
Requirement: Topological sort provides a linear order where each vertex
precedes all vertices it directs to.
DAG Suitability: Only DAGs ensure there are no circular dependencies,
allowing for a consistent linear order.

DAG: Tasks with dependencies:

A→B→D

C
Possible topological sorts: A,B,C,D or A,C,B,

Cycle Introduction:

A→B→D
↘ ↑
C←
Impossible to find a topological sort due to the cycle A→B→D→A

33: Explain Reduction Source Problems

o Reduction in computer science involves transforming one problem


into another to utilize existing solutions.
o This technique is vital for tackling complex problems by simplifying
them into known ones.
o Two common types of reductions are polynomial-time reduction and
many-one reduction, where the former aims to achieve the
transformation efficiently.
o Reductions play a crucial role in classifying problems into
complexity classes such as P, NP, NP-complete, and NP-hard.
o They are also instrumental in proving problem hardness by
demonstrating that a problem is at least as hard as another known
problem.

34: Discuss in detail about the class P, NP, NP-hard and NP-complete
problems. Give example for each class.
= P Class
The P in the P class stands for Polynomial Time. It is the collection of
decision problems(problems with a “yes” or “no” answer) that can be
solved by a deterministic machine in polynomial time.
Examples

 Calculating the greatest common divisor.


 Merge Sort
NP Class
The NP in NP class stands for Non-deterministic Polynomial Time. It is the
collection of decision problems that can be solved by a non-deterministic
machine in polynomial time. 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.
Examples :
Hamiltonian Path Problem.
Graph coloring.
NP-hard class
An NP-hard problem is at least as hard as the hardest problem in NP. It
takes a long time to check them. This means if a solution for an NP-hard
problem is given then it takes a long time to check whether it is right or
not.
Examples :
Halting problem.
No Hamiltonian cycle.
NP-complete class
A problem is NP-complete if it is both NP and NP-hard. NP-complete
problems are the hard problems in NP.
Examples :
Hamiltonian Cycle.
Vertex cover.
35: What is halting problem explain with an example?
= The Halting Problem is a classic undecidable problem in computer
science, first proven by Alan Turing. It asks whether a given program,
when run on a given input, will eventually halt (terminate) or run
indefinitely (loop forever). Due to its undecidable nature, there is no
algorithm that can determine the halting behavior of all programs on all
inputs.
Example:
Consider the following Python function:
def halts(input):
while True:
pass
This function contains an infinite loop (while True:), so it will run
indefinitely, never halting

You might also like