0% found this document useful (0 votes)
20 views15 pages

Daa Syllabus+ Unit1 QB With Answers

The document outlines the course structure for the Design and Analysis of Algorithms for B. Tech CSE (AI&ML) at Malla Reddy University, detailing course objectives, outcomes, and unit topics including algorithm performance analysis, data structures, and various algorithm design methods such as greedy, dynamic programming, and backtracking. It also includes a list of practical exercises and references for further reading. The course aims to equip students with the ability to analyze algorithms, understand their performance implications, and solve complex problems using appropriate algorithmic techniques.

Uploaded by

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

Daa Syllabus+ Unit1 QB With Answers

The document outlines the course structure for the Design and Analysis of Algorithms for B. Tech CSE (AI&ML) at Malla Reddy University, detailing course objectives, outcomes, and unit topics including algorithm performance analysis, data structures, and various algorithm design methods such as greedy, dynamic programming, and backtracking. It also includes a list of practical exercises and references for further reading. The course aims to equip students with the ability to analyze algorithms, understand their performance implications, and solve complex problems using appropriate algorithmic techniques.

Uploaded by

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

B.

TECH CSE (AI&ML) R23

MALLA REDDY UNIVERSITY


II B.Tech - CBCS pool-1 L/T/P/C
2/0/2/3
(MR23-1CS0223) DESIGN AND ANALYSIS OF ALGORITHMS
COURSE OBJECTIVES:
● To analyze performance of algorithms.
● To choose the appropriate data structure and algorithm design method for a specified application.
● To understand how the choice of data structures and algorithm design methods impacts the
performance of programs.
● To solve problems using algorithm design methods such as the greedy method, divide and
conquer, dynamic programming, backtracking and branch and bound.
● To understand the differences between tractable and intractable problems and to introduce
P and NP classes.
UNIT–I: Introduction: Characteristics of an Algorithm, Specification of Algorithms - Pseudocode
Conventions, Big oh notation, omega notation, theta notation and little oh notation. Disjoint set data
structure, Simple algorithm for Union and Find[REFERENCE-1,2]
Divide and Conquer: Divide and Conquer General method, Applications of Divide and Conquer - Binary
search algorithm, Analysis of Binary search, Quick sort algorithm, Analysis of Quick sort. [REFERENCE-
1,3]
UNIT–II:
Backtracking-General method of Backtracking, Applications of Backtracking - The N-queen problem, Sum
of subset problem, Graph coloring by backtracking, Hamiltonian Cycles problem. [REFERENCE-1,2]
UNIT–III: Greedy method: General method - Greedy algorithm, Applications of Greedy method - Job
sequencing with deadlines- Knapsack problem- Greedy Knapsack algorithm - Fractional Knapsack problem,
Spanning trees- Minimum cost spanning trees(MST)-Types of MST- Prim's Algorithm, Kruskals Algorithm
[REFERENCE-1,2]
UNIT–IV: Dynamic Programming: General method of Dynamic Programming-Applications of Dynamic
Programming - 0/1 Knapsack problem, All pairs shortest path problem, Traveling salesperson problem,
Optimal Binary Search Tree. [REFERENCE-1,3]
UNIT–V: Branch and Bound: General method of Branch and Bound , Applications of Branch and Bound-
Travelling salesperson problem, 0/1 Knapsack problem-LC branch and Bound solution(LCBB).
[REFERENCE-1,4]
NP-Hard and NP-Complete Problems: Basic concepts, P- class and NP- class problems, NP- Hard and
NP-Complete classes, NP-Hard problems. [REFERENCE-1,3]

REFERENCE BOOKS:
[1] Fundamentals of Computer Algorithms, Ellis Horowitz, SatrajSahni and Rajasekharan,
Universities press.
[2] Design and Analysis of Algorithms, Aho ,Ulman and Hopcraft , Pearson Education.
[3] Introduction to Design & Analysis of Algorithm “AnanyLevitin”, 3rd edition Pearson.
[4] Design and Analysis of Algorithms, P. h. Dave,2ndedition, Pearson Education.

