Download as pdf or txt
Download as pdf or txt
You are on page 1of 47

2XB3

Practice and Experience: Binding Theory to Practice

Lecture 2
Programming Model for Implementing Algorithms
(Static Methods)

Reza Samavi

©Reza Samavi 2XB3/ 2XB3 Lecture 2 1


Outline

• Why Study Algorithms?


• Basic programming model for implementing algorithms
(Static Methods)

©Reza Samavi 2XB3/ 2XB3 Lecture 2 2


Why study algorithms?

• Algorithms are interesting


- Useful applications
- Clever (bear mathematical properties)

• Practical side, how to make a good program?


- Careful program design
- Tweaking
- Good algorithm design
§ Our program running fast
§ Our program is efficient

©Reza Samavi 2XB3/ 2XB3 Lecture 2 4


Why study algorithms?
Algorithms and Data Structure
Their impact is broad and far-reaching.

• Algorithm: methods for solving a problem


Internet. Web search, packet routing, distributed file sharing, ... 

Biology. Human genome project, protein folding, …

“An algorithm describes a finite, deterministic, and
Computers. Circuit layout, file system, compilers, …

effective
Computer problem-solving
graphics. method
Movies, video games, suitable
virtual reality, …
 for
implementation as a computer program”
Security. Cell phones, e-commerce, voting machines, …

[Sedgewick and Wyne]
Multimedia. MP3, JPG, DivX, HDTV, face recognition, …

Data
•Social structure:
networks. methods news
Recommendations, to store
feeds, information
advertisements, …

Physics. N-body simulation, particle collision simulation, …


• ⋮Performance is a central consideration in designing


an algorithm

©Reza Samavi 2XB3/ 2XB3 Lecture 2 5 !X


Algorithms are Clever: Sorting problem

©Reza Samavi 2XB3/ 2XB3 Lecture 2 7


Why study algorithms?

• Algorithms are interesting


- Useful applications
- Clever (bear mathematical properties)

• Practical side, how to make a good program?


- Careful program design
- Tweaking
- Good algorithm design
§ Our program running fast
§ Our program is efficient

©Reza Samavi 2XB3/ 2XB3 Lecture 2 12


Programming for Algorithms
• Algorithms are programming language independent
- What you learn in 2C03
• Our study of algorithms is based upon implementing
them as programs written in Java
- What you learn in 2XB3
• Why?
- programs are concise, elegant, and complete descriptions
of algorithms.
- You can put the algorithms immediately to good use in
applications.
- You can run the programs to study properties of the
algorithms.

©Reza Samavi 2XB3/ 2XB3 Lecture 2 13


Outline

• Why Study Algorithms?


• Basic programming model for implementing
algorithms.

©Reza Samavi 2XB3/ 2XB3 Lecture 2 14


A Foundation for Programming

any program you might want to write

objects

functions and modules

graphics, sound, and image I/O

arrays

conditionals and loops

Math text I/O


equivalent
to a calculator
primitive data types assignment statements

15
Built-in Data Types

Data type. A set of values and operations defined on those values.

type set of values literal values operations


'A'
char characters '@' compare

sequences of "Hello World"


String concatenate
characters "126 is fun"

17 add, subtract,
int integers 12345 multiply, divide

double
floating-point 3.1415 add, subtract,
numbers 6.022e23 multiply, divide

true
boolean truth values false and, or, not

16
Basic Definitions

Variable. A name that refers to a value of declared type.


Literal. Programming language representation of a value.
Assignment statement. Associates a value with a variable.

17
Integer Operations

public class IntOps {


public static void main(String[] args) {
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]); command-line
arguments
int sum = a + b;
int prod = a * b;
int quot = a / b;
int rem = a % b;
System.out.println(a + " + " + b + " = " + sum);
System.out.println(a + " * " + b + " = " + prod);
System.out.println(a + " / " + b + " = " + quot);
System.out.println(a + " % " + b + " = " + rem);
}
}
% javac IntOps.java
% java IntOps 1234 99
1234 + 99 = 1333
1234 * 99 = 122166 Java automatically converts
1234 / 99 = 12 a, b , and rem to type String
1234 % 99 = 46

1234 = 12*99 + 46
18
Type Conversion

Type conversion. Convert value from one data type to another.


■ Automatic: no loss of precision; or with strings.
■ Explicit: cast; or method.

19
Data type Summary

A data type is a set of values and operations on those values.


■ String text processing.
■ double, int mathematical calculation.
■ boolean decision making.

In Java, you must:


■ Declare type of values.
■ Convert between types when necessary.

Why do we need types?


■Type conversion must be done at some level.
example of bad type conversion
■Compiler can help do it correctly.
■Ex 1: in 1996, Ariane 5 rocket exploded after
takeoff because of bad type conversion.
■Ex 2: i = 0 in Matlab redefines √-1.

21
A Foundation for Programming

any program you might want to write

objects

functions and modules

graphics, sound, and image I/O

arrays

conditionals and loops to infinity


