This preview shows pages 1–2. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: Illinois Institute of Technology Department of Computer Science Lecture 12: March 5 CS 330 Discrete Mathematics Spring Semester, 2008 1 Analysis of algorithms In this section, we will return to the problem that originally led us into the discussion of probability: For example, consider the algorithm to find the maximum of n numbers x 1 , x 2 , . . . , x n listed below. To analyze this algorithm fully we want to know exactly how many times each of the instructions is executed; but for the moment, let us ask only how many times the statement “ m ← k ” is executed. The answer, of course, depends not on the particular values of the x i , but rather on their relative order: If x 1 is the maximum of the n elements, the statement will never be executed. If x 1 < x 2 < ··· < x n then the statement will be executed n 1 times. For how many of the relative arrangements of the inputs x 1 , x 2 , . . ., x n will the statement be executed exactly i times? The answer to this question is at the heart of the analysis of the algorithm. ALGORITHM 1: /* Selecting the largest of x [1] , x [2] , . . . , x [ n ] . */ m ← 1 FOR k ← 2 TO n DO IF x k > x m THEN m ← k  {z } “assignment” of interest We have shown that the best case of algorithm 1 is zero assignments and in the worst case there are...
View
Full
Document
This note was uploaded on 04/15/2008 for the course CS 330 taught by Professor Reingold,edwardm. during the Spring '08 term at Illinois Tech.
 Spring '08
 Reingold,EdwardM.
 Algorithms

Click to edit the document details