0% found this document useful (0 votes)
49 views36 pages

Lab Manual Dsa

The document is a lab manual for a course on data structures and algorithms. It outlines 10 labs covering topics like arrays, pointers, stacks, queues, linked lists, binary search trees, AVL trees, heap trees, spanning trees, and minimum spanning trees. For each lab there is a task description, code snippets, and output examples. Algorithms covered include Kruskal's algorithm and Prim's algorithm for finding minimum spanning trees.

Uploaded by

Javeria Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views36 pages

Lab Manual Dsa

The document is a lab manual for a course on data structures and algorithms. It outlines 10 labs covering topics like arrays, pointers, stacks, queues, linked lists, binary search trees, AVL trees, heap trees, spanning trees, and minimum spanning trees. For each lab there is a task description, code snippets, and output examples. Algorithms covered include Kruskal's algorithm and Prim's algorithm for finding minimum spanning trees.

Uploaded by

Javeria Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 36

Data Structures and Algorithms (CS-212) Lab Manual

LAB # 01
LAB TASK:
ARRAY:
CODE:

Sir Syed University of Engineering and Technology 1|Page


(Department of Computer Science & Information Technology)
Data Structures and Algorithms (CS-212) Lab Manual

OUTPUT:

Sir Syed University of Engineering and Technology 2|Page


(Department of Computer Science & Information Technology)
Data Structures and Algorithms (CS-212) Lab Manual

LAB # 02
LAB TASK:
POINTERS:
CODE:

Sir Syed University of Engineering and Technology 3|Page


(Department of Computer Science & Information Technology)
Data Structures and Algorithms (CS-212) Lab Manual

OUTPUT:

Sir Syed University of Engineering and Technology 4|Page


(Department of Computer Science & Information Technology)
Data Structures and Algorithms (CS-212) Lab Manual

LAB # 03
LAB TASK:
STACK:
CODE:

Sir Syed University of Engineering and Technology 5|Page


(Department of Computer Science & Information Technology)
Data Structures and Algorithms (CS-212) Lab Manual

OUTPUT:

Sir Syed University of Engineering and Technology 6|Page


(Department of Computer Science & Information Technology)
Data Structures and Algorithms (CS-212) Lab Manual

LAB # 04
LAB TASK:
QUEUE:
CODE:

Sir Syed University of Engineering and Technology 7|Page


(Department of Computer Science & Information Technology)
Data Structures and Algorithms (CS-212) Lab Manual

OUTPUT:

Sir Syed University of Engineering and Technology 8|Page


(Department of Computer Science & Information Technology)
Data Structures and Algorithms (CS-212) Lab Manual

LAB # 05
LAB TASK:
LINKED LIST:
CODE:

Sir Syed University of Engineering and Technology 9|Page


(Department of Computer Science & Information Technology)
Data Structures and Algorithms (CS-212) Lab Manual

Sir Syed University of Engineering and Technology 10 | P a g e


(Department of Computer Science & Information Technology)
Data Structures and Algorithms (CS-212) Lab Manual

OUTPUT

Sir Syed University of Engineering and Technology 11 | P a g e


(Department of Computer Science & Information Technology)
Data Structures and Algorithms (CS-212) Lab Manual

LAB # 06
LAB TASK:
BST: (BINARY SEARCH TREE)
CODE:

Sir Syed University of Engineering and Technology 12 | P a g e


(Department of Computer Science & Information Technology)
Data Structures and Algorithms (CS-212) Lab Manual

Sir Syed University of Engineering and Technology 13 | P a g e


(Department of Computer Science & Information Technology)
Data Structures and Algorithms (CS-212) Lab Manual

OUTPUT

Sir Syed University of Engineering and Technology 14 | P a g e


(Department of Computer Science & Information Technology)
Data Structures and Algorithms (CS-212) Lab Manual

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:

Sir Syed University of Engineering and Technology 15 | P a g e


(Department of Computer Science & Information Technology)
Data Structures and Algorithms (CS-212) Lab Manual

Sir Syed University of Engineering and Technology 16 | P a g e