and beyond!
Math text I/O

primitive data types assignment statements

22
If Statement

The if statement. A common branching structure.


■ Evaluate a boolean expression.
■ If true, execute some statements.
■ If false, execute other statements.

if (boolean expression) { boolean expression


statement T;
} can be any sequence
else { true false
of statements
statement F;
}
statement T statement F

23
If Statement Examples

24
While Loop

The while loop. A common repetition structure.


■Evaluate a boolean expression.
■If true, execute some statements.
■Repeat.

loop continuation condition

statement 2
while (boolean expression) {
statement 1;
statement 2; loop body
true
} boolean expression statement 1

false

25
While Loop Challenge

26
For Loops

The for loop. Another common repetition structure.


■Execute initialization statement.
■Evaluate a boolean expression.
■If true, execute some statements.
■And then the increment statement.
■Repeat.
loop continuation condition
init
increment
for (init; boolean expression; increment) {
statement 1;
statement 2;
statement 2
} body

true
boolean expression statement 1

false

27
Loop Examples

29
Control Flow Summary

Control flow.
■ Sequence of statements that are actually executed in a program.
■ Conditionals and loops: enable us to choreograph the control flow.

Control Flow Description Examples

straight-line all statements are


programs executed in the order given

certain statements are


if
conditionals executed depending on the
if-else
values of certain variables

certain statements are while


loops executed repeatedly until for
certain conditions are met do-while

30
A Foundation for Programming

any program you might want to write

objects

functions and modules

graphics, sound, and image I/O

arrays

conditionals and loops

Math text I/O

primitive data types assignment statements

31
Arrays

Problem: Store and manipulate huge quantities of data.

Array. Indexed sequence of values of the same type.

Examples. index value


■ 52 playing cards in a deck. 0 wayne
■ 5 thousand undergrads at McMaster. 1 rs
■ 1 million characters in a book. 2 doug
■ 10 million audio samples in an MP3 file. 3 dgabai
■ 4 billion nucleotides in a DNA strand. 4 maia
■ 73 billion Google queries per year. 5 llp
■ 50 trillion cells in the human body. 6 funk
7 vertanen
■ 6.02 ´ 1023 particles in a mole.

32
Many Variables of the Same Type

Goal. 10 variables of the same type.

// tedious and error-prone


double a0, a1, a2, a3, a4, a5, a6, a7, a8, a9;
a0 = 0.0;
a1 = 0.0;
a2 = 0.0;
a3 = 0.0;
a4 = 0.0;
a5 = 0.0;
a6 = 0.0;
a7 = 0.0;
a8 = 0.0;
a9 = 0.0;

a4 = 3.0;

a8 = 8.0;

double x = a4 + a8;

33
Many Variables of the Same Type

Goal. 10 variables of the same type.

// easy alternative
double[] a = new double[10];

a[4] = 3.0;
… declares, creates,
a[8] = 8.0; and initializes
… [stay tuned for
double x = a[4] + a[8]; details]

34
Arrays in Java

Java has special language support for arrays.


■ To make an array: declare, create, and initialize it.
■ To access entry i of array named a, use a[i].
■ Array indices start at 0.

int N = 10; // size of array


double[] a; // declare the array
a = new double[N]; // create the array
for (int i = 0; i < N; i++) // initialize the array
a[i] = 0.0; // all to 0.0

36
Arrays in Java

Java has special language support for arrays.


■ To make an array: declare, create, and initialize it.
■ To access entry i of array named a, use a[i].
■ Array indices start at 0.

int N = 10; // size of array


double[] a; // declare the array
a = new double[N]; // create the array
for (int i = 0; i < N; i++) // initialize the array
a[i] = 0.0; // all to 0.0

Compact alternative.
■Declare, create, and initialize in one statement.
■Default initialization: all numbers automatically set to zero.

int N = 10; // size of array


double[] a = new double[N]; // declare, create, init

37
A Foundation for Programming

any program you might want to write

objects
build bigger programs
functions and modules and reuse code

graphics, sound, and image I/O

arrays

conditionals and loops

Math text I/O

primitive data types assignment statements

38
Functions

x
y

z
f f (x, y, z)
Functions (Static Methods)

Java function.
■ Takes zero or more input arguments.
■ Returns one output value.
■ Side effects (e.g., output to standard draw). more general than
mathematical functions

Applications.
■Scientists use mathematical functions to calculate formulas.
■Programmers use functions to build modular programs.
■You use functions for both.

Examples.
■ Built-in functions: Math.random(), Math.abs(), Integer.parseInt().
■ Our I/O libraries: StdIn.readInt(), StdDraw.line(), StdAudio.play().
■ User-defined functions: main().

40
Anatomy of a Java Function

Java functions. Easy to write your own.

input output
2.0 f(x) = Öx 1.414213…

41
Flow of Control

Key point. Functions provide a new way to control the flow of execution.

implicit return statement


at end of void function
42
Flow of Control

Key point. Functions provide a new way to control the flow of execution.

