Algorithm Analysis. Applied Algorithmics COMP526. Algorithm Analysis. Algorithm Analysis via experiments

Similar documents
ANALYSIS OF ALGORITHMS

Data Structures and Algorithms

Elementary maths for GMT. Algorithm analysis Part I

Analysis of Algorithm. Chapter 2

Choice of C++ as Language

LECTURE 9 Data Structures: A systematic way of organizing and accessing data. --No single data structure works well for ALL purposes.

Data Structures Lecture 8

Analysis of Algorithms

Algorithms and Data Structures

PROGRAM EFFICIENCY & COMPLEXITY ANALYSIS

Asymptotic Analysis of Algorithms

Outline and Reading. Analysis of Algorithms 1

Introduction to Data Structure

[ 11.2, 11.3, 11.4] Analysis of Algorithms. Complexity of Algorithms. 400 lecture note # Overview

Data Structures and Algorithms

CSE 373 APRIL 3 RD ALGORITHM ANALYSIS

9/10/2018 Algorithms & Data Structures Analysis of Algorithms. Siyuan Jiang, Sept

Algorithm Analysis. (Algorithm Analysis ) Data Structures and Programming Spring / 48

0.1 Welcome. 0.2 Insertion sort. Jessica Su (some portions copied from CLRS)

Agenda. The worst algorithm in the history of humanity. Asymptotic notations: Big-O, Big-Omega, Theta. An iterative solution

L6,7: Time & Space Complexity

Complexity Analysis of an Algorithm

CS240 Fall Mike Lam, Professor. Algorithm Analysis

RUNNING TIME ANALYSIS. Problem Solving with Computers-II

Chapter 2: Complexity Analysis

CS:3330 (22c:31) Algorithms

CSc 225 Algorithms and Data Structures I Case Studies

(Refer Slide Time: 1:27)

Algorithm Analysis. October 12, CMPE 250 Algorithm Analysis October 12, / 66

CS240 Fall Mike Lam, Professor. Algorithm Analysis

CS/ENGRD 2110 Object-Oriented Programming and Data Structures Spring 2012 Thorsten Joachims. Lecture 10: Asymptotic Complexity and

CSE373: Data Structures and Algorithms Lecture 4: Asymptotic Analysis. Aaron Bauer Winter 2014

Data structure and algorithm in Python

Unit 1 Chapter 4 ITERATIVE ALGORITHM DESIGN ISSUES

10/5/2016. Comparing Algorithms. Analyzing Code ( worst case ) Example. Analyzing Code. Binary Search. Linear Search

