2IS80 Fundamentals of Informatics Fall 2015 Lecture 5: Algorithms.

Slides:



Advertisements
Similar presentations
Analysis of Algorithms
Advertisements

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 2. Getting Started. Outline Familiarize you with the to think about the design and analysis of algorithms Familiarize you with the framework to.
A Basic Study on the Algorithm Analysis Chapter 2. Getting Started 한양대학교 정보보호 및 알고리즘 연구실 이재준 담당교수님 : 박희진 교수님 1.
Spring 2015 Lecture 5: QuickSort & Selection
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
Analysis of Algorithms intro.  What is “goodness”?  How to measure efficiency? ◦ Profiling, Big-Oh  Big-Oh: ◦ Motivation ◦ Informal examples ◦ Informal.
Introduction to Analysis of Algorithms
CS421 - Course Information Website Syllabus Schedule The Book:
Complexity Analysis (Part I)
Analysis of Algorithms1 Estimate the running time Estimate the memory space required. Time and space depend on the input size.
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.
CS3381 Des & Anal of Alg ( SemA) City Univ of HK / Dept of CS / Helena Wong 2. Analysis of Algorithms - 1 Analysis.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 2 Elements of complexity analysis Performance and efficiency Motivation: analysis.
Elementary Data Structures and Algorithms
Analysis of Algorithms COMP171 Fall Analysis of Algorithms / Slide 2 Introduction * What is Algorithm? n a clearly specified set of simple instructions.
Introduction CIS 606 Spring The sorting problem Input: A sequence of n numbers 〈 a 1, a 2, …, a n 〉. Output: A permutation (reordering) 〈 a’ 1,
DATA STRUCTURES AND ALGORITHMS Lecture Notes 1 Prepared by İnanç TAHRALI.
CSE 5311 DESIGN AND ANALYSIS OF ALGORITHMS. Definitions of Algorithm A mathematical relation between an observed quantity and a variable used in a step-by-step.
Analysis of Algorithm Lecture 3 Recurrence, control structure and few examples (Part 1) Huma Ayub (Assistant Professor) Department of Software Engineering.
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
C. – C. Yao Data Structure. C. – C. Yao Chap 1 Basic Concepts.
Mon 29 Sep 2014Lecture 4 1. Running Time Performance analysis Techniques until now: Experimental Cost models counting execution of operations or lines.
Discrete Structures Lecture 11: Algorithms Miss, Yanyan,Ji United International College Thanks to Professor Michael Hvidsten.
Data Structures and Algorithms Lecture 5 and 6 Instructor: Quratulain Date: 15 th and 18 th September, 2009 Faculty of Computer Science, IBA.
Lecture 2 Computational Complexity
Algorithm Efficiency CS 110: Data Structures and Algorithms First Semester,
Algorithm Input Output An algorithm is a step-by-step procedure for solving a problem in a finite amount of time. Chapter 4. Algorithm Analysis (complexity)
Algorithm Analysis An algorithm is a clearly specified set of simple instructions to be followed to solve a problem. Three questions for algorithm analysis.
2IL50 Data Structures Fall 2015 Lecture 2: Analysis of Algorithms.
Lecture 2 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
CS 221 Analysis of Algorithms Instructor: Don McLaughlin.
Analyzing algorithms & Asymptotic Notation BIO/CS 471 – Algorithms for Bioinformatics.
Asymptotic Analysis-Ch. 3
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
Data Structure Introduction.
Algorithm Analysis CS 400/600 – Data Structures. Algorithm Analysis2 Abstract Data Types Abstract Data Type (ADT): a definition for a data type solely.
September 9, Algorithms and Data Structures Lecture II Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Time Complexity of Algorithms (Asymptotic Notations)
Lecture 2 Analysis of Algorithms How to estimate time complexity? Analysis of algorithms Techniques based on Recursions ACKNOWLEDGEMENTS: Some contents.
Analysis of algorithms. What are we going to learn? Need to say that some algorithms are “better” than others Criteria for evaluation Structure of programs.
1/6/20161 CS 3343: Analysis of Algorithms Lecture 2: Asymptotic Notations.
Spring 2015 Lecture 2: Analysis of Algorithms
E.G.M. PetrakisAlgorithm Analysis1  Algorithms that are equally correct can vary in their utilization of computational resources  time and memory  a.
2IS80 Fundamentals of Informatics Fall 2015 Lecture 6: Sorting and Searching.
Lecture 2 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
2IL50 Data Structures Spring 2016 Lecture 2: Analysis of Algorithms.
Big O David Kauchak cs302 Spring Administrative Assignment 1: how’d it go? Assignment 2: out soon… Lab code.
BITS Pilani Pilani Campus Data Structure and Algorithms Design Dr. Maheswari Karthikeyan Lecture1.
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.
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.
Data Structures & Algorithm CS-102 Lecture 12 Asymptotic Analysis Lecturer: Syeda Nazia Ashraf 1.
CS6045: Advanced Algorithms Sorting Algorithms. Sorting Input: sequence of numbers Output: a sorted sequence.
Lecture 2 Algorithm Analysis
COP 3503 FALL 2012 Shayan Javed Lecture 15
Introduction to Algorithms
Growth of functions CSC317.
CS 3343: Analysis of Algorithms
Asymptotic Notations Algorithms Lecture 9.
Asst. Dr.Surasak Mungsing
David Kauchak cs161 Summer 2009
CS210- Lecture 2 Jun 2, 2005 Announcements Questions
Algorithms and Data Structures Lecture II
Presentation transcript:

