0% found this document useful (0 votes)
41 views24 pages

DSA (Lalit)

The document contains assignments related to Python programming involving various data structures like matrices, linear lists, linked lists, stacks, queues, trees and graphs. Some key assignments include creating and manipulating matrices and linear lists, performing operations on linked lists, implementing stacks and queues using arrays and linked lists, and assignments related to trees and their applications. The document also provides examples of code and outputs for the various assignments.

Uploaded by

Litta
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)
41 views24 pages

DSA (Lalit)

The document contains assignments related to Python programming involving various data structures like matrices, linear lists, linked lists, stacks, queues, trees and graphs. Some key assignments include creating and manipulating matrices and linear lists, performing operations on linked lists, implementing stacks and queues using arrays and linked lists, and assignments related to trees and their applications. The document also provides examples of code and outputs for the various assignments.

Uploaded by

Litta
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/ 24

Directorate of Distance Education

Guru Jambheshwar University of Science & Technology


Hisar
Session 2021-22

Practical File of Python


Programming
MCA- 26 : Software Lab – Data Structure & Algorithms

Submitted to: Submitted by:

Ms. Kapila Kundu Lalit


Assistant Professor 21151002003
Deptt. Of Computer Science MCA 2nd-Sem

0|Page
INDEX
Sr. No. Content Page No.

1 Assignments related to creating and manipulating matrices and linear 2-4


lists.

2 Assignments associated with linked list, operations on linked lists and 5-8
their applications.

3 Assignments on array and linked implementation of stacks and queues. 9-15

4 Assignments on trees and their applications. 16-20

5 Assignments on graphs and their applications. 21-23

1|Page
1 – Creation of matrices, linear lists and
operations.
Code:

Output:

2|Page
Code:

Output:

3|Page
Code:

Output:-

4|Page
2-Linked list & operations on LL
Code:

5|Page
6|Page
7|Page
Output:

Applications of linked lists :

1. Implementation of stacks and queues.

2. Implementation of graphs: Adjacency list representation of graphs is the most popular

which uses a linked list to store adjacent vertices.

3. Dynamic memory allocation: We use a linked list of free blocks.

4. Maintaining a directory of names.

5. Performing arithmetic operations on long integers.

6. Manipulation of polynomials by storing constants in the node of the linked list.

7. Representing sparse matrices.

8|Page
3- Array and Linked list implementation of
stacks and queues.
Stack using Array
Code: Output:

9|Page
Queue using Array
Code:

10 | P a g e
Output:

11 | P a g e
Queue using Linked list

Code:

12 | P a g e
Output:

13 | P a g e
Stack using Linked list
Code:

14 | P a g e
Output:

15 | P a g e
4-Trees and their applications
Introduction

Trees: Unlike Arrays, Linked Lists, Stack and queues, which are linear data structures, trees are
hierarchical data structures.

Tree Vocabulary: The topmost node is called root of the tree. The elements that are directly
under an element are called its children. The element directly above something is called its
parent. For example, ‘a’ is a child of ‘f’, and ‘f’ is the parent of ‘a’. Finally, elements with no
children are called leaves.

Why Trees?

1. One reason to use trees might be


because you want to store information
that naturally forms a hierarchy. For
example, the file system on a
computer is as in fig.
2. Trees (with some ordering e.g., BST)
provide moderate access/search
(quicker than Linked List and slower
than arrays).
3. Trees provide moderate
insertion/deletion (quicker than
Arrays and slower than Unordered
Linked Lists).
4. Like Linked Lists and unlike Arrays,
Trees don’t have an upper limit on
number of nodes as nodes are linked using pointers.

16 | P a g e
Binary Tree: A tree whose elements have at most 2 children is called a binary tree. Since each
element in a binary tree can have only 2 children, we typically name them the left and right child.

A Tree node contains following parts:

1. Data
2. Pointer to left child
3. Pointer to right child

Properties

1. The maximum number of nodes at level ‘l’ of a binary tree is 2 l.


2. The Maximum number of nodes in a binary tree of height ‘h’ is 2h – 1.
3. In a Binary Tree with N nodes, minimum possible height becomes | Log2(N+1) | – 1.
4. A Binary Tree with L leaves has at least | Log2L? |+ 1 levels.
5. In Binary tree where every node has 0 or 2 children, the number of leaf nodes is always one more than
nodes with two children. L = T + 1

Types of Binary Trees


The following are common types of Binary Trees:

1. Full Binary Tree:


A Binary Tree is a full binary tree if every node has 0 or 2 children.
The following are the examples of a full binary tree. We can also say a
full binary tree is a binary tree in which all nodes except leaf nodes
have two children.

2. Complete Binary Tree:


A Binary Tree is a Complete Binary Tree if all the levels are
completely filled except possibly the last level and the last level has all
keys as left as possible

The following are examples of Complete Binary Trees:

3. Perfect Binary Tree:


A Binary tree is a Perfect Binary Tree in which all the internal
nodes have two children and all leaf nodes are at the same level.

17 | P a g e
The following are the examples of Perfect Binary Trees:

In a Perfect Binary Tree, the number of leaf nodes is the number of internal nodes plus 1
L = I + 1 Where L = Number of leaf nodes, I = Number of internal nodes.
A Perfect Binary Tree of height h (where the height of the binary tree is the longest path from the
root node to any leaf node in the tree, height of root node is 1) has 2h – 1 node.
An example of a Perfect binary tree is ancestors in the family. Keep a person at root, parents as
children, parents of parents as their children.

4. Balanced Binary Tree:


