Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

ALGORITHM SPACE COMPLEXITY Omega Notation, Ω

•Algorithm is a step-by-step finite sequence of instruction, to solve a well Space complexity of an algorithm represents the amount of memory space
The notation Ω(n) is the formal way to express the lower bound of an
defined computational problem. required by the algorithm in its life cycle.
algorithm's running time. It measures the best case time complexity or
•An algorithm can be implemented in more than one programming language. Space complexity S(P) of any algorithm P is
•In practice to solve any complex real life problems; S(P) = C + SP(I), the best amount of time an algorithm can possibly take to complete.
➢ first we have to define the problems. where C is the fixed part , and S(I) is the variable part of the algorithm, which
➢Second step is to design the algorithm to solve that Problem. depends on instance characteristic I
ALGORITHM - PROPERTIES - A fixed part - a space required to store certain data code ,constants that
An algorithm should have the following characteristics − are independent of the size of the problem..
➢Input − An algorithm should have 0 or more well-defined inputs. - A variable part - a space required by variables, whose size depends on
➢Output − An algorithm should have 1 or more well-defined outputs, and the size of the problem..
should match the desired output. TIME COMPLEXITY
➢Finiteness − Algorithms must terminate after a finite number of steps. Time complexity of an algorithm represents the amount of time required by
➢Definiteness –Algorithm should be clear and unambiguous. Each of its the algorithm to run to completion. Time requirements can be defined as a
steps(or phases), and their inputs/outputs should be clear and must lead to numerical function T(n), where T(n) can be measured as the number of steps,
only one meaning. provided each step consumes constant time. Theta Notation, θ
➢Effectiveness – Each step should be basic and independent. T(n)=Compile time + Execution time The notation θ(n) is the formal way to express both the lower bound and
ALGORITHM ANALYSIS (fixed part) (variable part) the upper bound of an algorithm's running time. It is represented as
A problem can be solved in more than one way. Hence, many solution The exact time will depend on the implementation of the algorithm, compiler follows −
algorithms can be derived for a given problem. The next step is to analyze used, input size, the CPU speed, other hardware characteristics/specifications
those proposed solution algorithms and implement the best suitable solution and so on. To measure the time complexity accurately, we have to count all
ALGORITHM ANALYSIS sorts of operations performed in an algorithm. Since the time complexity
Efficiency of an algorithm can be analyzed at two different stages, before depends on input size we are focussing only on the execution time of an
implementation and after implementation. algorithm not compile time.
They are the following − Big Oh Notation, Ο
➢ A Priori Analysis - Theoretical approach The notation Ο(n) is the formal way to express the upper bound of an
➢ A Posteriori Analysis – done during implementation algorithm's running time. It measures the worst case time complexity or the
longest amount of time an algorithm can possibly take to complete.

Algorithm For Deleting An Element From A Linear Queue Algorithm for PUSH operation:
Input : Queue with elements with FRONT and REAR pointer. Step 1: IF REAR = MAX-1
Output: Deleted element ,ITEM. Write OVERFLOW
Data Structure: Array representation of queue. Goto step 4
Step 1: If FRONT= -1 then [END OF IF]
1.1: Print Queue is Empty Step 2: IF FRONT=-1 and REAR=-1
1.2: Exit SET FRONT = REAR = 0
Step 2 : Else ELSE
2.1: ITEM= Q[FRONT]. SET REAR = REAR+1
2.2: If FRONT=REAR then [END OF IF]
FRONT=REAR=-1 Step 3: SET QUEUE[REAR] = NUM
2.3: Else Step 4: EXIT
FRONT=FRONT+1
Step 4: End if Algorithm for POP operation:
Step 5: Stop Input: A stack, stk with elements
Queue is a simple powerful data structure to solve numerous computer Output: Item removed from the stack.
applications. Queue is also an ordered collection of homogeneous STACK Data Structure: Array with TOP as the pointer for POP
data elements like stack. It's a linear data structure. Queue works on the A stack is an ordered collection of homogeneous data elements 1. Start
principle of FIFO(First In First Out) manner. The element which is inserted In stack insertion and deletion take place at one end only. It is a linear data 2. Check for UNDERFLOW condition ,
first will leave first in the queue. structure. Stack works in LIFO(Last In First Out) manner. ie the last Item
If TOP== -1 then
A Queue can be represented in memory in many ways, mainly in two ways: which is Put into stack will be taken first Real time Examples for stack: A
1. Using 1-D array(Static) pile of coins. Order supply in a restaurant. Shipment in Cargo. Shunting of Print Underflow . Cannot POP .
2. Using Linked list (Dynamic) trains in a rail yard. 3. Else
Algorithm For Inserting An Element Into A Linear Queue 3.1 Print the item popped, stk[TOP].
Assume FRONT=REAR= -1 STACK -TERMINOLOGY 3.2 TOP=TOP-1
Step1 : Start 4. Stop
Step 2: If REAR=MAX.SIZE then 1. ITEM: An element in the stack is termed as ITEM
2.1 : Print Queue is Full 2. TOP : End where insertion and deletion takes place is called TOP
2.2 : Exit 3. MAX.SIZE : The maximum number of elements that a stack can
Step 3 : Else accommodate is termed as MAX. SIZE
3.1: If FRONT= -1 and REAR = -1 then 4. PUSH : The insertion operation in the stack is termed as PUSH operation
FRONT =REAR= 0. 5. POP : The deletion operation in the stack is termed as POP operation.
3.2: Q[REAR] = ITEM
3.3 : REAR= REAR + 1

