Algorithms 1 (Lecture 3 of CSE1301)

advertisement

CSE1301

Computer Programming

Lecture 2:

Introduction to Algorithms

1

Topics

• A little bit of History:

The idea of a universal machine

• Systematic problem solving

• Algorithms

2

A “Universal” Machine

• Programmability

• Binary Code (everything stored as “0” or “1”)

• Program and Data are represented in the same form

• Electronic Components

3

Early Computing History in a Nutshell

200 BC - today Abacus

~1670

Leibniz’

“Rechenmaschine”

~1750

1820

1838

1841

Jacquard’s Loom

Babbage’s

“Difference Engine”

Babbage’s

“Analytical Engine”

Ada Byron,

Lady Lovelace

-

Fixed calculations

+, -, *, /, sqrt

Punch Tape

Fixed

Table Calculation

Program & Data separate

First program

(Bernoulli-

Numbers)

4

Leibniz’s Step Reckoner

• Gottfried von Leibniz

1670

• Add, subtract, multiply, divide, square roots

5

Jacquard’s punch card

• Joseph Marie Jacquard 1804

• punch cards used to operate loom

6

Babbage’s analytical engine

• Charles Babbage

(1791-1871)

Design for the analytical engine

Programmable, but mechanical.

Data+Program separate

7

One of the first Programmers

• Lady Ada Augusta

Byron, Countess of

Lovelace (1815-1952)

(1791-1871)

8

Early Computing History in a Nutshell

200 BC - today Abacus

~1670

~1750

Leibniz’

“Rechenmaschine”

Jacquard’s Loom

1820

1838

Babbage’s

“Difference Engine”

Babbage’s

“Analytical Engine”

1841 Ada Byron,

Lady Lovelace

-

Fixed calculations

Punch Tape

Fixed

Table Calculation

Program & Data separate

First program

(Bernoulli-

Numbers)

First universal programmable machine

9

Binary code universal electronic

Integration:

•fully electronic

•binary

•Programmable

•universal

1943

1946

1949

The Ancestors

1886

1936

1941-1944

1944

Hollerith Punch Cards

Zuse Z1 Relais

Zuse Z3/Z4

“Plankalkül”

Mark I Partly mechanic

Colossus

Eniac

Manchester

Universal

Computer

Not universal

Programmable with

Re-wiring

Cathode-Ray

Tube Memory

10

Hollerith’s Tabulating Machine

• Herman Hollerith (1860-1929)

• Invented a punched card device to help analyse the 1890 US census data

• Founded “Tabulating Machine Company”

1896

• 1924 – Tabulating Machine Company merges with others to form IBM

Binary but not really programmable

11

Konrad Zuse

• 1936: Z1 first binary computer using

Erector Set parts, keyboard and lights for output (relay memory)

• 1938: Z2 – using punched tape and relays

Z1

Z3: fully binary, fully programmable, but partly mechanical 12

Colossus

• 1943 British Colossus –

(1,500 vacuum tubes)

– Decipher enigma coded messages at 5,000 chars/sec

– At peak, 10 machines ran 24 hours a day in Bletchley Park

A German enigma coding machine

Fully electronic, but not universally programmable 13

Harvard Mark 1

• 1943-44 Aiken at Harvard/IBM “Mark 1” – first electromechanical digital computer (electromagnetic relays – magnets open and close metal switches)

(recreation of Analytical Engine)

– 8 ft tall, 50 ft long, 1 million parts

– 323 decimal-digit additions per sec

– storage for 72 23-digit numbers.

Universal, fully programmable

But partly mechanical

14

ENIAC (1946)

• 18,000 tubes, 1500 sq ft

• Programmed by wire plugs into panels

– 5,000 decimal-digit additions/sec

– 20 10-decimal digit “accumulators”

Von Neumann and ENIAC

Fully electronic, programmable

But extensive-wiring required

15

Experminental MUC (1949)

• 1941 Von Neumann proposes

EDVAC – Electronic Discrete

Variable Computer

• Computer should

– Use binary

– Have stored programs

– Be function-oriented

• Team Members from

Bletchley Park and EDVAC/ENIAC collaborate at Manchester

• Add CRT display memory

First fully electronic universal computer

16

The Problem-solving

Process

Analysis

Problem specification

Design

