Dsa Theory
Dsa Theory
New-Baneshwor, Kathmandu
SUBMITTED BY:
Karan Bhujel
BCA, Third Semester
Symbol No.: ………………
TU Registration No.: ……………………….
SUBMITTED TO:
----------------------- ------------------------
Signature Signature
BCA BCA
----------------------- -----------------------
Signature Signature
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?
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
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.
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
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
Page9
Web Technology
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.
Page10
Web Technology
Page11
Web Technology
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.
Page13
Web Technology
The above tree is a binary tree because each node contains the utmost two
children.
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.
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.
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.
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
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
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.
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