Professional Documents
Culture Documents
com
UNIT-1 and Unit-2
Data Structures
(AI&DS, AI&ML,IIOT)
The operations are adding, updating, deleting, sorting, searching the data and so on.
A data structure is not only used for organizing the data. It is also used for processing, retrieving, and storing data. There are different basic
and advanced types of data structures that are used in almost every program or software system that has been developed. So we must have
good knowledge about data structures.
Depending on your requirement and project, it is important to choose the right data structure for your project.
We can organize these data as a record like Employee record, which will have both employee's name and age in it. Now we can collect and
store employee's records in a file or database as a data structure.
For example, if you want to store data sequentially in the memory, then you can go for the Array data structure
ashish0751@gmail.com
Advantages of Data structures
Efficiency:
If the choice of a data structure for implementing a particular ADT is proper, it makes the program very efficient
Reusability:
The data structure provides reusability means that multiple client programs can use the data structure.
Abstraction:
The data structure specified by an ADT also provides the level of abstraction. The client cannot see the internal
working of the data structure, so it does not have to worry about the implementation part. The client can only
ashish0751@gmail.com
DATA STRUCTURES
ashish0751@gmail.com
Primitive data structures
Primitive Data Structures: A primitive data structure can store the value of only one data type. For example, a char data
structure (a primitive data structure) can store only characters.
Key features of a primitive data structure:
•The size of a primitive data structure is known as it can store can only one data type.
•The primitive data structure will always store some data. It cannot contain the NULL value.
•Examples of the primitive data type are integer, character, boolean, float, double, long, etc.
ashish0751@gmail.com
Non-Primitive data structures
Linear data structure
Data structure in which data elements are arranged sequentially or linearly, where each element is attached to
its previous and next adjacent elements, is called a linear data structure. Examples of linear data structures
are array, stack, queue, linked list, etc.
• Static data structure: Static data structure has a fixed memory size. It is easier to access the elements in a
static data structure. An example of this data structure is an array.
• Dynamic data structure: In dynamic data structure, the size is not fixed. It can be randomly updated
during the runtime which may be considered efficient concerning the memory (space) complexity of the
code. Examples of this data structure are queue, stack, etc.
Non-linear data structure:
Data structures where data elements are not placed sequentially or linearly are called non-linear data
structures. In a non-linear data structure, we can’t traverse all the elements in a single run only.
ashish0751@gmail.com
Examples of non-linear data structures are trees and graphs.
Linear Vs Non-linear Data Structures
The data items are arranged in The data items are arranged in non-
sequential order, one after the other. sequential order (hierarchical manner).
All the items are present on the single The data items are present at different
layer. layers.
ashish0751@gmail.com
Algorithmic complexity
ashish0751@gmail.com
Algorithm
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.
An algorithm is any well-defined computational procedure that takes some value, or set of values, as input
and produces some value, or set of values, as output”
Algorithms are generally created independent of underlying languages, i.e. an algorithm can be implemented in
more than one programming language.
Complexity Analysis
Based on Based on
Measuring an Input’s Size Measuring Running Time
Space Complexity Time Complexity
Algorithm & Pseudocode
What is Algorithm What is Pseudocode
• Pseudocode is an informal way of writing a program. It is
• An algorithm is a step by step procedure to not exactly a computer program. It represents the algorithm
solve a problem. of the program in natural language and mathematical
• A procedure is a finite sequence of notations. Usually, there is no particular code syntax to write
instructions, where each is carried out in a a pseudocode. Therefore, there is no strict syntax as a usual
finite amount of time. programming language. It uses simple English language.
• Every problem can be solved with the help of
an algorithm. • Pseudocode to add 2 numbers is as follows:
• For example, when the user wants to login to a
Facebook account, first he has to go to Sum of Two Numbers()
Facebook.com. Then he has to give the correct Begin
username and password. Then he has to click Set sum=0;
the login button. If the username and password Read: num1, num2;
are correct, the user can enter his account. Set sum = num1+num2;
• Likewise, every problem has a sequence of Print sum;
steps to solve it. This is also an algorithm End
because it provides a correct sequence of steps
to solve the problem.
ashish0751@gmail.com
Characteristics and Properties of an Algorithm
Clear and Unambiguous: The algorithm should be clear and unambiguous. Each of its steps should be clear in all aspects and
must lead to only one meaning.
Well-Defined Inputs: If an algorithm says to take inputs, it should be well-defined inputs. It may or may not take input.
Well-Defined Outputs: The algorithm must clearly define what output will be yielded and it should be well-defined as well. It
should take at least 1 output.
Finite-ness: The algorithm must be finite, i.e. it should terminate after a finite time.
Feasible: The algorithm must be simple, generic, and practical, such that it can be executed with the available resources. It must
not contain some future technology or anything.
Language Independent: The Algorithm designed must be language-independent, i.e. it must be just plain instructions that can be
implemented in any language, and yet the output will be the same, as expected.
Properties of Algorithm:
•It should terminate after a finite time.
•It should produce at least one output.
•It should take zero or more input.
•It should be deterministic means giving the same output for the same input case.
•Every step in the algorithm must be effective i.e. every step should do some work.
ashish0751@gmail.com
Example
Write an algorithm and draw a flowchart that will read the two sides of a rectangle and calculate its area.
Algorithm
START
• Print A
ALxW
Pseudocode
• Step 1: Input W,L Print
A
• Step 2: A L x W
• Step 3: Print A STOP
ashish0751@gmail.com
Algorithm Complexity (Performance Analysis)
The performance of an algorithm is Time Complexity--
judged by two important criteria. • The time complexity of an algorithm quantifies the amount of time
(i) Time complexity
(ii) space complexity. taken by an algorithm to run as a function of the length of the input.
• Note that the time to run is a function of the length of the input and
Generally, there is always more than one not the actual execution time of the machine on which the algorithm
way to solve a problem in computer is running on.
science with different algorithms. • In order to calculate time complexity on an algorithm, it is assumed
Therefore, it is highly required to use a that a constant time c is taken to execute one operation, and then
method to compare the solutions in order
the total operations for an input length on N are calculated.
to judge which one is more optimal. The
method must be:
Independent of the machine and Space Complexity −
its configuration, on which the Space is measured by counting the maximum memory space required
algorithm is running on. by the algorithm.
Shows a direct correlation with The complexity of an algorithm f(n) gives the running time and/or the
the number of inputs. storage space required by the algorithm in terms of n as the size of
Can distinguish two algorithms input data.
clearly without ambiguity.
ashish0751@gmail.com
Space Complexity
Space complexity of an algorithm represents the amount of memory space
Following is a simple example that
required by the algorithm in its life cycle. tries to explain the concept −
The space required by an algorithm is equal to the sum of the following two • Algorithm: SUM(A, B)
• Step 1 - START
components − • Step 2 - C ← A + B + 10
A fixed part that is a space required to store certain data and variables, • Step 3 - Stop
that are independent of the size of the problem. For example, simple
variables and constants used, program size, etc.
A variable part is a space required by variables, whose size depends on Here we have three variables A, B,
the size of the problem. For example, dynamic memory allocation, and C and one constant.
recursion stack space, etc. Hence S(P) = 1 + 3.
Now, space depends on data types of
Space complexity S(P) of any algorithm P is
given variables and constant types and
it will be multiplied accordingly.
S(P) = C + SP(I)
where C is the fixed part and S(I) is the variable part of the algorithm, which
depends on instance characteristic I.
ashish0751@gmail.com
Examples(Space Complexity)
1. Algorithm abc (a, b, c) • For every instance
2. { • 3 computer words required to store variables: a, b,
and c.
3. return a+b+b*c+(a+b-c)/(a+b)+4.0; • Therefore Sp()= 3.
4. }
• S(P) = 3.
• Time complexity of an algorithm represents the amount of time required by the algorithm to run to completion.
• Time Complexity: The 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. Note that the time to run is a function of the length of the input and not the actual
execution time of the machine on which the algorithm is running on.
• In order to calculate time complexity on an algorithm, it is assumed that a constant time c is taken to execute one
operation, and then the total operations for an input length on N are calculated.
• Time requirements can be defined as a numerical function f(n), where f(n) can be measured as the number of steps,
provided each step consumes constant time.
• Time required f(P) to run a program P also consists of two components:
A fixed part:
compile time which is independent of the problem instance c.
A variable part:
run time which depends on the problem instance tp(instance)
T(P) = c + tp(instance)
ashish0751@gmail.com
Example 1 (Time Complexity)
1 Algorithm Sum(a[],n,m) 0 - 0
2 { 0 - 0
5 s = s+a[i][j]; 1 nm nm
6 return s; 1 1 1
7 } 0 - 0
2nm+2n+2
Example 2 (Time Complexity)
Statements S/E Freq. Total
1 Algorithm Sum(a[],n) 0 - 0
2 { 0 - 0
3 S = 0.0; 1 1 1
4 for i=1 to n do 1 n+1 n+1
5 s = s+a[i]; 1 n n
6 return s; 1 1 1
7 } 0 - 0
F(n)=2n+3
**IMP**
This f(n) can be represented in the form of best case, worst case, average case, see next slides
1. Big O (Oh) Notation = Worst Case Analysis
2. Big Ω (Omega) Notation = Best Case Analysis
3. Big Ɵ (Theta) Notation = Average Case Analysis
• Big O of a function gives us ‘rate of growth’ of the step count function f(n), in terms of a
simple function g(n), which is easy to compare.
f(n) = O(g(n))
iff there exist positive constants c and n0 such that
f(n) <= c*g(n) for all n, n>=n0.
ashish0751@gmail.com
Big - Theta Notation (Θ)
• Big - Theta notation is used to define the average bound of an algorithm in terms of Time Complexity.
• Theta notation always indicates the average time required by an algorithm for all input values.
• Big - Theta notation describes the average case of an algorithm time complexity.
• Consider function f(n) as time complexity of an algorithm and g(n) is the most significant term.
• If C1 g(n) <= f(n) <= C2 g(n) for all n >= n0, C1 > 0, C2 > 0 and n0 >= 1. Then we can represent f(n) as Θ(g(n)).
• f(n) = Θ(g(n))
f(n) = Θ(g(n))
Example
Consider the following f(n) and g(n)...
f(n) = 3n + 2
g(n) = n
If we want to represent f(n) as Θ(g(n)) then it must satisfy
C1 g(n) <= f(n) <= C2 g(n) for all values of C1 > 0, C2 > 0 and n0>= 1
C1 g(n) <= f(n) <= C2 g(n)
⇒C1 n <= 3n + 2 <= C2 n
Above condition is always TRUE for all values of C1 = 1, C2 = 4 and n >= 2.
By using Big - Theta notation we can represent the time complexity as
follows...
ashish0751@gmail.com
3n + 2 = Θ(n)
Array, String and recursion
ashish0751@gmail.com
Introduction of Array
• An array is a collection of similar data elements.
• These data elements have the same data type.
• The elements of the array are stored in consecutive memory locations and
are referenced by an index (also known as the subscript).
• The subscript is an ordinal number which is used to identify an element of the
array.
Declaration
int arr[5];
ashish0751@gmail.com
Declaration of Arrays
• We have already seen that every variable must be declared before it is used.
• The same concept holds true for array variables. An array must be declared
before being used.
• Declaring an array means specifying the following:
• Data type—the kind of values it can store, for example, int, char, float, double.
• Name—to identify the array.
• Size—the maximum number of values that the array can hold.
• Arrays are declared using the following syntax:
type name[size]; int arr[5];
• The type can be either int, float, double, char, or any other valid data type.
• The number within brackets indicates the size of the array, i.e., the maximum
number of elements that can be stored in the array.
ashish0751@gmail.com
Declaration of Arrays
ashish0751@gmail.com 31
Calculating the Address of Array Elements
• The address of other data elements can simply be
calculated using the base address.
• The formula to perform this calculation is, Address of
data element,
A[k] = BA(A) + w(k – lower_bound)
Example: Given the base address of an array A[1300 ………… 1900] as 1020 and the size of each element is
2 bytes in the memory, find the address of A[1700].
Solution:
Given:
Base address B = 1020
Lower Limit/Lower Bound of subscript LB = 1300
Storage size of one element store in any array W = 2 Byte
Subset of element whose address to be found I = 1700
Formula used:
Address of A[I] = B + W * (I – LB)
Solution:
Address of A[1700] = 1020 + 2 * (1700 – 1300)
= 1020 + 2 * (400)
= 1020 + 800
ashish0751@gmail.com
Address of A[1700] = 1820 32
Initializing Arrays during Declaration
• The elements of an array can be initialized at the time of declaration, just as any other variable.
• When an array is initialized, we need to provide a value for every element in the array.
• Arrays are initialized by writing,
ashish0751@gmail.com
Inputting Values from the Keyboard
ashish0751@gmail.com 34
Run time initialization( from user)
#include <stdio.h>
//////////////////////////////////
void main()
{
int i;
int A[6];
for(i = 0; i < 6; i++)
{ printf("enter elementt number %d ",i+1);
scanf("%d" , &A[i]); }
printf("***********\n");
for(i = 0; i < 6; i++)
printf("elementt number %d is: %d\n",i+1,A[i]);
}//end main
ashish0751@gmail.com
Accessing the Elements of an Array
• Now to process all the elements of the array, we use a loop as shown in Fig. 3.4.
• Figure 3.5 shows the result of the code shown in Fig. 3.4.
• The code accesses every individual element of the array and sets its value to –1.
• In the for loop, first the value of marks[0] is set to –1, then the value of the index (i) is
incremented and the next value, that is, marks[1] is set to –1.
• The procedure continues until all the 10 elements of the array are set to –1.
ashish0751@gmail.com
36
Assigning Values to Individual Elements
• Any value that evaluates to the data type as that of the array can be assigned to the individual array element.
• A simple assignment statement can be written as marks[3] = 100; Here, 100 is assigned to the fourth
element of the array which is specified as marks[3].
• Note that we cannot assign one array to another array, even if the two arrays have the same type and size.
• To copy an array, you must copy the value of every element of the first array into the elements of the second
array. Figure 3.10 illustrates the code to copy an array.
char string1[20];
scanf( "%s", string1 ); //Reads characters until whitespace encountered
// Can write beyond end of array, be careful
ashish0751@gmail.com 38
c string
• In C programming, a string is a sequence of
characters terminated with a null character \
0. When the compiler encounters a sequence
of characters enclosed in the double
quotation marks, it appends a null character \
0 at the end by default. For example:
char c[] = "c string";
ashish0751@gmail.com
TWO-DIMENSIONAL ARRAYS
• One-dimensional arrays are organized linearly in only one
direction.
For example, if we want to store the marks obtained by
• But at times, we need to store data in the form of grids or
tables. three students in three different subjects, we can declare a
• Here, the concept of single-dimension arrays is extended to two dimensional array as:
incorporate two-dimensional data structures.
int marks[3][3];
• A two-dimensional array is specified using two subscripts
where the first subscript denotes the row and the second A two-dimensional array is declared as:
denotes the column.
• The C compiler treats a two-dimensional array as an array data_type array_name[row_size][column_size];
of one-dimensional arrays.
• Figure 3.26 shows a two-dimensional array which can be
viewed as an array of arrays.
• The declaration statement tells the compiler the name of the
array, the data type of each element in the array, and the size
of each dimension.
ashish0751@gmail.com
TWO-DIMENSIONAL ARRAYS
ashish0751@gmail.com
Address calculation in 2D Array in C
Graphical
int A[3][4]; Representa
Address of A[I][J] = B + W * ((I – LR) * N + (J – LC))
tion of
I = Row Subset of an element whose address to be found,
Array
0th 1st 2nd 3rd address in
J = Column Subset of an element whose address to be found,
B = Base address,
memory
0th 100 102 104 106 W = Storage size of one element store in an array(in byte),
LR = Lower Limit of row/start row index of the matrix(If not
1st 108 110 112 114 given assume it as zero),
LC = Lower Limit of column/start column index of the
matrix(If not given assume it as zero),
2nd 116 118 120 122 N = Number of column given in the matrix.
Calculation:
A[2][2]= 100+2{4(2-0)+(2-0)}
A[2][2]= Address ? =100+2{8+2}
If base address is 100 the address of =120
A[2][2] is 120
ashish0751@gmail.com
Example: Given an array, arr[1………10][1………15] with base value 100 and the
size of each element is 1 Byte in memory. Find the address of arr[8][6] with the help
of row-major order.
Solution:
Given:
Base address B = 100
Storage size of one element store in any array W = 1 Bytes
Row Subset of an element whose address to be found I = 8
Column Subset of an element whose address to be found J = 6
Lower Limit of row/start row index of matrix LR = 1
Lower Limit of column/start column index of matrix = 1
Number of column given in the matrix N = Upper Bound – Lower Bound + 1
= 15 – 1 + 1
= 15
Formula:
Address of A[I][J] = B + W * ((I – LR) * N + (J – LC))
Solution: https://www.geeksforgeeks.org/calculation-of-address-of-elem
Address of A[8][6] = 100 + 1 * ((8 – 1) * 15 + (6 – 1)) ent-of-1-d-2-d-and-3-d-using-row-major-and-column-major-ord
= 100 + 1 * ((7) * 15 + (5)) er/
= 100 + 1 * (110)
Address of A[I][J] = 210
ashish0751@gmail.com
2D Array: Initializing Two-dimensional arrays
1.#include <stdio.h>
2.void main ()
3.{
4. int arr[3][3],i,j;
5. for (i=0;i<3;i++)
6. {
7. for (j=0;j<3;j++)
8. {
9. printf("Enter a[%d][%d]: ",i,j);
10. scanf("%d",&arr[i][j]);
11. }
12. }
13. printf("\n printing the elements ....\n");
14. for(i=0;i<3;i++)
15. {
16. printf("\n");
17. for (j=0;j<3;j++)
18. {
19. printf("%d\t",arr[i][j]);
20. }
21. }
22.}
3D Array
Declaring a 3D array:
•Specify data type, array name, block size, row size and
column size.
•Each subscript can be written within its own separate
pair of brackets.
•Syntax: data_type array_name[block_size]
[row_size][column_size];
Function calling using Recursion
• In C, When a function calls a copy of itself then the
process is known as Recursion. To put it short, when a
function calls itself then this technique is known as
Recursion. And the function is known as a recursive
function.
• You have to be more careful when you are using recursion
in your program. You just cannot use recursion in all your
problems because it will make your program more
complex and difficult.
• Recursion can be used in case of similar subtasks like
sorting, searching, and traversal problems. While using
recursion, you will have to define an exit condition on that
function, if not then it will go into an infinite loop.
ashish0751@gmail.com
Example: Sum of Natural Numbers Using Recursion
This exit condition inside a recursive function is known as base condition.
int sum(int n)
{
if(n==1) //Base Condition
return 1;
return n+ sum(n-1); // Function calling itself
}
Factorial using Recursion
1.#include<stdio.h>
2.
3.long factorial(int n)
4.{
5. if (n == 0)
6. return 1;
7. else
8. return(n * factorial(n-1));
9.}
10.
11.void main() Enter a number: 6 Factorial of 5 is: 720
12.{
13. int number;
14. long fact;
15. printf("Enter a number: ");
16. scanf("%d", &number);
17.
18. fact = factorial(number);
19. printf("Factorial of %d is %ld\n", number, fact);
20. return 0;
21.}
ashish0751@gmail.com
Stack
Stack
Stack is an abstract data type with a bounded(predefined) capacity. It is a simple data structure that allows adding and
removing elements in a particular order.
A stack is an abstract data type that holds an ordered, linear sequence of items. A stack is a last in, first out (LIFO)
structure.
A real-life example is a stack of plates: you can only take a plate from the top of the stack, and you can only add a plate
to the top of the stack. If you want to reach a plate that is not on the top of the stack, you need to remove all of the
plates that are above that one. In the same way, in a stack data structure, you can only access the element on the top of
the stack. The element that was added last will be the one to be removed first. Therefore, to implement a stack, you
need to maintain a pointer to the top of the stack (the last element to be added).
Every time an element is added, it goes on the top of the stack and the only element that can be removed is the element
that is at the top of the stack.
ashish0751@gmail.com
Basic features of Stack
2. Stack is a LIFO (Last in First out) structure or we can say FILO(First in Last out).
3. push() function is used to insert new elements into the Stack and pop() function is used to remove an element
from the stack. Both insertion and removal are allowed at only one end of Stack called Top.
4. Stack is said to be in Overflow state when it is completely full and is said to be in Underflow state if it is
completely empty.
ashish0751@gmail.com
The main stack operations are
ashish0751@gmail.com
Stack and array
Stacks differ from the arrays in a way that in arrays random access is possible; this means that we can access any
element just by using its index in the array. Whereas, in a stack only limited access is possible and only the top
element is directly available.
Consider an array, arr = [1, 2, 3, 4]. To access 2, we can simply write arr[1], where 1 is the index of the array
element, 2. But if the same is implemented using a stack, we can only access the topmost element that is 4.
ashish0751@gmail.com
Applications of Stack
1. Balancing of symbols: Stack is used for balancing a symbol. For example, we have the following program:
int main()
a) {
b) cout<<"Hello";
c) cout<<"javaTpoint";
d) }
each program has an opening and closing braces; when the opening braces come, we push the braces in a stack, and when the closing braces appear, we pop the
opening braces from the stack. Therefore, the net value comes out to be zero. If any symbol is left in the stack, it means that some syntax occurs in a program.
2. String reversal: Stack is also used for reversing a string. For example, we want to reverse a "javaTpoint" string, so we can achieve this with the help of a stack.
First, we push all the characters of the string in a stack until we reach the null character. After pushing all the characters, we start taking out the character one by one
until we reach the bottom of the stack.
3. UNDO/REDO: It can also be used for performing UNDO/REDO operations. For example, we have an editor in which we write 'a', then 'b', and then 'c'; therefore,
the text written in an editor is abc. So, there are three states, a, ab, and abc, which are stored in a stack. There would be two stacks in which one stack shows UNDO
state, and the other shows REDO state.
4. Recursion: The recursion means that the function is calling itself again. To maintain the previous states, the compiler creates a system stack in which all the
previous records of the function are maintained.
5. . DFS (Depth First Search): This search is implemented on a Graph, and Graph uses the stack data structure.
6. Backtracking: Suppose we have to create a path to solve a maze problem. If we are moving in a particular path, and we realize that we come on the wrong way. In
order to come at the beginning of the path to create a new path, we have to use the stack data structure.
7. Expression conversion: Stack can also be used for expression conversion. This is one of the most important applications of stack. The list of the expression
conversion is given below:. Infix to prefixInfix to postfixPrefix to infixPrefix to postfixPostfix to infix
8. Memory management: The stack manages the memory. The memory is assigned in the contiguous memory blocks. The memory is known as stack memory as all
the variables are assigned in a function call stack memory. The memory size assigned to the program is known to the compiler. When the function is created, all its
ashish0751@gmail.com
variables are assigned in the stack memory. When the function completed its execution, all the variables assigned in the stack are released.
Implementation of Stack Data Structure
Stack can be easily implemented using an Array or a Linked List. Arrays are quick, but are limited in
size and Linked List requires overhead to allocate, link, unlink, and deallocate, but is not limited in size.
Here we will implement Stack using array.
Analysis of Stack Operations
Below mentioned are the time complexities for various operations that can be performed on the Stack
data structure.
•Push Operation : O(1)
•Pop Operation : O(1)
•Top Operation : O(1)
•Search Operation : O(n)
The time complexities for push() and pop() functions are O(1) because we always have to insert or
remove the data from the top of the stack, which is a one step process.
ashish0751@gmail.com
Algorithm for PUSH and POP Operation
PUSH
begin procedure push: stack, data
if stack is full
return null
endif
top ← top + 1
stack[top] ← data
end procedure
POP
The steps involved in the POP operation is given begin procedure pop: stack
below:
if stack is empty
Before deleting the element from the stack, we check
whether the stack is empty.
return null
endif
If we try to delete the element from the empty stack, then
the underflow condition occurs. data ← stack[top]
If the stack is not empty, we first access the element top ← top - 1
which is pointed by the top return data
Once the pop operation is performed, the top is
decremented by 1, i.e., top=top-1.
end procedure
ashish0751@gmail.com
ashish0751@gmail.com
Programming code (Stack) 23 int main(){ 46 void push(){
24 printf("STATIC ARRAY (Total Capacity: %d)\
1 // Implementing Static Stack using an n", N); 47 // Checking overflow state
Array in C 2 25 int choice;26
3 #include <stdio.h> 27 while(1){ 48 if(top == N-1)
4 #include <stdlib.h> 28 printf("\nChoose any of the following
5 #include <stdbool.h> 6
49 printf("Overflow State: can't add
options:\n");
7 // N will be the capacity of the Static 29 printf(" 0: Exit 1: Push 2: Pop more elements into the stack\n");
Stack 3: Peek\n");
8 #define N 1000 9
50 else{
30 printf(" 4: Check if the stack is empty 5:
10 // Initializing the top of the stack to be -1 Check if the stack is full\n\n"); 51 int x;
11 int top = -1;12 31 scanf("%d", &choice);
13 // Initializing the stack using an array 52 printf("Enter element to be pushed
14 int stack[N];15 33 switch(choice){ into the stack: ");
16 // Function prototypes 34 case 0: exit(0);
17 void push(); // Push element to the 35 case 1: push(); break; 53 scanf("%d", &x);
top of the stack 36 case 2: pop(); break;
18 int pop(); // Remove and return the 37 case 3: peek(); break; 54 top+=1;
top most element of the stack 38 case 4: isEmpty(); break;
19 int peek(); // Return the top most 39 case 5: isFull(); break;
55 stack[top] = x;
element of the stack 40 default: printf("Please choose a correct 56 }
20 bool isEmpty(); // Check if the stack is option!");
in Underflow state or not 41 } 57 }
21 bool isFull(); // Check if the stack is in 42 }
Overflow state or not 43 return 0; 58
44 }
ashish0751@gmail.com
78 bool isEmpty(){
79 if(top == -1){
59 int pop(){ 80 printf("Stack is empty: Underflow
State\n");
60 // Checking underflow state
81 ; return true;
61 if(top == -1) 82 }
62 printf("Underflow State: Stack 83 printf("Stack is not empty\n"); 87 bool isFull(){
already empty, can't remove any 84 return false; 88 if(top == N-1){
element\n"); 85 } 89 printf("Stack is full: Overflow
63 else{ State\n");
64 int x = stack[top]; 90 return true;
65 printf("Popping %d out of the 91 }
72 int peek(){
stack\n", x); 92 printf("Stack is not full\n");
66 top-=1; 73 int x = stack[top]; 93 return false;
67 return x; 74 printf("%d is the top most 94 }
68 } element of the stack\n", x);
69 return -1;
75 return x;
70 }
ashish0751@gmail.com
76 }
Arithmetic expression
ashish0751@gmail.com
Arithmetic Expressions
• Arithmetic Expressions involve constants and operations.
• An expression can be written with constants, variables, and symbols that can act as an operator or parenthesis.
All this expression needs to follow a specific set of rules. According to this rule, the parsing of the expression
is done based on grammar.
• An arithmetic expression is expressed in the form of Notation. Now, there are three ways to write an
expression in Arithmetics:
• Infix Notation
• Prefix (Polish) Notation
• Postfix (Reverse-Polish) Notation
• Binary operations have different levels of precedence and association .
• First : Exponentiation (^)
• Second: Multiplication (*) and Division (/)
• Third : Addition (+) and Subtraction (-)
ashish0751@gmail.com
Example
• Evaluate the following Arithmetic Expression:
5 ^ 2 + 3 * 5 – 6 * 2 / 3 + 24 / 3 + 3
• First:
25 + 3 * 5 – 6 * 2 / 3 + 24 / 3 + 3
• Second:
25 + 15 – 4 + 8 + 3
• Third:
47
Q. 1 + 2*3 + 30/5
=1+ (2*3) + (30/5)
=1+6+6 = 13
ashish0751@gmail.com
Arithmetic Expressions
1. Infix Notation
Infix is the day to day notation that we use of format A + B type. The general form can be classified as (a op
b) where a and b are operands(variables) and op is Operator. <operands> <operators> <operands>
•Example 1 : A + B
•Example 2 : A * B + C / D
2. Postfix Notation
Postfix is notation that compiler uses/converts to while reading left to right and is of format AB+ type. The general form can
be classified as (ab op) where a and b are operands(variables) and op is Operator.
•Example 1 : AB+
•Example 2 : AB*CD/+
3. Prefix Notation
Prefix is notation that compiler uses/converts to while reading
right to left (some compilers can also read prefix left to right)
and is of format +AB type. The general form can be classified
as (op ab) where a and b are operands(variables) and op is
Operator. <operators> <operands> <operands>
•Example 1 : +AB
•Example 2 : +*AB/CD
ashish0751@gmail.com
Postfix and Prefix Examples
ashish0751@gmail.com
Examples ( Infix to Prefix Notation)
• (A + B) * C = * + ABC
• A + (B * C) = + A *BC
• (A + B) / (C - D) = / +AB –CD
ashish0751@gmail.com
Infix to Postfix
Method-1
Method-2
A * B + C ((A * B) + C) A * (B + C) (A * (B + C) )
((A B * C + (A (B C + *
ashish0751@gmail.com
A B * C + A B C + *
Infix to Postfix
Infix Postfix
A+B AB+
A+B*C ABC*+
(A + B) * C AB+C*
A+B*C+D ABC*+D+
(A + B) * (C + D) AB+CD+*
A*B+C*D AB*CD*+
• • A+B* C+D
A+B*C
• ((A + (B * C)) + D )
• (A + (B * C)) • ((A + (B C*) )+ D)
• (A + (B C *) ) • ((A B C *+) + D)
• A B C * + • ABC*+D+
Infix to postfix conversion using stack
Reference: Debasis Samanta, Computer Science & Engineering, Indian Institute of Technology Kharagpur
Infix to Postfix using stack
Current Operator Postfix string
Expression: symbol Stack
1 A A
A * (B + C * D) + E 2 * * A
3 ( *( A
becomes
4 B *( AB
ashish0751@gmail.com
↑ ( + ( - ( / ↑ ABC*DE
Continue…
A+(B*C–(D/E↑F)*G)*H
Symbol Scanned Stack Expression P
F ( + ( - ( / ↑ ABC*DEF
) ( + ( - A B C * D E F ↑/
* ( + ( - * A B C * D E F ↑/
G ( + ( - * A B C * D E F ↑/ G
) ( + A B C * D E F ↑/ G * -
* ( + * A B C * D E F ↑/ G * -
H ( + * A B C * D E F ↑/ G * - H
) A B C * D E F ↑/ G * - H * +
ashish0751@gmail.com
Infix to prefix using stack
Sr. no. Expression Stack Prefix
Given Infix - ((a/b)+c)-(d+(e*f)) 0 ( ((
1 ( (((
•Step 1: Reverse the infix string. Note that while 2 f ((( f
reversing the string you must interchange left and right 3 * (((* f
4 e (((* fe
parentheses.
5 ) (( fe*
•Step 2: Obtain the postfix expression of the infix
6 + ((+ fe*
expression Step 1.
7 d ((+ fe*d
•Step 3: Reverse the postfix expression to get the
8 ) ( fe*d+
prefix expression
9 – (- fe*d+
10 ( (-( fe*d+
This is how you convert manually for theory 11 c (-( fe*d+c
question in the exam 12 + (-(+ fe*d+c
1.String after reversal – ))f*e(+d(-)c+)b/a(( 13 ( (-(+( fe*d+c
2.String after interchanging right and left 14 b (-(+( fe*d+cb
parenthesis – ((f*e)+d)-(c+(b/a)) 15 / (-(+(/ fe*d+cb
3.Apply postfix conversion algorithm using stack 16 a (-(+(/ fe*d+cba
4.Reverse Postfix Expression (Given After the table 17 ) (-(+ fe*d+cba/
below) 18 ) (- fe*d+cba/+
19 ) fe*d+cba/+-
Example:
2 3 4 + 5 6 - - *
® 2 3 4 + 5 6 - - *
® 2 7 5 6 - - *
® 2 7 5 6 - - *
® 2 7 -1 - *
® 2 7 -1 - * ® 2 8 * ® 2 8 * ® 16
76
Evaluation of Postfix (RPN) Expression using stack
2. Scan P from left to right and Repeat Step 3 and 4 for each element of P until the
sentinel “)” is encountered.
• The real-world example of a queue is the ticket queue outside a cinema hall, where the person who enters first in the
queue gets the ticket first, and the last person enters in the queue gets the ticket at last. Similar approach is followed
in the queue in data structure.
Queue
A queue is defined as a linear data structure that is open at both ends and
the operations are performed in First In First Out (FIFO) order.
We define a queue to be a list in which all additions to the list are made at
one end, and all deletions from the list are made at the other end.
The element which is first pushed into the order, the operation is first
performed on that.
ashish0751@gmail.com
Representation of Queue
ashish0751@gmail.com
FIFO Principle of Queue
•A Queue is like a line waiting to purchase tickets, where the first person in line is the first person served.
(i.e. First come first serve).
•Position of the entry in a queue ready to be served, that is, the first entry that will be removed from the
queue, is called the front of the queue(sometimes, head of the queue), similarly, the position of the last
entry in the queue, that is, the one most recently added, is called the rear (or the tail) of the queue. See
the below figure.
ashish0751@gmail.com
Characteristics and Representation of Queue
Characteristics of Queue:
•Queue can handle multiple data.
•We can access both ends.
•They are fast and flexible.
Queue Representation:
Like stacks, Queues can also be represented in an array: In this
representation, the Queue is implemented using the array.
Variables used in this case are
•Queue: the name of the array storing queue elements.
•Front: the index where the first element is stored in the array
representing the queue.
•Rear: the index where the last element is stored in an array
representing the queue.
ashish0751@gmail.com
Applications of Queue
Queue, as the name suggests is used whenever we need to manage any group of objects in an
order in which the first one coming in, also gets out first while the others wait for their turn,
like in the following scenarios:
Printers: Queue data structure is used in printers to
maintain the order of pages while printing.
Interrupt handling in computes: The interrupts are
operated in the same order as they arrive, i.e., interrupt
which comes first, will be dealt with first.
Process scheduling in Operating systems: Queues
are used to implement round-robin scheduling
algorithms in computer systems.
Switches and Routers: Both switch and router
interfaces maintain ingress (inbound) and egress
(outbound) queues to store packets.
Customer service systems: It develops call center
phone systems using the concepts of queues.
ashish0751@gmail.com
Basic Operations of Queue
•Peek: Get the value of the front of the queue without removing
it ashish0751@gmail.com
Queue operations
Enqueue Dequeue
The Enqueue operation is used to add The Dequeue operation is used to
an element to the front of the queue. remove an element from the rear of the
Steps of the algorithm: queue.
1.Check if the Queue is full. Steps of the algorithm:
2.Set the front as 0 for the first 1.Check if the Queue is empty.
element. 2.Return the value at the front index.
3.Increase rear by 1. 3.Increase front by 1.
4.Add the new element at 4.Set front and rear as -1 for the last
the rear index.
ashish0751@gmail.com
element.
Queue operations
Peek isFull
The Peek operation is The isFull operation is used to check if the queue is
used to return the full or not.
Steps of the algorithm:
front most element of
1.Check if the number of elements in the queue
the queue. (size) is equal to the capacity, if yes, return True.
Steps of the 2.Return False.
algorithm:
1.Check if the Queue isEmpty
is empty. The isEmpty operation is used to check if the queue
2.Return the value at is empty or not.
the front index. Steps of the algorithm:
1.Check if the number of elements in the queue (size)
is equal to 0, if yes, return True.
2.Return False.
ashish0751@gmail.com
Enqueue() Operation
•Step 1: Check if the queue is full.
•Step 2: If the queue is full, Overflow
error.
•Step 3: If the queue is not full,
increment the rear pointer to point to
the next available empty space.
•Step 4: Add the data element to the
queue location where the rear is
pointing.
•Step 5: Here, you have successfully
added 7, 2, and -9.
ashish0751@gmail.com
ashish0751@gmail.com
Major drawback of using a linear Queue
• The major drawback of using a linear Queue is that insertion is done only
from the rear end. If the first three elements are deleted from the Queue, we
cannot insert more elements even though the space is available in a Linear
Queue.
• In this case, the linear Queue shows the overflow condition as the rear is
pointing to the last element of the Queue.
#include<stdio.h>
#include<stdlib.h> switch(choice)
#define maxsize 3 {
void insert(); case 1:
void delete(); insert();
void display(); break;
int front = -1, rear = -1; case 2:
int queue[maxsize]; delete();
void main () break;
{ case 3:
int choice=1; display();
while(choice == 1) break;
{ case 4:
printf("\ exit(0);
n*************************Main break;
Menu*****************************\n"); default:
printf("\ printf("\nEnter valid choice??\n");
n============================================ }
=====================\n"); printf("do you want to repeat
printf("\n1.insert an element\ operations");
n2.Delete an element\n3.Display the queue\ scanf("%d",&choice);
n4.Exit\n"); }
printf("\nEnter your choice ?"); }
scanf("%d",&choice);
void insert() void delete()
{ {
int item; int item;
printf("\nEnter the element\n"); if (front == -1 || front > rear)
{
scanf("\n%d",&item);
printf("\nUNDERFLOW\n");
if(rear == maxsize-1) return;
{ }
printf("\nOVERFLOW\n"); else
{
return;
item = queue[front];
} if(front == rear)
if(front == -1 && rear == -1) {
{ front = 0; front = -1;
rear = -1 ;
rear = 0;
}
} else
else {
{ front = front + 1;
rear = rear+1; }
} printf("\nvalue deleted ");
queue[rear] = item; }
printf("\nValue inserted ");
}
}
void display()
{
int i;
if(rear == -1)
{
printf("\nEmpty queue\n");
}
else
{ printf("\nprinting values .....\n");
for(i=front;i<=rear;i++)
{
printf("\n%d\n",queue[i]);
}
}
}
Priority Queues
• Priority Queue is an abstract data type that is similar to a queue, and every element has some priority value
associated with it.
• The priority of the elements in a priority queue determines the order in which elements are served (i.e., the
order in which they are removed).
• If in any case the elements have same priority, they are served as per their ordering in the queue.
• A priority queue is a special type of queue in which each element is associated with a priority value. And,
elements are served on the basis of their priority. That is, higher priority elements are served first.
A tuple is A dictionary is
A list is a collection A set is an unordered collection
of ordered data. an ordered collection of an unordered collection. of data that stores data
data. in key-value pairs
Dictionaries are
Lists are declared with Tuples are enclosed Sets are represented in enclosed in curly
square braces. within parenthesis. curly brackets. brackets in the form of
key-value pairs.
list1=[1,4,"Gitam",6,"college"]
list2=[] # creates an empty list
list3=list((1,2,3))
print(list1) Output
print(list2) [1, 4, 'Gitam', 6, 'college’]
print(list3) []
[1, 2, 3]
tuple1=(1,2,"college",9)
tuple2=() # creates an empty tuple
tuple3=tuple((1,3,5,9,"hello"))
print(tuple1) Output
(1, 2, 'college', 9)
print(tuple2) ()
print(tuple3) (1, 3, 5, 9, 'hello')
set1={1,2,3,4,5,"hello","tup"} Output
set2={(1,8,"python",7)} {1, 2, 3, 4, 5}
print(set1) {1, 3, 6, 7}
print(set2)
dict1={"key1":1,"key2":"value2",3:"value3"}
print(dict1.keys()) # all the keys are
printed
Output
print(dict1.values()) # all the values are
dict_keys(['key1', 'key2', 3])
printed
dict_values([1, 'value2', 'value3'])
dict1["key1"]="replace_one" # value
{'key1': 'replace_one', 'key2':
assigned to key1 is replaced
'value2', 3: 'value3'} value2
print(dict1)
print(dict1["key2"])
List
A list is an ordered data structure with elements separated by a comma and enclosed within square brackets. For
example, list1 and list2 shown below contains a single type of data.
list1 = ["apple", "banana", "cherry"]
list2 = [1, 5, 7, 9, 3]
list3 = [True, False, False]
list1 = ["abc", 34, True, 40, "male"]
thislist = ["apple", "banana", "cherry"]
print(thislist[1]) output---banana
ashish0751@gmail.com
Dr. Ashish Kumar
ashish0751@gmail.com
VIPS-TC
Linear search program in C
#include <stdio.h>
1. Linear Search int main()
{
int array[9];
int search, c, n;
for (c = 0; c < n; c++) for(c=0;c<10;c++)
{ {
if (array[c] == search) /* If printf("Enter element”);
required element is found */
Scanf(“%d”,&array[c]);
{
printf("%d is present at location
%d.\n", search, c+1);
break;
}
}
if (c == n) scanf("%d", &n);
printf("%d isn't present in the array.\ for (c = 0; c < n; c++)
n", search);
scanf("%d", &array[c]);
return 0;
} printf("Enter a number to search\n");
scanf("%d", &search);
2. Binary Search
Binary Search Algorithm: The basic steps to perform Binary Search are:
•Begin with an interval covering the whole array. (Array is Sorted)
•If the value of the search key is less than the item in the middle of the interval, narrow the interval to the lower half.
•Otherwise, narrow it to the upper half.
•Repeatedly check until the value is found or the interval is empty.
Binary Search
low = 0;
#include <stdio.h> high = n - 1;
mid = (low+high)/2;
int main()
{ while (low <= high) {
if(array[mid] < key)
int i, low, high, mid, n, key, low = mid + 1;
array[100];
printf("Enter number of elementsn"); else if (array[mid] == key) {
scanf("%d",&n); printf("%d found at location %d.n", key, mid+1);
printf("Enter %d integersn", n); break;
for(i = 0; i < n; i++) }
scanf("%d",&array[i]); else
printf("Enter value to findn"); high = mid - 1;
scanf("%d", &key); mid = (low + high)/2;
}
if(low > high)
printf("Not found! %d isn't present in the list.n", key);
return 0;
}
3. Insertion Sort
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.
Algorithm
To sort an array of size n in ascending order:
1: Iterate from arr[1] to arr[n] over the array.
2: Compare the current element (key) to its predecessor.
3: If the key element is smaller than its predecessor, compare it to
the elements before. Move the greater elements one position up to
make space for the swapped element.
Insertion Sort
Bubble Sort
Bubble sort
Bubble Sort
Selection Sort
Selection Sort
Selection sort program in C for (c = 0; c < (n - 1); c++)
#include <stdio.h>
int main() {
{
position = c;
int array[100], n, c, d, position, t;
printf("Enter number of elements\n"); for (d = c + 1; d < n; d++)
scanf("%d", &n); {
printf("Enter %d integers\n", n); if (array[position] > array[d])
for (c = 0; c < n; c++) position = d;
scanf("%d", &array[c]); }
if (position != c)
{
t = array[c];
array[c] = array[position];
array[position] = t;
}
}
printf("Sorted list in ascending order:\
n");
for (c = 0; c < n; c++)
printf("%d\n", array[c]);
return 0;
}
Quick Sort
https://www.geeksforgeeks.org/quick-sort/
ashish0751@gmail.com
int main()
{
Quick Sort #include <stdio.h>
void swap(int *p, int *q)
int n, i; {
void quickSort(int a[], int low, int high) int temp = *p;
printf("What is the size of the
{ *p = *q;
array?\n");
if (low < high) *q = temp;
scanf("%d",&n);
{ }
int a[n]; int k = partition(a, low, high); int partition(int a[], int low, int high)
printf("Enter elements of quickSort(a, low, k - 1); {
the array one by one\n"); quickSort(a, k + 1, high); int pivot = a[high];
for(i = 0; i < n; i++) } int i = (low - 1);
{ }
for (int j = low; j <= high- 1; j++)
scanf("\n%d",&a[i]); {
void print(int a[], int size)
} if (a[j] <= pivot)
{
{
quickSort(a, 0, n - 1);
int i;
i++;
printf("Sorted array: "); for (i = 0; i < size; i++)
swap(&a[i], &a[j]);
print(a, n); printf("%d ", a[i]);
}
return 0; printf("\n");
}
} }
swap(&a[i + 1], &a[high]);
return (i + 1);
}
ashish0751@gmail.com
Quick Sort Algorithm
1.The divide-and-conquer strategy is used in quicksort. Below the recursion step is described: Choose a pivot value. We take the value of the middle element as
pivot value, but it can be any value, which is in range of sorted values, even if it doesn't present in the array.
2.Partition. Rearrange elements in such a way, that all elements which are lesser than the pivot go to the left part of the array and all elements greater than the
pivot, go to the right part of the array. Values equal to the pivot can stay in any part of the array. Notice, that array may be divided in non-equal parts.
3.Sort both parts. Apply quicksort algorithm recursively to the left and the right parts.
The algorithm rearranges the unsorted list by picking a pivot value and rearranging the list so that values that are lower than the pivot value are positioned before
it in the list, and values that are higher than the pivot value are positioned after it in the list. To rearrange the values, the low mark and high mark are used.
4. As the list is checked, the low mark is moved up over values that are lower than the pivot value. Once the low mark reaches a value that is higher than the
pivot value, it stops in that place and the algorithm proceeds to examine the high mark.
5. The high mark is moved down over values that are higher than the pivot value. If the high mark reaches a value that is lower than the pivot value, the
algorithm swaps the values of the low mark and the high mark.
6. The process continues until the high mark overlaps with the low mark, which indicates the new position for the pivot value. Sometimes the pivot value is
already in that
ashish0751@gmail.com position.
Radix sort
The algorithm is named radix sort as it specifies the radix r to be used which
changes how the sort is performed. The radix, or base, of the number system is
the number of digits that represent a single position in the number; a radix of 2
is binary (0-1), 10 is decimal (0-9), 16 is hexadecimal (0-F) and so on. Radix
Sort is a linear sorting algorithm. The algorithm based on idea that sorting the
elements by first grouping the individual digits of the same place value. Radix
sort depends on the digits or letters, so it is less flexible than other sorts.
Radix Sort Algorithm
1.Take the least significant digit of each element.
2.Sort the list of elements based on that digit, but keep the order of elements
with the same digit.
3.Repeat the sort with each more significant digit.
The speed of the Radix sort depends on the inner basic operations.
If the operations are not efficient enough, Radix sort can be slower than other
algorithms such as Quick Sort and Merge Sort.
ashish0751@gmail.com
https://www.growingwiththeweb.com/sorting/radix-sort-lsd/
Radix sort
Merge sort is one of the efficient & fastest sorting algorithms with the following time complexity:
Worst Case Time Complexity: O(n*logn)
Best Case Time Complexity: O(n*log n)
Average Time Complexity: O(n*log n)
Given: Arr(5, 8, 3, 9, 1, 2)
•We split the array into two halves Arr1 = (5, 8, 3) and Arr2 = (9, 1, 2).
•Again, we divide them into two halves: Arr3 = (5, 8) and Arr4 = (3) and
Arr5 = (9, 1) and Arr6 = (2)
•Again, we divide them into two halves: Arr7 = (5), Arr8 = (8), Arr9 =
(9), Arr10 = (1) and Arr6 = (2)
•We will now compare the elements in these sub arrays in order to
merge them.
step 4: Stop
#include <stdio.h> void sort(int low, int high) { void merging(int low, int mid, int high)
#define max 10 int mid; {
int a[11] = { 10, 14, 19, 26, 27, int l1, l2, i;
31, 33, 35, 42, 44, 0 };
if(low < high) { for(l1 = low, l2 = mid + 1, i = low; l1
int b[10]; <= mid && l2 <= high; i++) {
int main() mid = (low + high) / 2;
if(a[l1] <= a[l2])
{ sort(low, mid);
b[i] = a[l1++];
int i; sort(mid+1, high); else
printf("List before sorting\ merging(low, mid, high);
n"); b[i] = a[l2++];
} else { }
for(i = 0; i <= max; i++)
printf("%d ", a[i]);
return; while(l1 <= mid)
} b[i++] = a[l1++];
• Min-heap or max-heap represents the ordering of array in which the root element
represents the minimum or maximum element of the array.
• Max heap is a complete binary tree. A complete binary tree is a binary tree in
which all levels are completely filled and all the nodes in the last level are as left
as possible.
• also meets this criteria: the parent’s key is larger than both children’s keys. The
Max heap should largest value is at the root. Heap is related to priority Heap sort
queue and heapsort.
Heap sort basically recursively performs two main operations -
Algorithm 1: Algorithm 2:
•Build a heap H, using the elements of array. Insertion Heapify +
•Repeatedly delete the root element of the heap formed in 1st phase. +Deletion Deletion
ashish0751@gmail.com
Algorithm1: Max Heap Construction Algorithm
We shall use the same example to demonstrate how a Max Heap is created. The procedure to create Min Heap is
similar but we go for min values instead of max values. We are going to derive an algorithm for max heap by inserting
one element at a time. At any point of time, heap must maintain its property.
ashish0751@gmail.com
Max Heap Deletion Algorithm
ashish0751@gmail.com
After Inserting all
the node,
Deletion Starts
with root node
ashish0751@gmail.com
ashish0751@gmail.com
Algotithm-2 (part 1:Heapify )
Algotithm-2 (part
ashish0751@gmail.com 2:Delete procedure is same, refer previous slided)
Max-heap implementation – Insertion algorithm
ashish0751@gmail.com
Summary of Sorting Algorithms
Algorithm Time Notes
in-place
selection-sort O(n2) slow (good for small inputs)
in-place
insertion-sort O(n2) slow (good for small inputs)
in-place
heap-sort O(n log n) fast (good for large inputs)
ashish0751@gmail.com
Deletion
ashish0751@gmail.com
Linked List
ashish0751@gmail.com
Pointer
Fun with Pointers
134
Introduction to structure
A structure is a user defined data type : collection of
variables under a single name.
Example 1:
struct passenger
{ Example 2:
char name [20]; struct student{
int age; char name[20];
int train_no; int roll;
char board_place[20]; float marks;
char destn[20]; char remarks;
};
ashish0751@gmail.com };
e 1. struct motor p, q, r;
y p Declares and sets aside
e t
• Defines a new type f th storage for three variables ,
e o
• E.g., a m t h e p, q, and r, each of type
f
struct motor { N r s o struct motor
float volts; be t 2. struct motor M[25];
float amps; em uc Declares a 25-element array
M tr
int phases; s of struct motor;
float rpm;
allocates 25 units of storage,
}; //struct motor each one big enough to hold
the data of one motor
3. struct motor *m;
Declares a pointer to an
object of type struct
ashish0751@gmail.com
motor
Examples of structure ( Declaration &Initiation)
ashish0751@gmail.com
Self Referential Structures
Self Referential structures are those structures that have one or more Types of Self Referential Structures
pointers which point to the same type of structure, as their member. Self Referential Structure with Single Link
• There is a pointer (called header) points the first element (also called node)
head
A B C NULL
145
Defining a Node of a Linked List
Each structure of the list is called a node, and consists of two fields:
• Item (or) data
• Address of the next item in the list (or) pointer to the next node in the list
How to define a node of a linked list?
typedef struct node Note:
{ node Such structures which contain a
int data; /* Data */ member field pointing to the same
struct node *next; /* pointer*/ Data structure type are called self-
}node; next referential structures.
A B C 1.typedef struct node
2.{
3. struct node *prev;
4. int data;
5. struct node *next;
6.} node;
Defining a Node of a Double Linked List
Each node of doubly linked list (DLL) consists of three fields:
• Item (or) Data
• Pointer of the next node in DLL
• Pointer of the previous node in DLL
node
Data
prev next
150
Double versus Single Linked List
151
Circular Linked List
• The pointer from the last element in the list points back to the first element.
• A circular linked list is basically a linear linked list that may be single- or double-linked.
• The only difference is that there is no any NULL value terminating the list.
• In fact in the list every node points to the next node and last node points to the first node, thus forming a circle. Since it forms a circle with
no end to stop it is called as circular linked list.
• In circular linked list there can be no starting or ending node, whole node can be traversed from any node.
• In order to traverse the circular linked list, only once we need to traverse entire list until the starting node is not traversed again.
• A circular linked list can be implemented using both singly linked list and doubly linked list.
head
A B C
152
Creating a single linked list
If we need n number of nodes in the linked list:
• Allocate n newNodes, one by one.
• Read in the data for the newNodes.
• Modify the links of the newNodes so that the chain is formed.
It creates n number of nodes . For e.g. if the data entered is 200, 50, 30 then
the list look like
head
100 200 50 30 NULL
153
Operations on Linked Lists
154
Operations on single linked list
• Traversing a list
• Printing, finding minimum, etc.
155
Traversing a Linked List
156
Single Linked List: Traversing
157
Single linked list: Traversing
158
Insertion in a Linked List
159
Single Linked List: Insertion
Insertion steps:
SN Operation Description
• Create a new node 1 Insertion
beginning
at It involves inserting any element at the front of the list. We just
need to a few link adjustments to make the new node as the head
of the list.
• Start from the header node 2 Insertion at It involves insertion at the last of the linked list. The new node
end of the list can be inserted as the only node in the list or it can be inserted as
• Manage links to the last one. Different logics are implemented in each scenario.
• Insert at front 3 Insertion after It involves insertion after the specified node of the linked list. We
specified node need to skip the desired number of nodes in order to reach the
node after which the new node will be inserted. .
• Insert at end
• Insert at any position
160
Insertion at Front
161
Insertion at Front
Step 2: Link the newly created node with the head node, i.e. the newNode will now
point to head node.
Step 3: Make the new node as the head node, i.e. now head node will point to newNode.
162
Insertion at front
/*Create a new node and insert at the beginning of the linked list.*/
if(newNode == NULL)
{
printf("Unable to allocate memory.");
}
else
{
newNode->data = data; //Links the data part
newNode->next = head; //Links the address part
163
Single Linked List: Insertion at End
Steps to insert node at the end of Singly linked list
Step 1: Create a new node and make sure that the address part of the new node points to
NULL. i.e. newNode->next=NULL
Step 2: Traverse to the last node of the linked list and connect the last node of the list with
the new node, i.e. last node will now point to new node. (lastNode->next =
newNode).
164
Insertion at End
/* Create a new node and insert at the end of the linked list. */
void insertNodeAtEnd(int data)
{
struct node *newNode, *temp;
newNode = (struct node*)malloc(sizeof(struct node));
if(newNode == NULL)
{
printf("Unable to allocate memory.");
}
else
{
newNode->data = data; //Links the data part
newNode->next = NULL;
temp = head;
while(temp->next != NULL) //Traverse to the last node
temp = temp->next;
temp->next = newNode; //Links the address part
printf("DATA INSERTED SUCCESSFULLY\n");
}
}
165
Single Linked List: Insertion at any Position
Steps to insert node at any position of Singly Linked List
Step 1: Create a new node.
Step 2: Traverse to the n-1th position of the linked list and connect the new node with the
n+1th node. (newNode->next = temp->next) where temp is the n-1th node.
166
Single Linked List: Insertion at any position
Step 3: Now at last connect the n-1th node with the new node i.e. the n-1th node will now
point to new node. (temp->next = newNode) where temp is the n-1th node.
Insertion at any Position
if(newNode == NULL)
{
printf("Unable to allocate memory.");
}
else
{
newNode->data = data; //Links the data part
newNode->next = NULL;
temp = head;
168
Insertion at any Position
for(i=2; i<=position-1; i++) /* Traverse to the n-1 position */
{
temp = temp->next;
if(temp == NULL)
break;
}
if(temp != NULL)
{
/* Links the address part of new node */
newNode->next = temp->next;
/* Links the address part of n-1 node */
temp->next = newNode;
printf("DATA INSERTED SUCCESSFULLY\n");
}
else
{
printf("UNABLE TO INSERT DATA AT THE GIVEN POSITION\n");
}
}
}
169
Double Linked List: Insertion at any Position
Steps to insert a new node at nth position in a Doubly linked list.
Step 1: Traverse to N-1 node in the list, where N is the position to insert. Say temp now
points to N-1th node.
Step 2: Create a newNode that is to be inserted and assign some data to its data field.
170
Doubly Linked List: Insertion at any Position
Step 3: Connect the next address field of newNode with the node pointed by next address
field of temp node.
Step 4: Connect the previous address field of newNode with the temp node.
171
Doubly Linked List: Insertion at any Position
Step 5: Check if temp.next is not NULL then, connect the previous address field of node
pointed by temp.next to newNode.
172
Doubly Linked List: Insertion at any Position
Step 7: Final doubly linked list looks like
173
Doubly Linked List: Insertion at any Position
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n, data;
head = NULL;
last = NULL;
last = head;
for(i=2; i<=n; i++){ /* Creates and links rest of the n-1 nodes */
newNode = (struct node *)malloc(sizeof(struct node));
printf("Enter data of %d node: ", i);
scanf("%d", &data);
newNode->data = data;
newNode->prev = last; //Links new node with the previous node
newNode->next = NULL;
newNode->data = data;
newNode->next = temp->next; //Connects new node with n+1th node
newNode->prev = temp; //Connects new node with n-1th node
if(temp->next != NULL)
{
temp->next->prev = newNode; /* Connects n+1th node with new node */
}
temp->next = newNode; /* Connects n-1th node with new node */
printf("NODE INSERTED SUCCESSFULLY AT %d POSITION\n", position);
}
else{
printf("Error, Invalid position\n");
}
}
}
176
Doubly Linked List: Insertion at any Position
void displayList()
{
struct node * temp;
int n = 1;
if(head == NULL)
{
printf("List is empty.\n");
}
else
{
temp = head;
printf("DATA IN THE LIST:\n");
while(temp != NULL)
{
printf("DATA of %d node = %d\n", n, temp->data);
n++;
178
Deletion from a Linked List
179
Single Linked List: Deletion
Deletion steps
• Start from the header node
• Manage links to
• Delete at front
• Delete at end
• Delete at any position
• freeingup the node as free space.
180
Free Memory after Deletion
• Do not forget to free() memory location dynamically allocated
for a node after deletion of that node.
181
Single Linked List: Deletion at Front
Steps to delete first node of Singly Linked List
Step 1: Copy the address of first node i.e. head node to some temp variable say toDelete.
Step 2: Move the head to the second node of the linked list (head = head->next).
Single linked list: Deletion at front
Step 3: Disconnect the connection of first node to second node.
183
Deletion at Front
if(head == NULL)
{
printf("List is already empty.");
}
else
{
toDelete = head;
head = head->next;
Step 2: If the last node is the head node then make the head node as NULL else disconnect the second last node with the last
node i.e. secondLastNode->next = NULL
Step 2: Reconnect n-1th node with the n+1th node i.e. prevNode->next = toDelete->next
(Where prevNode is n-1th node and toDelete node is the nth node and toDelete->next is the n+1th
node).
187
Single Linked List: Deletion at any Position
Step 3: Free the memory occupied by the nth node i.e. toDelete node.
188
Deletion at any Position
/* Delete the node at any given position of the linked list */
void deleteMiddleNode(int position)
{
int i;
struct node *toDelete, *prevNode;
if(head == NULL)
{
printf("List is already empty.");
}
else
{
toDelete = head;
prevNode = head;
for(i=2; i<=position; i++)
{
prevNode = toDelete;
toDelete = toDelete->next;
if(toDelete == NULL)
break;
}
189
Deletion at any Position
if(toDelete != NULL)
{
if(toDelete == head)
head = head->next;
prevNode->next = toDelete->next;
toDelete->next = NULL;
190
Circular linked list
Basic structure of singly circular linked list:
191
Circular linked list:
Advantages of a Circular linked list
• Entire list can be traversed from any node.
• Circular lists are the required data structure when we want a list to be accessed
in a circle or loop.
• Despite of being singly circular linked list we can easily traverse to its previous
node, which is not possible in singly linked list.
192
Operations on circular linked list
• Creation of list
• Traversal of list
• Insertion of node
• At the beginning of list
• At any position in the list
• Deletion of node
• Deletion of first node
• Deletion of node from middle of the list
• Deletion of last node
• Counting total number of nodes
• Reversing of list
193
Creation and Traversal of a Circular List
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node * next;
}*head;
int main()
{
int n, data;
head = NULL;
return 0;
}
194
Circular Linked List: Creation of List
void createList(int n)
{
int i, data;
struct node *prevNode, *newNode;
if(n >= 1){ /* Creates and links the head node */
head = (struct node *)malloc(sizeof(struct node));
head->data = data;
head->next = NULL;
prevNode = head;
for(i=2; i<=n; i++){ /* Creates and links rest of the n-1 nodes */
newNode = (struct node *)malloc(sizeof(struct node));
newNode->data = data;
newNode->next = NULL;
prevNode->next = newNode; //Links the previous node with newly created node
prevNode = newNode; //Moves the previous node ahead
}
prevNode->next = head; //Links the last node with first node
printf("\nCIRCULAR LINKED LIST CREATED SUCCESSFULLY\n");
}
}
195
Circular Linked List: Traversal of List
void displayList()
{
struct node *current;
int n = 1;
if(head == NULL)
{
printf("List is empty.\n");
}
else
{
current = head;
printf("DATA IN THE LIST:\n");
do {
printf("Data %d = %d\n", n, current->data);
current = current->next;
n++;
}while(current != head);
}
}
196
Unit- 3 and Unit-4
AI&ML, AI&DS, IIOT
What is a Tree data structure?
• A Tree is a non-linear data structure and a hierarchy consisting of a collection of nodes such that each node of the tree
stores a value and a list of references to other nodes (the “children”).
• This data structure is a specialized method to organize and store data in the computer to be used more effectively. It
consists of a central node, structural nodes, and sub-nodes, which are connected via edges. We can also say that tree data
structure has roots, branches, and leaves connected with one another.
Basic Terminologies In Tree Data Structure:
•Parent Node: The node which is a predecessor of a node is called the parent node of that node. {B} is the parent node of {D, E}.
•Child Node: The node which is the immediate successor of a node is called the child node of that node. Examples: {D, E} are the child nodes
of {B}.
•Root Node: The topmost node of a tree or the node which does not have any parent node is called the root node. {A} is the root node of the tree.
A non-empty tree must contain exactly one root node and exactly one path from the root to all other nodes of the tree.
•Leaf Node or External Node: The nodes which do not have any child nodes are called leaf nodes. {G, K, L, M, N, O, P} are the leaf nodes of
the tree.
•Ancestor of a Node: Any predecessor nodes on the path of the root to that node are called Ancestors of that node. {A,B} are the ancestor nodes
of the node {E}
• Depth of a Node
The depth of a node is the number of edges from the root to
the node.
Degree of a Node
• The degree of a node is the total number of branches of that Height and depth of each node in a tree
node.
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.
Properties of Binary Tree
•At each level of i, the maximum number of nodes is 2i.
•The height of the tree is defined as the longest path from the root node to the leaf node. The tree which
is shown above has a height equal to 3. Therefore, the maximum number of nodes at height 3 is equal to
(1+2+4+8) = 15. In general, the maximum number of nodes possible at height h is (2 0 + 21 + 22+….2h) =
2h+1 -1.
•The minimum number of nodes possible at height h is equal to h+1.
•If the number of nodes is minimum, then the height of the tree would be maximum. Conversely, if the
number of nodes is maximum, then the height of the tree would be minimum.
If there are 'n' number of nodes in the binary tree.
The minimum height can be computed as:
As we know that,
n = 2h+1 -1
n+1 = 2h+1
Taking log on both the sides,
log2(n+1) = log2(2h+1)
log2(n+1) = h+1
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.
1. Full Binary Tree-
•A binary tree in which every node has either 0 or 2 children is
called as a Full binary tree.
•Full binary tree is also called as Strictly binary 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.
smaller than the parent node, and the value of right node must
A B
Option A is BST
Creating a binary search tree Step 6 - Insert 55.
Suppose the data elements are - 45, 15, 79, 90, 10, 55, 12
Step 1 - Insert 45. Step 4 - Insert 90.
90 is greater than 45 and 79, so it will
be inserted as the right subtree of 79.
Step 3 - Insert 79
As 79 is greater than 45, so insert it as
the root node of the right subtree.
Insert: 65
https://www.javatpoint.com/binary-search-tree
Searching Element in BST
1.Compare the element with the root of the tree.
2.If the item is matched then return the location of the node.
3.Otherwise check if item is less than the element present on root, if so then move to the left sub-tree.
4.If not, then move to the right sub-tree.
5.Repeat this procedure recursively until match found.
6.If element is not found then return NULL.
Searching Element in BST
Delete a node in BST
The node to be deleted is a leaf node
In the following image, we are deleting the node 85,
since the node is a leaf node, therefore the node will be
replaced with NULL and allocated space will be freed.
Case 3: Deleting a node with one child: remove the node and
replace it with its child.
Applications of Binary Search tree
Applications of Binary Search tree:
•BSTs are used for indexing.
•It is also used to implement various searching algorithms.
•IT can be used to implement various data structures.
•BSTs can be used in decision support systems to store and quickly retrieve data.
•BSTs can be used to store and quickly retrieve data in computer simulations.
•BSTs can be used to implement fast autocomplete systems.
Real-time Application of Binary Search tree:
•BSTs are used for indexing in databases.
•It is used to implement searching algorithms.
•BSTs are used to implement Huffman coding algorithm.
•It is also used to implement dictionaries.
•Used for data caching.
•Used in Priority queues.
•Used in spell checkers.
Advantages of Binary Search Tree:
•BST is fast in insertion and deletion when balanced. It is fast with a time complexity of O(log n).
•BST is also for fast searching, with a time complexity of O(log n) for most operations.
•BST is efficient. It is efficient because they only store the elements and do not require additional memory for pointers or other data structures.
•We can also do range queries – find keys between N and M (N <= M).
•BST code is simple as compared to other data structures.
•BST can automatically sort elements as they are inserted, so the elements are always stored in a sorted order.
•BST can be easily modified to store additional data or to support other operations. This makes it flexible.
AVL Tree
• AVL Tree is invented by GM Adelson - Velsky and EM Landis in 1962.
• The tree is named AVL in honour of its inventors.
• AVL Tree can be defined as height balanced binary search tree in which each node is associated with a
balance factor which is calculated by subtracting the height of its right sub-tree from that of its left sub-
tree.
Balance Factor
• Balance factor of a node in an AVL tree is the difference
between the height of the left subtree and that of the right
subtree of that node. Tree is said to be balanced if balance
factor of each node is in between -1 to 1, otherwise, the
tree will be unbalanced and need to be balanced.
Balance Factor (k) = height (left(k)) - height (right(k))
AVL tree may become unbalanced, if a node is inserted in the left subtree of the left subtree. The tree then
needs a right rotation.
Step 3.
Now one left
Rotation with
respect to node (1)
4. LR Imbalance
Solution
2 Rotation
First Left-
Then Right Rotation
-1 1 -2
1 0 1
3 0
2 -1
0 2 -1 2 -2
2
4 5 4 0
0 1
0 -1 -2
1 3 0 1 3 0 1 3
0
5 0
0 3
After Rotation
4 0 4 -1
AVL Tree
5 0
ashish0751@gmail.com
Construct
AVL Tree with
data items:
51, 26, 11, 6,
8, 4, 31
ashish0751@gmail.com 235
AVL Example (insert 42)
which node is imbalanced ? Ans Node (40)
How ? When 42 is added
Conclusion: Node (46) will be connected in right
Which imbalanced ; it is RLLR , remove last two letters, with node(45)
Now it is RL imbalanced node (45) will be added in the right sub tree but (50)
is added. But it is confirmed that it will be part of
2 Rotations are required right subtree, therefore it is connected node(45)’s
right and (50)’s Left
First Right rotation with respect to 50
After one left rotation with respect to node (40)
Deletion in AVL Tree
• Deleting a node from an AVL tree is similar to that in a binary search tree.
• Deletion may disturb the balance factor of an AVL tree and therefore the tree needs to be rebalanced in order to
maintain the AVLness.
• For this purpose, we need to perform rotations.
AVL Deletion
of keys is 4 and maximum 5 children are possible. •Internal node= (ceiling of m/2 - 1) minimum keys.
https://www.youtube.com/watch?v=pN4C8cLVc7I
2-3 Tree
• 2-3 Tree is also a height balanced tree.
• The time complexity of search/insert/delete is O(log N) .
minimum of m/2 children.
of ⌈m/2⌉ - 1 keys.
B+ Tree
B*-Tree
B*-tree of order m is a search tree that is either empty or that satisfies three
properties:
•The root node has minimum two and maximum 2 floor ((2m-2)/3) +1 children
•Other internal nodes have the minimum floor ((2m-1)/3) and maximum m
children
•All external nodes are on the same level.
The advantage of using B* trees over B-trees is a unique feature called the
‘two-to-three’ split. By this, the minimum number of keys in each node is not
half the maximum number, but two-thirds of it, making data far more compact.
Unit-4
AI&ML, AI&DS, IIOT
Unit 4
Section 1
File
File
File is a collection of records related to each other. The file size is limited by the size of memory and storage
medium.
A file structure is a combination of representations for data in files. It is also a collection of operations for
accessing the data. It enables applications to read, write, and modify data. File structures may also help to find the
data that matches certain criteria. An improvement in file structure has a great role in making applications
hundreds of times faster.
The main goal of developing file structures is to minimize the number of trips to the disk in order to get
desired information. It ideally corresponds to getting what we need in one disk access or getting it with as little
disk access as possible.
A good file structure should:
•Fast access to a great capacity
File Organization
•Reduce the number of disk accesses
• File organization ensures that records are available for processing. It is used to •Manage growth by splitting these collections.
determine an efficient file organization for each base relation.
There are three types of organizing the file:
• For example, if we want to retrieve employee records in alphabetical order of 1. Sequential access file organization
name. Sorting the file by employee name is a good file organization. 2. Direct access file organization
• However, if we want to retrieve all employees whose marks are in a certain 3. Indexed sequential access file organization
range, a file is ordered by employee name would not be a good file organization.
1. Sequential access file organization 3. Indexed sequential access file organization
•Storing and sorting in contiguous block within files on tape or disk is called as sequential access file •Indexed sequential access file combines both
organization. sequential file and direct access file organization.
•In sequential access file organization, all records are stored in a sequential order. The records are arranged in the •In indexed sequential access file, records are stored
ascending or descending order of a key field. randomly on a direct access device such as magnetic
•Sequential file search starts from the beginning of the file and the records can be added at the end of the file. disk by a primary key.
•In sequential file, it is not possible to add a record in the middle of the file without rewriting the file. •This file have multiple keys. These keys can be
alphanumeric in which the records are ordered is
•Advantages of sequential file called primary key.
•It is simple to program and easy to design. •The data can be access either sequentially or
•Sequential file is best use if storage space. randomly using the index. The index is stored in a
•Disadvantages of sequential file file and read into memory when the file is opened.
•Sequential file is time consuming process. •Advantages of Indexed sequential access file
•It has high data redundancy. organization
•Random searching is not possible. In indexed sequential access file, sequential file and
random file access is possible.
•It accesses the records very fast if the index table is
2. Direct access file organization properly organized.
•Direct access file is also known as random access or relative file organization. •The records can be inserted in the middle of the file.
•In direct access file, all records are stored in direct access storage device (DASD), such as hard disk. The records are •It provides quick access for sequential and direct
randomly placed throughout the file. The records does not need to be in sequence because they are updated directly and processing.
rewritten back in the same location. •It reduces the degree of the sequential search.
•This file organization is useful for immediate access to large amount of information. It is used in accessing large •Disadvantages of Indexed sequential access file
databases. It is also called as hashing. organization
•Advantages of direct access file organization Indexed sequential access file requires unique keys
Direct access file helps in online transaction processing system (OLTP) like online railway reservation system. and periodic reorganization.
•In direct access file, sorting of the records are not required. •Indexed sequential access file takes longer time to
•It accesses the desired records immediately, It updates several files quickly. search the index for the data access or retrieval.
•It has better control over record allocation. •It requires more storage space.
•Disadvantages of direct access file organization •It is expensive because it requires special software.
Direct access file does not provide back up facility and It is expensive. •It is less efficient in the use of storage space as
•It has less storage space as compared to sequential file. compared to other file organizations.
Indexed sequential access file organization
Indexing
Indexing is a data structure technique that helps to speed up data
retrieval. As we can quickly locate and access the data in the database, it
is a must-know data structure that will be needed for database
optimizing. Indexing minimizes the number of disk accesses required Indexing is a general name for a process of partitioning
when a query is processed. Indexes are created as a combination of the
intended at speeding up data look-ups. Indexing can
two columns.
•First column is the Search key. It contains a copy of the primary key or partition the data set based on a value of a field or a
candidate key of the table. The values of this column may be sorted or
combination of fields. It can also partition the data set based
not. But if the values are sorted, the corresponding data can be accessed
easily. on a value of a function, called hash function, computed
•Second column is the Data reference or Pointer. It contains the
from the data in a field or a combination of fields. In this
address of the disk block where we can find the corresponding key value.
specific case, indexing is called data hashing.
Read more:
https://medium.com/nerd-for-tech/indexing-data-structure
s-aa7363693c40
Hashing (Hash Function)
• In a hash table, a new index is processed using the keys. and, the element corresponding to that
key is stored in the index. This process is called hashing.
• Let k be a key and h(x) be a hash function.
• Here, h(k) will give us a new index to store the
element linked with k.
Hash Collision
When the hash function generates the same index for multiple keys, there will be a conflict
(what value to be stored in that index). This is called a hash collision. When the two
different values have the same hash value, then the problem occurs between the two values,
known as a collision.
Example
If the key value is 26 and 16 then the index would be:
h(26) = 26%10 = 6
h(16) = 16%10 = 6
We can resolve the hash collision using one of the following techniques.
1. Collision resolution by chaining/Separate chaining
2. Open Addressing: Linear/Quadratic Probing and Double Hashing
Collision resolution by chaining/Separate chaining
Example: Let us consider a simple hash function as “key mod 7” and a sequence
of keys as 50, 700, 76, 85, 92, 73, 101
linked list.
Separate Chaining
0 / Chaining:
1 / All keys that map to the same
2 / table location are kept in a list (a.k.a. a
3 / “chain” or “bucket”)
4 /
5 / As easy as it sounds
6 /
7 / Example:
8 /
insert 10, 22, 107, 12, 42
9 /
with mod hashing
and TableSize = 10
Let hash(x) be the slot index computed using a hash function and S be the table size
If slot hash(x) % S is full, then we try (hash(x) + 1) % S
If (hash(x) + 1) % S is also full, then we try (hash(x) + 2) % S
If (hash(x) + 2) % S is also full, then we try (hash(x) + 3) % S
…………………………………………..
…………………………………………..
Let us consider a simple hash function as “key mod 7” and a sequence of keys as 50, 700,
76, 85, 92, 73, 101.
ashish0751@gmail.com
Linear Probing
ashish0751@gmail.com
Key Hash Function h(X) Index Collision Alt Index
79 h0(79) 9
= ( Hash(79) + F(0)2) % 10
= ((79 % 10) + 0) % 10
28 h0(28) 8
= ( Hash(28) + F(0)2) % 10
= ((28 % 10) + 0) % 10
39 h0(39) 9 The first
= ( Hash(39) + F(0)2) % 10 collision
= ((39 % 10) + 0) % 10 occurs
h1(39) 0 0
= ( Hash(39) + F(1)2) % 10
= ((39 % 10) + 1) % 10
68 h0(68) 8 The
= ( Hash(68) + F(0)2) % 10 collision
= ((68 % 10) + 0) % 10 occurs
h1(68) 9 Again
= ( Hash(68) + F(1)2) % 10 collision
= ((68 % 10) + 1) % 10 occurs
h2(68) 2 2
= ( Hash(68) + F(2)2) % 10
= ((68 % 10) + 4) % 10
89 h0(89) 9 The
= ( Hash(89) + F(0)2) % 10 collision
= ((89 % 10) + 0) % 10 occurs
h1(89) 0 Again
= ( Hash(89) + F(1)2) % 10 collision
= ((89 % 10) + 1) % 10 occurs
h2(89) 3 3
= ( Hash(89) + F(2)2) % 10
ashish0751@gmail.com = ((89 % 10) + 4) % 10 Quadratic Probing
Quadratic Probing
We need a hash to save the mapping from phone number to name and name to phone number:
Using Array — Direct Addressing Using List
To implement a hash using an array, Unlike using array, using list will
firstly we convert phone number to help us to save space. We just new
number: data to list but the
'0930-345-234' -> 0930345234 operation search still costs O(n) to
perform.
name_to_number = { 'john' : '0930-345-234', 'alice' :
'0934-234-546', ... }
number_to_name = { '0930-345-234' : 'john', '0934-
Application: Telephone Directory
Hash integer
We want to transform phone number into smaller set — 1000 elements.
•Take phone numbers up to length 7, for example 148–25–67.
•Convert phone numbers to integers 1482 567
•Choose prime number bigger than 10⁷ , e.g. p = 10 000 019
•Choose hash table size, e.g. m = 1 000
We will make phone number bigger than prime p, and modulo to m, the result will be a set of hashing
values that smaller than m.
We just choose a, b number big enough to be multipliers. So ax +b will be larger than p. We modulo p
and modulo m to get the result.
Select a = 34, b = 2, so h = h(34,2,p) and consider x = 1 482 567
corresponding to phone number 148–25–67. p = 10 000 019.h(x) = ((34 ×
1482567 + 2) mod 10000019) mode 1000 = 185
Dictionary (Hash Tables)
• A dictionary is a general concept that maps keys to values. There are many ways
to implement such a mapping. 0 12
2
• A hashtable is a specific way to implement a dictionary. 1 5
“key”
• Consider an array or an array list.
2 42
3 91
• To access a value, you use an integer index. Key 2 maps to
value 42. 4 0
• The array “maps” the index to a data value stored in the array.
5 57
• We can consider the index value to be the “key” to obtaining the
corresponding data value.
• As long as the index value is within range, there is a strict one-to-one Hash Table
correspondence between an index value and a stored data value. Hashing in data structure uses hash tables to store the key-
• A hash table also stores data values. value pairs. The hash table then uses the hash function to
generate an index. Hashing uses this unique index to perform
• Use a key to obtain the corresponding data value.
insert, update, and search operations.
• The key does not have to be an integer value. It can be defined as a bucket where the data are stored in an
• For example, the key could be a string. array format. These data have their own index value. If the
index values are known then the process of accessing the
data is quicker.
ashish0751@gmail.com
Unit 4
Section 2
Graph Theory
NONLINEAR DATA STRUCTURES: GRAPH
vertex V and edge E can be represented as: V = {v1, v2, v3, v4, v5, v6} and E = {e1, e2, e3,
e4, e5, e6} E = {(v1, v2) (v2, v3) (v1, v3) (v3, v4), (v3, v5) (v5, v6)}. There are six edges and
vertices in the graph.
vertices
1 2
3
5 4
2
Edges
1
5 3
Directed
Edge 4
BASIC TERMINOLOGIES
A directed graph G is defined as an ordered pair (V, E) where, V is a set of vertices and the ordered pairs in E are called edges on V. A
directed graph can be represented geometrically as a set of marked points (called vertices) V with a set of arrows (called edges) E between
pairs of points (or vertex or nodes) so that there is at most one arrow from one vertex to another vertex. For example, Fig 9.2 shows a directed
graph, where G = {a, b, c, d}, {(a, b), (a, d), (d, b), (d, d), (c, c)}
An edge (a, b), in said to the incident with the vertices it joints, i.e., a, b. We can also say that the
edge (a, b) is incident from a to b. The vertex a is called the initial vertex and the vertex b is called the
terminal vertex of the edge (a, b). If an edge that is incident from and into the same vertex, say (d, d) of
(c, c) in Fig. 9.2, is called a loop.
Two vertices are said to be adjacent if they are joined by an edge. Consider edge (a, b), the vertex a
is said to be adjacent to the vertex b, and the vertex b is said to be adjacent from the vertex a. A vertex is
said to be an isolated vertex if there is no edge incident with it. In Fig. 9.2 vertex C is an isolated vertex.
An undirected graph G is defined abstractly as an ordered pair (V, E), where V is a set of vertices
and the E is a set at edges. An undirected graph can be represented geometrically as a set of marked
points (called vertices) V with a set at lines (called edges) E between the points. An undirected graph G is
shown in Fig. 9.3.
The degree of a vertex v, denoted deg(v), is the number of incident
edges of v. The in-degree and out-degree of a vertex v are the number
of the incoming and outgoing edges of v, and are denoted indeg(v) and
outdeg(v), respectively.
Degree of 3 is 3
2 In Degree of 3 is 2
1 Out Degree of 3 is 1
5 3 Degree of 4 is 2
In Degree of 4 is 1
4
adjacent
1 2
3
Incoming
Edges 2
5 4
2
Incident 3
1
5 3
Outgoing
Edges 2 4
Lec.11
Let G = (V, E) be a graph. A graph G1 = (V1, E1) is said to be a sub-graph of G if E1 is a subset at E and V1 is a subset at V such that the
edges in E1 are incident only with the vertices in V1. For example Fig 9.5 (b) is a sub-graph at Fig. 9.5(a). A sub- graph of G is said to be a
spanning sub-graph if it contains all the vertices of G. For example Fig. 9.5(c) shows a spanning sub-graph at Fig. 9.5(a).
The number of edges incident on a vertex is its degree. The degree of vertex a, is written as degree (a). If the degree of
vertex a is zero, then vertex a is called isolated vertex. For example the degree of the vertex a in Fig. 9.5 is 3.
A graph G is said to be weighted graph if every edge and/or vertices in the graph is assigned with some weight or value. A
weighted graph can be defined as G = (V, E, We, Wv) where V is the set of vertices, E is the set at edges and We is a weights of
the edges whose domain is E and Wv is a weight to the vertices whose domain is V. Consider the following graph.
Fig 9:6 shows the distance in km b/w four cities in India. Here V = {N, K, M, C,} E = {(N, K), (N, M), (M, K), (M,C), (K,C)} We = {55,47,
39, 27, 113} and Wv = {N, K, M, C} the weight at the vertices is not necessary to maintain have become the set Wv and V are same.
Allowing for two undirected edges to have the same end vertices, and for two
directed edges to have the same origin and the same destination. Such edges
are called parallel edges or multiple edges.
e3
A e4 B
e5
e2 e1
D C
e2
e3
self-loop A e4 B
e5
e1
An edge (undirected or directed) is a
self-loop if its two endpoints coincide
D C
A B
simple
Graph Representation
• Adjacency Matrix
• Incidence Matrix
• Adjacency List
2. Incidence Matrix
e1 e2 e3 e4 e5
e1 1 1 0 0 0
A B A
e3 e5 B -1 0 1 0 1
e2
C 0 0 0 -1 -1
e4
D C D 0 -1 -1 1 0
Adjacency List
•Open walk- A walk is said to be an open walk if the starting and ending
vertices are different i.e. the origin vertex and terminal vertex are different.
Closed walk- A walk is said to be a closed walk if the starting and ending
vertices are identical i.e. if a walk starts and ends at the same vertex, then it is said to
be a closed walk.
Trail – Circuit –
Trail is an open walk in which no edge is repeated. Traversing a graph such that not an edge is repeated but vertex
Vertex can be repeated. can be repeated and it is closed also i.e. it is a closed trail.
Vertex can be repeated.
Edge can not be repeated.
• Given a directed graph, find out if a vertex j is reachable from another vertex i
for all vertex pairs (i, j) in the given graph. Here reachable mean that there is a
path from vertex i to j. The reach-ability matrix is called the transitive closure
of a graph.
Activity Network
First define the activities and estimate their duration, we are ready to create an activity network diagram which is a
graphical representation of the logical relationship (i.e., dependencies) among the project activities.
Activities are carried out in order. Therefore, they have predecessors and successors. They have logical
relationships or dependencies which show the sequence in which the activities are to be performed. There are four
relationships between activities, which can be indicated as “Finish-to-Start” (FS), “Finish-to-Finish” (FF), “Start-
to-Start” (SS), and “Start-to-Finish” (SF).
2. Lag
A lag is the amount of time a successor activity can be delayed with
respect to a predecessor activity Consider that we should paint one room
in our house. We need to apply plaster to walls first (predecessor). When
the walls dry, we can paint them (successor). It is an FS relationship.
Read More:
However, we need to wait for two days for the walls to dry. This causes a
https://pressbooks.ulib.csuohio.edu/pro
two-day delay between two activities which is called a lag (Figure 7.3). ject-management-navigating-the-compl
exity/chapter/7-4-creating-an-activity-n
etwork-diagram/
Can a graph have more than one valid topological ordering? Yep! In the example
D E
A B F C D E
Note that F can go anywhere in this list because it is not connected.
Also the solution is not unique.
Topo sort - bad example
B
C
A Any linear ordering in which
an arrow goes to the left
F is not a valid solution
D E
A B F C E D
NO!
Topological Sort
Given a digraph G = (V, E), find a linear ordering of
its vertices such that:
for any edge (v, w) in E, v precedes w in the ordering
B
C
A
F
D E
12/26/03
Only acyclic graphs can be topo. sorted
B
C
A
F
D E
B
C
A
Select
B B
C C
A A
F F
D E D E
Step 3: Delete this vertex of in-degree Repeat Step 1 and Step 2 Step 3until
0 and all its outgoing edges from the graph is empty
graph. Place it in the output.
Continue until done
B Select
C
A
B
F A C
D F A
E
D E
319
Select B. Copy to sorted list. Delete B
B and its edges.
B
C
F A B
D E
C
Select C. Copy to sorted list. Delete C and its edges.
C
F A B C
12/26/03 D E 320
D Select D. Copy to sorted list. Delete D and its edges.
F A B C D
D E
F A B C D E F
E
Done
B
C
A
F
D E
A B C D E F
0 1 2 4
1 2 3
1 3 4 5
In-Degree
array; or add a 2 4 5
field to array A 5
2
0 6
0 1 2 4
Queue 1 6
1 2 3
1 3 4 5
2 3
6 2 4 5
1
2 5
4 5
0 6
ashish0751@gmail.com