Algorithm

Implementation

Program

Compilation

Executable

( solution

)

"Doctor, my head hurts"

Patient has elevated pressure in anterior parietal lobe

1. Sterilize cranial saw

2. Anaesthetize patient

3. Remove top of skull

4. Get the big spoon...

5. etc., etc. sterilize(saw,alcohol); raise_hammer(); lower hammer(fast); start(saw);

/* etc. etc. */

0100111010110010101010101

0010101010101001100101010

1010100101101001110101010

1010010010111010011110101

010111110101010001101…

18

The Problem-solving

Process

" Doctor , my head hurts"

Analysis

Problem specification

Design

Algorithm

Implementation

Program

Compilation

Executable

(solution)

Patient has elevated pressure in anterior parietal lobe.

1. Sterilize cranial saw

2. Anaesthetize patient

3. Remove top of skull

4. Get the big spoon...

5. etc., etc. sterilize(saw,alcohol); raise_hammer(); lower hammer(fast); start(saw);

/* etc. etc. */

01001110101100101010101010010

10101010100110010101010101001

011010011101010101010010010111

010011110101010111110101010001

10100001101...

19

The Problem-solving

Process

Analysis

Problem specification

Design

Algorithm

Implementation

Program

Compilation

Executable

(solution)

20

Algorithm

• A sequence of instructions specifying the steps required to accomplish some task

• Named after:

Muhammad ibn Musa al-Khwarizmi

of Khowarezm (now Khiva in Uzbekistan )

Circa 780-850 C.E. (Common Era)

21

Algorithm –History

Muhammad ibn Musa Al-Khwarizmi http://www-groups.dcs.st-andrews.ac.uk/~history/Mathematicians/Al-Khwarizmi.html

• Book on arithmetic:

– Hindu numeration, decimal numbers, use of zero, method for finding square root

– Latin translation (c.1120 CE): “

Algoritm i de numero Indorum ”

• Book on algebra

Hisab al-jabr w’al-muqabala

22

Algorithm – Working Definition

• A sequence of instructions describing how to do a task

[ As opposed to actually executing the instructions]

23

Algorithm -- Examples

• A cooking recipe

• Assembly instructions for a model

• The rules of how to play a game

• VCR instructions

Description of a martial arts technique

Directions for driving from A to B

• A knitting pattern

• A car repair manual

24

Algorithm – Examples (cont)

• Recipe for Almond and honey slice

• Recipe for Arroz con pollo

25

Almond and Honey Slice

1/2 quantity Shortcrust

Pastry

185 g unsalted butter

100 g castor sugar

5 tablespoons honey

50 ml cream

50 ml brandy or any other liqueur or spirit

300 g flaked almonds

From: Stephanie Alexander, The

Cook’s Companion, Viking/Penguin,

Ringwood, Victoria, 1996, p. 349.

Preheat oven for 200° C

Line a 30 cm

20 cm baking tray with baking paper, and then with pastry

Bake blind for 20 minutes, then remove weights and foil

Turn oven up to 220° C.

Bring remaining ingredients to a boil, stirring.

Spread evenly over pastry.

Bake until topping is bubbling and has caramelised evenly, about 15 minutes.

Cool before cutting into fingers or squares.

26

Almond and Honey Slice

1/2 quantity Shortcrust

Pastry

185 g unsalted butter

100 g castor sugar in the order in which

5 tablespoons honey they are performed

(“executed”)

50 ml brandy or any other liqueur or spirit

300 g flaked almonds

From: Stephanie Alexander, The

Cook’s Companion, Viking/Penguin,

Ringwood, Victoria, 1996, p. 349.

Preheat oven for 200° C

Line a 30 cm

20 cm baking tray with baking paper, and then with pastry

Bake blind for 20 minutes, then remove weights and foil

Turn oven up to 220° C.

Bring remaining ingredients to a boil, stirring.

Spread evenly over pastry.

Bake until topping is bubbling and has caramelised evenly, about 15 minutes.

Cool before cutting into fingers or squares.

27

Correct Algorithm?

Cut chicken into pieces and brown the pieces on all sides in a casserole dish in hot olive oil.

Remove the chicken and to the juices in the casserole add garlic, onions and green peppers, and sauté until onion is golden.

