You are on page 1of 10

CHAPTER 1 Introduction

Page 1

Algorithm Properties
Input The algorithm receives input

Output The algorithm produces output


Precision The steps are precisely stated

Determinism The intermediate results are unique and are determined by the input and the results of previous steps Finiteness Algorithm stops after a finite number of steps
Generality The algorithm applies to a set of inputs.
Page 2

Analysis of Algorithm
Algorithm analysis answers the following questions Correctness: Does a given algorithm solve the problem Termination: Does the algorithm stop in a finite number of steps. Time Analysis: How many instructions does the algorithm execute? Space Analysis: How much memory does the algorithm need to execute?
Page 3

Algorithm Design
We will look at several important algorithm design techniques Searching techniques Divide and Conquer methods Greedy Algorithms Dynamic Programming
Page 4

Pseudocode for Algorithms


Pseudocode is universal system in which ideas can be expressed informally during the algorithm development process. Pseudocode is precise, structured and resembles computer programming language such as C or java. Examples of pseudocode will follow.

Page 5

Finding the Maximum of Three Numbers


This algorithm finds the largest of the numbers a, b, and c.
Input Parameters: a, b, c count Output Parameter: x statements max(a,b,c,x){ x = a 1 if (b > x) // if b is larger than x, update x 1 x = b 1 if (c > x) // if c is larger than x, update x 1 x = c 1 } ----5

The idea of the algorithm is to inspect the numbers one by one and copy the value seen so far into variable x. Note The algorithm is stated precisely, with no ambiguity. It is deterministic and the intermediate results are unique. The algorithms terminates and general enough so that it can find the largest number of any set of three numbers.
Page 6

Finding the Maximum Value in an Array Using a While Loop


This algorithm finds the largest number in the array s[1], s[2], ... , s[n].
Input Parameter: s Output Parameters: None array_max_ver1(s) { suppose s has size n large = s[1] i = 2 while (i s.last) { if (s[i] > large) // larger value large = s[i] i = i + 1 } return large } count statements 1 1 n-2+2 n-2+1 n-2+1 n-2+1 --------3(n-1)+n+2

Use the same idea as the previous algorithm to find the largest element in an array. Is this algorithm finite, deterministic, correct and general? The counting considers the worst case. Note that the while loop statement is executed one last time when i > s.last
Page 7

Finding the Maximum Value in an Array Using a For Loop


This algorithm finds the largest number in the array s[1], s[2], ... , s[n]. Input Parameter: s Output Parameters: None array_max_ver2(s) { large = s[1] for i = 2 to s.last if (s[i] > large) // larger value found large = s[i] return large }

Note that the output parameter is none since it will be the return value of the function.
Page 8

Randomized Algorithms
Many Algorithms currently in use are not general, deterministic or even finite. An OS program never terminates. It is called an online algorithm

A randomized algorithm does not require that the intermediate results of each step of the algorithm to be unique. A randomized algorithm has to make a random choice at some point during the execution.

Page 9

Array Shuffle A randomized algorithm


This algorithm shuffles the values in the array a[1], a[2], ... , a[n].

Input Parameter: a -- Suppose array size is n Output Parameters: a count shuffle(a) { for i = 1 to a.last 1 swap(a[i], a[rand(i,a.last)]) }

n n-1

The elements of the array a are randomly shuffled using the command rand(). The running time or time complexity of this algorithm is 2n-1 i.e. it is a linear algorithm.
Page 10

You might also like