Introduction to Algorithms

Similar documents
Lecture 2: Getting Started

The divide-and-conquer paradigm involves three steps at each level of the recursion: Divide the problem into a number of subproblems.

Sorting & Growth of Functions

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

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

Lecture 1. Introduction / Insertion Sort / Merge Sort

Divide-and-Conquer. Divide-and conquer is a general algorithm design paradigm:

DESIGN AND ANALYSIS OF ALGORITHMS. Unit 1 Chapter 4 ITERATIVE ALGORITHM DESIGN ISSUES

Unit 1 Chapter 4 ITERATIVE ALGORITHM DESIGN ISSUES

Lecture 9: Sorting Algorithms

Scientific Computing. Algorithm Analysis

Advanced Algorithms and Data Structures

Programming II (CS300)

CSCI 104 Log Structured Merge Trees. Mark Redekopp

How many leaves on the decision tree? There are n! leaves, because every permutation appears at least once.

CIS 121 Data Structures and Algorithms with Java Spring Code Snippets and Recurrences Monday, January 29/Tuesday, January 30

ESc101 : Fundamental of Computing

Algorithms + Data Structures Programs

EECS 2011M: Fundamentals of Data Structures

Data Structures and Algorithms Key to Homework 1

COMP Data Structures

Theory and Algorithms Introduction: insertion sort, merge sort

Algorithm Analysis. Spring Semester 2007 Programming and Data Structure 1

Chapter 3:- Divide and Conquer. Compiled By:- Sanjay Patel Assistant Professor, SVBIT.

CSC Design and Analysis of Algorithms

PROGRAM EFFICIENCY & COMPLEXITY ANALYSIS

Lecture 19 Sorting Goodrich, Tamassia

Algorithm Efficiency & Sorting. Algorithm efficiency Big-O notation Searching algorithms Sorting algorithms

Searching Algorithms/Time Analysis

Analysis of Algorithms. Unit 4 - Analysis of well known Algorithms

Lecture Notes for Chapter 2: Getting Started

Order Analysis of Algorithms. Sorting problem

CS2223: Algorithms Sorting Algorithms, Heap Sort, Linear-time sort, Median and Order Statistics

CSC Design and Analysis of Algorithms. Lecture 6. Divide and Conquer Algorithm Design Technique. Divide-and-Conquer

Lecture 5: Sorting Part A

SAMPLE OF THE STUDY MATERIAL PART OF CHAPTER 6. Sorting Algorithms

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

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

Sorting Goodrich, Tamassia Sorting 1

CS 137 Part 7. Big-Oh Notation, Linear Searching and Basic Sorting Algorithms. November 10th, 2017

Sankalchand Patel College of Engineering - Visnagar Department of Computer Engineering and Information Technology. Assignment

Module 2: Classical Algorithm Design Techniques

CSci 231 Final Review

Introduction to Algorithms 6.046J/18.401J

Sorting. There exist sorting algorithms which have shown to be more efficient in practice.

Lecture 6: Divide-and-Conquer

Analysis of Algorithms - Introduction -

Algorithms - Ch2 Sorting

COE428 Lecture Notes Week 1 (Week of January 9, 2017)

Algorithm Analysis and Design

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

UNIT 1 ANALYSIS OF ALGORITHMS

Data Structures Lecture 8

Data Structures and Algorithms CSE 465

Analysis of Algorithms Part I: Analyzing a pseudo-code

The Limits of Sorting Divide-and-Conquer Comparison Sorts II

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

Sorting and Selection

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

Computer Science 385 Analysis of Algorithms Siena College Spring Topic Notes: Divide and Conquer

A loose end: binary search

Analysis of Algorithms - Quicksort -

Introduction to Algorithms 6.046J/18.401J/SMA5503

Data Structures and Algorithms

Advanced Algorithms and Data Structures

Pseudo code of algorithms are to be read by.

Fundamental problem in computing science. putting a collection of items in order. Often used as part of another algorithm

SORTING AND SELECTION

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

CSC236 Week 6. Larry Zhang

Divide & Conquer. 2. Conquer the sub-problems by solving them recursively. 1. Divide the problem into number of sub-problems

