Tuesday, March 26, 2024

Data Structure and Algorithm

Algorithms


An algorithm is a set of steps of operations to solve a problem performing calculation, data processing, and automated reasoning tasks. An algorithm is an efficient method that can be expressed within finite amount of time and space. An algorithm is the best way to represent the solution of a particular problem in a very simple and efficient way. If we have an algorithm for a specific problem, then we can implement it in any programming language, meaning that the algorithm is independent from any programming languages

The word Algorithm means “a process or set of rules to be followed in calculations or other problem-solving operations”. Therefore Algorithm refers to a set of rules/instructions that step-by-step define how a work is to be executed upon in order to get the expected results.


Properties of Algorithm 


All Algorithms must satisfy the following criteria -

 1) Input There are more quantities that are extremely supplied. 

2) Output At least one quantity is produced. 

3) Definiteness Each instruction of the algorithm should be clear and unambiguous.

 4) Finiteness The process should be terminated after a finite number of steps. 

5) Effectiveness Every instruction must be basic enough to be carried out theoretically or by using paper and pencil For example,suppose you are cooking a recipe and you chop vegetables which are not be used in the recipe then it is a waste of time

6)Independent An algorithm should have step-by-step directions, which should be independent of any programming code. It should be such that it could be run on any of the programming languages



What are the Characteristics of an Algorithm








Performance Analysis of Algorithm


i) Time Complexity 

ii) Space Complexity


Time Complexity

Time complexity is the amount of time taken by an algorithm to run, as a function of the length of the input. It measures the time taken to execute each statement of code in an algorithm
 
Space Complexity

When an algorithm is run on a computer, it necessitates a certain amount of memory space. The amount of memory used by a program to execute it is represented by its space complexity. Because a program requires memory to store input data and temporal values while running, the space complexity is auxiliary and input space.


Analyzing Algorithm( Analysis of Algorithm)

To analyze a programming code or algorithm, we must notice that each instruction affects the overall performance of the algorithm and therefore, each instruction must be analyzed separately to analyze overall performance. However, there are some algorithm control structures which are present in each programming code and have a specific asymptotic


Some Algorithm Control Structures are: 

1. Sequencing

 2.If-then-else

 3.for loop 

4.While loop


Asymptotic Notation of Algorithm (Growth Functions)


Growth functions are used to estimate the number of steps an algorithm uses as its input grows

Asymptotic Notation is used to describe the running time of an algorithm - how much time an algorithm takes with a given input, n. Asymptotic Notation is a way of comparing function that ignores constant factors and small input sizes. 

  • It is a way to describe the characteristics of a function in the limit.
  • It describes the rate of growth of functions.
  • Focus on what’s important by abstracting away low-order terms and constant factors.
  • It is a way to compare “sizes” of functions:
  • O≈ ≤ Ω≈ ≥ Θ≈= o≈< ω≈> 


Why is Asymptotic Notation Important? 

1. They give simple characteristics of an algorithm's efficiency. 

2. They allow the comparisons of the performances of various algorithms. 


growth function

Three notations are used to calculate the running time complexity of an algorithm: There are three different notations: 

big O,

big Theta (Θ), 

big Omega (Ω)





1) Big-O Notation

 The Big-O notation describes the worst-case running time of a program. We compute the Big-O of an algorithm by counting how many iterations an algorithm will take in the worst-case scenario with an input of N. We typically consult the Big-O because we must always plan for the worst case.

 For example, O(log n) describes the Big-O of a binary search algorithm. 



2) Big-Ω Notation 

Big-Ω (Omega) describes the best running time of a program. We compute the big-Ω by counting how many iterations an algorithm will take in the best-case scenario based on an input of N. For example, a Bubble Sort algorithm has a running time of Ω(N) because in the best case scenario the list is already sorted, and the bubble sort will terminate after the first iteration. 





3. Theta (θ) Notation

The function f (n) = θ (g (n)) [read as "f is the theta of g of n"] if and only if there exists positive constant k1, k2 and k0 such that k1 * g (n) ≤ f(n)≤ k2 g(n)for all n, n≥ n0




Explanation

Recurrence relation

A recurrence is an equation or inequality that describes a function in terms of its values on smaller inputs. To solve a Recurrence Relation means to obtain a function defined on the natural numbers that satisfy the recurrence.

There are four methods for solving Recurrence: 

1. Substitution Method

2. Iteration Method

3. Recursion Tree Method 

4. Master Method 


Masters theorem

The master theorem is used in calculating the time complexity of recurrence relations (divide and conquer) in a simple and quick way.









https://www.scaler.com/topics/data-structures/master-theorem/

Analysis of sorting Algorithms



Time Complexity 
BestO(n+k)
WorstO(n+k)
AverageO(n+k)
Space ComplexityO(max)
StabilityYe
O(n*k)
Since radix sort is a non-comparative algorithm, it has advantages over comparative sorting algorithms.


Time Complexity 
BestO(nlog n)
WorstO(nlog n)
AverageO(nlog n)
Space ComplexityO(1)
StabilityNo





Time Complexity 
BestO(n*log n)
WorstO(n*log n)
AverageO(n*log n)
Space ComplexityO(n)
StabilityYes


Time Complexity 
BestO(n*log n)
WorstO(n2)
AverageO(n*log n)
Space ComplexityO(log n)
StabilityNo


Advance Data Structure


Red-Black Trees: 
B Tree
B+ Tree
 

https://www.youtube.com/watch?v=YUtUNlLNB5c

https://www.youtube.com/watch?v=BwUvgG29fPc

Graph Algorithm




Advance Design and Analysis Techniques


Dynamic Programming

Dynamic programming is a programming technique where an algorithmic problem is broken down into subproblems

Dynamic programming is defined as a computer programming technique where an algorithmic problem is first broken down into sub-problems, the results are saved, and then the sub-problems are optimized to find the overall solution — which usually has to do with finding the maximum and minimum range of the algorithmic query.




Greedy Algorithm


Branch and Bound Algorithm


https://www.youtube.com/watch?v=XZbrmetb9VE

Backtracking algorithm



Parallel Algorithm


Performance Measures of Parallel Algorithms

A parallel algorithm can be executed simultaneously on many different processing devices and then combined together to get the correct result. Parallel algorithms are highly useful in processing huge volumes of data in quick time. This tutorial provides an introduction to the design and analysis of parallel algorithms. In addition, it explains the models followed in parallel algorithms, their structures, and implementation.



Parallel Merging/Sorting Algorithms on CREW/EREW [Sorting in parallel]


Why do we need parallel sorting?

The process of sorting a list of elements is fairly frequent. When we need to sort a sizable amount of data, a sequential sorting method could not be effective enough. Therefore, parallel algorithms are used in sorting.

What is the potential speed-up with parallel sorting?

If we talk about sequential sorting, its best complexity turns out to be O(nlogn). Using parallel sorting, if we have n processors, its optimal complexity turns out to be O(nlogn)/n = O(log n).


Parallel searching algorithms




PRAM





NP Complete Problem 



A decision problem  is NP-complete if:

  1.  is in NP, and
  2. Every problem in NP is reducible to  in polynomial time.

 can be shown to be in NP by demonstrating that a candidate solution to  can be verified in polynomial time.
























2 comments:

  1. Above doc has all topic related to data structure and analysis of algo

    ReplyDelete
  2. https://er.yuvayana.org/performance-analysis-of-parallel-algorithms/

    ReplyDelete

Interview Prep: Java full stack

 SOLID principle :  https://www.educative.io/answers/what-are-the-solid-principles-in-java Design Pattern:  Creational:  https://medium.com...