Scientific Computing. Algorithm Analysis

Similar documents
Algorithms + Data Structures Programs

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

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

PROGRAM EFFICIENCY & COMPLEXITY ANALYSIS

Sorting & Growth of Functions

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

Lecture 1. Introduction / Insertion Sort / Merge Sort

Data Structures Lecture 8

ECS122A Lecture Notes on Algorithm Design and Analysis

Theory and Algorithms Introduction: insertion sort, merge sort

Data Structures and Algorithms CSE 465

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

Divide and Conquer. Design and Analysis of Algorithms Andrei Bulatov

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

CS4311 Design and Analysis of Algorithms. Lecture 1: Getting Started

Analysis of Algorithms

CS2351 Data Structures. Lecture 1: Getting Started

Introduction to Algorithms 6.046J/18.401J

Algorithmic Analysis. Go go Big O(h)!

Introduction to the Analysis of Algorithms. Algorithm

UNIT 1 ANALYSIS OF ALGORITHMS

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

Analysis of Algorithm. Chapter 2

SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY. Lecture 11 CS2110 Spring 2016

SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY

Lecture 2: Algorithm Analysis

Asymptotic Analysis of Algorithms

ASYMPTOTIC COMPLEXITY

6/12/2013. Introduction to Algorithms (2 nd edition) Overview. The Sorting Problem. Chapter 2: Getting Started. by Cormen, Leiserson, Rivest & Stein

Introduction to Data Structure

Introduction to Algorithms 6.046J/18.401J/SMA5503

Analysis of Sorting Algorithms. Imagine you have a few thousand dollars in your safe in all different denominations. The

Outline and Reading. Analysis of Algorithms 1

Algorithm Analysis and Design

What is an Algorithm?

ASYMPTOTIC COMPLEXITY

Introduction to Algorithms 6.046J/18.401J

Checking for duplicates Maximum density Battling computers and algorithms Barometer Instructions Big O expressions. John Edgar 2

CSE 373 APRIL 3 RD ALGORITHM ANALYSIS

Introduction to Computers & Programming

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

Choice of C++ as Language

Introduction to Computers and Programming. Today

Programming in Haskell Aug-Nov 2015

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

Algorithm Performance. (the Big-O)

Chapter 2: Complexity Analysis

Algorithm Analysis. CENG 707 Data Structures and Algorithms

Assignment 1 (concept): Solutions

Chapter 6 INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS

Computer Algorithms. Introduction to Algorithm

CS:3330 (22c:31) Algorithms

Lecture 2: Getting Started

RUNNING TIME ANALYSIS. Problem Solving with Computers-II

Run Times. Efficiency Issues. Run Times cont d. More on O( ) notation

Algorithm Analysis. Spring Semester 2007 Programming and Data Structure 1

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

Computer Science 210 Data Structures Siena College Fall Topic Notes: Complexity and Asymptotic Analysis

How do we compare algorithms meaningfully? (i.e.) the same algorithm will 1) run at different speeds 2) require different amounts of space

CMPSCI 187: Programming With Data Structures. Lecture 5: Analysis of Algorithms Overview 16 September 2011

Data Structures and Algorithms

CS583 Lecture 01. Jana Kosecka. some materials here are based on Profs. E. Demaine, D. Luebke A.Shehu, J-M. Lien and Prof. Wang s past lecture notes

Elementary maths for GMT. Algorithm analysis Part I

CS302 Topic: Algorithm Analysis #2. Thursday, Sept. 21, 2006

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

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

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

Lecture Notes on Sorting

Introduction to Algorithms

What is an algorithm?

Lecture 5 Sorting Arrays

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

CS 6402 DESIGN AND ANALYSIS OF ALGORITHMS QUESTION BANK

Quicksort (Weiss chapter 8.6)

Copyright 2009, Artur Czumaj 1

SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY. Lecture 11 CS2110 Fall 2014

Mergesort again. 1. Split the list into two equal parts

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

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

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

THE UNIVERSITY OF WESTERN AUSTRALIA

Algorithm Analysis. Fall 2013 Carola Wenk

1 Introduction. 2 InsertionSort. 2.1 Correctness of InsertionSort

CS 506, Sect 002 Homework 5 Dr. David Nassimi Foundations of CS Due: Week 11, Mon. Apr. 7 Spring 2014

CSCI 261 Computer Science II

Introduction to Computer Science

Introduction to Computer Algorithms Lecture Notes (undergraduate CS470 course) Grzegorz Malewicz

Chapter 2: Algorithm Complexity Analysis. Chaochun Wei Spring 2018

Data Structures and Algorithms. Part 2

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

Scribe: Sam Keller (2015), Seth Hildick-Smith (2016), G. Valiant (2017) Date: January 25, 2017

Lecture Notes for Chapter 2: Getting Started

DATA STRUCTURES AND ALGORITHMS. Asymptotic analysis of the algorithms

CS126 Final Exam Review

Introduction to Algorithms and Complexity Theory

Algorithm Complexity Analysis: Big-O Notation (Chapter 10.4) Dr. Yingwu Zhu

COMP171 Data Structures and Algorithms Fall 2006 Midterm Examination

DIVIDE & CONQUER. Problem of size n. Solution to sub problem 1

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

L6,7: Time & Space Complexity

Transcription:

ECE257 Numerical Methods and Scientific Computing Algorithm Analysis

Today s s class: Introduction to algorithm analysis Growth of functions

Introduction What is an algorithm? A sequence of computation steps that takes a set of values as input and produces a set of values as output A method for solving a specific computational problem Many different algorithms may exist for the same problem and they may different characteristics in terms of resources consumed Time Memory Bandwidth Area

