






















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Algorithm Analysis full notes for Semester
Typology: Study notes
1 / 30
This page cannot be seen from the preview
Don't miss anything!
1.1) The Analysis of Algorithm
1.2) Time and Space Complexities
1.3) Asymptotic Notation
1.4) Classes of Algorithm
1.5) Big-Oh Notation
1.6) Big-Omega Notation (Ω)
1.1 – The Analysis of Algorithm
Data Structure:
A systematic way of accessing and organizing data is known as Data Structure.
Algorithm:
An Algorithm is a step by step sequence of instruction to solve the computational problem in a finite amount of time in an English language.
Algorithm can be in any natural language.
In other words, an algorithm is logical representation of instructions which should be executed to perform a meaningful task.
Every algorithm should have the five characteristics:
Input: Any algorithm should take either zero or more inputs.
Output: Any algorithm should produce one or more outputs.
Definiteness: Each and every step of algorithm should be defined clearly.
Effectiveness: Any person can be able to calculate the values involved in the procedure (process) of algorithm. (Ex: Dry Run)
Termination: Any algorithm must be terminated with finite no. of steps.
After designing an algorithm, the analysis of algorithm comes.
The analysis of algorithm is used to check and predict the correctness of an algorithm.
The analysis of algorithm means tracing the all steps of instructions, checking logical correctness and mathematical techniques (equations).
The simple design of an algorithm makes easier to be implemented. That means, the simplicity of an algorithm makes us to easily analyze (dry run) the problem.
1.2 – Time and Space Complexities
Complexity of an algorithm is also known as computational complexity.
The analysis of algorithm means prediction of resources which includes memory, logic gates, time and etc.
Computational complexity is a characterization of the time and space requirements for solving a problem by an algorithm.
The algorithm mainly depends on performance analysis, measurement.
The analysis of the program requires two main considerations: Space Complexity and Time Complexity
The time complexities of an algorithm / program mean the amount of time that computer needs to run / execute the program.
The space complexities of an algorithm / program mean the amount of memory (space) requires by the computer to run /execute the program.
Time Complexity:
This means the amount of time requires by the computer to run any program / algorithm.
For any program, the exact time will depend on the implementation of an algorithm, language used to execute that program, capabilities of CPU and CPU speed, and other hardware specification.
To measure the time complexity accurately, we have to count all types of operations performed in an algorithm.
instance of variables (Dynamic Allocation). It also includes additional space required when the function uses recursion.
Except these components, a program also require data space, stack space, return address space and etc.
1.3 – Asymptotic Notation
When we study algorithm, we are interested in characterizing them according to their efficiency.
We are usually interesting in the order of growth of the running time of an algorithm, not an exact running time. This is also referred to as Asymptotic Running Time.
The Asymptotic Notation gives us a method for classifying functions according to their rate of growth.
The Asymptotic Notations are the “Big O”, “Big Omega”, “Big Theta” are used in Asymptotic Analysis.
1.4 – Classes of Algorithm
Any Algorithm can be classified into two categories:
By Implementation Way: This category is classified in:
Recursion / Iteration:
o In this the recursive algorithm invokes itself until a certain condition is true.
o And iterative algorithm invokes repeatedly (like loops).
Logical:
o This is used in the computation and used by the control component to determine the way (if condition).
o Any change in this may change well-defined algorithm.
Serial / Parallel / Distributed:
o A computer which can execute one instruction of an algorithm at a time is known as Serial Computers. And this type of algorithm is known as Serial Algorithm.
o Parallel algorithms take advantage of computer architectures where several processors can work on a problem at the same time.
o Distributed algorithms utilize multiple machines connected with a network.
o The parallel and distributed algorithms divide the problem into sub problems.
Deterministic or Non-Deterministic:
o Deterministic algorithms solve the problem with exact decision at every step of the algorithm.
o Non-Deterministic algorithm solves problems via guessing, although typical guesses are made more accurate through the use of heuristics.
o Exact or Approximate:
Many algorithms have exact solution, whereas some have approximate solution.
The approximation algorithm tries to find an approximation that is close to the true / final solution.
By Design Paradigm: Followings are different types of paradigms which are different from each other:
Brute-force or Exhaustive Search:
o This is the natural method for designing an algorithm of trying every possible solution to see which is best.
Divide and Conquer (Also known as Decrease and Conquer algorithm):
The worst case is when an algorithm requires a maximum number of iterations or steps to search and find out the target value in the array. That is the target value will be found at nth position of the array. (i.e. f(n) = n)
The best case is when the number of steps is as less as possible. That is, if the target value found at first position then only one iteration is executed. (i.e. f(n) = 1)
The average case falls between best and worst cases. If the target value is found at n / 2nd position, on an average we need to compare the target value with only half of the element in the array. (i.e. f(n) = n / 2)
For Example: For the quick sort the worst case complexity is O(n2) whereas for Bubble Sort the average case complexity is O(n2).
Thus, quick sort can be graded as the better than bubble sort.
Based on time complexity representation of the Big-O notation, the algorithm can be categorized as:
o Constant Time – O(1): That means the algorithm requires fixed number of steps regardless of the size of the task. For Example: Push and Pop operation for a stack.
o Linear Time – O(n): That means the algorithm requires the number of steps relative to the size of the task. For Example: Traversal of a list with n elements.
o Quadratic Time – O(n^2 ): The number of operations in relative to the size of the task squared. For Example: Comparing two 2D- Arrays.
o Logarithmic Time – O(log n): For Example: Binary Search Operations.
o O (n log n): For Example: Sorting algorithms like Quick and Merge.
Big O is asymptotic execution time of an algorithm.
In this, f(n) represents the computing time of some algorithm and g(n) represents a standard functions. That
means for BIG-O notation, it is also pronounced as f(n) is big-oh of g(n).
So, g(n) is an asymptotic upper bound for f(n). The equation is: |f(n)| ≤ c |g(n)|, for n ≥ n0.
As we know, the Big-Oh Notation provides an asymptotic way of saying that the function is “less than or equal to” another function; likewise the Big-Omega notation provides an asymptotic way to denote the function is “greater than or equal to” another function.
In this, f(n) represents the computing time of some algorithm and g(n) represents a standard functions. That means the Big-Omega notation (also pronounced as f(n)) is big-omega of g(n).
So, g(n) is an asymptotic lower bound for f(n). The equation is: |f(n)| ≥ c |g(n)|, for n ≥ n0.
This type of analysis is based on the idea that as the problem size grows, the complexity can be describe as a simply to some known function.
This is incorporated in the notations for asymptotic performance.
When the execution time of an algorithm varies and other factors which may differ from computer to computer then this type of analysis is used.
The absolute growth depends on the machine used to execute the program, the compiler used to construct the program, and many other factors.
We would like to have a way of describing the inherent complexity of a program (or piece of a program), independent of machine / compiler considerations.
This means that we must not try to describe the absolute time or storage needed.
We must instead concentrate on a “proportionality” approach, expressing the complexity in terms of its relationship to some known function. This type of analysis is known as asymptotic analysis.
o calloc(): Description: Allocate memory of array elements and initialize to zero, return address of first element. Calloc() is used to allocate memory for array / structure type variables. The default value for variable allocated by calloc() is 0 whereas by malloc() is garbage.
Syntax: void calloc(variable, size); Example: #include<stdio.h> #include<conio.h> #include<alloc.h> void main() { int i,p,q; clrscr(); p = (int )malloc(5sizeof(int)); q = (int )calloc(6,sizeof(int)); for(i=0;i<5;i++) printf("%d\t%u\n",p,p++); printf("\n\n"); for(i=0;i<5;i++) printf("%d\t%u\n",q,q++); getch(); }
o realloc(): Description: Allocate again previously allocated memory by malloc() or calloc(). Syntax: void *realloc(variable, newsize); Example: #include<stdio.h> #include<conio.h> #include<alloc.h> void main() { char *str; clrscr(); printf("Enter Name : "); str = (char *)malloc(sizeof(5)); gets(str); printf("Name is : %s",str); printf("\nEnter Name : "); str = (char *)realloc(str,sizeof(10)); gets(str); printf("Name is :%s",str); getch(); }
Output: 2162 1454
0 1468 0 1470 0 1472 0 1474 0 1476
Output: Enter Name : rpbc Name is : rpbc Enter Name : rpbc Rajkot rpbc rajkot
o free(): Description: Release allocates memory, allocated by malloc(), calloc() and realloc(). Syntax: free(pointer);
Dangling Pointer Problem
Dangling Pointer arise when an object is deleted or de-allocated, without
modifying the value of the pointer, so that the pointer still points to the memory location of the de-allocated memory.
In Short, Pointer pointing to non-existing memory location is called
Dangling Pointer Problem.
There are different ways where Pointer acts as Dangling Pointer.
void main() { char *p1 = malloc(value); …. …. free(p1); //p1 now becomes dangling pointer… } // Solution : write p1 = NULL after free();
void main() { char p1 = NULL; …. { char ch; p1 = &ch; } /Here p1 becomes dangling as it points to ch which is in inner block.*/ }
Enumerated Constants
Enumerated Constants can be defined with "enum" keyword.
enum is user defined data type for integral type of constants.
#include <stdio.h>
void main()
{
enum {RED=5, YELLOW, GREEN=4, BLUE};
printf("RED = %d\n", RED);
printf("YELLOW = %d\n", YELLOW);
printf("GREEN = %d\n", GREEN);
printf("BLUE = %d\n", BLUE);
}
#include<stdio.h>
enum week{Sun, Mon, Tue, Wed, Thu, Fri, Sat};
void main()
{
enum week day;
day = Mon;
printf("%d",day);
}
Adjacency Matrix and Adjacency List
Graph Traversal – BFS, DFS
Shortest Path Problem
Minimal Spanning Tree
INTRODUCTION:
A graph is non-linear data structure. It is a general tree with no parent-child relationship. This data structure is useful in many fields related to science / computer science. For Example: Graphs are used in mapping, games, puzzles, networks, engineering, transportation and etc. In general, graphs represent a relatively less relationship between the data items. It is a way of representing relationships that exists between pairs of objects. A graph G includes set of Vertices V (called nodes) and set of Edges (E). It is represented as Graph G = (V, E). The Vertices (or v) is a finite and non empty set and Edges (or E ) denotes pair of connected vertices. In other words, we can say that graph is a set of objects together with a collection of connected pair. The Vertices also referred to as nodes whereas the edges also referred to as arcs.
The single unit of Vertices is known as Vertex. For Ex: 1 in this graph.
In this graph G, we have set of Vertices V = {1,2,3,4,5} and set of Edges E = {(1,2), (1,3), (2,3), (3,4), (4,5)}.
The graph has very limited relationship between the vertices (nodes). As we can see there is no direct relationship between 1 and 4 although there are connected through 3.