By
Dr. Ahmed Taha
Lecturer, Computer Science Department,
Faculty of Computers & Informatics,
Benha University Lecture 2
1
Lecture Two
Preliminaries
2
3
4
Book Title:
First Course in Algorithms Through Puzzles
Authors:
Ryuhei Uehara
Publisher:
Springer
Edition:
2019
5
Machine Models
• An Algorithm is a method for solving a given problem.
• Representing how to solve a problem requires you to
define basic operations precisely.
• That is, an algorithm is a sequence of basic operations
of some machine model you assume.
6
Machine Models
7
Machine Models
• When you adopt a simple machine model, it is easy to consider
the computational power of the model.
• However, because it has poor or restricted operations, it is hard
to design/develop some algorithms.
• On the other hand, if you use a more abstract machine model,
even though it is easier to describe an algorithm, you may face
a gap between the algorithm and real computers when you
implement it.
8
Machine Models
Turing Machine
Model
Machine Models RAM Model
Other Models
9
Turing Machine
Model
• A Turing machine is the machine model introduced by
Alan Turing in 1936.
• The structure is very simple because it is a virtual machine
defined to discuss the mechanism of computation
mathematically.
Alan Turing
• It consists of four parts:
tape, finite control, head, and motor
10
Turing Machine
Model
11
Turing Machine
Model
• The tape is one sided, that is, it has a left end, but has no right end.
• One of the digits 0, 1, or a blank is written in each cell of the tape.
• The finite control is in the “initial state,” and the head indicates the leftmost
cell in the tape. When the machine is turned on, it moves as follows:
1. Read the letter c on the tape;
2. Following the predetermined rule [movement on the state x with letter c]
• rewrite the letter in the head (or leave it);
• move the head to the right/left by one step (or stay there);
• change the state from x to y;
3. If the new state is “halt”, it halts; otherwise, go to step 1.
12
Turing Machine
Model
13
Turing Machine
Model
14
Turing Machine
Model
• The mechanism is so simple that it seems to be too
weak. However, in theory, every existing computer
can be abstracted by this simple model.
• The typical computer that runs programs from
memory is known as a von Neumann-type computer.
• Any problem solvable by a real computer is also
solvable by a Turing machine
15
Turing Machine
Model
• It is known that there exists a problem that cannot be
solved by any computer.
Theorem 1 There exists no algorithm that can determine whether any
given Turing machine with its input will halt or not. That is, this
problem, known as the halting problem, is theoretically unsolvable.
Example you cannot design a universal debugger that determines
whether any program will halt or not.
In other words, we may be able to design an almost universal debugger
that solves the halting problem for many cases, but not all cases.
16
Turing Machine
Model
• The Turing machine is a theoretical and virtual
machine model, and it is useful when we discuss the
theoretical limit of computational power.
• However, it is too restricted or too weak to consider
algorithms on it.
• For example, it does not have basic mathematical
operations such as addition and subtraction.
17
RAM Model
18
RAM Model
• We usually use a RAM (Random Access Machine)
model when we discuss practical algorithms.
• A RAM model consists of finite control, which
corresponds to the CPU (Central Processing Unit),
and memory
19
RAM Model
• Memory: Real computers keep their data in the cache in
the CPU, memory, and external memory such as a hard
disk drive or DVD. Such storage is modeled by memory in
the RAM model.
• Each memory unit stores a word.
• Data exchange between finite control and memory
occurs as one word per unit time.
• Finite control uses an address of a word to specify it.
20
RAM Model
• Usually, in a CPU, the number of bits in a word is the
same as the number of bits of the address in the
memory.
• This is because each address itself can be dealt with as
data.
• We sometimes say that your PC contains a 64-bit CPU.
• This number indicates the size of a word and the size of
an address.
21
RAM Model
• For example, in a 64-bit CPU:
▪ One word consists of 64 bits
▪ One address can be represented from 00 . . . 0 to 11 . . . 1
64 64
which indicates one of 264 ≈ 1.8 × 1019 different data.
• That is: when we refer to a RAM model with a 64-bit CPU:
▪ it refers to a word with 64 bits on each clock,
▪ and its memory can store 264 words in total.
22
RAM Model
23
RAM Model
• Finite control: The finite control unit reads the word
in a memory, applies an operation to the word, and
writes the resulting word to the memory again (at
the same or different address at which the original
word was stored).
• A computation is the repetition of this sequence.
24
RAM Model
• A CPU has some special memory units that consist of one
program counter (PC), and several registers.
• When a computer is turned on, the contents of the PC
and registers are initialized by 0. Then the CPU repeats
the following operations:
1. It reads the content X at the address PC to a register.
2. It applies an operation Y according to the value of X.
3. It increments the value of PC by 1.
25
RAM Model
Substitution
Basic operations
Calculation
on the RAM model
Comparison and
branch
26
RAM Model
• Substitution:
▪ This operation gives and takes between the data
and a register.
▪ For example, when data at address A is written
into a memory at address B, the CPU first reads
the data at address A into a register, and next
writes the data in the register into the memory at
address B.
27
RAM Model
• Calculation:
▪ This operation performs a calculation between two
data values and puts the result into a register.
▪ The two data values come from a register or memory
cell.
▪ Typically, the CPU reads data from a memory cell,
applies some calculation (e.g., addition) to the data
and some register, and writes the resulting data into
another register.
28
RAM Model
• Comparison and branch:
▪ This operation overwrites the PC if a register satisfies
some condition.
▪ if the condition is satisfied, the value of the PC is
changed.
▪ Otherwise, the value of PC is not changed, and hence,
the next operation will be performed in the next step
29
RAM Model
• Example:
• In many programs, “add 1 to some variable A” frequently appears
A ← A + 1;
• suppose that the variable A is written at the address 11111000
• The sequence of statements performed on the RAM model:
1. When it is turned on
2. It reads the contents at address 1111 1000 to register 1
3. It adds 0000 0001 to the contents of register 1
4. It writes the contents in register 1 to the memory at address 1111 1000
5. The contents 0000 1010 at address 1111 1000 are replaced by 0000 1011
30
RAM Model
31
Other Machine
Models
• Lately, new computation frameworks have been introduced
and investigated.
▪ Quantum computer is a computation model based on
quantum mechanics.
▪ DNA computer is a computation model based on the
chemical reactions of DNA (or string of amino acids).
• In these models, a computer consists of different elements
and a mechanism to solve some specific problems more
efficiently than ordinary computers.
32
33