0% found this document useful (0 votes)
14 views

algo1

Uploaded by

Minh Tâm
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

algo1

Uploaded by

Minh Tâm
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 75

Introduction to Algorithmic and Programming

Lecture 1

Cyrille Rosset
[email protected]
Université Paris Cité

7 November 2022

C. Rosset Algorithmic & Programming 1 7 November 2022 1 / 40


Outline

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

C. Rosset Algorithmic & Programming 1 7 November 2022 2 / 40


Outline

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

C. Rosset Algorithmic & Programming 1 7 November 2022 3 / 40


What will you learn?

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)

C. Rosset Algorithmic & Programming 1 7 November 2022 4 / 40


Outline

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

C. Rosset Algorithmic & Programming 1 7 November 2022 5 / 40


Algorithmic: what is it?

Definitions
Algorithm: formal description of a recipe to accomplish some task.
Algorithmic: the science (and art!) of algorithms.

C. Rosset Algorithmic & Programming 1 7 November 2022 6 / 40


Algorithmic: what is it?

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

C. Rosset Algorithmic & Programming 1 7 November 2022 6 / 40


Algorithmic: what is it?

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

C. Rosset Algorithmic & Programming 1 7 November 2022 6 / 40


Algorithmic: what is it?

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

C. Rosset Algorithmic & Programming 1 7 November 2022 6 / 40


Algorithmic: what is it?

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

C. Rosset Algorithmic & Programming 1 7 November 2022 6 / 40


Outline

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

C. Rosset Algorithmic & Programming 1 7 November 2022 7 / 40


Programming

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).

C. Rosset Algorithmic & Programming 1 7 November 2022 8 / 40


The Python 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, . . . )

C. Rosset Algorithmic & Programming 1 7 November 2022 9 / 40


The Python language

Example: A Python program


def fibonacci(n):
"""Returns element n of Fibonacci suite.
"""
if n < 0:
raise ValueError('forbidden negative value')
if n <= 1:
return n
a, b = 0, 1
i = 2
while i < n + 1:
a, b = b, a + b
i += 1
return b

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))

C. Rosset Algorithmic & Programming 1 7 November 2022 9 / 40


Outline

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

C. Rosset Algorithmic & Programming 1 7 November 2022 10 / 40


Numerical Python

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

C. Rosset Algorithmic & Programming 1 7 November 2022 11 / 40


Plotting with Python

Matplotlib Example
Python library for plotting import numpy as np
graphs from matplotlib import pyplot as plt

Data are provided as numerical x = np.linspace(-10, 10, 201)


data (NumPy) y = np.sinc(x)
plt.plot(x, y)
Scientific publication quality
figures

C. Rosset Algorithmic & Programming 1 7 November 2022 12 / 40


Plotting with Python

Matplotlib Example
Python library for plotting import numpy as np
graphs from matplotlib import pyplot as plt

Data are provided as numerical x = np.linspace(-10, 10, 201)


data (NumPy) y = np.sinc(x)
plt.plot(x, y)
Scientific publication quality
figures

Numerical Python and plotting:


Next week!

C. Rosset Algorithmic & Programming 1 7 November 2022 12 / 40


Outline

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

C. Rosset Algorithmic & Programming 1 7 November 2022 13 / 40


Outline

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

C. Rosset Algorithmic & Programming 1 7 November 2022 14 / 40


The Turing Machine

Alan Turing was a mathematician (1912-1954), father of theoretical


computing
Invented the concept of computer: the Turing machine is a mathematical
abstraction of a computing machine.
Allows demonstration of theorems concerning the decidability of an algorithm.

Tape

1 1 0 1 0 1 1

Head Program:
Current state: S
S, H → S 0 , H 0 , M
Current head: H
...

C. Rosset Algorithmic & Programming 1 7 November 2022 15 / 40


The Turing Machine

Tape

1 1 0 1 0 1 1

Head Program:
Current state: S
S, H → S 0 , H 0 , M
Current head: H
...

