introduction to computer science - idc to /intro1/ literature.htm p o si bec ur k : • “j av: n...

26
1 10 October 2007 © Ariel Shamir 1 Introduction to Computer Science Ariel Shamir, Inter Disciplinary Center Herzliya Israel, Fall 2007 Ariel Shamir, Ariel Shamir, Inter Disciplinary Center Inter Disciplinary Center Herzliya Herzliya Israel, Fall Israel, Fall 2007 2007 10 October 2007 © Ariel Shamir 2 Course Overview Introduce basic terms and concepts of computer science Acquire basic programming skills Understand general principles in CS Learn a modern object oriented programming language Introduce basic terms and concepts Introduce basic terms and concepts of computer science of computer science Acquire basic programming skills Acquire basic programming skills Understand general principles in CS Understand general principles in CS Learn a modern object oriented Learn a modern object oriented programming language programming language

Upload: buiquynh

Post on 17-Jun-2019

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to Computer Science - IDC to /intro1/ Literature.htm P o si bec ur k : • “J av: n itrou cC S& P gm ”, W .h • "J aS oftw r el u i ns- F d P gm D ”, J hn Lew i

1

10 October 2007 © Ariel Shamir 1

Introduction to Computer Science

Ariel Shamir,

Inter Disciplinary Center

Herzliya Israel, Fall 2007

Ariel Shamir, Ariel Shamir,

Inter Disciplinary Center Inter Disciplinary Center

HerzliyaHerzliya Israel, Fall Israel, Fall 20072007

10 October 2007 © Ariel Shamir 2

Course Overview•Introduce basic terms and concepts of computer science

•Acquire basic programming skills

•Understand general principles in CS

•Learn a modern object oriented programming language

••Introduce basic terms and concepts Introduce basic terms and concepts of computer scienceof computer science

••Acquire basic programming skills Acquire basic programming skills

••Understand general principles in CSUnderstand general principles in CS

••Learn a modern object oriented Learn a modern object oriented programming languageprogramming language

Page 2: Introduction to Computer Science - IDC to /intro1/ Literature.htm P o si bec ur k : • “J av: n itrou cC S& P gm ”, W .h • "J aS oftw r el u i ns- F d P gm D ”, J hn Lew i

2

10 October 2007 © Ariel Shamir 3

Course MethodologyLecturesReading & booksRecitationsProgramming exercises & tutorsThe course site & the web

Support Structures FOR YOU!

LecturesLecturesReading & booksReading & booksRecitationsRecitationsProgramming exercises & tutorsProgramming exercises & tutorsThe course site & the webThe course site & the web

Support Structures FOR YOU!Support Structures FOR YOU!

10 October 2007 © Ariel Shamir 4

The Course Web SiteAdministration & regulation

Information & downloads

Exercises (receive & submit)

Newsgroup!!!

Go to:www1.idc.ac.il/coursesftp/cs/intro1/

Administration & regulationAdministration & regulation

Information & downloadsInformation & downloads

Exercises (receive & submit)Exercises (receive & submit)

Newsgroup!!!Newsgroup!!!

Go to:Go to:www1.idc.ac.il/coursesftp/cs/intro1/

Page 3: Introduction to Computer Science - IDC to /intro1/ Literature.htm P o si bec ur k : • “J av: n itrou cC S& P gm ”, W .h • "J aS oftw r el u i ns- F d P gm D ”, J hn Lew i

3

10 October 2007 © Ariel Shamir 5

Information SourcePossible course books:• “Java: and introduction to CS & Programming”, W. Savitch.

• "Java Software Solutions - Foundations of Program Design”, John Lewis & William Loftus.

• "The Java Tutorial“, Mary Campione, Kathy Walrath.

• "The Java Programming Language“, Ken Arnold, James Gosling.

• More books…(check the site!):

Go to www1.idc.ac.il/coursesftp/cs/intro1/

Literature.htm