2IS80 Fundamentals of Informatics Fall 2015 Lecture 5: Algorithms

Algorithms  Using a navigation system to plan a route shortest-path algorithms  Buying products on the internet secure web-site running encryption algorithms  Delivery of goods bought online algorithms to pack and route trucks Algorithms run everywhere: cars, smartphones, laptops, servers, climate-control systems, elevators …

Algorithms on computers  How do you “teach” a computational device to perform an algorithm?  Humans can tolerate imprecision, computers cannot “if traffic is bad, take another route …”  Computers do not have intuition or spatial insight “An algorithm is a set of steps to accomplish a task that is described precisely enough that a computer can run it.”

Algorithms Algorithm a well-defined computational procedure that takes some value, or a set of values, as input and produces some value, or a set of values, as output. Algorithm sequence of computational steps that transform the input into the output. Algorithms research design and analysis of algorithms and data structures for computational problems.

What do we expect from an algorithm?  Computer algorithms solve computational problems  Computational problems have well-specified input and output There are 2 requirements: 1. Given an input the algorithm should produce the correct output 2. The algorithm should use resources efficiently

Correctness Given an input the algorithm should produce the correct output  What is a correct solution? For example, the shortest-path … but given traffic, constructions … input might be incorrect  Not all problems have a well-specified correct solution ➨ we focus on problems with a clear correct solution  Randomized algorithms and approximation algorithms special cases with alternative definition of correctness

Efficiency The algorithm should use resources efficiently  The algorithm should be reasonably fast (elapsed time)  The algorithm should not use too much memory  Other resources: network bandwidth, random bits, disk operations … ➨ we focus on time  How do you measure time?

Efficiency The algorithm should use resources efficiently  How do you measure time?  Extrinsic factors: computer system, programming language, compiler, skill of programmer, other programs … ➨ implementing an algorithm, running it on a particular machine and input, and measuring time gives very little information

Efficiency analysis Two components: 1. Determine running time as function T(n) of input size n 2. Characterize rate of growth of T(n)  Focus on the order of growth ignore all but the most dominant terms Examples Algorithm A takes 50n machine cycles to search a list 50n dominates 125 if n ≥ 3, even factor 50 is not significant ➨ the running time of algorithm A grows linearly in n Algorithm B takes 20n n n machine cycles ➨ the running time of algorithm B grows as n 3

