Basics of Algorithmics

Algorithmics – Session 1

  • What is an algorithm?
  • Values, variables and constants
  • Algorithmic statements.
  • Recap of the session

What is an algorithm?

A possible definition

An algorithm is a solution to a specific problem that takes a set of values as input and produces a value as a result. This solution is realised by a sequence of algorithmic statements applied to the input data to produce the final result value.

Values, variables and constants

Values in an algorithm

Values

34, 1.09, “IMT Atlantique” or false are values that can be integrated into calculus, as for instance 34 * 1.09 to get the conversion into euros of 34 dollars using a rate of 1.09.

Variables

  • Variables are names that refer to values, while constants are fixed.

  • Always favor variables over reuses of the same value.

  • Values have an associated type, which defines possible operations on them.

  • Programming languages define several elementary types (booleans, integers, real numbers, strings, etc.) and allow us to create your own composite types.

  • Data structures allow to group simple types into collections.

Values, variables and constants

Types of values

Variables

The type of a variable determines:

  • which operations can be applied on it,
  • the space used in memory to store its associated value,
  • if its value may evolve.

The location where a variable is defined determine its visibility:

  • as a global or local variable.

Algorithmic statements

Basic toolbox

The toolbox to write algorithms is very basic.

  • defining variables / assigning values
  • conditions
  • loops
  • defining / calling functions (focus of next session)

Algorithmic statements

Declaring variables and assigning values

Managing variables

  • declare variables
  • assign (calculated) values to variables
a : int = 34 * nb

Algorithmic statements

Control statements

Conditions

Conditional statements (if) are used to split the sequential execution of the algorithm based on a Boolean condition.

if est - reel < epsilon: 
    print("Estimation found ", est)
else:
    print("Continuation")

Algorithmic statements

Control statements

Loops

Loops make it possible to define cycles in the execution flow of an algorithm.

# Initialize a few variables
v : List[int] = [1, 5, -5, 15, 0]
maxv : int = v[O] if len(v) > 0 else None
i : int = 0

# A loop that will iterate n times
for i in range(len(v)):
    if v[i] > max:
        max = v[i] 

An invariant is used to prove by induction that a loop effectively reaches an expected state from an initial state.

    maxv == max v[0:i]

Recap of the session

What’s next?

Practical activity (~1h15)

Solving problems with algorithms

  • Combining algorithmic statements
  • Focus on loops

After the session

  • Complete the practical activity
  • Assess your understanding of the basic notions
  • Check next session’s “Before the class” section