0% found this document useful (0 votes)
21 views21 pages

Dsa Theory

Uploaded by

hike.praji
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)
21 views21 pages

Dsa Theory

Uploaded by

hike.praji
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/ 21

KATHMANDU NATIONAL COLLEGE

New-Baneshwor, Kathmandu

Lab Reports of Data Structure And Algorithms


(CACST-202)
For the partial fulfillment of Bachelor’s Degree of Computer Applications

SUBMITTED BY:

Karan Bhujel
BCA, Third Semester
Symbol No.: ………………
TU Registration No.: ……………………….

SUBMITTED TO:

Kathmandu National College


Department of Bachelor in Computer Application
Affiliated to Tribhuvan University
2079
KATHMANDU NATIONAL COLLEGE
New-Baneshwor, Kathmandu
This is to certify that the Lab Reports prepared entitled " Data Structure and
Algorithm " is an academic work done by " Karan Bhujel" submitted in the
partial fulfillment of the requirements for the degree of Bachelor of Computer
Applications at Faculty of Humanities and Social Science, Tribhuvan University
under my guidance and supervision. To the best of my knowledge, the work
performed by him/her in the lab reports is his/her own creation.

----------------------- ------------------------
Signature Signature

Subject Teacher HOD/Coordinator/Principal

BCA BCA

----------------------- -----------------------
Signature Signature

Internal Examiner External Examiner


Abstract

It is mine pleasure that I am presenting the report of Data Structure And


Algorithm. It is the syllabus of Bachelor in Computer Application (BCA), Third
Semester program affiliate to Tribhuvan University. The main objectives to learn
Data structures and algorithms (DSA) goes through solutions to standard problems in
detail and gives you an insight into how efficient it is to use each one of them. It also
teaches you the science of evaluating the efficiency of an algorithm. This enables you
to choose the best of various choices.
ACKNOWLEDGEMENT
I would like to express my special thanks of gratitude to my teacher Shambhu
Gautam sir as well as college management who gave me the golden opportunity to do
this lab reports on, which help Data Structure and Algorithm me doing a lot of
research and I came to know about so many things I am really thankful to them.

Secondly, I would also like to thank to my parents and friends who helped me a lot in
finalizing this lab assessment within the limited time frame.
Table of content:
Contents
CHAPTER 1.....................................................................................................................................
INTRODUCTION:.......................................................................................................................
1.1 DATA STRUCTURE:...................................................................................................
1.2 STACK...........................................................................................................................
1.3 QUEUE..........................................................................................................................
1.4 LIST....................................................................................................................................
1.5 RECURSION......................................................................................................................
CHAPTER 2.....................................................................................................................................
2.1TREE...................................................................................................................................
2.2 SEARCH.............................................................................................................................
2.3GRAPH................................................................................................................................
2.4SORTING............................................................................................................................
CHAPTER 3.....................................................................................................................................
3.1 ALOGRITHM....................................................................................................................
CHAPTER 4.....................................................................................................................................
4.1 RESULT AND ANALYSIS:..............................................................................................
4.2 CODING AND OUTPUTS:...............................................................................................
CONCLUSION:...............................................................................................................................
REFERENCE:..................................................................................................................................
Web Technology

CHAPTER 1
INTRODUCTION:
1.1 DATA STRUCTURE:
What is data structure?

 It can be defined as the group of data elements which provides an efficient


way of storing and organizing data in the computer so that it can be used
efficiently.
 Basically , it is a technique of organizing and storing of different types of data
items in the computer memory.
 Maintaining of logical relationship between data elements.
 Example: Array, stack queue, list ,tree etc.

Data structure mainly specifies:


 Organization of data
 Accessing methods
 Degree of associativity
 Processing alternative for information
Program= Algorithm + Data structure

1.2 STACK
A stack is a linear data structure that follows the principle of last in first
out(LIFO).This means the last elements inserted inside the stack is removed
first. This strategy states that the elements that is inserted last will come out first.
Real life example:
 Pile of plates
 Pile of playing card