Describing algorithms  A complete description of an algorithm consists of three parts: 1. the algorithm (expressed in whatever way is clearest and most concise, can be English and / or pseudocode) 2. a proof of the algorithm’s correctness 3. a derivation of the algorithm’s running time

Searching

Linear Search Linear-Search(A, n, x) Input: A: an array n: the number of elements in A to search through x: the value to be searched for Output: Either an index i for which A[i] = x, or Not-Found 1. Set answer to Not-Found 2. For each index i, going from 1 to n, in order: A. If A[i] = x, then set answer to the value of i 3. Return the value of answer as the output Input and Output specification

Linear Search Linear-Search(A, n, x) Input: A: an array n: the number of elements in A to search through x: the value to be searched for Output: Either an index i for which A[i] = x, or Not-Found 1. Set answer to Not-Found 2. For each index i, going from 1 to n, in order: A. If A[i] = x, then set answer to the value of i 3. Return the value of answer as the output A.length = n 4 array A[1 … n]

Linear Search Linear-Search(A, n, x) Input: A: an array n: the number of elements in A to search through x: the value to be searched for Output: Either an index i for which A[i] = x, or Not-Found 1. Set answer to Not-Found 2. For each index i, going from 1 to n, in order: A. If A[i] = x, then set answer to the value of i 3. Return the value of answer as the output This loop always runs until n. Is that necessary? Loop with variable i Body of the loop

Linear Search Better-Linear-Search(A, n, x) Input: A: an array n: the number of elements in A to search through x: the value to be searched for Output: Either an index i for which A[i] = x, or Not-Found 1. For i = 1 to n: A. If A[i] = x, then return the value of i as the output 2. Return Not-Found as the output This loop uses 2 comparisons per iteration. We could be more efficient … and learn about sentinels on the way … n4

Linear Search Sentinel-Linear-Search(A, n, x) Input & Output as before 1. Save A[n] into last then put x into A[n] 2. Set i to 1 3. While A[i] ≠ x to the following: A. Increment i 4. Restore A[n] from last 5. If i < n or A[n] = x then return the value of i as the output 6. Otherwise, return Not-Found as the output n4 x = 25last = 5 25

Correctness

Correctness proof It’s easy to see that Linear-Search works … it’s not always that easy …  There are several methods to prove correctness ➨ we focus on loop invariants Loop invariant an assertion that we prove to be true each time a loop iteration starts

Correctness proof  To proof correctness with a loop invariant we need to show three things: Initialization Invariant is true prior to the first iteration of the loop. Maintenance If the invariant is true before an iteration of the loop, it remains true before the next iteration. Termination The loop terminates, and when it does, the loop invariant, along with the reason that the loop terminated gives us a useful property.

Linear Search Better-Linear-Search(A, n, x) 1. For i = 1 to n: A. If A[i] = x, then return the value of i as the output 2. Return Not-Found as the output to show 1. if index i is returned than A[i] = x 2. if Not-Found is returned then x is not in the array Loop invariant At the start of each iteration of step 1, if x is present in the array A, then it is present in the subarray from A[i] through A[n] ✔

Linear Search Better-Linear-Search(A, n, x) 1. For i = 1 to n: A. If A[i] = x, then return the value of i as the output 2. Return Not-Found as the output Loop invariant At the start of each iteration of step 1, if x is present in the array A, then it is present in the subarray from A[i] through A[n] Initialization Initially, i=1 so that the subarray in the loop invariant is A[1] through A[n], which is the entire array.

Linear Search Better-Linear-Search(A, n, x) 1. For i = 1 to n: A. If A[i] = x, then return the value of i as the output 2. Return Not-Found as the output Loop invariant At the start of each iteration of step 1, if x is present in the array A, then it is present in the subarray from A[i] through A[n] Maintenance If at the start of an iteration x is present in the array, then it is present in the subarray from A[i] though A[n]. If we do not return then A[i] ≠ x. Hence, if x is in the array then is it in the subarray from A[i+1] though A[n]. i is incremented before the next iteration, so the invariant will hold again.