Possible course books:Possible course books:•• ““Java: and introduction to CS & ProgrammingJava: and introduction to CS & Programming””, W. , W. SavitchSavitch..

•• "Java Software Solutions "Java Software Solutions -- Foundations of Program DesignFoundations of Program Design””, , John Lewis & William Loftus.John Lewis & William Loftus.

•• "The Java Tutorial"The Java Tutorial““, Mary , Mary CampioneCampione, Kathy , Kathy WalrathWalrath. .

•• "The Java Programming Language"The Java Programming Language““, Ken Arnold, James , Ken Arnold, James Gosling.Gosling.

•• More booksMore books……(check the site!):(check the site!):

Go toGo to www1.idc.ac.il/coursesftp/cs/intro1/

Literature.htm

10 October 2007 © Ariel Shamir 6

Lecture Overview1. What is a computer?

2. How is a typical computer built

3. Algorithms

4. Programming

5. Introducing Java

1.1. What is a computer?What is a computer?

2.2. How is a typical computer builtHow is a typical computer built

3.3. AlgorithmsAlgorithms

4.4. ProgrammingProgramming

5.5. Introducing Java Introducing Java

Page 4: Introduction to Computer Science - IDC to /intro1/ Literature.htm P o si bec ur k : • “J av: n itrou cC S& P gm ”, W .h • "J aS oftw r el u i ns- F d P gm D ”, J hn Lew i

4

10 October 2007 © Ariel Shamir 7

Why Use Computers?Computers are very good at:

• processing data in a short time,

• storing and retrieving large bulks of data,

• transferring large bulks of data across sites

Things that people are bad at, or need help at or simply hate to do.

Computers are very good at:Computers are very good at:

•• processing data in a short time,processing data in a short time,

•• storing and retrieving large bulks of data,storing and retrieving large bulks of data,

•• transferring large bulks of data across sitestransferring large bulks of data across sites

Things that people are bad at, or need help Things that people are bad at, or need help at or simply hate to do.at or simply hate to do.

10 October 2007 © Ariel Shamir 8

What Is a Computer?Computers are computational machines that can carry out given tasks…(???)

Computational machines: hardware

Given tasks: software

Computers are Computers are computational machines that computational machines that can carry out given taskscan carry out given tasks……(???)(???)

Computational machines: Computational machines: hardwarehardware

Given tasks: Given tasks: softwaresoftware

Page 5: Introduction to Computer Science - IDC to /intro1/ Literature.htm P o si bec ur k : • “J av: n itrou cC S& P gm ”, W .h • "J aS oftw r el u i ns- F d P gm D ”, J hn Lew i

5

10 October 2007 © Ariel Shamir 9

“Computational Machine”

Do it faster, and do it with larger amounts of data. The main parameters are therefore

Time and space!

Do it faster, and do it with larger amounts Do it faster, and do it with larger amounts of data. The main parameters are thereforeof data. The main parameters are therefore

TimeTime and and spacespace!!

Storage Computation Network

10 October 2007 © Ariel Shamir 10

Time & Space!

Page 6: Introduction to Computer Science - IDC to /intro1/ Literature.htm P o si bec ur k : • “J av: n itrou cC S& P gm ”, W .h • "J aS oftw r el u i ns- F d P gm D ”, J hn Lew i

6

10 October 2007 © Ariel Shamir 11

The Machine (Hardware)

Storage Computation Network

Memory Disks CPU I/O Network

Other peripherals: Video, Audio…

10 October 2007 © Ariel Shamir 12

John Von Neumann1945: John von Neumann wrote "First Draft of a Report on the EDVAC" in which he outlined the architecture of a stored-program computer.

1945: John von 1945: John von Neumann wrote "First Neumann wrote "First Draft of a Report on Draft of a Report on the EDVAC" in which the EDVAC" in which he outlined the he outlined the architecture of a architecture of a storedstored--program program computer.computer.