Page6
Web Technology

Abstract Data Structure(ADT) Operations;


1) Push : Add an elements to the top of a stack.
2) Pop : Remove the elements from the top of the stack.
3) IsEmpty: Check if the stack is empty.
4) IsFull: Check if the stack is full.
5) Peek: Get the value of the top elements without removing it.

Page7
Web Technology

1.3 QUEUE
1) A Queue is a linear structure which fallows First in First out (FIFO) mechanism
for insert and remove elements. It is open at both ends.
2) One end is used to insert data (called Enqueue) and another end is used to remove
data (called Dequeue). It uses two variables rear (Tail) and front (head).
3) Rear will be increment while inserting element and front is increment while
deleting element in queue.
Applications of Queue:
1) Queue is used as waiting list for a single shared resource like printer.
2) Queue is used in asynchronous transfer of data (data transfer rate is not same).
3) Call center to hold people calling them in an order.
4) Used in OS for handling interrupts.

Operation of Queue / Queue as ADT:


a. MakeEmpty(q): To make an empty queue named q.
b. IsEmpty(q): To check whether the queue q is empty or not? Return true if queue is
empty else return false.
c. IsFull(q): To check whether the queue q is full or not? Return true if queue is full
else return
false.
d. Enqueue(q,x): To insert item(element) x in queue q from rear if and only if q is not
full.
e. Dequeue(q): To delete an item from the front of the queue q. if and only if q is not
empty.
f. Peek(q) : Gets the element at the front without removing .
g. Traverse(q) or Display(q): To Read entire elements from queue q.

Ref link: https://www.tutorialspoint.com/data_structures_algorithms/dsa_queue.htm

1.4 LIST
List is an ordered data structure that is used to store different or same elements in
a sequential manner. The list is similar to array in data structure but in the array, we
can store only the elements which are of same data-type whereas in the list in the data
structure, with some of the programming languages like python and javascript we can
store elements with different data types also

Ordered means= each elements has a position in the list.

Page8
Web Technology

Operation on list:

1. Creating of a list: Declaring memory for list then insert elements in list.
2. Inserting a new elements at required position of list.
3. Delection of any elements from list.
4. Modification of any elements from list.
5. Traversing of list.
6. Merging of list

1.5 RECURSION
The process in which a function calls itself directly or indirectly is called recursion
and the corresponding function is called a recursive function. Using a recursive
algorithm, certain problems can be solved quite easily

1.6 Linked list


A linked list is the most sought-after data structure when it comes to handling
dynamic data elements. A linked list consists of a data element known as a node.
And each node consists of two fields: one field has data, and in the second field,
the node has an address that keeps a reference to the next node.

Page9
Web Technology

What is a Linked List?

 A linked list is a linear data structure that stores a collection of data elements
dynamically.

 Nodes represent those data elements, and links or pointers connect each node.

 Each node consists of two fields, the information stored in a linked list and a pointer
that stores the address of its next node.

 The last node contains null in its second field because it will point to no node.

 A linked list can grow and shrink its size, as per the requirement.
 It does not waste memory space.

Essential Operation on Linked Lists

 Traversing: To traverse all nodes one by one.


 Insertion: To insert new nodes at specific positions.
 Deletion: To delete nodes from specific positions.
 Searching: To search for an element from the linked list.

Types of Linked Lists


The linked list mainly has three types, they are:

1. Singly Linked List

2. Doubly Linked List

3. Circular Linked List

Singly Linked List


A singly linked list is the most common type of linked list. Each node has data and an
address field that contains a reference to the next node.

Page10
Web Technology

Doubly Linked List


There are two pointer storage blocks in the doubly linked list. The first pointer block in
each node stores the address of the previous node. Hence, in the doubly linked
inventory, there are three fields that are the previous pointers, that contain a reference to
the previous node. Then there is the data, and last you have the next pointer, which
points to the next node. Thus, you can go in both directions (backward and forward).