How much space does this routine use in the worst case for a given n? public static void use_space(int n) { int b; int [] A;

DESIGN AND ANALYSIS OF ALGORITHMS. Unit 1 Chapter 4 ITERATIVE ALGORITHM DESIGN ISSUES

Chapter 6 INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS

Algorithm. Algorithm Analysis. Algorithm. Algorithm. Analyzing Sorting Algorithms (Insertion Sort) Analyzing Algorithms 8/31/2017

Data Structures and Algorithms. Part 2

Assignment 1 (concept): Solutions

CS302 Topic: Algorithm Analysis. Thursday, Sept. 22, 2005

Introduction to Computer Science

asymptotic growth rate or order compare two functions, but ignore constant factors, small inputs

Lecture 5: Running Time Evaluation

COMP Analysis of Algorithms & Data Structures

Review for Midterm Exam

Data Structures and Algorithms CSE 465

IE 495 Lecture 3. Septermber 5, 2000

INTRODUCTION. Analysis: Determination of time and space requirements of the algorithm

csci 210: Data Structures Program Analysis

University of Petra Faculty of Information Technology

Algorithm. Lecture3: Algorithm Analysis. Empirical Analysis. Algorithm Performance

Programming II (CS300)

Algorithms and Data Structures

Data Structures and Algorithms

INTRODUCTION. Objective: - Algorithms - Techniques - Analysis. Algorithms:

UNIT 1 ANALYSIS OF ALGORITHMS

Scientific Computing. Algorithm Analysis

Algorithm Analysis. Gunnar Gotshalks. AlgAnalysis 1

Introduction to Computer Science

Sankalchand Patel College of Engineering - Visnagar Department of Computer Engineering and Information Technology. Assignment

Algorithm efficiency can be measured in terms of: Time Space Other resources such as processors, network packets, etc.

Why study algorithms? CS 561, Lecture 1. Today s Outline. Why study algorithms? (II)

CSCA48 Winter 2018 Week 10:Algorithm Analysis. Marzieh Ahmadzadeh, Nick Cheng University of Toronto Scarborough

Data Structures and Algorithms Chapter 2

CSE 421: Introduction to Algorithms Complexity

What is an algorithm?

Plotting run-time graphically. Plotting run-time graphically. CS241 Algorithmics - week 1 review. Prefix Averages - Algorithm #1

Introduction to Analysis of Algorithms

INTRODUCTION. An easy way to express the idea of an algorithm (very much like C/C++, Java, Pascal, Ada, )

Midterm solutions. n f 3 (n) = 3

Pseudo code of algorithms are to be read by.

NCS 301 DATA STRUCTURE USING C

8/19/2014. Most algorithms transform input objects into output objects The running time of an algorithm typically grows with input size

ASYMPTOTIC COMPLEXITY

MergeSort, Recurrences, Asymptotic Analysis Scribe: Michael P. Kim Date: April 1, 2015

Module 1: Asymptotic Time Complexity and Intro to Abstract Data Types

ASYMPTOTIC COMPLEXITY

CSE 146. Asymptotic Analysis Interview Question of the Day Homework 1 & Project 1 Work Session

Overview. CSE 101: Design and Analysis of Algorithms Lecture 1

Instructions. Definitions. Name: CMSC 341 Fall Question Points I. /12 II. /30 III. /10 IV. /12 V. /12 VI. /12 VII.

Data Structures Question Bank Multiple Choice

EECS 203 Spring 2016 Lecture 8 Page 1 of 6

Another Sorting Algorithm

Algorithm Analysis. Algorithm Efficiency Best, Worst, Average Cases Asymptotic Analysis (Big Oh) Space Complexity

MergeSort, Recurrences, Asymptotic Analysis Scribe: Michael P. Kim Date: September 28, 2016 Edited by Ofir Geri

Cpt S 223 Course Overview. Cpt S 223, Fall 2007 Copyright: Washington State University

CS141: Intermediate Data Structures and Algorithms Analysis of Algorithms

9/3/12. Outline. Part 5. Computational Complexity (1) Software cost factors. Software cost factors (cont d) Algorithm and Computational Complexity

Algorithms: Efficiency, Analysis, techniques for solving problems using computer approach or methodology but not programming

Homogeneous and Non Homogeneous Algorithms

Data Structures, Algorithms & Data Science Platforms

Announcements. CSEP 521 Applied Algorithms. Announcements. Polynomial time efficiency. Definitions of efficiency 1/14/2013

Solutions. (a) Claim: A d-ary tree of height h has at most 1 + d +...

UNIT 1 BASICS OF AN ALGORITHM

Today s Outline. CSE 326: Data Structures Asymptotic Analysis. Analyzing Algorithms. Analyzing Algorithms: Why Bother? Hannah Takes a Break

Algorithms A Look At Efficiency

Algorithm Analysis. College of Computing & Information Technology King Abdulaziz University. CPCS-204 Data Structures I

1 Introduction. 2 InsertionSort. 2.1 Correctness of InsertionSort

Transcription:

Applied Algorithmics COMP526 Lecturer: Leszek Gąsieniec, 321 (Ashton Bldg), L.A.Gasieniec@liverpool.ac.uk Lectures: Mondays 4pm (BROD-107), and Tuesdays 3+4pm (BROD-305a) Office hours: TBA, 321 (Ashton) Assessments (25%) + final exam (75%) http://www.csc.liv.ac.uk/~leszek/comp526/ Algorithm Analysis We are interested in the design of good algorithms and data structures Algorithm is a step-by-step procedure that performs tasks in a finite amount of time Data structure is a system of fixed rules of how to organize and access stored data 29/01/2018 Applied Algorithmics 2 29/01/2018 Applied Algorithmics 3 Algorithm Analysis Algorithm Analysis via experiments Primary interest: the running time (time complexity) of algorithms and operations defined on data structures Secondary interest: space usage (space complexity) In more complex models (e.g., distributed systems or networks) we also use other measures, e.g., the number of exchanged messages (communication complexity) We need some mathematics to describe running times and compare efficiency of algorithms The main emphasis is on finding dependency of the running time on the size of the input In order to determine this, we can perform several well designed experiments This type of analysis requires a good choice of sample inputs and appropriate number of tests (statistical certainty) 29/01/2018 Applied Algorithmics 4 29/01/2018 Applied Algorithmics 5

Experimental Analysis The running time depends on the size and the instance of the input but also the hardware environment (lack of universality!) Experimental Analysis Experiments can be performed only on a limited set of test inputs All experiments should be performed in the same hardware and software environment The actual implementation and execution of the algorithm(s) is required 29/01/2018 Applied Algorithmics 6 29/01/2018 Applied Algorithmics 7 Theoretical Analysis Takes into account all possible inputs Evaluation of relative efficiency of any two algorithms is independent from hard/software environment Performed by studying high-level description of the algorithm Theoretical Analysis This abstract methodology aims at associating with each algorithm a function f(n) that characterizes (provides as accurate as possible bounds on) the running time of the algorithm in terms of the input size n Typical functions include n, n 2, n log n, 29/01/2018 Applied Algorithmics 8 29/01/2018 Applied Algorithmics 9

Theoretical Analysis requires Pseudo-Code A formal language for describing algorithms A computational model in which considered algorithms are analysed and compared. An accurate metric for measuring algorithm running time, space usage, communication, An approach for characterizing running times Description of algorithms that is formal but for human eyes only Description of algorithms that is more structured than regular prose Description that facilitates the high-level analysis of a data structures and algorithms 29/01/2018 Applied Algorithmics 10 29/01/2018 Applied Algorithmics 11 Pseudo-Code example Finding the maximum element Pseudo-Code Pseudo-code is a mixture of natural languages and high-level programming (Ada, Pascal, C++, Java like) constructs Pseudo-code describes the main ideas behind generic implementation of data structures and algorithms Pseudo-code constructions include: expressions, declarations, decision structures, loops, arrays, methods of calls, 29/01/2018 Applied Algorithmics 12 29/01/2018 Applied Algorithmics 13

Computational Model Set of high-level primitive operations that can be found in the pseudo-code includes: assigning a value, calling method, performing an arithmetic operation, comparing two numbers, array indexing, following object reference, returning from a method Time complexity refers to counting the number of primitive operations that are executed Random Access Machine (RAM) CPU connected to a bank of memory cells Each memory cell can store a number, a character string, or an address, i.e., the value of a base type We assume that any primitive operation can be performed in constant time 29/01/2018 Applied Algorithmics 14 29/01/2018 Applied Algorithmics 15 Counting Primitive Operations Average vs. Worst Case Algorithm may run faster on some inputs and slower on the others Average case refers to the running time of an algorithm as an average taken over all inputs of the same size Worst case refers to the running time of an algorithm as the maximum taken over all inputs of the same size 29/01/2018 Applied Algorithmics 16 29/01/2018 Applied Algorithmics 17

Worst vs. Average Case Asymptotic Notation Asymptotic notation allows to characterize the main factors (computational components) affecting an algorithm s running time It also allows a simplified analysis that estimates the number of primitive operations executed up to a constant factor 29/01/2018 Applied Algorithmics 18 29/01/2018 Applied Algorithmics 19 Big-Oh Notation f(n), g(n) positive integer functions We say f(n) is O(g(n)) if there is real constant c, s.t., f(n) < c g(n), for n > n 0. Example: 7n -2 = O(n) We have to find constants c and n 0, s.t., 7n 2 < c n, for all n > n 0 A possible choice is c = 7 and n 0 = 1 In fact this is one of infinitely many possible choices, because any real number c > 7 and any integer n 0 > 1 would serve as well 29/01/2018 Applied Algorithmics 20 29/01/2018 Applied Algorithmics 21

Further Examples Frequent Functions 20 n 3 +10 n log n +5 is O(n 3 ), and any polynomial a k n k + +a 1 n 1 +a 0 is O(n k ) 3 log n + loglog n is O(log n) 2 100 is O(1) 5/n is O(1/n) Logarithmic O(log n) Linear O(n) Quadratic O(n 2 ) Polynomial O(n k ), k > 2 Exponential O(a n ), a > 1 29/01/2018 Applied Algorithmics 22 29/01/2018 Applied Algorithmics 23 Relatives of Big-Oh Two Examples f(n), g(n) positive integer functions We say that f(n) is Ω(g(n)) (big-omega) if there is a real const. c, s.t., f(n) > c g(n), for n > n 0. We say that f(n) is Θ(g(n)) (big-theta) if f(n) is Ω(g(n)) and f(n) is O(g(n)) ¾ logn + loglog n is Ω(log n) 3 logn + loglog n is Θ(log n) More examples will be studied at practicals 29/01/2018 Applied Algorithmics 24 29/01/2018 Applied Algorithmics 25

Importance of Asymptotics The maximum size allowed for an input instance for various running times to be solved in 1sec, 1min and 1h Growth Rate (running time) Functions ordered by growth rate: log n, log 2 n, n 1/2, n, n log n, n 2, n 3, 2 n 29/01/2018 Applied Algorithmics 26 29/01/2018 Applied Algorithmics 27 Typical Justification Techniques Mathematical Induction Proof by counter-example negative, frequently used in testing Proof by contra-positive argument uses observation: (A => B) (~B => ~A) Proof by contradiction uses observation: (A => B) ((A and ~B) => ~A) Proof by mathematical induction direct, used to justify iterative and recursive solutions Used to prove some property P(i) of analysed program for all (or arbitrarily large) integers The proof is performed as follows: Prove P( ) for some small integer, e.g., P(1) Prove that P(1),.., P(i-1) plus all other knowledge we have imply P(i) Then it follows that P(i) holds for all integers i 29/01/2018 Applied Algorithmics 28 29/01/2018 Applied Algorithmics 29

Loop Invariant Method Used to prove correctness of loops The proof is performed as follows: Find an invariant I(i) for considered loop, where i is the loop index iterated from 1 to F(i) Prove that I(1) holds just before the 1 st loop test Prove that from I(1),.., I(i-1) and the content of the loop it follows that I(i) holds too Show that from stop condition F(i) and invariant I(i) we get the desired solution S(i) 29/01/2018 Applied Algorithmics 30 Example of Invariant Method Stop condition F(i) (i=n) Loop invariant I(i) currentmax A[0],..,A[i-1] imply Desired solution S(n) currentmax A[0],..,A[n-1] I.e., currentmax contains the maximum element in A 29/01/2018 Applied Algorithmics 31 and Decreasing Function Method Mainly used to prove that a loop stops The proof is preformed as follows Find a positive (potential) function f(i), where i is the loop index iterated from 1 to F(i) with bounded original value Show that each iteration of the loop decreases the value of f(i) but it always remains positive This shows that the number of loop iterations is bounded and that the loop stops eventually Example of Decreasing Function and Stop condition F(i) (i=n) Decreasing function f(i) = (n+1-i) During each iteration f(i) is decreased by 1 But it always remains positive with f(i=n)=1 29/01/2018 Applied Algorithmics 32 29/01/2018 Applied Algorithmics 33