(Department of Computer Science & Information Technology)
Data Structures and Algorithms (CS-212) Lab Manual

LAB # 08
LAB TASK:
HEAP TREE
Maximum Heap:

Sir Syed University of Engineering and Technology 17 | P a g e


(Department of Computer Science & Information Technology)
Data Structures and Algorithms (CS-212) Lab Manual

Minimum Heap:

Sir Syed University of Engineering and Technology 18 | P a g e


(Department of Computer Science & Information Technology)
Data Structures and Algorithms (CS-212) Lab Manual

LAB # 09
LAB TASK:
SPANNING TREE

Sir Syed University of Engineering and Technology 19 | P a g e


(Department of Computer Science & Information Technology)
Data Structures and Algorithms (CS-212) Lab Manual

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.

A simple undirected connected graph (G) where n = 4 and m = 5

Eight Spanning Trees of G

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

Weight Source Destination

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 1: Pick edge 7-6. No cycle is formed, include it.

Sir Syed University of Engineering and Technology 21 | P a g e


(Department of Computer Science & Information Technology)
Data Structures and Algorithms (CS-212) Lab Manual

Step 2: Pick edge 8-2. No cycle is formed, include it.

Sir Syed University of Engineering and Technology 22 | P a g e


(Department of Computer Science & Information Technology)
Data Structures and Algorithms (CS-212) Lab Manual

Step 3: Pick edge 6-5. No cycle is formed, include it.

Step 4: Pick edge 0-1. No cycle is formed, include it.

Step 5: Pick edge 2-5. No cycle is formed, include it.

Sir Syed University of Engineering and Technology 23 | P a g e


(Department of Computer Science & Information Technology)
Data Structures and Algorithms (CS-212) Lab Manual

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.

Sir Syed University of Engineering and Technology 24 | P a g e


(Department of Computer Science & Information Technology)
Data Structures and Algorithms (CS-212) Lab Manual

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

Sir Syed University of Engineering and Technology 25 | P a g e


(Department of Computer Science & Information Technology)
Data Structures and Algorithms (CS-212) Lab Manual

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].

Sir Syed University of Engineering and Technology 26 | P a g e


(Department of Computer Science & Information Technology)
Data Structures and Algorithms (CS-212) Lab Manual

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.

Sir Syed University of Engineering and Technology 27 | P a g e


(Department of Computer Science & Information Technology)
Data Structures and Algorithms (CS-212) Lab Manual

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.

Sir Syed University of Engineering and Technology 28 | P a g e


(Department of Computer Science & Information Technology)
Data Structures and Algorithms (CS-212) Lab Manual

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}.

Sir Syed University of Engineering and Technology 29 | P a g e


(Department of Computer Science & Information Technology)
Data Structures and Algorithms (CS-212) Lab Manual

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.

Sir Syed University of Engineering and Technology 30 | P a g e


(Department of Computer Science & Information Technology)
Data Structures and Algorithms (CS-212) Lab Manual

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}.

Sir Syed University of Engineering and Technology 31 | P a g e


(Department of Computer Science & Information Technology)
Data Structures and Algorithms (CS-212) Lab Manual

 Update the distance values of adjacent vertices of 7. The distance value of


vertex 6 and 8 becomes finite (15 and 9 respectively).

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).

Sir Syed University of Engineering and Technology 32 | P a g e


(Department of Computer Science & Information Technology)
Data Structures and Algorithms (CS-212) Lab Manual

LAB # 12
LAB TASK:
HUFFMAN ENCODING

Sir Syed University of Engineering and Technology 33 | P a g e


(Department of Computer Science & Information Technology)
Data Structures and Algorithms (CS-212) Lab Manual

Sir Syed University of Engineering and Technology 34 | P a g e


(Department of Computer Science & Information Technology)
Data Structures and Algorithms (CS-212) Lab Manual

Sir Syed University of Engineering and Technology 35 | P a g e


(Department of Computer Science & Information Technology)
Data Structures and Algorithms (CS-212) Lab Manual

Sir Syed University of Engineering and Technology 36 | P a g e


(Department of Computer Science & Information Technology)

You might also like