Circular Linked List


The circular linked list is extremely similar to the singly linked list. The only difference
is that the last node is connected with the first node, forming a circular loop in the
circular linked list.

Page11
Web Technology

Circular link lists can either be singly or doubly-linked lists.

 The next node's next pointer will point to the first node to form a singly linked list.
 The previous pointer of the first node keeps the address of the last node to form a
doubly-linked list.

CHAPTER 2
2.1TREE
A tree data structure is a hierarchical structure that is used to represent and organize data in
a way that is easy to navigate and search. It is a collection of nodes that are connected by
edges and has a hierarchical relationship between the nodes.

Page12
Web Technology

Binary Tree
The Binary tree means that the node can have maximum two children.
Here, binary name itself suggests that 'two'; therefore, each node can have
either 0, 1 or 2 children.

Let's understand the binary tree through an example.

Page13
Web Technology

The above tree is a binary tree because each node contains the utmost two
children.

What is a Binary Search tree?


A binary search tree follows some order to arrange the elements. In a
Binary search tree, the value of left node must be smaller than the parent
node, and the value of right node must be greater than the parent node.
This rule is applied recursively to the left and right subtrees of the root.

Let's understand the concept of Binary search tree with an example.

In the above figure, we can observe that the root node is 40, and all the
nodes of the left subtree are smaller than the root node, and all the nodes
of the right subtree are greater than the root node.

2.2GRAPH
What is Graph Data Structure?
A Graph is a non-linear data structure consisting of vertices and edges. The vertices
are sometimes also referred to as nodes and the edges are lines or arcs that connect
any two nodes in the graph. More formally a Graph is composed of a set of
vertices( V ) and a set of edges( E ). The graph is denoted by G(E, V).

Components of a Graph
Vertices: Vertices are the fundamental units of the graph. Sometimes, vertices are
also known as vertex or nodes. Every node/vertex can be labeled or unlabelled.

Edges: Edges are drawn or used to connect two nodes of the graph. It can be ordered
pair of nodes in a directed graph. Edges can connect any two nodes in any possible

Page14
Web Technology

way. There are no rules. Sometimes, edges are also known as arcs. Every edge can be
labeled/unlabelled.

Types of graph:
Undirected Graphs: A graph in which edges have no direction, i.e., the edges do not
have arrows indicating the direction of traversal. Example: A social network graph
where friendships are not directional.

Directed Graphs: A graph in which edges have a direction, i.e., the edges have
arrows indicating the direction of traversal. Example: A web page graph where links
between pages are directional.

Weighted Graphs: A graph in which edges have weights or costs associated with
them. Example: A road network graph where the weights can represent the distance
between two cities.

Unweighted Graphs: A graph in which edges have no weights or costs associated


with them. Example: A social network graph where the edges represent friendships.

2.3SORTING
What is Sorting?
A Sorting Algorithm is used to rearrange a given array or list of elements according
to a comparison operator on the elements. The comparison operator is used to decide
the new order of elements in the respective data structure.

Selection sort is a simple and efficient sorting algorithm that works by repeatedly
selecting the smallest (or largest) element from the unsorted portion of the list and
moving it to the sorted portion of the list.

Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the
adjacent elements if they are in the wrong order. This algorithm is not suitable for
large data sets as its average and worst-case time complexity is quite high.

Insertion sort is a simple sorting algorithm that works similar to the way you sort
playing cards in your hands. The array is virtually split into a sorted and an unsorted
part. Values from the unsorted part are picked and placed at the correct position in
the sorted part.

Page15
Web Technology

Merge sort is defined as a sorting algorithm that works by dividing an array into
smaller subarrays, sorting each subarray, and then merging the sorted subarrays back
together to form the final sorted array.

Quick Sort is a sorting algorithm based on the Divide and Conquer algorithm that
picks an element as a pivot and partitions the given array around the picked pivot by
placing the pivot in its correct position in the sorted array.

