complexity - Algorithmic Cost and Complexity Measuring...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Algorithmic Cost and Complexity There are two aspects of algorithmic performance: • Time - Instructions take time. - How fast does the algorithm perform? - What affects its runtime? • Space - Data structures take space - What kind of data structures can be used? - How does choice of data structure affect the runtime? 2 Measuring Performance For example: A simple calculator : Perform the four basic arithmetic functions: - Addition - Subtraction - Multiplication - Division Prompt the user for: - Operand 1 - Operand 2 - Operator
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
3 Algorithm Calculator double op1 // 1st operand op2 // 2nd operand answer ; // result char operator ; // operator // obtain operands and operator from user printf( “Enter the first operand: ” ); scanf(“%lf”, &op1 ); printf( “Enter the second operand: ” ); scanf(“%lf”, &op2 ); printf( “Enter the operator: ” ); scanf(“%c”, &operator ); // perform the calculation if ( operator == ‘+’ ) answer = op1 + op2; if ( operator == ‘-’ ) answer = op1 - op2; if ( operator == ‘*’ ) answer = op1 * op2; if ( operator == ‘/’ ) answer = op1 / op2; printf( “The answer is %f \n”, answer ); // end algorithm Calculator 4 Analyzing Work Done How many operations does Calculator do? • read/write pairs (to obtain data) • Testing conditionals • Branching • Performing operation • Assigning variables Note : We will ignore the read/write instructions. They deal with the world “outside the algorithm” and involve factors beyond what we care about here. Measures of Work (ignoring read/write pairs) What’s the best case ? Addition - four tests (@ 2 each) - one add - one assignment - total: 10 What’s the worst case ? Division - four tests (@ 2 each) - one divide - one assignment - total: 10 What’s the average (expected) case ? - 10
Background image of page 2
A Better Way? // Perform the calculation if ( operator == ‘+’ ) answer = op1 + op2; else if ( operator == ‘-’ ) answer = op1 - op2; else if ( operator == ‘*’ ) answer = op1 * op2; else if ( operator == ‘/’ ) answer = op1 / op1; printf( “The answer is %f\n”, answer ); // end of algorithm Measures of Work (ignoring read/write pairs) What’s the best case ? Addition - one test (@ 2 each) - one add - one assignment - total: 4 What’s the worst case ? Division - four tests (@ 2 each) - one divide - one assignment - total: 10 What’s the average (expected) case ? - (4+6+8+10)/4 = 7 6 The Dangers of “Average” Work In many circumstances, the assumption of random distribution of input values is a faulty one. What about a cash register? • Addition operators most frequent (ring up an item) • Subtraction less frequent (use a coupon) • Multiplication rare (buy many of same item) • Division very rare (???) The average work in this situation would migrate somewhat towards 4 from the mean of 7 suggested by the assumption of random data. Don’t assume random distribution without reason.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This document was uploaded on 06/12/2011.

Page1 / 15

complexity - Algorithmic Cost and Complexity Measuring...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online