http://inventors.about.com/library/blcoindex.htm

Page 7: Introduction to Computer Science - IDC to /intro1/ Literature.htm P o si bec ur k : • “J av: n itrou cC S& P gm ”, W .h • "J aS oftw r el u i ns- F d P gm D ”, J hn Lew i

7

10 October 2007 © Ariel Shamir 13

Where We Were

The ENIAC contained 17,468 vacuum tubes, along with 70,000 resistors, 10,000 capacitors, 1,500 relays, 6,000 manual switches and 5 million soldered joints. It covered 1800 square feet (167 square meters) of floor space, weighed 30 tons, consumed 160 kilowatts of electrical power. In one second, the ENIAC could perform 5,000 additions, 357 multiplications or 38 divisions.

The ENIAC contained 17,468 vacuum tubes, along The ENIAC contained 17,468 vacuum tubes, along with 70,000 resistors, 10,000 capacitors, 1,500 with 70,000 resistors, 10,000 capacitors, 1,500 relays, 6,000 manual switches and 5 million relays, 6,000 manual switches and 5 million soldered joints. It covered 1800 square feet (167 soldered joints. It covered 1800 square feet (167 square meters) of floor space, weighed 30 tons, square meters) of floor space, weighed 30 tons, consumed 160 kilowatts of electrical power. In consumed 160 kilowatts of electrical power. In one second, the ENIAC could perform 5,000 one second, the ENIAC could perform 5,000 additions, 357 multiplications or 38 divisions. additions, 357 multiplications or 38 divisions.

10 October 2007 © Ariel Shamir 14

Today…Blue Gene (LLNL) is the world's fastest computer, at 360 TFLOPS (1000000000000 = 1012 FLOPS):

Cluster of nodes where up to 1024 Compute nodes plus additional IO nodes in a standard 19" cabinet

Blue Gene Blue Gene (LLNL) is the (LLNL) is the world's fastest computer, at world's fastest computer, at 360 TFLOPS (1000000000000 360 TFLOPS (1000000000000 = 10= 101212 FLOPS):FLOPS):

Cluster of nodes where up to Cluster of nodes where up to 1024 Compute nodes plus 1024 Compute nodes plus additional IO nodes in a additional IO nodes in a standard 19" cabinet standard 19" cabinet

Page 8: Introduction to Computer Science - IDC to /intro1/ Literature.htm P o si bec ur k : • “J av: n itrou cC S& P gm ”, W .h • "J aS oftw r el u i ns- F d P gm D ”, J hn Lew i

8

10 October 2007 © Ariel Shamir 15

����

�����

��

���

����

�����

����

Long term storage

�������

�������

I/O devices

Von - Newman Architecture

10 October 2007 © Ariel Shamir 16

Opening the Hood of Your PC

Memory Chips

CPUChip

MonitorPeripherials

Motherboard

Page 9: Introduction to Computer Science - IDC to /intro1/ Literature.htm P o si bec ur k : • “J av: n itrou cC S& P gm ”, W .h • "J aS oftw r el u i ns- F d P gm D ”, J hn Lew i

9

10 October 2007 © Ariel Shamir 17

Computer Architecture•The CPU and main memory are the two key hardware components•All other devices are considered peripherals. Controllers coordinate the activities of specific peripherals•Binary information moves between devices across a group of wires called a bus

••The The CPUCPU and and main memorymain memory are the two are the two key hardware componentskey hardware components

••All other devices are considered All other devices are considered peripherals. Controllersperipherals. Controllers coordinate the coordinate the activities of specific peripheralsactivities of specific peripherals

••Binary information moves between Binary information moves between devices across a group of wires called a devices across a group of wires called a busbus

10 October 2007 © Ariel Shamir 18

The Bus

Videocontroller Controller

Monitor Harddisk

Diskcontroller

MainmemoryCPU

Floppydisk

otherperipherals

Bus

