You are on page 1of 24

# An Algorithmic Language

Dorel Lucanu
Faculty of Computer Science Alexandru Ioan Cuza University, Ia si, Romania dlucanu@info.uaic.ro

TPAA 2013

## D. Lucanu (FII - UAIC)

An Algorithmic Language

TPAA 2013

1 / 24

Outline

## D. Lucanu (FII - UAIC)

An Algorithmic Language

TPAA 2013

2 / 24

Introduction

Plan

## D. Lucanu (FII - UAIC)

An Algorithmic Language

TPAA 2013

3 / 24

Introduction

Motivation
The goal is to have a language that is simple enough to be easily understood by the undergraduate students expressive enough to describe all taught algorithms abstract: the description of algorithm is abstracted from the implementation details such that the focus is on the algorithmic thinking supply a computation model suitable for analysis, especially time complexity executable: the students (and teachers, as well) should be able to make experiments with the described algorithms the input and output data should be represented in an abstract way (e.g., as simple mathematical objects) such that of the implementation details are totally omitted
D. Lucanu (FII - UAIC) An Algorithmic Language TPAA 2013 4 / 24

Memory Model

Plan

## D. Lucanu (FII - UAIC)

An Algorithmic Language

TPAA 2013

5 / 24

Memory Model

Memory Model
the memory is a set of variables variables are nothing but reserved memory locations to store values however, we do not care about the memory locations, so that we represent a variable as a pair math notation variable-name value value variable-name

graphical notation

## we consider four kinds of variables: scalar cons list array structure

D. Lucanu (FII - UAIC) An Algorithmic Language TPAA 2013 6 / 24

Memory Model

Scalars

primitive data types: booleans, integers, oats, strings math notation b true true b bool b=true; i5 5 i int i=5;

## D. Lucanu (FII - UAIC)

An Algorithmic Language

TPAA 2013

7 / 24

Memory Model

Cons Lists
Con lists are also called lists a la Lisp. They have a recursive denition: the empty list () is a cons list; if V is a value and L is a list, then (V , L) is a cons list (this is the cons (from constructor) operation) Cons lists are abstract representation of the linked lists math notation L = (a, (b , (c , ()))) a L b c

graphical notation

C++

struct List { int info; List *next; } * L; L = new List; L.info = a; L.next = new List; ...
An Algorithmic Language TPAA 2013 8 / 24

## D. Lucanu (FII - UAIC)

Memory Model

Arrays
An array is represented as a nite set of maps from indexes to values. math notation a {0 a0 1 a1 2 a2 }. 0 a0 a 1 a1 2 a2

graphical notation

## int a[3]; for (i=0; i<3;++i) cin >> a[i];

C++
or int a = {a0, a1, a2};

## D. Lucanu (FII - UAIC)

An Algorithmic Language

TPAA 2013

9 / 24

Memory Model

Structures
The structures are represented in a similar way as the arrays, but the maps are from elds names to values. For instance, a structure p representing 2D points has two elds: x and y: math notation p {x 2 y 7} x 2 p y 7

graphical notation

C++

## D. Lucanu (FII - UAIC)

An Algorithmic Language

TPAA 2013

10 / 24

Memory Model

## Complex Values: Graph Example

the graph G = ({0, 1, 2}, {(0, 1), (0, 2), (1, 2)}) is represented by adjacency lists as follows: math notation {n 3 a {0 [1, (2, ())] 1 [0, (2, ())] 2 [0, (1, ())]}} graphical notation n a 0 1 3 1, (2, ()) 0, (2, ()) G C++ struct Graph { int n; vector<list<int>> a; } G;
D. Lucanu (FII - UAIC) An Algorithmic Language

2 0, (1, ())

## G.n = 3; G.a[0].push_front(1); ...

TPAA 2013 11 / 24

Operations

Plan

## D. Lucanu (FII - UAIC)

An Algorithmic Language

TPAA 2013

12 / 24

Operations

Scalars

Integers: Operation a+b ab ... Uniform cost O (1) O (1) Logarithmic cost O (max(log a, log b )) O (log a log b ) O (max(log a, log b )1.545 )

## D. Lucanu (FII - UAIC)

An Algorithmic Language

TPAA 2013

13 / 24

Operations

Cons Lists

## D. Lucanu (FII - UAIC)

An Algorithmic Language

TPAA 2013

14 / 24

Operations

Arrays

## D. Lucanu (FII - UAIC)

An Algorithmic Language

TPAA 2013

15 / 24

Statements

Plan

## D. Lucanu (FII - UAIC)

An Algorithmic Language

TPAA 2013

16 / 24

Statements

Expressions

syntax Exp ::= Id | Int | Exp "+" Exp | Exp "*" Exp ... | Id "(" Exps ")" ... syntax Exps ::= List{Exp,","}

## D. Lucanu (FII - UAIC)

An Algorithmic Language

TPAA 2013

17 / 24

Statements

Satements Syntax

syntax Stmt ::= Exp "=" Exp ";" | Exp ";" | "{" "}" | "{" Stmts "}" | "while" "(" Exp ")" Stmt | "return" Exps ";" | Exp "(" Ids ")" "{" Stmts "}" | "if" "(" Exp ")" Stmt "else" Stmt | "if" "(" Exp ")" Stmt ... syntax Stmts ::= List{Stmt,""}

## D. Lucanu (FII - UAIC)

An Algorithmic Language

TPAA 2013

18 / 24

Statements

Congurations

Conguration

## D. Lucanu (FII - UAIC)

An Algorithmic Language

TPAA 2013

19 / 24

Statements

Congurations

Evaluation of Expressions

eval (, E ) returns the value obtained by replacing program variables with their values in the state S Formal denition . . . (on the blackboard) Example eval (x 7 y 12, y + 2 * x) = eval (x 7 y 12, y) + 2 eval (x 7 y 12, x) = 12 + 2 7 = 26

## D. Lucanu (FII - UAIC)

An Algorithmic Language

TPAA 2013

20 / 24

Statements

Computations

Computations:Denition

S, S , i executing the current statement of S in the state we get new code (to execute) S and a new state

## D. Lucanu (FII - UAIC)

An Algorithmic Language

TPAA 2013

21 / 24

Statements

Computations

## Semantics of the Statements

assignment: x = e ; informal : evaluate e and the result is the new value of the variable e formal : x = e ;S , S , [x eval (, e )] while: while (e ) S informal : evaluate e ; if the result is true, then execute S and after that evaluate e . . . ; otherwise the execution of the statement is nished formal : while (e ) S S , S ; while (e ) S S , if eval (, e ) = true while (e ) S S , S , if eval (, e ) = false

## D. Lucanu (FII - UAIC)

An Algorithmic Language

TPAA 2013

22 / 24

Statements

Computations

## Semantics of the Statements

if: if (e ) then S else S informal : evaluate e ; if the result is true, then execute S , otherwise execute S formal : e ) then S else S S , S S , if eval (, e ) = true e ) then S else S S , S S , if eval (, e ) = false

## D. Lucanu (FII - UAIC)

An Algorithmic Language

TPAA 2013

23 / 24

Statements

Computations

Computations:Example

x = x + 1; y = y + 2 * x;, x 7 y 12 y = y + 2 * x;, x 8 y 12 ., x 8 y 28

## D. Lucanu (FII - UAIC)

An Algorithmic Language

TPAA 2013

24 / 24