MALLA REDDY UNIVERSITY


B. TECH CSE (AI&ML) R23

COURSE OUTCOMES:
● Ability to analyze the performance of algorithms and to choose appropriate algorithm design
techniques for solving problems.
● Ability to understand how the choice of data structures and the algorithm design methods to impact
the performance of programs.
● Describes the dynamic-programming paradigm and explain when an algorithmic design situation
calls for it. Recite algorithms that employ this paradigm. Synthesize dynamic programming
algorithms, and analyze them.
● Describes the greedy paradigm and explain when an algorithmic design situation calls for it. Recite
algorithms that employ this paradigm. Synthesize greedy algorithms, and analyze them.
● Ability to understand the differences between tractable and intractable problems and to introduce P and
NP classes.
LIST OF EXERCISES:
1. Union - Find operations in a social network. (Unit I)
2. Implement Merge sort and Quick sort using Divide & Conquer algorithm (Unit I)
3. Rat in a Maze: A rat is placed in a maze and needs to find its way out. (Unit II)
4. Find the positions to place queens using Backtracking approach. (Unit II)
5. Use Prim’s and Kruskal’s algorithm to find Minimum Cost Spanning Tree for the following graph.
(Unit III)

6. Implement Graph coloring using Greedy algorithm.(Unit III)


7. Consider a scenario where you are tasked with solving the Traveling Salesman Problem (TSP) for a
given set of cities. The goal is to find the shortest possible tour that visits each city exactly once and
returns to the starting city. (Unit IV)
8. Consider the keys: 15,25,35,45,55,65,75 and the graph

Apply Dynamic Programming to find the possible minimum search time.

9. Consider a scenario where you are investigating a decision problem related to graph theory. The problem
involves determining whether there exists a Hamiltonian cycle in a given undirected graph. (Unit V)
10. Implement Job-Scheduling using Branch & Bound Algorithm. (Unit V)

MALLA REDDY UNIVERSITY


B. TECH CSE (AI&ML) R23

UNIT – 1 QB WITH ANSWERS


1. Define various asymptotic notations used for best case, average case and worst case analysis of
algorithms
Answer: Asymptotic notations are the mathematical notations used to describe the running
time of an algorithm when the input tends towards a particular value or a limiting value.

For example: In bubble sort, when the input array is already sorted, the time taken by the
algorithm is linear i.e. the best case.

But, when the input array is in reverse condition, the algorithm takes the maximum time
(quadratic) to sort the elements i.e. the worst case.

When the input array is neither sorted nor in reverse order, then it takes average time. These
durations are denoted using asymptotic notations.

There are mainly three asymptotic notations:

Big-O notation

Omega notation

Theta notation
Big Oh ( O ): Asymptotic Upper Bound
The notation Ο(n) is the formal way to express the upper bound of an algorithm's
running time, is the most commonly used notation. It measures the worst case time
complexity or the longest amount of time an algorithm can possibly take to complete.

A function f(n) can be represented is the order of g(n) that is O(g(n)), if there
exists a value of positive integer n as n0 and a positive constant c such that −

f(n)⩽c.g(n)f(n)⩽c.g(n) for n>n0 in all case

Hence, function g(n) is an upper bound for function f(n), as g(n) grows faster than f(n).

MALLA REDDY UNIVERSITY


B. TECH CSE (AI&ML) R23

Big Omega (Ω): Asymptotic Lower Bound

The notation Ω(n) is the formal way to express the lower bound of an algorithm's
running time. It measures the best case time complexity or the best amount of time an
algorithm can possibly take to complete.

We say that f(n)=Ω(g(n))f(n)=Ω(g(n)) when there exists


constant c that f(n)⩾c.g(n)f(n)⩾c.g(n) for all sufficiently large value of n. Here n is a
positive integer. It means function g is a lower bound for function f ; after a certain
value of n, f will never go below g.

Theta (θ): Asymptotic Tight Bound