Page 10: Introduction to Computer Science - IDC to /intro1/ Literature.htm P o si bec ur k : • “J av: n itrou cC S& P gm ”, W .h • "J aS oftw r el u i ns- F d P gm D ”, J hn Lew i

10

10 October 2007 © Ariel Shamir 19

Memory HierarchyThe computer memory divided into:• Main memory.

• Secondary memory (disks, flash, tapes).

Main memory is volatile - stored information is lost if the electric power is removed, but it is FAST!Secondary memory devices are nonvolatile, but generally much slower.

The computer memory divided into:The computer memory divided into:•• Main memory.Main memory.

•• Secondary memory (disks, Secondary memory (disks, flash, tapesflash, tapes).).

Main memory is volatile Main memory is volatile -- stored stored information is lost if the electric power is information is lost if the electric power is removed, but it is FAST!removed, but it is FAST!

Secondary memory devices are Secondary memory devices are nonvolatile, but generally much slower.nonvolatile, but generally much slower.

10 October 2007 © Ariel Shamir 20

Storage Capacity UnitsEvery memory device has a storage capacity, indicating the number of bytes it can hold.

Every memory device has a storage Every memory device has a storage capacity, indicating the number of bytes it capacity, indicating the number of bytes it can hold.can hold.

224040 (over 1 trillion)(over 1 trillion)TBTBterabyteterabyte223030 (over 1 billion)(over 1 billion)GBGBgigabytegigabyte

222020 = 1048576= 1048576MBMBmegabytemegabyte221010 = 1024= 1024KBKBkilobytekilobyte

Actual sizeActual sizeSymbolSymbolCapacity unitCapacity unit

Page 11: Introduction to Computer Science - IDC to /intro1/ Literature.htm P o si bec ur k : • “J av: n itrou cC S& P gm ”, W .h • "J aS oftw r el u i ns- F d P gm D ”, J hn Lew i

11

10 October 2007 © Ariel Shamir 21

Main Memory•The main memory is divided into memory locations called cells.

•Each memory location has an address which uniquely identifies it.

•Data stored in the main memory can be stored in one or more consecutive memory locations.

• It is termed RAM – Random Access, as access to any random address takes (approximately) the same (compare for instance to tapes…)

••The main memory is divided into memory The main memory is divided into memory locations called cells. locations called cells.

••Each memory location has an address which Each memory location has an address which uniquely identifies it.uniquely identifies it.

••Data stored in the main memory can be stored Data stored in the main memory can be stored in one or more consecutive memory in one or more consecutive memory locations.locations.

•• It is termed RAM It is termed RAM –– Random Access, as access Random Access, as access to any random address takes (approximately) the to any random address takes (approximately) the same (compare for instance to tapessame (compare for instance to tapes……))

10 October 2007 © Ariel Shamir 22

Main Memory Cells

3456

011010103457

3458

3459

3500

3501

Memory location - cell(usually holds 8 bits,

or 1 byte)

Each memory cell has aunique numeric addresswhich identifies it

Page 12: Introduction to Computer Science - IDC to /intro1/ Literature.htm P o si bec ur k : • “J av: n itrou cC S& P gm ”, W .h • "J aS oftw r el u i ns- F d P gm D ”, J hn Lew i

12

10 October 2007 © Ariel Shamir 23

Main Memory Cells

3456

3457

3458

3459

3500

3501 Values larger than8 bits are stored in consecutive cellsThe address of such values will

be the address of the first byte of the cells holding the value

01101010

10110001

10 October 2007 © Ariel Shamir 24

The Central Processing Unit•A central processing unit (CPU) is also called a microprocessor•The CPU is a single "chip" on the order of one square inch in size•It is the brain of the computer•The job of the CPU is to execute programs: it continuously follows the fetch-decode-execute cycle

••A central processing unit (CPU) is A central processing unit (CPU) is also called a microprocessoralso called a microprocessor

