Professional Documents
Culture Documents
Updated Dsa Lab Manual 2022-23
Updated Dsa Lab Manual 2022-23
Updated Dsa Lab Manual 2022-23
Experiment No. : 1
2. Algorithm
If one of the members of structure is also a structure, then such a structure is called as a
nested structure.
The syntax of a nested structure can be as given below :
struct structure_name
{
data_type variable_name;
struct { data_type variable_name;
} internal_structure_name;
};
For example to describe a circle we need its radius and centre. The centre requires x and
y co–ordinates. Hence, this can be described by a nested structure as shown below :
struct circle
{
float radius;
struct
{
float x,y;
}centre;
}
To access the element of an internal structure the syntax is as given below:
structure_variable name.internal_structure_name.element_name;
For example if we make an variable of structure circle as struct circle c, then to access the
“x” coordinate of the structure we will have the syntax as “c.centre.x”
Experiment No. : 2
Implementation of self-referential
structure in C.
Experiment No. 2
1. Aim: Implementation of self-referential structure in C.
2. Algorithm
▪ Self Referential structures are those structures that have one or more pointers which
point to the same type of structure, as their member. In other words, structures pointing
to the same type of structures are self-referential in nature.
Example:
struct node {
int data1;
char data2;
struct node* link;
};
int main()
{
struct node ob;
return 0;
}
▪ In the above example ‘link’ is a pointer to a structure of type ‘node’. Hence, the
structure ‘node’ is a self-referential structure with ‘link’ as the referencing pointer.
An important point to consider is that the pointer should be initialized properly before
accessing, as by default it contains garbage value.
Types of Self Referential Structures
1. Self Referential Structure with Single Link
2. Self Referential Structure with Multiple Links
Self Referential Structure with Single Link: These structures can have only one self-
pointer as their member. The following example will show us how to connect the
objects of a self-referential structure with the single link and access the corresponding
data members. The connection formed is shown in the following figure.
▪ Self Referential Structure with Multiple Links: Self referential structures with
multiple links can have more than one self-pointers. Many complicated data structures
can be easily constructed using these structures. Such structures can easily connect to
more than one nodes at a time. The following example shows one such structure with
more than one links.
▪ The connections made in the above example can be understood using the following
figure.
Experiment No. : 3
2. Algorithm
1. Declare and initialize a two-dimensional array a.
2. Calculate the number of rows and columns present in the given array and store it in
variables rows and cols respectively.
3. Loop through the array and count the number of zeroes present in the given array and
store in the variable count.
4. Calculate the size of the array by multiplying the number of rows with many columns of
the array.
5. If the count is greater than size/2, given matrix is the sparse matrix. That means, most of
the elements of the array are zeroes.
6. Else, the matrix is not a sparse matrix.
Experiment No. : 4
2. Algorithm
Insertion Sort
Step 1 - If the element is the first element, assume that it is already sorted. Return 1.
Step3 - Now, compare the key with all elements in the sorted array.
Step 4 - If the element in the sorted array is smaller than the current element, then move
to the next element. Else, shift greater elements in the array towards the right.
Selection Sort
1. SELECTION SORT(arr, n)
2.
3. Step 1: Repeat Steps 2 and 3 for i = 0 to n-1
4. Step 2: CALL SMALLEST(arr, i, n, pos)
5. Step 3: SWAP arr[i] with arr[pos]
6. [END OF LOOP]
7. Step 4: EXIT
8.
9. SMALLEST (arr, i, n, pos)
10. Step 1: [INITIALIZE] SET SMALL = arr[i]
11. Step 2: [INITIALIZE] SET pos = i
12. Step 3: Repeat for j = i+1 to n
13. if (SMALL > arr[j])
14. SET SMALL = arr[j]
15. SET pos = j
16. [END OF if]
17. [END OF LOOP]
18. Step 4: RETURN pos
Bubble Sort
1. bubbleSort(array)
2. n = length(array)
3. repeat
4. swapped = false
5. for i = 1 to n - 1
6. if array[i - 1] > array[i], then
7. swap(array[i - 1], array[i])
8. swapped = true
9. end if
10. end for
11. n=n-1
12. until not swapped
13. end bubbleSort
Quick Sort
1. QUICKSORT (array A, start, end)
2. {
3. 1 if (start < end)
4. 2{
5. 3 p = partition(A, start, end)
6. 4 QUICKSORT (A, start, p - 1)
7. 5 QUICKSORT (A, p + 1, end)
8. 6}
9. }
Partition Algorithm:
Merge Sort
1. MERGE_SORT(arr, beg, end)
2.
3. if beg < end
4. set mid = (beg + end)/2
5. MERGE_SORT(arr, beg, mid)
6. MERGE_SORT(arr, mid + 1, end)
7. MERGE (arr, beg, mid, end)
8. end of if
9.
10. END MERGE_SORT
Heap Sort
1. HeapSort(arr)
2. BuildMaxHeap(arr)
3. for i = length(arr) to 2
4. swap arr[1] with arr[i]
5. heap_size[arr] = heap_size[arr] ? 1
6. MaxHeapify(arr,1)
7. End
BuildMaxHeap(arr)
1. BuildMaxHeap(arr)
2. heap_size(arr) = length(arr)
3. for i = length(arr)/2 to 1
4. MaxHeapify(arr,i)
5. End
MaxHeapify(arr,i)
1. MaxHeapify(arr,i)
2. L = left(i)
3. R = right(i)
4. if L ? heap_size[arr] and arr[L] > arr[i]
5. largest = L
6. else
7. largest = i
8. if R ? heap_size[arr] and arr[R] > arr[largest]
9. largest = R
10. if largest != i
11. swap arr[i] with arr[largest]
12. MaxHeapify(arr,largest)
13. End
Experiment No. : 5
2. Algorithm
Insert (TREE, ITEM)
Experiment No. : 6
2. Algorithm:
Experiment No. : 7
2. Algorithm:
Display(Traverse) Operation
1: [Initialize] Set Ptr = Start
2: Repeat Steps 3 And 4 While Ptr != Null
3: Apply Process To Ptr Data
4: Set Ptr = Ptr Next
[End Of Loop]
Step 5: EXIT
Doubly Linked List
Conclusion: Hence we have implemented all the operations of single linked list, Doubly Linked
List and circular Linked List.
Data Structure and Information
Representation
Experiment No. : 8
Find the max number from the two linked lists nodes.
Experiment No. : 9
5. Algorithm
Experiment No. : 10
• Enqueue
• Dequeue
Experiment No. 10
1. Aim: Linked List and Array implementation of queue, double ended queue and circular queue
considering following operations:
Enqueue
Dequeue
2. Algorithm
(rear+1) % MAXSIZE)
Algorithm for Inserting an item at Rear end of the Double Ended Queue
Algorithm for Deleting an item from front end of the Double Ended Queue
Algorithm for Inserting an item at front end of the Double Ended Queue
front=front-1
Algorithm for Deleting an item from a rear end of Double Ended Queue
Experiment No. : 11
• Preorder
• Post order
• Inorder
Experiment No. 11
1. Aim:. Implementation of tree Traversal techniques as given below:
• Preorder
• Post order
• Inorder
2. Algorithm
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)
Algorithm 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)
Algorithm 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.
3. Conclusion and Discussion:
Thus we have implemented creation of Binary Tree and its traversal methods.
Data Structure and Information
Representation
Experiment No. : 12
2. Algorithm
Algorithm for inserting an item in AVL Tree.
A newNode is always inserted as a leaf node with balance factor equal to 0.
1. Let the initial tree be:
Let the node to be inserted be:
2. Go to the appropriate leaf node to insert a newNode using the following recursive
steps. Compare newKey with rootKey of the current tree.
If newKey < rootKey, call insertion algorithm on the left subtree of the current
node until the leaf node is reached.
Else if newKey > rootKey, call insertion algorithm on the right subtree of current
node until the leaf node is reached.
Else, return leafNode.
If balanceFactor < -1, it means the height of the right subtree is greater than
that of the left subtree. So, do right rotation or right-left rotation
5. Locate nodeToBeDeleted
6. There are three cases for deleting a node:
a. If nodeToBeDeleted is the leaf node (ie. does not have any child), then
remove nodeToBeDeleted.
b. If nodeToBeDeleted has one child, then substitute the contents of
nodeToBeDeleted with that of the child. Remove the child.
c. If nodeToBeDeleted has two children, find the inorder successor w of
nodeToBeDeleted (ie. node with a minimum value of key in the right
subtree).
7. Update balanceFactor of the nodes
8. Rebalance the tree if the balance factor of any of the nodes is not equal to -1, 0 or
1.
If balanceFactor of currentNode > 1,
If balanceFactor of leftChild >= 0, do right rotation
Else do left-right rotation.
If balanceFactor of currentNode < -1,
If balanceFactor of rightChild <= 0, do left rotation.
Else do right-left rotation.
Experiment No. : 13
2. Theory
There are two ways in which we represent graphs, these are:
Adjacency Matrix
Adjacency List
Both these have their advantages and disadvantages. In this tutorial, we will cover both of these
graph representation along with how to implement them.
Adjacency Matrix
Adjacency matrix representation makes use of a matrix (table) where the first row and first
column of the matrix denote the nodes (vertices) of the graph. The rest of the cells contains either
0 or 1 (can contain an associated weight w if it is a weighted graph).
Each row X column intersection points to a cell and the value of that cell will help us in
determining that whether the vertex denoted by the row and the vertex denoted by the column are
connected or not. If the value of the cell for v1 X v2 is equal to 1, then we can conclude that
these two vertices v1 and v2 are connected by an edge, else they aren't connected at all.
Consider the given graph below:
The graph shown above is an undirected one and the adjacency matrix for the same looks
as:
Experiment No. : 14
2. Algorithm
Algorithm (Depth First Search)
1. Select any node in the graph. Mark this node as visited, push this node on the stack.
2. Find the adjacent node to the node on top of stack, and which, and which is not yet
visited. (We can use adjacency matrix for it). Make this new node as visited and push
onto the stack.
3. Repeat step 2, until no new adjacent node to the top of the stack, node can be found.
When no new adjacent node can be found, pop the top of the stack.
5. Repeat above steps, if there are any more nodes which are still unvisited.
3. Visit this node, which is at the front of the queue. Delete this node form queue and place
it’s adjacent in node in queue. (We can use adjacency matrix for it). Make this new node
as visited.
4. Repeat step 2 and 3 till queue becomes empty.