Download presentation
Presentation is loading. Please wait.
1
Design and Analysis of Algorithms
Lecture # 02 Muhammad Nasir Department of Computer Science COMSATS University Islamabad, Lahore Campus
2
Model of Computation Our analysis will be independent of the variations in: Machine hardware Operating system Compiler Programming language etc Algorithms need to understood by the people not machines
3
Model of Computation Ideally this model should be a reasonable abstraction of a standard generic single-processor machine We will call this machine as Random Access Machine
4
Random Access Machine (RAM)
A Random Access Machine (RAM) is an ideal machine with Infinite large memory Executes one instruction at a time (no parallelism) Each instruction performs some basic operations on two values in memory
5
Basic RAM operations Basic operations include
Assigning a value to a variable Performing basic arithmetic operation (+, -, *, /) on any size of integer values Accessing an element of an Array like array[100]
6
An Important Assumption
Each basic operation takes same constant time to execute There is no difference between performing addition or multiplication on any size of the values
7
Running Time Analysis Criteria
Worst case analysis Maximum running time over all (legal) inputs of size n Average case analysis average running time over all inputs of size n Best case analysis Running time over the best case input of size n We always look for the worst case running time
8
Theoretical versus empirical
The difference is the theoretical analysis, as the name suggests, is based on theory and not actual events. Empirical analysis is based on real testing events. Empirical is what you KNOW, theoretical is what you THINK.
9
Example: 2-Dimension Maxima
Suppose that you want to buy a computer You want to buy a fastest computer But fastest computer is expensive and you want the cheapest You are unable to decide which is more important: Speed or Price
10
Example: 2-Dimension Maxima
Certainly, we don’t want a computer if there is another which is faster and cheaper We can say that the fast and cheap dominates the slow and expensive computer So, we want a list of computers that are not dominated by any other
11
Example: 2-Dimension Maxima
This how we might model this as a formal problem Let a point p in 2-dimentional space be represented by its integer coordinate s, p = (p.x, p.y) A point p is said to be dominated by a point q if, p.x ≤ q.x and P.y ≤ q.y
12
Example: 2-Dimension Maxima
Given a set of n points, P ={p1, p2, p3, … , pn}, a point p is said to be maximal if it is not dominated by any other point in the set P.
13
Example: 2-Dimension Maxima
The computer selection problem can be modeled this way For each computer we associate (x, y) pair in such a way that x is the speed of computer y is the negation of price High y value means cheap computer and low y value means expensive computer Maximal point corresponds to fastest and cheapest computer
14
Example: 2-Dimension Maxima
Input A set of n points i.e. P = {p1, p2, p3, …, pn} in 2 dimensional space Output The set of maximal points of P, the set of those points which are not dominated by the others
15
Example
16
Brute-Force Algorithm for 2-D Maxima Example
17
Analysis of 2-D Maxima To measure the running time of brute force maxima algorithm we can Count the number of steps of the pseudo code that are executed Or, count the number of times an element of set P is accessed Or, the number of comparisons that are performed
18
Running Time Analysis Depends upon the input size e.g. n
Depends upon the structure of input For example, the breaking of inner loop in brute force algorithm depends upon the input size.
19
Analysis of 2-D Maxima Analysis of brute-force maxima algorithm
Input size is n We are going to count the number of times an element of P is accessed
20
Analysis of 2-D Maxima (Cont…)
21
Analysis of 2-D Maxima (Cont…)
The outer loop runs n times For each iteration of the outer loop, the inner loop runs n times P is accessed four times in the if statement P is accessed two times in output statement In worst case, every point is maximal so every point is output
22
Analysis of 2-D Maxima (Cont…)
23
Analysis of 2-D Maxima (Cont…)
The worst case running time is
24
Some Important Summations
25
Some Important Summations
26
Analysis: A Harder Example
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.