Introduction to Algorithms (2 nd edition) by Cormen, Leiserson, Rivest & Stein Chapter 2: Getting Started.

Slides:



Advertisements
Similar presentations
Introduction to Algorithms 6.046J Lecture 1 Prof. Shafi Goldwasser Prof. Erik Demaine.
Advertisements

Comp 122, Spring 2004 Divide and Conquer (Merge Sort)
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.
2. Getting started Hsu, Lih-Hsing. Computer Theory Lab. Chapter 2P Insertion sort Example: Sorting problem Input: A sequence of n numbers Output:
ALGORITHMS Introduction. Definition Algorithm: Any well-defined computational procedure that takes some value or set of values as input and produces some.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 5.
Analysis of Algorithms CS 477/677 Sorting – Part B Instructor: George Bebis (Chapter 7)
Spring 2015 Lecture 5: QuickSort & Selection
Quicksort Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
What is an Algorithm? (And how do we analyze one?)
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
CS 253: Algorithms Chapter 7 Mergesort Quicksort Credit: Dr. George Bebis.
Introduction to Algorithms Second Edition by Cormen, Leiserson, Rivest & Stein Chapter 2.
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:
Complexity Analysis (Part I)
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.
Analysis of Algorithms COMP171 Fall Analysis of Algorithms / Slide 2 Introduction * What is Algorithm? n a clearly specified set of simple instructions.
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,
Computer Algorithms Lecture 10 Quicksort Ch. 7 Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR.
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.
1 Chapter 24 Developing Efficient Algorithms. 2 Executing Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
Algorithm Correctness A correct algorithm is one in which every valid input instance produces the correct output. The correctness must be proved mathematically.
Introduction to Algorithms Lecture 1. Introduction The methods of algorithm design form one of the core practical technologies of computer science. The.
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.
CMPT 438 Algorithms. Why Study Algorithms? Necessary in any computer programming problem ▫Improve algorithm efficiency: run faster, process more data,
A Lecture /24/2015 COSC3101A: Design and Analysis of Algorithms Tianying Ji Lecture 1.
Major objective of this course is: Design and analysis of modern algorithms Different variants Accuracy Efficiency Comparing efficiencies Motivation thinking.
ALGORITHMS THIRD YEAR BANHA UNIVERSITY FACULTY OF COMPUTERS AND INFORMATIC Lecture three Dr. Hamdy M. Mousa.
Getting Started Introduction to Algorithms Jeff Chastine.
1Computer Sciences Department. Book: Introduction to Algorithms, by: Thomas H. Cormen Charles E. Leiserson Ronald L. Rivest Clifford Stein Electronic:
September 17, 2001 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.
Midterm Review 1. Midterm Exam Thursday, October 15 in classroom 75 minutes Exam structure: –TRUE/FALSE questions –short questions on the topics discussed.
Sorting Algorithm Analysis. Sorting  Sorting is important!  Things that would be much more difficult without sorting: –finding a phone number in the.
Introduction to Complexity Analysis. Computer Science, Silpakorn University 2 Complexity of Algorithm algorithm คือ ขั้นตอนการคำนวณ ที่ถูกนิยามไว้อย่างชัดเจนโดยจะ.
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.
Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 1 Prof. Charles E. Leiserson.
Big O David Kauchak cs302 Spring Administrative Assignment 1: how’d it go? Assignment 2: out soon… Lab code.
BITS Pilani Pilani Campus Data Structure and Algorithms Design Dr. Maheswari Karthikeyan Lecture1.
Introduction to Algorithms (2 nd edition) by Cormen, Leiserson, Rivest & Stein Chapter 2: Getting Started (slides enhanced by N. Adlai A. DePano)
CS6045: Advanced Algorithms Sorting Algorithms. Sorting Input: sequence of numbers Output: a sorted sequence.
ECOE 456/556: Algorithms and Computational Complexity
Lecture 2 Algorithm Analysis
CMPT 438 Algorithms.
Introduction to Algorithms (2nd edition)
CS 583 Fall 2006 Analysis of Algorithms
CS 3343: Analysis of Algorithms
Ch 7: Quicksort Ming-Te Chi
Ch 2: Getting Started Ming-Te Chi
Algorithms: the big picture
Ch. 2: Getting Started.
Divide & Conquer Algorithms
Introduction To Algorithms
Algorithms and Data Structures Lecture II
Presentation transcript:

Introduction to Algorithms (2 nd edition) by Cormen, Leiserson, Rivest & Stein Chapter 2: Getting Started

Overview  Aims to familiarize us with framework used throughout text  Examines alternate solutions to the sorting problem presented in Ch. 1  Specify algorithms to solve problem  Argue for their correctness  Analyze running time, introducing notation for asymptotic behavior  Introduce divide-and-conquer algorithm technique