Heap sort is a comparison-based sorting technique based on Binary Heap data


structure. It is similar to the selection sort where we first find the minimum element
and place the minimum element at the beginning. Repeat the same process for the
remaining elements.

CHAPTER 3
3.1 ALOGRITHM
Algorithm is a step-by-step procedure, which defines a set of instructions to be
executed in a certain order to get the desired output. Algorithms are generally
created independent of underlying languages, i.e. an algorithm can be
implemented in more than one programming language.

Types of Algorithm

Based on how they function, we can divide Algorithms into multiple types. Let’s
take a look at some of the important ones.
1. Recursive Algorithm
This is one of the most interesting Algorithms as it calls itself with a smaller
value as inputs which it gets after solving for the current inputs. In more simpler
words, It’s an Algorithm that calls itself repeatedly until the problem is solved.

2. Divide and Conquer Algorithm


This is another effective way of solving many problems. In Divide and Conquer
algorithms, divide the algorithm into two parts, the first parts divides the problem
on hand into smaller subproblems of the same type. Then on the second part,
these smaller problems are solved and then added together (combined) to
produce the final solution of the problem.
Example:
Merge sort , Quick sort

Page16
Web Technology

3. Greedy Algorithm
These algorithms are used for solving optimization problems. In this algorithm,
we find a locally optimum solution (without any regard for any consequence in
future) and hope to find the optimal solution at the global level.

The method does not guarantee that we will be able to find an optimal solution.
The algorithm has 5 components:
The first one is a candidate set from which we try to find a solution.
A selection function which helps choose the best possible candidate.
A feasibility function which helps in deciding if the candidate can be used to find
a solution.
An objective function which assigns value to a possible solution or to a partial
solution
Solution function that tells when we have found a solution to the problem.
Huffman Coding and Dijkstra’s algorithm are two prime examples where Greedy
algorithm is used.
In Huffman coding, The algorithm goes through a message and depending on the
frequency of the characters in that message, for each character, it assigns a
variable length encoding. To do Huffman coding, we first need to build a
Huffman tree from the input characters and then traverse through the tree to
assign codes to the characters.
Example:
Huffman tree, Counting money

4 .Brute Force Algorithm


This is one of the simplest algorithms in the concept. A brute force algorithm
blindly iterates all possible solutions to search one or more than one solution that
may solve a function. Think of brute force as using all possible combinations of
numbers to open a safe.
Example:

a. Exact string

b. Matching algorithm

5. Backtracking Algorithm
Backtracking is a technique to find a solution to a problem in an incremental
approach. It solves problems recursively and tries to get to a solution to a
problem by solving one piece of the problem at a time. If one of the solutions
fail, we remove it and backtrack to find another solution.

Page17
Web Technology

In other words, a backtracking algorithm solves a subproblem and if it fails to


solve the problem, it undoes the last step and starts again to find the solution to
the problem.
N Queens problem is one good example to see Backtracking algorithm in action.
The N Queen Problem states that there are N pieces of Queens in a chess board
and we have to arrange them so that no queen can attack any other queen in the
board once organized.

Page18
Web Technology

CHAPTER 4
4.1 RESULT AND ANALYSIS:
A result is the final consequence of action or events expressed qualitatively or
quantitatively. Performance analysis is an operational analysis, is a set of basic
quantitative relationship between the performance quantities.

4.2 CODING AND OUTPUTS:

Page19
Web Technology

CONCLUSION:
Programming is all about data structures and algorithms. Data structures are
used to hold data while algorithms are used to solve the problem using that
data.
Data structures and algorithms (DSA) goes through solutions to standard
problems in detail and gives you an insight into how efficient it is to use each
one of them. It also teaches you the science of evaluating the efficiency of an
algorithm. This enables you to choose the best of various choices.

Page20
Web Technology

REFERENCE:

Page21

You might also like