Sorting. Hsuan-Tien Lin. June 9, Dept. of CSIE, NTU. H.-T. Lin (NTU CSIE) Sorting 06/09, / 13

Computational biology course IST 2015/2016

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

Chapter 2: Complexity Analysis

Quick-Sort. Quick-Sort 1

5105 BHARATHIDASAN ENGINEERING COLLEGE

Lecture 2: Algorithm Analysis

ECS122A Lecture Notes on Algorithm Design and Analysis

Introduction to Computers and Programming. Today

Algorithms and Applications

CLASS: II YEAR / IV SEMESTER CSE CS 6402-DESIGN AND ANALYSIS OF ALGORITHM UNIT I INTRODUCTION

Choice of C++ as Language

Test Bank Ver. 5.0: Data Abstraction and Problem Solving with C++: Walls and Mirrors, 5 th edition, Frank M. Carrano

Programming II (CS300)

Sorting. Popular algorithms: Many algorithms for sorting in parallel also exist.

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

Data Structures and Algorithm Analysis (CSC317) Introduction: sorting as example

Introduction to Algorithms 6.046J/18.401J

CS 445: Data Structures Final Examination: Study Guide

O(n): printing a list of n items to the screen, looking at each item once.

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

Data Structures and Algorithm Analysis (CSC317) Introduction: sorting as example

Better sorting algorithms (Weiss chapter )

Chapter 4: Sorting. Spring 2014 Sorting Fun 1

LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS

Math 501 Solutions to Homework Assignment 10

LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS

Divide and Conquer Algorithms

Lecture 8: Mergesort / Quicksort Steven Skiena

Transcription:

Introduction to Algorithms An algorithm is any well-defined computational procedure that takes some value or set of values as input, and produces some value or set of values as output. 1 Why study algorithms? As the speed of processors increase, performance is often said to be less important than the other software quality characteristics (e.g. security, extensibility, re-usability, etc.). However, large problem sizes are common place in the area of computational science; which makes performance a very important factor. This is because longer computation time, to name a few, mean slower results, less through research, and higher cost of computation (if buying CPU hours from an external party). The study of algorithms, therefore, gives us a language to express performance as a function of problem size. A similar approach can also be taken to express resource requirements as a function of problem size. It is important to keep resource requirements in check as it may easily result in poorer performance. 2 Notation The representation of the problem, the solution, and the algorithm to obtain the solution are greatly influenced by the state of technology. In this day and age, the basic model is the sequential processor with access to random access memory. Simple problems in Mathematics such as the minimum of a set of integers requires us to consider two points: 1. How is the set represented? 2. How to find the minimum (depends on how the set is represented)? To keep things simple 1, let s assume that the integers are stored in a sequential array; and that we know the number of integers to be n. Since any one of the integers in the array may be the minimum, the solution is simply to take 1 Readers who consistently require extracting the minimum/maximum value of a set should take a look at the heap data structure covered in these notes. 1

a look at each one of them and report the minimum. The following algorithm implements this strategy. Find-Minimum(A, n) 1 if n = 0 2 then return NIL /* No minimum in an empty array. */ 3 m A[1] 4 i 2 5 while i n 6 do if A[i] < m 7 then m A[i] 8 i i + 1 9 return m The Find-Minimum algorithm reflects on the state of present day computer technology: The computer executes one line at the time. There may be branches (if). There may be iterations (while, for, repeat/until). There may be assignments (m A[1]). There may be random memory accesses (Using A[i] to access the ith element of A). 3 Analysis The basic idea is that the more instructions a computer has to process, the longer it will take to complete processing. Thus, the aim of analysing algorithms is to describe how much work a computer has to do. Since the amount of work usually depends on the problem size, we are mostly interested how an algorithm scales with problem size. For starters, let us examine the Find-Minimum algorithm introduced earlier. Lines 1 to 4 can be completed in some constant amount of time c 1. Due to the while-loop at line 5, lines 5 to 8 take c 2 (n 1) time. Lastly, line 9 takes c 3 time. Total time taken is therefore the sum of all these: c 1 + c 2 (n 1) + c 3 = c 1 + c 2 n c 2 + c 3 Let c 4 = c 1 c 2 + c 3 = c 4 + c 2 n Therefore, each element in the input array contributes c 2 runtime. time to the total 2

