Time Complexity and the divide and conquer strategy

Slides:



Advertisements
Similar presentations
Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
Advertisements

CS420 lecture one Problems, algorithms, decidability, tractability.
Introduction to Analysis of Algorithms
Scott Grissom, copyright 2004 Chapter 5 Slide 1 Analysis of Algorithms (Ch 5) Chapter 5 focuses on: algorithm analysis searching algorithms sorting algorithms.
2 -1 Chapter 2 The Complexity of Algorithms and the Lower Bounds of Problems.
The Complexity of Algorithms and the Lower Bounds of Problems
Summary of Algo Analysis / Slide 1 Algorithm complexity * Bounds are for the algorithms, rather than programs n programs are just implementations of an.
CS107 Introduction to Computer Science Lecture 7, 8 An Introduction to Algorithms: Efficiency of algorithms.
Analysis of Algorithms COMP171 Fall Analysis of Algorithms / Slide 2 Introduction * What is Algorithm? n a clearly specified set of simple instructions.
Lecture 2 We have given O(n 3 ), O(n 2 ), O(nlogn) algorithms for the max sub-range problem. This time, a linear time algorithm! The idea is as follows:
Time Complexity Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
Lecture 2 Computational Complexity
Mathematics Review and Asymptotic Notation
CSC 201 Analysis and Design of Algorithms Lecture 04: CSC 201 Analysis and Design of Algorithms Lecture 04: Time complexity analysis in form of Big-Oh.
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “ Introduction to the Design & Analysis of Algorithms, ” 2 nd ed., Ch. 1 Chapter.
Analysis of Algorithms
Project 2 due … Project 2 due … Project 2 Project 2.
The Selection Problem. 2 Median and Order Statistics In this section, we will study algorithms for finding the i th smallest element in a set of n elements.
Télécom 2A – Algo Complexity (1) Time Complexity and the divide and conquer strategy Or : how to measure algorithm run-time And : design efficient algorithms.
Introduction to Analysis of Algorithms COMP171 Fall 2005.
CMSC 341 Asymptotic Analysis. 2 Complexity How many resources will it take to solve a problem of a given size? –time –space Expressed as a function of.
Algorithm Analysis Part of slides are borrowed from UST.
Algorithm Analysis. What is an algorithm ? A clearly specifiable set of instructions –to solve a problem Given a problem –decide that the algorithm is.
E.G.M. PetrakisAlgorithm Analysis1  Algorithms that are equally correct can vary in their utilization of computational resources  time and memory  a.
Young CS 331 D&A of Algo. Topic: Divide and Conquer1 Divide-and-Conquer General idea: Divide a problem into subprograms of the same kind; solve subprograms.
Lecture # 6 1 Advance Analysis of Algorithms. Divide-and-Conquer Divide the problem into a number of subproblems Similar sub-problems of smaller size.
BITS Pilani Pilani Campus Data Structure and Algorithms Design Dr. Maheswari Karthikeyan Lecture1.
Algorithm Design Techniques, Greedy Method – Knapsack Problem, Job Sequencing, Divide and Conquer Method – Quick Sort, Finding Maximum and Minimum, Dynamic.
Algorithm Analysis 1.
CS1022 Computer Programming & Principles
CMPT 438 Algorithms.
The NP class. NP-completeness
Theoretical analysis of time efficiency
Analysis of Algorithms
Analysis of Algorithms
Analysis of Algorithms
UNIT- I Problem solving and Algorithmic Analysis
Introduction to complexity
Analysis of Algorithms
Introduction Algorithms Order Analysis of Algorithm
CS 3343: Analysis of Algorithms
Chapter 4: Divide and Conquer
Quicksort
Chapter 4 Divide-and-Conquer
Lecture 22 Complexity and Reductions
Growth of functions CSC317.
Courtsey & Copyright: DESIGN AND ANALYSIS OF ALGORITHMS Courtsey & Copyright:
Algorithm Analysis CSE 2011 Winter September 2018.
Algorithm Analysis (not included in any exams!)
Algorithm design and Analysis
The Complexity of Algorithms and the Lower Bounds of Problems
Objective of This Course
Data Structures Review Session
Topic: Divide and Conquer
CS 201 Fundamental Structures of Computer Science
Analysis of Algorithms
Design and Analysis of Algorithms (DAA) (6 cp)
Algorithm Efficiency: Searching and Sorting Algorithms
Sub-Quadratic Sorting Algorithms
Quicksort.
Algorithm Efficiency and Sorting
8. Comparison of Algorithms
At the end of this session, learner will be able to:
David Kauchak cs161 Summer 2009
Topic: Divide and Conquer
The Selection Problem.
Analysis of Algorithms
Quicksort.
Algorithms and data structures: basic definitions
Lecture 22 Complexity and Reductions
Presentation transcript:

