Algorithm analysis basics
Upcoming SlideShare
Loading in...5
×
 

Algorithm analysis basics

on

  • 102 views

 

Statistics

Views

Total Views
102
Views on SlideShare
102
Embed Views
0

Actions

Likes
0
Downloads
3
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Algorithm analysis basics Algorithm analysis basics Presentation Transcript

  • Algorithm Analysisg y Muhammad Shoaib Farooq
  • AlgorithmAlgorithm • An Algorithm is an ordered sequence ofAn Algorithm is an ordered sequence of precise steps for solving a particular problemproblem. • Well defined computational procedure that takes some value as input and producetakes some value as input and produce same value as output A R i A bli T• e.g. A Recipe, Assembling a Toy M.Shoaib Farooq
  • AlgorithmAlgorithm • In mathematics and computing anIn mathematics and computing, an algorithm is a procedure (a finite set of well- defined instructions) for accomplishing somedefined instructions) for accomplishing some task which, given an initial state, will terminate in a defined end-stateterminate in a defined end state From Wikipediao ped a M.Shoaib Farooq
  • Algorithm [webster.com]Algorithm [webster.com] • Main Entry: al·go·rithmy g Pronunciation: 'al-g&-"ri-[th]&m Function: noun Etymology: alteration of Middle English algorisme, fromy gy g g Old French & Medieval Latin; Old French, from Medieval Latin algorismus, from Arabic al-khuwArizmi, from al- KhwArizmI fl A.D. 825 Arabian mathematician : a procedure for solving a mathematical problem (as of finding the greatest common divisor) in a finite number of steps that frequently involves repetition of an operation;p q y p p ; broadly : a step-by-step procedure for solving a problem or accomplishing some end especially by a computer M.Shoaib Farooq
  • HistoryHistory • The word algorithm comes from the name of the 9th century Persian Muslim mathematician Abu9th century Persian Muslim mathematician Abu Abdullah Muhammad ibn Musa al-Khwarizmi. • The word algorism originally referred only to the rules of performing arithmetic using Hindu-rules of performing arithmetic using Hindu Arabic numerals but evolved via European Latin translation of al-Khwarizmi's name into algorithmg by the 18th century. M.Shoaib Farooq
  • Algorithms and Programs Algorithm: a method or a process followed toAlgorithm: a method or a process followed to solve a computational problem. An algorithm takes the input to a problem (function) and transforms it to the output(function) and transforms it to the output. – A mapping of input to output. A problem can have many algorithms. A computer program is an instance orA computer program is an instance, or concrete representation, for an algorithm in some programming language M.Shoaib Farooq some programming language
  • Instance • An input sequence is called an instance of algorithmalgorithm. e.g. A sorting Algorithm Input : A sequence of n numbers (a1,a2…an) Output: A permutation (reordering) (a1’,a2’..an’) of the input sequence such that a1’<=a2’<=……<=an’ M.Shoaib Farooq
  • Algorithm PropertiesAlgorithm Properties An algorithm possesses the followingg p g properties: –It must be correct. –It must be composed of a series of concrete steps. Th b bi it t hi h–There can be no ambiguity as to which step will be performed next. It must be composed of a finite number of–It must be composed of a finite number of steps. –It must terminate M.Shoaib Farooq It must terminate..
  • Analyzing Algorithmy g g • Analysis means predicting the resources that the algorithm requiresthat the algorithm requires. • Before analysis we must know the implementation technology/ Model ofimplementation technology/ Model of computation. - RAM (Random Access Machine) Concurrent Operations (Uni Processor)Concurrent Operations (Uni-Processor) Standard generic single-processor machine. - Parallel Machines (PRAM) M.Shoaib Farooq ( )
  • Model Of Computation RAM - A RAM is an idealized machine with an infinityy large random-access memory. - Instruction are executed one by one (there isInstruction are executed one by one (there is no parallelism). Abstract machine which has an unlimited- Abstract machine which has an unlimited number of registers of unlimited size which can be accessed randomlycan be accessed randomly M.Shoaib Farooq
  • RAM (cont..) • The RAM model contains instructions commonly found in real computers: • Arithmetic (Add, subtract, multiply, divide, remainder floor, ceiling),, g), • Data movement (load, store, copy) • Control (Conditional and unconditional• Control (Conditional and unconditional branch) * Each instruction takes a constant amount of time M.Shoaib Farooq
  • Distributed Systems • A collection of certain no of autonomous• A collection of certain no of autonomous, dispersed, but interconnected computers via communication path that appear to the usercommunication path that appear to the user of the system as a single computer. • Autonomous Controlling itselfAutonomous Controlling itself • Dispersed geographically scattered in a room, building, Campusa room, building, Campus city, Continent or World-Over • Communication Path Copper-wire, Optic-Fiber,pp , p , Laser, Microwaves or Communication Satellite • Single Computer virtual UniProcessor
  • Lost Update Problem Transaction T: Bank$Withdraw(A,4); Bank$Deposit(B, 4) Transaction U: Bank$Withdraw(C, 3); Bank$Deposit(B, 3)$ p ( , ) $ p ( , ) Banlance := A.Read() $100a a ce ead() $ 00 A.Write (balance-4) $96 Balance := C.Read() $300 C Write(balance 3) $297 Balance := B.Read() $200 C.Write(balance-3) $297 Balance := B.Read() $200 B.Write ( balance +4 ) $204 B.Write( balance + 3) $203 M.Shoaib Farooq
  • PRAM • PRAM stands for Parallel Random Access Machine, which is an abstract machine for designing the algorithms applicable to parallel computers. • It eliminates the focus on miscellaneous issuesIt eliminates the focus on miscellaneous issues such as synchronization and communication. • In terms of Flynn‘s, PRAMs are multiple i t ti lti l d t t (MIMD ) M.Shoaib Farooq instruction multiple data computers (MIMDs).
  • Algorithm Selection Criteria 1- Correctness Compute valid input for a finite amount of time and produce a right output 2. Amount of Work Done How much data is processed in a unit of time 3. Amount of space usedp How much memory is used 4 Simplicity4. Simplicity 5. Optimality M.Shoaib Farooq Optimal Algorithm can save both time and space
  • Complexity of Algorithm • Complexity is a function T(n) which measures the time and/or space used bymeasures the time and/or space used by an algorithm in term of the input size n. • The running Time of an algorithm on a particular input is the number of primitive operations or “steps” executed. M.Shoaib Farooq
  • Space-Time Trade OffSpace Time Trade Off • Refers to choice between algorithm thatRefers to choice between algorithm that allows one to decrease the running time of an algorithm solution by increasing thean algorithm solution by increasing the space to store and vice-versa M.Shoaib Farooq
  • Algorithmic Languageg g g 1- Indentation indicates block structure. e.g body of loopbody of loop 2- Looping Constructs while, for and the conditional if then elseconditional if-then-else 3- The symbol ► indicates that the reminder of the line is a comment. 4- Arithmetic expressions may use usual arithmetic op (+, -,*,/, ). Relational expression may use relational operators ( =, ,, and ) M.Shoaib Farooq logical ops and, or and not.
  • 5- Assignment statements are a ba b a b c ( Multiple Assignment) 6 S i6- Swapping a b 7- GOTO GoTo LableGoTo Lable loop : if c the do s goto loop M.Shoaib Farooq end
  • 8- An exit statement may be used to terminate a while or for loopp 9- Return is used to indicate termination of an algorithmalgorithm. 10- Algorithm lang mostly relies on end of lines rather than semicolons Several shortlines rather than semicolons. Several short stmts(eg, assignment stmt) may be written on one line separated by semicolonsone line separated by semicolons. 11- Local variables are used in a procedure. W h ll t l b l i bl ith tWe shall not use global variables without explicit indication. M.Shoaib Farooq
  • 12- If A is a structure the |A| size of structure If A is an Array the n=legth[A] upper bound of arrayof array. 13 All A l t d b13- All Array elements are accessed by name followed by index in square brackets. A[i] 14- Parameters are passed to a procedure byp p y values. M.Shoaib Farooq
  • Mathematical Notations & Functions • Floor Function X called the floor of x, denotes the greatest integer that does not exceed x. • Ceiling FunctionCeiling Function X called the ceiling of x, denotes the least integer that is not less than xinteger that is not less than x • Integer and Absolute Function M.Shoaib Farooq
  • Mathematical Notations & Functions PermutationPermutation Set of n elements is an arrangement of the elements in given orderelements in given order e.g. Permutation for elements are a, b, c abc, acb, bac, bca, cab, cba - n! permutation exist for a set of elementspe utat o e st o a set o e e e ts 5! = 120 permutation for 5 elements M.Shoaib Farooq
  • Mathematics Review + = baba xxx bl Logarithms x − = ba b a x x x blog blog ybylogx c a x b = == 2 )( + = nnn abba xx logloglog alog g c a += baab 1 22*222 2 + ==+ =+ nnnn nnn xxx logloglog −= ba b a 101024lg,12lg,01lg log)log( === = abab M.Shoaib Farooq g,g,g
  • Mathematics Review +n nn )1( Arithmetic Series ∑ + ==++++ nn in 2 )1( ...321 =i 1 2 Q d ti i ∑ ++ ==++++ n nnn in 22 )12)(1( ...941 Quadratic series ∑=i 1 6 M.Shoaib Farooq
  • Mathematics Review Geometric Series Harmonic series M.Shoaib Farooq
  • Mathematics Review Geometric Series ProofGeometric Series Proof 11 32 −+ ∑ an n :Proof 1 1 ...1 0 32 − ==+++++ = ∑ a a aaaaa i in ... ...1S 132 32 +++++= +++++= + aaaaaaS aaaa nn n 1 1 1 1 − = −=− + + a S aSaS n n 1−a S M.Shoaib Farooq