0% found this document useful (0 votes)
2 views19 pages

DSD Lab Aim and Algorithm

The document outlines various exercises focused on implementing data structures and algorithms in Python, including ADTs like Stack, Queue, List, and advanced concepts like trees, graphs, and shortest path algorithms. Each exercise includes an aim, algorithm steps, and a result indicating successful execution. Overall, the document serves as a comprehensive guide for practical implementations of fundamental data structures and algorithms.

Uploaded by

rajadurai
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)
2 views19 pages

DSD Lab Aim and Algorithm

The document outlines various exercises focused on implementing data structures and algorithms in Python, including ADTs like Stack, Queue, List, and advanced concepts like trees, graphs, and shortest path algorithms. Each exercise includes an aim, algorithm steps, and a result indicating successful execution. Overall, the document serves as a comprehensive guide for practical implementations of fundamental data structures and algorithms.

Uploaded by

rajadurai
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/ 19

Ex.

No:1 Implement simple ADTs as Python classes

Aim:
To Implement simple ADTs as Python classes using Stack,Queue,List using python.

Algorithm:
1. Create a Stack[ ],Queue[],List[] with MAX size as your wish.
2. Write function for all the basic operations of stack,Queue,List - PUSH(),
POP() and DISPLAY(),append(),Extend().
3. Close the program

Result:
Thus the Implemention of simple ADTs as Python classes was executed successfully
Ex.No:2 Implement recursive algorithms in Python

Aim:
To Implement a recursive algorithms in Python using Fibonacci Series

Algorithm:
Step 1:Input the 'n' value until which the Fibonacci series has to be generated

Step 2:Initialize sum = 0, a = 0, b = 1 and count = 1

Step 3:while (count <= n)

Step 4:print sum

Step 5:Increment the count variable

Step 6:swap a and b

Step 7:sum = a + b

Step 8:while

count > n)

Step 9:End the

algorithm

Step 10:Else

Step 11:Repeat from steps 4 to 7

Result:
Thus the Implemention of recursive algorithms in Python using Fibonacci series was executed
successfully
Ex.No:3 Implement List ADT using Python arrays

Aim:
To Implement List ADT using Python arrays
Algorithm
1. Using define function intialise the list
2. while loop to declare the elements until the condition is satisfied.
3. using convertarr function to convert the elemnts to an array
Stop the program

Result:
Thus the implementation of List in arrays was executed successfully
Ex.NO:4 Linked list implementations of List

Aim:
To Implement the Linked list implementations of List using python

Algorithm:
1. Create a list[ ] with MAX size as your wish.
2. Write function for all the basic operations of list - create(), insert(), deletion(), display().
3. Using append() to extend the elements, removal() to delete the elements
4. Close the program.

Result:
Thus the list was created,inserted,removed and extend the element was executed
successfully
Ex.No:5 Implementation of Stack and Queue ADTs

Aim:
To Implementation of Stack and Queue ADTs

Algorithm:

1. Create a Stack[ ],Queue[] with MAX size as your wish.


2. Write function for all the basic operations of stack - append(), POP()
3. Close the program.

Result:
Thus the program was executed successfully
Ex.No:6a Application of List

Aim:
To implement list application using Polynomial Addition in python

Algorithm:
1. Using the define function intial elements will be declared.
2. for loop gives the output of sum of the elements
3. print[poly] statement have the sum of two polynomial elements.
4. Close the program

Result:
Thus the program was executed successfully.
Ex.No:6b Application of Stack

Aim:
To implement the conversion of infix to postfix in stack

Algorithm:
1. Read the given expression
2. check ifempty or not ,the stack will insert the elements.
3. Using push(),pop() to insert the element or remove the element.
4. Check the operator based on the precedence the expression will be evaluated
5. Close the program

Result:
Thus the conversion can be successfully executed
Ex.No:6c Application of Queue
Aim:
To implement the application of queue using FCFS CPU Scheduling
Algorithm:
1. Input the number of processes required to be scheduled using FCFS, burst
time for each process and its arrival time.

2. Calculate the Finish Time, Turn Around Time and Waiting Time for each
process which in turn help to calculate Average Waiting Time and Average
Turn Around Time required by CPU to schedule given set of process using
FCFS.

a. for i = 0, Finish Time T 0 = Arrival Time T 0 + Burst Time T 0

b. for i >= 1, Finish Time T i = Burst Time T i + Finish Time T i - 1

c. for i = 0, Turn Around Time T 0 = Finish Time T 0 - Arrival Time T 0