The Sorting Problem Input:A sequence of n numbers [a 1, a 2, …, a n ]. Output:A permutation or reordering [a' 1, a' 2, …, a' n ] of the input sequence such that a' 1  a' 2  …  a' n. An instance of the Sorting Problem: Input:A sequence of 6 number [31, 41, 59, 26, 41, 58]. Expected output for given instance: Expected Output:The permutation of the input [26, 31, 41, 41, 58, 59].

Insertion Sort Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. The main idea …

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Insertion Sort (cont.)

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Insertion Sort (cont.)

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Insertion Sort (cont.) The algorithm …

Loop Invariant  Property of A[1.. j  1] 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.  Need to establish the following re invariant: Initialization: true prior to first iteration Maintenance: if true before iteration, remains true after iteration Termination: at loop termination, invariant implies correctness of algorithm

Analyzing Algorithms  Has come to mean predicting the resources that the algorithm requires  Usually computational time is resource of primary importance  Aims to identify best choice among several alternate algorithms  Requires an agreed-upon “model” of computation  Shall use a generic, one-processor, random-access machine (RAM) model of computation

Random-Access Machine  Instructions are executed one after another (no concurrency)  Admits commonly found instructions in “real” computers, data movement operations, control mechanism  Uses common data types (integer and float)  Other properties discussed as needed  Care must be taken since model of computation has great implications on resulting analysis

Analysis of Insertion Sort  Time resource requirement depends on input size  Input size depends on problem being studied; frequently, this is the number of items in the input  Running time: number of primitive operations or “steps” executed for an input  Assume constant amount of time for each line of pseudocode

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Analysis of Insertion Sort Time efficiency analysis …

Best Case Analysis  Least amount of (time) resource ever needed by algorithm  Achieved when incoming list is already sorted in increasing order  Inner loop is never iterated  Cost is given by: T(n) = c 1 n+c 2 (n  1)+c 4 (n  1)+c 5 (n  1)+c 8 (n  1) = (c 1 +c 2 +c 4 +c 5 +c 8 )n  (c 2 +c 4 +c 5 +c 8 ) = an + b  Linear function of n

Worst Case Analysis  Greatest amount of (time) resource ever needed by algorithm  Achieved when incoming list is in reverse order  Inner loop is iterated the maximum number of times, i.e., t j = j  Therefore, the cost will be: T(n) = c 1 n + c 2 (n  1)+c 4 (n  1) + c 5 ((n(n+1)/2)  1) + c 6 (n(n  1)/2) + c 7 (n(n  1)/2) + c 8 (n  1) = ( c 5 /2 + c 6 /2 + c 7 /2 ) n 2 + (c 1 +c 2 +c 4 +c 5 /2  c 6 /2  c 7 /2 +c 8 ) n  ( c 2 + c 4 + c 5 + c 8 ) = an 2 + bn + c  Quadratic function of n

Future Analyses  For the most part, subsequent analyses will focus on: Worst-case running time  Upper bound on running time for any input Average-case analysis  Expected running time over all inputs  Often, worst-case and average-case have the same “order of growth”

Order of Growth  Simplifying abstraction: interested in rate of growth or order of growth of the running time of the algorithm  Allows us to compare algorithms without worrying about implementation performance  Usually only highest order term without constant coefficient is taken  Uses “theta” notation Best case of insertion sort is  (n) Worst case of insertion sort is  (n 2 )

Designing Algorithms  Several techniques/patterns for designing algorithms exist  Incremental approach: builds the solution one component at a time  Divide-and-conquer approach: breaks original problem into several smaller instances of the same problem Results in recursive algorithms Easy to analyze complexity using proven techniques

Divide-and-Conquer  Technique (or paradigm) involves: “Divide” stage: Express problem in terms of several smaller subproblems “Conquer” stage: Solve the smaller subproblems by applying solution recursively – smallest subproblems may be solved directly “Combine” stage: Construct the solution to original problem from solutions of smaller subproblem

n (sorted) M ERGE Merge Sort Strategy  Divide stage: Split the n- element sequence into two subsequences of n/2 elements each  Conquer stage: Recursively sort the two subsequences  Combine stage: Merge the two sorted subsequences into one sorted sequence (the solution) n (unsorted) n/2 (unsorted) M ERGE S ORT n/2 (sorted)

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Merging Sorted Sequences

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Merging Sorted Sequences Combines the sorted subarrays A[p..q] and A[q+1..r] into one sorted array A[p..r] Makes use of two working arrays L and R which initially hold copies of the two subarrays Makes use of sentinel value (  ) as last element to simplify logic  (1) (n)(n) (n)(n)

Merge Sort Algorithm  (1) (n)(n) T(n/2) T(n) = 2T(n/2) +  (n)

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Analysis of Merge Sort Analysis of recursive calls …

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Analysis of Merge Sort T(n) = cn(lg n + 1) = cnlg n + cn T(n) is  (n lg n)