The notation θ(n) is the formal way to express both the lower bound and the upper
bound of an algorithm's running time. Some may confuse the theta notation as the
average case time complexity; while big theta notation could be almost accurately
used to describe the average case, other notations could be used as well.

We say that f(n)=θ(g(n))f(n)=θ(g(n)) when there exist


constants c1 and c2 that c1.g(n)⩽f(n)⩽c2.g(n)c1.g(n)⩽f(n)⩽c2.g(n) for all sufficiently
large value of n. Here n is a positive integer.

This means function g is a tight bound for function f.

MALLA REDDY UNIVERSITY


B. TECH CSE (AI&ML) R23

2. Explain binary search algorithm and analyze its time complexity.


Answer: Binary search algorithm
int binary_search(int A[], int key, int imin, int imax)
{
while (imax >= imin)
{
int imid = midpoint(imin, imax);
if(A[imid] == key)
return imid;
else if (A[imid] < key)
imin = imid + 1;
else
imax = imid - 1;
}
}
Analysis:

MALLA REDDY UNIVERSITY


B. TECH CSE (AI&ML) R23

3. Explain quick sort algorithm and simulate it for the following data: 20, 35, 10, 16, 54, 21, 25

Simulation of Quick Sort


Let's simulate quick sort on the dataset: 20, 35, 10, 16, 54, 21, 25.
Step-by-Step Simulation
1. Initial Array:

[20,35,10,16,54,21,25]
2. Choosing Pivot: We will choose the last element (25) as the pivot.

MALLA REDDY UNIVERSITY


B. TECH CSE (AI&ML) R23

3. Partitioning Process:
• Initialize two pointers: i (initially -1) and j (iterating through the array).
• Compare each element with the pivot (25):

• For j = 0 (20): 20<2520<25, increment i to 0 and swap (no change).

• For j = 1 (35): 35>2535>25, do nothing.

• For j = 2 (10): 10<2510<25, increment i to 1 and swap with j = 2:

[20,10,35,16,54,21,25][20,10,35,16,54,21,25]
• For j = 3 (16): 16<2516<25, increment i to 2 and swap with j = 3:

[20,10,16,35,54,21,25][20,10,16,35,54,21,25]
• For j = 4 (54): 54>2554>25, do nothing.

• For j = 5 (21): 21<2521<25, increment i to 3 and swap with j = 5:

[20,10,16,21,54,35,25][20,10,16,21,54,35,25]
4. Final Swap with Pivot: Swap pivot (25) with element at i + 1 (54):

[20,10,16,21,25,35,54][20,10,16,21,25,35,54]
The pivot is now at index 4.
5. Recursive Calls:

• Left Sub-array: [20,10,16,21][20,10,16,21]

• Right Sub-array: [35,54][35,54]

Sorting Left Sub-array [20,10,16,21]


1. Choose pivot (21):
• After partitioning:

[20,10,16]+[20,10,16]+
2. Sort left sub-array:
• Choose pivot (16):
• After partitioning:

++
Sorting Right Sub-array [35,54][35,54]
1. Choose pivot (54):
• No changes needed since all elements are less than or equal to it.

Final Sorted Array


Combining all results gives us:
[10,16,20,21,25,35,54]

MALLA REDDY UNIVERSITY


B. TECH CSE (AI&ML) R23

4. Define iterative binary search algorithm.


Answer: Binary search algorithm
int binary_search(int A[], int key, int imin, int imax)
{
while (imax >= imin)
{
int imid = midpoint(imin, imax);
if(A[imid] == key)
return imid;
else if (A[imid] < key)
imin = imid + 1;
else
imax = imid - 1;
}
}
5. Define the Pseudo code conventions for specifying algorithms of recursive and an iterative
algorithm to compute n!.
Answer:

MALLA REDDY UNIVERSITY


B. TECH CSE (AI&ML) R23

repeat-until:
repeat

<statement-1>
.
.
.

<statement-n> until<condition>

