Sei sulla pagina 1di 12

ALGORITHMS AND COMPUTERS (INTRO)

Def 1 An algorithm is a well-defined, step-by-step, list of operations for solving a particular problem.

Def 2 - In mathematics and computer science an algorithm is a finite set of well- defined instructions for accomplishing some task which, given an initial state, will terminate in a corresponding recognizable end state.

A mod B (remainder of an integer division) algorithm:

2. If A<B, go to step 5.

3. Replace A with A-B.

4. If A >=B, go to step 3.

5. END, the result is A.

The most famous definition of an algorithm is based on Alan Turin’s concept of an abstract machine called the Turin Machine.

To define a TM operation, we need a finite number of internal states and a set of transition rules.

Programming language method of formulating algorithms by combining a set of universal instructions.

Von Neumann Machine a computer where a program resides in the same storage as the data used by that program.

Processor = CPU+ALU. CPU (Central processing unit) responsible for storing and retrieving integers from memory. ALU (Arithmetic logic unit) is supplied with instructions and data from CPU, carries out arithmetic and logic operations, tells when data should be read from memory and used by the ALU. Responsible for calculations, contains decision mechanisms that allow comparing two items from memory.

Almost every computer can be divided into six logical units: input u./output u./memory u./CPU/ALU/secondary storage u.

Programming languages based on methology:

1. Procedural or Imperative (Python, C++, Java, Pascal)

3. Logic (ALF, Curry)

4. Object Orientated(Python, C++, PHP, J, Perl 5)

-------- on closeness to computer hardware:

1. Machine (every computer’s different, native language)

2. Assembly

3. High Level (Python, C++

)

Compiler = High-level-------->Machine language

Python: high level, object orientated (OO), supports dynamic typing

C++: high level, OO

Compiled programs usually go through six phases to be executed: edit/pre- process/compile/link/load/execute

WRITING SIMPLE PROGRAMS

Variable name = identifier

Identifier letters, digits,

Every variable has a: name, value, type. (x = 5; name ‘x’, type int, value 5)

Every variable corresponds to: location in memory.

Binary operators (=, + etc.) take two arguments or operands.

Arithmetic in C++:

Case sensitive. Cannot begin with a digit.

 o + (addition) o – (subtraction) o *(multiplication) o / (integer division) o %(modulus)

Rules of operator precedence (order of evaluation):

 o ( ) (Parenthesis) o *, / , % o +, -

Equality operators: = = and !=

Relation operators: >, <, >=, <=

Associativity:

 o ( ), *,/,%,+,-,<,>,<=,>=,==,!= - left to right o = - right to left (value is assigned to name)

WRITING SIMPLE PROGRAMS in PY

Arithmetic operators:

 o + (addition) o – (subtraction) o * (multiplication) o / (division) o // (integer division) o ** (expontiation) o % (modulus division)

String operations:

 o + (string concatenation) (‘key’+’word’=’keyword’) o * (string repetition) (‘Hey’ * 3 = ‘HeyHeyHey’) o ==, != (string comparisons) (‘Hey’ == ‘Bye’ – false)

Python is a typed language (like ‘C’)

Python is a dynamically typed language (unlike ‘C’)

Python float == C double

Comparison operators: >, <, >=, <=, ==, !=

Operator associativity:

 o ( ), *,/,%,//, +,- - left to right o **, = - right to left o >, <, >=, <=, ==, != - non associative (evaluation left to right)

Comparison syntax is not always the same as in C/C++.

BOOLEAN OPERATIONS

NOT, AND, OR (logic operators)

|And| truth table:

>>>True and True True

>>>True and False False >>>False and True False >>>False and False

False >>>True or True True

>>>True or False True

>>>False or True True

>>>False or False False >>>not True

False

>>>not False

True

 o True & True = True o True & False = False o False & True = False o False & False = False

|Or| truth table:

 o True & True = True o True & False = True o False & True = True o False & False = False

|Not| truth table:

 o True = False o False = True

In Python and C/C++ logic operators are left to right associative.

Short circuit evaluation the stopping of evaluation once the result is known, not when all of parts of the boolean expression have been evaluated. (e.g. 5<0 and 6>1)

Short circuit evaluation: OR operator will omit its right operand if the left operand is True.

Short circuit evaluation: AND operator will omit its right operand if the left operand is False.

Short circuit evaluation may sometimes lead to unexpected results/errors.

0, NULL (in C++), None or empty sequence (Py) = false

Operator precedence in Python and C/C++:

>>> x =True >>> x or Error True >>> x = False >>> x or Error <Traceback > Name ‘Error’ is not defined

 o ( ) Parenthesis || ( ) Parenthesis o ** Exponentiation || not, ! (Logical NOT) o *, / , % || *, /, % o +, - || +, - o <,>,<=,>=,==,!= || <,>, <=, >= o not (Logical NOT) || ==, != (comparisons for equality) o and(Logical AND) || and, && (Logical AND) o or (Logical OR) || or, || (Logical OR) o || = Assignment

STRUCTURED PROGRAMMING

Sequential execution carrying out operations in sequence.

Transfer of control some operations enable to specify that the next statement to be executed maybe other than the next one in the sequence.

Control structures operations that allow transfer of control.

Control flow (aka flow of control) refers to the order in which individual statements, operations or function calls are executed or evaluated.

Structured programming - subset of imperative programming. Most famous for removing or reducing reliance on the go to statement.

Best to avoid goto, instead use three control structures:

 o Sequence structure o Selection structure (branching) o Repetition structure

Structured A mod B algorithm

Repetition control structures:

 o for loop (C++/Python) o while loop (C++/Python) o do while loop (C++)

Branching control structures:

 o if and if/else (C++/Python) o switch (C++)

A=11

B=7

while A>=B:

A = A B print A

Non-structured control flow operations:

 o break and continue (C++/Python) o else clause on loops (Python)

Cartesian product of two sets A and B, (A X B) is the set of all possible pairs made out of elements (a, b) where a belongs to A and b belong to B.

Cartesian product example: A[True, False], B[False, True] = TrueTrue, TrueFalse, FalseTrue, FalseFalse.

FUNCTIONS AND PROCEDURES

Procedures = functions, subroutines, routines, sub-programs, methods.

Def 1: relation between an input set and an output set. The relationship between these sets may not necessarily be a function.

Def 2: an algorithm that may be used as a component-part to construct other larger algorithms (programs).

Def 3: high level control structure which groups together a number of related statements.

A procedure which is part of computer program P is said to have side-effects if:

 o It results in an operation which modifies the shared or global state of computer program P; OR: o Its output depends not only on its input arguments but also on the global state of computer program P.

Side-effect examples: procedures which modify shared program data, depend on shared program data, receive interactive input etc.

 o Once defined, can be called multiple times (reducing duplication of code) o Help break up programs to smaller manageable tasks o Make programs easier to read and develop amongst a team of people o Once correctly defined, there’s no need to worry about implementation – procedures hide their implementation details

Procedural/Imperative programming language a language which facilitates the development of computer programs as collections (libraries) of procedures.

Procedure call program control is transferred from “caller procedure” (aka “caller’s environment”) to the “body” of the “called procedure” (aka “callee”).

The return statement or the last statement of a procedure body signifies the end of a procedure call. The end of a procedure call signifies that the program’s control flow continues from the point where the procedure call was originally made.

None type (Py) and void (C/C++) used when a procedure returns no specific arithmetic result directly to its caller.

Argument-list of a procedure definition a comma-separated list containing the name of each argument received by the procedure when it is called.

LOCAL VARIABLES AND SCOPE

Block [of statements] (Py), compound statement (C++) group of statements that are executed as a unit. A.k.a. ‘body’ of a procedure.

Local evaluation environment (or frame, local scope) well defined set of variables used during the evaluation of a procedure. Variables belonging to this environment are called local variables (procedure’s arguments+variables defined within procedure)

A new local evaluation environment is generated every time a procedure is evaluated. Every time the procedure is terminated, the frame is normally deleted.

Global variables variables which are not an argument of a procedure or are outside of it. They belong to a set of variables known as the ‘global evaluation environment’.

Storage duration (global variable) duration of execution of program

Storage duration (local variable) duration of execution of procedure

Scope part of the program where a variable is visible & can be used without errors.

Scope (globalvariable) whole program or module/library they are defined in.

Scope (local variable) body of procedure they are defined in, any other enclosed/nested frames following their definition.

Stack a data structure used by most imperative languages to organise nested evaluation of procedure calls. Resembles a physical stack where two operations are allowed A)PUSH place on top, B)POP remove from top.

Python allows having nested procedure definitions. C/C++ does not.

STACK AND RECURSION

Definition of integer factorial:

Recursive definition:

n!= 1*2*3

0!=1 (base case(s))

*n*(n+1)

n!=n (n-1)! (Recursion step, how case (n-1)! is related to n!)

Recursive procedure procedure containing a call to itself.

Call graph graphic representation of calling relationships between procedures in a program.

Fibonacci series:

 o Base case: Fibonacci(0)=0; Fibonacci(1)=1 o Recursive step: Fibonacci(n)=Fibonacci(n-1) + Fibonacci(n-2) o Number series: 0,1,1,2,3,5,8,13,21

DATA STRUCTURES

Data structure (in computer science) a particular way of storing and organizing data

