DS 1
DS 1
DS 1
Stack:
Stack, LIFO(Last In First Out) system, is a linear data structure in which insertion(PUSH) and
deletion(POP) are restricted to one endpoint called TOP.
Queue:
A queue is a FIFO(First In First Out) system, in which insertion(ENQUEUE) can take place only
at an end called REAR and deletion(DEQUEUE) can take place only at another end called FRONT.
Linked lists:
A linked list is a collection of data elements whose order is not given by their physical location
in memory. It consists of nodes that contain data and link to the next node. The structure allows
easiness in insertion and deletion operations. The last nodes are linked to NULL and signify the
end of the list
Trees:
Tree is a non linear data structure. Some data contains a hierarchical relationship between
various elements. These data are represented in the form of a Rooted tree graph or simply Tree.
Here node A is called the root of the free.
Graphs:
Sometimes data contain relationships that are not hierarchical in nature. This type of relationship
can be expressed in the form of a graph data structure.
Algorithm complexity
Algorithmic complexity is a measure of how long an algorithm takes to complete a task
or solve a problem.
It's a numerical function that calculates the amount of time an algorithm takes to produce
a result as a function of the size of the input.
Algorithmic complexity is calculated asymptotically as n approaches infinity. This means
that an algorithm should compute the result within a finite and practical time bound even
for large values of n.
Algorithm complexity is concerned about how fast or slow a particular algorithm
performs.
It evaluates the order of count of operations executed by an algorithm as a function of
input data size.
Algorithmic complexity can be divided into two types: time complexity and space
complexity. The most common measure of complexity is time complexity.
Time Complexity:
Time taken by the algorithm to solve the problem. It is measured by calculating the iteration
of loops, number of comparisons etc.
Time complexity is a function describing the amount of time an algorithm takes in
terms of the amount of input to the algorithm.
“Time” can mean the number of memory accesses performed, the number of
comparisons between integers, the number of times some inner loop is executed, or
some other natural unit related to the amount of real time the algorithm will take.
Space Complexity:
Space taken by the algorithm to solve the problem. It includes space used by necessary input
variables and any extra space (excluding the space taken by inputs) that is used by the
algorithm.
Space complexity is a function describing the amount of memory(space)an
algorithm takes in terms of the amount of input to the algorithm.
Space complexity is sometimes ignored because the space used is minimal and/ or
obvious, but sometimes it becomes an issue as time.
Example: linear search algorithm, the best-case time complexity is O(1). The average case time
complexity is O(n). The worst-case complexity is O(n). The best case occurs when the search
item is at the beginning of the list. The worst case occurs when the search item is at the end of
the list or not at all.
space-time trade-off
In computer science, a space-time or time-memory trade-off is a way of solving a problem or
calculation in less time by using more storage space (or memory), or by solving a problem in
very little space by spending a long time.
Asymptotic Notation
Asymptotic notation is a mathematical notation that describes the efficiency of an
algorithm as the input size grows.
It is used to compare the performance of different algorithms on different inputs.
it can also be used to predict the performance of an algorithm on a given input size.
There are three main types of asymptotic notation:
Big O notation
It describes the worst-case running time of an algorithm. It tells you how much time the
algorithm will take in the worst possible scenario. For example, if an algorithm has a
time complexity of O(n^2), it means that the algorithm will take at most n^2 steps to
complete.
Omega Notation:
It describes the best-case running time of an algorithm. It tells you how little time the
algorithm will take in the best possible scenario. For example, if an algorithm has a
time complexity of Omega(n), it means that the algorithm will take at least n steps to
complete.
Theta Notation:
It describes the average-case running time of an algorithm. It tells you how much time
the algorithm will take on average. For example, if an algorithm has a time complexity
of Theta(n^2), it means that the algorithm will take on average n^2 steps to complete.
When choosing which asymptotic notation to use, it is important to consider the type of data
structure that is being used. For example, if the data structure is a linked list, then the asymptotic
notation for the worst-case running time would be O(n), because the worst-case scenario is that
the algorithm has to traverse the entire linked list.
Asymptotic notation can be used to compare the performance of different algorithms on different
inputs. For example, if we have two algorithms that both sort a list of numbers:
Algorithm A:
This algorithm has a time complexity of O(n). This means that the algorithm will take at
most n steps to complete, regardless of the size of the input.
Algorithm B:
This algorithm has a time complexity of O(n^2). This means that the algorithm will take at
most n^2 steps to complete, regardless of the size of the input.
In this case, Algorithm A is faster than Algorithm B because it has a lower time complexity.
Introduction to strings
In computer terminology, a sequence of characters is called a string.
Strings are represented by enclosing inside quotation marks.
A string with length 0 is called an empty string.
Let A and B be two strings. A string consisting of characters in A followed by characters
in B is called concatenation of A and B. It is often represented as A//B.
Suppose string A = ‘Hello’ and B = ‘World’ then A//B will be ‘HelloWorld’.
A string Q is called a substring of a string S, if there exist strings P and R such that
S=P//Q//R.
Suppose S=”Evening” then ‘Ev’, ‘ni’, ‘ng’… are the substrings of S.
String storage
Generally, three types of structures are used to store strings.
Fixed length structure: In a fixed-length structure, each line of print is viewed as a
record. All records have the same length.
Variable-length storage: Variable length strings can be stored in memory cells either by
using a marker like $$ or \0 to represent the end of the string or by listing the length of
the string as an additional element in a pointer array.
Linked Storage: We use a linked list to store strings. It helps to easily delete, change,
and insert words, sentences even paragraphs in the text. A linked list is an ordered
sequence of nodes, where each node contains a link which has the address of the next
node in the list. Here each node is assigned one character or a fixed number of characters
String Operations
Substrings
A substring is the basic unit of access in a string. Unlike other array elements, substrings have
their own meaning. To access a substring we need the following information,
Name of the string.
Position of the first character of the substring in the given string.
length of the substring.
Eg. if string A=’WELCOME’, then after SUBSTRING(SUB, A,3,2), the value of SUB will
be ‘LC’.
Indexing
Indexing operation is used to find the position of the first appearance of a string pattern in a
given string. It is also called pattern matching. INDEX operation returns a 0 if there is no such
pattern in that string.
Eg. if A=’MORNING’ then INDEX(A,’NI’) will return, 4, the first appearance of pattern ‘NI’ in
the given string ‘MORNING’.
Concatenation
if A and B are two strings, then the concatenation of two strings, A//B is the string that consisting
of the characters of A followed by the characters of B.
Eg. If A=’GOOD’ and B=’MORNING’ then A//B will be ‘GOODMORNING’
Length
The number of characters in a string is called the length of that string.
Eg. If A=”DECEMBER” then LENGTH(A) will return 8.
From the figure Index starts with 0. Array length is 10 which means it can store 10 elements.
Each element can be accessed via its index. For example, we can fetch an element at index 6 as
27.
Basic Operations
Traverse − print all the array elements one by one.
Insertion − Adds an element at the given index.
Deletion − Deletes an element at the given index.
Search − Searches an element using the given index or by the value.
Traverse Operation
This operation is to traverse through the elements of an array.
Example: Following program traverses and prints the elements of an array:
#include int main()
{
int age[5]={20,21,19,18,25};
int i;
for(i=0;i<a[i];i++)
printf("%d ", age[i]);
return 0;
}
Output: 20 21 19 18 25
Algorithm:
ARR is the array and UB is the limit of the array.
TRAVERSE(ARR,UB)
1. Set I =0.
2. Repeat steps 3 and 4 while I<UB.
3. Print ARR[i].
4. Set I=I+1
5. Stop.
Insertion operation
Given an array arr of size n, this article tells how to insert an element x in this array arr at a
specific position pos.
Approach:
1. First get the element to be inserted, say x
2. Then get the position at which this element is to be inserted, say pos
3. Then shift the array elements from this position to one position forward, and do this for all the
other elements next to pos.
4. Insert the element x now at the position pos, as this is now empty.
Algorithm
INSERT (arr, n, pos, x)
1. Set i=n
2. Repeat steps 3 and 4 while i>=pos
3. Set arr[i+1]=arr[i]
4. Set i=i-1
5. Set arr[pos]=num
6. Set n=n+1
7. Exit.
Deletion operation
A user will enter the position at which the array element deletion is required. Deleting an
element does not affect the size of the array.
It also checks whether deletion is possible or not, for example, if an array contains five
elements and user wants to delete the element at the sixth position, it isn't possible.
we need to shift array elements which are after the element to be deleted, it is very
inefficient if the size of the array is large or we need to remove elements from an array
repeatedly
Algorithm
Arr is the array with n elements. And pos is a positive integer such that pos<n. This
algorithm deletes the element at a position pos from arr.
DELETE(arr, n, pos, num)
1. set num=arr[pos]
2. set I=pos
3. repeat steps 4 and 5 while i<=n-1
4. arr[i]=arr[i+1]
5. Set i=i+1
6. Set n=n-1
7. Exit.
Sparse Matrix
A matrix is a two-dimensional data object made of m rows and n columns, therefore having
total m x n values. If most of the elements of the matrix have 0 value, then it is called a sparse
matrix.
Why to use Sparse Matrix instead of simple matrix ?
Storage: There are lesser non-zero elements than zeros and thus lesser memory can be used
to store only those elements.
Computing time: Computing time can be saved by logically designing a data structure
traversing only non-zero elements.
Example:
00304
00570
00000
02600
Representing a sparse matrix by a 2D array leads to wastage of lots of memory as zeroes in the
matrix are of no use in most of the cases. So, instead of storing zeroes with non-zero elements,
we only store non-zero elements. This means storing non-zero elements with triples- (Row,
Column, value).
Sparse Matrix Representations can be done in many ways following are two common
representations:
1. Array representation
2. Linked list representation
Method 1: Using Arrays:
2D array is used to represent a sparse matrix in which there are three rows named as
Row: Index of row, where non-zero element is located
Column: Index of column, where non-zero element is located
Value: Value of the non zero element located at index – (row,column)
Algorithm
1. Start
2. Set I=0
3. Repeat steps 4 to 7 while I<N
4. Val[I]= Read value of the non-zero element
5. Row[I]= Read row number of the non-zero element
6. Col[I]= Read column number of the non-zero element
7. Set I=I+1
8. Set I=j=0 // display the sparse matrix
9. Repeat steps 10 to 17 while I<p
10. Repeat steps 11 to 16 while j<q
11. Set k=flag=0
12. Repeat steps 13 to 14 while k<N
13. If val[k]!= 0 and Row[k]=I and Col[k]=j then
14. Display val[k]
15. Flag=1
[end of if]
16. Set k=k+1
[end of step 12 loop]
17. If flag=0 then display 0
18. Set j=j+1
[end of step 10 loop]
19.Set I=I+1
[end of step 9 loop]
20.stop
#include<stdio.h>
int main()
{
// Assume 4x5 sparse matrix
int sparseMatrix[4][5] =
{
{0 , 0 , 3 , 0 , 4 },
{0 , 0 , 5 , 7 , 0 },
{0 , 0 , 0 , 0 , 0 },
{0 , 2 , 6 , 0 , 0 }
};
int size = 0;
for (int i = 0; i < 4; i++)
for (int j = 0; j < 5; j++)
if (sparseMatrix[i][j] != 0)
size++;
printf("\n");
}
return 0;
}
Output
001133
242312
345726
Time Complexity: O(NM), where N is the number of rows in the sparse matrix, and M is the
number of columns in the sparse matrix.
Parallel arrays
A parallel array is a structure that contains multiple arrays. Each of these arrays are of the same
size and the array elements are related to each other. All the elements in a parallel array represent
a common entity. An example of parallel arrays is as follows –
employee_name = { Harry, Sally, Mark, Frank, Judy }
employee_salary = {10000, 5000, 20000, 12000, 5000}
In the above example, the name and salary of 5 different employees is stored in 2 arrays.