d. for i >= 1, Turn Around Time T i = Finish Time T i - Arrival Time T i

e. for i = 0, Waiting Time T 0 = Turn Around Time T 0 - Burst Time T 0

f. for i >= 1, Waiting Time T i = Turn Around Time T i - Burst Time T i - 1

3. Process with less arrival time comes first and gets scheduled first by the CPU.

4. Calculate the Average Waiting Time and Average Turn Around Time.
5. Stop the program

Result:
Thus the FCFS CPU Scheduling was Executed Successfully
Ex.No:7A Implementation of searching algorithms
Aim:
To implement searching using Linear and Binary Search algorithm using python
Algorithm:
Linear Search:
1. Read the search element from the user
2. Compare, the search element with the first element in the list
. 3. If both are matching, then display "Given element found!!!" and terminate the
function
4. If both are not matching, then compare search element with the next element in the list.
5. Repeat steps 3 and 4 until the search element is compared with the last element in the
list.
6. If the last element in the list is also doesn't match, then display "Element not
found!!!" and terminate the function.

Binary search :
1. Read the search element from the user
2. Find the middle element in the sorted list
3. Compare, the search element with the middle element in the sorted list.
4. If both are matching, then display "Given element found!!!" and terminate the function
5. If both are not matching, then check whether the search element is smaller or
larger than middle element
. 6. If the search element is smaller than middle element, then repeat steps 2, 3, 4 and
5 for the left sublist of the middle element.
7. If the search element is larger than middle element, then repeat steps 2, 3, 4 and 5
for the right sublist of the middle element.
8. Repeat the same process until we find the search element in the list or until
sublist contains only one element.
9. If that element also doesn't match with the search element, then display "Element
not found in the list!!!" and terminate the function.

Result:
Thus the implementation of searching using Linear and Binary Search using
python was executed successfully
Ex.No:7BImplementation of Sorting Algorithm
Aim:
To Implement sorting Algorithm using Quick Sort and Insertion Sort algorithm using
python

Algorithm:
Quick Sort:

1. Find a “pivot” item in the array. This item is the basis for comparison
for a single round.
2. Start a pointer (the left pointer) at the first item in the array.
3. Start a pointer (the right pointer) at the last item in the array.
4. While the value at the left pointer in the array is less than the pivot
value, move the left pointer to the right (add 1). Continue until the value
at the left pointer is greater than or equal to the pivot value.
5. While the value at the right pointer in the array is greater than the pivot
value, move the right pointer to the left (subtract 1). Continue until the
value at the right pointer is less than or equal to the pivot value.
6. If the left pointer is less than or equal to the right pointer, then swap
the values at these locations in the array.
7. Move the left pointer to the right by one and the right pointer to the left by one.

Insertion Sort:

1. Compare each element with preceding elements


2. Shift each compared element on the right
3. Place the element at the empty spot
4. Print the sorted array

Result:
Thus the implementation of searching Quick and Insertion Sort algorithm using
python was executed successfully
Ex.No:8 Implementation of Hash tables

Aim:
To Implement the Hash tables using python
Algorithm:
1. Create a structure, data (hash table item) with key and value as data.
2. for loops to define the range within the set
of elements. 3.hashfunction(key) for the size
of capacity
4. Using insert(),removal() data to be presented or removed.
5. Stop the program

Result:
Thus the Implementation of hashing was executed successfully
Ex.No:9a Tree representation
Aim:
To implement tree representation in binary tree format
Algorithm:
1. Create a binary tree.
2. Intially all the left and right vertex are none , then declare the values using insert()
function.
3. If data>right element place the element in right
4. If data<left element place the element in left
5. prin the tree
6. Stop the program

Result:
Thus the binary tree was successfully created
Ex.No:9b Tree Traversal Algorithms

Aim: To Implement traversal using Inorder,Preorder,Postorder techniques.


Algorithm:
Inorder(tree)
1. Traverse the left subtree, i.e., call Inorder(left-subtree)
2. Visit the root.
3. Traverse the right subtree, i.e., call Inorder(right-subtree)
Preorder(tree)
1. Visit the root.
2. Traverse the left subtree, i.e., call Preorder(left-subtree)
3. Traverse the right subtree, i.e., call Preorder(right-subtree)
Postorder(tree)
1. Traverse the left subtree, i.e., call Postorder(left-subtree)
2. Traverse the right subtree, i.e., call Postorder(right-subtree)
3.Visit the root