An infinitely long tape contains symbols (here, 0 and 1) in boxes;


A head can read or write a symbol on the tape;
The machine is at any time in some state S;
A program defines what to do when in some state S with symbol H under
the current position of the head: new state S 0 , write symbol H 0 at head
position and move the tape left or right (M = ← / →).

C. Rosset Algorithmic & Programming 1 7 November 2022 16 / 40


Outline

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

C. Rosset Algorithmic & Programming 1 7 November 2022 17 / 40


The Von Neumann architecture of a computer

The Turing machine is an


Memory
abstract mathematical concept.
Its main defect for practical use:
a new program requires a new
machine.
Arithmetic The Von Neumann (1903-1957)
Control
Logic architecture solve this probem
Unit
Unit by storing the program itself in
the same memory as the data
(on the tape).

C. Rosset Algorithmic & Programming 1 7 November 2022 18 / 40


The Von Neumann architecture of a computer

The Turing machine is an


Memory
abstract mathematical concept.
Its main defect for practical use:
a new program requires a new
machine.
Arithmetic The Von Neumann (1903-1957)
Control
Logic architecture solve this probem
Unit
Unit by storing the program itself in
the same memory as the data
(on the tape).

This is still the architecture on most modern computers.


Some microcontrollers use Harvard architecture, in which program and data are
stored in different memory space.

C. Rosset Algorithmic & Programming 1 7 November 2022 18 / 40


The Von Neumann architecture of a computer

The Turing machine is an


Memory
abstract mathematical concept.
Its main defect for practical use:
a new program requires a new
machine.
Arithmetic The Von Neumann (1903-1957)
Control
Logic architecture solve this probem
Unit
Unit by storing the program itself in
the same memory as the data
(on the tape).

Keep in mind the image of the memory as a long tape


where data and program are stored!

C. Rosset Algorithmic & Programming 1 7 November 2022 18 / 40


Summary of computer architecture

a Turing machine is an abstract mathematical concept to describe an


algorithm.
The Turing machine gives an image of how a modern computer works: the
data are stored onto a tape (memory), a read/write head can move along the
tape, and a program describe the operation performed depending on the
current state and a table of action (or program).
In practice, modern computer use the Von Neumann architecture, where
program and data are both stored in memory. This allows for flexibility for
writing program.

C. Rosset Algorithmic & Programming 1 7 November 2022 19 / 40


Outline

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

C. Rosset Algorithmic & Programming 1 7 November 2022 20 / 40


Digital information

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.

C. Rosset Algorithmic & Programming 1 7 November 2022 21 / 40


Outline

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

C. Rosset Algorithmic & Programming 1 7 November 2022 22 / 40


Binary numbers

Why using binary numbers?


Using one symbol to store data (for example |) means a number would be
encoded by the length of data: 2 = ||, 3 = |||, etc.
With two or more symbols, a number n can be encoded with k ∝ log n
symbols.
Two symbols, 0 and 1, are easy to distinguish electronically (0 V and 5 V).

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 )

Multiples in term of power of 2 are also used: 1 kiB = 210 B, . . .

C. Rosset Algorithmic & Programming 1 7 November 2022 23 / 40


Base 2

Conversion Example of binary numbers


A number written cn cn−1 . . . c1 c0 in Only two digits, 0 and 1.
base b (where ci are the digits) have Take number 1012 :
a value:
position 2 1 0
v (cn ) × b n + v (cn−1 ) × b b−1 + digits 1 0 1
values 22 21 20
· · · + v (c1 ) × b + v (c0 )
total 4 0 1 5
where v (ci ) is the value of digit ci .
Base 10 Base 2
0 02
1 12
2 102
3 112
4 1002
5 1012
7 1112
C. Rosset Algorithmic & Programming 1 7 November 2022 24 / 40
Other bases

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

C. Rosset Algorithmic & Programming 1 7 November 2022 25 / 40


Conversion from base b to base 10