What happens when a function is called:


■Control transfers to the function code.
■Argument variables are assigned the values given in the call.
■Function code is executed.
■Return value is assigned in place of the function name in calling code.
■Control transfers back to the calling code.

Note. This is known as "pass by value."

43
Scope

Scope (of a name). The code that can refer to that name.
Ex. A variable's scope is code following the declaration in the block.

Best practice: declare variables to limit their scope.


44
Gaussian Distribution

Standard Gaussian distribution.


■ "Bell curve."
■ Basis of most statistical analysis in social and physical sciences.

Ex. 2000 SAT scores follow a Gaussian distribution with


mean µ = 1019, stddev s = 209.

601 810 1019 1228 1437

50
Java Function for f(x)

Mathematical functions. Use built-in functions when possible;


build your own when not available.

public class Gaussian {


public static double phi(double x) {
return Math.exp(-x*x / 2) / Math.sqrt(2 * Math.PI);
}

public static double phi(double x, double mu, double sigma) {


return phi((x - mu) / sigma) / sigma;
}
}

Overloading. Functions with different signatures are different.


Multiple arguments. Functions can take any number of arguments.
Calling other functions. Functions can call other functions.

library or user-defined

51
Building Functions

Functions enable you to build a new layer of abstraction.


■ Takes you beyond pre-packaged libraries.
■ You build the tools you need: Gaussian.phi(), …

Process.
■ Step 1: identify a useful feature.
■ Step 2: implement it.
■ Step 3: use it.

■ Step 3': re-use it in any of your programs.

52
Libraries

Library. A module whose methods are primarily intended for use


by many other programs.

Client. Program that calls a library.

API. Contract between client and


implementation.

Implementation. Program that


implements the methods in an API.

53
Using a Library
public class RandomPoints {
public static void main(String args[]) {
int N = Integer.parseInt(args[0]);
for (int i = 0; i < N; i++) {
double x = StdRandom.gaussian(0.5, 0.2);
double y = StdRandom.gaussian(0.5, 0.2);
StdDraw.point(x, y);
}
} use library name
} to invoke method
% javac RandomPoints.java
% java RandomPoints 10000

©Reza Samavi 2XB3/ 2XB3 Lecture 2 54 54


Libraries and Clients

• You can write your program using only static


methods!
• Each program that you have written consists of Java
code that resides in a single .java file.
• You can extend the Java language by developing
libraries of static methods for use by any other
program, keeping each library in its own file.
• It enables modular programming, where we divide a
program up into static methods, grouped together in
some logical way.

©Reza Samavi 2XB3/ 2XB3 Lecture 2 56


Modular Programming
• Modular programming.
- Divide program into self-contained pieces.
- Test each piece individually.
- Combine pieces to make program.

• Ex. Flip N coins. How many heads?


- Read arguments from user.
- Flip one fair coin.
- Flip N fair coins and count number of heads.
- Repeat simulation, counting number of times each outcome occurs.
- Plot histogram of empirical results.
- Compare with theoretical predictions.

©Reza Samavi 2XB3/ 2XB3 Lecture 2 57 57


Bernoulli Trials
public class Bernoulli {
public static int binomial(int N) { flip N fair coins;
int heads = 0;
for (int j = 0; j < N; j++) return # heads
if (StdRandom.bernoulli(0.5)) heads++;
return heads;
}

public static void main(String[] args) {


int N = Integer.parseInt(args[0]);
int T = Integer.parseInt(args[1]);

int[] freq = new int[N+1]; perform T trials


for (int i = 0; i < T; i++)
freq[binomial(N)]++;
of N coin flips each

double[] normalized = new double[N+1]; plot histogram


for (int i = 0; i <= N; i++)
normalized[i] = (double) freq[i] / T; of number of heads
StdStats.plotBars(normalized);

double mean = N / 2.0, stddev = Math.sqrt(N) / 2.0;


double[] phi = new double[N+1];
for (int i = 0; i <= N; i++)
phi[i] = Gaussian.phi(i, mean, stddev);
StdStats.plotLines(phi);
} theoretical prediction
}
©Reza Samavi 2XB3/ 2XB3 Lecture 2 58 58
©Reza Samavi 2XB3/ 2XB3 Lecture 2 59
Dependency Graph

• Modular programming. Build relatively complicated


program by combining several small, independent,
modules.

©Reza Samavi 2XB3/ 2XB3 Lecture 2 60 ‹#›


Libraries

• Why use libraries?


- Makes code easier to understand.
- Makes code easier to debug.
- Makes code easier to maintain and improve.
- Makes code easier to reuse.

©Reza Samavi 2XB3/ 2XB3 Lecture 2 61 61


Libraries

• Why use libraries?


- Makes code easier to understand.
- Makes code easier to debug.
- Makes code easier to maintain and improve.
- Makes code easier to reuse.

Can We Do Better than Static Methods to even better programs?


(Next lecture)

©Reza Samavi 2XB3/ 2XB3 Lecture 2 62 62

You might also like