What is an algorithm? Algorithms are the ideas behind computer programs. An algorithm is the thing which stays the same whether the program is in Pascal.

Slides:



Advertisements
Similar presentations
MATH 224 – Discrete Mathematics
Advertisements

Analysis of Algorithms
CSE 373: Data Structures and Algorithms Lecture 5: Math Review/Asymptotic Analysis III 1.
Razdan with contribution from others 1 Algorithm Analysis What is the Big ‘O Bout? Anshuman Razdan Div of Computing.
Chapter 1 – Basic Concepts
What is an Algorithm? (And how do we analyze one?)
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
CSC401 – Analysis of Algorithms Lecture Notes 1 Introduction
Asymptotic Growth Rate
Introduction to Analysis of Algorithms
CS421 - Course Information Website Syllabus Schedule The Book:
Eleg667/2001-f/Topic-1a 1 A Brief Review of Algorithm Design and Analysis.
Analysis of Algorithms (Chapter 4)
CSE 830: Design and Theory of Algorithms
Analysis of Algorithms1 Estimate the running time Estimate the memory space required. Time and space depend on the input size.
CSE 830: Design and Theory of Algorithms
1 Data Structures A program solves a problem. A program solves a problem. A solution consists of: A solution consists of:  a way to organize the data.
CSE 830: Design and Theory of Algorithms Dr. Eric Torng.
Lecture 3 Aug 31, 2011 Goals: Chapter 2 (algorithm analysis) Examples: Selection sorting rules for algorithm analysis discussion of lab – permutation generation.
CS Main Questions Given that the computer is the Great Symbol Manipulator, there are three main questions in the field of computer science: What kinds.
1 CSE 417: Algorithms and Computational Complexity Winter 2001 Instructor: Paul Beame TA: Gidon Shavit.
Analysis of Algorithms 7/2/2015CS202 - Fundamentals of Computer Science II1.
Analysis of Algorithms COMP171 Fall Analysis of Algorithms / Slide 2 Introduction * What is Algorithm? n a clearly specified set of simple instructions.
Introduction to Algorithm design and analysis
Analysis of Algorithms Spring 2015CS202 - Fundamentals of Computer Science II1.
David Luebke 1 8/17/2015 CS 332: Algorithms Asymptotic Performance.
Algorithm Analysis (Big O)
COMP s1 Computing 2 Complexity
Instructor Neelima Gupta
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:
Program Performance & Asymptotic Notations CSE, POSTECH.
CSC 201 Analysis and Design of Algorithms Lecture 03: Introduction to a CSC 201 Analysis and Design of Algorithms Lecture 03: Introduction to a lgorithms.
Week 2 CS 361: Advanced Data Structures and Algorithms
Discrete Structures Lecture 11: Algorithms Miss, Yanyan,Ji United International College Thanks to Professor Michael Hvidsten.
Lecture 2 Computational Complexity
Algorithm Efficiency CS 110: Data Structures and Algorithms First Semester,
Mathematics Review and Asymptotic Notation
1 Computer Algorithms Lecture 3 Asymptotic Notation Some of these slides are courtesy of D. Plaisted, UNC and M. Nicolescu, UNR.
DISCRETE MATHEMATICS I CHAPTER 11 Dr. Adam Anthony Spring 2011 Some material adapted from lecture notes provided by Dr. Chungsim Han and Dr. Sam Lomonaco.
Algorithm Analysis An algorithm is a clearly specified set of simple instructions to be followed to solve a problem. Three questions for algorithm analysis.
Analysis of Algorithms
Chapter 3 Sec 3.3 With Question/Answer Animations 1.
Analyzing algorithms & Asymptotic Notation BIO/CS 471 – Algorithms for Bioinformatics.
Asymptotic Analysis-Ch. 3
Major objective of this course is: Design and analysis of modern algorithms Different variants Accuracy Efficiency Comparing efficiencies Motivation thinking.
Asymptotic Notation (O, Ω, )
Program Efficiency & Complexity Analysis. Algorithm Review An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm.
September 17, 2001 Algorithms and Data Structures Lecture II Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Algorithm Analysis Part of slides are borrowed from UST.
Time Complexity. Solving a computational program Describing the general steps of the solution –Algorithm’s course Use abstract data types and pseudo code.
1/6/20161 CS 3343: Analysis of Algorithms Lecture 2: Asymptotic Notations.
Asymptotic Performance. Review: Asymptotic Performance Asymptotic performance: How does algorithm behave as the problem size gets very large? Running.
1 Ch. 2: Getting Started. 2 About this lecture Study a few simple algorithms for sorting – Insertion Sort – Selection Sort (Exercise) – Merge Sort Show.
CES 592 Theory of Software Systems B. Ravikumar (Ravi) Office: 124 Darwin Hall.
Announcement We will have a 10 minutes Quiz on Feb. 4 at the end of the lecture. The quiz is about Big O notation. The weight of this quiz is 3% (please.
Complexity of Algorithms Fundamental Data Structures and Algorithms Ananda Guna January 13, 2005.
Ch03-Algorithms 1. Algorithms What is an algorithm? An algorithm is a finite set of precise instructions for performing a computation or for solving a.
Analysis of Algorithms Spring 2016CS202 - Fundamentals of Computer Science II1.
Chapter 3 Chapter Summary  Algorithms o Example Algorithms searching for an element in a list sorting a list so its elements are in some prescribed.
GC 211:Data Structures Week 2: Algorithm Analysis Tools Slides are borrowed from Mr. Mohammad Alqahtani.
September 18, Algorithms and Data Structures Lecture II Simonas Šaltenis Aalborg University
CSE 3358 NOTE SET 2 Data Structures and Algorithms 1.
Data Structures I (CPCS-204) Week # 2: Algorithm Analysis tools Dr. Omar Batarfi Dr. Yahya Dahab Dr. Imtiaz Khan.
Introduction to Algorithms
Introduction Algorithms Order Analysis of Algorithm
CS 3343: Analysis of Algorithms
Objective of This Course
Asst. Dr.Surasak Mungsing
Estimating Algorithm Performance
Algorithms and Data Structures Lecture II
Presentation transcript:

What is an algorithm? Algorithms are the ideas behind computer programs. An algorithm is the thing which stays the same whether the program is in Pascal running on a Cray in New York or is in BASIC running on a PC in Taipei! To be interesting, an algorithm has to solve a general, specified problem. An algorithmic problem is specified by describing the set of instances it must work on and what desired properties the output must have.

What is an algorithm? Problem: specified by its input/output behaviors,   Algorithm: a problem-solving procedure which can be implemented on a computer and satisfies the following conditions: it terminates,  it is correct, and  it is well-defined Program: implementation of an algorithm on a computer A problem will normally have many (usually infinitely many) instances. An algorithm must work correctly on every instance of the problem it claims to solve.

Example

Another Example Suppose you have a robot arm equipped with a tool, say a soldering iron. To enable the robot arm to do a soldering job, we must construct an ordering of the contact points, so the robot visits (and solders) the first contact point, then visits the second point, third, and so forth until the job is done. Since robots are expensive, we need to find the order which minimizes the time (I.e., travel distance) it takes to assemble the circuit board.

Correctness is Not Obvious! You are given the job to program the robot arm. Give me an algorithm to find the best tour!

Nearest Neighbor Tour A very popular solution starts at some point p0 and then walks to its nearest neighbor p1 first, then repeats from p1, etc. until done. Pick and visit an initial point p0 p = p0 i = 0 While there are still unvisited points i = i +1 Let pi be the closest unvisited point to pi-1 Visit pi Return to p0 from pi

It Does Not Solve The Problem! This algorithm is simple to understand and implement and very efficient. However, it is not correct! Always starting from the leftmost point or any other point will not solve the problem.

Closest Pair Tour Always walking to the closest point is too restrictive, since that point might trap us into making moves we don't want. Another idea would be to repeatedly connect the closest pair of points whose connection will not cause a cycle or a three-way branch to be formed, until we have a single chain with all the points in it.

It is Still Not Correct! Let n be the number of points in the set d =  For i = 1 to n - 1 do For each pair of endpoints (x; y) of partial paths If dist(x; y) < d then xm = x, ym = y, d = dist(x; y) Connect (xm; ym ) by an edge Connect the two endpoints by an edge. Although it works correctly on the previous example, other data causes trouble:

A Correct Algorithm We could try all possible orderings of the points, then select the ordering which minimizes the total length: d =  For each of the n! permutations i of the n points if (cost(i) < d) then d = cost(i) and Pmin = i Return Pmin

It is Not Efficient Since all possible orderings are considered, we are guaranteed to end up with the shortest possible tour. Because it tries all n! permutations, it is extremely slow, much too slow to use when there are more than 10-20 points. No efficient, correct algorithm exists for the traveling salesman problem, as we will see later.

Efficiency “Why not just use a supercomputer?” Supercomputers are for people too rich and too stupid to design efficient algorithms! A faster algorithm running on a slower computer will always win for sufficiently large instances, as we shall see. Usually, problems don't have to get that large before the faster algorithm wins.

Expressing Algorithms We need some way to express the sequence of steps comprising an algorithm. In order of increasing precision, we have English, pseudocode, and real programming languages. Unfortunately, ease of expression moves in the reverse order. I prefer to describe the ideas of an algorithm in natural language, moving to pseudocode to clarify sufficiently tricky details of the algorithm.

Pseudocode notation Similar to any typical imperative programming language, such as Pascal, C, Modula, Java, ... Liberal use of English. Use of indentation for block structure. Employs any clear and concise expressive methods. Typically not concerned with software engineering issues such as: error handling. data abstraction. modularity.

Algorithm Analysis Predicting the amount of resource required from the size of the input. We must have some quantity to count. Typically: runtime. memory. number of basic operations, such as: arithmetic operations (e.g., for multiplying matrices). bit operations (e.g., for multiplying integers). comparisons (e.g., for sorting and searching). Types of Analysis: worst-case. average-case. best-case.

Best, Worst, and Average-Case Types of Analysis: The worst case complexity of the algorithm is the function defined by the maximum number of steps takes on any instance of size n. The best case complexity of the algorithm is the function defined by the minimum number of steps taken on any instance of size n. The average-case complexity of the algorithm is the function defined by an average number of steps taken on any instance of size n. Each of these complexities defines a numerical function: time v.s. size!

Best, Worst, and Average-Case

The RAM Model Algorithms are the only important, durable, and original part of computer science because they can be studied in a machine and language independent way. We will do most of our design and analysis for the RAM model of computation: Each "simple" operation (+, -, =, if, call) takes exactly 1 step. Loops and subroutine calls are not simple operations, but depend upon the size of the data and the contents of a subroutine. We do not want “sort" to be a single step operation. Each memory access takes exactly 1 step.

Insertion Sort One way to sort an array of n elements is to start with an empty list, then successively insert new elements in the proper position: a1  a2    ak  ak+1  an At each stage, the inserted element leaves a sorted list, and after n insertions contains exactly the right elements. Thus the algorithm must be correct. But how efficient is it? Note that the run time changes with the permutation instance! (even for a fixed size problem)

Example

Exact Analysis of Insertion Sort In the following table, n = length(A), and tj = number of times the while test in line 5 is executed in the jth iteration. Line InsertionSort(A) #Inst. #Exec. 1 2 3 4 5 6 7 8 for j:=2 to n do key:=A[j] /* put A[j] into A[1..j-1] */ i:=j-1 while i>0 & A[i] > key do A[i+1] :=A[i] i:=i-1 A[i+1]:=key c1 c2 c4 c5 c6 c7 c8 n (why?) n-1

The Total Cost Add up the executed instructions for all pseudo-code lines to get the run-time of the algorithm: What are the tj‘s? They depend on the particular input.

Best Case If it's already sorted, all tj‘s are 1. Hence, the best case time is c1n + (c2 + c4 + c5 + c8)(n – 1) = Cn + D where C and D are constants.

Worst Case If the input is sorted in descending order, we will have to slide all of the already-sorted elements, so tj = j, and step 5 is executed Total runtime is

Average Case

Exact Analysis is Hard! Exact Analysis is difficult to work with because it is typically very complicated! Thus it is usually cleaner and easier to talk about upper and lower bounds of the function.

Life Cycle of Algorithm Development

Related Courses • Formal Specification • Abstract computation models • Data structure design • Algorithm design techniques • Theory of Computation / Complexity • Software engineering • Program verification • Computability

Classification of Algorithms by methods (techniques) by characteristics by running environments (architectures)

Classified by methods (techniques) Divide and Conquer Dynamic Programming Greedy Network Flow Linear/Integer Programming Backtracking Branch and Bound • • •

Classified by characteristics Heuristic Approximation Randomized (Probabilistic) On-Line Genetic • • •

Classified by running environments Sequential Parallel Distributed Systolic • • •

Asymptotic Notations Suppose f and g are functions defined on positive integers: Asymptotic upper bound: O(g(n)) = {f(n): there exist positive constants c and n0 such that 0 ≤ f(n) ≤ cg(n) for all n > n0 }. Asymptotic lower bound: W(g(n)) = {f(n): there exist positive constants c and n0 such that 0 ≤ cg(n) ≤ f(n) for all n > n0}. Asymptotic tight bound: Q(g(n)) = {f(n): there exist positive constants c1, c2, and n0 such that 0 ≤ c1g(n) ≤ f(n) ≤ c2g(n) for all n > n0}.

Useful (abuse of) notation Write f(n) = O(g(n)) to mean f(n)  O(g(n)). Similarly for W, and Q. Very useful, e.g.:

Big-O

Big-W f(n) = W(g(n))

Big-Q f(n) = Q(g(n))

W, Q and O tight bound upper bound lower bound f(n) = Q(g(n)) f(n) = O(g(n)) f(n) = W(g(n)) tight bound upper bound lower bound

Growth Rate of Functions

A Revealing Table

Another Revealing Table If an algorithm runs in time T(n), what is the largest problem instance that it can solve in 1 minute?

Another definition of W   In using this notation, the left-hand side is more precise than the right, i.e., n2 = O(n4), 27n3 = Q(n3), Q(n) = O(n2), we do not say O(n2) = n2. Another definition of the big omega notation: f(n) = W(g(n)) iff there exists constant c > 0 and positive integer n0 such that f(n) ≥ c  g(n), for infinitely many n ≥ n0. Why define big-O and big- W notation in such an asymmetric why?

Example What is the asymptotic order of f(n)? Clearly, f(n) is O(n) but not Q(n) and hence not W(n). • What is the lower bound of f(n)? According to the original definition, the best lower bound is 0.

More Asymptotic Notations Upper bound that is not asymptotically tight: o(g(n)) = {f(n): for any c > 0, there exist positive constant n0 such that 0 ≤ f(n) < cg(n) for all n > n0 }.   Lower bound that is not asymptotically tight: w(g(n)) = {f(n): for any c >0, there exist positive constant n0 such that 0 ≤ cg(n) < f(n) for all n > n0}. f(n)  O(g(n)) iff g(n)  W(f(n)). f(n)  o(g(n)) iff g(n)  w(f(n)).

Comparison of functions Many of the relational properties of real numbers apply to asymptotic comparisons as well. For the following, assume that f(n) and g(n) are asymptotically positive. Transitivity: f(n) = Q(g(n)) and g(n) = Q(h(n)) imply f(n) = Q(h(n)), f(n) = O(g(n)) and g(n) = O(h(n)) imply f(n) = O(h(n)), f(n) = W(g(n)) and g(n) = W(h(n)) imply f(n) = W(h(n)), f(n) = o(g(n)) and g(n) = o(h(n)) imply f(n) = o(h(n)), f(n) = w(g(n)) and g(n) = w(h(n)) imply f(n) = w(h(n)).

Relations Reflexivity:   f(n) = Q(f(n)), f (n) = O(f(n)), f(n) = W(f(n)). Symmetry: f (n) = Q(g(n)) if and only if g(n) = Q(f(n)). Transpose symmetry: f(n) = O(g(n)) iff g(n) = W(f(n)), f(n) = o(g(n)) iff g(n) = w(f(n)).

Asymptotic vs. Real Numbers Analogy between the asymptotic comparison of two functions f and g and the comparison of two real numbers a and b:   f(n) = Q(g(n)) € a = b f(n) = O(g(n)) € a ≤ b f(n) = o(g(n))  € a < b f(n) = W(g(n))  € a ≥ b f(n) = w(g(n))  a > b. Any two real numbers can be compared, but not all functions are asymptotically comparable. That is, for two functions f(n) and g(n), it may be the case that neither f(n) = O(g(n)) nor f(n) = W(g(n)) holds. E.g.: f(n) = n1+sin(n), and g(n) = n1-sin(n).

Properties of Asymptotic Notations 1. For all k > 0, kf is O(f).   2. If f is O(g) and f’ is O(g) then (f + f’) is O(g). If f is O(g) then (f + g) is O(g). 3. If f is O(g) and g is O(h) then f is O(h). 4. nr is O(ns) if 0 ≤ r ≤ s. 5. If p is a polynomial of degree d then p is Q(nd).

More Properties 6. If f is O(g) and h is O(r) then f  h is O(g  r).   7. nk is O(bn), for all b > 1, k 0. 8. logb n is O(nk), for all b > 1, k > 0. 9. logbn is Q(logdn), for all b, d > 1. 10. kr = O(nr+1).

Intractability Exercises: • Is 3n O(2n)?   Definition: An algorithm has polynomial time complexity iff its time complexity is O(nd) for some integer d. A problem is intractable iff no algorithm with polynomial time complexity is known for it. Exercises: • Is 3n O(2n)? • What is O(n2 - n + 6) - O(n2 - 6)? • Find functions f and g such that (1) f is O(g), (2) f is not W(g), and (3) f(n) > g(n) for infinitely many n.

Example Q(0.9n) < Q(22001) < Q((log n)2000) < Q(n0.0001) < Q(n) < Q(log n!) = Q(log nn) = Q(n2log log n) = Q(n log n) < Q(n1.5) < Q(3log n) < Q(n1999) < Q(2log n log log n) < Q(2n2) < Q(22n) Order the following functions by their growth rate from smallest to largest. (a) log n! (b) log nn (c) 2log n log log n (d) 22n (e) 3log2n (f) 22001 (g) 2n2 (h) n2log log n (i) n1999 (j) 0.9n (k) n (l) n log n (m) n0.0001 (n) (o) (log n)2000

Proof of log n! = Q(n log n) To show f(n) = Q(g(n)), we must show O and W. Go back to the definition. Big O: Must show that log n! = O(n log n) log n! < log nn = n log n, therefore log n! = O(n log n) Big W: Must show that log n! = W(n log n) log n! > log (n/2)n/2 = n(log n/2)/2 > n(log n)/4 for n > 4 Thus, log n! = W(n log n) Therefore, log n! = Q(n log n)