You are on page 1of 6

History

CS 101 Data Structures and Algorithms

Abu JaFar Mohammed Ibn Musa Al-Khowarizmi ALGORITHM Astronomer and Mathematician from Baghdad (780-850) From the town of Khowarizmi (part of Persia) now called Khiva (part of Uzbekistan) Western Europeans first learned about algebra from his books like Kitab al-jabr wal muquabala ALGEBRA His books describe procedures for arithmetic operations using Hindu numerals.

01 Algorithms

CJD

Definition
An ALGORITHM is a finite set of precise instructions for performing a computation or for solving a problem. a well-defined computational procedure that takes some value(s) as input, and produces or transforms the input into some value(s) as output.

Desired Properties
Properties shared in general by algorithms : Input instance values of problem to be solved Output values produced that solves problem Definiteness unambiguous - steps defined precisely Effectiveness Each step performed exactly in finite amount of time Finiteness output after finite number of steps must terminate Correctness correct output from input Generality applicable to all instances of the problem
CJD CJD

Pseudocode
Our Pseudocode will be used in this course to describe algorithms. is very much like C, Pascal or Algol. will employ whatever expressive method is clear and concise. may intersperse English with real code. is computer language and machine independent is not concerned with issues of software engineering like data abstraction, modularity, and error handling. Sometimes, Java code may be the used to describe an algorithm.
CJD

Pseudocode Conventions
Use // for comments Use left) for assignments. Allow i j k (right to

Use for exchange or swap Indentation indicates block structure reduces clutter (eg. {,},begin, end) and enhances clarity. Use conditional constructs if-then-else, switchcase (auto-break) Use looping constructs while, for, repeat-until A[i] is ith element of array A, A[i..j] is ith to jth elements of array A A[i,j] is the ith row and jth column of 2-D array A
CJD

Pseudocode Conventions, contd


Access a field or method by suffixing the node or object name by . and field or method name. Ex. N.id or N.add(3) Name procedures and identify parameters All variables are local unless otherwise specified Parameters are passed by value calling parameters are unchanged as a result of the procedure call (just like Java) Although for objects, procedures may change contents of objects referred to, even though the parameters refer to same object. Use return statement to exit procedure and return output(s)
CJD

Pseudocode Example
Compute an where a is a real number and n is a non-negative integer.
power(a,n) 1 p 1 2 for i 1 to n 3 p p*a 4 return p

//initialize, works for n=0 //repeat multiplication n times //exit, returning result

Does power(n) procedure have the desired properties of an algorithm ?


CJD

Implementation
occasionally, we will demonstrate implementation of algorithms in Java we will emphasize correctness and clarity of solution we will keep our input and output processes simple we will not worry about beauty of presentation we will have minimal concern with error handling we will implement many algorithms in Java in the laboratory classes
CJD

Java Example Implementation 1