Add bay leaf, whole tomatoes, and chicken broth.

When the broth boils add salt, saffron and rice.

Arrange chicken on rice, cover casserole and bake in a moderate oven

(350°F) for 20 minutes or until the rice is tender.

Add beans and artichokes during last 10 minutes of cooking.

From: “Arroz Con Pollo” in The

Margaret Fulton Cookbook, Hamlyn,

Sydney, 1968.

28

Correct Algorithm?

Cut chicken into pieces and brown the pieces on all sides in a casserole dish in hot olive oil.

Remove the chicken and to the juices in the casserole add garlic, onions and green peppers, and sauté until onion is golden.

Add bay leaf, whole tomatoes, and chicken broth.

When the broth boils add salt, saffron and rice.

Arrange chicken on rice, cover casserole and bake in a moderate oven

(350°F) for 20 minutes or until the rice is tender.

Add beans and artichokes during last 10 minutes of cooking.

From: “Arroz Con Pollo” in The

Margaret Fulton Cookbook, Hamlyn,

Sydney, 1968.

29

Correct Algorithm?

Cut chicken into pieces and brown the pieces on all sides in a casserole dish in hot olive oil.

Remove the chicken and to the juices in the casserole add garlic, onions and green peppers, and sauté until onion is golden.

Add bay leaf, whole tomatoes, and chicken broth.

When the broth boils add salt, saffron and rice.

Arrange chicken on rice, cover casserole and bake in a moderate oven

(350°F) for 10 minutes.

Add beans and artichokes.

Cover, and bake for another

10 minutes or until rice is tender.

30

From Algorithms to Programs

Problem

Algorithm : A sequence of instructions describing how to do a task (or process)

C Program

31

Components of an Algorithm

• Variables and values

• Instructions

– Sequences

– Procedures

– Selections

– Repetitions

Also required: Documentation

32

Values

• Represent quantities, amounts or measurements

• May be numerical or alphabetical

(or other things)

• Often have a unit related to their purpose

• Example:

– Recipe ingredients

33

Almond and Honey Slice

1/2 quantity Shortcrust

Pastry

185 g unsalted butter

100 g castor sugar

5 tablespoons honey

50 ml cream

50 ml brandy or any other liqueur or spirit

300 g flaked almonds

From: Stephanie Alexander, The

Cook’s Companion, Viking/Penguin,

Ringwood, Victoria, 1996, p. 349.

Preheat oven for 200° C

Line a 30 cm

20 cm baking tray with baking paper, and then with pastry

Bake blind for 20 minutes, then remove weights and foil

Turn oven up to 220° C.

Bring remaining ingredients to a boil, stirring.

Spread evenly over pastry.

Bake until topping is bubbling and has caramelised evenly, about 15 minutes.

Cool before cutting into fingers or squares.

34

Almond and Honey Slice

1/2 quantity Shotcrust Pastry

185 g unsalted butter

100 g castor sugar

5 tablespoons honey

50 ml cream

50 ml brandy or any other liqueur or spirit

300 g flaked almonds

From: Stephanie Alexander, The

Cook’s Companion, Viking/Penguin,

Ringwood, Victoria, 1996, p. 349.

Preheat oven for 200° C

Line a 30 cm

20 cm baking tray with baking paper , and then with pastry

Bake blind for 20 minutes , then remove weights and foil

Turn oven up to 220° C .

Bring remaining ingredients to a boil, stirring.

Spread evenly over pastry.

Bake until topping is bubbling and has caramelised evenly, about 15 minutes .

Cool before cutting into fingers or squares.

35

Variables

• Are containers for values

– places to store values

• Example:

Variable Values

This jar can contain

10 cookies

50 grams of sugar

3 slices of cake etc.

36

Restrictions on Variables

• Variables may be restricted to contain a specific type of value

37

Components of an Algorithm

Values and Variables

• Instruction (a.k.a. primitive)

• Sequence (of instructions)

• Procedure (involving instructions)

• Selection (between instructions)

• Repetition (of instructions)

Documentation (beside instructions)

38

Summary

• Problem Solving Process

• Algorithms

• Components of Algorithms

– Values and Variables

– Instructions

– to be continued...

Reading

• Deitel & Deitel,

C: How to program

– Chapter 3, Sections 3.1 to 3.3

39

Download