Algorithm Analysis How do you predict the resources that an algorithm uses? For the most part, we are concerned with time - the overall computational speed of the algorithm For numerical applications, memory can be also be a concern

Algorithm Analysis Use a high-level pseudocode representation of the algorithm To get an estimate of time, we need to count the number of operations needed to complete the algorithm The number of operations will be a function of n where n is the size of the input Allows us to characterize the performance of an algorithm independent of the implementation and runtime platform

Algorithm Analysis What is the input size? Depends on the problem Examples Size of an array to be sorted Number of bits in two numbers being multiplied Number of edges and vertices in a graph

Algorithm Analysis What operations do we count? Primitive operations Doing a comparison Assigning a value Evaluating an arithmetic expression Calling a subroutine In general, count a line of pseudocode as an operation Assume that each operation takes constant time Constant time means that the operation time is not dependent on the input size

Sorting algorithms Problem: Sort an array of numbers in ascending order Several algorithms Insertion sort Merge sort Quick sort

Insertion sort Start at the beginning of the array and move towards the end of the array Insert each array element that you encounter back into the array in ordered fashion Requires comparisons with each element of the sorted part of the array

Insertion sort 6 5 9 7 8 3 4 6 5 9 7 8 3 4 5 6 9 7 8 3 4 5 6 9 7 8 3 4 5 6 7 9 8 3 4 5 6 7 8 9 3 4

Insertion sort Cost #times for j=2 to n k = A[j] i = j-1 while i > 0 and A[i] > k A[i+1] = A[i] i = i-1 A[i+1] = k c 1 n c 2 n-1 c 3 n-1 c 4 c 5 c 6 j= 2 n j= 2 t j c 7 n-1 n j= 2 n ( t j 1) ( t j 1) n T(n) = c 1 n + c 2 (n 1) + c 3 (n 1) + c 4 t j + c 5 t j 1 + c 6 t j 1 + c 7 (n 1) j= 2 n j= 2 ( ) n j= 2 ( )

Insertion sort n T(n) = c 1 n + c 2 (n 1) + c 3 (n 1) + c 4 t j + c 5 t j 1 + c 6 t j 1 + c 7 (n 1) j= 2 Best case, already sorted t j =1 n j= 2 ( ) T(n) = c 1 n + c 2 (n 1) + c 3 (n 1) + c 4 (n 1) + c 7 (n 1) n j= 2 ( ) = an + b a = c 1 + c 2 + c 3 + c 4 + c 7,b = c 2 + c 3 + c 4 + c 7 Worst case, reverse sort t j = j n(n +1) T(n) = c 1 n + c 2 (n 1) + c 3 (n 1) + c 4 1 2 n(n 1) n(n 1) +c 5 + c 6 + c 7 (n 1) 2 2 = an 2 + bn + c

Algorithm analysis Worst-case running time is more meaningful since it gives an upper bound on the running Does the equation give us an accurate estimate of the running time? What are the constants? Do we care?

Algorithm Analysis The actual precise running time is not that important especially since we havent said anything about the implementation or runtime platform What s s more interesting to analyze is the order of growth with respect to the input size When looking at the order of growth, we don t t care about the constants An algorithm is considered more efficient than another if it has a lower worst-case order of growth

Order of growth Use O(g(n)) notation to represent order of growth O(g(n)) = {f(n){ : there exist positive constants c and n 0 such that 0 f(n) f(n) cg(n) for all n > n 0 } O(g(n)) is the upper bound on the worst case running time of the function f(n)

Order of growth Use the highest degree term in a polynomial f(n) If f(n) is a sum, use the larger term No constants in g(n)

Order of growth Linear order of growth f(n) = an + b => O(n) = f(n) Quadratic order of growth f(n) = an 2 + bn +c => O(n 2 ) = f(n) Insertion sort is an O(n 2 ) algorithm

Merge sort Recursive divide-and-conquer approach merge_sort(a,p,r) if p < r q = (p+r)/2 merge_sort(a,p,q) merge_sort(a,q+1,r) merge(a,p,q,r)

Merge sort 6 5 9 7 8 3 4 1 5 6 7 9 8 3 1 4 5 6 7 9 1 3 4 8 1 3 4 5 6 7 8 9

Merge sort Assume that the run time of merge_sort is T(n) The merge routine is O(n) Then, we can write the following recurrence O(1) if n =1 T(n) = 2T n + O(n) if n >1 2

Merge sort 6 5 9 7 8 3 4 1 8 n/8 5 6 7 9 8 3 1 4 4 n/4 5 6 7 9 1 3 4 8 2 n/2 1 3 4 5 6 7 8 9 n n log 2 n

Merge sort Merge sort has an asymptotic upper bound on the running time of O(n log n) Since n log n < n 2 as n gets large, we can say that merge sort is more efficient than insertion sort

Algorithm analysis Order of growth analysis allows us to compare different algorithms for a problem 1 < log n < n < n log n < n 2 < 2 n < n!

Algorithm Analysis 70 60 50 40 30 20 O(1) O(log n) O(n) O(n log n) O(n^2) O(2^n) 10 0 0 1 2 3 4 5 6 7

Algorithm Analysis Why does order of growth matter? With the wrong algorithm, a supercomputer can be slower than an old PC Eg. Highly optimized insertion sort on a 10 BIPS supercomputer may take 2n 2 instructions. Poorly optimized merge sort on a 100 MIPS PC takes 50 n log n instructions. For n=10 7, the supercomputer will take roughly 5 1/2 hours, while the PC will take less than 17 minutes.

Next Lecture Error Analysis Read Chapter 3