Lecture 2 Algorithm Analysis

Slides:



Advertisements
Similar presentations
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Advertisements

1 Divide & Conquer Algorithms. 2 Recursion Review A function that calls itself either directly or indirectly through another function Recursive solutions.
Divide-and-Conquer Recursive in structure –Divide the problem into several smaller sub-problems that are similar to the original but smaller in size –Conquer.
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.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 5.
Spring 2015 Lecture 5: QuickSort & Selection
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
CS421 - Course Information Website Syllabus Schedule The Book:
Lecture 2: Divide and Conquer I: Merge-Sort and Master Theorem Shang-Hua Teng.
CS3381 Des & Anal of Alg ( SemA) City Univ of HK / Dept of CS / Helena Wong 2. Analysis of Algorithms - 1 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.
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,
Unit 1. Sorting and Divide and Conquer. Lecture 1 Introduction to Algorithm and Sorting.
Introduction to Algorithm design and analysis
HOW TO SOLVE IT? Algorithms. An Algorithm An algorithm is any well-defined (computational) procedure that takes some value, or set of values, as input.
10/14/ Algorithms1 Algorithms - Ch2 - Sorting.
ECOE 456/556: Algorithms and Computational Complexity Lecture 1 Serdar Taşıran.
Lecture 2 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
1 Designing algorithms There are many ways to design an algorithm. Insertion sort uses an incremental approach: having sorted the sub-array A[1…j - 1],
CMPT 438 Algorithms. Why Study Algorithms? Necessary in any computer programming problem ▫Improve algorithm efficiency: run faster, process more data,
ALGORITHMS THIRD YEAR BANHA UNIVERSITY FACULTY OF COMPUTERS AND INFORMATIC Lecture three Dr. Hamdy M. Mousa.
Getting Started Introduction to Algorithms Jeff Chastine.
September 17, 2001 Algorithms and Data Structures Lecture II Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Divide-and-Conquer UNC Chapel HillZ. Guo. Divide-and-Conquer It’s a technique instead of an algorithm Recursive in structure – Divide the problem into.
1/6/20161 CS 3343: Analysis of Algorithms Lecture 2: Asymptotic Notations.
Introduction to Algorithms (2 nd edition) by Cormen, Leiserson, Rivest & Stein Chapter 2: Getting Started.
Introduction to Complexity Analysis. Computer Science, Silpakorn University 2 Complexity of Algorithm algorithm คือ ขั้นตอนการคำนวณ ที่ถูกนิยามไว้อย่างชัดเจนโดยจะ.
Sorting. Sorting Sorting is important! Things that would be much more difficult without sorting: –finding a telephone number –looking up a word in the.
2IS80 Fundamentals of Informatics Fall 2015 Lecture 6: Sorting and Searching.
1 Ch. 2: Getting Started. 2 About this lecture Study a few simple algorithms for sorting – Insertion Sort – Selection Sort (Exercise) – Merge Sort Show.
Lecture 2 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
CSC317 1 So far so good, but can we do better? Yes, cheaper by halves... orkbook/cheaperbyhalf.html.
 Design and Analysis of Algorithms تصميم وتحليل الخوارزميات (311 عال) Chapter 2 Sorting (insertion Sort, Merge Sort)
Big O David Kauchak cs302 Spring Administrative Assignment 1: how’d it go? Assignment 2: out soon… Lab code.
CS6045: Advanced Algorithms Sorting Algorithms. Sorting Input: sequence of numbers Output: a sorted sequence.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 4.
ECOE 456/556: Algorithms and Computational Complexity
CMPT 438 Algorithms.
Analysis of Algorithms CS 477/677
UNIT- I Problem solving and Algorithmic Analysis
Unit 1. Sorting and Divide and Conquer
CS 3343: Analysis of Algorithms
Introduction to Algorithms (2nd edition)
Lecture 4 Divide-and-Conquer
CS 3343: Analysis of Algorithms
CS 3343: Analysis of Algorithms
CS 3343: Analysis of Algorithms
Analysis of Algorithms CS 477/677
Lecture 5 Algorithm Analysis
Asymptotic Notations Algorithms Lecture 9.
Ch 7: Quicksort Ming-Te Chi
Lecture 5 Algorithm Analysis
Divide and Conquer (Merge Sort)
Ch 2: Getting Started Ming-Te Chi
Lecture No 6 Advance Analysis of Institute of Southern Punjab Multan
CS200: Algorithms Analysis
Divide and Conquer (Merge Sort)
Algorithms: the big picture
Divide-and-Conquer 7 2  9 4   2   4   7
Ch. 2: Getting Started.
Divide & Conquer Algorithms
Analysis of Algorithms
Introduction To Algorithms
David Kauchak cs161 Summer 2009
The Selection Problem.
Quicksort Quick sort Correctness of partition - loop invariant
Lecture 5 Algorithm Analysis
Algorithms and Data Structures Lecture II
Presentation transcript:

Lecture 2 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea

Overview 2 algorithms for sorting of numbers are presented Divide-and-Conquer strategy Growth of functions / asymptotic notation Algorithm Analysis

Sorting of Numbers Input A sequence of n numbers [a1, a2,..., an] Output A permutation (reordering) [a’1, a’2,..., a’n] of the input sequence such that a’1 a’2 ...  a’n Algorithm Analysis

Sorting a hand of cards Algorithm Analysis

