Lab Manual Dsa
Lab Manual Dsa
LAB # 01
LAB TASK:
ARRAY:
CODE:
OUTPUT:
LAB # 02
LAB TASK:
POINTERS:
CODE:
OUTPUT:
LAB # 03
LAB TASK:
STACK:
CODE:
OUTPUT:
LAB # 04
LAB TASK:
QUEUE:
CODE:
OUTPUT:
LAB # 05
LAB TASK:
LINKED LIST:
CODE:
OUTPUT
LAB # 06
LAB TASK:
BST: (BINARY SEARCH TREE)
CODE:
OUTPUT
LAB # 07
AVL TREE:
LAB TASK:
Construct AVL tree for the following data 21,26,30,9,4,14,28,18,15,10,2,3,7
SOLUTION:
LAB # 08
LAB TASK:
HEAP TREE
Maximum Heap:
Minimum Heap:
LAB # 09
LAB TASK:
SPANNING TREE
The graph G = (V, E) shown in Fig. 1 is considered for explaining all algorithms discussed in this
article, where V = {v1, v2, v3, v4}, E = {e1, e2, e3, e4, e5}, n = 4, m = 5, and τ(G) = 8. All the spanning
trees of G are shown in Figure.
LAB # 10
LAB TASK:
MIN SPANNING TREE
Sir Syed University of Engineering and Technology 20 | P a g e
(Department of Computer Science & Information Technology)
Data Structures and Algorithms (CS-212) Lab Manual
a) KRUSKAL’S ALGORITHM
1 7 6
2 8 2
2 6 5
4 0 1
4 2 5
6 8 6
7 2 3
7 7 8
8 0 7
8 1 2
9 3 4
10 5 4
11 1 7
14 3 5
Step 6: Pick edge 8-6. Since including this edge results in the cycle, discard it. Pick
edge 2-3: No cycle is formed, include it.
Step 7: Pick edge 7-8. Since including this edge results in the cycle, discard it. Pick
edge 0-7. No cycle is formed, include it.
Step 8: Pick edge 1-2. Since including this edge results in the cycle, discard it. Pick
edge 3-4. No cycle is formed, include it.
Since the number of edges included in the MST equals to (V – 1), so the algorithm stops
here.
b) PRIMS ALGORITHM
Step 1: Firstly, we select an arbitrary vertex that acts as the starting vertex of the
Minimum Spanning Tree. Here we have selected vertex 0 as the starting vertex.
Step 2: All the edges connecting the incomplete MST and other vertices are the edges {0,
1} and {0, 7}. Between these two the edge with minimum weight is {0, 1}. So include
the edge and vertex 1 in the MST.
Step 3: The edges connecting the incomplete MST to other vertices are {0, 7}, {1, 7} and
{1, 2}. Among these edges the minimum weight is 8 which is of the edges {0, 7} and {1,
2}. Let us here include the edge {0, 7} and the vertex 7 in the MST. [We could have also
included edge {1, 2} and vertex 2 in the MST].
Step 4: The edges that connect the incomplete MST with the fringe vertices are {1, 2},
{7, 6} and {7, 8}. Add the edge {7, 6} and the vertex 6 in the MST as it has the least
weight (i.e., 1).
Step 5: The connecting edges now are {7, 8}, {1, 2}, {6, 8} and {6, 5}. Include edge {6,
5} and vertex 5 in the MST as the edge has the minimum weight (i.e., 2) among them.
Step 6: Among the current connecting edges, the edge {5, 2} has the minimum weight.
So include that edge and the vertex 2 in the MST.
Step 7: The connecting edges between the incomplete MST and the other edges are {2,
8}, {2, 3}, {5, 3} and {5, 4}. The edge with minimum weight is edge {2, 8} which has
weight 2. So include this edge and the vertex 8 in the MST.
Step 8: See here that the edges {7, 8} and {2, 3} both have same weight which are
minimum. But 7 is already part of MST. So we will consider the edge {2, 3} and include
that edge and vertex 3 in the MST.
Step 9: Only the vertex 4 remains to be included. The minimum weighted edge from the
incomplete MST to 4 is {3, 4}.
The final structure of the MST is as follows and the weight of the edges of the MST is (4
+ 8 + 1 + 2 + 4 + 2 + 7 + 9) = 37.
LAB # 11
LAB TASK:
DIJKSTRA ALGORITHM
Step 1:
The set sptSet is initially empty and distances assigned to vertices are {0,
INF, INF, INF, INF, INF, INF, INF} where INF indicates infinite.
Now pick the vertex with a minimum distance value. The vertex 0 is picked,
include it in sptSet. So sptSet becomes {0}. After including 0 to sptSet,
update distance values of its adjacent vertices.
Adjacent vertices of 0 are 1 and 7. The distance values of 1 and 7 are
updated as 4 and 8.
Step 2:
Pick the vertex with minimum distance value and not already included
in SPT (not in sptSET). The vertex 1 is picked and added to sptSet.
So sptSet now becomes {0, 1}. Update the distance values of adjacent
vertices of 1.
The distance value of vertex 2 becomes 12.
Step 3:
Pick the vertex with minimum distance value and not already included
in SPT (not in sptSET). Vertex 7 is picked. So sptSet now becomes {0, 1,
7}.
Step 4:
Pick the vertex with minimum distance value and not already included
in SPT (not in sptSET). Vertex 6 is picked. So sptSet now becomes {0, 1, 7,
6}.
Update the distance values of adjacent vertices of 6. The distance value of
vertex 5 and 8 are updated.
We repeat the above steps until sptSet includes all vertices of the given graph. Finally,
we get the following Shortest Path Tree (SPT).
LAB # 12
LAB TASK:
HUFFMAN ENCODING