From controlling machines to designing algorithms

Prof. Dr. Mirco Schoenfeld

Recap

Previously…


https://imgur.com/gallery/most-useless-machine-ever-FRflC

Recap

So far, we dealt with
models of automata and more sophisticated machines.

Today

Today, we will start delving into algorithms.

The problem

Remember?

The task

Escape the maze.

Possible solutions

Possible solutions:

  1. Choose a random direction if you hit a wall
  2. Keep walking along the (left|right) wall
  3. Like 1. but only visit paths you haven’t visited before

Functionality

What functionality do you need to design
these high-level heuristics as algorithms?

  • move() to move your agent forward
  • turnLeft() and turnRight() to turn your agent
  • canMove() to tell you’re blocked by a wall
  • visitedBefore() to tell you if you have visited a path before
  • isExit() which tells you if you have reached the exit

Functionality

Why have we just written the functionality like this?

move()

In program code, this is how we call functions. More on that later.

Functionality

What else do we need?

We need control instructions to control the flow of execution.

Control instructions

Control instructions are core components of
imperative programming.

In imperative programming, algorithms are
described as a sequence of commands to be executed.

Your algorithm is a description of how to achieve a certain result.

Imperative programming

Imperative programming is a programming paradigm.

That is a high-level way to conceptualize and structure the implementation of a computer program.

Imperative programming

In imperative programming, we have

Programming paradigms

There are also other paradigms:

Back to the topic

https://www.getyarn.io/yarn-clip/87e76303-46c4-4cb3-9552-2b5cf2a9b2e7/gif

Control instructions

What kind of control instructions do we need to escape our maze?

Control instructions

To escape our maze, we need to

  • decide, e.g. if we hit a wall,
  • repeat, e.g. moving forward,
  • save our state, e.g. describing where we were,
  • evaluate our state.

if-then-else

To decide, we use conditional statements.

If (A) Then
  (B)
Else
  (C)
End If

A is a Boolean expression. More on that later.

The Else-part is optional!

loops

To repeat, we use loops, here a condition-controlled loop:

While (A) do
  (B)
Done

B is executed and repeated as long as the condition A holds.

loops

Another variant of this loop called do while loop:

Do
  (B)
While (A)

It executes B once before checking A the first time.

assignments

To save our state, we need variables and assignments.

Variables

Variables are abstract storage locations with a symbolic name.

Variables contain some data or object referred to as a value.

Variables

Variables in CS are different from variables in mathematics!

In mathematics, variables refer to something that can be modified.

In computers, variables can actually change their value.

assignments

Assignments are the most fundamental construct in imperative programming.

x = 5

This assigns the value 5 to a variable named x.

assignments

The value of x can be changed later in the program through subsequent assignments.

x = 5
# some other stuff
# happening here
x = 7

expressions

To evaluate our state, we need expressions.

It is a syntactic entity that may be evaluated to determine its value.

Expressions

Example expressions:

x + y
2 * (i + 1)
pow(x,2)

Expressions combine values of variables to a new value.

Boolean expressions

And what are Boolean expressions?

If (A) Then
  (B)
End If

Boolean expressions are expressions that evaluate to
either true or false.

Boolean expressions

Example Boolean expressions:

x < 5
x >= y
x == y
x != y

A special loop

What does this count-controlled loop do?

for ( i=1; i<=100; i+=1){
  print i;
}

Summary

Summary

A brief summary of control flows:

  • variables
  • (Boolean) expressions
  • assignments
  • conditionals
  • loops

Escape the maze

Can you design an algorithm for one of the possible solutions?
e.g. “Choose a random direction if you hit a wall”.

  • variables
  • (Boolean) expressions
  • assignments
  • conditionals
  • loops
  • move()
  • turnLeft() and turnRight()
  • canMove()
  • visitedBefore()
  • isExit()

Caveats

Caveats

Does your algorithm…

  • …yield the shortest path?
  • …work for any maze?
  • …find a solution efficiently?

Outlook

The question of efficiency will be
an important topic later in this course.

Thanks

https://xkcd.com/1652/

Back to Lecture Website