The insertion sort algorithm Algorithm Analysis

Correctness of insertion sort Loop invariants – for proving that some algorithm is correct Three things must be showed about a loop invariant: Initialization: It is true prior to the first iteration of the loop Maintenance: If it is true before an iteration of the loop, it remains true before the next iteration Termination: When the loop terminates, the invariant gives us a useful property that helps show that the algorithm is correct Algorithm Analysis

Loop invariant of insertion sort At the start of each iteration of the for loop of lines 1-8, the subarray A[1..j-1] consists of the elements originally in A[1..j-1] but in sorted order Algorithm Analysis

Analysing algorithms Input size = number of items (numbers) to be sorted We count the number of comparisons Algorithm Analysis

Insertion sort / best-case In the best-case (the input sequence is already sorted) insertion sort requires n-1 comparisons Algorithm Analysis

Insertion sort / worst-case The input sequence is in reverse sorted order We need comparisons Algorithm Analysis

Worst-case vs. average case Worst-case running time of an algorithm is an upper bound on the running time for any input For some algorithms, the worst case occurs fairly often. The „average case“ is often roughly as bad as the worst case. Algorithm Analysis

Asymptotic Notation and Complexity Growth of Functions Asymptotic Notation and Complexity

Asymptotic upper bound Algorithm Analysis

Asymptotic upper bound (cont.) Example of application: Description of what is the required number of operations of some algorithm in worst case situations. E.g. O(n2) , where n is the size of the input O(n2) means the algorithm has a “square behavior” in the worst case. The O-notation allows us to hide scalars/constants involved in the complexity description. Algorithm Analysis

Asymptotic lower bound Algorithm Analysis

Asymptotic lower bound (cont.) Examples of application: Description of what is the required number of operations of some algorithm in best case situations. E.g. Ω(n), where n is the size of the input Ω(n) means the algorithm has “linear behavior” in the best case. (The algorithm can still be O(n2) in worst case situations!) Description of what is the required number of operations in the worst case for any algorithm that solves some specific problem. Example: The required number of comparisons of any sorting algorithm in the algorithm’s worst case. (The worst case can be different for different algorithms that solve the same problem.) Algorithm Analysis

Asymptotically tight bound Algorithm Analysis

Asymptotic tight bound (cont.) Example of application: Simultaneous worst case and best case statement for some algorithm: E.g. θ(n2) expresses that some algorithm A shows a square behavior in the best case (A requires at least Ω(n2) many operations for all inputs) and at the same time it expresses that A will never need more than “square many” operations (the effort is limited by O(n2) many operations). Algorithm Analysis

Complexity of an algorithm Complexity expresses the counting of performed operations of an algorithm with respect to the size of the input: We can count only a single type of operations, e.g. the number of performed comparisons. We can count all operations performed by some algorithm. This complexity is called time complexity. Complexity may be (normally is) expressed by using asymptotic notation. Algorithm Analysis

Exercises With respect to the number of performed comparisons: What is the asymptotic upper bound of Insertion Sort ? What is the asymptotic lower bound of Insertion Sort ? Is there any asymptotically tight bound of Insertion Sort? If yes: What is the asymptotically tight bound? If no: Why is there no asymptotically tight bound? If we move from counting comparisons to the more general time complexity, then are there any differences with respect to the three bounds? Algorithm Analysis

Merge-Sort Algorithm

Example merge procedure Algorithm Analysis

Merge procedure Algorithm Analysis

Merging – Complexity for symmetrically sized inputs Symmetrically sized inputs (n/2, n/2) (here 2 times 4 elements) We compare 6 with all 5 and 8 (4 comparisons) We compare 8 with all 7 (3 comparisons) Generalized for n elements: Worst case requires n – 1 comparisons Exercise: Best case? Time complexity cn = Θ(n). 6 8 5 7 Algorithm Analysis

Correctness merge procedure Loop invariant: Algorithm Analysis

The divide-and-conquer approach Divide the problem into a number of subproblems. Conquer the subproblems by solving them recursively. If the subproblem sizes are small enough, however, just solve the subproblems in straightforward manner. Combine the solutions to the subproblems into the solution for the original problem Algorithm Analysis

Merge-sort algorithm Divide: Divide the n-element sequence to be sorted into two subsequences of n/2 elements each. Conquer: Sort the two subsequences recursively using merge sort. Combine: Merge the two sorted subsequences to produce the sorted answer. Algorithm Analysis

Merge-sort algorithm Algorithm Analysis

Example merge sort Algorithm Analysis

Analysis of Mergesort regarding Comparisons When n ≥ 2, for mergesort steps: Divide: Just compute q as the average of p and r ⇒ no comparisons Conquer: Recursively solve 2 subproblems, each of size n/2⇒2T (n/2). Combine: MERGE on an n-element subarray requires cn comparisons ⇒ cn = Θ(n). Algorithm Analysis

Analysis merge sort 2 Algorithm Analysis

Analysis merge sort 3 Algorithm Analysis

Mergesort recurrences Recurrence regarding comparisons (worst case) Recurrence time complexity: Both recurrences can be solved using the master-theorem: C C -1 Algorithm Analysis

Lower Bound for Sorting Is there some lower bound for the time complexity / number of comparisons with sorting? Answer: Yes! Ω(n log n) where n is the size of the input Later more about this topic …… Algorithm Analysis

Bubblesort Further popular sorting algorithm Algorithm Analysis