September 17, 2001 Algorithms and Data Structures Lecture II Simonas Šaltenis Nykredit Center for Database Research Aalborg University

Slides:



Advertisements
Similar presentations
Back to Sorting – More efficient sorting algorithms.
Advertisements

Analysis of Algorithms
Comp 122, Spring 2004 Divide and Conquer (Merge Sort)
September 12, Algorithms and Data Structures Lecture III Simonas Šaltenis Nykredit Center for Database Research Aalborg University
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.
DIVIDE AND CONQUER APPROACH. General Method Works on the approach of dividing a given problem into smaller sub problems (ideally of same size).  Divide.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 5.
Spring 2015 Lecture 5: QuickSort & Selection
Sorting. Input: A sequence of n numbers a 1, …, a n Output: A reordering a 1 ’, …, a n ’, such that a 1 ’ < … < a n ’
Lecture 3 Nearest Neighbor Algorithms Shang-Hua Teng.
CS421 - Course Information Website Syllabus Schedule The Book:
Lecture 2: Divide and Conquer I: Merge-Sort and Master Theorem Shang-Hua Teng.
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.
25 June 2015Comp 122, Spring 2004 Asymptotic Notation, Review of Functions & Summations.
Fall 2008 Insertion Sort – review of loop invariants.
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.
Analysis of Algorithms COMP171 Fall Analysis of Algorithms / Slide 2 Introduction * What is Algorithm? n a clearly specified set of simple instructions.
Introduction to Algorithm design and analysis
October 1, Algorithms and Data Structures Lecture III Simonas Šaltenis Nykredit Center for Database Research Aalborg University
CSC2105: Algorithms Mashiour Rahman American International University Bangladesh.
Introduction Data Structures & Algorithm Data Structures & Algorithm.
Algorithm Efficiency CS 110: Data Structures and Algorithms First Semester,
Algorithm Correctness A correct algorithm is one in which every valid input instance produces the correct output. The correctness must be proved mathematically.
10/13/20151 CS 3343: Analysis of Algorithms Lecture 9: Review for midterm 1 Analysis of quick sort.
2IL50 Data Structures Fall 2015 Lecture 2: Analysis of Algorithms.
10/14/ Algorithms1 Algorithms - Ch2 - Sorting.
Lecture 2 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Program Efficiency & Complexity Analysis. Algorithm Review An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm.
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
COSC 3101A - Design and Analysis of Algorithms 2 Asymptotic Notations Continued Proof of Correctness: Loop Invariant Designing Algorithms: Divide and Conquer.
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.
Spring 2015 Lecture 2: Analysis of Algorithms
Introduction to Algorithms (2 nd edition) by Cormen, Leiserson, Rivest & Stein Chapter 2: Getting Started.
Algorithms A well-defined computational procedure that takes some value as input and produces some value as output. (Also, a sequence of computational.
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.
2IL50 Data Structures Spring 2016 Lecture 2: Analysis of Algorithms.
CSC317 1 So far so good, but can we do better? Yes, cheaper by halves... orkbook/cheaperbyhalf.html.
A Introduction to Computing II Lecture 7: Sorting 1 Fall Session 2000.
Asymptotic Notation Faculty Name: Ruhi Fatima
Big O David Kauchak cs302 Spring Administrative Assignment 1: how’d it go? Assignment 2: out soon… Lab code.
1 Computer Algorithms Tutorial 2 Mathematical Induction Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR.
September 10, 2001 Algorithms and Data Structures Simonas Šaltenis Nykredit Center for Database Research Aalborg University
September 18, Algorithms and Data Structures Lecture II Simonas Šaltenis Aalborg University
CS6045: Advanced Algorithms Sorting Algorithms. Sorting Input: sequence of numbers Output: a sorted sequence.
M. Böhlen and R. Sebastiani 9/29/20161 Data Structures and Algorithms Roberto Sebastiani
Lecture 2 Algorithm Analysis
Analysis of Algorithms CS 477/677
CS 3343: Analysis of Algorithms
CS 3343: Analysis of Algorithms
CS 3343: Analysis of Algorithms
Asymptotic Notations Algorithms Lecture 9.
Divide and Conquer (Merge Sort)
Algorithms and Data Structures Lecture III
CS200: Algorithms Analysis
Divide and Conquer Algorithms Part I
Divide and Conquer (Merge Sort)
Algorithms: the big picture
Math/CSE 1019N: Discrete Mathematics for Computer Science Winter 2007
Introduction To Algorithms
nalysis of lgorithms A A Universidad Nacional de Colombia
Algorithms and Data Structures Lecture III
Algorithms and Data Structures Lecture II
Presentation transcript:

September 17, 2001 Algorithms and Data Structures Lecture II Simonas Šaltenis Nykredit Center for Database Research Aalborg University

September 17, 2001 This Lecture Correctness of algorithms Growth of functions and asymptotic notation Some basic math revisited Divide and conquer example – the merge sort

September 17, 2001 Correctness of Algorithms The algorithm is correct if for any legal input it terminates and produces the desired output. Automatic proof of correctness is not possible But there are practical techniques and rigorous formalisms that help to reason about the correctness of algorithms

September 17, 2001 Partial and Total Correctness Partial correctness Any legal input Algorithm Output IF this point is reached, THEN this is the desired output Total correctness Any legal input Algorithm Output INDEED this point is reached, AND this is the desired output

September 17, 2001 Assertions To prove partial correctness we associate a number of assertions (statements about the state of the execution) with specific checkpoints in the algorithm. E.g., A[1], …, A[k] form an increasing sequence Preconditions – assertions that must be valid before the execution of an algorithm or a subroutine Postconditions – assertions that must be valid after the execution of an algorithm or a subroutine

September 17, 2001 Loop Invariants Invariants – assertions that are valid any time they are reached (many times during the execution of an algorithm, e.g., in loops) We must show three things about loop invariants: Initialization – it is true prior to the first iteration Maintenance – if it is true before an iteration, it remains true before the next iteration Termination – when loop terminates the invariant gives a useful property to show the correctness of the algorithm

September 17, 2001 Example of Loop Invariants (1) Invariant: at the start of each for loop, A[1…j- 1] consists of elements originally in A[1…j-1] but in sorted order for j=2 to length(A) do key=A[j] i=j-1 while i>0 and A[i]>key do A[i+1]=A[i] i-- A[i+1]:=key for j=2 to length(A) do key=A[j] i=j-1 while i>0 and A[i]>key do A[i+1]=A[i] i-- A[i+1]:=key

September 17, 2001 Example of Loop Invariants (2) Invariant: at the start of each for loop, A[1…j- 1] consists of elements originally in A[1…j-1] but in sorted order for j=2 to length(A) do key=A[j] i=j-1 while i>0 and A[i]>key do A[i+1]=A[i] i-- A[i+1]:=key for j=2 to length(A) do key=A[j] i=j-1 while i>0 and A[i]>key do A[i+1]=A[i] i-- A[i+1]:=key Initialization: j = 2, the invariant trivially holds because A[1] is a sorted array

September 17, 2001 Example of Loop Invariants (3) Invariant: at the start of each for loop, A[1…j- 1] consists of elements originally in A[1…j-1] but in sorted order for j=2 to length(A) do key=A[j] i=j-1 while i>0 and A[i]>key do A[i+1]=A[i] i-- A[i+1]:=key for j=2 to length(A) do key=A[j] i=j-1 while i>0 and A[i]>key do A[i+1]=A[i] i-- A[i+1]:=key Maintenance: the inner while loop moves elements A[j- 1], A[j-2], …, A[j-k] one position right without changing their order. Then the former A[j] element is inserted into k-th position so that A[k-1]  A[k]  A[k+1]. A[1…j-1] sorted + A[j]  A[1…j] sorted

September 17, 2001 Example of Loop Invariants (4) Invariant: at the start of each for loop, A[1…j- 1] consists of elements originally in A[1…j-1] but in sorted order for j=2 to length(A) do key=A[j] i=j-1 while i>0 and A[i]>key do A[i+1]=A[i] i-- A[i+1]:=key for j=2 to length(A) do key=A[j] i=j-1 while i>0 and A[i]>key do A[i+1]=A[i] i-- A[i+1]:=key Termination: the loop terminates, when j=n+1. Then the invariant states: “A[1…n] consists of elements originally in A[1…n] but in sorted order”

September 17, 2001 Asymptotic Analysis Goal: to simplify analysis of running time by getting rid of ”details”, which may be affected by specific implementation and hardware like “rounding”: 1,000,001  1,000,000 3n 2  n 2 Capturing the essence: how the running time of an algorithm increases with the size of the input in the limit. Asymptotically more efficient algorithms are best for all but small inputs

September 17, 2001 Asymptotic Notation The “big-Oh” O-Notation asymptotic upper bound f(n) = O(g(n)), if there exists constants c and n 0, s.t. f(n)  c g(n) for n  n 0 f(n) and g(n) are functions over non-negative integers Used for worst-case analysis Input Size Running Time

September 17, 2001 The “big-Omega”  Notation asymptotic lower bound f(n) =  (g(n)) if there exists constants c and n 0, s.t. c g(n)  f(n) for n  n 0 Used to describe best-case running times or lower bounds of algorithmic problems E.g., lower-bound of searching in an unsorted array is  (n). Input Size Running Time Asymptotic Notation (2)

September 17, 2001 Asymptotic Notation (3) Simple Rule: Drop lower order terms and constant factors. 50 n log n is O(n log n) 7n - 3 is O(n) 8n 2 log n + 5n 2 + n is O(n 2 log n) Note: Even though (50 n log n) is O(n 5 ), it is expected that such an approximation be of as small an order as possible

September 17, 2001 The “big-Theta”  Notation asymptoticly tight bound f(n) =  (g(n)) if there exists constants c 1, c 2, and n 0, s.t. c 1 g(n)  f(n)  c 2 g(n) for n  n 0 f(n) =  (g(n)) if and only if f(n) =  (g(n)) and f(n) =  (g(n)) O(f(n)) is often misused instead of  (f(n)) Input Size Running Time Asymptotic Notation (4)

September 17, 2001 Asymptotic Notation (5) Two more asymptotic notations "Little-Oh" notation f(n)=o(g(n)) non-tight analogue of Big-Oh For every c, there should exist n 0, s.t. f(n)  c g(n) for n  n 0 Used for comparisons of running times. If f(n)=o(g(n)), it is said that g(n) dominates f(n). "Little-omega" notation f(n)=  (g(n)) non-tight analogue of Big-Omega

September 17, 2001 Asymptotic Notation (6) Analogy with real numbers f(n) = O(g(n))  f  g f(n) =  (g(n))  f  g f(n) =  (g(n))  f  g f(n) = o(g(n))  f  g f(n) =  (g(n))  f  g Abuse of notation: f(n) = O(g(n)) actually means f(n)  O(g(n))

September 17, 2001 A Quick Math Review Geometric progression given an integer n 0 and a real number 0< a  1 geometric progressions exhibit exponential growth Arithmetic progression

September 17, 2001 A Quick Math Review (2)

September 17, 2001 Summations The running time of insertion sort is determined by a nested loop Nested loops correspond to summations for j  2 to length(A) key  A[j] i  j-1 while i>0 and A[i]>key A[i+1]  A[i] i  i-1 A[i+1]  key for j  2 to length(A) key  A[j] i  j-1 while i>0 and A[i]>key A[i+1]  A[i] i  i-1 A[i+1]  key

September 17, 2001 Proof by Induction We want to show that property P is true for all integers n  n 0 Basis: prove that P is true for n 0 Inductive step: prove that if P is true for all k such that n 0  k  n – 1 then P is also true for n Example Basis

September 17, 2001 Proof by Induction (2) Inductive Step

September 17, 2001 Divide and Conquer Divide and conquer method for algorithm design: Divide: If the input size is too large to deal with in a straightforward manner, divide the problem into two or more disjoint subproblems Conquer: Use divide and conquer recursively to solve the subproblems Combine: Take the solutions to the subproblems and “merge” these solutions into a solution for the original problem

September 17, 2001 MergeSort: Algorithm Divide: If S has at least two elements (nothing needs to be done if S has zero or one elements), remove all the elements from S and put them into two sequences, S 1 and S 2, each containing about half of the elements of S. (i.e. S 1 contains the first  n/2  elements and S 2 contains the remaining  n/2  elements. Conquer: Sort sequences S 1 and S 2 using MergeSort. Combine: Put back the elements into S by merging the sorted sequences S 1 and S 2 into one sorted sequence

September 17, 2001 Merge Sort: Algorithm Merge-Sort(A, p, r) if p < r then q  (p+r)/2 Merge-Sort(A, p, q) Merge-Sort(A, q+1, r) Merge(A, p, q, r) Merge-Sort(A, p, r) if p < r then q  (p+r)/2 Merge-Sort(A, p, q) Merge-Sort(A, q+1, r) Merge(A, p, q, r) Take the smallest of the two topmost elements of sequences A[p..q] and A[q+1..r] and put into the resulting sequence. Repeat this, until both sequences are empty. Copy the resulting sequence into A[p..r]. Merge(A, p, q, r) Take the smallest of the two topmost elements of sequences A[p..q] and A[q+1..r] and put into the resulting sequence. Repeat this, until both sequences are empty. Copy the resulting sequence into A[p..r].

September 17, 2001 MergeSort (Example) - 1

September 17, 2001 MergeSort (Example) - 2

September 17, 2001 MergeSort (Example) - 3

September 17, 2001 MergeSort (Example) - 4

September 17, 2001 MergeSort (Example) - 5

September 17, 2001 MergeSort (Example) - 6

September 17, 2001 MergeSort (Example) - 7

September 17, 2001 MergeSort (Example) - 8

September 17, 2001 MergeSort (Example) - 9

September 17, 2001 MergeSort (Example) - 10

September 17, 2001 MergeSort (Example) - 11

September 17, 2001 MergeSort (Example) - 12

September 17, 2001 MergeSort (Example) - 13

September 17, 2001 MergeSort (Example) - 14

September 17, 2001 MergeSort (Example) - 15

September 17, 2001 MergeSort (Example) - 16

September 17, 2001 MergeSort (Example) - 17

September 17, 2001 MergeSort (Example) - 18

September 17, 2001 MergeSort (Example) - 19

September 17, 2001 MergeSort (Example) - 20

September 17, 2001 MergeSort (Example) - 21

September 17, 2001 MergeSort (Example) - 22

September 17, 2001 Recurrences Recursive calls in algorithms can be described using recurrences A recurrence is an equation or inequality that describes a function in terms of its value on smaller inputs Example: Merge Sort

September 17, 2001 Next Week Solving recurrences