The document provides an overview of algorithms, detailing their definition, properties, and characteristics. It emphasizes the importance of algorithm analysis, including time and space complexity, to predict performance and resource requirements. Additionally, it includes examples and questions to test understanding of these concepts.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
5 views24 pages
Session1 - Introduction To Algortihm and Analysis
The document provides an overview of algorithms, detailing their definition, properties, and characteristics. It emphasizes the importance of algorithm analysis, including time and space complexity, to predict performance and resource requirements. Additionally, it includes examples and questions to test understanding of these concepts.
Algorithm? • An algorithm is a finite sequence of well-defined instructions that can be used to solve a computational problem. It provides a step-by-step procedure that convert an input into a desired output. • Algorithms typically follow a logical structure: • Input: The algorithm receives input data.
• Processing: The algorithm performs a series of operations on the input
data.
• Output: The algorithm produces the desired output.
Dr. Vandna Batra
Properties of Algorithm • Finiteness: An algorithm must always terminate after a finite number of steps. • Definiteness: Each step of an algorithm must be precisely defined; the actions to be carried out must be rigorously and unambiguously specified for each case. • Input: An algorithm has zero or more inputs, i.e, quantities which are given to it initially before the algorithm begins. • Output. An algorithm has one or more outputs i.e, quantities which have a specified relation to the inputs. • Effectiveness. An algorithm is also generally expected to be effective. This means that all of the operations to be performed in the algorithm must be sufficiently basic that they can in principle be done exactly and in a finite length of time. Dr. Vandna Batra Characteristics of an Algorithm • Unambiguous − Algorithm should be clear and unambiguous. Each of its steps (or phases), and their inputs/outputs should be clear and must lead to only one meaning. • Input − An algorithm should have 0 or more well-defined inputs. • Output − An algorithm should have 1 or more well-defined outputs, and should match the desired output. • Finiteness − Algorithms must terminate after a finite number of steps. • Feasibility − Should be feasible with the available resources. • Independent − An algorithm should have step-by-step directions, which should be independent of any programming code.
Dr. Vandna Batra
Analysis of an Algorithm • Algorithm analysis is an important part of computational complexity theory. • It provides theoretical estimation for the required resources of an algorithm to solve a specific computational problem. • Analysis of algorithms is the determination of the amount of time and space resources required to execute it.
Dr. Vandna Batra
Importance for Analysis of an Algorithm • To predict the behavior of an algorithm for large inputs.
• It is much more convenient to have simple measures for the efficiency
of an algorithm than to implement the algorithm and test the efficiency every time a certain parameter in the underlying computer system changes.
• More importantly, by analyzing different algorithms, we can compare
them to determine the best one for our purpose.
Dr. Vandna Batra
Types of Complexity/Analysis of Algorithms 1. Time Complexity Running time of the program as a function of the size of input 2. Space Complexity Amount of computer memory required during the program execution, as a function of the input size.
Dr. Vandna Batra
Time Complexity • Time T(P) taken by a program is the sum of the Compile Time and Run(or Execution) time. • Program once compiled can be run several times. • Compile time doesn’t depend on instance characteristics. • Time complexity measures the amount of time an algorithm takes to complete as a function of the input size n. • It reflects the growth rate of the number of operations as input size increases.
Dr. Vandna Batra
Time Complexity • Example 1: Addition of two scalar variables. Algorithm ADD SCALAR(A, B) //Description: Perform arithmetic addition of two numbers Input: Two scalar variables A and B //Output: variable C, which holds the addition of A and B C <- A + B return C
Dr. Vandna Batra
Time Complexity • The addition of two scalar numbers requires one addition operation. • The time complexity of this algorithm is constant, so • T(n) = O(1) .
Dr. Vandna Batra
Types of Time Complexity
1.Best Case: The minimum time required for an algorithm to complete
(optimistic scenario). 2.Worst Case: The maximum time required (pessimistic scenario). 3.Average Case: The expected time for a random input of size n.
Dr. Vandna Batra
Space Complexity • To estimate the memory requirement we need to focus on two parts: • (1) A fixed part: It is independent of the input size. It includes memory for instructions (code), constants, variables, etc. • (2) A variable part: It is dependent on the input size. It includes memory for recursion stack, referenced variables, etc.
Dr. Vandna Batra
Space Complexity • Example 1: Addition of two scalar variables. Algorithm ADD SCALAR(A, B) //Description: Perform arithmetic addition of two numbers Input: Two scalar variables A and B //Output: variable C, which holds the addition of A and B C <- A + B return C
Dr. Vandna Batra
Space Complexity • The addition of two scalar numbers requires one extra memory location to hold the result. • Thus the space complexity of this algorithm is constant, hence • S(n) = O(1).
Dr. Vandna Batra
Space Complexity • The addition of two scalar numbers requires one extra memory location to hold the result. • Thus the space complexity of this algorithm is constant, hence • S(n) = O(1).
Dr. Vandna Batra
Test Your Knowledge • Which of the following best defines an algorithm? • (A) A set of rules for solving a problem in a finite amount of time. • (B) A computer program written in a high-level language. • (C) A flowchart describing a problem. • (D) A mathematical formula.
Dr. Vandna Batra
Test Your Knowledge 2. What is the purpose of algorithm analysis? (A) To verify the correctness of the algorithm. (B) To optimize the algorithm's performance in terms of time and space. (C) To convert the algorithm into code. (D) To document the algorithm in detail.
3. What is the main focus of time complexity analysis?
(A) The amount of memory used by the algorithm. (B) The growth of execution time with input size. (C) The correctness of the algorithm. (D) The structure of the algorithm.
Dr. Vandna Batra
Test Your Knowledge 4. What does space complexity NOT include? (A) Memory required for input data. (B) Memory for auxiliary variables. (C) CPU usage during execution. (D) Memory for recursive stack calls.
5. The growth rate of an algorithm's execution time with respect to
input size is called its ___ complexity.
Dr. Vandna Batra
Test Your Knowledge 6. What is time complexity of fun()? int fun(int n) { int count = 0; for (int i = n; i > 0; i /= 2) for (int j = 0; j < i; j++) count += 1; return count; } Dr. Vandna Batra Test Your Knowledge 7. In a competition, four different functions are observed. All the functions use a single for loop and within the for loop, same set of statements are executed. Consider the following for loops: A) for(i = 0; i < n; i++) B) for(i = 0; i < n; i += 2) C) for(i = 1; i < n; i *= 2) D) for(i = n; i <= n; i /= 2) If n is the size of input(positive), which function is most efficient(if the task to be performed is not an issue)? A, B, C or D Dr. Vandna Batra Recap In this lecture we have discussed: • Algorithm definition • Properties of an algorithm • Characteristics of an algorithm • Analysis of algorithm • Time and Space complexity