class Power1 { public static void main( String args[] ) { //assume inputs are of correct type //first parameter is a, second is n double a = Double.parseDouble( args[0] ); int n = Integer.parseInt( args[1] ); double p = 1; for (int i = 1; i<=n; i++) p = p*a; System.out.println(a + to the + n + = + p); } }
CJD

Java Example Implementation 2


import javax.swing.*; class Power2 { public static void main(String args[]) { String input = JOptionPane.showInputDialog(a=); double a = Double.parseDouble(input); input = JOptionPane.showInputDialog(n=); int n = Integer.parseInt(input); double p = 1; for (int i = 1; i<=n; i++) p = p*a; System.out.println(a + to the + n + = + p); System.exit(0); }}
CJD

Java Example Implementation 3


class Power3 { public double power(double a, int n) { double p = 1; for (int i = 1; i<=n; i++) p = p*a; return p; } } // elsewhere, Power3 pow = new Power3(); System.out.println(-2.3 to the 7 = + pow.power(-2.3,7));

CJD

Power Function
power(a,n) 1 p 1 2 for i 1 to n 3 p pa 4 return p

Recursive Definitions
Recursion
Basis Step : provides initial or base conditions that solve simplest versions of itself Example : f(0) = 1 and f(1) = 1 Recursive Step : defines something in terms of simpler versions of itself Example : f(n) = n f(n-1) if n>1 Illustration : f(4) = 4 f(3) = 4 3 f(2) = 4 3 2 f(1) = 4 3 2 1 = 24 = 4 ! or 4 Factorial
CJD CJD

//initialize, works for n=0 //repeat multiplication n times //exit, returning result

Iteration : Each execution of a group of statements in a loop Iterative Algorithms : Solve problems using iteration(s).

Recursive Power Function


Recursive
powerR(a,n) 1 if n=0 then return 1 2 else return a powerR(a,n-1) // basis step // recursive step

Factorial Function
Recursive
factorialR(n) 1 if n=1 or 0 then return 1 2 else return n factorialR(n-1) // basis step // recursive step

Compare Iterative
power(a,n) 1 p 1 2 for i 1 to n 3 p pa 4 return p //initialize, works for n=0 //repeat multiplication n times //exit, returning result
CJD

Compare Iterative
factorial(n) 1 p 1 2 for i n downto 1 3 p pi 4 return p //initialize, works for n=0

CJD

Fibonacci Numbers
Recursive Step : fn = fn-1 + fn-2 for n>1 Basis Step : f0 = 0 and f1 = 1 Generates the sequence {fn} = { 0, 1, 1, 2, 3, 5, 8, 17, }
fibR(n)

Fibonacci Algorithm
1 if n=1 or 0 then return n 2 // basis step

else return fibR(n-1) + fibR(n-2) // recursive step

Illustration: fibR(4) = fibR(3)+fibR(2) = { fibR(2) + fibR(1) } + { fibR(1) + fibR(0) } = { [ fibR(1) + fibR(0) ] + 1 } + { 1 + 0 } ={[1+0]+1}+{1+0}=3 Observe: This is not efficient because the method is called several times for the same parameter value. So some computations are executed redundantly.
CJD CJD

Iterative Fibonacci Algorithm


fib(n) 1 if n=0 then return 0 2 else x 0 // initially fib(0) 3 y 1 // initially fib(1) 4 for i 2 to n 5 z x+y // fib(i) 6 x y // prepare for next iteration 7 y z 8 return y Bottom-Up Approach: This computes fib(n) by computing fib(0), then fib(1), then fib(2), upwards to fib(n). In each step, remember only 2 last computed numbers in sequence.
CJD

Which method is better?


Iteration usually more efficient than recursion Recursion may be faster if hardware and o/s are built for it. Test it!

if part of the work is repeated often simpler and more (e.g. Fibonacci), choose consistent with the logic of iteration the solution to problem if application is critical (e.g. space shuttle program) if application is executed or called repeatedly (e.g. compiler) if difference in execution time is not perceivable (10ms vs 100ms) Conversion to iteration may be difficult and require explicit manipulation of complex data structures
CJD

Tower of Hanoi
Given 3 posts on which N disks of increasing radii are resting on left post. How do you move all N disks to right post if only 1 disk may be moved at a time from one post to another such that no disk is on top of a smaller disk at any time ?
1 2 3 4 5 6 7 8
CJD

Tower of Hanoi Algorithm


moveTower( height, fromP, toP, useP) if height > 0 moveTower( height-1, fromP, useP, toP) moveDisk( fromP, toP) // base step moveTower( height-1, useP, toP, fromP) moveDisk(ejectP,loadP) print(move disk from ejectP to loadP) main call : moveTower (N, left, middle, right)
CJD

Example
Show how to tile a 2n x 2n checkerboard, n1, with one cell removed, using right triominoes where each piece covers 3 squares as shown. (Basis Step) when n=1, tiling is as shown. (Recursive Step) Suppose n=k>1. Identify the quadrant with the removed cell. Place a tile in the center with coverings in the other 3 quadrants. Treat these 3 covered cells as removed in each quadrant. Then, tile the four 2k-1 x 2k-1 quadrants.
CJD

End

CJD

You might also like