CIRCULAR QUEUE The prefixes pre, post and in represents the relative positions Binary Search Algo
A circular queue is one in which the first element comes just after the last of the operators with respect to the operator. 1. Step 1: set beg = lower_bound, end = upper_bound, pos = - 1
element. Its a method to overcome the problem of unutilized space In INFIX notation the operator is between the two operands. 2. Step 2: repeat steps 3 and 4 while beg <=end
in linear queues implemented as arrays. A circular queue also has two (IN means between) 3. Step 3: set mid = (beg + end)/2
pointers: FRONT and REAR like a linear queue. operand1 < operator> operand2 4. Step 4: if a[mid] = val
In PREFIX notation the operator precedes the two operands. 5. set pos = mid
(PRE means before)
6. print pos
< operator> operand1 operand2
Algorithm for inserting an element into circular queue 7. go to step 6
In POSTFIX notation the operator follows the two
Assume FRONT=REAR = -1 8. else if a[mid] > val
operands. (POST means after)
Step 1 : Start operand1 operand2 < operator> 9. set end = mid - 1
Step 2 : If FRONT=(REAR+1)% MAX.SIZE ADVANTAGES OF POSTFIX OVER INFIX, 10. else
2.1 : Print Queue is Full =================================== 11. set beg = mid + 1
2.2 : Exit In postfix and prefix expressions such kind of parenthesization is not 12. [end of if]
Step 3: Read the item to be inserted ,item required. Moreover the expression needs to be scanned only once from left to 13. [end of loop]
Step 4: If FRONT= -1 then right. 14. Step 5: if pos = -1
4.1 : FRONT=0 a. Operand - add it to the output string. 15. print "value is not present in the array"
Step 5: REAR=(REAR+1)%MAX.SIZE b. (‘ - push it to stack 16. [end of if]
Step 6:CQ[REAR]= item c. operator ‘op’ is found then 17. Step 6: exit
Step 7:Stop { The time complexity of the binary search algorithm is O(log n). The
Algorithm for deleting an element Check the priority of ‘op’ with the operators available in Stack. best-case time complexity would be O(1) when the central index would
Step 1 : Start If priority (‘op’)>= priority (operator available in stack ) directly match the desired value. Binary search worst case differs from that.
Step 2 : If FRONT=-1 then Push op into stack The worst-case scenario could be the values at either extremity of the list or
2.1 :Print Queue is Empty Else values not in the list.
2.2 : Exit Repeatedly Pop operators from the stack and add it to the
Step 3:Else output String, until an operator in stack that has Priority less Representation of Polynomials Using array
3.1 : Print Deleted item is CQ[FRONT] than op is found.
3.2 : If FRONT=REAR then Push ‘op’ into stack. The simple way is to represent a polynomial with degree 'n' and store the
FRONT=REAR= -1 } coefficient of n+1 terms of the polynomial in the array. So every array
3.3 : FRONT=(FRONT+1)%MAX.SIZE d. ‘)’ - then element will consist of two values:
Step 4: End if {
Step 5: Stop ● Coefficient and
Repeatedly POP each element from the stack and Add it to
● Exponent
the output string until a ‘(‘ is found.
When ‘(‘ is encountered in stack simply remove it from stack
and DONOT add to output string
}
Dynamic memory allocation in C Doubly linked list is a complex type of linked list in which a node contains a
The concept of dynamic memory allocation in c language enables the C pointer to the previous as well as the next node in the sequence. Therefore, in
ArrayList LinkedList
programmer to allocate memory at runtime. Dynamic memory allocation in c a doubly linked list, a node consists of three parts: node data, pointer to the
language is possible by 4 functions of stdlib.h header file. 1) ArrayList internally uses a dynamic array to LinkedList internally uses a doubly linked next node in sequence (next pointer) , pointer to the previous node (previous
pointer).
store the elements. list to store the elements.
1. malloc()
1. struct node
2. calloc()
2) Manipulation with ArrayList is slow because it Manipulation with LinkedList is faster than 2. {
3. realloc()
internally uses an array. If any element is ArrayList because it uses a doubly linked 3. struct node *prev;
4. free()
4. int data;
removed from the array, all the other elements are list, so no bit shifting is required in
The malloc() function allocates a single block of requested memory. It 5. struct node *next;
doesn't initialize memory at execution time, so it has garbage value initially. shifted in memory. memory. 6. }
It returns NULL if memory is not sufficient.
ptr=(cast-type*)malloc(byte-size) 3) An ArrayList class can act as a list only LinkedList class can act as a list and
because it implements List only. queue both because it implements List and
The calloc() function allocates multiple blocks of requested memory. It
initially initializes all bytes to zero. It returns NULL if memory is not Deque interfaces.
sufficient.
ptr=(cast-type*)calloc(number, byte-size) 4) ArrayList is better for storing and accessing LinkedList is better for manipulating
If memory is not sufficient for malloc() or calloc(), you can reallocate the data. data.
memory by realloc() function. In short, it changes the memory size.
Insertion at beginning - Adding the node into the linked list at beginning.
5) The memory location for the elements of an The location for the elements of a linked Insertion at end - Adding the node into the linked list to the end.
ptr=realloc(ptr, new-size)
Insertion after specified node - Adding the node into the linked list after the
The memory occupied by malloc() or calloc() functions must be released by ArrayList is contiguous. list is not contagious. specified node.
calling free() function. Otherwise, it will consume memory until the program
Deletion at beginning - Removing the node from beginning of the list
exits. 6) Generally, when an ArrayList is initialized, a There is no case of default capacity in a Deletion at the end - Removing the node from end of the list.
free(ptr) Deletion of the node having given data - Removing the node which is
default capacity of 10 is assigned to the LinkedList. In LinkedList, an empty list is
present just after the node containing the given data.
ArrayList. created when a LinkedList is initialized. Searching - Comparing each node data with the item to be searched and
return the location of the item in the list if the item found else return null.
7) To be precise, an ArrayList is a resizable array. LinkedList implements the doubly linked
Traversing - Visiting each node of the list at least once in order to perform
some specific operation like searching, sorting, display, etc.
list of the list interface.

