Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13
Data Structure & Algorithms
Chapter 2: Preliminaries(Complexity of Algorithm)
IT Department Complexity of Algorithms • The analysis of algorithm is major task in computer programming.
• In order to compare algorithms, we must have
some criteria to measure the efficiency of our algorithms. Complexity of Algorithms • There are problems and algorithms to solve them. • Problems and problem instances. • Example: Sorting data in ascending order. – Problem: Sorting – Problem Instance: e.g. sorting data (2 3 9 5 6 8) – Algorithms: Bubble sort, Merge sort, Quick sort, Selection sort, etc. • Which is the best algorithm for the problem? How do we judge? Complexity of Algorithms • Two criteria are used to judge algorithms: (i) time complexity (ii) space complexity. • Space Complexity of an algorithm is the amount of memory it needs to run to completion. • Time Complexity of an algorithm is the amount of CPU time it needs to run to completion. 2.4 Complexity of Algorithms • The time is measured by counting the number of key operations. • The space is measured by counting the maximum of memory needed by the algorithm.
• The complexity of an Algorithm M is the
function f(n) which gives the running time and/or storage space requirement of the algorithm in terms of the size n of the input data. Time Complexity • A measure of the amount of time required to execute an algorithm. • Time complexity expresses the relationship between the size of the input and the run time for the algorithm • When comparing two algorithms that perform the same task, we often just concentrate on the differences between algorithms Time Complexity • Simplified analysis can be based on: – Number of arithmetic operations performed – Number of comparisons made – Number of times through a critical loop – Number of array elements accessed – etc Time Complexity • Suppose that exponentiation is carried out using multiplications. Two ways to evaluate the polynomial • p(x) = 4x4 + 7x3 – 2x2 + 3x1 + 6 • are: Brute force method: • p(x) = 4*x*x*x*x + 7*x*x*x – 2*x*x + 3*x + 6 Horner’s method: • p(x) = (((4*x + 7) * x – 2) * x + 3) * x + 6