Linear Search Better-Linear-Search(A, n, x) 1. For i = 1 to n: A. If A[i] = x, then return the value of i as the output 2. Return Not-Found as the output Loop invariant At the start of each iteration of step 1, if x is present in the array A, then it is present in the subarray from A[i] through A[n] Termination If A[i] = x ✔. If i > n consider contrapositive of invariant. “if A then B” ● “if not B then not A”

Linear Search Better-Linear-Search(A, n, x) 1. For i = 1 to n: A. If A[i] = x, then return the value of i as the output 2. Return Not-Found as the output Loop invariant At the start of each iteration of step 1, if x is present in the array A, then it is present in the subarray from A[i] through A[n] Termination If A[i] = x ✔. If i > n consider contrapositive of invariant. “if x is not present in the subarray from A[i] through A[n], then x is not present in A.” i > n ➨ subarray from A[i] through A[n] is empty ➨ x is not present in an empty subarray ➨ x is not present in A

Efficiency

Efficiency analysis 1. Determine running time as function T(n) of input size n 2. Characterize rate of growth of T(n)  Focus on the order of growth ignore all but the most dominant terms n single integer, x as large as array element, hence insignificant …  input size is n: number of elements in A Linear-Search(A, n, x) 1.Set answer to Not-Found 2.For each index i, going from 1 to n, in order: A.If A[i] = x, then set answer to the value of i 3.Return the value of answer as the output

Efficiency analysis 1. Determine running time as function T(n) of input size n best case average case worst case elementary operations add, subtract, multiply, divide, load, store, copy, conditional and unconditional branch, return … An algorithm has worst case running time T(n) if for any input of size n the maximal number of elementary operations executed is T(n).

Efficiency analysis Linear-Search(A, n, x) 1. Set answer to Not-Found 2. For each index i, going from 1 to n, in order: A. If A[i] = x, then set answer to the value of i 3. Return the value of answer as the output Assumption each execution of step i takes t i time (independent of n) 1. t 1 2. t 2 ’ (test i againt n) + t 2 ’’ (increment i) A. t 2A ’ (test A[i] = x) + t 2A ’’ (set answer to i) 3. t 3

Efficiency analysis Linear-Search(A, n, x) 1. Set answer to Not-Found 2. For each index i, going from 1 to n, in order: A. If A[i] = x, then set answer to the value of i 3. Return the value of answer as the output 1. t 1 2. t 2 ’ (test i againt n) + t 2 ’’ (increment i) A. t 2A ’ (test A[i] = x) + t 2A ’’ (set answer to i) 3. t 3 Running time between t 1 + t 2 ’ ∙ (n+1) + t 2 ’’ ∙ n + t 2A ’ ∙ n + t 2A ’’ ∙ 0 + t 3 and t 1 + t 2 ’ ∙ (n+1) + t 2 ’’ ∙ n + t 2A ’ ∙ n + t 2A ’’ ∙ n + t 3

Efficiency analysis Linear-Search(A, n, x) 1. Set answer to Not-Found 2. For each index i, going from 1 to n, in order: A. If A[i] = x, then set answer to the value of i 3. Return the value of answer as the output Running time between t 1 + t 2 ’ ∙ (n+1) + t 2 ’’ ∙ n + t 2A ’ ∙ n + t 2A ’’ ∙ 0 + t 3 and t 1 + t 2 ’ ∙ (n+1) + t 2 ’’ ∙ n + t 2A ’ ∙ n + t 2A ’’ ∙ n + t 3 lower bound(t 2 ’ + t 2 ’’ + t 2A ’) ∙ n + (t 1 + t 2 ’ + t 3 ) ➨ c 1 ∙ n + d upper bound(t 2 ’ + t 2 ’’ + t 2A ’ + t 2A ’’ ) ∙ n + (t 1 + t 2 ’ + t 3 ) ➨ c 2 ∙ n + d ➨ bounded below and above by linear function in n ➨ Θ(n)

