0% found this document useful (0 votes)
40 views31 pages

Algo 2

Uploaded by

Minh Tâm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views31 pages

Algo 2

Uploaded by

Minh Tâm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

Introduction to Algorithmics and Programming

Lecture 2

Cyrille Rosset
[email protected]
Université Paris Cité

7 November 2022

C. Rosset Algorithmics & Programming 2 7 November 2022 1 / 31


Outline

1 Variables

2 Functions and Procedures

3 Pseudo-code vs Python code

4 Standard programming constructs


Conditions
Loops
Example: Euclid’s algorithm

5 Introduction to Data Structures


Arrays
Lists
Comparison

C. Rosset Algorithmics & Programming 2 7 November 2022 2 / 31


Outline

1 Variables

2 Functions and Procedures

3 Pseudo-code vs Python code

4 Standard programming constructs


Conditions
Loops
Example: Euclid’s algorithm

5 Introduction to Data Structures


Arrays
Lists
Comparison

C. Rosset Algorithmics & Programming 2 7 November 2022 3 / 31


Variables

Name and type


A variable is a name attached to a location in memory. It must start with a
letter and may contain only letters, digits or (underscore).
A variable has a type, which indicates the memory used by the variable and
how to interpret it: unsigned/signed integer, floating point number, . . .
a 10

Example in Python
>>> a_number = 10
>>> type(a_number)
int
>>> greatings = 'Hello, World!'
>>> type(greatings)
str

C. Rosset Algorithmics & Programming 2 7 November 2022 4 / 31


Outline

1 Variables

2 Functions and Procedures

3 Pseudo-code vs Python code

4 Standard programming constructs


Conditions
Loops
Example: Euclid’s algorithm

5 Introduction to Data Structures


Arrays
Lists
Comparison

C. Rosset Algorithmics & Programming 2 7 November 2022 5 / 31


Functions

Definition
Similar to function in mathematics
A function takes arguments as input, does some computations and returns a
value.

Example in Python
def f(x, y):
z = x**2 + y**2
return z
>>> result = f(2, 3)
>>> result
13
>>>

C. Rosset Algorithmics & Programming 2 7 November 2022 6 / 31


Procedures

Definition
A procedure executes some statements but does not return any values
It can be seen as a function which returns nothing
In Python, there is no difference between a procedure and a function

