0% found this document useful (0 votes)
2 views

unit - 1

The document outlines the objectives and course outcomes of a Data Structure and Algorithms course at Rajarshi Janak University, focusing on basic concepts, searching and sorting techniques, and various data structures like stacks, queues, lists, trees, and graphs. It discusses data representation, classification of data structures, operations on data structures, and the importance of pointers and dynamic memory allocation. Additionally, it covers asymptotic notations for analyzing algorithm complexity, including Big-O, Omega, and Theta notations.

Uploaded by

ashokkmahato2024
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

unit - 1

The document outlines the objectives and course outcomes of a Data Structure and Algorithms course at Rajarshi Janak University, focusing on basic concepts, searching and sorting techniques, and various data structures like stacks, queues, lists, trees, and graphs. It discusses data representation, classification of data structures, operations on data structures, and the importance of pointers and dynamic memory allocation. Additionally, it covers asymptotic notations for analyzing algorithm complexity, including Big-O, Omega, and Theta notations.

Uploaded by

ashokkmahato2024
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 20

DATA STRUCTURE AND ALGORITHM

RAJARSHI JANAK UNIVERSITY


JANAKPURDHAM
BATCH – 2076
DATA STRUCTURE AND ALGORITHM

OBJECTIVE OF DATA STRUCTURE AND ALGORITHMS


• TO IMPART THE BASIC CONCEPTS OF DATA STRUCTURE AND ALGORITHMS.
• TO UNDERSTAND ABOUT SEARCHING AND SORTING TECHNIQUES.
• TO UNDERSTAND THE BASIC CONCEPTS ABOUT STACKS, QUEUES, LISTS TREES AND GRAPHS.
• TO UNDERSTANDING ABOUT WRITING ALGORITHMS AND STEP BY STEP APPROACH IN SOLVING
PROBLEMS WITH THE HELP OF FUNDAMENTAL DATA STRUCTURE.
COURSE OUTCOMES
1. ABILITY TO ANALYZE ALGORITHMS AND ALGORITHM CORRECTNESS.
2. ABILITY TO SUMMARIZE SEARCHING AND SORTING TECHNIQUES.
3. ABILITY TO DESCRIBE STACKS, QUEUES, AND LINKED LISTS OPERATION.
4. ABILITY TO HAVE KNOWLEDGE OF TREE AND GRAPHS CONCEPTS.

UNIT – 1
Introduction to Data Structure
Data
 Data is the basic fact or entity that is utilized in calculation or manipulation.
 There are two different types of data Numeric data and Alphanumeric data.
 When a programmer collects such type of data for processing, he would require to store them in
computer’s main memory.
 The process of storing data items in computer’s main memory is called representation.
 Data to be processed must be organized in a particular fashion, these organization leads to
structuring of data, and hence the mission to study the Data Structures starts.
Data structure
 Data Structure is a representation of the logical relationship existing between individual
elements of data.
 In other words, a data structure is a way of organizing all data items that considers not only the
elements stored but also their relationship to each other.
 We can also define data structure as a mathematical or logical model of a particular
organization of data items.
 Data Structure mainly specifies the following four things
 Organization of Data
 Accessing Methods
 Degree of Associativity
 Processing alternatives for information

BY:- P.K. Sah Page 1


DATA STRUCTURE AND ALGORITHM

 The representation of a particular data structure in the memory of a computer is called


Storage Structure.
 The storage structure representation in auxiliary memory is called as File Structure.

Classification of Data Structure

Primitive / Non-primitive data structures


 Primitive data structures
 Primitive data structures are basic structures and directly operated upon by machine
instructions.
 Integers, floats, character and pointers are examples of primitive data structures.
 Non primitive data structure
 These are derived from primitive data structures.
 The non-primitive data structures emphasize on structuring of a group of homogeneous
or heterogeneous data items.
 Examples of Non-primitive data type are Array, List, and File.

