Professional Documents
Culture Documents
Page 1
Algorithm Properties
Input The algorithm receives input
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
Page 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
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
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
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