Example in Python
def print_square(x):
print 'The square of', x, 'is', x**2
>>> print_square(4)
The square of 4 is 16
>>> x = print_square(3
The square of 3 is 9
>>> x
>>> print x
None

C. Rosset Algorithmics & Programming 2 7 November 2022 7 / 31


Outline

1 Variables

2 Functions and Procedures

3 Pseudo-code vs Python code

4 Standard programming constructs


Conditions
Loops
Example: Euclid’s algorithm

5 Introduction to Data Structures


Arrays
Lists
Comparison

C. Rosset Algorithmics & Programming 2 7 November 2022 8 / 31


Pseudo-code vs Python code

Pseudo-code
In some books you will see algorithms describe in pseudo-code.
Pseudo-code is a kind of language to describe algorithm, but it is not actually a
programming language.
In this lecture, we will use mostly Python to describe algorithm, adding comments
when necessary.
We can do that because Python syntax is very close to pseudo-code you could
find in books.

Pseudo-code (from Wikipedia) Python


algorithm quicksort(A, lo, hi) is def quicksort(A, lo, hi):
if lo < hi then if lo < hi:
p := partition(A, lo, hi) p = partition(A, lo, hi)
quicksort(A, lo, p - 1) quicksort(A, lo, p - 1)
quicksort(A, p + 1, hi) quicksort(A, p + 1, hi)

C. Rosset Algorithmics & Programming 2 7 November 2022 9 / 31


Outline

1 Variables

2 Functions and Procedures

3 Pseudo-code vs Python code

4 Standard programming constructs


Conditions
Loops
Example: Euclid’s algorithm

5 Introduction to Data Structures


Arrays
Lists
Comparison

C. Rosset Algorithmics & Programming 2 7 November 2022 10 / 31


Outline

1 Variables

2 Functions and Procedures

3 Pseudo-code vs Python code

4 Standard programming constructs


Conditions
Loops
Example: Euclid’s algorithm

5 Introduction to Data Structures


Arrays
Lists
Comparison

C. Rosset Algorithmics & Programming 2 7 November 2022 11 / 31


Conditions

if. . . then. . . else


Used to execute execute only if some condition is realized (True).
if x > 3:
y = x - 2
else:
y = x + 2

Conditions
The conditions can use:
comparison operators: ==, !=, <, >, <=, >=
boolean operators: and, or, not

Example
# if a number is even and positive, divide it by 2
if x > 0 and x % 2 == 0:
x = x / 2

C. Rosset Algorithmics & Programming 2 7 November 2022 12 / 31


Outline

1 Variables

2 Functions and Procedures

3 Pseudo-code vs Python code

4 Standard programming constructs


Conditions
Loops
Example: Euclid’s algorithm

5 Introduction to Data Structures


Arrays
Lists
Comparison

C. Rosset Algorithmics & Programming 2 7 November 2022 13 / 31


For loops

Iterations
A for loop is a way to iterate over successive numbers.
In Python, the for loop allows looping over elemnets of a list.
The function range creates a list of numbers.

For loop in Python Result


for i in range(5): 0 0
print i, i**2 1 1
2 4
3 9
4 16

Notes
range(5) goes from 0 to 4!
range(5, 10) to go from 5 to 9
range(1, 10, 2) to get odd numbers 1, 3, 5, 7, 9.

C. Rosset Algorithmics & Programming 2 7 November 2022 14 / 31


While loops

Definition
A while loop will repeat a block of instructions as long as the given condition is
True.

While loop in Python Result


n = 1 1
while n < 100: 11
print n 41
n = 3 * n + 8

Notes
Use a while loop when you don’t know a priori the number of iterations.

C. Rosset Algorithmics & Programming 2 7 November 2022 15 / 31


Break and continue

Definition
break Allows to stop a loop before the normal end of the loop
continue Allows to go directly to the next iteration

Break in Python Result


n = 1 1
while True: 11
print n 41
n = 3 * n + 8
if n > 100:
break

Continue in Python Result


for i in range(1, 30): 12
if not (i%3 == 0 and i%4 == 0): 24
continue
print i

C. Rosset Algorithmics & Programming 2 7 November 2022 16 / 31


Outline

1 Variables

2 Functions and Procedures

3 Pseudo-code vs Python code

4 Standard programming constructs


Conditions
Loops
Example: Euclid’s algorithm

5 Introduction to Data Structures


Arrays
Lists
Comparison

C. Rosset Algorithmics & Programming 2 7 November 2022 17 / 31


Example: Euclid’s algorithm

Greatest common divisor


The purpose of the Euclid’s algorithm is to find the greatest common divisor of
two numbers:

Given two integers, subtract the smaller to the bigger and repeat until the two
numbers are equal. The obtained value is the greatest common divisor.

Exercise
Write the algorithm using what we have learnt so far.

C. Rosset Algorithmics & Programming 2 7 November 2022 18 / 31


Outline

1 Variables

2 Functions and Procedures

3 Pseudo-code vs Python code

4 Standard programming constructs


Conditions
Loops
Example: Euclid’s algorithm

5 Introduction to Data Structures


Arrays
Lists
Comparison

C. Rosset Algorithmics & Programming 2 7 November 2022 19 / 31


Data structures

Importance for algorithmic


The data structure is how you organize (or structure) the data in memory.
For the same data, a data structure may require more or less memory space than
another.
For the same operation on the data, some data structures will lead to more or less
efficient algorithms than others.

Definition
A data structure is defined by the operations which can be performed on it.
It introduces the notion of abstract data types (as opposed to an implementation
through a programming language).

Examples
We will study the following data structures: array, list, stack and tree.

C. Rosset Algorithmics & Programming 2 7 November 2022 20 / 31


Outline

1 Variables

2 Functions and Procedures

3 Pseudo-code vs Python code

4 Standard programming constructs


Conditions
Loops
Example: Euclid’s algorithm

5 Introduction to Data Structures


Arrays
Lists
Comparison

C. Rosset Algorithmics & Programming 2 7 November 2022 21 / 31


Arrays

Definition
An array a is defined by:
a fixed number of data items (N)
all data items have the same type
all data items are stored contiguously in memory
any data items can be accessed through an index i: a[i]

Example
In Python, there is no array by default (a list is used instead).
The package numpy provides this data type.
import numpy as np
gives:
a = np.array([0, 1, 2, 3, 4]) [0 1 2 3 4]
print a [ 0 1 999 3 4]
a[2] = 999
print a

C. Rosset Algorithmics & Programming 2 7 November 2022 22 / 31


Exercise: The Sieve of Eratosthenes

Prime numbers
The Sieve of Eratosthenes is an algorithm to find all prime numbers up to some
given number N.
This is done by marking as non-prime the multiples of each prime, starting with
the multiples of 2.

Initially: 2 3 4 5 6 7 8 9 10. Remove multiples of 2: 2 3 


4 5 A6 7 A8 9 
A 1Z
Z0. Remove

multiples of 3: 2 3 45
A 67
A 8
A 9Z
A 1Z
0.


Exercise
Write the algorithm using an array of boolean values called isprime, which, in
the end, will be True at the prime number indices (e.g. isprime[7] will be True,
but isprime[6] will be False).

C. Rosset Algorithmics & Programming 2 7 November 2022 23 / 31


Outline

1 Variables

2 Functions and Procedures

3 Pseudo-code vs Python code

4 Standard programming constructs


Conditions
Loops
Example: Euclid’s algorithm

5 Introduction to Data Structures


Arrays
Lists
Comparison

C. Rosset Algorithmics & Programming 2 7 November 2022 24 / 31


Linked lists

Definition
A linked list contains multiple items (as an array), but they have the advantage
they can grow or shrink easily.
The data is organized in nodes which:
contain a data item
contain a pointer (or a link) to the next value (the arrow on the picture)

A L I S T

Operations on a list: creation, insertion of an item, deletion of an item.

C. Rosset Algorithmics & Programming 2 7 November 2022 25 / 31


Operations on lists

Creation
An empty list will have a head linking to a tail.
These are special nodes: the head contains no item, and the tail will point to
itself.

head tail

Insertion
X

head A L I S T tail

C. Rosset Algorithmics & Programming 2 7 November 2022 26 / 31


Operations on lists II

Deletion

head A L I X S T tail

Exercise
Assuming a node can be described with variables node.item and node.link,
write in details the algorithms for the functions to manipulate a list.

C. Rosset Algorithmics & Programming 2 7 November 2022 27 / 31


Stack

Definition
A stack is a data structure which piles up data items.
Operations on stack:
Push put data on the stack
Pop pop the topmost data off the stack

C C

C C
B B
A A

C. Rosset Algorithmics & Programming 2 7 November 2022 28 / 31


Outline

1 Variables

2 Functions and Procedures

3 Pseudo-code vs Python code

4 Standard programming constructs


Conditions
Loops
Example: Euclid’s algorithm

5 Introduction to Data Structures


Arrays
Lists
Comparison

C. Rosset Algorithmics & Programming 2 7 November 2022 29 / 31


Comparison between arrays and lists

Memory
The array use exactly the memory taken by the data
The list use additionally a link for each item: if the items are small, you may
need twice the memory your data needs.

Speed
List: inserting and removing elements is easy and fast with list. For arrays,
you need to create a new array. . .
Array: accessing an element at any index i is fast (while you need to go
through all item and follow the links until you find your element with lists)

C. Rosset Algorithmics & Programming 2 7 November 2022 30 / 31


Summary

Variables: a tag to some location in memory, with a type.


Functions and procedures: a group of instructions which depends on some
input parameters.
Conditional instructions: use if. . . then. . . else. . .
Loops: for and while loops
Data structures: arrays and linked lists

C. Rosset Algorithmics & Programming 2 7 November 2022 31 / 31

You might also like