••The CPU is a single "chip" on the The CPU is a single "chip" on the order of one square inch in sizeorder of one square inch in size••It is the brain of the computerIt is the brain of the computer

••The job of the CPU is to execute The job of the CPU is to execute programs: it continuously follows programs: it continuously follows the fetchthe fetch--decodedecode--execute cycleexecute cycle

Page 13: Introduction to Computer Science - IDC to /intro1/ Literature.htm P o si bec ur k : • “J av: n itrou cC S& P gm ”, W .h • "J aS oftw r el u i ns- F d P gm D ”, J hn Lew i

13

10 October 2007 © Ariel Shamir 25

Inside the CPU

Arithmetic / Logic Unit

Registers

Control Unit

Small memory units capable of storing information

Performs calculations and decisions

Coordinates processing steps

10 October 2007 © Ariel Shamir 26

Machine Execution?How does a piece of physical hardware machine carry out instructions?

How does a piece of physical How does a piece of physical hardware machine carry out hardware machine carry out instructions?instructions?

Page 14: Introduction to Computer Science - IDC to /intro1/ Literature.htm P o si bec ur k : • “J av: n itrou cC S& P gm ”, W .h • "J aS oftw r el u i ns- F d P gm D ”, J hn Lew i

14

10 October 2007 © Ariel Shamir 27

Executing a Program•Programs are represented digitally in machine language as a list of numbers.•Each number represents either the code of an instruction or some data.•The program is loaded from disk.•The CPU fetches the instructions from memory one after another and executes them (execution cycle).

••Programs are represented digitally in Programs are represented digitally in machine languagemachine language as a list of numbers.as a list of numbers.

••Each number represents either the code of Each number represents either the code of an instruction or some data.an instruction or some data.••The program is loaded from disk.The program is loaded from disk.

••The CPU fetches the instructions from The CPU fetches the instructions from memory one after another and executes memory one after another and executes them (execution cycle).them (execution cycle).

10 October 2007 © Ariel Shamir 28

Machine LanguageEach computer has a limited set of machine language instructionsThe instructions are very elementary• Add two integers

• Test if a given integer is zero

The strength of computers is due to their capability of executing millions of simple instructions per second

Each computer has a limited set of Each computer has a limited set of machine language instructionsmachine language instructions

The instructions are very elementaryThe instructions are very elementary•• Add two integersAdd two integers

•• Test if a given integer is zeroTest if a given integer is zero

The strength of computers is due to their The strength of computers is due to their capability of executing millions of simple capability of executing millions of simple instructions per secondinstructions per second

Page 15: Introduction to Computer Science - IDC to /intro1/ Literature.htm P o si bec ur k : • “J av: n itrou cC S& P gm ”, W .h • "J aS oftw r el u i ns- F d P gm D ”, J hn Lew i

15

10 October 2007 © Ariel Shamir 29

Machine Code InstructionsThe machine code is a sequence of numbers representing• Instruction codes

• Data values

… 0011 92FD 0007 0014 …

… LOAD 92FD ADD 20 …

“Load whatever is in address 92FD in memory and add 20 to it”

The machine code is a sequence of numbers The machine code is a sequence of numbers representingrepresenting•• Instruction codesInstruction codes

•• Data valuesData values

…… 0011 92FD 0007 0014 0011 92FD 0007 0014 ……

…… LOAD 92FD ADD 20 LOAD 92FD ADD 20 ……

““Load whatever is in address Load whatever is in address 92FD 92FD in in memory and add 20 to itmemory and add 20 to it””

10 October 2007 © Ariel Shamir 30

Program Loading & Fetching

01001011101110010101001111001100

Memory

Disk

01001011101110010101001111001100

CPU

01110Load Fetch

Page 16: Introduction to Computer Science - IDC to /intro1/ Literature.htm P o si bec ur k : • “J av: n itrou cC S& P gm ”, W .h • "J aS oftw r el u i ns- F d P gm D ”, J hn Lew i