4 Order of growth How an algorithm scales in relation to its input problem size is termed order of growth. For example, say we have an algorithm that runs at n 3 + n 2 + n + 4. The following table shows us the significance of each term: Value of n n 3 n 2 n 1 4 1 1 1 1 4 10 1000 100 10 4 100 1000000 10000 100 4 1000 1000000000 1000000 1000 4 As can be seen, as n approaches infinity, all the other terms fade in significance next to n 3. Therefore the order of growth of the algorithm is n 3. So applying this vocabulary to Find-Minimum, whose cost we found to be c 4 + c 2 n, we say that the order of growth is n. 5 Insertion-Sort Example A sorting algorithm arranges the elements of a list into a certain order. A simple example is the Insertion-Sort algorithm. The strategy is to divide the list into a set of sorted elements and a set of unsorted elements. We then take an element from the unsorted set and insert it into the correct position in the sorted set until the unsorted set is empty. Insertion-Sort(A) Cost Times 1 for j 2 to length[a] c 1 n 2 do key A[j] c 2 n 1 3 i j 1 c 3 n 1 4 while i > 0 and A[i] > key c 4 Σ n j=2 t j 5 do A[i + 1] A[i] c 5 Σ n j=2 (t j 1) 6 i i 1 c 6 Σ n j=2 (t j 1) 7 A[i + 1] key c 7 n 1 To compute the running time of this algorithm, we sum the products of the cost and times columns. If the array is in reverse sorted order, the worst case will apply, and so, noting that: n j = j=2 the worst case running time is ( c4 T (n) = 2 + c 5 2 + c ) ( 6 n 2 + 2 Hence the order of growth is n 2. n(n + 1) 2 1 c 1 + c 2 + c 3 + c 4 2 c 5 2 c 6 2 + c 7 ) n (c 2 + c 3 + c 4 + c 7 ) 3

6 Design of Algorithms As with all areas of computing, there are many different approaches to designing an algorithm to solve any given problem. Over the course of this unit, we will look at a few of these approaches, and how to recognise which approach is best for which problem. Some of the popular approaches include: Divide-and-conquer Greedy approach Dynamic programming Linear programming Branch-and-bound α β pruning 7 Divide-and-Conquer Algorithms Divide-and-Conquer is a recursive approach, which involves the following steps: 1. Divide the problem into a number of subproblems. 2. Conquer the subproblems by solving them recursively. If however the subproblems are small enough, simply solve them. 3. Combine the solutions of the subproblems into the solution for the original problem. Consider the sorting problem. The insertion sort uses an incremental approach, it sorts the first item, then the first two items, then first three items, and so on, until it has sorted all n items of the array. Another approach is merge sort. It uses the divide-and-conquer approach, as follows: 1. Divide the n-element sequence into two subsequences of n/2 elements each. 2. Conquer the two subsequences by recursively using the merge sort on each of them. 3. Merge the two sorted subsequences into a sorted sequence. See Figure 1. 4

Figure 1: Merging an eight integer sequence. 8 Analysis of Merge Sort The running time of an algorithm that makes a recursive call to itself is often called a recursion. We can analyse the running time of merge sort by looking at each of its three steps. 1. Divide Dividing the array requires simply finding the mid-point of the array, this takes constant time, thus D(n) = 1. 2. Conquer Here the algorithm makes two calls to itself, each one with an order of input of n/2. Thus C(n) = 2T (n/2). 3. Merge The process of merging the two sorted arrays can be done in M(n) = n time. Combining the above, and remembering that an order of growth of 1 is insignificant next to an order of growth of n, we get the following: { c if n = 1 T (n) = 2T ( ) n 2 + Θ(n) if n > 1 Using the above formula, we can build a tree (Figure 2) depicting how much time is spent at each level of the recursion. The summation of all the costs of each level shows that the running time of merge sort is n lg n. 5

Figure 2: The breakdown of the cost of a Merge Sort. Each level of the tree represents a level of recursion in the algorithm; thus there are lg n levels. The cost of each level is shown on the right of the tree. Intuitively, the cost of the whole algorithm is the sum of all the level costs. In this case, it is n lg n. 6