Nice well experienced teacher notes, Lecture notes of Data Structures and Algorithms

Nice well experienced teacher notes

Typology: Lecture notes

2024/2025

Uploaded on 08/24/2025

akshita-gaur
akshita-gaur 🇮🇳

1 document

1 / 16

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Data Structures & Algorithms(3CS4-
03)
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Nice well experienced teacher notes and more Lecture notes Data Structures and Algorithms in PDF only on Docsity!

Data Structures & Algorithms(3CS4-

Introduction to Algorithm &

Analysis

(Unit-1)

In computer programming data structure designed to
store data for the purpose of working on it with various
algorithms.
Data Structure contains information about the data values,
relationship between the data and functions that can be
applied to the data.

Importance of Algorithms in placement:

  • (^) DSA is important for placement, it is used to evaluate a

technical skills and ability to solve complex problems.

  • (^) DSA develop problem-solving and critical-thinking skills.

These skills are highly valued in the industry

  • (^) Good knowledge of DSA is able to write efficient and

optimized code.

The choice of an algorithm depends upon analysis:

  1. Time complexity
  2. Space complexity A good algorithm is one that is taking less time and less space , but this is not possible all the time. There is trade-off between space and time. If we want to reduce the time then space might increase. Similarly if we want to if we want to reduce the space then time may increase. Space complexity: The total amount of memory required by an algorithm to complete its execution is called space complexity. For any algorithm memory is required for following purposes:
  • (^) Memory to store program instrucitons
  • (^) Memory to store constants
  • (^) Memory to store variable values To perform analysis of algorithm based on its space complexity , we can consider only data space and ignore constants , variables.

e.g. int func(int a) { retur n a; } 2-bytes to store variable 2-bytes required to return value 4-bytes memory is required to complete execution. This 4-byte of memory is fixed for any input value of a. So space complexity is said to be constant (i.e. O(1) ) Another e.g. int sum(int a[ ],int n) { int s=0,i; for(i=0;i<n;i++) s+=a[i];

  • (^) return s; }

Suppose a loop will run n number of times, so the time complexity will be n atleast and the value of n will increase the time taken will also increase. Since the algorithm performance may vary with different types of input data hence we use worst- case time complexity of an algorithm because that is the maximum time taken for any input size. Calculating Time complexity : The most common metric for calculating time complexity is Big-O notation. This removes all constant factors , so that the running time can be estimated in relation to n, as n approaches infinity. e.g. Statement; only single statement , its time complexity will be constant. The running time of the statement will not change in relation to n. e.g. for(i=0;i<n;i++) { statement; } The time complexity will be linear. The running time of loop is directly proportional to n. Asymptotic notation: Asymptotic notations are the mathematical notations used to describe the running time of an algorithm. To analyze time complexity of an algorithm , we rely on notations. There are three asymptotic notations:

  • Big-O natation
  • (^) Omega notation

Big-O notation: It is used to define the upper bound of an algorithm in terms of time complexity. It always indicates the maximum time required by an algorithm for all input values. So Big-O describes the worst case of an algorithm time complexity. It is represented as shown below: If f(n) and g(n) are two functions defined as positive integers, then f(n) = O(g(n)) as f(n) is big-O of g(n) or f(n) is on the order of g(n) if there exist constant C such that f(n)<=C.g(n). This implies that f(n) does not grow faster than g(n) or g(n) is the upper bound on the function f(n). We are calculating the growth rate of the function which eventually calculates the worst time complexity of a function i.e. how worst an algorithm can perform. e.g. f(n)=2n+3, g(n)=n we have to find f(n)=O(g(n)) It must satisfy following condition f(n)<=C.g(n) So replace f(n) with 2n+3 and g(n) with n 1). 2n+3 <=C.n lets assume C=5 and n= 21+3<=51 for n=1 condition is true C.g(n ) f(n) k

e.g. If f(n)=2n+3 and g(n)=n Is f(n)= Ωg(n)? It must satisfy the condition f(n)>=C.g(n) 2n+3>=C.n Suppose C=1 then 2n+3 >=n (this is true for any value of n starting from 1) therefor it is proved that g(n) is big omega of 2n+3 function. g(n) function is the lower bound of f(n), when C=1. So the notation fastest running time. Theta Notation (θ): The notation describes the average case scenario. It represents the realistic time complexity of an algorithm. Every time an algorithm does not perform worst or best , algorithms mainly fluctuate between worst and best case and this gives average case of an algorithm. It is a formal way to express both the upper and lower bound of an algorithm running time. If f(n) and g(n) are two functions defined as positive integers, then f(n)= θ (g(n)). The condition is satisfied only if C1.g(n) <=f(n) <= C2.g(n) The function is bounded by two limits i.e. upper and lower limit and f(n) comes in between C1.g( n) f(n) Time Input C2.g( n)

Commonly used functions:

1. Constant function f(n)=1: whatever the size of input , these function takes constant time. 2. Linear function f(n)=n: These functions grow linearly with input size n. 3. Quadratic function f(n)=n^2 : grow faster f(n)=n^3. (non linear complexity) 4. Logarithm function f(n)=log(n): These functions are slower growing than linear (It reduces the size of input data in each step, this indicates that the number of operations is not the same as input size e.g. Binary search). Time required by an algorithm falls under three cases: 5. Best case : It is the minimum time required for program execution. We calculate the lower bound on running time of an algorithm. It bounds the function below i.e. we must know the cases causes minimum number of operations to be executed. e.g. In linear search best case occurs when element found at first position , so time complexity is O(1). The number of operations are fixed. Linear Search: for(i=0;i<n;i++) { if(a[i]==item) return a[i]; } 2. Worst-case analysis : It is the maximum time required for program execution. We calculate the upper bound on running time of an algorithm. It bounds the function from above. We must know the case that causes a

Type of Data
Structure

Primitive Data Structure Non-Primitive Data Structure

  • (^) Integer
  • (^) Float
  • (^) Charact er Linear Data structure Non Linear Data structure
  • (^) Array
  • (^) Stack
  • (^) Queue
  • (^) Linked list
  • (^) Tree
  • (^) Graph
  • (^) Hash Table Linear Data structure : In this elements are stored in contiguous memory locations or are arranged sequentially. It can be constructed by using an array. The adjacency relationship is maintained between elements. e.g. Array, Stack, Queue, Linked List Non Linear Data structure : In this the elements are not arranged sequentially or linearly. The adjacency relationship is not maintained between data elements. They