A binary tree is balanced if the height of the tree is O(Log n)
where n is the number of nodes. For Example, the AVL tree
maintains O(Log n) height by making sure that the difference
between the heights of the left and right subtrees is at
most 1. Red-Black trees maintain O(Log n) height by
making sure that the number of Black nodes on every
root to leaf paths is the same and there are no adjacent
red nodes. Balanced Binary Search trees are performance-wise good as they
provide O(log n) time for search, insert and delete.

5. A degenerate (or pathological) tree:


A tree where every internal node has one child. Such trees are performance-wise same as
linked list.

Binary Search Tree:

Binary Search Tree is a node-based binary tree data structure


which has the following properties:

 The left subtree of a node contains only nodes with keys


lesser than the node’s key.
 The right subtree of a node contains only nodes with keys
greater than the node’s key.
 The left and right subtree each must also be a binary search
tree.

B-Tree

18 | P a g e
1. All leaves are at the same level.
2. A B-Tree is defined by the term minimum degree ‘t’. The value of t depends upon disk
block size.

3. Every node except root must contain at least (ceiling)([t-1]/2) keys. The root may contain
minimum 1 key.
4. All nodes (including root) may contain at most t – 1 keys.
5. Number of children of a node is equal to the number of keys in it plus 1.
6. All keys of a node are sorted in increasing order. The child between two keys k1 and k2
contains all keys in the range from k1 and k2.
7. B-Tree grows and shrinks from the root which is unlike Binary Search Tree. Binary
Search Trees grow downward and also shrink from downward.
8. Like other balanced Binary Search Trees, time complexity to search, insert and delete is
O(log n).

AVL tree

AVL tree is a binary search tree


in which the difference of heights of left
and right subtrees of any node is less than
or equal to one. The technique of
balancing the height of binary trees was
developed by Adelson, Velskii, and Landi
and hence given the short form as AVL tree or Balanced Binary Tree.

Main applications of trees:

19 | P a g e
1. Manipulate hierarchical data.
2. Make information easy to search (see tree traversal).
3. Manipulate sorted lists of data.
4. As a workflow for compositing digital images for visual effects.
5. Router algorithms
6. Form of a multi-stage decision-making (see business chess).

Other Applications

1. Store hierarchical data, like folder structure, organization structure, XML/HTML data.
2. Binary Search Tree is a tree that allows fast search, insert, delete on a sorted data. It also
allows finding closest item
3. Heap is a tree data structure which is implemented using arrays and used to implement
priority queues.
4. B-Tree and B+ Tree: They are used to implement indexing in databases.
5. Syntax Tree: Used in Compilers.
6. K-D Tree: A space partitioning tree used to organize points in K dimensional space.
7. Trie: Used to implement dictionaries with prefix lookup.
8. Suffix Tree: For quick pattern searching in a fixed text.
9. Spanning Trees and shortest path trees are used in routers and bridges respectively in
computer networks
10. As a workflow for compositing digital images for visual effects.

20 | P a g e
5-Graph Data Stucture
In this tutorial, you will learn what a Graph Data Structure is. Also, you will find representations
of a graph.

A graph data structure is a collection of nodes that have data and are connected to other nodes.

Let's try to understand this through an example. On facebook, everything is a node. That includes
User, Photo, Album, Event, Group, Page, Comment, Story, Video, Link, Note...anything that has
data is a node.

Every relationship is an edge from one node to another. Whether you post a photo, join a group,
like a page, etc., a new edge is created for that relationship.

Example of graph data structure

All of facebook is then a collection of these nodes and edges. This is because facebook uses a
graph data structure to store its data.

More precisely, a graph is a data structure (V, E) that consists of

 A collection of vertices V
 A collection of edges E, represented as ordered pairs of vertices
(u,v)

Vertices and edges


In the graph,
V = {0, 1, 2, 3}
E = {(0,1), (0,2), (0,3), (1,2)}
G = {V, E}

21 | P a g e
Graph Terminology

 Adjacency: A vertex is said to be adjacent to another vertex if there is an edge connecting them.
Vertices 2 and 3 are not adjacent because there is no edge between them.
 Path: A sequence of edges that allows you to go from vertex A to vertex B is called a path. 0-1,
1-2 and 0-2 are paths from vertex 0 to vertex 2.
 Directed Graph: A graph in which an edge (u,v) doesn't necessarily mean that there is an edge
(v, u) as well. The edges in such a graph are represented by arrows to show the direction of the
edge.

Graph Representation

Graphs are commonly represented in two ways:

1. Adjacency Matrix

An adjacency matrix is a 2D array of V x V vertices. Each row and column represent a vertex.

If the value of any element a[i][j] is 1, it represents that there is an edge connecting vertex i and
vertex j.

The adjacency matrix for the graph we created above is

Graph adjacency matrix

Since it is an undirected graph, for edge (0,2), we also need to mark edge (2,0); making the
adjacency matrix symmetric about the diagonal.

22 | P a g e
Edge lookup(checking if an edge exists between vertex A and vertex B) is extremely fast in
adjacency matrix representation but we have to reserve space for every possible link between all
vertices(V x V), so it requires more space.

2. Adjacency List

An adjacency list represents a graph as an array of linked lists.

The index of the array represents a vertex and each element in its linked list represents the other
vertices that form an edge with the vertex.

The adjacency list for the graph we made in the first example is as follows:

Adjacency list representation

An adjacency list is efficient in terms of storage because we only need to store the values for the
edges. For a graph with millions of vertices, this can mean a lot of saved space.

Graph Operations

The most common graph operations are:

 Check if the element is present in the graph


 Graph Traversal
 Add elements(vertex, edges) to graph
 Finding the path from one vertex to another

23 | P a g e

You might also like