From base 2
What is the value of 10110102 ?

C. Rosset Algorithmic & Programming 1 7 November 2022 26 / 40


Conversion from base b to base 10

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

C. Rosset Algorithmic & Programming 1 7 November 2022 26 / 40


Conversion from base b to base 10

From base 2
What is the value of 10110102 ?

From base 16
What is the value of 4FA316 ?

C. Rosset Algorithmic & Programming 1 7 November 2022 26 / 40


Conversion from base b to base 10

From base 2
What is the value of 10110102 ?

From base 16
What is the value of 4FA316 ?

4FA316 = 4 × 163 + 15 × 162 + 10 × 161 + 3 × 160


= 4 × 4096 + 15 × 256 + 10 × 16 + 3
= 20387

C. Rosset Algorithmic & Programming 1 7 November 2022 26 / 40


Conversion from base 10 to base b

Recipe (or algorithm!)


What is the representation of 90 in base 2?

C. Rosset Algorithmic & Programming 1 7 November 2022 27 / 40


Conversion from base 10 to base b

Recipe (or algorithm!)


What is the representation of 90 in base 2?

90 = 2 × 45 + 0

C. Rosset Algorithmic & Programming 1 7 November 2022 27 / 40


Conversion from base 10 to base b

Recipe (or algorithm!)


What is the representation of 90 in base 2?

90 = 2 × 45 + 0
45 = 2 × 22 + 1

C. Rosset Algorithmic & Programming 1 7 November 2022 27 / 40


Conversion from base 10 to base b

Recipe (or algorithm!)


What is the representation of 90 in base 2?

90 = 2 × 45 + 0
45 = 2 × 22 + 1
22 = 2 × 11 + 0

C. Rosset Algorithmic & Programming 1 7 November 2022 27 / 40


Conversion from base 10 to base b

Recipe (or algorithm!)


What is the representation of 90 in base 2?

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

C. Rosset Algorithmic & Programming 1 7 November 2022 27 / 40


Conversion from base 10 to base b

Recipe (or algorithm!)


What is the representation of 90 in base 2?

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

C. Rosset Algorithmic & Programming 1 7 November 2022 27 / 40


Exercise !

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

C. Rosset Algorithmic & Programming 1 7 November 2022 28 / 40


Bitwise operators

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

C. Rosset Algorithmic & Programming 1 7 November 2022 29 / 40


Bitwise operators

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

C. Rosset Algorithmic & Programming 1 7 November 2022 29 / 40


Bitwise operators

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!

C. Rosset Algorithmic & Programming 1 7 November 2022 29 / 40


Number representation I

Unsigned integers
Unsigned (only positive) integers are directly represented with their binary
representation.

C. Rosset Algorithmic & Programming 1 7 November 2022 30 / 40


Number representation I

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)

C. Rosset Algorithmic & Programming 1 7 November 2022 30 / 40


Number representation I

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.

C. Rosset Algorithmic & Programming 1 7 November 2022 30 / 40


Number representation I

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.

Two’s complement with 8 bits

C. Rosset Algorithmic & Programming 1 7 November 2022 30 / 40


Number representation I

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.

Two’s complement with 8 bits


The two’s complement of 45 is 00101101 (normal binary representation).

C. Rosset Algorithmic & Programming 1 7 November 2022 30 / 40


Number representation I

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.

Two’s complement with 8 bits


The two’s complement of 45 is 00101101 (normal binary representation).
The two’s complement of -45 is 110100112 = 28 − 45.

C. Rosset Algorithmic & Programming 1 7 November 2022 30 / 40


Number representation I

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.

Two’s complement with 8 bits


The two’s complement of 45 is 00101101 (normal binary representation).
The two’s complement of -45 is 110100112 = 28 − 45.
This is equivalent to invert each bit and add 1.

C. Rosset Algorithmic & Programming 1 7 November 2022 30 / 40


Number representation I

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.

Two’s complement with 8 bits