Non primitive Data Structure


 Array: An array is a fixed-size sequenced collection of elements of the same data type.
 List: An ordered set containing variable number of elements is called as Lists.
 File: A file is a collection of logically related information. It can be viewed as a large list of
records consisting of various fields.

BY:- P.K. Sah Page 2


DATA STRUCTURE AND ALGORITHM

Linear / Non-Linear data structure


 Linear data structures
 A data structure is said to be Linear, if its elements are connected in linear fashion by
means of logically or in sequence memory locations.
 Examples of Linear Data Structure are Stack and Queue.
 Nonlinear data structures
 Nonlinear data structures are those data structure in which data items are not arranged
in a sequence.
 Examples of Non-linear Data Structure are Tree and Graph.

BY:- P.K. Sah Page 3


DATA STRUCTURE AND ALGORITHM

Abstract Data type


Abstract Data type (ADT) is a type (or class) for objects whose behavior is defined by a set of value and a
set of operations.
The definition of ADT only mentions what operations are to be performed but not how these operations
will be implemented. It does not specify how data will be organized in memory and what algorithms will
be used for implementing the operations. It is called “abstract” because it gives an implementation-
independent view. The process of providing only the essentials and hiding the details is known as
abstraction.

Example:
Abstract / logical view
Implementation view
- Store a set of elements of integer type.
int arr[5] = {1,2,3,4,5};
- Read elements by position i.e. index
printf(“%d”,a[1]);
- Modify elements by index
a[2] = 20;
- Perform sorting
and many more….

BY:- P.K. Sah Page 4


DATA STRUCTURE AND ALGORITHM

Operations of Data Structure


 Create: It results in reserving memory for program elements.
 Destroy: It destroys memory space allocated for specified data structure.
 Selection: It deals with accessing a particular data within a data structure.
 Updation: It updates or modifies the data in the data structure.
 Searching: It finds the presence of desired data item in the list of data items.
 Sorting: It is a process of arranging all data items in a data structure in a particular order.
 Merging: It is a process of combining the data items of two different sorted list into a single
sorted list.
 Splitting: It is a process of partitioning single list to multiple list.
 Traversal: It is a process of visiting each and every node of a list in systematic manner.

Overview of Arrays
 Array is a container which can hold a fix number of items and these items should be of the same
type. Following are the important terms to understand arrays:
• Element − Each item stored in an array is called an element.
• Index − Each location of an element in an array has a numerical index,
which is used to identify the element.
 Array Representation

Basic Operations on array


Following are the basic operations supported by an array.
• Traverse
• Insertion
• Deletion
• Search
• Update

BY:- P.K. Sah Page 5


DATA STRUCTURE AND ALGORITHM

Self referential structure

 Self Referential structures are those structures that have one or more pointers which point to
the same type of structure, as their member.

Types of self referential data structure


1. self referential data structure with single link
2. Self referential data structure with multiple link

Union
 A union is a special data type available in C that allows to store different data types in the same
memory location. You can define a union with many members, but only one member can
contain a value at any given time. Unions provide an efficient way of using the same memory
location for multiple-purpose.
union [union tag] {
member definition;
member definition; ...
member definition;
} [one or more union variables];
Example :-
union Data {
int i;
float f;
char str[20];
} data;

BY:- P.K. Sah Page 6


DATA STRUCTURE AND ALGORITHM

Pointers and Dynamic Memory Allocation


 Pointers are the variables that are used to store the location of value present in the memory. A
pointer to a location stores its memory address. The process of obtaining the value stored at a
location being referenced by a pointer is known as dereferencing.
Need of pointers
Optimization of our code and improving the time complexity of one algorithm. Since using pointers
helps to reduce the time needed by an algorithm to copy data from one place to another. Since it used
the memory locations directly, any change made to the value will be reflected at all the locations.

It can be done by using two methods


