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

Slides:



Advertisements
Similar presentations
Analysis of Algorithms
Advertisements

CSE 373: Data Structures and Algorithms Lecture 5: Math Review/Asymptotic Analysis III 1.
1 ICS 353 Design and Analysis of Algorithms Spring Semester (062) King Fahd University of Petroleum & Minerals Information & Computer Science.
Introduction to Analysis of Algorithms
Sorting. Input: A sequence of n numbers a 1, …, a n Output: A reordering a 1 ’, …, a n ’, such that a 1 ’ < … < a n ’
CS421 - Course Information Website Syllabus Schedule The Book:
Not all algorithms are created equally Insertion of words from a dictionary into a sorted list takes a very long time. Insertion of the same words into.
Analysis of Algorithms
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.
Algorithm Analysis CS 201 Fundamental Structures of Computer Science.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 2 Elements of complexity analysis Performance and efficiency Motivation: analysis.
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.
Proving correctness. Proof based on loop invariants  an assertion which is satisfied before each iteration of a loop  At termination the loop invariant.
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
COMP s1 Computing 2 Complexity
Asymptotic Notations Iterative Algorithms and their analysis
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,
CS 3343: Analysis of Algorithms
Analysis of Algorithms1 Running Time Pseudo-Code Analysis of Algorithms Asymptotic Notation Asymptotic Analysis Mathematical facts.
Algorithm Correctness A correct algorithm is one in which every valid input instance produces the correct output. The correctness must be proved mathematically.
Analysis of Algorithms Algorithm Input Output Last Update: Aug 21, 2014 EECS2011: Analysis of Algorithms1.
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.
September 15, Algorithms and Data Structures Simonas Šaltenis Aalborg University
Jessie Zhao Course page: 1.
Lecture 2 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Analysis of Algorithms These slides are a modified version of the slides used by Prof. Eltabakh in his offering of CS2223 in D term 2013.
Asymptotic Analysis-Ch. 3
September 29, Algorithms and Data Structures Lecture V Simonas Šaltenis Aalborg University
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.
Introduction to Analysis of Algorithms CS342 S2004.
Algorithm Analysis Part of slides are borrowed from UST.
Big-Oh Notation. Agenda  What is Big-Oh Notation?  Example  Guidelines  Theorems.
1/6/20161 CS 3343: Analysis of Algorithms Lecture 2: Asymptotic Notations.
2IS80 Fundamentals of Informatics Fall 2015 Lecture 5: Algorithms.
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.
CES 592 Theory of Software Systems B. Ravikumar (Ravi) Office: 124 Darwin Hall.
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.
Asymptotic Notation Faculty Name: Ruhi Fatima
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
1 Chapter 2 Algorithm Analysis Reading: Chapter 2.
Analysis of Algorithms Spring 2016CS202 - Fundamentals of Computer Science II1.
GC 211:Data Structures Week 2: Algorithm Analysis Tools Slides are borrowed from Mr. Mohammad Alqahtani.
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
CS 3343: Analysis of Algorithms
CS 3343: Analysis of Algorithms
CS 3343: Analysis of Algorithms
Asymptotic Notations Algorithms Lecture 9.
Algorithms and Data Structures
Ch. 2: Getting Started.
Math/CSE 1019N: Discrete Mathematics for Computer Science Winter 2007
September 10, 2001 Algorithms and Data Structures Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Algorithms and Data Structures Lecture II
Algorithms and Data Structures
Presentation transcript:

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

September 9, This Lecture Binary search Correctness of algorithms Growth of functions and asymptotic notation Some basic math revisited

September 9, Searching in a Sorted Array INPUT sorted non-descending sequence of numbers (database) a single number (query) a 1, a 2, a 3,….,a n ; q j OUTPUT an index of the found number or NIL ; ; 9 NIL

September 9, Binary search INPUT: A[1..n] – a sorted (non-decreasing) array of integers, q – an integer. OUTPUT: an index j such that A[j] = q. NIL, if  j (1  j  n): A[j]  q left  1 right  n do j  (left+right)/2 if A[j]=q then return j else if A[j]>q then right  j-1 else left=j+1 while left<=right return NIL INPUT: A[1..n] – a sorted (non-decreasing) array of integers, q – an integer. OUTPUT: an index j such that A[j] = q. NIL, if  j (1  j  n): A[j]  q left  1 right  n do j  (left+right)/2 if A[j]=q then return j else if A[j]>q then right  j-1 else left=j+1 while left<=right return NIL Idea: Divide and conquer, one of the key design techniques

September 9, Binary search – analysis How many times the loop is executed: With each execution the difference between left and right is cult in half Initially the difference is n The loop stops when the difference becomes 0 How many times do you have to cut n in half to get 1? lg n

September 9, 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 9, 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 9, 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 9, 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 9, 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 9, 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 9, 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 9, 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 9, 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 9, 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 9, 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 9, 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 9, 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 9, 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 9, 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 9, Comparison of Running Times Running Time Maximum problem size (n) 1 second1 minute1 hour 400n n log n n22n n4n n2n

September 9, 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 9, A Quick Math Review (2)

September 9, 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 9, 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 9, Proof by Induction (2) Inductive Step

September 9, Next Week Divide-and-conquer, Merge sort Writing recurrences to describe the running time of divide-and-conquer algorithms and solving them.