so that it can be used efficiently. (e.g. arrays, records, link-lists, stacks, queues

)

RAM (random access memory) is a form of computer data storage. Today, it takes the form of integrated circuits that allow stored data to be accessed in any order (randomly). ‘Random’ refers to the fact that any piece of data can be returned in a constant time, regardless of its physical location and whether or not it is related to the previous piece of data.

We can imagine RAM as a long array of memory elements, each of which have the capacity to store one byte, and each identified by an integer value (its address).

Array one of the most fundamental data structures. Defining feature: the way data is organised within RAM: any one element may be accessed in constant time. For efficiency, elements are stored in consecutive locations in memory, with each element having the same size.

Indices used to access array elements in order to read/modify their values.

Address of element given by index ‘n’ in memory is given by:

 o address(n) = (a + s*n) o a – starting address, n – array index, s – size of array elements in bytes

Data type name identifying different types of data. It signifies a possible range of data values and operations allowed on these values. (e.g. integers, floats, strings )

Python also allows sequence data types.

Sequences in python: strings, lists, tuples. Only lists are mutable. len(Length), in(membership), [index](access), [index : index] work the same in all three.

Sequences in Py are typically implemented as arrays of references. In fact, in Py all variable names are references to ‘objects’.

In order to define lists that contain only a single data type in Python one has to import the module ‘array’.

ARRAYS, REFERENCES, POINTERS

Variable data stored in computer memory. Value data referred to by the variable.

Variables are usually given a symbolic name/identifier.

Constants variables that point to data which cannot change during program evaluation. Such variables are immutable.

A variable in C/C++ is often a name that refers to data, of a given data type, stored in memory.

(C/C++) Once a new value is assigned to a variable, the data referred to by the variable in memory changes.

In python all variable identifiers are references to objects in memory. This is why assignment operations are different in Python/ C/C++ (one rebinds, other changes)

Reference: variable that identifies another variable in memory.

Modifying a reference does not change the object referred to, it changes the contents of the reference variable.

In C/C++ we can define variables that are references using pointers. Pointers a re a built in data type in C/C++. It is declared like this: int *xPtr;

 o A)int is the data type of the variable the pointer refers to o B)* indicates that this is a pointer declaration o C)xPtr is the name of the pointer variable

Two operations defined in relation to pointer data types in C/C++:

 o Address of operation (operator ‘&’) o Dereference operation (operator ‘*’)

If the value of a pointer variable is NULL/0, it implies that the pointer holds a non- valid address and it has not been initialised.

Pointers can be compared if they point to members of the same array (relation operators work properly).

Pointers and integers can be added or subtracted.

Pointers can be subtracted if they point to members of the same array.

RECORDS

Data record (or record) on of the simplest and most fundamental data structures. Made of two or more data “members”/”fields” stored in consecutive locations in memory, each member not necessarily the same type or size as the previous one.

Object (in OO language) data record which includes tailor made procedures to handle the data stored in it.

The data fields are usually made of various primitive data type (ints, floats, strings

The data layout of a record is referred to as the data type of a Record.

Using records and record data types usually requires these operations:

)

 o Declaring a record, including the type, size, names of its fields o Create instances of a record, i.e. declare variables which will hold the record o Accessing and modifying the data fields o Copying one record to another record of the same ‘type’ o Comparing two records of the same ‘type’

To copy a record in py, one has to import the ‘copy’ module

 o import copy o r2 = copy.copy(r1)

To compare two records of the same type, one has to compare their corresponding fields.

To copy all data from one record to another without copying references, one can use:

o Pair2 = copy.deepcopy(pair1)

TURING MACHINES

GCD (greatest common divisor) algorithm finds the highest integer that divides

exactly into A and B. The algorithm operates by dividing the smallest number into the largest number and taking the remainder iteratively until the remainder is 0, i.e.:

 o 550 mod 155 = 85 o 155 mod 85 = 70 o 85 mod 70 = 15 o 70 mod 15 = 10 o 15 mod 10 = 5 o 10 mod 5 = 0, therefore the result is 5.

GCD algorithm:

 o 1) Read two integers A and B, with B

The idea of a mechanical method or procedure requires that the person/machine executing the procedure needn’t know what the procedure is about and needn’t apply any judgement or ingenuity.

The operation of TM is defined by a set of transition rules (aka transition table), i.e. 0001R (state|markstate|mark|movement)

The blackbox can execute the .HALT command which takes the TM into a complete ‘halt’.

TM with a BB of two {a,b} internal states which adds 1 to any unary number (aka UN+1 TM):

 o a0  a0R o a1b1R o b0a1R.HALT o b1b1R

Church-Turing thesis: every mechanical calculation, effective procedure, or algorithm can be computed by a TM.