Time Complexity and the divide and conquer strategy Or : how to measure algorithm run-time And : design efficient algorithms Oct. 2005 Télécom 2A – Algo Complexity

Télécom 2A – Algo Complexity Contents Initial considerations Complexity of an algorithm About complexity and order of magnitude The “divide and conquer” strategy and its complexity A word about “greedy algorithms” Télécom 2A – Algo Complexity

Basic preliminary considerations We are interested by the asymptotic time complexity T(n) with n being the size of the input order of magnitude : O(f(n))  A,  α n>A g(n)< α f(n) => g is said to be O(f(n)) Examples : n2 is O(n3) (why?), 1000n + 1010 is O(n) Télécom 2A – Algo Complexity

Understanding order of magnitude If 1000 steps/sec, how large can a problem be in order to be solved in : Time complexity 1 sec 1 min 1 day log2 n 21000  n 1000 60 000 8,6 . 107 n logn 140 4893 5,6 . 105 n² 31 244 9300 n3 10 39 442 2n 15 26 Télécom 2A – Algo Complexity

Is it worth to improve the code? If moving from n² to n.logn, definitively If your step is running 10 times faster, For the same problem, 10 time faster! For the same time how larger might the data be: Linear : 10 time larger n.logn : almost 10 time larger n² : 3 time larger 2n : initial size + 3.3 ◄Forget about Télécom 2A – Algo Complexity

Complexity of an algorithm Depends on the data : If an array is already sorted, some sorting algorithms have a behavior in O(n), Default definition : complexity is the complexity in the worst case Alternative : Complexity in the best case (no interest) Complexity on the average : Requires to define the distribution of the data. Télécom 2A – Algo Complexity

Complexity of a problem The complexity of the best algorithm for providing the solution Often the complexity is linear: you need to input the data; Not always the case : the dichotomy search is in O(n logn) if the data are already in memory Make sense only if the problem can be solved : Unsolvable problem : for instance: deciding if a program will stop (linked to what is mathematically undecidable) Solvable problem: for instance: deciding if the maximum of a list of number is positive; complexity O(n) Télécom 2A – Algo Complexity

Télécom 2A – Algo Complexity Complexity of sorting Finding the space of solutions : one of the permutations that will provide the result sorted : size of the space : n! How to limit the search solution Each answer to a test on the data specifies a subset of possible solutions In the best case, the set of possible solution in cut into 2 half Télécom 2A – Algo Complexity

Télécom 2A – Algo Complexity Sorting (cont.) If we are smart enough for having this kind of tests : we need a sequence of k tests to reach a subset with a single solution. Therefore : 2k ~ n! So Therefore sorting is at best in O(n.logn) And we know an algorithm in O(nlogn) T1 T2.1 T2.2 T3.1 T3.2 Télécom 2A – Algo Complexity

Examples of complexity Polynomial sum : O(n) Product of polynoms : O(n²) ? O(nlogn) Graph coloring : probably O(2n) Are 3 colors for a planar graph sufficient? Can a set of numbers be splitted in 2 subsets of equal sum? Télécom 2A – Algo Complexity

Télécom 2A – Algo Complexity Space complexity Complexity in space : how much space is required? don’t forget the stack when recursive calls occur Usually much easier than time complexity Télécom 2A – Algo Complexity

The divide and conquer strategy A first example : sorting a set S of values sort (S) = if |S| ≤ 1 then return S else divide (S, S1, S2) fusion (sort (S1), sort (S2)) end if fusion is linear is the size of its parameter; divide is either in O(1) or O(n) The result is in O(nlogn) Télécom 2A – Algo Complexity

