0% found this document useful (0 votes)
25 views6 pages

Exp 1

The document is a practical file for a student that contains: - The student's details and course information - An index listing experiments conducted for the Design and Analysis of Algorithms lab course - Details and code for the first experiment which involves implementing a stack using templates and performing push, pop, checking for empty/full operations.

Uploaded by

Akshat Kulharia
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)
25 views6 pages

Exp 1

The document is a practical file for a student that contains: - The student's details and course information - An index listing experiments conducted for the Design and Analysis of Algorithms lab course - Details and code for the first experiment which involves implementing a stack using templates and performing push, pop, checking for empty/full operations.

Uploaded by

Akshat Kulharia
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/ 6

PRACTICAL FILE

Student Name Jakkinapalli Kartikeya Vinay Deepak


UID 21BCS6833
Section & 21BCS_SN-903_B
Group
Department Computer Science & Engineering
Session July-Dec 2023
Course Name Design and Analysis of Algorithms with Lab
Course Code 21CSH-311
Semester 5
TH

Department of Computer Science & Engineering


Chandigarh University, Mohali
Course Name: DAA Lab Course Code: 21ITH-311/21CSH-311
INDEX

S. No. Experiment Date Conduct Viva Worksheet Total Remarks


(12) (10) (8) (30)

1 Experiment 1.1: Analyze if


stack Isempty, Isfull and if
elements are present then
return top element in stacks
using templates and also
perform push and pop
operation in stack.
2 Experiment 1.2: Develop a
program for implementation
of power function and
determine that complexity
should be O(log n) .
3 Experiment 1.3: Evaluate
the complexity of the
developed program to find
frequency of elements in a
given array.
4 Experiment 1.4:
i. Apply the concept of
Linked list and write
code to Insert and
Delete an element at
the beginning and
end of Singly
Linked List.
ii. Apply the concept of
Linked list and write
code to Insert and
Delete an element at
the beginning and at
end in Doubly and
Circular Linked List.
5 Experiment 2.1: Sort a
given set of elements using
the Quick sort method and
determine the time required
to sort the elements. Repeat
the experiment for different
values of n, the number of
elements in the list to be
sorted. The elements can be
read from a file or can be
generated using the random
number generator.

6 Experiment 2.2 Develop a


program and analyze
complexity to implement
subset-sum problem using
Dynamic Programming.
7 Experiment 2.3: Develop a
program and analyze
complexity to implement 0-1
Knapsack using Dynamic
Programming.
8 Experiment 3.1: Develop a
program and analyze
complexity to do a depth-
first search (DFS) on an
undirected graph.
Implementing an application
of DFS such as (i) to find the
topological sort of a directed
acyclic graph, OR (ii) to find
a path from source to goal in
a maze
9 Experiment 3.2: Develop a
program and analyze
complexity to find shortest
paths in a graph with positive
edge weights using Dijkstra’s
algorithm.
10 Experiment 3.3: Develop a
program and analyze
complexity to find all
occurrences of a pattern P in
a given string S.
Course Name: DAA Lab Course Code: 21CSH-311

Experiment 1.1

Aim: Analyze if stack Isempty, Isfull and if elements are present then return top element in
stacks using templates and also perform push and pop operation in stack.

Objectives: To understand stacks

Input/Apparatus Used: Stacks are implemented using templates

Procedure/Algorithm:
Step1: Create stack.
Step2: Check underflow and overflow condition.
Step3: Increment top to store element in stack.
Step4: Decrement top after removing element form stack.
Step5: Check is stack empty or not.

Sample Code:
class Stack:
def __init__(self, size):
self.data = [None] * size
self.top_index = -1
self.max_size = size

def is_empty(self):
return self.top_index == -1

def is_full(self):
return self.top_index == self.max_size - 1

def top(self):
if self.is_empty():
Name: Kartikeya UID: 21BCS6833
Course Name: DAA Lab Course Code: 21CSH-311
raise RuntimeError("Stack is empty.")
return self.data[self.top_index]

def push(self, element):


if self.is_full():
raise RuntimeError("Overflow")
self.top_index += 1
self.data[self.top_index] = element

def pop(self):
if self.is_empty():
raise RuntimeError("Stack is empty.")
self.top_index -= 1

if __name__ == "__main__":
stack_size = int(input("Enter the size of the stack: "))
my_stack = Stack(stack_size)

num_elements = int(input("Enter the number of elements to push: "))


for i in range(num_elements):
element = int(input(f"Enter element {i + 1}: "))
my_stack.push(element)

print("Top element:", my_stack.top())

my_stack.pop()

print("Top element after pop:", my_stack.top())

if my_stack.is_empty():
print("Stack is empty.")
elif my_stack.is_full():
print("Stack is full.")

Name: Kartikeya UID: 21BCS6833


Course Name: DAA Lab Course Code: 21CSH-311
else:
print("Underflow.")

Observations/Outcome :

Time Complexity:
The time complexity is O(n) where n is the number of elements pushed into the stack.

Name: Kartikeya UID: 21BCS6833

You might also like