Θ-notation Let g(n) : N ↦ N be a function. Then we have Θ(g(n)) = { f(n) : there exist positive constants c 1, c 2, and n 0 such that 0 ≤ c 1 g(n) ≤ f(n) ≤ c 2 g(n) for all n ≥ n 0 } “Θ(g(n)) is the set of functions that grow as fast as g(n)”

Θ-notation Let g(n) : N ↦ N be a function. Then we have Θ(g(n)) = { f(n) : there exist positive constants c 1, c 2, and n 0 such that 0 ≤ c 1 g(n) ≤ f(n) ≤ c 2 g(n) for all n ≥ n 0 } n n0n0 c 1 g(n) c 2 g(n) f(n) 0 Notation: f(n) = Θ(g(n))

Θ-notation Example n 2 / n + 50 = Θ(n 2 ) n > 400 ➨ n 2 /4 > 100 n + 50 n = 1000 ➨ n 2 /4 = 250,000 and 100 n + 50 = 100,050 n = 2000 ➨ n 2 /4 = 1,000,000 and 100 n + 50 = 200,050 …

Efficiency analysis Better-Linear-Search(A, n, x) 1. For i = 1 to n: A. If A[i] = x, then return the value of i as the output 2. Return Not-Found as the output  Running time?  How often does line 1A execute? in the worst case and in the best case … Worst case: x is not in the array ➨ Θ(n) Best case: A[1] = x ➨ Θ(1)  A linear function is an upper bound ➨ O(n) the running time might be better than linear, but never worse …

O-notation Let g(n) : N ↦ N be a function. Then we have O(g(n)) = { f(n) : there exist positive constants c and n 0 such that 0 ≤ f(n) ≤ cg(n) for all n ≥ n 0 } “O(g(n)) is the set of functions that grow at most as fast as g(n)”

O-notation Let g(n) : N ↦ N be a function. Then we have O(g(n)) = { f(n) : there exist positive constants c and n 0 such that 0 ≤ f(n) ≤ cg(n) for all n ≥ n 0 } n n0n0 cg(n) f(n) 0 Notation: f(n) = O(g(n))

Efficiency analysis O-notation running time is never worse than given function upper bound Lower bound: the running time is never better than a given function Better-linear-search: Ω(1) very weak bound … sometimes we can do better …

Ω-notation Let g(n) : N ↦ N be a function. Then we have Ω(g(n)) = { f(n) : there exist positive constants c and n 0 such that 0 ≤ cg(n) ≤ f(n) for all n ≥ n 0 } “Ω(g(n)) is the set of functions that grow at least as fast as g(n)”

Ω-notation Let g(n) : N ↦ N be a function. Then we have Ω(g(n)) = { f(n) : there exist positive constants c and n 0 such that 0 ≤ cg(n) ≤ f(n) for all n ≥ n 0 } n n0n0 cg(n) f(n) 0 Notation: f(n) = Ω(g(n))

Asymptotic notation Θ(…) is an asymptotically tight bound O(…) is an asymptotic upper bound Ω(…) is an asymptotic lower bound  other asymptotic notation o(…) → “grows strictly slower than” ω(…) → “grows strictly faster than” “ asymptotically equal” “asymptotically smaller or equal” “asymptotically greater or equal”

Quiz n = O(n 2 ) 2.4 n log 2 n + 20 n = Θ(n log n) 3.n log n = Ω(n) 4.n log 2 n = O(n 4/3 ) 5.57 n log n = Θ(n 2 ) 6.An algorithm with worst case running time O(n log n) is always slower than an algorithm with worst case running time O(n) if n is sufficiently large. true false true false

Recap and preview Today  Describing algorithms  Linear Search  Correctness proofs via loop invariants  Efficiency analysis Next lecture  Searching algorithms InsertionSort MergeSort & QuickSort Divide & Conquer Binary Search  Correctness proofs & efficiency analysis