The divide and conquer principle General principle : Take a problem of size n Divide it into a sub problems of size n/b this process adds some linear complexity cn What is the resulting complexity? Example . Sorting with fusion ; a=2, b=2 Télécom 2A – Algo Complexity

Fundamental complexity result for the divide and conquer strategy If Then If a=b : T(n) = O(n.logn) If a<b and c>0 : T(n) = O(n) If a<b and c=0 : T(n) = O(logn) If a>b : Proof : see lecture notes section 12.1.2 ◄ Most frequent case Télécom 2A – Algo Complexity

Télécom 2A – Algo Complexity Proof steps Consider n = bk (k = logbn) Summing terms together : Télécom 2A – Algo Complexity

Télécom 2A – Algo Complexity Proof steps (cont.) a<b  the sum is bounded by a constant and ak < n , so T(n) = O(n) a=b, c>0  ak = n , so T(n) = O(n.logn) a>b : the (geometric) sum is of order ak/n Both terms in ak Therefore Télécom 2A – Algo Complexity

Application: matrix multiplication Standard algorithm For all (i,j) Divide and conquer: Direct way : Counting : b=2, a=8 therefore O(n3) !!! Smart implementation: Strassen, able to bring it down to 7 Therefore O(n3) Only for large value of n (>700) Télécom 2A – Algo Complexity

Greedy algorithms : why looking for A standard optimal search algorithm: Computes the best solution extending a partial solution S’ only if its value exceeds the initial value of Optimal_Value; The result in such a case is Optimal_S; these global variables might be modified otherwise Search (S: partial_solution) : if Final(S) then if value(S)> Optimal_Value then Optimal_Value := value(S); Optimal_S := S; end if; else for each S’ extending S loop Search (S’); end if Complexity : if k steps in the loop, if the search depth is n : O(kn) Télécom 2A – Algo Complexity

Instantiation for the search of the longest path in a graph Longest (p: path) -- compute the longest path without circuits in a graph -- only if the length extends the value of The_Longest set -- before the call; in this case Long_Path is the value of this path, ….. if Cannot_Extend(p) and then length(p)> The_Longest then The_Longest := length(p); Long_Path := p; else let x be the end node of p; for each edge (x,y) such that y  p loop Longest (p  y); end if; -- initial call : The_Longest := -1; Longest (path (departure_node)); Télécom 2A – Algo Complexity

Télécom 2A – Algo Complexity Alternative Instead of the best solution, a not too bad solution? Greedy_search(S: partial_solution) : if final (S) then sub_opt_solution := S else select the best S’ expending S greedy_search (S’) end if; Complexity : O(n) Télécom 2A – Algo Complexity

Greedy search for the longest path Greedy_Longest (p: path) : if Cannot_Extend(p) then Sub_Opt_Path := p else let x be the end node of p; select the longest edge (x,y) such that y  p exp Greedy_Longest (p  y); end if; Obviously don’t lead to the optimal solution in the general case Exercise : build an example where it leads to the worst solution. Télécom 2A – Algo Complexity

How good (bad?) is such a search? Depends on the problem Can lead to the worst solution in some cases Sometimes can guarantee the best solution Example : the minimum spanning tree (find a subset of edges of total minimum cost connecting a graph) Edge_set :=  for i in 1..n-1 loop Select the edge e with lowest cost not connecting already connected nodes Add e to Edge_set End loop; Télécom 2A – Algo Complexity

Télécom 2A – Algo Complexity Notice that this algorithm might not be in O(n) as we need to find a minimum cost edge, and make sure that it don’t connect already connected nodes This can be achieved in logn steps, but is out of scope of this lecture : see the “union-find” data structure in Aho-Hopcroft-Ulman Télécom 2A – Algo Complexity

Conclusion: What to remember Complexity on average might differ from worst case complexity : smart analysis required For unknown problems, explore first the size of solution space Divide and conquer is an efficient strategy (exercises will follow); knowing the complexity theorem is required Smart algorithm design is essential: a computer 100 times faster will never defeat an exponential complexity Télécom 2A – Algo Complexity