algo1
algo1
Lecture 1
Cyrille Rosset
[email protected]
Université Paris Cité
7 November 2022
1 Introduction
Algorithmic
Python Programming
Numerical Python
2 Architecture of a computer
The Turing Machine
The Von Neuman architecture
3 Digital information
Numbers
Other formats
1 Introduction
Algorithmic
Python Programming
Numerical Python
2 Architecture of a computer
The Turing Machine
The Von Neuman architecture
3 Digital information
Numbers
Other formats
Algorithmic
Theory: 8 hours
Python Programming
Practice: 8 hours
Numerical Python
Theory and practice: 4 hours
Project
Small project (homework, to return by the end of December)
1 Introduction
Algorithmic
Python Programming
Numerical Python
2 Architecture of a computer
The Turing Machine
The Von Neuman architecture
3 Digital information
Numbers
Other formats
Definitions
Algorithm: formal description of a recipe to accomplish some task.
Algorithmic: the science (and art!) of algorithms.
Definitions
Algorithm: formal description of a recipe to accomplish some task.
Algorithmic: the science (and art!) of algorithms.
Examples
Addition, multiplication using decimal numbers.
5 2 3
+ 1 2 8
Definitions
Algorithm: formal description of a recipe to accomplish some task.
Algorithmic: the science (and art!) of algorithms.
Examples
Addition, multiplication using decimal numbers.
5 21 3
+ 1 2 8
1
Definitions
Algorithm: formal description of a recipe to accomplish some task.
Algorithmic: the science (and art!) of algorithms.
Examples
Addition, multiplication using decimal numbers.
5 21 3
+ 1 2 8
5 1
Definitions
Algorithm: formal description of a recipe to accomplish some task.
Algorithmic: the science (and art!) of algorithms.
Examples
Addition, multiplication using decimal numbers.
5 21 3
+ 1 2 8
6 5 1
1 Introduction
Algorithmic
Python Programming
Numerical Python
2 Architecture of a computer
The Turing Machine
The Von Neuman architecture
3 Digital information
Numbers
Other formats
Program
A program is a sequence of instructions the computer can execute.
The computer only understands very basic operations (store numbers in memory,
arithmetic operations on numbers, comparisons, . . . )
An algorithm must be translated into a program in order to be executed on a
computer.
Programming language
A programming language is a conventional notation of the instructions to be
executed by a computer that:
a human can understand;
can be translated automatically in a form the computer can understand
(machine language).
Python
Python is a high-level programming language: this means each instruction in
Python corresponds to many instructions in machine language. This makes
writing programs easier.
Python is widely used in the scientific community (but also in other fields:
web development, . . . )
Python will be used in various lectures during the Master
Once you know Python, you can easily learn other languages (Matlab, C,
C++, Rust, . . . )
n = raw_input('Enter an integer:')
n = int(n) # raw_input() returns a string, convert to int
f = fibonacci(n)
print('F({0})={1}\n'.format(n, f))
1 Introduction
Algorithmic
Python Programming
Numerical Python
2 Architecture of a computer
The Turing Machine
The Von Neuman architecture
3 Digital information
Numbers
Other formats
Numerical computing
In scientific programming, lot of numbers are manipulated: an image from a
satellite can be made of millions of pixels
Perform mathematical operations on these numbers (convolution, filtering,
patterm recognition, . . . )
Need to manipulate them efficiently: this is best done in low-level languages
(such as C, Fortran), while high-level language such as Python will be slow
for these tasks
NumPy
Python library which allows to manipulate numerical data efficiently in Python
Executes efficient C code on Python data
Provides efficient numerical algorithms
Fundamental building block for numerical computing in Python
Matplotlib Example
Python library for plotting import numpy as np
graphs from matplotlib import pyplot as plt
Matplotlib Example
Python library for plotting import numpy as np
graphs from matplotlib import pyplot as plt
1 Introduction
Algorithmic
Python Programming
Numerical Python
2 Architecture of a computer
The Turing Machine
The Von Neuman architecture
3 Digital information
Numbers
Other formats
1 Introduction
Algorithmic
Python Programming
Numerical Python
2 Architecture of a computer
The Turing Machine
The Von Neuman architecture
3 Digital information
Numbers
Other formats
Tape
1 1 0 1 0 1 1
Head Program:
Current state: S
S, H → S 0 , H 0 , M
Current head: H
...
Tape
1 1 0 1 0 1 1
Head Program:
Current state: S
S, H → S 0 , H 0 , M
Current head: H
...
1 Introduction
Algorithmic
Python Programming
Numerical Python
2 Architecture of a computer
The Turing Machine
The Von Neuman architecture
3 Digital information
Numbers
Other formats
1 Introduction
Algorithmic
Python Programming
Numerical Python
2 Architecture of a computer
The Turing Machine
The Von Neuman architecture
3 Digital information
Numbers
Other formats
Data in a computer
A computer process data in the form of numbers only.
All information must be transformed into numerical values at some point.
In this section, we will describe how data are stored in the computer, from
basic data (numbers) to more complex ones (text, image, . . . ).
Later, we will study the complex data structure and their relation with
algorithms.
1 Introduction
Algorithmic
Python Programming
Numerical Python
2 Architecture of a computer
The Turing Machine
The Von Neuman architecture
3 Digital information
Numbers
Other formats
Units
bit: binary digit, one elementary information (0 or 1), noted b.
byte: a block of 8 bits, noted B.
multiples: kilo- (103 ), Mega- (106 ), Giga- (109 ), Tera (1012 ), Peta- (1015 )
Base 8 Base 16
In base 8, there are 8 symbols: In base 16, there are 16 symbols:
0, 1, 2, 3, 4, 5, 6, 7. 0, . . . , 9 and A, B, C, D, E, F.
Examples Examples
Base 10 Base 8 Base 10 Base 16
0 08 0 016
7 78 9 916
8 108 10 A16
15 178 15 F16
240 3608 16 1016
255 3778 240 F 016
256 4008 255 FF16
512 10008 256 10016
From base 2
What is the value of 10110102 ?
From base 2
What is the value of 10110102 ?
10110102 = 1 × 26 + 0 × 25 + 1 × 24 + 1 × 23 + 0 × 22 + 1 × 21 + 0 × 20
= 64 + 0 + 16 + 8 + 0 + 2 + 0
= 90
From base 2
What is the value of 10110102 ?
From base 16
What is the value of 4FA316 ?
From base 2
What is the value of 10110102 ?
From base 16
What is the value of 4FA316 ?
90 = 2 × 45 + 0
90 = 2 × 45 + 0
45 = 2 × 22 + 1
90 = 2 × 45 + 0
45 = 2 × 22 + 1
22 = 2 × 11 + 0
90 = 2 × 45 + 0
45 = 2 × 22 + 1
22 = 2 × 11 + 0
11 = 2×5+1
5 = 2×2+1
2 = 2×1+0
1 = 2×0+1
90 = 2 × 45 + 0
45 = 2 × 22 + 1
22 = 2 × 11 + 0
11 = 2×5+1
5 = 2×2+1
2 = 2×1+0
1 = 2×0+1
The final answer is found by taking the remainders in the reverse order:
90 = 10110102
Definitions
Decimal Numbers in base 10
Hexadecimal Numbers in base 16
Octal Numbers in base 8
Binary Numbers in base 2
Exercises
1 Convert to decimal: FFFF, BC03, 10110110, 2558 , 6448
2 Convert to hexadecimal: 133, 32767, 4096, 11101010
3 Convert to binary: 4F23, 7778 , 6448 , 640, 1023, 508
4 Convert to octal: 10101101, 7B54, 200
Bit operations
One bit is either 0 or 1: related to Boolean algebra (two values: True and False)
OR 0 1 AND 0 1 XOR 0 1
NOT 0 1
0 0 1 0 0 0 0 0 1
1 0
1 1 1 1 0 1 1 1 0
Bit operations
One bit is either 0 or 1: related to Boolean algebra (two values: True and False)
OR 0 1 AND 0 1 XOR 0 1
NOT 0 1
0 0 1 0 0 0 0 0 1
1 0
1 1 1 1 0 1 1 1 0
Bitwise operations
Apply the Boolean operations on each bit of the numbers.
x AND y 1100 & 0110 = 0100
x OR y 1100 | 0110 = 1110
x XOR y 1100ˆ0110 = 1010
NOT x ! 1100 = 0011
Bit operations
One bit is either 0 or 1: related to Boolean algebra (two values: True and False)
OR 0 1 AND 0 1 XOR 0 1
NOT 0 1
0 0 1 0 0 0 0 0 1
1 0
1 1 1 1 0 1 1 1 0
Bitwise operations
Apply the Boolean operations on each bit of the numbers.
x AND y 1100 & 0110 = 0100
x OR y 1100 | 0110 = 1110
x XOR y 1100ˆ0110 = 1010
NOT x ! 1100 = 0011
Using Python operators here!
Unsigned integers
Unsigned (only positive) integers are directly represented with their binary
representation.
Unsigned integers
Unsigned (only positive) integers are directly represented with their binary
representation.
# bytes Min Max
1 0 255 (= 28 − 1)
2 0 65535 (= 216 − 1)
4 0 4294967295 (= 232 − 1)
Unsigned integers
Unsigned (only positive) integers are directly represented with their binary
representation.
Signed integers
Signed integers (positive or negative) are stored as two’s complement
representation.
Unsigned integers
Unsigned (only positive) integers are directly represented with their binary
representation.
Signed integers
Signed integers (positive or negative) are stored as two’s complement
representation.
Unsigned integers
Unsigned (only positive) integers are directly represented with their binary
representation.
Signed integers
Signed integers (positive or negative) are stored as two’s complement
representation.
Unsigned integers
Unsigned (only positive) integers are directly represented with their binary
representation.
Signed integers
Signed integers (positive or negative) are stored as two’s complement
representation.
Unsigned integers
Unsigned (only positive) integers are directly represented with their binary
representation.
Signed integers
Signed integers (positive or negative) are stored as two’s complement
representation.
Unsigned integers
Unsigned (only positive) integers are directly represented with their binary
representation.
Signed integers
Signed integers (positive or negative) are stored as two’s complement
representation.
Advantage: addition works in the same way for positive or negative numbers!
Example
In this example:
x = (−1)0 × 2124−127 × (1 + 0 × 2−1 + 1 × 2−2 ) = 1.25/8 = 0.15625
Special values
+0 and −0 are different
a float number can be infinite: +∞ and −∞
a float number can be NaN (not a number), in case of error (0/0 for example)
Precision
Single precision (32 bits):
min ' 1.4 × 10−45 , max ' 3.4 × 1038 , precision ∼ 10−7 .
Double precision (64 bits):
min ' 4.9 × 10−324 , max ' 1.8 × 10308 , precision ∼ 2 × 10−16 .
1 Introduction
Algorithmic
Python Programming
Numerical Python
2 Architecture of a computer
The Turing Machine
The Von Neuman architecture
3 Digital information
Numbers
Other formats
Ascii
For text, need to define a conversion table between numbers and characters.
First one was Ascii (using 7bits).
Problem
Ascii does not allow encoding of different languages. . .
The numbers between 128 and 255 were used to encode characters for different
language: iso-8859-1, . . .
C. Rosset Algorithmic & Programming 1 7 November 2022 33 / 40
Text: Unicode
Unicode
Unicode was developed to overcome problems of Ascii.
It is a table similar to ASCII table but containing more than 100,000
characters.
The first 128 characters are the same as Ascii.
In Ascii, characters were always encoded with 1 byte (using only 7 bits),
while with Unicode, 21 bits are needed (at least 3 bytes). Special encoding
is used to avoid using too much space (most frequent characters will use
fewer bytes).
One such encoding is called Utf-8: it is identical to Ascii for the first 128
characters!
0XXX XXXX (00-7F): 1 byte, similar to ASCII
110X XXXX-10XX XXXX: 2 bytes coding Unicode characters using 8 to 11
bits
...
Unicode
Unicode was developed to overcome problems of Ascii.
It is a table similar to ASCII table but containing more than 100,000
characters.
The first 128 characters are the same as Ascii.
In Ascii, characters were always encoded with 1 byte (using only 7 bits),
while with Unicode, 21 bits are needed (at least 3 bytes). Special encoding
is used to avoid using too much space (most frequent characters will use
fewer bytes).
One such encoding is called Utf-8: it is identical to Ascii for the first 128
characters!
0XXX XXXX (00-7F): 1 byte, similar to ASCII
110X XXXX-10XX XXXX: 2 bytes coding Unicode characters using 8 to 11
bits
...
Text files
Formatted text: includes format specifications (italic, bold characters, font size,
. . . ).
The formatting can be given using textual notation: Html, LATEX, . . . which can
be read and written in a text editor.
A program will translate the text into graphical representation.
Text files
Formatted text: includes format specifications (italic, bold characters, font size,
. . . ).
The formatting can be given using textual notation: Html, LATEX, . . . which can
be read and written in a text editor.
A program will translate the text into graphical representation.
Html
<html>
<body>
<h1>Algorithmique</h1>
<p>
L'<b>Algorithmique</b> est l'ensemble des règles et des techniques qui sont
impliquées dans la <em>définition</em> et la <em>conception</em>
d'<a href="http://fr.wikipedia.org/wiki/Algorithme">algorithme</a>, ...
</p>
</body>
</html>
C. Rosset Algorithmic & Programming 1 7 November 2022 35 / 40
Formatted text
Text files
Formatted text: includes format specifications (italic, bold characters, font size,
. . . ).
The formatting can be given using textual notation: Html, LATEX, . . . which can
be read and written in a text editor.
A program will translate the text into graphical representation.
Binary files
Softwares such as Word use a binary format.
Examples: Rich Text Format (Rtf), docx, . . .
Pixels
An image is decomposed into
pixels (as is the screen).
Each pixel is decomposed into 3
colors: red, green, blue.
The color can then be encoded
as a number: in the example,
0 000 noir each color is encoded as one bit
1 001 bleu (0 or 1), so that each pixel need
2 010 vert 3 bits.
3 011 cyan Usually: 1 byte per color, so 3
4 100 rouge bytes per pixels
5 101 magenta Sometimes: RGBA, adding
6 110 jaune Alpha channel (encodes
7 111 blanc transparency).
File formats
Without compression:
BMP (Bitmap): mainly used for small images
TIFF: flexible (may be compressed), for large images
With compression:
PNG (Portable Network Graphics): small image, no loss, good for scientific
plotting.
JPEG: for photography or smooth images only
Jpeg
Png