310 likes | 452 Views
Computer Science II. University of Palestine Faculty of Engineering and Urban planning Software Engineering Department. Lecture 4 of. Mohammad Amin Kuhail M.Sc. (York, UK). Analysis Tools. Tuesday, 25 September 2007. Analysis Tools. Agenda. What Is Running Time Anyway? Pseudo-Code
E N D
Computer Science II University of Palestine Faculty of Engineering and Urban planning Software Engineering Department Lecture 4 of Mohammad Amin KuhailM.Sc. (York, UK) Analysis Tools Tuesday, 25 September 2007
Analysis Tools Agenda • What Is Running Time Anyway? • Pseudo-Code • A Quick Mathematical Review • Analysis of algorithms • Average-Case and Worst-Case Analysis • Asymptotic Notation
What Is Running Time Anyway? Definition • Running time : How long it takes a show to go from start to finish.
What Is Running Time Anyway? Experimental Studies • Run a piece of code and record the time spent in execution. • Java.System.currentTimeMillis() • In general the time increases with The input size, and is affected by the hardware, and software environment.
What Is Running Time Anyway? Limitations of running time studies • Experiments can be done on a limited set of test inputs, and may not be indicative of other inputs. • It is difficult to compare two algorithms unless experiments have the same software, and hardware environments. • It is necessary to implement and study an algorithm in order to study its running time.
What Is Running Time Anyway? Requirements for a general methodology • Takes into account all possible inputs. • Allows us to evaluate the efficiency of an algorithm in a way that is independent from hardware and software environment. • Can be performed by studying a high-level description of the algorithm without actually implementing it or running experiments on it.
Pseudo-Code Definition • Pseudo-code (derived from pseudo and code) is a compact and informal high-level description of a computer programming algorithm that uses the structural conventions of programming languages, but omits detailed subroutines, variable declarations or language-specific syntax. The programming language is augmented with natural language descriptions of the details, where convenient.
Pseudo-Code Characteristics • High- level description of an algorithm • More structured than English prose • Less detailed than a program • Preferred notation for describing algorithms • Hides program design issues
Pseudo-Code Syntax • As the name suggests, pseudo-code generally does not actually obey the syntax rules of any particular language; there is no systematic standard form, although any particular writer will generally borrow the appearance of a particular language.
Pseudo-Code Syntax • Expression: Use ← instead of =, Use = instead of == • Method declaration: algorithm name(A,n) • Decision structures:If condition then true-actions else false-actions • While-loops:while condition do actions • Repeat-loops:repeat actions until condition • For-loops:for increment-action do actions • Array-indexing:A[i] • Method-calling:object.mehtod(args) or simply method(args). • Method-returs:return
Pseudo-Code Example Algorithm arrayMax(A, n) Input array A of n integers Output maximum element of A currentMax ← A[0] for i ← 1 to n − 1 do if A[i] > currentMax then currentMax ← A[i] return currentMax
Pseudo-Code Vs. Flowchart ? • A flowchart (also spelled flow-chart and flow chart) is a schematic representation of an algorithm or a process.
A Quick Mathematical Review Logarithms and Exponents
A Quick Mathematical Review Logarithms and Exponents
A Quick Mathematical Review Preposition • Let a, b, and c positive real numbers, we have:
A Quick Mathematical Review Preposition Ceiling, Floor
A Quick Mathematical Review Examples
A Quick Mathematical Review Summations
A Quick Mathematical Review Proposition For any integer n>=0 and any real number 0 <a1 , consider the summation
A Quick Mathematical Review Example
A Quick Mathematical Review Proposition For any integer n>=1 We have:
A Quick Mathematical Review Example
Analysis of algorithms Primitive Operations Definition • Assigning a value to a variable. • Calling a method. • Performing an arithmetic operation (e.g., adding two Numbers). • Comparing two numbers. • Indexing into an array. • Following an object reference. • Returning from a method.
Analysis of algorithms Primitive Operations Analysis approach • Code up the algorithm in some high-level computer language. • Compile the program into some low-level executable language. • Determine for each instruction i of the low-level language, the time ti needed to execute the instruction. • Determine for each instruction i of the low-level language, the times ni that instruction i gets executed when the algorithm is run. • Sum up the products ni.ti over all the instructions, which yields the running time of the algorithm. Advantages Vs. Disadvantages ?
Analysis of algorithms Primitive Operations Example Algorithm arrayMax(A, n) #operations currentMax ← A[0] for i ← 1 to n − 1 do if A[i] < currentMax then currentMax ← A[i] return currentMax
Analysis of algorithms Primitive Operations Example Algorithm arrayMax(A, n) #operations currentMax ← A[0] 2 for i ← 1 to n − 1 do 1+ n if A[i] < currentMax then 2(n-1) currentMax ← A[i] at most 2(n-1) { increment counter i} 2(n − 1) return currentMax 1 AT LEAST: 2+1+n+4(n-1)+1=5n-1 AT MOST: 2+1+n+6(n-1)+1=7n-3
Average-Case and Worst-Code Analysis Primitive Operations Example • Best Case. • Worst Case. • Average Case. - Expected number of execution times probability theory
Average-Case and Worst-Code Analysis Primitive Operations Example Worst case time Running Time ms Average Best case time Input Instance
Asymptotic Notation Definition Asymptotic Notation is often used to describe how the size of the input data affects an algorithm's usage of computational resources (usually running time or memory).
Asymptotic Notation Do we need that level of detail? Example: y= a*x+b ?
References Analysis Tools [1] Textbook. [2]http://www.google.com/url?sa=X&start=0&oi=define&q=http://www.ascd.org/advocacykit/glossary_med.html&usg=AFQjCNEqZ25hi_bWLZ_uURa_YzaPGf7f5g [3] http://en.wikipedia.org/wiki/Pseudocode [4] http://en.wikipedia.org/wiki/Flowchart [5] http://en.wikipedia.org/wiki/Asymptotic_notation