comp3 new revision guide updated

Upload: alex-jones

Post on 02-Apr-2018

223 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/27/2019 COMP3 New Revision Guide Updated

    1/72

    Comput

    ingCOMP3 Revision Guide

    CONTENTSContents................................................................................................................ 1

    3.1 Problem Solving...............................................................................................3

    3.1.1 Abstraction and Information Hiding...........................................................3

    3.1.2 Comparing Algorithms...............................................................................3

    3.1.3Types of Problem........................................................................................7

    3.1.4 Finite State Machines................................................................................7

    3.1.5 Language and Notation........................................................................... 11

    3.2 Programming Concepts.................................................................................14

    3.2.1 Programming Paradigms.........................................................................14

    3.2.3Abstract Data Types.................................................................................18

    3.2.4 Queues.................................................................................................... 20

    3.2.5 Stacks......................................................................................................22

    3.2.6 Hashing....................................................................................................23

    3.2.7 Graphs and trees.....................................................................................25

    3.2.8 Searching and sorting..............................................................................32

    3.2.9 Simulations..............................................................................................35

    3.3 Real Numbers................................................................................................ 36

    3.3.1 Real numbers.......................................................................................... 36

    3.4 Operating Systems........................................................................................38

    3.4.1 Role of an operating system....................................................................38

    3.4.2 Operating system classification...............................................................41

    3.5 Databases...................................................................................................... 46

    3.5.1Conceptual data modelling.......................................................................46

    3.5.2Database design.......................................................................................47

    3.5.2 Structured Query Language (SQL)...........................................................503.6 Communication and Networking....................................................................52

    1

  • 7/27/2019 COMP3 New Revision Guide Updated

    2/72

    3.6.1 Communication Methods.........................................................................52

    3.6.2Baud, bit rate, etc.....................................................................................53

    3.6.3 Asynchronous data transmission.............................................................53

    3.6.4 Baseband and broadband........................................................................55

    3.6.5 Networks..................................................................................................55

    3.6.6 Network topologies..................................................................................57

    3.6.7 Networks Part Two...................................................................................58

    3.6.8 Routers and gateways.............................................................................60

    3.6.9Web services............................................................................................ 64

    3.6.10Wireless networking............................................................................... 65

    3.6.11 Server-side scripting..............................................................................66

    3.6.12 Security................................................................................................. 673.6.13 Encryption............................................................................................. 70

    2

  • 7/27/2019 COMP3 New Revision Guide Updated

    3/72

    3.1 PROBLEM SOLVING

    3.1.1Abstraction and Information HidingAbstraction - simplifying a problem to omit all unnecessary details.

    Information hiding - hiding the complexities of the system behind a simple to

    use interface.

    -

    An example of information hiding

    Consider a pocket calculator. Anyone familiar with the device is likely to be able

    to operate a calculator that they haven't seen before. The keypad and screen are

    the interface that is used for communication with the user. The workings of the

    calculator are normally behind plastic cases and are hidden from the user. The

    user does not need to know how the calculator works in order to use it - they justneed to be familiar with the standard interface.

    3.1.2 Comparing AlgorithmsTime complexity - how long an algorithm takes to complete a task with a given

    input.

    Space complexity - how much memory an algorithm needs to complete a task

    with a given input.

    -

    Building on the definition of an algorithm from the AS course, there are some

    additional key points to remember: An algorithm is a sequence of unambiguous instructions.

    An algorithm has a range of legitimate inputs and should produce the

    correct result for all values within the range.

    Different algorithms can be developed to perform the same task. These

    algorithms can have different time and space complexities.

    The overall complexity of an algorithm depends on its time complexity and space

    complexity.

    Some algorithms are more time efficient than other -- insertion sort is faster

    than bubble sort.

    Some algorithms are more space efficient than others -- some use memory

    efficiently, others waste RAM.

    The table on the following page gives the order of complexity of algorithms.

    These points should be noted:

    3

    *

    *

  • 7/27/2019 COMP3 New Revision Guide Updated

    4/72

    Quadratic and cubic time are examples of polynomial time which take the

    following form:

    The number of nested loops can dictate the value of c. For example, a bubble

    sort algorithm contains a loop inside a loop meaning that it will have

    quadratic time complexity.

    4

  • 7/27/2019 COMP3 New Revision Guide Updated

    5/72

    Order of complexity

    Big Onotation

    Graph Shape Complexity Examples / Comments

    Constant time Is a number odd oreven?No matter the size of theinput, it will always just bea matter of looking at thelast digit.

    Logarithmictime

    Binary searchIf a list is constantly beingsplit into half with one halfrejected then its timecomplexity will not growas fast as linear time.

    Linear time Linear searchIf each item in a list has tobe checked to see if it isthe desired item then thetime complexity will growwith the list size.

    Linearithmictime

    Quick sortA quick sort splits a listand sorts it recursively. Inthe base-case scenario ithas linearithmic time

    complexity.Quadratictime

    Bubble sort algorithmTwo items in a list arelooked at together andeither swapped or not.Each pass can feature n -1 swaps and there are canbe a total ofn - 1 passes,giving quadratic timecomplexity.

    Cubic time

    Exponentialtime

    The recursive Fibonaccialgorithm roughly has thislevel of time complexity.

    Factorial time Travelling SalesmanProblem

    The travelling salesman

    problem tries to find theoptimal tour between a

    5

    1

    n

    f

    n

    f

    n

    f

    x

    n

    f

    n

    f

    n

    f

    n

    f

    x

    FA

    STE

    S

    T

    SLO

    WE

    ST

  • 7/27/2019 COMP3 New Revision Guide Updated

    6/72

    finite number n of nodes.As n grows in size, theproblem becomesincreasingly more difficult.

    6

  • 7/27/2019 COMP3 New Revision Guide Updated

    7/72

    3.1.3Types of ProblemNon-computable - a problem that does not have an algorithmic solution.

    Tractable - a problem that has a reasonable (polynomial) time solution.

    Intractable - a problem for which no reasonable time solution has yet beenfound.

    Decision problem - a problem whose answer is always yes or no.

    Undecidable - a decision problem that is not computable.

    Heuristic solution - a trial and error approach using 'informed guesses' or

    learned knowledge to find a solution to an intractable problem.

    The Travelling Salesman Problem

    The travelling salesman problem is an example of an intractable problem. This is

    since the most straightforward method of finding the shortest tour between a

    number n of nodes is to try all possible permutations. The number ofpermutations will become larger and larger, with only small networks

    representing tractable problems. This is why heuristic methods such as starting

    from one node and choosing the smallest distance to another node (an

    adaptation of Prim's algorithm) are often devised to simplify the problem.

    The Halting Problem

    The halting problem is a famous non-computable and hence undecidable

    problem. It asks the question, is it possible to create a program which takes

    another program as an input and determineswhether it will halt or whether it will

    loop infinitely? Through reductio ad absurdum (proof by contradiction), AlanTuring proved that such a program is impossible.

    3.1.4 Finite State MachinesMealy machine - an FSM that determines its outputs from the present state and

    inputs.

    Moore machine - an FSM that determines its outputs from the present state

    Mealy machine

    Inputs and outputs on transitions.

    Moore machine

    Inputs on transitions, outputs on

    7

    S

    0

    S

    1

    'a'|'A'

    'b'|'B'

    inpu

    toutp

    ut

    transitio

    n

    state

    S

    0

    S

    1

    'a'

    'b'

    'A''B'

    inpu

    t

    output

    *

    *

  • 7/27/2019 COMP3 New Revision Guide Updated

    8/72

    states.

    A finite state machine without an output is known as a finite state automaton

    (FSA).

    FSA are restricted to decision problems (they only output yes or no). If a given input causes an FSA to stop at a valid halt state then the output is

    Below is a diagram demonstrating a finite state automaton for unlocking a

    combination lock with the code 2371. Since an input from any given state only

    corresponds to one transition it can also be called a deterministic FSA or

    deterministic finite automaton (DFA).

    Non-deterministic FMAs have their uses in pattern matching and can be

    converted into DFA's.

    NFA(Nondeterministic FiniteAutomaton)

    DFA(Deterministic FiniteAutomaton)

    8

    STA

    RT

    NOT

    2

    2

    2

    NOT

    3

    23

    3

    NOT

    7

    23

    7

    7

    NOT

    1

    UNLOCK

    ED

    initial

    state acceptingstate

    a

  • 7/27/2019 COMP3 New Revision Guide Updated

    9/72

    9

    1 2 6

    53

    47

    a b

    b

    a

    b

    b

    a

    a

    a or b

    b

    1 2 5a b

    3

    a

    a or b

    b

    6

    b

    b

    4a

    b

  • 7/27/2019 COMP3 New Revision Guide Updated

    10/72

    Principle of universality - a universal machine is a machine capable of

    simulating any other machine.

    Equivalent Turing machine - all other types of computing machine are

    reducible to an equivalent Turing machine.Power of a Turing machine - no physical computing device can be more

    powerful than a Turing machine. If a Turing machine cannot solve a decision

    problem, nor can any physical computing device.

    Alan Turing devised the Turing machine, an abstract computational device, in

    order to explore the limitations and capabilities of computer machines. Turing

    machines are the most basic of computing machines (their operations cannot be

    divided any further) and therefore have the theoretical potential to describe the

    operation of any computing machine. This is the principle of universality. Byreasoning that every machine has an equivalent Turing machine, we can

    conclude that nothing is more powerful than a Turing machine.

    Example Turing Machine

    The above state transition diagram shows an algorithm which will print "1 0 1 0 1

    0" on our theoretical tape infinitely (until there is no more empty tape

    available).

    1 0 1 0 1 0 1 0 1 0

    If we halted the Turing machine as it approaches the end of its fifth pass our tapewould appear as shown above. We can display the table of instructions which

    creates this pattern as shown below:

    Currentstate

    New state Input Output Tape head

    A B 1 Move right

    B C Move rightC D 0 Move right

    D A Move right

    10

    A B C D

    |1 | |0

    |movem

    ent

    inputoutput

    *

  • 7/27/2019 COMP3 New Revision Guide Updated

    11/72

    Another way to express this is using a transition function with the following

    syntax:

    (current state, input symbol) = (next state, output symbol,

    movement)

    These are the appropriate transition functions for our example:

    (A, ) = (B, 1, )

    (B, ) = (C, , )

    (C, ) = (D, 0, )

    (D, ) = (A, , )

    The Busy Beaver Function

    The function B(n) is defined as calculating the largest number of ones an n-state

    Turing machine can write on an initially empty tape and still stop. Busy

    beaversare machines which produce B(n) marks with n states. The function B(n)

    can be easily defined but remains non-computable.

    Turing also conceived a universal Turing machine (UTM). The following

    description details a UTM which uses a single one-dimensional tape. The instructions of the Turing machine, M, are placed on the tape followed by

    the data, D, to be processed by M.

    The UTM, U, processes M and D by starting with its read/write head

    positioned on M and then moving between M and D as M is executed.

    U may be a lot slower than M but it acts as an interpreter would, identifying the

    next instruction to be executed, and then executing it.

    3.1.5 Language and Notation

    Natural language - a real spoken and written language with grammar or syntaxrules and ambiguities, such as English and French.

    Formal language - a language defined by an alphabet and rules of syntax.

    Regular language - any language that an FSM will accept.

    Bothregularand formal languagescan be defined by a regular

    expression or Backus-Naur form.

    Natural language is a lot less strict than both regular and formal

    languages.

    Regular Expressions(Regex)

    Regular Expressions are used for data validation, file searching, and matching.

    Regex Meaning Examplesa* Matches zero or more a's. c(at)* cat, c, catat,

    catatata+ Matches one or more a's. ar+t art, arrt, arrrta? Matches zero or one a's. colou?r colour and

    coloronly.a|b Matches a or b. gr(a|e)y gray and grey

    only.[ab] Matches a or b - an alternative

    form of a|b.gr[ae]y gray and grey

    only.[a-z] All lowercase letters. ([a-z])+ zbcbs, acdssx

    11

    *

  • 7/27/2019 COMP3 New Revision Guide Updated

    12/72

    [A-Z] All UPPERCASE letters. [A-Z]([a-z])+ Tom, Harry,London

    \d Matches any single digit. Student\d Student5,Student8

    \w Matches any single

    alphanumeric character.

    \w\w\d\d Mk19, sC52

    \W Matches any single non-alphanumeric character.

    \d\d\W 90?, 23$

    \s Matches a single space. 123\d\d 123 2, 123 8. Matches any single character. .ear hear, fear, dear^f Matches an 'f' with nothing

    before it.^re rebound, rehab

    f$ Matches an 'f' with nothingbefore it.

    ine$ cosine, genuine

    Backus-Naur Form(BNF)

    Backus-Naur form is a notation for expressing the rules for constructing valid

    strings in a regular language. It can be expressed in a number of ways.

    Consider a British postcode for example SQ12 4YA. While "SQ" does not

    correspond to any city and therefore the postcode is invalid, it is in fact

    syntactically correct. We can break down this syntax as follows:

    ::=

    ::= ||

    |

    ::= ::= A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z

    ::= 0|1|2|3|4|5|6|7|8|9

    ::= " "|""

    This can be represented on a parse tree.

    The same syntax can also be expressed by a series ofsyntax diagrams.

    12

    S Q 1 2 4 Y A" "

    This value is terminal

    since it cannot be brokendown any further.

    Anything expressed in triangular

    brackets, , is non-terminal

    since it is an expression of

    terminal and/or non-terminal

    values.

  • 7/27/2019 COMP3 New Revision Guide Updated

    13/72

    Reverse Polish Notation(Postfix Notation)In reverse Polish notation (RPN), operands are followed by operators, rather

    than there being operators between the operands as we see in infix notation.

    There are many advantages of this notation of expressions:

    No need for parentheses to avoid ambiguity

    Calculations occur as soon as an operator is specified

    RPN calculators have no limit on the complexity of the expressions that can

    be evaluated

    No equals key needs to be included for an expression to be evaluated

    We can convert an expression using infix notation to a postfix expression like so:

    Take our infix expression e.g. 5 + ((1 + 2) * 4) - 3

    Add all implicit parentheses: {[5 + ((1 + 2) * 4)] - 3}. You should ensure

    that each set of brackets only encloses one operator.

    Shift all operators to the right side of their containing brackets: {[5 ((1 2 +)

    4 *) +] 3 -}

    Remove all the brackets: 5 1 2 + 4 * + 3 -

    13

    first-bit optional-

    space

    last-bit

    postco

    de

    lette

    rlette

    r

    digit

    digitfirst-

    bit

    " " ""

    optional-

    space

    lette

    r

    lette

    r

    digit

    last-bit

    0 1

    digit

    2 3 etc

    .

    A B

    letter

    C D etc

    .

    Terminal values

    are represented in

    ellipses.

  • 7/27/2019 COMP3 New Revision Guide Updated

    14/72

    We can convert postfix to infix using a stack which follows a last-in first-out

    (LIFO) basis.

    Input Operation Stack Infix expression5 Push operand 51 Push operand 5, 12 Push operand 5, 1, 2+ Add 5 1+24 Push operand 5, 4 1+2* Multiply 5 (1+2)*4+ Add -- ((1+2)*4)+5

    3 Push operand 3 ((1+2)*4)+5- Subtract -- ((1+2)*4)+5-3

    3.2 PROGRAMMING CONCEPTS

    3.2.1 Programming ParadigmsStructured Programming- reminder from COMP1

    Structure programming is the preferred approach to writing code. Its aim is to

    create programs which are easy to understand and work efficiently. Outlined

    below are the main approaches towards following structured programmingprinciples:

    Meaningful identifiers:

    When choosing what to name a variable you should consider what purpose

    the variable has. Ensure consistency for the style of identifier name (e.g. use

    camelCase throughout).

    Indentation:

    Indenting code ensures that it is easy to identify where a function begins and

    ends and that the code appears a lot neater and easier to read.

    Procedure & functions:

    Breaking tasks down into subtasks allows each function or procedure to dealwith its own subtask only. Programs are made more readable if functions

    have only one task and it also becomes much easier to adjust the code since

    all functions have distinct operations.

    Avoid global variables:

    Since the value of global variables can be changed at any point during the

    function, it quickly becomes difficult to keep track of the variable's value.

    Especially for other programmers looking at another person's code, it is a lot

    better to pass variables as arguments and avoid global variables altogether.

    14

    5 + ((1 + 2) * 4) - 3 5 1 2 + 4 * + 3 -

    operan

    doperato

    r

    infix postfix

  • 7/27/2019 COMP3 New Revision Guide Updated

    15/72

    Programmingparadigm

    Description Example

    Functionalprogramming

    Treats computation as theevaluation of mathematicalfunctions.

    Haskell, F#.

    Logic programming Defines a set of facts andrules.

    Prolog.

    Event-drivenprogramming

    The flow of the program isdetermined by events suchas mouse clicks and keypresses which triggersubroutines.

    C, C++, etc. Mostlanguages.

    Object-orientedprogramming

    Programmers use instancesof class in order to createobjects.

    C++, Java, etc.

    Procedural/imperativeprogramming

    Code is executed one lineafter the other. Code can be

    15

  • 7/27/2019 COMP3 New Revision Guide Updated

    16/72

    Object-oriented programming

    Object - an instance of a class.

    Instantiation - the action of declaring an instance of a class (an object).

    Class definition - a pattern or template that can be used to create objects ofthat class.

    Encapsulation - combining a record with the procedures and functions that

    manipulate it to form a new data type, a class.

    Inheritance - defining a class and then using it to build a hierarchy of

    descendant classes with each descendant inheriting access to all its ancestors'

    code and data.

    Polymorphism - giving an action one name that is shared up and down a class

    A class defines the properties and methods (procedures and functions -

    behaviours) that will be used for each instance of that class. Below is an exampleof a class written in C#.

    class Member{

    private intMemberShipNo;private string Name;private string Email;public Member(intnewmemberno, stringmembername, string email){

    MemberShipNo = newmemberno;

    Name = membername;Email = email;

    }public voidAmendMember(stringmembername, string email){

    Name = membername;Email = email;

    }public voidDisplayMember(){

    Console.WriteLine("ID: {0}, Name: {1}, Email: {2}",MemberShipNo, Name, Email);

    }}

    In this basic example we have a class named "Member". We initialise this class

    by saying that we can create a new member with three main properties;

    membership number, name, and email address. An instantiation of this class

    would be written, for example, "Member mymember = new Member(1,

    "Frederick Dragonswatter", "[email protected]");". We can then

    use the public methods to alter or print the information which we have stored;

    this usage of public methods to deal with private variables is referred to as

    encapsulation.

    16

    *

    Private variables, procedures and

    functions are only accessible inside

    the class.

    Everything which is public

    can be accessed outside

    the class.

  • 7/27/2019 COMP3 New Revision Guide Updated

    17/72

    Important points to note:

    A child class inherits all the properties and methods of its parent class. This

    may be used for adding more complexity to a basic functional class, for

    example, a scientific calculator child class with a calculator parent class.

    A child class can override methods, an idea called polymorphism. This is

    particularly useful when applying the same method to every object in a list

    when a different result is desired for different objects.

    Above is a simple inheritance diagram which shows the relationship between a

    clock, an alarm clock and a watch. We can expand on this diagram with a class

    diagram as shown below.

    Below, again in C#, is an example of inheritance and polymorphism.

    public class Clock{ private intHours;

    privateintMinutes; public virtual void Declare()

    public classAlarmClock : Clock{ public override void Declare()

    {Console.WriteLine("I am an alarm

    17

    Clock

    Alarm clock Watch

    is ais a

    TClockCLASS

    Fields:Hours

    Minutes

    Methods:

    SetTime

    GetHours

    GetMinutes

    IncrementTim

    e

    TAlarmClockCLASS

    Fields:Hours

    Minutes

    Methods:

    SetTime

    GetHours

    GetMinutes

    IncrementTimeAdditional field and

    methods:AlarmHours

    AlarmMinutes

    SetAlarmTime

    GetAlarmHour

    s

    GetAlarmMinu

    tes

    TWatchCLASS

    Fields:Hours

    Minutes

    Methods:

    SetTime

    GetHours

    GetMinutes

    IncrementTimeAdditional field and

    methods:DayOfWeek

    DayNumber

    SetDay

    SetDayNumbe

    r

    GetDay

    GetDayNumbe

    r

    These fieldsand methods

    are

    automaticall

    y inherited

    by the child

    class.

  • 7/27/2019 COMP3 New Revision Guide Updated

    18/72

    {Console.WriteLine("I am a

    clock.");}

    }

    clock.");}

    }

    3.2.2 Recursive TechniquesRecursive routine - a routine defined in terms of itself.

    General case - the solution in terms of itself for a value n.

    Base case - a value that has a solution which does not involve any reference to

    the general case solution.

    Stack frame - the locations in the stack area used to store the values referring

    to one invocation of a routine.

    Recursive routines can often offer elegant solutions to a problem. Despite this,

    they are less efficient in terms of both time and space than iteration. One of the

    most common examples of a recursive method is the calculation of a factorial

    value, n!, with the function name Factorial(n). Below is code, in Pascal, for the

    function.

    Function Factorial (n : Integer) : Integer; Begin

    Ifn = 1

    Then Result := 1 Else Result := n * Factorial(n-1)

    End

    This only works because the routine is called with values passed as parameters.

    Recursion does not work with global variables. For each invocation of a recursive

    routine, a portion of the stack (the stack frame) is assigned to store return

    values. This means that, for some inputs, there is a risk of stack overflow. Trying

    to calculate 0! recursively will cause such an error since the base case will never

    be reached.

    Below is a dry run for calculating 5!:

    Callnumber

    Functioncall

    n Result Result Returnvalue

    1 Factorial(5) 5 5 * Factorial(4) 5 * 24 1202 Factorial(4) 4 4 * Factorial(3) 4 * 6 243 Factorial(3) 3 3 * Factorial(2) 3 * 2 64 Factorial(2) 2 2 * Factorial(1) 2 * 1 25 Factorial(1) 1 1 1 1

    3.2.3Abstract Data Types

    18

    The child class overrides the method

    defined by the parent class but inherits the

    other fields.

    *

    This is the base case since a factorial is defined as n!

    = n x (n-1) x x 1, therefore 1 is the smallest value of

    n in the recursion.This is the general case since it is the

    recursive definition for every value except the

  • 7/27/2019 COMP3 New Revision Guide Updated

    19/72

    Abstract data type (ADT) - a data type whose properties are specified

    independently of any particular programming language.

    List - a collection of elements with an inherent order.

    Pointer - a variable that contains an address. The pointer points to the memory

    location with that address.

    Null pointer - a point that does not point to anything, usually represented by or -1.

    Dynamic data structure - the memory taken up by the data structure varies at

    run time.

    Static data structure - the memory required to store the data structure is

    Lists are a type ofabstract data typesince they are a collection of elements

    with an inherent order but this order is expressed outside of the programming

    language with the programmer needing no knowledge of how the list is stored or

    how it functions (an example of information hiding). There are two key types of

    lists.

    Linear lists

    A linear list is a static structure. This means that on declaring the list it is

    reserved a portion of the heap using adjacent memory locations. In this

    instance, order is given by the order of the memory locations that the items

    occupy.

    Advantages:

    Linear lists are easy to program.

    If elements are stored in key order, a binary search is possible.Disadvantages:

    Memory locations may be wasted due to arrays being static.

    Insertion of an element within an ordered list requires moving elements.

    Deletion of an element within a list requires moving elements.

    Linked lists

    A linked list is a dynamic structure. Since each item in the list points to the next,

    there is no need for the list to occupy adjacent memory locations in the heap

    therefore the list can be as large or small as necessary.

    Let's say we want to insert the words "one", "two", "three" and "four" into our

    list.

    19

    one two three

    four

    Start

    A null pointer is

    required to indicate

    the end of the list.

    *

    *

  • 7/27/2019 COMP3 New Revision Guide Updated

    20/72

    If we want to put these into alphabetical order we can simply rearrange the

    pointers as demonstrated below.

    We can program this as an array, with each item connected to a pointer. The

    table on the next page shows how to express the above linked list as an array.

    When adding an item in this format, the next item will be placed in the field with

    the "NextFree" index and the current last item in the list will point to the new

    item. The "NextFree" value will then be altered. When deleting an item, simply

    update the pointers and the "Start" or "NextFree" values in order to ensure that

    it is still possible to chain through the list and to know where the first empty fieldis.

    In dynamic allocation memory space is only allocated when required at run

    time. Each time a list requires more memory space, it will be allocated a portion

    of the heap. If the memory locations used by a dynamic structure type are not

    given back to the stack when they are no longer in use, memory leakage will

    occur. This is when there becomes no memor left in the stack.

    3.2.4 QueuesQueue - a first-in first-out (FIFO) abstract data-type.

    Circular queue - when the array element with the largest possible index hasbeen used, the next element to join the queue reuses the vacated location at the

    beginning of the array.

    Linear queue - elements join the queue at one end and leave the queue at the

    other.

    20

    one two three

    four

    Start

    IndexData fieldsPointer

    field0one21two2three13four043

    4

    Start

    NextFre

    e

    VIP

    Join

    here

    Front of

    queue

    Front of

    queue

    *

  • 7/27/2019 COMP3 New Revision Guide Updated

    21/72

    A queue is a type of list where the first item to be added is the first item to be

    removed (FIFO). In the programming sense, a queue has two operations; add a

    new item to the rear of the queue or remove an item from the front of the queue.

    Some uses of queues in a computing context are these:

    print jobs waiting to be printed

    characters entered at the keyboard and held in a buffer

    jobs waiting to be executed under a batch operating system

    simulations

    Array implementation

    On the next page is an example of using an array to implement a queue. In this

    example the items stay static therefore will always remain at the same index.

    Adding Joe, Harry and Simon to the queue, and removing Fred and Jack from the

    queue, we now have a problem since we have reached the end of the memory

    locations reserved for this queue.

    Shuffle queue

    In a shuffle queue, once someone leaves the queue, all the items are moved to

    the next position along. A front pointer is not necessary since the item at the

    front of the queue will always be the item with the lowest index (in this case, 0).

    Circular queue

    In a circular queue, vacated entries may be reused. This means that when an

    index is no longer reachable by chaining through the queue, we should delete

    the item that was in that position, i.e. when a person leaves the queue, the

    space they occupied becomes free. The example below shows Jack rejoining the

    queue.

    21

    IndexData

    fields0Fred1Jack2

    Matt345

    0

    2

    Front

    Rear

    IndexData

    fields0Fred1Jack2

    Matt3Joe4Harry5Si

    mon

    3

    5

    Front

    Rear

    IndexData

    fields0Fred1Jack2

    Matt345

    2Rear IndexData

    fields0Joe1Harry2

    Simon345

    2Rear

  • 7/27/2019 COMP3 New Revision Guide Updated

    22/72

    Linked list implementation

    A linear queue includes a linked list of items in the queue which point to the

    item after them. We therefore need two extra pointers; one to point to the front

    of the queue and another to point to the rear.

    If we add an item to the queue, we simply add a pointer from Matt to the new

    item and move the rear pointer to point to this new item. Similarly, if we want to

    remove the Fred from the queue, we just need to move the front point to point to

    Jack.

    Priority queues

    Priority queues effectively take the first-in first-out principle of a queue and

    adjust it so that every element in the queue has an associated priority. The

    element in the queue with the highest priority will be the first to leave the queue

    therefore we essentially insert an element in the queue where they fit in terms of

    their given priority. Priority queues are especially used in simulations.

    3.2.5 StacksStack- a last-in first-out (LIFO) abstract type data.

    There are two operations which can be performed on a stack. These are adding a

    new item to the top of the stack (pushing) and removing an item from the top of

    the stack (popping).

    Some uses in a computing context are:

    Stacks are used to store the return address, parameter and register contents

    when a procedure or function call is made. When the procedure or function

    completes execution, the return address and other data are retrieved from

    the stack.

    Stacks are used for evaluating expressions in Reverse Polish Notation.

    22

    IndexData

    fields0Fred1Jack2

    Matt345

    0

    2

    Front

    Rear

    IndexData

    fields0Jack123Joe

    4Harry5Simon

    3

    0

    Front

    Rear

    Fred Jack Matt

    Front Rear

  • 7/27/2019 COMP3 New Revision Guide Updated

    23/72

    As with queues, there are two main ways of representing stacks.

    Array implementation

    In this instance, Fred, Jack and Matt are added to the stack in that order. While

    this stack goes downwards, they can also go upwards take heed of the order of

    the index numbers. If Matt and Jack are popped from the stack and Harry and Joe

    are pushed onto the stack, the stack will appear as shown by the diagram on the

    right. Since items are pushed onto and popped from the top of the stack, we only

    need one pointer.

    Linked list implementation

    In the linked list implementation of a stack, each item in the stack points to the

    item below it. If a new item is added to the top of the stack, the new item will

    point to Fred and the start pointer will point to the new item. Removing an item

    from the stack simply means the start pointer pointing to the next highest item

    on the stack.

    3.2.6 HashingHashing - The process of applying a hash function to a key to generate a hash

    value.Hash key - the key that the hash function is applied to.

    Hash function - a function H, applied to a key k, which generates a hash value

    H(k) of range smallerthan the domain of values ofk.

    Hash value - the value generated by the application of a hash function to the

    key.

    Hash table - a table with a column dedicated to the range of hash values that

    can be enerated b a l in a hash function to a ke . If the hash value

    The premise

    In databases there may be a table with thousands or even millions of records.This makes searching for a specific record more time-consuming especially since

    23

    IndexData

    fields0Fred1Jack2Matt345

    2TopOfStack

    IndexData

    fields0Fred1Harry2Joe345

    2TopOfStack

    Fred Jack Matt

    Start

    Joe

    *

  • 7/27/2019 COMP3 New Revision Guide Updated

    24/72

    a linear search may have to search through all records just to find one. A hash

    tablemakes reading, writing and deleting records a much quicker process.

    Hashing is also used for storing passwords in databases since the process of

    hashing cannot be reversed. This means that the user will be able to input a

    password which can be verified by passing it through the hash function buteven if someone could access the database table containing the passwords, they

    would not be able to successfully access any of the accounts. According to the

    pigeonhole principle, a good hash function will be one which generates as many

    combinations of hash values as there are combinations of hash keys so as to

    ensure no collisions by different hash keys .

    CollisionsCollision - when two or more different keys hash to the same hash value.

    Open hashing - a method in which a collision is resolved by storing the record in

    the "next available" location.

    Closed hashing - all other locations in the table are closed off therefore a

    pointer column is added and a linked list of records with the same hash key is

    created.

    Rehashing - when the initial hash results in a collision, the hash value of the key

    is rehashed to generate a new hash value.

    Linear rehashing - the original hash value is incremented by 1 modulo N, 2

    modulo N, etc. until an empty slot is found in a table of size N rows.

    As explained by the pigeonhole principle, if there are N + 1 items to be inserted

    into a table of size N rows, then there must be at least one row which will have to

    contain two items. In hashing terms, hashing two different keys to the same hash

    value is called a collision. There are two main methods of dealing with

    collisions.

    1. In open hashing, the hash value is rehashed in order to position itself in

    the next available location. This is especially advantageous if the table has

    a large number of rows so that collisions are infrequent. When searching,if the hash value is found in the table yet the record is not the desired one ,

    rehashing is used to look for where the desired item could be. If the end of

    the table is reached and the item is not found, only then can we

    conclusively say that an item is not in the hash table.If an item is deleted,

    a special marker must be put in place in order to prevent the search

    stopping prematurely.

    2. In closed hashing, collisions are predicted as almost common

    occurrences. A pointer column is introduced in the table and if a collision

    occurs between two hash keys, a linked list is created. When searching, ifthe hash value is found but the first item is not the desired one, the search

    24

    *

  • 7/27/2019 COMP3 New Revision Guide Updated

    25/72

    will follow through the linked list until it finds the item. If the end of the

    linked list is reached and the item has not been found, we can conclusively

    say that the item is not in the hash table. If an item is deleted there are

    two options for where a linked list exists. Either the deleted item in the

    chain is replaced by a special marker or each item is moved up.

    A worked example

    Give the contents of the hash table that results when you insert items with the

    keys CO M P U T I N G in that order into an initially empty table of M= 5 rows,

    using closed hashing. Use the hash function ktimes 11mod Mto transform the

    kth letter of thealphabet into a table index (row number), e.g., hash(B) = hash(2)

    = 22 Mod 5 = 2.

    Character

    k k * 11Mod 5

    Character k k * 11Mod 5

    C 3 33 Mod 5 =3

    T 20 220 Mod 5= 0

    O 15 165 Mod 5= 0

    I 9 99 Mod 5 =4

    M 13 143 Mod 5= 3

    N 14 154 Mod 5= 4

    P 16 176 Mod 5= 1

    G 17 187 Mod 5= 2

    U 21 231 Mod 5= 1

    Index Character Pointer0 O1 P2 G3 C4 I

    If we have a larger table, we can represent the same information using open

    hashing. We have nine letters to enter into our hash table so we should let the

    hash table have nine rows.

    Index Character0 O1 P2 U3 C4 M5 T6 I7 N8 G

    3.2.7 Graphs and trees

    25

    M

    U

    T

    N

    Here, we have used linear rehashing, i.e.keep incrementing the hash value until the

    row belonging to that index is empty. You can

    see how, since there are many collisions in

    this example, the indexes of a lot of the

    characters do not represent its actual hash

    value or a value close thereof.

  • 7/27/2019 COMP3 New Revision Guide Updated

    26/72

    Graph - a diagram consisting of circles, called vertices, joined by lines, called

    edges or arcs; each edge joins exactly two vertices.

    Neighbours - two vertices are neighbours if they are connected by an edge.

    Degree (of a vertex) - the number of neighbours for that vertex.

    Labelled or weighted graph - a graph in which the edges are labelled or given

    a value called a weight.Automation - turning an abstraction into a form that can be processed by a

    computer.

    Directed graph or digraph - a diagram consisting of circles, called vertices,

    joined by directed lines, called edges.

    Simple graph - an undirected graph without multiple edges and in which each

    edge connects two different vertices.

    Closed path or cycle - a sequence of edges that starts and ends at the same

    vertex and such that any two successive edges in the sequence share a vertex.

    Above are two diagrams which represent the same graph. The graph on the right

    is a directed graph or digraph form of the simple graph on the left. Simple

    graph cannot contain loops since these edges do not connect two different

    vertices.

    If we look again at the above diagrams, we can form a closed path or circuit

    from the graph by travelling on the path C-D-F-E-C. Here, we have visited

    different vertices sequentially and returned back to the node we began at.

    In computing, a graph often represents an abstration of a problem. For example,

    a company may have different business plans for generating profit and may

    want to discover which route would give them the most profit year on year. The

    London Underground map is a typical example of abstraction since it only keeps

    the important details and does not say true to the actual geography of the

    stations.

    Data representation of a graph

    A graph with multiple edges can be represented using an adjacency matrix or

    adjacency list.

    26

    A

    B

    C

    D

    E

    F

    A

    B

    C

    D

    F

    E

    *

  • 7/27/2019 COMP3 New Revision Guide Updated

    27/72

    Adjacency matrix

    An adjacency matrix of size n by n for a graph with n vertices stores whether or

    not two vertices are directly connected. We can use 0s and 1s to represent this

    information, 1 meaning that two vertices are neighbours and 0 meaning that

    they are not.

    For an undirected graph there will always be a symmetrical pattern as shown inthe above matrix, since aij = ajiwhere a is the cell in the adjacency matrix and i

    and j are two distinct vertices. Notice that the matrix tells us that vertex 1 is not

    adjacent to vertex 1.

    When the graph becomes directed, the matrix is no longer symmetrical. If we

    read offa31, we have a value of 0 meaning that we cannot travel from vertex 3 to

    vertex 1. However, reading off a13 tells us that we can travel from vertex 1 to

    vertex 3. Therefore aijaji. You should fill in the matrix for each row, I, to column,

    j, representing whether it is possible to travel from vertex i to vertex j.

    For a weighted or labelled graph, we can no longer use 0 in our adjacency matrix

    since it could easily be a valid distance between two vertices. For that reason,

    we may use the infinity symbol () instead. An adjacency matrix for a labelled

    graph may be called a distance matrix.

    27

    1 3

    2 4

    5

    12345101110210010310011411101

    500110

    1 3

    2 4

    5

    12345101100200010300011410001

    500000

    1 3

    2 4

    5

    1234511920223390411125

    0

    1

    2

    9

    2

    0

    1

    11

    9

    2

    3

  • 7/27/2019 COMP3 New Revision Guide Updated

    28/72

    Adjacency list

    An adjacency list specifies which vertices are connected in a different way to an

    adjacency matrix.

    Similarly, if we have a directed graph we fill in the list from the view of the vertex

    in the vertex column, i.e. which vertices can we go to from that node?

    If we want to fill in the table with distances the following format can be used (this

    time we do not need to use the infinity symbol because we only include adjacent

    vertices):

    Matrix or list?

    Adjacency matrix

    o If many vertex pairs are connected by edges, then the adjacency

    matrix doesnt waste much space and it indicates whether an edge

    exists with one access (rather than following a list).

    Adjacency list

    o If the graph is sparse, so not many of its vertex pairs have edges

    between them, the adjacency list is preferable.

    Trees

    If a non-directed connected graph has no cycles then we can identify it as being

    a tree. In a tree, there is just one path between each pair of vertices.

    28

    1 3

    2 4

    5

    VertexAdjacent vertices12, 3,

    421, 431, 4, 541, 2, 3, 553, 4

    1 3

    2 4

    5

    VertexAdjacent vertices12,

    32434, 541, 55

    VertexAdjacent vertices12,19; 3, 2024, 2334, 9; 5, 041, 11; 5,

    125

    1 3

    2 4

    5

    0

    1

    2

    9

    2

    0

    1

    11

    9

    2

    3

  • 7/27/2019 COMP3 New Revision Guide Updated

    29/72

    If a tree has a designated root from with every edge being directed away from

    this root it is called arooted tree

    .Algorithms

    Graphs can be used to represent mazes by placing a node on each decision point

    of the maze, i.e. each place where a path splits into two or more paths, as well

    as at the start and end positions. We can then designate three Boolean flags to

    each node. These flags are:

    Undiscovered is the node yet to be found?

    Discovered has the node been found yet?

    Processed or completely explored Have we visited all the incident edges

    of the node?

    In this abstraction, all the dead ends of the maze have been included to ensurethat each path of the maze can be fully explored as it might be in real life. Since

    the graph that we have abstracted from our maze has no cycles, it is a tree

    therefore we can transform it into a rooted tree by choosing A as our root node.

    On a rooted tree we can apply both breadth-first and depth-first searching

    algorithms in order to fully explore the whole of the maze.

    29

    AC

    D

    E

    F

    B A

    B C

    ED

    F

    root

    node

    Tree Rooted tree

    internal

    node

    leaf node

    A

    B

    C

    F H

    E J

    L

    N

    P

    A

    B C

    F

    HJ

    L

    NO

    D

    E

    G I

    K

    M

    O

    D

    E

    G

    I

    K

    M

    P

  • 7/27/2019 COMP3 New Revision Guide Updated

    30/72

  • 7/27/2019 COMP3 New Revision Guide Updated

    31/72

    Storing binary trees

    Given that any node on a binary tree only has left or right leaf connected to it,

    we can use a standardised data type to represent each node in a binary tree. All

    we need to achieve this is to write a class definition like so (written in Python):

    classNodeType:

    __int__(left, item, right):

    LeftPointer = left

    Item = item

    RightPointer = right

    We can then create a list of objects (or indeed a list of records) where the index

    corresponds to the index of that node.

    31

    *

    A C

    /

    E G

    +

    Pre-order Traversal

    Visit the root

    Traverse the left sub-tree in pre-order

    Traverse the right sub-tree in pre-

    order

    1

    2

    3 4

    5

    6 7

    Our recursive definition tells us that we

    should always pick up the value of the root

    of a sub-tree before we explore that sub-

    tree. We then always go to the left first.

    *

    A C

    /

    E G

    +

    7

    3

    1 2

    6

    4 5

    Post-order Traversal

    Traverse the left sub-tree in post-

    order

    Traverse the right sub-tree in post-

    order

    Visit the rootThis is effectively the same as pre-order but

    flipped. But remember, the left is always

    visited before the right.

    In-order Traversal

    Traverse the left sub-tree in in-order

    Visit the root

    Traverse the right sub-tree in in-order*

    A C

    /

    E G

    +

    4

    2

    1 3

    6

    5 7

    For in-order traversal, the values from theleaves are always separated by the values

    from the nodes.

    Result: +*AC/EG

    Result: AC*EG/+

    Result: A*C+E/G

  • 7/27/2019 COMP3 New Revision Guide Updated

    32/72

    Above is an abstraction of a tree to show the information that we have to

    represent and store. You will notice that each leaf has a left and right pointer

    with a value of 0. This is a null pointer since there is nothing to the left or right of

    these leaves.

    Using this method of storing the tree, it is then possible to apply any of the

    aforementioned algorithms in order to traverse the tree.3.2.8 Searching and sortingLinear search - this search method starts at the beginning of the list and

    compares each element in turn with the required value until a match is found or

    the end of the list is reached.

    Bubble sort - during a pass through the list, neighbouring values are compareand swapped if they are not in the correct order. Several passes are made until

    one ass does not re uire an further swa s.

    Bubble sort

    Bubble sort was covered in the AS specification but it is important to remember

    exactly how it works and to recognise the complexity of this sorting algorithm.

    First, we should consider a list which has been sorted into reverse alphabetical

    order. This was actually a mistake and we would now like to take this sorted list

    and re-sort it into alphabetical order. This is an example of the worst case

    scenario for the bubble sort.

    32

    2 * 3

    1

    4 + 5

    2

    0 z 0

    4

    0 6 0

    5

    6 - 7

    3

    0 y 0

    6

    0 3 0

    7

    Index

    Item

    LeftPoint

    er RightPoint

    er

    Null

    pointer

    12*324+536-740 z0506060y070301Root node

    pointer

    *

  • 7/27/2019 COMP3 New Revision Guide Updated

    33/72

    Zebra

    Yak Tiger Snake Sloth Mouse

    Lion Cow Bird Ant

    Pass#(Swaps)1 (9) Yak Tiger Snake Sloth Mous

    eLion Cow Bird An

    tZebra

    2 (8) Tiger Snake

    Sloth Mouse

    Lion Cow Bird Ant Yak

    Zebra

    3 (7) Snake

    Sloth Mouse Lion Cow Bird Ant Tiger

    Yak

    Zebra

    4 (6) Sloth Mous

    e

    Lion Cow Bird Ant Snake Tige

    r

    Ya

    k

    Zebr

    a5 (5) Mouse

    Lion Cow Bird Ant Sloth

    Snake Tiger

    Yak

    Zebra

    6 (4) Lion Cow Bird Ant Mouse

    Sloth

    Snake Tiger

    Yak

    Zebra

    7 (3) Cow Bird Ant Lion Mouse

    Sloth

    Snake Tiger

    Yak

    Zebra

    8 (2) Bird Ant Cow Lion Mouse

    Sloth

    Snake Tiger

    Yak

    Zebra

    9 (1) Ant Bird Cow Lion Mouse

    Sloth

    Snake Tiger

    Yak

    Zebra

    10

    (0)

    Ant Bird Cow Lion Mous

    e

    Slot

    h

    Snake Tige

    r

    Ya

    k

    Zebr

    a

    Here we have the bubble sort results for each pass of the algorithm. Since in

    each instance the first item is always higher in alphabetical value than all the

    others, it will feature as part of every comparison in each pass. Since we can

    have up to n - 1 swaps in up to n - 1 passes (the last pass just ensures that there

    will be no more swaps) we can say that the bubble sort has O(n2).

    Searching

    Linear search is the most straightforward of search algorithms. Given a list of

    length n, each item will be looked at from the start of the list until the desireditem is either found, or the end of the list is reached. In the worst case scenario,

    we may have to look at all n items therefore the algorithm is of O(n).

    If we have a sorted list we can use the more efficient binary search. In binary

    search, we look at the middle term of the list and compare it with the item that

    we are looking for. We then reject one half of the list based on whether the item

    we are looking for would be higher or lower in the list.

    Below we are searching for Dave in the list.

    33

  • 7/27/2019 COMP3 New Revision Guide Updated

    34/72

    This is an example of the worst case scenario for a binary search since the item

    is in fact not in the list. We have made 4 comparisons for a 10 item list, this

    makes the order of complexity for the binary search O(log2n).

    Insertion sort

    For an insertion sort algorithm we divide a list into a sorted part and an unsorted

    part. We take the first item in the list that we want to sort and insert it into our

    sorted part. We then compare each item in the unsorted part to the items in the

    sorted part and insert them where they fit, rearranging the other items as

    appropriate.

    Quicksort

    In a quicksort we split a list into two sub-lists with a "pivot" acting as the value

    that all items within that sub-list are compared to. By continually splitting the listin two into smaller sub-lists and selecting the middle value as the pivot, we are

    able to sort an entire list.

    54 23 15 74 19 22 14 3 11 64 27 35

    Above is a list of 12 items. We should choose the 7 th item of the list as our pivot

    and compare each value either side. We want to sort this list into ascending

    numerical order so we should place all the numbers of lesser value to the left

    and all the numbers of greater value to the right. Make sure not to reorder any of

    the values.

    34

    1Ant2Bird3Cow4Li

    on5Mouse6Sloth7

    Snake8Tiger9Yak1

    0Zebra

    1Ant2Bird3Cow4Li

    on5Mouse

    4Lion5Mouse

    4Lion

    (10 + 1)/2 = 5.5 therefore we should look

    at the 6th item in the list. Sloth >

    Dave so we reject items 6-10.

    (1+5)/2 = 3 therefore we should

    look at the 3rd item. Cow

    Dave so we reject item 5.

    Only item 4 remains. However,

    Lion Dave therefore Dave is

    not in the list.

    1Snake2Yak3Li

    on4Zebr

    a5Tiger

    6Ant7Sl

    oth

    1Snake2Yak3Li

    on4Zebr

    a5Tiger

    6Ant7Sl

    oth

    1Lion2Snake3Y

    ak4Zebr

    a5Tiger

    6Ant7Sl

    oth

    1Lion2Snake3Y

    ak4Zebr

    a5Tiger

    6Ant7Sl

    oth

    1Lion2Snake3Ti

    ger

    4Yak5Z

    ebra6An

    t7Sloth

    1Ant2Lion3Sna

    ke4Tige

    r

    5Yak6Z

    ebra7Sl

    oth

    1Ant2Lion3Slot

    h4Snak

    e5Tiger

    6Yak7Z

    ebra

  • 7/27/2019 COMP3 New Revision Guide Updated

    35/72

    3 11 14 54 23 15 74 19 22 64 27 35

    We then select new pivots in the two sub-lists that have been created by splitting

    about our initial pivot.

    3 11 14 15 19 54 23 74 22 64 27 35

    3 11 14 15 19 22 54 23 74 64 27 35

    3 11 14 15 19 22 54 23 27 35 64 74

    3 11 14 15 19 22 23 27 54 35 64 74

    3 11 14 15 19 22 23 27 35 54 64 74

    3 11 14 15 19 22 23 27 35 54 64 74

    Only once all the items have themselves been pivots can we conclusively say

    that the list has been sorted into a correct order. The quicksort algorithm is an

    example of recursive programming at its best. However, its worst case scenario

    is a complexity of O(n2). This makes it seem as if quicksort is only as efficient as

    bubble sort. Nevertheless, quicksort has a much better average case complexity.

    3.2.9 SimulationsModel - an abstraction of an entity in a real world or in the problem that enables

    an automated solution. The abstraction is a representation of the problem that

    leaves out unnecessary detail.State history - consists of state descriptions at each of a chronological

    succession of instants.

    Entities - the components that make up a system.

    Attributes - a property of an object, e.g. an object car has attributes make,

    Simulation is the imitation of a process of a real system. An example of the

    purpose of a simulation comes in the form of trying to understand the effect of a

    new supermarket being built, which means a reconfiguration of the current road.

    Although many of the proposed designs will never be realised, they represent

    the simulated effects of if they were to be carried out.

    Consider a queue. A queue consists of three entities:

    Customer

    Queue

    Server

    The customer could be waiting in the queue -- this is therefore one of its

    attributes. The state of a system at any instant is determined by where the

    entities are, what they are doing and their attributes. A succession of recorded

    new identifiable states results in a state history.

    Some possible states of a queue are:

    35

    *

  • 7/27/2019 COMP3 New Revision Guide Updated

    36/72

    nobody in the queue, server waiting

    nobody in the queue, a customer being served

    customers in the queue, a customer being served.

    Some possible events in this system are:

    a customer arriving end of serving.

    Some possible activities are:

    the serving of a customer

    the time between customers arriving.

    If we consider a queue system where a customer arrives at the end of every 3

    minutes and serving takes 2 minutes, running a simulation for 20 minutes using

    time-driven simulation will give the following hand simulation.

    Masterclock

    Customerarriving

    Customerbeingserved

    Customersin queue

    Minutes inqueue for allcustomer

    Serverstatus

    1 - - 0 - Idle2 - - 0 - Idle3 Customer1 - 0 0 Idle4 Customer1 0 0 Serving5 Customer1 0 0 Serving6 Customer2 Customer1 1 0 Serving7 Customer1 1 1 Serving

    8 Customer2 0 1 Serving9 Customer3 Customer2 1 1 Serving10 Customer2 1 2 Serving11 Customer2 1 3 Serving12 Customer4 Customer3 1 3 Serving13 Customer3 1 4 Serving14 Customer3 1 5 Serving15 Customer5 Customer3 2 6 Serving16 Customer4 1 7 Serving17 Customer4 1 8 Serving18 Customer6 Customer4 2 9 Serving19 Customer4 2 11 Serving20 Customer5 1 12 Serving

    Each state represents the end of a minute. Since a customer only arrives at the

    end of every three minutes, the first three ticks of the master clock see no

    activity besides the server being at an idle state.

    3.3 REAL NUMBERS

    3.3.1 Real numbers

    36

  • 7/27/2019 COMP3 New Revision Guide Updated

    37/72

    Real number - a number with a fractional part.

    Significant digits - those digits that carry meaning contributing to the accuracy

    of a number. This includes all digits except leading and trailing zeroes where they

    serve merely as placeholders to indicate the scale of the number.

    Floating-point notation - a real number represented by a sign, some

    significant digits, and a power of 2.Precision - the maximum number of significant digits that can be represented.

    Absolute error - the difference between the actual number and the nearest

    representable number.

    Relative error - the absolute error divide by the actual number.

    Underflow - the value is too small to be represented using the available number

    of bits.

    Overflow - the value is too large to be represented using the available number

    In the AS specification, we dealt only with fixed point numbers. This meant that

    there was a fixed way of representing a real number with a decimal point after a

    certain number of bits.

    In floating-point notation, real numbers are represented in the following

    way: a sign, some significant digits expressed as a number with a fractional

    part, and an integer power of two.

    Some examples are: 4.6 x 26, -3.12 x 25, 6.2 x 2-3. This gives us a general

    form ofm x 2e where the significant digits are called the mantissa (m) and

    the power of 2 is called the exponent (e).

    In the exam, you will have to convert from a normalised twos complementfloating-point number to its denary real number equivalent and vice versa.

    In the following format we have a number of size 16 bits with the 10 most

    significant bits reserved for the mantissa and 6 bits reserved for the mantissa.

    0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0

    This represents the smallest positive normalised value. It is normalised since the

    first two bits are of opposite polarity i.e. 01 or 10. The exponent value = -32.

    This therefore makes adjusted mantissa

    0.0000000000000000000000000000000012 = 2-33.

    37

    *

  • 7/27/2019 COMP3 New Revision Guide Updated

    38/72

    Why normalise?

    Maximises precision and accuracy for a given number of bits.

    Creates a unique representation for every number (allowing equality to be

    checked more simply).

    The length of the mantissa increases the precision.

    The length of the exponent increases the range.

    Underflow and overflow Dividing a very small number by a non-decimal number may make a value

    too small for it to be represented by a given number of bits. In this case, it

    will be stored as zero. This is known as underflow.

    Multiplying two large numbers together may make a value too large for it to

    be represented by a given number of bits. This is known as overflow.

    3.4 OPERATING SYSTEMS

    3.4.1 Role of an operating systemSystem program - a program that manages the operation of a computer.

    Operating system - the software that supports a computer's basic functions,

    such as scheduling tasks, executing applications, and controlling peripherals.

    Virtual machine - the apparent machine that the operating system presents to

    the user, achieved by hiding the complexities of the hardware behind layers of

    operating system software.

    Application programming interface - a layer of software that allows

    application programs to call on the services of the operating system.

    38

    Worked example

    Using the above format of normalised floating point representation, convert

    -23.375 into binary.

    First, assess the number. We need a -32 which is equal to -25 since it is the

    nearest larger power of 2 to 23. Then 32 - 23.625 = 8.625, so we can break-23.375 down to -32 + 8 + + 1/8 = -25 + 23 + 2-1 + 2-3 = 101000.1012.

    1010001010

    Now that weve filled in our mantissa, we need to calculate the exponent.

    Our implied decimal point is 7 places to the left of our desired decimal

    place therefore our exponent should be 7 = 4 + 2 + 1 = 0001112.

    101000101000011 1

    *

  • 7/27/2019 COMP3 New Revision Guide Updated

    39/72

    Computer software can be divided into system programs, which manage the

    operation of the computer, and application programs, which solve problems for

    their users. The most fundamental of all the system programs is the operating

    system. An operating system has these roles:

    Hide the complexities of the hardware from the user.

    Manage the hardware resources to give orderly and controlled allocation of

    the processors, memories and input/ouput (I/O) devices among the various

    programs competing for them, and manage data and storage.

    Managing resources

    In a general-purpose computer, one purpose of an operating system is to

    manage the hardware so that a satisfactor performance is achieved. The

    operating system programs may be classified according to the resources they

    manage.

    Key resource OS programProcessors Processor schedulingStorage Memory managementInput/output devices I/O managementData File management

    The following are examples of the functions that an operating system has to be

    able to do:

    Allocating a processor 'time slot ' for each programming task that is

    running.

    Managing the priorities for each program task that is running. Allocating and keeping track of the memory used for storing programs and

    data.

    Managing the transfer of databetween memory and storage.

    Handling input operations from the user and from other input devices.

    Handling output operations.

    Managing the system security .

    39

  • 7/27/2019 COMP3 New Revision Guide Updated

    40/72

    Virtual machine

    An operating system hides from the user all the details of how the

    hardware works so that the useris presented with a virtual

    machine which is easier to use. These details are progressively hidden

    by placing layers of software on top of the hardware.

    Application programming interface

    A standard application programming interface (API) allows

    a software developer to write an application on one computer and

    have a high degree of confidence to that it will run on another computer of the

    same type, even if the other computer has a different specification.

    User interface

    Command line interface

    In a command line interface (CLI), a user responds to a prompt to enter

    commands by typing a single command word, followed by zero or moreparamaters on a single line, before pressing the enter key. An example of such a

    command is ipconfig.

    Graphical user interface

    A graphical user interface (GUI) is made up of windows. One window has the

    focus at any moment.

    GUIs are event-driven with events being mouse button clicks, key presses or

    mouse movements.

    The operating system detects an even and correlates it with the current

    mouse position and the window currently in focus, in order to select anaction to carry out.

    40

    Userinterface

    Applicationprogramming

    interfaceI/O management

    File managementMemory management

    Processor managementDevice Drivers

    KernelHardware

    More

    complex

    layers.

  • 7/27/2019 COMP3 New Revision Guide Updated

    41/72

    3.4.2 Operating system classificationNote: The notes on this section contain a lot of detail, mainly familiarise yourself

    with the key terms.

    Interactive operating system - an operating system in which the user and the

    computer are in direct two-way communication.

    Real-time operating system - inputs are processed in a timely manner so that

    the output can affect the source of inputs.

    Network operating system - a layer of software is added to the operating

    system of a computer connected to the network. This layer intercepts commands

    that reference resources elsewhere on the network, e.g. a file server, then

    redirects in a manner completely transparent to the user.

    Sandbox - a tightly controlled set of resources for guest programs to run in.

    Embedded computer system - a dedicated computer system with limited or

    non-existent user interface and designed to operate largely or completely

    autonomously from within other machinery.

    Desktop operating system - an operating system that allows a user to carry

    out a broad range of general-purpose tasks.

    Client-server system - a system in which some computers, the clients, request

    services provided by other computers, the servers.

    Server operating system - an operating system optimised to provide one or

    more specialised services to networked clients.

    Server operating system A server operating system is an operating system optimisedto provide one or

    more specialisedservices to networked clients such as: file storage, domaincontrol, running applications.

    Since they are specialised, their performance is optimal as little general-purpose processing is needed.

    Desktop operating system All desktop computers have operating systems which must support a broad

    range of general-purpose tasks. Examples of operating systems are the Windows family by Microsoft, the

    Macintosh family by Apple and the UNIX/Linux family developed bycollaborators.

    They are very sophisticated since they have to deal with many types ofhardware and software.o Modern PCs have large main memory capacities, multiple processors,

    huge disk storage capacities, various types of optical disks, and flashmemory drives.

    They have real-time requirements for multimedia applications.

    They must support a wide range of network protocols. They are written in a layered or modular fashion so that

    41

    *

  • 7/27/2019 COMP3 New Revision Guide Updated

    42/72

    o they can be updated easily.

    If they are found vulnerable to a security threat, then an update canbe deployed to counter the threat.

    They can support sophisticated GUIs.o However, their memory footprint is very large and load times can be

    significant. They provide a virtual machine which allows the user to perform tasks more

    easily than if they had to interact directly with the hardware.

    Desktop operating systems often act as the client operating system in aclient-server system.o In a client-server system some computers, the clients, request services

    provided by other computers, the servers.

    Embedded computer systems Embedded computer systems are those which are embedded in machinery

    other than PC, such as cars, telephones, appliances and peripherals for

    computer systems. Today's motor cars may have 12 or more embedded computer systems.

    These systemso have a dedicated purposeo have a limited or non-existent user interfaceo are designed to operate completely or largely autonomously within

    other machinery (e.g. an engine management system)o have a limited memory capacity.

    As embedded systems have become more complex and have developedmore features, their applications increasingly require an operating systemto keep the development timereasonable and to manage multiple tasks or

    threads that need to meet specific time constraints.o A low-level piece of code switches between tasks or threads basedon a timer (connected to an interrupt).

    o At this level a system is considered to have an operating systemkernel.

    Any code can potentially damage the data of another task. Thereforeo programs must be carefully designed and tested.o access to shared data must be controlled by some synchronisation

    strategy. Operating systems for embedded systems are designed to work with the

    constraints ofo limited memory sizeo limited processor performance

    If the system is portable, the operating system must also take account oflimited battery life.

    In addition to core operating system, many of these systems haveadditional upper-layersoftware components.

    o These consist of networking protocol stacks such as TCP/IP, FTP,HTTP and HTTPS.

    o They also include storage capabilities such as flash memorymanagement systems.

    o If the embedded device has audio and video capabilities, then theappropriate drivers and codecs will be present in the system.

    Device

    42

  • 7/27/2019 COMP3 New Revision Guide Updated

    43/72

    The way a device works can usually be changed by altering the code ofthe operating system.

    o This means that the physical circuits of the device need not berewired when new functionality is required but the code of theoperating system can be rewritten.

    The OS code should be layered or modular with clearinterfaces between these layers. This makes changing theinterface as simple as altering the interface layer or module.

    Not all computer having operating systems.o A washing machine uses an example of such a computer. The input

    of this computer is simple (since all settings are preset), the processto be performed is equally as simple, and it is not necessary for thecomputer to complete more than one task at a time. In this caseincluding an operating system would add complexity where none isrequired which would increase the development and manufacturingcosts. The computer in this case would run a single firmwareprogram all the time.

    Computer-operated devices which have to carry out more than one taskbenefit from these things that an operating system allows:

    o The device can multi-task.o The device can operate in real time with critical timing constraints

    observed, if required.o The hardware can be changedorupgraded without the need to change

    application code that runs on the hardware.o New applications can be added fairly easily.o Changes to basic functionality can be achieved by upgrading operating

    system code that runs on the hardware.o Applications can be developed in situ on the device or can be easily

    installed if developed on a more powerful machine.o The entire OS can be replaced by a different OS where the new OS allows

    a much greater range of software changes to be made.o Open Source operating systems can be used. The source code for an

    Open Source OS is available therefore applications that will work ondevices running this OS can be designed easily.

    Operating systems for mobile devices need to consider the kind of resourcesavailable to these devices. For example,o the amount of energy providedo the amount of memory available

    Smartphones A smartphone is a mobile phone that offers advanced capabilities beyond a

    typical mobile phone, often with PC-like functionality. This means runningcomplete operating system software, which provides a standardisedinterface and platform for application developers.

    Regular mobile phones typical only support sandboxes applications.o A sandbox is a tightly controlled set of resources for guest programs to

    run in, such as scratch space on disk and memory.o Network access and the ability to inspect the host system or read from

    input devices are usually disallowed or heavily restricted in sandboxedsystems.

    43

  • 7/27/2019 COMP3 New Revision Guide Updated

    44/72

    Applications for smartphones may be developed by anyone (including themanufacturer of the device, the network operator or any other third-partysoftware developer) since the operating system is open.

    Personal digital assistants

    A personal digital assistant (PDA) is a hand-held portable computer that canaccomplish quite specific tasks and can take on the role of a personalassistant. PDA functionality has recently been included in smartphonestherefore the sales of PDAs have declined.

    The operating system of a PDA takes on the tasks of basic input/outputsystem (BIOS) and has to be designed to run on processors with low clockfrequency and a main memory of limited capacity.

    The OS must use various techniques to save energy and must cater for shortreaction times.

    Real-time operating system

    In a real-time operating system inputs are processed in a timely manner sothat the output can affect the source of the inputs.

    Real-time operating systems are characterised by four requirements:1. They have to support application programs which are non-sequential in

    nature, i.e. programs which do not have a START-PROCESS-ENDstructure.

    2. They have to deal with a number of events which happen in parallel andat unpredictable moments.

    3. They have to carry out processing and produce a response within aspecific time interval.

    4. Some systems are safety-critical, so they must be fail-safe and guaranteea response within a specified time interval.

    Examples:

    Airline reservation system: up to 1000 messages per second can arrive from

    any one of 11000 to 12000 terminals situation all over the world. The

    response time must be less than 3 seconds.

    Process control system: up to 1000 signals per second can arrive fromsensors attached to the system being controlled. The response time must beless than 0.001 second.

    Real-time operating systems that are used to control machinery typically

    have limited user-interface capability and no end-user utilities. They mustperform a task quickly whenever signalled to do so in a specific amount oftime.

    Some RTOS manage the resources of the computer so that a particularoperation executes in precisely the same amount of time every time itoccurs. In a complex machine, this can be catastrophic and unnecessary.

    Network operating system In a network operating system, a layer of software is added to the operating

    system of a computer connected to the network. This layer interceptscommands that reference resources elsewhere on the network, e.g. a fileserver.

    The network layer then redirects the request to the remote resourcein amanner completely transparent to the user. In this way, files which reside on

    44

  • 7/27/2019 COMP3 New Revision Guide Updated

    45/72

    a server are available to the client computer, exactly as if they resided onthat client computer's system.

    Remove drives (perhaps N: and P: ) are usually available to all clientcomputers connected to a network.

    Exam note: Operating systems are a good basis for an extended answerquestion. Consider the differences between certain types of operating systems. A

    question on the definition of an operating system could equally be a short (2

    mark) answer question or a longer (4-6 mark) answer question.

    45

  • 7/27/2019 COMP3 New Revision Guide Updated

    46/72

    3.5 DATABASES

    3.5.1Conceptual data modellingDatabase - a structured collection of data.Database management system - a software system that enables the

    definition, creation and maintenance of a database and which provides controlled

    access to this database.

    Data model - a method of describing the data, its structure, the way it is

    interrelated and the constraints that apply to it for a given system or

    organisation.

    Conceptual model - a representation of the data requirements of an

    organisation constructed in a way that is independent of any software used to

    construct the database.Entity - an object, person, event or thing of interest to an organisation and about

    which data is recorded.

    Relationship - an association or link because two entities.

    Relationships between entities can be represented using Entity-Relationship

    diagrams. See the example on the next page.

    Here is a scenario which involves a college enrolling students for AS and A2

    courses:

    Each course is assigned a unique course code and has a course name.

    Each student is assigned a unique student ID and has their name, address

    and date of birth recorded.

    Each student enrols on one or more courses.

    The students enrolled on a course will be assigned to one of several sets

    taught by different teachers.

    Teachers are assigned unique initials.

    If we want to model the three entities of "Set", "Student" and "Course", we get

    the below diagram.

    A many-to-many relation is not clear, most of the time such a relation can be

    made a lot clearerby adding a link table.We can analyse the many-to-many

    relationship between the "Student" and "Courses" entities and decide to add an"Enrolment" entity.

    46

    Student

    Set

    Course

    One-to-one

    relationshipMany-to-

    many

    relationship

    belongs to

    is enrolled

    on

    is assigned

    to

    *

  • 7/27/2019 COMP3 New Revision Guide Updated

    47/72

    3.5.2Database designRelation - a set of attributes and tuples, modelling an entity (a table).

    Attribute - a property or characteristic of an entity (a named column in a table).

    Tuple - a set of attribute variables (a row in a table).

    Primary key - an attribute which uniquely identifies a tuple.

    Relational database - a collection of tables.

    Composite key - a combination of attributes that uniquely identify a tuple.

    Foreign key - an attribute in one table that is a primary key in another table.

    Referential integrity - if a value appears in a foreign key in one table, it must

    also appear in the primary key in another table.

    Normalised entities - a set of entities that contain no redundant data.Normalisation - a technique used to produce a set of normalised entities.

    A relation consists of a heading and a body. A heading is a set of attributes. A

    body is a set of tuples. A relation must have an identifier, an attribute that

    uniquely identifies a tuple.

    Normalisation

    The important thing about database design is that the correct attributes are

    groups into the correct tables in order to minimise duplication of data. If there isno duplicated data, the possibility for inconsistencies will be eliminated.

    47

    Student

    Set

    Course

    One-to-one

    relationshipMany-to-

    many

    relationship

    Enrolment

    belongs to

    is enrolledon

    is assigned

    to

    formakes

    Husband WifeOne-to-one

    One husband has one wife

    (conventionally).

    Area ResidentOne-to-many

    One area has many

    residents.

    Newspaper ReaderMany-to-many

    One reader reads many

    newspapers. One newspaper

    has many readers.

    *

  • 7/27/2019 COMP3 New Revision Guide Updated

    48/72

    OnlineOrder(OrderNumber, CustomerID, DeliveryAddress, EmailAddress,

    OrderDate, ItemCode, Description, OrderQuantity, UnitPrice)

    Order

    Number

    Custom

    er ID

    Delivery

    Address

    Email

    Address

    OrderDa

    te

    Ite

    mCode

    Descripti

    on

    Order

    Quantity

    Unit

    Price

    012367

    BLF1 Fred Bloggs1, HighStreetAnytown

    [email protected]

    01/05/09 1234

    Ringbinder

    3 1.50

    8967

    Divider 4 0.50

    3456

    Stapler 1 2.99

    034231

    SMJ2 Joe Smith7, The LaneAnytown

    [email protected]

    03/05/09 9684

    Scissors 2 1.99

    345

    6Stapler 4 2.99

    1NF: atomic data test

    Given a table that has a primary key; it is in first normal form (1NF) if all of the

    data values are atomic values. That is, the table does not contain repeating

    groups of attributes.

    To put a table into first normal form, we move any repeating attributes, with a

    copy of the primary key, to a separate table.

    OnlineOrder(OrderNumber, CustomerID, DeliveryAddress, EmailAddress,

    OrderDate)

    OrderNumber

    Customer ID Delivery Address Email Address OrderDate

    012367 BLF1 Fred Bloggs1, High StreetAnytown

    [email protected]

    01/05/09

    034231 SMJ2 Joe Smith7, The LaneAnytown

    [email protected]

    03/05/09

    ItemOrder(OrderNumber, ItemCode, Description, OrderQuantity, UnitPrice)

    OrderNumber

    Item Code Description OrderQuantity

    Unit Price

    012367 1234 Ring binder 3 1.50012367 8967 Divider 4 0.50012367 3456 Stapler 1 2.99034231 9684 Scissors 2 1.99034231 3456 Stapler 4 2.99

    OrderNumber is now not sufficient to be a primary key since it does not act as a

    unique identifier for a tuple. We therefore have to create a composite

    keyincluding ItemCode.

    48

  • 7/27/2019 COMP3 New Revision Guide Updated

    49/72

    2NF: partial key dependence test

    A table is in second normal form (2NF) if it is in first normal form and contains no

    partial key dependencies. This means that if some parts of the row depend only

    on one half of the composite key, we should move these parts into a new table

    with the relevant primary key.

    ItemOrder(OrderNumber, ItemCode,

    OrderQuantity)

    OrderNumber

    ItemCode

    OrderQuantity

    012367 1234 3012367 8967 4012367 3456 1034231 9684 2034231 3456 4

    Item(ItemCode, Description,

    UnitPrice)

    ItemCode

    Description

    Unit Price

    1234 Ringbinder

    1.50

    8967 Divider 0.503456 Stapler 2.999684 Scissors 1.99

    3NF: non-key dependence test

    A table is in third normal form (3NF) if it is in second normal form and contains

    no non-key dependencies.

    OnlineOrder(OrderNumber, CustomerID, OrderDate)

    Order Number Customer ID OrderDate012367 BLF1 01/05/09034231 SMJ2 03/05/09

    Customer(CustomerID, DeliveryAddress, EmailAddress)

    By splitting the original OnlineOrder table into two tables containing only the

    required information, we have successfully created a fully normalised database.A database is fully normalised if every attribute is a fact about the key, the whole

    key, and nothing but the key (so help me Codd).

    49

    CustomerID is a foreign key.

    Customer ID Delivery Address Email AddressBLF1 Fred Bloggs

    1, High Street

    Anytown

    [email protected]

    SMJ2 Joe Smith

    7, The Lane

    Anytown

    JoeSmith @NT.co.uk

  • 7/27/2019 COMP3 New Revision Guide Updated

    50/72

    3.5.2 Structured Query Language (SQL)

    DDL

    DDL is used to create a database structure; that is, to define which attributesbelong in which tables. It also allows you to create users and grant access rights

    to users. For the exam, you will only need to know how to create tables using

    DDL.

    The following notation is used: e.g. For Item table.

    CREATE TABLE (

    ,,

    )

    CREATE TABLE Item(

    ItemCode INTEGER PRIMARYKEY,

    Description TEXT,

    UnitPrice FLOAT)

    Note that the end of every line, excluding the final line, is followed by a comma.

    You must specify which field is the primary key and which type each field is.

    There are many types which would be appropriate.

    Field type PurposeINTEGER or INT Stores integer types.

    TEXT or VARCHAR Stores text strings.FLOAT