Result:
Thus the Implementation of traversal using Inorder,Preorder,Postorder techniques
was executed successfully
Ex.No:10 Implementation of Binary Search Trees
Aim:
To Implement the Binary Search Trees using python
Algorithm:

Step 1-Read the search element from the user.

Step 2 - Compare the search element with the value of root node in the tree.

Step 3 - If both are matched, then display "Given node is found!!!" and terminate the
function

Step 4 - If both are not matched, then check whether search element is smaller or
larger than that node value.

Step 5 - If search element is smaller, then continue the search process in left subtree.

Step 6- If search element is larger, then continue the search process in right subtree.

Step 7 - Repeat the same until we find the exact element or until the search element is
compared with the leaf node

Step 8 - If we reach to the node having the value equal to the search value then display
"Element is found" and terminate the function.

Result:

Thus the Implementation of Binary Search Trees using python was executed successfully
Ex.NO:11 Implementation of Heaps
Aim:
To Implement the Heap algorithm using python
Algorithm:
1. Insert the heap function in the list
2. using heappush(),heappop(),heapify() to insert ,delete,display the elements.
3. Stop the program

Result:
Thus the Implementation of the Heap algorithm was executed succeefully.
Ex.No:12a Graph representation

Aim:
To implement the graph representation using python
Algorithm:

4. Insert the heap function in the list


5. using heappush(),heappop(),heapify() to insert ,delete,display the elements.
Stop the program

Result:
Thus the implementation of graphs was executed successfully.
Ex.No:12b Graph Traversal Algorithms
Aim:
To Implement using BFS,DFS can be traversed.
Algorithm:
DFS:

Step 1 - Define a Stack of size total number of vertices in the graph.

Step 2 - Select any vertex as starting point for traversal. Visit that vertex and push it
on to the Stack.

Step 3 - Visit any one of the non-visited adjacent vertices of a vertex which is at the
top of stack and push it on to the stack.

Step 4 - Repeat step 3 until there is no new vertex to be visited from the vertex which is
at the top of the stack.

Step 5 - When there is no new vertex to visit then use back tracking and pop one vertex
from the stack.

Step 6 - Repeat steps 3, 4 and 5 until stack becomes Empty.

Step 7 - When stack becomes Empty, then produce final spanning tree by removing
unused edges from the graph

BFS:

Step 1 - Define a Queue of size total number of vertices in the graph.

Step 2 - Select any vertex as starting point for traversal. Visit that vertex and insert it
into the Queue.

Step 3 - Visit all the non-visited adjacent vertices of the vertex which is at front of the
Queue and insert them into the Queue.

Step 4 - When there is no new vertex to be visited from the vertex which is at front of the
Queue then delete that vertex.

Step 5 - Repeat steps 3 and 4 until queue becomes empty.

Step 6 - When queue becomes empty, then produce final spanning tree by removing
unused edges from the graph

Result:
Thus the implementation of using BFS,DFS graph can be traversed.
Ex.No:13 Implementation of single source shortest path
algorithm

Aim:
To Implement single source shortest path algorithm using Bellman Ford Algorithm
Algorithm:

1) This step initializes distances from source to all vertices as infinite and distance to
source itself as 0. Create an array dist[] of size |V| with all values as infinite except
dist[src] where src is source vertex.
2) This step calculates shortest distances. Do following |V|-1 times where |V| is the
number of vertices in given graph.
a) Do following for each edge u-v
If dist[v] > dist[u] + weight of edge uv, then update
dist[v] dist[v] = dist[u] + weight of edge uv
3) This step reports if there is a negative weight cycle in graph. Do following for
each edge u-v If dist[v] > dist[u] + weight of edge uv, then “Graph contains
negative weight cycle”
The idea of step 3 is, step 2 guarantees shortest distances if graph doesn’t contain
negative weight cycle. If we iterate through all edges one more time and get a shorter
path for any vertex, then there is a negative weight cycle

Result:
Thus the Implementation of single source shortest path algorithm was successfully
executed.
Ex.No:14 Implementation of minimum spanning tree algorithms
Aim:
To implement the minimum spanning tree algorithms using Kruskal Algorithm
Algorithm:
1. Label each vertex
2. List the edges in non-decreasing order of weight.
3. Start with the smallest weighted and beginning growing the minimum weighted
spanning tree from this edge.
4. Add the next available edge that does not form a cycle to the construction of the
minimum weighted spanning tree. If the addition of the next least weighted edge
forms a cycle, do not use it.
5. Continue with step 4 until you have a spanning tree.

Result:
Thus the program was executed successfully

You might also like