16

10 October 2007 © Ariel Shamir 31

Fetch-decode-execute Cycle

decodeexecute

fetch

Retrieve an instruction from mainmemory

Determine what theinstruction isCarry out the

instruction

10 October 2007 © Ariel Shamir 32

Execution Cycle

…5F 88 D3 04 01 F7…

Decode D3 = ADD

Fetch D3

Execute

Page 17: Introduction to Computer Science - IDC to /intro1/ Literature.htm P o si bec ur k : • “J av: n itrou cC S& P gm ”, W .h • "J aS oftw r el u i ns- F d P gm D ”, J hn Lew i

17

10 October 2007 © Ariel Shamir 33

Execution Cycle

…5F 88 D3 04 01 F7…

Decode D3 = ADD

Fetch D3

Execute 4

10 October 2007 © Ariel Shamir 34

Execution Cycle

…5F 88 D3 04 01 F7…

Decode D3 = ADD

Fetch D3

Execute 4+1

Page 18: Introduction to Computer Science - IDC to /intro1/ Literature.htm P o si bec ur k : • “J av: n itrou cC S& P gm ”, W .h • "J aS oftw r el u i ns- F d P gm D ”, J hn Lew i

18

10 October 2007 © Ariel Shamir 35

Execution Cycle

…5F 88 D3 04 01 F7…

Decode D3 = ADD

Fetch D3

Execute 4+1=5

10 October 2007 © Ariel Shamir 36

Execution Cycle Cont.

…5F 88 D3 04 01 F7…

Decode

Fetch F7

Execute

Page 19: Introduction to Computer Science - IDC to /intro1/ Literature.htm P o si bec ur k : • “J av: n itrou cC S& P gm ”, W .h • "J aS oftw r el u i ns- F d P gm D ”, J hn Lew i

19

10 October 2007 © Ariel Shamir 37

CPU Control•The speed of a CPU (i.e. the fetch-decode-execute cycle) is controlled by the system clock•The system clock generates an electronic pulse at regular intervals which coordinate the activities of the CPU•The speed is measured in megahertz (MHz) or Gigahertz (GHz)

••The speed of a CPU (i.e. the fetchThe speed of a CPU (i.e. the fetch--decodedecode--execute cycle) is controlled by the system execute cycle) is controlled by the system clockclock

••The system clock generates an electronic The system clock generates an electronic pulse at regular intervals which pulse at regular intervals which coordinate the activities of the CPUcoordinate the activities of the CPU

••The speed is measured in megahertz The speed is measured in megahertz ((MHz) or Gigahertz (GHz)MHz) or Gigahertz (GHz)

10 October 2007 © Ariel Shamir 38

CPU & Memory ConnectionThe main memory stores not only data but also the programs!The CPU continuously retrieves information from the memory in the execution cycle! The CPU – memory connection is the bottleneck of computation!

The main memory stores not only The main memory stores not only data but also the programs!data but also the programs!The CPU continuously retrieves The CPU continuously retrieves information from the memory in the information from the memory in the execution cycle! execution cycle! The CPU The CPU –– memory connection is the memory connection is the bottleneck of computation!bottleneck of computation!

Page 20: Introduction to Computer Science - IDC to /intro1/ Literature.htm P o si bec ur k : • “J av: n itrou cC S& P gm ”, W .h • "J aS oftw r el u i ns- F d P gm D ”, J hn Lew i

20

10 October 2007 © Ariel Shamir 39

“Given Tasks” - SoftwareComputers do only what we tell them to – they are deterministic machines!

What is a task?

Computers do Computers do onlyonly what we tell them what we tell them to to –– they are deterministic machines!they are deterministic machines!

What is a task?What is a task? Inputs

Outputs

ProcessA process that gets inputs A process that gets inputs related to the task, and outputs related to the task, and outputs data which represent the result data which represent the result of executing the task.of executing the task.

10 October 2007 © Ariel Shamir 40