Recursive Algorithm to Compute n!n!


In a recursive approach, the function calls itself with a smaller input until it reaches a base case.

FUNCTION factorial(n)
IF n = 0 THEN
RETURN 1
ELSE
RETURN n * factorial(n - 1)
END FUNCTION

MALLA REDDY UNIVERSITY


B. TECH CSE (AI&ML) R23

6. Explain the frequency counts for all statements in the following algorithm segment.
i=1;
while(i¡=n)
do x=x+1;
i=i+1;
Answer:

To analyze the frequency counts for the provided algorithm segment, we will break down each statement
and determine how many times it executes in relation to the input size nn.
Algorithm Segment
text
i = 1;
while (i <= n) do

x = x + 1;

i = i + 1;

Frequency Count Analysis


1. Initialization Statement:
• Statement: i = 1;
• Frequency Count: This statement is executed 1 time when the algorithm starts.
2. While Loop Condition:
• Statement: while (i <= n)
• The condition is checked before each iteration of the loop, including one final check after the last
increment of ii.

• Frequency Count: The loop runs as long as ii is less than or equal to nn. Since ii starts at 1 and
increments by 1 until it exceeds nn, the condition is checked a total of n+1n+1 times:

• For i=1,2,...,ni=1,2,...,n (n checks)

• Plus one additional check when i=n+1i=n+1 (the loop terminates).

• Therefore, the frequency count for this statement is n+1n+1.


3. Body of the While Loop:
• Statement: x = x + 1;
• This statement is executed once for each iteration of the loop. Since the loop iterates
from i=1i=1 to i=ni=n, it executes exactly nn times.
• Frequency Count: nn.
4. Increment Statement:
• Statement: i = i + 1;
• Similar to the body of the loop, this increment occurs once for each iteration of the loop.

• Thus, it also executes nn times.

MALLA REDDY UNIVERSITY


B. TECH CSE (AI&ML) R23

• Frequency Count: nn.

Summary of Frequency Counts


Now we can summarize the frequency counts for each statement:

Statement Frequency Count

i = 1; 1

while (i <= n) n+1n+1

x = x + 1; nn

i = i + 1; nn

Total Frequency Count Calculation


To compute the total frequency count for all statements combined:
Total=1+(n+1)+n+n=3n+2

7. What is Quick sort? Is quick sort a best sorting method? Justify


Answer:

Quick sort is a widely used and efficient sorting algorithm that employs a divide-and-conquer strategy to
sort an array or list of elements. Developed by Tony Hoare in 1959, it is known for its speed and efficiency
compared to other sorting methods.
How Quick Sort Works
1. Choosing a Pivot: The algorithm selects an element from the array as the pivot. The choice of pivot can
vary; common strategies include selecting the first element, the last element, or a random element.
2. Partitioning: The array is rearranged so that all elements less than the pivot are on its left and all
elements greater than the pivot are on its right. After partitioning, the pivot is in its correct position in the
sorted array.
3. Recursive Calls: The same process is recursively applied to the two sub-arrays formed by splitting at the
pivot until each sub-array contains one or zero elements, which are inherently sorted.

Time Complexity
• Best Case: The best-case time complexity occurs when the pivot divides the array into two equal halves,
resulting in a complexity of O(nlog⁡n)O(nlogn).

• Average Case: On average, quick sort performs well with a time complexity of O(nlog⁡n)O(nlogn).
• Worst Case: The worst-case scenario occurs when the smallest or largest element is consistently chosen
as the pivot (e.g., when the array is already sorted), leading to a time complexity of O(n2)O(n2).

MALLA REDDY UNIVERSITY


B. TECH CSE (AI&ML) R23

Space Complexity
Quick sort has a space complexity of O(log⁡n)O(logn) due to recursive stack space in average
scenarios but can reach O(n)O(n) in worst-case scenarios.