(i) call by value
Here the values of actual parameter should be copied to formal parameters and these two different
parameters stores the values in two different memory locations.
Eg.
Int fun(int x,int y)
int x = 10, y = 20; {
fun(x,y); x= 20;
printf(“x=%d,y=%d”,x,y) y = 10
}
(ii) call by reference
Actual and formal parameters refers to same memory location. Instead of passing values, we pass
addresses in it.
Int fun(int *ptr1,int *ptr2)
Eg. int x, y;
{
fun(&x,&y); *ptr1 = 20;
*ptr2 = 10
}
Dynamic memory allocation
 Many programming languages use dynamic memory allocations to allocate the memory for run-
time variables. For such type of memory allocations heap is used rather than the stack, which
uses pointers. Here pointers hold the address of these dynamically generated data blocks or
array of objects.

Syntax:-
<datatype> *variable_name

BY:- P.K. Sah Page 7


DATA STRUCTURE AND ALGORITHM

Example :-

 int *ptr1 – ptr1 references to a memory location that holds data of int datatype.
int var = 30;
int *ptr1 = &var; // pointer to var
int **ptr2 = & ptr1; // pointer to pointer variable ptr1
 print(“%d”, *ptr1) // prints 30
print(“%d”,**ptr2) // prints 30

Types of Array

Array

Linear Array (One Dimensional Array) Multi Dimensional Array

Multidimensional Array
 In C programming, you can create an array of arrays. These arrays are known as
multidimensional arrays.
 For example:-
float x[3][4];

BY:- P.K. Sah Page 8


DATA STRUCTURE AND ALGORITHM

Representation of Linear Array in Memory


A linear array is a list of finite numbers of elements stored in the memory. In a linear array, we can store
only homogeneous data elements. Elements of the array form a sequence or linear list that can have
the same type of data.
Each element of the array is referred by an index set. And, the total number of elements in the array list
is the length of an array. We can access these elements with the help of the index set
 We can perform various operations on a linear array:
 Traversing- processing each element of the array list.
 Inserting- adding new elements in the array list.
 Deleting- removing an element from the array list.
 Sorting- arranging the elements of the list in some sorting order.
 Merging- combining the elements of two array lists in a single array

BY:- P.K. Sah Page 9


DATA STRUCTURE AND ALGORITHM

Asymptotic Notations
Asymptotic Notations are the expressions that are used to represent the complexity of an algorithm.
There are three types of analysis that we perform on a particular algorithm.
 Best Case: In which we analyze the performance of an algorithm for the input, for which the
algorithm takes less time or space.
 Worst Case: In which we analyze the performance of an algorithm for the input, for which the
algorithm takes long time or space.
 Average Case: In which we analyze the performance of an algorithm for the input, for which the
algorithm takes time or space that lies between best and worst case
 Types of Data Structure Asymptotic Notation
• Big-O Notation (Ο) – Big O notation specifically describes worst case scenario.
• Omega Notation (Ω) – Omega(Ω) notation specifically describes best case scenario.
• Theta Notation (θ) – This notation represents the average complexity of an
algorithm.
Big-O Notation (Ο)
It represents the upper bound running time complexity of an algorithm. Let’s take few examples to
understand how we represent the time and space complexity using Big O notation.
 O(1) :- Big O notation O(1) represents the complexity of an algorithm that always execute in
same time or space regardless of the input data.
• Example
The following step will always execute in same time(or space) regardless
of the size of input data.
• Accessing array index(int num = arr[5])
 O(n) :- Big O notation O(N) represents the complexity of an algorithm, whose performance
will grow linearly (in direct proportion) to the size of the input data.
• Example
The execution time will depend on the size of array. When the size of
the array increases, the execution time will also increase in the same
proportion (linearly)
• Traversing an array
 O(n^2) :-Big O notation O(n^2) represents the complexity of an algorithm, whose
performance is directly proportional to the square of the size of the input data.
• O(n^2) example
• Traversing a 2D array

 Similarly there are other Big O notations such as:


 logarithmic growth O(log n), log-linear growth O(n log n), exponential growth O(2^n) and