The two’s complement of 45 is 00101101 (normal binary representation).
The two’s complement of -45 is 110100112 = 28 − 45.
This is equivalent to invert each bit and add 1.

Advantage: addition works in the same way for positive or negative numbers!

C. Rosset Algorithmic & Programming 1 7 November 2022 30 / 40


Number representation II

Floating point numbers


Non-integer values could be represented as with fixed point notation:
00345.01230 but problems arise when multipying (overflow or underflow, lost
of precision, . . . )
The IEEE 754 standard define floating point numbers using 32 bits (single
precision) or 64 bits (double precision).
In this representation, a number is defined as: v = s × 2e × m where s is the
sign (1 bit), e is the shifted exponent (8 bits in single precision) and m is the
fraction (23 bits in single precision).

C. Rosset Algorithmic & Programming 1 7 November 2022 31 / 40


Number representation II

Floating point numbers


In this representation, a number is defined as: v = s × 2e × m where s is the
sign (1 bit), e is the shifted exponent (8 bits in single precision) and m is the
fraction (23 bits in single precision).

Example

In this example:
x = (−1)0 × 2124−127 × (1 + 0 × 2−1 + 1 × 2−2 ) = 1.25/8 = 0.15625

C. Rosset Algorithmic & Programming 1 7 November 2022 31 / 40


Number representation II

Floating point numbers


In this representation, a number is defined as: v = s × 2e × m where s is the
sign (1 bit), e is the shifted exponent (8 bits in single precision) and m is the
fraction (23 bits in single precision).

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)

C. Rosset Algorithmic & Programming 1 7 November 2022 31 / 40


Number representation II

Floating point numbers


In this representation, a number is defined as: v = s × 2e × m where s is the
sign (1 bit), e is the shifted exponent (8 bits in single precision) and m is the
fraction (23 bits in single precision).

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 .

C. Rosset Algorithmic & Programming 1 7 November 2022 31 / 40


Outline

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

C. Rosset Algorithmic & Programming 1 7 November 2022 32 / 40


Text: Ascii

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
...

C. Rosset Algorithmic & Programming 1 7 November 2022 34 / 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
...

C. Rosset Algorithmic & Programming 1 7 November 2022 34 / 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.

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.

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, . . .

C. Rosset Algorithmic & Programming 1 7 November 2022 35 / 40


Images

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).

C. Rosset Algorithmic & Programming 1 7 November 2022 36 / 40


Images

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

C. Rosset Algorithmic & Programming 1 7 November 2022 37 / 40


Examples: Plots with Matplotlib

Jpeg

C. Rosset Algorithmic & Programming 1 7 November 2022 38 / 40


Examples: Plots with Matplotlib

C. Rosset Algorithmic & Programming 1 7 November 2022 38 / 40


Examples: Plots with Matplotlib

C. Rosset Algorithmic & Programming 1 7 November 2022 38 / 40


Examples: Plots with Matplotlib

Png

C. Rosset Algorithmic & Programming 1 7 November 2022 39 / 40


Examples: Plots with Matplotlib

C. Rosset Algorithmic & Programming 1 7 November 2022 39 / 40


Examples: Plots with Matplotlib

C. Rosset Algorithmic & Programming 1 7 November 2022 39 / 40


Summary of lecture 1

Algorithm: a formal recipe to perform some task


Programming: translate an algorithm into a programming language, which
can be automatically transform into machine language so it can be executed
by a computer
Binary numbers: all data (and code) in a computer is in binary form, so it’s
good to have some knowledge of it and how to convert between various bases.
Data must be encoded:
integers: usually 1, 2, 4 or 8 bytes, can be unsigned or signed (two’s
complement)
floats: 4 (single) or 8 bytes (double precision), follow norm IEEE 754. Can
represent also ±∞, 0± or invalid (nan) numbers.
Text can be represented as Unicode.
Various binary formats for images, music, video, . . .

C. Rosset Algorithmic & Programming 1 7 November 2022 40 / 40

You might also like