While quick sort is one of the most efficient sorting algorithms and often faster than other
algorithms like merge sort or bubble sort, whether it is the "best" sorting method depends on
several factors:
1. Performance: Quick sort generally outperforms other algorithms for average cases due to its
efficient partitioning and low overhead. It can be significantly faster than algorithms with higher
constant factors in their time complexities.
2. In-Place Sorting: Quick sort is an in-place sorting algorithm, meaning it requires only a small
amount of additional storage space, making it more memory-efficient compared to some other
algorithms like merge sort.
3. Stability: Quick sort is not a stable sort by default, meaning that it does not preserve the relative
order of equal elements. In scenarios where stability is required, other algorithms may be
preferred.
4. Worst-Case Performance: In specific cases (like already sorted arrays), quick sort can degrade
to O(n2)O(n2). However, this can be mitigated by using techniques such as randomizing the
pivot selection or using median-of-three methods.

8. Compare different asymptotic notations


Answer:

MALLA REDDY UNIVERSITY


B. TECH CSE (AI&ML) R23

9. Ghjgh
#include <stdio.h>
#include <stdlib.h>

// Structure to represent a subset


typedef struct {
int parent; // Parent of the node
int rank; // Rank of the node
} Subset;

// Function to create and initialize a subset


Subset* createSubsets(int n) {
Subset* subsets = (Subset*)malloc(n * sizeof(Subset));
for (int i = 0; i < n; i++) {
subsets[i].parent = i; // Each element is its own parent
subsets[i].rank = 0; // Initial rank is 0
}
return subsets;
}

// Find function with path compression


int find(Subset* subsets, int i) {
if (subsets[i].parent != i) {
subsets[i].parent = find(subsets, subsets[i].parent); // Path compression
MALLA REDDY UNIVERSITY
B. TECH CSE (AI&ML) R23

}
return subsets[i].parent;
}

// Union function by rank


void unionByRank(Subset* subsets, int x, int y) {
int xroot = find(subsets, x);
int yroot = find(subsets, y);

if (xroot != yroot) {
if (subsets[xroot].rank < subsets[yroot].rank) {
subsets[xroot].parent = yroot;
} else if (subsets[xroot].rank > subsets[yroot].rank) {
subsets[yroot].parent = xroot;
} else {
subsets[yroot].parent = xroot;
subsets[xroot].rank++;
}
}
}

// Function to free allocated memory for subsets


void freeSubsets(Subset* subsets) {
free(subsets);
}

// Main function to demonstrate the Union-Find algorithm


int main() {
int n = 10; // Number of elements
Subset* subsets = createSubsets(n);

// Perform some union operations


unionByRank(subsets, 0, 1);
unionByRank(subsets, 1, 2);
unionByRank(subsets, 3, 4);
unionByRank(subsets, 4, 5);
unionByRank(subsets, 2, 3); // This will connect the two sets

// Find the roots of different elements


printf("Find(0): %d\n", find(subsets, 0));
printf("Find(1): %d\n", find(subsets, 1));
printf("Find(2): %d\n", find(subsets, 2));
printf("Find(3): %d\n", find(subsets, 3));
printf("Find(4): %d\n", find(subsets, 4));
printf("Find(5): %d\n", find(subsets, 5));

// Free allocated memory


freeSubsets(subsets);

return 0;
}
10. Determine disjoint set operations, union and find algorithms.

void initialize( int Arr[ ], int N)


{
MALLA REDDY UNIVERSITY
B. TECH CSE (AI&ML) R23
for(int i = 0;i<N;i++)
Arr[ i ] = i ;
}
//returns true,if A and B are connected, else it will return false.
bool find( int Arr[ ], int A, int B)
{
if(Arr[ A ] == Arr[ B ])
return true;
else
return false;
}
//change all entries from Arr[ A ] to Arr[ B ].
void union(int Arr[ ], int N, int A, int B)
{
int TEMP = Arr[ A ];
for(int i = 0; i < N;i++)
{
if(Arr[ i ] == TEMP)
Arr[ i ] = Arr[ B ];

MALLA REDDY UNIVERSITY

You might also like