factorial growth O(n!).
 A diagram to compare the performance of algorithms denoted by these notations, then it
would be it like this:

BY:- P.K. Sah Page 10


DATA STRUCTURE AND ALGORITHM

Omega Notation (Ω)


It represents the lower bound running time complexity of an algorithm. So if we represent a complexity
of an algorithm in Omega notation, it means that the algorithm cannot be completed in less time than
this, it would at-least take the time represented by Omega notation or it can take more (when not in
best case scenario).

BY:- P.K. Sah Page 11


DATA STRUCTURE AND ALGORITHM

Theta Notation (θ)


This notation describes both upper bound and lower bound of an algorithm so we can say that it defines
exact asymptotic behavior. In the real case scenario the algorithm not always run on best and worst
cases, the average running time lies between best and worst and can be represented by the theta
notation.

Time and space analysis of algorithms


 Sometimes, there are more than one way to solve a problem.
 We need to learn how to compare the performance of different algorithms and choose the best
one to solve a particular problem.
 While analysing an algorithm, we mostly consider two factors time complexity and space
complexity.
 Time complexity of an algorithm quantifies the amount of time taken by an algorithm to run as
a function of the length of the input.
 Space complexity of an algorithm quantifies the amount of space or memory taken by an
algorithm to run as a function of the length of the input.
 Time & space complexity depends on lots of things like hardware, operating system, processors,
etc.
 However, we do not consider any of these factors while analysing the algorithm. We will only
consider the execution time of an algorithm.

BY:- P.K. Sah Page 12


DATA STRUCTURE AND ALGORITHM

Calculating Time Complexity


Calculate Time Complexity of Sum of elements of List (One dimensional Array)
SumOfList(A,n)
{
Line 1 total = 0
Line 2 for i = 0 to n-1
Line 3 total = total + A[i]
Line 4 return total
}
Line Cost No. of times
1 1 1
2 2 n+1
3 2 n
4 1 1

Total sum of list = 1 + 2 (n+1) + 2n + 1


= 1 + 2n + 2 + 2n + 1
= 4n + 4
=4n ( we can neglect 4 )
Time complexity of given algorithm is n unit time

BY:- P.K. Sah Page 13


DATA STRUCTURE AND ALGORITHM

UNIT – 2
Stack
 A linear list which allows insertion and deletion of an element at one end only is called stack.
 The insertion operation is called as PUSH and deletion operation as POP.
 The most accessible elements in stack is known as top.
 The elements can only be removed in the opposite orders from that in which they were added
to the stack.
 Such a linear list is referred to as a LIFO (Last In First Out) list.

 A pointer TOP keeps track of the top element in the stack.


 Initially, when the stack is empty, TOP has a value of “-1”.
 Each time a new element is inserted in the stack, the pointer is incremented by “one” before,
the element is placed on the stack.
 The pointer is decremented by “one” each time a deletion is made from the stack.

Applications of Stack
 Recursion
 Keeping track of function calls
 Evaluation of expressions
 Reversing characters
 Servicing hardware interrupts
 Solving combinatorial problems using backtracking
 Expression Conversion (Infix to Postfix, Infix to Prefix)
 Game Playing (Chess)
 Microsoft Word (Undo / Redo)
 Compiler – Parsing syntax & expression
 Finding paths

BY:- P.K. Sah Page 14


DATA STRUCTURE AND ALGORITHM

PUSH
Procedure : PUSH (S, TOP, X)
 This procedure inserts an element X to the top of a stack.
 Stack is represented by a vector S containing N elements.
 A pointer TOP represents the top element in the stack.

Algorithm for inserting (PUSH) elements in stack


1. [Check for stack overflow]
If TOP ≥ N
Then write (‘STACK OVERFLOW’)
Return
2. [Increment TOP]
TOP ← TOP + 1
3. [Insert Element]
S[TOP] ← X
4. [Finished]
Return
Stack is empty, TOP = 0, N=3

