BCAC 0026:
ANALYSIS OFALGORITHM
AND DATA STRUCTURES
Faculty Name
Dr. Vinod Jain, Assistant Professor, CEA
Mob : 9813078438, Email – vinod.jain@gla.ac.in
Seating : 329-B, AB-X, Third Floor
Course Outcome
• Astudent who successfully completes the course will have the ability to:
• CO1: Understand that various problem solving categories exist such as; iterative
technique, divide and conquer, dynamic programming, greedy algorithms, and
understand various searching and sorting algorithms.
• CO2: Employ a deep knowledge of various data structures when constructing a
program.
• CO3: Design and construct simple object-oriented software with an appreciation
for data abstraction and information hiding.
• CO4: Effectively use software development tools including libraries, compilers,
editors, linkers and debuggers to write and troubleshoot programs.
5.
Books
• Text Book:
•Cormen T.H., Leiserson Charles E., Rivest Ronald L., Stein Clifford, Introduction to
Algorithms, PHI Learning Pvt. Ltd., 2009, 3rd Edition.
• Reference Books:
• Basse Sara & A.V. Gelder, Computer Algorithm: Introduction to Design and
Analysis, Pearson, 2000, 3rd Edition.
• Drozdek Adam, "Data Structures and algorithm in C++", Cengage Learning, 2012,
Third Edition.
• Tenenbaum Aaron M., Augenstein Moshe J., Langsam Yedidyah, "Data Structures
Using C and C++, PHI, 2009, Second edition.
• Kruse Robert L., "Data Structures and Program Design in C++", Pearson.
6.
digital platforms
• Suggestivedigital platforms web links or online course
• https://www.oercommons.org/authoring/14873-data-structure/view
• https://www.oercommons.org/courses/data-structure-and-
algorithms
• https://onlinecourses.swayam2.ac.in/cec19_cs04/preview (online
course)
7.
Introduction to Algorithm?
•Definition of Algorithm
• The word Algorithm means ” A set of finite rules or instructions to be
followed in calculations or other problem-solving operations ”
•
Or
•
” A procedure for solving a mathematical problem in a finite number
of steps that frequently involves recursive operations”.
10.
Introduction to Algorithm?
•What is the need for algorithms?
• Algorithms are necessary for solving complex problems efficiently and
effectively.
• They help to automate processes and make them more reliable, faster, and
easier to perform.
• Algorithms also enable computers to perform tasks that would be difficult
or impossible for humans to do manually.
• They are used in various fields such as mathematics, computer science,
engineering, finance, and many others to optimize processes, analyze data,
make predictions, and provide solutions to problems.
11.
Introduction to Algorithm?
•What are the Characteristics of an Algorithm?
• For some instructions to be an algorithm, it must have the following
characteristics:
Characteristics of anAlgorithm?
1. Clear and Unambiguous: The algorithm should be unambiguous. Each of its steps
should be clear in all aspects and must lead to only one meaning.
2. Well-Defined Inputs: If an algorithm says to take inputs, it should be well-defined
inputs. It may or may not take input.
3. Well-Defined Outputs: The algorithm must clearly define what output will be
yielded and it should be well-defined as well. It should produce at least 1 output.
4. Finite-ness: The algorithm must be finite, i.e. it should terminate after a finite
time.
5. Feasible: The algorithm must be simple, generic, and practical, such that it can be
executed with the available resources. It must not contain some future
technology or anything.
15.
Characteristics of anAlgorithm?
6. Language Independent: The Algorithm designed must be language-
independent, i.e. it must be just plain instructions that can be implemented
in any language, and yet the output will be the same, as expected.
7. Input: An algorithm has zero or more inputs.
8. Output: An algorithm produces at least one output.
9. Definiteness: All instructions in an algorithm must be unambiguous, precise,
and easy to interpret. By referring to any of the instructions in an algorithm
one can clearly understand what is to be done.
10. Effectiveness: An algorithm must be developed by using very basic, simple,
and feasible operations so that one can trace it out by using just paper and
pencil.
16.
Introduction to Algorithm?
•Properties of Algorithm:
1. It should terminate after a finite time.
2. It should produce at least one output.
3. It should take zero or more input.
4. It should be deterministic means giving the same output for the
same input case.
5. Every step in the algorithm must be effective i.e. every step should
do some work.
17.
Introduction to Algorithm?
•Advantages of Algorithms:
• It is easy to understand.
• An algorithm is a step-wise representation of a solution to a given problem.
• In an Algorithm the problem is broken down into smaller pieces or steps
hence, it is easier for the programmer to convert it into an actual program.
• Disadvantages of Algorithms:
• Writing an algorithm takes a long time so it is time-consuming.
• Understanding complex logic through algorithms can be very difficult.
• Branching and Looping statements are difficult to show in Algorithms(imp).
18.
Introduction to Algorithm?
•How to Design an Algorithm?
• To write an algorithm, the following things are needed as a pre-requisite:
1. The problem that is to be solved by this algorithm i.e. clear problem definition.
2. The constraints of the problem must be considered while solving the problem.
3. The input to be taken to solve the problem.
4. The output is to be expected when the problem is solved.
5. The solution to this problem is within the given constraints.
6. Then the algorithm is written with the help of the above parameters such that it
solves the problem.
Analysis techniques ofAlgorithms:
Time and space complexity
• How to analyze an Algorithm?
• For a standard algorithm to be good, it must be efficient.
• Hence the efficiency of an algorithm must be checked and
maintained.
22.
What is Algorithmcomplexity and
how to find it?
• Hence the Complexity of an algorithm refers to the measure of the
time that it will need to execute and get the expected output, and the
Space it will need to store all the data (input, temporary data, and
output).
• Hence these two factors define the efficiency of an algorithm.
23.
Analysis techniques ofAlgorithms:
Time and space complexity
• The two factors of Algorithm Complexity are:
• Time Factor: Time is measured by counting the number of key
operations such as comparisons in the sorting algorithm.
• Space Factor: Space is measured by counting the maximum memory
space required by the algorithm to run/execute.
24.
Space Complexity
• Thespace complexity of an algorithm refers to the amount of
memory required by the algorithm to store the variables and get the
result.
• This can be for inputs, temporary operations, or outputs.
25.
Space Complexity
• Thespace complexity of an algorithm is calculated by determining the
following 2 components:
• Fixed Part:
• Variable Part:
26.
Space Complexity
• FixedPart:
• This refers to the space that is required by the algorithm.
• For example, input variables, output variables, program size, etc.
27.
Space Complexity
• VariablePart:
• This refers to the space that can be different based on the
implementation of the algorithm.
• For example, temporary variables, dynamic memory allocation,
recursion stack space, etc.
•
Therefore Space complexity S(P) of any algorithm P is S(P) = C + SP(I),
where C is the fixed part and S(I) is the variable part of the algorithm,
which depends on instance characteristic I.
28.
Space Complexity
• Example:Consider the below algorithm for Linear Search
• Step 1: START
Step 2: Get n elements of the array in arr and the number to be
searched in x
Step 3: Start from the leftmost element of arr[] and one by one
compare x with each element of arr[]
Step 4: If x matches with an element, Print True.
Step 5: If x doesn’t match with any of the elements, Print False.
Step 6: END
29.
Space Complexity
• Here,There are 2 variables arr[], and x, where the arr[] is the variable
part of n elements and x is the fixed part.
• Hence S(P) = 1+n. So, the space complexity depends on n(number of
elements).
• Now, space depends on data types of given variables and constant
types and it will be multiplied accordingly.
30.
Time Complexity
• TimeComplexity:
• The time complexity of an algorithm refers to the amount of time
required by the algorithm to execute and get the result.
• This can be for normal operations, conditional if-else statements, loop
statements, etc.
31.
Time Complexity
• Howto Calculate, Time Complexity?
The time complexity of an algorithm is also calculated by determining
the following 2 components:
• Constant time part
• Variable Time Part
32.
Time Complexity
• Constanttime part:
• Any instruction that is executed just once comes in this part.
• For example, input, output, if-else, switch, arithmetic operations, etc.
• Variable Time Part:
• Any instruction that is executed more than once, say n times, comes
in this part.
• For example, loops, recursion, etc.
33.
Time Complexity
• ThereforeTime complexity T(P)
• T(P) of any algorithm P is T(P) = C + TP(I),
• where C is the constant time part and TP(I) is the variable part of the
algorithm, which depends on the instance characteristic I.
Time Complexity
• Best,Worst, and Average Case Complexity:
• In analyzing algorithms, we consider three types of time complexity:
• Best-case complexity (O(best)): This represents the minimum time
required for an algorithm to complete when given the optimal input.
• It denotes an algorithm operating at its peak efficiency under ideal
circumstances.
• Worst-case complexity (O(worst)): This denotes the maximum time an
algorithm will take to finish for any given input.
• It represents the scenario where the algorithm encounters the most
unfavourable input.
36.
Time Complexity
• Average-casecomplexity (O(average)):
• This estimates the typical running time of an algorithm when
averaged over all possible inputs.
• It provides a more realistic evaluation of an algorithm's performance.
37.
Time Complexity
• BigO Notation
• In computer science, Big O notation is used more specifically to find
the worst case time complexity for an algorithm.
• Big O notation uses a capital letter O with parenthesis(), and inside
the parenthesis there is an expression that indicates the algorithm
runtime.
• Runtime is usually expressed using n, which is the number of values in
the data set the algorithm is working on.
38.
Here is howtime increases when the number of
values n increase for different algorithms:
42.
Time Complexity
• Conclusion:
•In conclusion, understanding time complexity in data structures is
crucial for analyzing and evaluating the efficiency and performance of
algorithms.
• Time complexity is a measure of how long an algorithm takes to run
as the size of the input increases.
• It provides valuable insights into how an algorithm scales and helps in
making informed decisions about algorithm selection and
optimization.