Circular Singly Linked List ALGORITHM – UNDIRECTED GRAPH DFS- DEPTH FIRST SEARCH TRAVERSAL
In a circular Singly linked list, the last node of the list contains a pointer to 1.Start � Starting from a given node this traversal visits all the
the first node of the list. We can have circular singly linked lists as well as 2. Read the no: of vertices, n nodes up to the deepest level and so on.
circular doubly linked lists. 3. Repeat the following until i!=n � General approach:
3.1. j=i Start from a vertex v . Visit vertex v first. Then visit all
We traverse a circular singly linked list until we reach the same node where 3.2 . Repeat the following until j!=n
we started. The circular singly linked list has no beginning and no ending. the vertices along the path which begins at v.
if we want an edge from I to j, Eg: In the given graph let the starting vertex be S.
There is no null value present in the next part of any of the nodes.
Assign G[i][j]=G[j][i]= 1
DFS order is S-A-D-C-B.
Circular linked list are mostly used in task maintenance in operating systems. else
Assign G[i][j]=G[j][i]= 0
[Visit Vertex S. Next, visit immediate
4. Stop Adjacent vertex is A, then D. after that
C last B. Thus we get
ALGORITHM – DIRECTED GRAPH S-A-D-C-]
1.Start
2. Read the no: of vertices, n ALGORITHM FOR DFS
3. Repeat the following until i!=n DFS()
3.1 Repeat the following until j!=n 1. Push v into stack
if we want an edge from I to j, 2. While stack not empty
Assign G[i][j]= 1 2.1. Pop vertex v from the stack
Circular Doubly Linked List else 2.2. If v is not in visited
Assign G[i][j]= 0 {
Circular doubly linked list is a more complexed type of data structure in 4. Stop Visit the vertex and Store v in Visit[]
which a node contain pointers to its previous node as well as the next node. Push all adjacent vertex of v to stack.
Circular doubly linked list doesn't contain NULL in any of the node. The last }
node of the list contains the address of the first node of the list. The first node
3. stop
of the list also contain address of the last node in its previous pointer.
Binary search tree is a non-linear data structure in which one node is
connected to n number of nodes. It is a node-based data structure. A node
can be represented in a binary search tree with three fields, i.e., data part,
left-child, and right-child. A node can be connected to the utmost two child
nodes in a binary search tree, so the node contains two pointers
1. struct node
2. {
3. int data;
4. struct node *left;
5. struct node *right; / 6. }

BFS- BREADTH FIRST SEARCH TRAVERSAL Quick Sort Merge Sort


� Here any vertex in the level I will be visited only after - Its an internal as well as external sorting algorithm.
the visit of all the vertices in its proceeding level . - Also known as Partition Exchange Sort.
- Quick sort is based on partition.
- Merge means combining
� It is similar to pre order traversal of a tree. - The basic concept of quick sort process is pick one element from - In this type of sorting we will take one unsorted array break
� General approach: an array and rearranges the remaining elements around it. This them to different subarrays and combine the solutions to
Start from a vertex v . Visit vertex v first. Then visit all chosen element is called pivot get a single sorted array.
the adjacent vertices of v. - Once pivot is chosen, array gets divided into two partitions where - It works on the principle of divide and conquer.
Eg: In the given graph let the starting vertex be S. - Left part contain all elements less than pivot.
- Right part contain all elements greater than pivot.
- Divide & Conquer:
BFS order is S-A-B-C-D.
- This procedure of choosing pivot and partition the list is applied It is a general algorithm design paradigm. Divide:
[Visit S first. Then visit all the adjacent
Vertices of S ie A,B, C .After that visit recursively until sub-lists consisting of only one element. Divide the input data S in two disjoint subsets S1 and S2
- Split point- the position of the pivot element after setting pivot Conquer: combine the solutions for S 1 and S2 into a
the all adjacent vertices of vertex A(ie S,D) CHOOSING PIVOT
then Vertex B and so on. Vertex is visited solution for S
How we can choose pivot
Only once.] 1) Select first element as pivot
WORKING
2) Randomly choose pivot. - Also called two way merge sort.
ALGORITHM FOR DFS 3) Select Pivot as the median value from the first,last and middle - The basic concept of merge sort is:
element of the array. Divides the list into two smaller sub-lists of
� Let v be the starting vertex and visit[] array to store the Algorithm approximately equal size.
result. Quicksort(low,high){ Recursively repeat this procedure till only one element
if(low<high){
DFS() is left in the sub-list.
j=partition(A,low,high)
1. Insert v into queue. Quicksort(low,j-1) After this, various sorted sub-lists are merged to form
2. While queue is not empty Quicksort(j,high-1) } a sorted parent list.
2.1. Delete vertex V from the queue. partition(A,low,high){ This process goes on recursively till the original sorted
2.2. If v is not visited 1. piv=A[0]. list arrived
{ 2. Assign l=low,h=high
Visit vertex v and add it to v in Visit[] 3. While(l<=h)
Add all adjacent vertices of v to queue. 3.1 .while(A[l]<pivot)
l++;
} 3.2 .while(A[h]>pivot)
3. stop h--;
3.3. if(l<h)
swap (A[l],A[h])
4. swap(piv,A[h])
5. Return h
Algorithm: Heap Sort ALGORITHM: INSERTION SORT
mergesort( low,high) HeapSort(arr) Step1: Assign i=1
if (low<=high){ Step 2: while i<n do the following:
BuildMaxHeap(arr) 2.1: temp=a[i]
mid=(low+high)/2
mergesort(low,mid) //divide for i = length(arr) to 2 2.2: Assign j=i-1
mergesort(mid+1,high) //divide swap arr[1] with arr[i] 2.3: While j>=0 and a[j]>temp
merge(low,mid,high) //conquer a[j+1]=a[j]
heap_size[arr] = heap_size[arr] ? 1 j=j-1
}
Merge(low,mid,high){ MaxHeapify(arr,1) 2.4:End while
1.Assign i=low,j=mid+1,k=low. End 2.5: a[j+1]=temp
2. While (i<=mid && j<=high) 2.6: i=i+1
2.1 . If a[i]<a[j]{ Step 3: Stop
b[k]=a[i] BuildMaxHeap(arr)
i=i+1 ALGORITHM: SELECTION SORT
heap_size(arr) = length(arr) Step1: Assign i=0
k=k+1
} for i = length(arr)/2 to 1 Step 2: while (i<n-1) do the following:
2.2 else{ MaxHeapify(arr,i) 2.1: j=i+1
b[k]=a[j] 2.2: Assign small=i
End 2.3: While j<n do
j=j+1
k=k+1 if A[small]>A[j]
} MaxHeapify(arr,i) small=j
3. While(i<=mid) j=j+1
L = left(i) 2.4:End while
3.1: b[k]=a[i]
3.2: i=i+1 R = right(i) 2.5: if (i!=small)
3.3:k=k+1 if L ? heap_size[arr] and arr[L] > arr[i] //swap A[i],A[small]
4. While(j<=high) temp=A[i]
largest = L A[i]= A[small]
4.1: b[k]=a[j]
4.2: j=j+1 else A[small]=temp
4.3:k=k+1 largest = i 2.6: i=i+1
5. Assign i=low Step 3: End while
if R ? heap_size[arr] and arr[R] > arr[largest]
6.While i<=high Step 4: Stop
6.1. a[i]=b[i] largest = R
6.2 . i=i+1 if largest != i
swap arr[i] with arr[largest]
MaxHeapify(arr,largest)
End

You might also like