Stack is empty, TOP = 0, N=3


PUSH(S, TOP, 11)
PUSH(S, TOP, 22)
PUSH(S, TOP, 33)
PUSH(S, TOP, 44)
PUSH(S, TOP, 55)
PUSH(S, TOP, 66) X
Overflow

BY:- P.K. Sah Page 15


DATA STRUCTURE AND ALGORITHM

POP
Function : POP (S, TOP)
 This function removes & returns the top element from a stack.
 Stack is represented by a vector S containing N elements.
 A pointer TOP represents the top element in the stack.
Algorithm
1. [Check for stack underflow]
If TOP = -1
Then write (‘STACK UNDERFLOW’)
Return (0)
2. [Decrement TOP]
TOP ← TOP - 1
3. [Return former top element of stack]
Return(S[TOP + 1])

POP(S, TOP)  Top = 4


POP(S, TOP)  Top = 3
POP(S, TOP)  Top = 2
POP(S, TOP)  Top = 1
POP(S, TOP)  Top = 0
POP(S, TOP)  Underflow

BY:- P.K. Sah Page 16


DATA STRUCTURE AND ALGORITHM

PEEP
Function : PEEP (S, TOP, P)
 This function returns the value of the (pth)element from the TOP of the stack. The element is
not deleted by this function.
 Stack is represented by a vector S containing N elements.
Algorithm
1. [Check for stack underflow]
If TOP == -1
Then write (‘STACK UNDERFLOW’)
Return (0)
2. [Check for stack overflow]
If P > TOP + 1
Then write (‘STACK OVERFLOW’)
Return (0)
3. [Return pth element from top
of the stack]
Return(S[TOP–P+1])

Operation

Overflow

PEEP (S, TOP, 1)

PEEP (S, TOP, 2)

PEEP (S, TOP, 3)

PEEP (S, TOP, 4)

PEEP (S, TOP, 5)

Underflow

BY:- P.K. Sah Page 17


DATA STRUCTURE AND ALGORITHM

CHANGE
CHANGE (S, TOP, X, P)
 This procedure changes the value of the Pth element from the top of the stack to X.
 Stack is represented by a vector S containing N elements.
Algorithm
1. [Check for stack underflow]
If TOP-P+1 ≤ 0
Then write (‘STACK UNDERFLOW’)
Return
2. [Check for stack overflow]
If P > TOP + 1
Then write (‘STACK OVERFLOW’)
Return (0)
3. [Change Pth element from top
of the stack]
S[TOP–P+1] ← X
4. [Finished]
Return

Example:-
CHANGE (S, TOP, 50, 2)
TOP = 3 -5
CHANGE (S, TOP, 9, 3)
8
CHANGE (S, TOP, 25, 8)
10
Underflow S

BY:- P.K. Sah Page 18


DATA STRUCTURE AND ALGORITHM

TRAVERSE
Traverse (S, Top, I)
 This procedure displays the elements of the stack with for loop using a variable I.
 Stack is represented by a vector S containing N elements.
Algorithm
1. [Check for stack underflow]
If TOP == -1
Then write (‘STACK UNDERFLOW’)
Return
2. Display the elements of stack
for (i=top;i>=0;--i)
display the elements of the stack.
3. [Finished]
Return
Example:-

Display(S,TOP,20) 20
TOP
30
Display(S,TOP,30)
40
Display(S,TOP,40)

Underflow

BY:- P.K. Sah Page 19


DATA STRUCTURE AND ALGORITHM

Array representation of stack


 An array is used to store an ordered list of elements. Using an array for representation of stack is
one of the easy techniques to manage the data. But there is a major difference between an
array and a stack.
 Size of an array is fixed.
 While, in a stack, there is no fixed size since the size of stack changed with the
number of elements inserted or deleted to and from it.
 Despite the difference, an array can be used to represent a stack by taking an array of maximum
size; big enough to manage a stack.

BY:- P.K. Sah Page 20

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy