The Design & Analysis of the algorithms Lecture 1. 2010.. by me M. Sakalli.

Slides:



Advertisements
Similar presentations
ALGORITHMS Introduction. Definition Algorithm: Any well-defined computational procedure that takes some value or set of values as input and produces some.
Advertisements

I Advanced Algorithms Analysis. What is Algorithm?  A computer algorithm is a detailed step-by-step method for solving a problem by using a computer.
Chapter 1 – Basic Concepts
Introduction to Analysis of Algorithms
CS421 - Course Information Website Syllabus Schedule The Book:
CS3381 Des & Anal of Alg ( SemA) City Univ of HK / Dept of CS / Helena Wong 2. Analysis of Algorithms - 1 Analysis.
What is an algorithm? An algorithm is a sequence of unambiguous instructions for solving a problem, i.e., for obtaining a required output for any legitimate.
CSC 2300 Data Structures & Algorithms January 30, 2007 Chapter 2. Algorithm Analysis.
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.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Design and Analysis of Algorithms - Chapter 11 Algorithm An algorithm is a.
Analysis of Algorithms COMP171 Fall Analysis of Algorithms / Slide 2 Introduction * What is Algorithm? n a clearly specified set of simple instructions.
TK3043 Analysis and Design of Algorithms Introduction to Algorithms.
David Luebke 1 8/17/2015 CS 332: Algorithms Asymptotic Performance.
Introduction Dr. Ying Lu RAIK 283: Data Structures & Algorithms.
Algorithm analysis and design Introduction to Algorithms week1
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
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.
Vishnu Kotrajaras, PhD.1 Data Structures. Vishnu Kotrajaras, PhD.2 Introduction Why study data structure?  Can understand more code.  Can choose a correct.
Lecture 2 Computational Complexity
Analysis of Algorithm Efficiency Dr. Yingwu Zhu p5-11, p16-29, p43-53, p93-96.
Asymptotic Analysis-Ch. 3
A Lecture /24/2015 COSC3101A: Design and Analysis of Algorithms Tianying Ji Lecture 1.
Major objective of this course is: Design and analysis of modern algorithms Different variants Accuracy Efficiency Comparing efficiencies Motivation thinking.
Program Efficiency & Complexity Analysis. Algorithm Review An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm.
Data Structure Introduction.
CS 361 – Chapters 8-9 Sorting algorithms –Selection, insertion, bubble, “swap” –Merge, quick, stooge –Counting, bucket, radix How to select the n-th largest/smallest.
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. Chapter 1 Introduction.
Design and Analysis of Algorithms - Chapter 11 Algorithm b An algorithm is a sequence of unambiguous instructions for solving a problem, i.e., for obtaining.
Chapter 5 Algorithms (2) Introduction to CS 1 st Semester, 2015 Sanghyun Park.
Algorithms 1.Notion of an algorithm 2.Properties of an algorithm 3.The GCD algorithm 4.Correctness of the GCD algorithm 5.Termination of the GCD algorithm.
UNIT-I INTRODUCTION ANALYSIS AND DESIGN OF ALGORITHMS CHAPTER 1:
Analysis of Algorithms COME 355 Introduction. What is an algorithm? An algorithm is a sequence of unambiguous instructions for solving a problem, i.e.,
Algorithm Analysis Part of slides are borrowed from UST.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3 rd ed., Ch. 1 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part 1. Complexity Bounds.
David Luebke 1 1/6/2016 CS 332: Algorithms Asymptotic Performance.
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.
ADVANCED ALGORITHMS REVIEW OF ANALYSIS TECHNIQUES (UNIT-1)
Introduction to Algorithms (2 nd edition) by Cormen, Leiserson, Rivest & Stein Chapter 2: Getting Started.
The Design & Analysis of the Algorithms Lecture by me M. Sakalli.
1 Ch. 2: Getting Started. 2 About this lecture Study a few simple algorithms for sorting – Insertion Sort – Selection Sort (Exercise) – Merge Sort Show.
Vishnu Kotrajaras, PhD.1 Data Structures
BITS Pilani Pilani Campus Data Structure and Algorithms Design Dr. Maheswari Karthikeyan Lecture1.
1 Chapter 2 Algorithm Analysis Reading: Chapter 2.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 3. Introduction to the Analysis of Algorithms.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Algorithm efficiency Prudence Wong.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Algorithm efficiency Prudence Wong
CS6045: Advanced Algorithms Sorting Algorithms. Sorting Input: sequence of numbers Output: a sorted sequence.
Advanced Algorithms Analysis and Design
TK3043 Analysis and Design of Algorithms
Chapter 1 Introduction Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
What is an algorithm? An algorithm is a sequence of unambiguous instructions for solving a problem, i.e., for obtaining a required output for any legitimate.
What is an algorithm? An algorithm is a sequence of unambiguous instructions for solving a problem, i.e., for obtaining a required output for any legitimate.
What is an algorithm? An algorithm is a sequence of unambiguous instructions for solving a problem, i.e., for obtaining a required output for any legitimate.
Introduction to the Design and Analysis of Algorithms
What is an algorithm? An algorithm is a sequence of unambiguous instructions for solving a problem, i.e., for obtaining a required output for any legitimate.
Algorithms Furqan Majeed.
CS 3343: Analysis of Algorithms
What is an algorithm? An algorithm is a sequence of unambiguous instructions for solving a problem, i.e., for obtaining a required output for any legitimate.
What is an algorithm? An algorithm is a sequence of unambiguous instructions for solving a problem, i.e., for obtaining a required output for any legitimate.
Objective of This Course
CSC 413/513: Intro to Algorithms
Algorithm Analysis and Design
Asst. Dr.Surasak Mungsing
Introduction to Algorithms
Chapter 1 Introduction Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
What is an algorithm? An algorithm is a sequence of unambiguous instructions for solving a problem, i.e., for obtaining a required output for any legitimate.
Presentation transcript:

The Design & Analysis of the algorithms Lecture by me M. Sakalli

M, Sakalli, CS246 Design & Analysis of Algorithms, Lecture Notes 1-1 You must have learnt these today.. b Why analysis?.. b What parameters to observe? Slight # 6.. b Methodological differences to achieve better outcomes, there Euclid’s algorithm with three methods. b Asymptotic order of growth. b Examples with insertion and selection sorting

M, Sakalli, CS246 Design & Analysis of Algorithms, Lecture Notes 1-2 Finite number of steps. An algorithm: A sequence of unambiguous – well defined procedures, instructions for solving a problem. For a desired output. For given range of input values Execution must be completed in a finite amount of time. “computer” Problem, relating input parameters with certain state parameters, analytic rendering.. algorithm inputoutput

M, Sakalli, CS246 Design & Analysis of Algorithms, Lecture Notes 1-3 Important points to keep in mind b Reduce ambiguity Keep the code simple, clean with well-defined and correct steps.Keep the code simple, clean with well-defined and correct steps. Specify the range of inputs applicable.Specify the range of inputs applicable. b Investigate other approaches solving the problem leading to determine if other efficient algorithms possible. b Theoretically: Prove its correctness..Prove its correctness.. Efficiency: Theoretical and Empirical analysisEfficiency: Theoretical and Empirical analysis Its optimalityIts optimality

M, Sakalli, CS246 Design & Analysis of Algorithms, Lecture Notes 1-4 Efficiency. b Complexity Time efficiency: Estimation in the asymptotic sense Big O, omega, theta. In comparison two also you have a hidden constant.Time efficiency: Estimation in the asymptotic sense Big O, omega, theta. In comparison two also you have a hidden constant. Space efficiency.Space efficiency. The methods applied, recursive, parallel.The methods applied, recursive, parallel. Desired scalability: Various range of inputs and the size and dimension of the problem under consideration. Examples: Video sequences. ROI.Desired scalability: Various range of inputs and the size and dimension of the problem under consideration. Examples: Video sequences. ROI. b Computational Model in terms of an abstract computer: A Turing machine. b

M, Sakalli, CS246 Design & Analysis of Algorithms, Lecture Notes 1-5 Historical Perspective … b Muhammad ibn Musa al-Khwarizmi – 9 th century mathematician b…b…b…b…

M, Sakalli, CS246 Design & Analysis of Algorithms, Lecture Notes 1-6 Analysis means: b evaluate the costs, time and space costs, and manage the resources and the methods.. b A generic RAM model of computation in which instructions are executed consecutively, but not concurrently or in parallel. Running time analysis: The # of the primitive operations executed for every line of the code c i (defined as machine independent as possible). b Asymptotic analysis b Ignore machine-dependent constants b Look at computational growth of T(n) as n→∞, n: input size that determines the number of iterations: Relative speed (in the same machine) Absolute speed (between computers)

M, Sakalli, CS246 Design & Analysis of Algorithms, Lecture Notes 1-7 Euclid’s Algorithm Problem definition: gcd(m,n) of two nonnegative, not both zero integers m and n, m > n Examples: gcd(60,24) = 12, gcd(60,0) = 60, gcd(0,0) = ? Euclid’s algorithm is based on repeated application of equality gcd(m,n) = gcd(n, m mod n) until the second number reaches to 0. Example: gcd(60,24) = gcd(24,12) = gcd(12,0) = 12 r 0 = m, r 1 = n, r i-1 = r i q i + r i+1, 0 < r i+1 < r i, 1  i<t, … r t-1 = r t q t + 0

M, Sakalli, CS246 Design & Analysis of Algorithms, Lecture Notes 1-8 Step 1 If (n == 0 or m==n), return m and stop; otherwise go to Step 2 Step 2 Divide m by n and assign the value of the remainder to r Step 3 Assign the value of n to m and the value of r to n. Go to Step 1. while n ≠ 0 do {r ← m mod n m← n m← n n ← r } n ← r } return m The upper bound of i iterations: i  log  (n)+1 where  is (1+sqrt(5))/2. !!! Not sure on this.. I need to check.. i = O(log(max(m, n))), since r i+1  r i-1 /2. The Lower bound,  (log(max(m, n))),  (log(max(m, n))).

M, Sakalli, CS246 Design & Analysis of Algorithms, Lecture Notes 1-9 Proof of Correctness of the Euclid’s algorithm b Step 1, If n divides m, then gcd(m,n)=n b Step 2, gcd(mn,)=gcd(n, m mod(n)). If gcd(m, n) divides n, this implies that gcd must be  n: gcd(m, n)  n. n divides m and n, implies that n must be smaller than the gcd of the pair {m,n}, n  gcd(m, n)If gcd(m, n) divides n, this implies that gcd must be  n: gcd(m, n)  n. n divides m and n, implies that n must be smaller than the gcd of the pair {m,n}, n  gcd(m, n) If m=n*b+r, for r, b integer numbers, then gcd(m, n) = gcd(n, r). Every common divisor of m and n, also divides r.If m=n*b+r, for r, b integer numbers, then gcd(m, n) = gcd(n, r). Every common divisor of m and n, also divides r. Proof: m=cp, n=cq, c(p-qb)=r, therefore g(m,n), divides r, so that this yields g(m,n)  gcd(n,r).Proof: m=cp, n=cq, c(p-qb)=r, therefore g(m,n), divides r, so that this yields g(m,n)  gcd(n,r).

M, Sakalli, CS246 Design & Analysis of Algorithms, Lecture Notes 1-10 Other methods for computing gcd(m,n) Consecutive integer checking algorithm, not a good way, it checks all the.. Step 1 Assign the value of min{m,n} to t Step 2 Divide m by t. If the remainder is 0, go to Step 3; otherwise, go to Step 4 Step 3 Divide n by t. If the remainder is 0, return t and stop; otherwise, go to Step 4 Step 4 Decrease t by 1 and go to Step 2 Exhaustive??.. Very slow even if zero inputs would be checked.. O(min(m, n)).  (min(m, n)), when gcd(m,n) =1.  (1) for each operation, Overall complexity is  (min(m, n))

M, Sakalli, CS246 Design & Analysis of Algorithms, Lecture Notes 1-11 Other methods for gcd(m,n) [cont.] Middle-school procedure Step 1 Find the prime factorization of m Step 2 Find the prime factorization of n Step 3 Find all the common prime factors Step 4 Compute the product of all the common prime factors and return it as gcd(m,n) Is this an algorithm?

M, Sakalli, CS246 Design & Analysis of Algorithms, Lecture Notes 1-12 Sieve of Eratosthenes Input: Integer n ≥ 2 Output: List of primes less than or equal to n, sift out the numbers that are not. for p ← 2 to n do A[p] ← p for p ← 2 to n do if A[p]  0 //p hasn’t been previously eliminated from the list j ← p * p if A[p]  0 //p hasn’t been previously eliminated from the list j ← p * p while j ≤ n do while j ≤ n do A[j] ← 0 // mark element as eliminated A[j] ← 0 // mark element as eliminated j ← j + p j ← j + p Ex:

M, Sakalli, CS246 Design & Analysis of Algorithms, Lecture Notes 1-13 Asymptotic order of growth A way of comparing functions that ignores constant factors and small input sizes b O(g(n)): class of functions f(n) that grow no faster than g(n)  Θ (g(n)): class of functions f(n) that grow at same rate as g(n)  Ω (g(n)): class of functions f(n) that grow at least as fast as g(n)

M, Sakalli, CS246 Design & Analysis of Algorithms, Lecture Notes 1-14 Establishing order of growth using the definition Definition: f(n) is in O(g(n)),  O(g(n)), if order of growth of f(n) ≤ order of growth of g(n) (within constant multiple), i.e., there exist a positive constant c,  c  N, and non-negative integer n 0 such that f(n) ≤ c g(n) for every n ≥ n 0 f(n) ≤ c g(n) for every n ≥ n 0 f(n) is o(g(n)), if f(n) ≤ (1/c) g(n) for every n ≥ n 0 which means f grows strictly!! more slowly than any arbitrarily small constant of g. ??? f(n) is  (g(n)),  c  N, f(n) ≥ c g(n) for  n ≥ n 0 f(n) is  (n) if f(n) is both O(n) and  (n) Examples: b 10n is O(cn 2 ), c ≥ 10, since 10n ≤ 10n 2 for n ≥ 1 or 10n ≤ n 2 for n ≥ 10since 10n ≤ 10n 2 for n ≥ 1 or 10n ≤ n 2 for n ≥ 10 b 5n+20 is O(cn), for all n>0, c>=25, since 5n+20 ≤ 5n+20n ≤ 25n, or c>=10 for n ≥ 4since 5n+20 ≤ 5n+20n ≤ 25n, or c>=10 for n ≥ 4

M, Sakalli, CS246 Design & Analysis of Algorithms, Lecture Notes 1-15 O, Ω, Θ

M, Sakalli, CS246 Design & Analysis of Algorithms, Lecture Notes 1-16 Example of computational problem: sorting b Statement of problem: Input: A sequence of n numbers Input: A sequence of n numbers Problem: Reorder in ascending or descending order.Problem: Reorder in ascending or descending order. Output desired: a ´ i ≤ a ´ j, for i jOutput desired: a ´ i ≤ a ´ j, for i j b Instance: The sequence b Instance: The sequence b Algorithms: Selection sortSelection sort Insertion sortInsertion sort Merge sortMerge sort (many others)(many others)

M, Sakalli, CS246 Design & Analysis of Algorithms, Lecture Notes 1-17 Selection Sort  Input: array a[1],…,a[n]  Output for example: array a sorted in non-decreasing order *** Scanning elements of unsorted part, n-1, n-2,.. Swapping. (n-1)*n/2=theta(n^2). Independent from the input. b Algorithm: (Insertion in place) for i =1 to n swap a[i] with smallest of a[i],…a[n]

M, Sakalli, CS246 Design & Analysis of Algorithms, Lecture Notes 1-18 Insertion-Sort and an idea of runtime analysis t j times line-4 executed Input size is n = length[A], and t j times line-4 executed ← 2 1: for j ← 2 to n do ← 2: key ← A[j] //Insert A[j] into the sorted sequence A[1... j − 1]. ← 3: i ← j - 1 4: while i > 0 and A[i] > key do ← 5: A[i+1] ← A[i] ← 6: i ← i-1 -4: end while ← 7: A[i+1] ← key -1: end for The total runtime for is T(n) The Best Case c 1 n + c 2 (n-1)+c 3 (n-1)+ c 4 (n-1)+c 7 (n-1) = an + b.. Times Cost nc 1 n-1 c 2 - n-1 n-1 c 3 Σ j=2:n t j Σ j=2:n t j c 4 Σ j=2:n (t j -1) Σ j=2:n (t j -1) c 5 Σ j=2:n (t j -1) Σ j=2:n (t j -1) c 6 n-10 n-1 n-1 c 7 n0 = c 1 n + c 2 (n-1)+c 3 (n-1)+ Σ j=2:n t j Σ j=2:n (t j -1) c 4 Σ j=2:n t j + c 5 Σ j=2:n (t j -1) Σ j=2:n (t j -1)+ (n-1) + c 6 Σ j=2:n (t j -1)+ c 7 (n-1)

M, Sakalli, CS246 Design & Analysis of Algorithms, Lecture Notes 1-19 Insertion-Sort Analysis continued For the worst case run time.. If the array A is sorted in reverse order, then.. while loop, Σ j=2:n t j Σ j=2:n Σ j=2:n Σ j=2:n t j = Σ j=2:n j = Σ j=2:n (n(n-1)/2) -1 Σ j=2:n (t j -1) = Σ j=2:n Σ j=2:n Σ j=2:n (t j -1) = Σ j=2:n (j-1) = Σ j=2:n n(n-1)/2 T(n) = ( + + )n 2 /2 + ( ( - - )/2 + )n + T(n) = ( c 4 + c 5 + c 6 )n 2 /2 + ( c 1 + c 2 + c 3 - ( c 4 - c 5 - c 6 )/2 + c 7 )n + ( ) ( c 2 + c 3 + c 4 + c 7 ) = An 2 + Bn + C = An 2 + Bn + C Average case runtime: Assume that about half of the subarray is out of order, then t j Average case runtime: Assume that about half of the subarray is out of order, then t j = j/2, which will lead a similar kind quadratic function

M, Sakalli, CS246 Design & Analysis of Algorithms, Lecture Notes 1-20 The methods b Brute force b Divide and conquer b Decrease and conquer b Transform and conquer b Greedy approach b Dynamic programming b Iterative improvement b Backtracking b Branch and bound b Randomized algorithms