Task ExamplesComputing taxes for employees of a company• Inputs: textual and numerical data representing information

about the employees

• Output: formatted textual tax reports

F15 flight simulator• Inputs: joystick signals and key strokes from the user

• Output: image data representing the pilot’s view

Search engine• Input: keywords

• Output: locations in internet where keywords found

Computing taxes for employees of a companyComputing taxes for employees of a company•• Inputs: textual and numerical data representing information Inputs: textual and numerical data representing information

about the employees about the employees

•• Output: formatted textual tax reportsOutput: formatted textual tax reports

F15 flight simulatorF15 flight simulator•• Inputs: joystick signals and key strokes from the userInputs: joystick signals and key strokes from the user

•• Output: image data representing the pilotOutput: image data representing the pilot’’s views view

Search engineSearch engine•• Input: keywordsInput: keywords

•• Output: locations in internet where keywords foundOutput: locations in internet where keywords found

Page 21: Introduction to Computer Science - IDC to /intro1/ Literature.htm P o si bec ur k : • “J av: n itrou cC S& P gm ”, W .h • "J aS oftw r el u i ns- F d P gm D ”, J hn Lew i

21

10 October 2007 © Ariel Shamir 41

Algorithms – the “How”An algorithm is a procedure for executing a given task.

An algorithm consist of a list of instructions.

An algorithm is a An algorithm is a procedure for procedure for executing a given executing a given task. task.

An algorithm An algorithm consist of a list of consist of a list of instructions.instructions.

Inputs

Outputs

Algorithm

10 October 2007 © Ariel Shamir 42

Al-KhowarizmiBorn in Uzbekistan (780AD). Worked as a mathematician in Baghdad (considered as founder of modern algebra). He believed that any math problem, no matter how difficult it may be, could be solved if broken into smaller steps and logically derive the correct answer!

Born in Uzbekistan (780AD). Born in Uzbekistan (780AD).

Worked as a mathematician in Worked as a mathematician in Baghdad (considered as founder of Baghdad (considered as founder of modern algebra). modern algebra).

He believed that any math He believed that any math problem, no matter how difficult it problem, no matter how difficult it may be, could be solved if broken may be, could be solved if broken into smaller steps and logically into smaller steps and logically derive the correct answer!derive the correct answer!

Page 22: Introduction to Computer Science - IDC to /intro1/ Literature.htm P o si bec ur k : • “J av: n itrou cC S& P gm ”, W .h • "J aS oftw r el u i ns- F d P gm D ”, J hn Lew i

22

10 October 2007 © Ariel Shamir 43

Real Life Example An algorithm for fixing a flat tier:

Take out the screws of the tier

Lift the car on a jack

Remove the flat tier

Mount the reserve wheel

Tighten back the screws

Lower back the car

Fasten the screws

An algorithm for fixing a flat tier:An algorithm for fixing a flat tier:Take out the screws of the tierTake out the screws of the tier

Lift the car on a jackLift the car on a jack

Remove the flat tierRemove the flat tier

Mount the reserve wheelMount the reserve wheel

Tighten back the screwsTighten back the screws

Lower back the carLower back the car

Fasten the screwsFasten the screws

Flat tier car

Algorithm

Drivable car

10 October 2007 © Ariel Shamir 44

Mathematical AlgorithmAlgorithm:

For all integers i such that 1<i<n

If i divides n

Output false

Output true

What does this algorithms do?

Algorithm:Algorithm:

For all integers For all integers ii such such that that 1<i<n1<i<n

If If ii divides divides nn

Output falseOutput false

Output trueOutput true

What does this What does this algorithms do?algorithms do?

A number n

Algorithm

True or False

Page 23: Introduction to Computer Science - IDC to /intro1/ Literature.htm P o si bec ur k : • “J av: n itrou cC S& P gm ”, W .h • "J aS oftw r el u i ns- F d P gm D ”, J hn Lew i

