DAA-Lab Manual-NBA 2023 - Geeta Rani
DAA-Lab Manual-NBA 2023 - Geeta Rani
LAB MANUAL
Course Coordinator
1. AIM:
Design and analysis of algorithms is the core course of computer and communication
engineering. This course is introduced in fifth semester to teach the designing and
analysis of algorithms based on different algorithm design strategies such as divide and
conquer, greedy approach, dynamic programming, branch and bound etc. This course is
introduced here after completing the pre-requisites “programming in C” and “data
structures and algorithms. In this course, the students can understand how an efficient
algorithm can be designed for a given problem. They can also learn analysis of algorithms
in space and time.
2. ASSESSMENT CRITERIA:
(HackerRank/Leetcode)
3. LAB OUTCOMES:
[CC3131.1] Recognize the appropriate algorithm design strategy based on the type
of problem.
[CC3131.2] Implement the divide, conquer based algorithms, and compute their
time complexity.
[CC3131.3] Implement the algorithms based on greedy approach, dynamic
programming, backtracking, and branch and bound. Also, analyses their time
complexity.
[CC3131.4] Strengthen the industry readiness, and entrepreneurship ability by
problem solving on an open-source platform.
4. GUIDELINES TO STUDENTS:
5. FORMAT OF INDEX
write
complete aim of the
program that
student has
written in the
DD/MM/YYY DD/MM/YYY Here faculty will Signature
aim section in every
Y Y write some of the
program
remark/grade/co faculty
mment/etc.
Students must follow the following format to write programs in lab record.
a) Aim: write the complete aim of the program to be developed.
b) Source Code: write complete source code with name of the file in the middle
of the sheet and program should be written in proper indentation.
c) Output: write the compete output with set of input entered by user during
execution.
d) Dry run: The written code should be illustrated with an example dry run.
Other instructions related to lab record
lab.
c. Every question in lab record must start with new page. (Look at image)
i. Aim
iii. Output
iv. Dry run (should include line number in corresponding code, logic,
vi. Students can write dry run on A4 size sheet and keep it in lab
record file, or they can submit a separate notebook for dry run.
Illustration of Experiments
Course Code: CC3131
Semester-V
1. Binary Search in both recursive and non-recursive manner.
Non-recursive (Iterative Binary Search)
Step 1: Start
Step 2: Initialize
low = 1
high = n
Step 3: Perform Search
While(low <= high)
Step 4: Obtain index of midpoint of interval
Middle = (low + high) / 2
Step 5: Compare
if(X < K[middle])
high = middle - 1
else
print “Element found at position”
Return(middle)
goto: step 2
Step 6: Unsuccessful Search
print “Element found at position”
Return (middle)
Step 7: Stop
ii Take two variables to point left and right of the list, excluding pivot.
iii The left will point to the lower index, and the right will point to the higher index.
iv Now you move all elements which are greater than pivot to the right.
v Move all elements smaller than the pivot to the left partition.
4. Insertion Sort operation with suitable input.
Create a heap.
Delete the elements.
Sorted array.
7. Priority Queue operation with suitable input.
• Elements are inserted according to their priority.
• The elements of highest priority are deleted earlier than elements of lower priority.
• The value of element can be considered its priority.
• Maintaining priority queue using array takes O(n) time.
• Maintaining priority queue using heap takes O (log n) time.
// If weight of the nth item is more than Knapsack capacity W, then this
item cannot be included in the optimal solution
if (wt[n - 1] > W)
return knapSack(W, wt, val, n - 1);
Steps:
BFS:
• visit a vertex, explore it completely, repeat till all vertices are explored.
DFS:
• Visit a vertex, explore one of its adjacent vertices. Explore one of the adjacent vertices of
the previously explored vertex. Repeat till no vertex to explore. Return back and explore.
Repeat.
4. Else take the maximum value from the previous column and previous
row element for filling the current cell. Point an arrow to the cell with
maximum value. If they are equal, point to any of them.
5. Fill the values
9. The bottom right corner is the length of the LCS. In order to find the
longest common subsequence, start from the last element and follow
the direction of the arrow. The elements corresponding to () symbol
form the longest common subsequence.
Create a path according to the arrows
15. Implement Binomial Coefficient using Dynamic Programming with suitable input.
16. Implement travelling sales person problem using dynamic programming with suitable input.
Algorithm: Traveling-Salesman-Problem
C ({1}, 1) = 0
for s = 2 to n do
for all subsets S є {1, 2, 3, … , n} of size s and containing 1
C (S, 1) = ∞
for all j є S and j ≠ 1
C (S, j) = min {C (S – {j}, i) + d(i, j) for i є S and i ≠ j}
Return minj C ({1, 2, 3, …, n}, j) + d(j, i)
17. Consider the problem of N-queens on a chess board. Two queens are said to attack each
other if they are on the same row, column or diagonal. Write a program that implements
back tracking algorithm to solve the problem i.e., place eight non-attacking queens on the
board.
Continue the same way untill two adjacent queens are placed at non-conflicting positions,
i.e these are not in same row, not in same column, and not in same diagonal.