23

10 October 2007 © Ariel Shamir 45

Measuring Algorithms: Complexity•The efficiency of the algorithm is often called it’s complexity

•In general terms:Complexity = cost in time and space

•Usually computed as a function of the size of the input

••The efficiency of the algorithm is The efficiency of the algorithm is often called itoften called it’’s complexitys complexity

••In general terms:In general terms:Complexity = cost in Complexity = cost in timetime and and spacespace

••Usually computed as a function of Usually computed as a function of the size of the inputthe size of the input

10 October 2007 © Ariel Shamir 46

Example: SearchingFind a phone number in a 1000 number phone book.• Linear search: could take 1000 checks!

• Binary search: 1000,500,250,125,63,32,16,8,4,2,1,

mostly 10 steps!

Find a phone number in a 1000 number Find a phone number in a 1000 number phone book.phone book.•• Linear search: could take 1000 checks!Linear search: could take 1000 checks!

•• Binary search: Binary search: 1000,500,250,125,63,32,16,8,4,2,1, 1000,500,250,125,63,32,16,8,4,2,1,

mostly 10 steps!mostly 10 steps!

How many jumps in the general case?How many jumps in the general case?

Page 24: Introduction to Computer Science - IDC to /intro1/ Literature.htm P o si bec ur k : • “J av: n itrou cC S& P gm ”, W .h • "J aS oftw r el u i ns- F d P gm D ”, J hn Lew i

24

10 October 2007 © Ariel Shamir 47

Algorithms in CS• Is there an algorithm for every task?

• How can we know if an algorithm is correct?

• What is the time/space complexity?

• What is the best algorithm for a given task?

• What is the best way to express an algorithm?

•• Is there an algorithm for every task?Is there an algorithm for every task?

•• How can we know if an algorithm is How can we know if an algorithm is correct?correct?

•• What is the time/space complexity?What is the time/space complexity?

•• What is the best algorithm for a given task?What is the best algorithm for a given task?

•• What is the best way to express an What is the best way to express an algorithm?algorithm?

10 October 2007 © Ariel Shamir 48

From Algorithm to Execution Cycle

Hardware

Execution Cycle

Algorithm/Model

Page 25: Introduction to Computer Science - IDC to /intro1/ Literature.htm P o si bec ur k : • “J av: n itrou cC S& P gm ”, W .h • "J aS oftw r el u i ns- F d P gm D ”, J hn Lew i

25

10 October 2007 © Ariel Shamir 49

Computer Programs•An algorithm is described to the computer as a computer program.•A computer program is simply a list of unambiguous instructions meant to be followed by a computer.•The operations sequence has to be correct or the program will fail.

••An algorithm is described to the An algorithm is described to the computer as a computer as a computer programcomputer program..••A computer program is simply a list A computer program is simply a list of unambiguous instructions meant of unambiguous instructions meant to be followed by a computer.to be followed by a computer.••The operations sequence has to be The operations sequence has to be correct or the program will fail.correct or the program will fail.

10 October 2007 © Ariel Shamir 50

Program Compilation•A program written in any high level computer language (java, C++, basic) must be translated into the machine language in order to be executed.

•The translation process is called Compilation (or Assembly)

••A program written in any high level A program written in any high level computer language (java, C++, basic) computer language (java, C++, basic) must be translated into the machine must be translated into the machine language in order to be executed. language in order to be executed.

••The translation process is called The translation process is called Compilation (or Assembly)Compilation (or Assembly)

Page 26: Introduction to Computer Science - IDC to /intro1/ Literature.htm P o si bec ur k : • “J av: n itrou cC S& P gm ”, W .h • "J aS oftw r el u i ns- F d P gm D ”, J hn Lew i

26

10 October 2007 © Ariel Shamir 51

Summary

Hardware

Execution Cycle

Java Java

CompilationCompilation

Machine CodeMachine Code

Algorithm/Model

Next few lectures