AL Notes by MAK
AL Notes by MAK
Key terms
User-defined data type – a data type based on an existing data type or other data types that
have been defined by a programmer.
Non-composite data type – a data type that does not reference any other data types.
Enumerated data type – a non-composite data type defined by a given list of all possible
values that has an implied order.
Pointer data type – a non-composite data type that uses the memory address of where the data
is stored.
Set – a given list of unordered elements that can use set theory operations such as intersection
and union.
Key terms
Serial file organisation – a method of file organisation in which records of data are physically
stored in a file, one after another, in the order they were added to the file.
Sequential file organisation – a method of file organisation in which records of data are
physically stored in a file, one after another, in a given order.
Random file organisation – a method of file organisation in which records of data are
physically stored in a file in any available position; the location of any record in the file is
found by using a hashing algorithm on the key field of a record.
Hashing algorithm (file access) – a mathematical formula used to perform a calculation on
the key field of the record; the result of the calculation gives the address where the record
should be found.
File access – the method used to physically find a record in the file.
Sequential access – a method of file access in which records are searched one after another
from the physical start of the file until the required record is found.
Direct access – a method of file access in which a record can be physically found in a file
without physically reading other records.
3.1.1 User-defined data types
Composite
Set
Record
Class/Object
Non-composite
Enumerated (enum)
Pointers
Non-composite data types
If you are using lots of constants in your program that are all related to each other, then
it is a good idea to keep them together using a structure called an “Enum”. For example,
you might want to store the names of each set of cards in a deck.
This allows you to set meaningful names to the enum and its members, making it
much easier to remember as well as making the code easier to read.
Pointers
Pointer data type
A pointer data type is used to reference a memory location. It may be used to construct
dynamically varying data structures
A pointer is a variable that represents the location of a particular data item within a
specified domain (such as the hard drive, or an array). Within the computer’s memory,
every stored data item occupies one or more contiguous memory cell/s. The number of
memory cells needed to store a data item depends on the data type of that item.
To understand pointers, you must first understand how data is stored in the computer’s
memory
In typical computer architecture of the memory, each byte of the memory has a unique
address. Let us assume the first byte has an address of “201”. Then the next address will
be “202” and we’ll go on “203”,”204” and so on…
Now when we initialize a variable, the computer allocates some amount of memory
corresponding to this particular variable depending on the data type of the variable.
So for example ‘a’ as type integer that means the computer allocates 4 bytes of
memory, we can allocate memory from ‘204’ to ‘207’
NULL Pointers
It is always a good practice to assign a NULL value to a pointer variable in
case you do not have an exact address to be assigned. This is done at the
time of variable declaration. A pointer that is assigned NULL is called
a nullpointer.
Set
It is a composite data type that can store data in any particular order, in which
each element is indexed. Sets cannot have data items repeated more than once
TYPE
TEmployeeRecord
DECLARE EmployeeFirstName : STRING
DECLARE EmployeeFamilyName : STRING
DECLARE DateEmployed : DATE
DECLARE Salary : CURRENCY
End TYPE
Attributes
Methods
13.2 File organisation and access
Candidates should be able to: Notes and guidance
Show understanding of the methods of file Including serial, sequential (using a key field), random
organisation and select an appropriate method of file (using a record key)
organisation and file access for a given problem
Show understanding of methods of file access Including
Sequential access for serial and sequential files
Direct access for sequential and random files
Show understanding of hashing algorithms Describe and use different hashing algorithms to read
from and write data to a random / sequential file
File organization
Serial files
A serial file contains records which have no defined order. A typical use of a serial file would be for a bank to
record transactions involving customer accounts. A program would be running. Each time there was a withdrawal
or a deposit the program would receive the details as data input and would record these in a transaction file. The
records would enter the file in chronological order but otherwise the file would have no ordering of the
records.
A text file can be considered to be a type of serial file but it is different because the file has repeating lines which
are defined by an end-of-line character or characters. There is no end of-record character. A record in a serial file
must have a defined format to allow data to be input and output correctly.
Sequential files
A sequential file has records that are ordered. It is the type of file suited to long-term storage of data. As such it
should be the type of file that is considered as an alternative to a database. In order to allow the sequential file to
be ordered there has to be a key field for which the values are unique and sequential but not necessarily
consecutive. It is worth emphasizing the difference between key fields and primary keys in a database table,
where the values are required to be unique but not to be sequential. In a sequential file, a particular record is
found by sequentially reading the value of the key field until the required value is found.
Direct-access files
Direct-access files are sometimes referred to as 'random-access' files but, as with random access memory, the
randomness is only that the access is not defined by a sequential reading of the file. For large files, direct access
is attractive because of the time that would be taken to search through a sequential file. In an ideal scenario, data
in a direct-access file would be stored in an identifiable record which could be located immediately when
required. Unfortunately, this is not possible. Instead, data is stored in an identifiable record but finding it may
involve an initial direct access to a nearby record followed by a limited serial search
The choice of the position chosen for a record must be calculated using data in the record so that the same
calculation can be carried out when subsequently there is a search for the data. The normal method is to use a
hashing algorithm. This takes as input the value for the key field and outputs a value for the position of the record
relative to the start of the file. The hashing algorithm must take into account the potential maximum length of the
file, that is, the number of records the file will store. A simple example of a hashing algorithm, if the key field has
a numeric value, is to divide the value by a suitably large number and use the remainder from the division to
define the position. This method will not create unique positions. If a hash position is calculated that duplicates
one already calculated by a different key, the next position in the file is used. This is why a search will involve a
direct access possibly followed by a limited serial search.
Sequential access
.?GGGGGGG6 ...
1 2 3 4 5 6 7 8
Random access
1 3 7 2 8 6 4 5
◄,�
M.FU�
Muhammad Ali Khan �
M.FI.K
O/A Computer Science Muhammad Ali Khan
O/A Computer Science
Uses of storing data In Files
CHAPTER : INTERNET COMMUNICATIONS
Diagram 2
1. The message would start at the top of the protocol stack on your computer and
work it's way downward.
2. If the message to be sent is long, each stack layer that the message passes
through may break the message up into smaller chunks of data. This is because
data sent over the Internet (and most computer networks) are sent in
manageable chunks. On the Internet, these chunks of data are known
as packets.
3. The packets would go through the Application Layer and continue to the TCP
layer. Each packet is assigned a port number. Ports will be explained later, but
suffice to say that many programs may be using the TCP/IP stack and sending
messages. We need to know which program on the destination computer needs to
receive the message because it will be listening on a specific port.
4. After going through the TCP layer, the packets proceed to the IP layer. This is
where each packet receives it's destination address, 5.6.7.8.
5. Now that our message packets have a port number and an IP address, they are
ready to be sent over the Internet. The hardware layer takes care of turning our
packets containing the alphabetic text of our message into electronic signals and
transmitting them over the phone line.
6. On the other end of the phone line your ISP has a direct connection to the
Internet. The ISPs router examines the destination address in each packet and
determines where to send it. Often, the packet's next stop is another router. More
on routers and Internet infrastructure later.
7. Eventually, the packets reach computer 5.6.7.8. Here, the packets start at the
bottom of the destination computer's TCP/IP stack and work upwards.
8. As the packets go upwards through the stack, all routing data that the sending
computer's stack added (such as IP address and port number) is stripped from
the packets.
9. When the data reaches the top of the stack, the packets have been re-assembled
into their original form, "Hello computer 5.6.7.8!"
Protocol: a set of rules for data transmission which are agreed by sender and receiver
Protocols are essential for successful transmission of data over a network. Each protocol
Defines a set of rules that must be agreed between sender and receiver. At the simplest level,
a protocol could define that a positive voltage represents a bit with value 1. At the other
Extreme, a protocol could define the format of the first 40 bytes in a packet
Word/phrase Meaning
Internet Protocol (IP) A protocol that specifies the size of the
packets that a message is broken down into,
and the address of the recipient device
Transmission Control Protocol (TCP) A protocol in the TCP/IP suite that handles the
transfer of data from one computer to another
User Datagram Protocol (UDP) A protocol in the TCP/IP suite that broadcasts
data across a network
What is a protocol?
Modern computers are designed with communication in mind. At a basic level, computers
may need to communicate with peripherals such as a printer, or to send messages to other
computers, which may be connected across a local area network (such as in a business, or
a school), a mobile phone cellular network, or the internet.
Before one computer can communicate with another, there must be a set of agreed rules in
place that manage how the communication is to take place. These rules are known
collectively as a protocol.
To illustrate the need for rules when two or more objects communicate, we will look at some
rules used when humans talk to each other. For example, we:
The protocols allow messages to be transmitted in a structured, specific manner, so that the
receiving device is able to process the data sent from the sending device. Protocols
therefore make sure that communication between any two devices is successful. Different
communication protocols exist to specify rules for different types of communication, for
example:
– handles transmission of data to and from a
website
– handles email transmission
– handles transmission of files across a network
– handles communication over the internet.
Layering and the OSI model
Many types of computer – for example, personal computers, laptops, servers, tablets and
smartphones – exist, and they are made by many different manufacturers. As a result, it is
important to have standards in place that designers and equipment manufacturers can follow
to make sure their products can communicate with others’.
The Open Systems Interconnection model (OSI model) was introduced to form standards for
computer communication. The model takes the various elements of communication and
conceptually separates them into layers. Each layer can be treated individually, and is
independent from the others. Each layer contains different protocols and handles a different
part of the transmission.
The model specifies seven layers: Layer Description
Application Layer The interface between applications and the
network. Data/messages from applications
are passed down to / received from the
Presentation Layer.
above the Physical Layer. This makes it easier for a manufacturer to introduce a new
transmission medium.
protocols specific to that equipment (e.g. for a new router, testing would be needed at the
Network Layer, but not the Transport Layer).
metimes updated, and as the layers are separate and independent, the rules
within them can be changed without affecting the rules contained in the other layers, as long as
the interface to the layers above and below is kept the same.
s are more easily traced, as the type of error is often specific to a layer. A
tester only has to investigate that layer to find the cause of the problem.
TCP/IP
The development and growth of the internet required specific protocols for internet
communication. Two protocols – Internet Protocol (IP) and Transmission Control Protocol (TCP)
– were developed as a result. Known collectively as the TCP/IP suite, each protocol is
responsible for different aspects of communication:
Internet communication uses ‘packet switching’ to handle data transfers from the sending device
to the receiving device – that is, transferred data, such as a downloaded file, is not sent across
the network in a single message, but is broken down into ‘packets’ of smaller pieces of data. IP
specifies the size of the packets that the message is broken down into, and the address of the
receiving device.
IP addresses
The Internet Protocol version 4 (IPv4) uses 32-bit numbers to specify internet addresses. These
addresses are broken down into four numbers in the range 0 to 255, and they are separated by
a dot. For example:
192.168.0.1
Each number is converted to 8 bits:
192 . 168 . 0 . 1
11000000 . 10101000 . 00000000 . 00000001
The four 8-bit numbers form one 32-bit number:
11000000101010000000000000000001
However, using 32 bits limits the total possible number of internet addresses to 4,294,967,296
(232) addresses (4.3 billion). Although this appears to be a very large number, the huge number
of computer devices currently running in the world means that the number of free addresses is
running out. As a result, a new version of the protocol, IPv6, using 128 bits, is now also in use.
This gives 2128 possible addresses!
– the ‘end-to-
end’ communication. It establishes a connection, then makes sure the packets are sent and
received correctly and in the right order. If a packet does not arrive, TCP re-sends the packet.
TCP differs from some other networking protocols in that it does not broadcast data across a
network – instead it is used for one-to-one communication between two devices. Another
protocol, the User Datagram Protocol (UDP) is used for broadcasting data. Unlike TCP, UDP
does not guarantee the packet will safely arrive.
The TCP/IP layers handle similar
communication rules compared to their OSI
model equivalents:
Layer Description
Application Layer The interface between applications (e.g. file
transfer, e-mail, web browsing) and the
network. Data/messages from applications
are passed to / received from the Transport
Layer.
FTP
File transfer Protocol (FTP) is a standard network protocol used to copy a file from one host to
another over a TCP/IP-based network, such as The Internet. By copying from one host to another,
you could say for example uploading a website with pictures from your PC to your website will
require use of FTP. Or the transfer of files over Skype will require FTP. FTP works on port 21
HTTPU
The Hypertext Transfer Protocol (HTTP) is a networking protocol behind the World Wide Web. It
allows for users on the web to exchange information found on web pages. HTTP works on ports 80
& 8080
Email protocols
The traditional method of sending and receiving emails is schematically illustrated in Figure
17.07. It can be seen that three individual client-server interactions are involved. The client
has a connection to a mail server which then has to function as a client in the transmission to the mail
server used by the receiver.
SMTP (Simple Mail Transfer Protocol) is a 'push' protocol. POP3 (Post Office Protocol ve rsion
3) is a 'pull' protocol. There is a more recent alternative to POP3, which is IMAP (Internet
Message Access Protocol). IMAP offers the same facilities as POP3 but also a lot more.
This approach has been largely superseded by the use of web-based mail. A browser is used
to access the email application, so HTTP is now the protocol used. However, SMTP remains in
use for transfer between the mail servers.
TCP is responsible for breaking data down into small packets before they can be sent over a
network, and for assembling the packets again when they arrive.
IP – Internet Protocol
IP takes care of the communication between computers. It is responsible for addressing, sending
and receiving the data packets over the Internet.
Communication between computers on a network is done through protocol suits. The most widely
used and available suite is TCP/IP. A protocol suit consists of a layered architecture where each
layer depicts some functionality which can be carried out by a protocol.
Each layer usually has more than one protocol option to carry out the responsibility that the layer
adheres to. TCP/IP is normally considered to be a 4 layer system. The 4 layers are as follows:
1. Application layer
2. Transport layer
3. Network layer
4. Data link layer
ApplicationU Layer:U
This is the top layer of TCP/IP. It includes applications or processes that use transport layer protocols
to deliver the data to destination computers.
As mentioned above, at each layer there are certain protocol options to carry out the task
designated to that particular layer. So, application layer also has various protocols that applications
use to communicate with the transport layer. Some of the popular application layer protocols are:
Transport Layer:
This layer provides backbone to data flow between two hosts. This layer receives data from the
application layer above it. There are many protocols that work at this layer but the 2 most
commonly used protocols here are TCP and UDP
TCP is used where a reliable connection is required while UDP is used in case of unreliable
connections.
TCP divides the data (coming from the application layer) into proper sized chunks and then passes
these chunks onto the network. It acknowledges received packets, waits for the acknowledgment
of sent packets and sets timeout to resend the packets if acknowledgements are not received in
time. The term ‘reliable connection’ is used where it is not desired to lose any information that is
being transferred over the network through this connection. So, the protocol used for this type of
connection must provide the mechanism to achieve this desired characteristic. For example, while
downloading a file, it is not desired to lose any information (bytes) as it may lead to corruption of
the downloaded file.
UDP provides a comparatively simpler but unreliable service by sending packets from one host to
another. UDP does not take any extra measurements to ensure that the data sent is received by the
target host or not. The term ‘unreliable connection’ is used where minor data loss does not hamper
the task being fulfilled through this connection. For example while streaming a video; loss of few
bytes of information is acceptable as this does not harm the user experience much.
Network Layer:
This layer is also known as the Internet Layer. The main purpose of this layer is to organize or handle
the movement of data on the network. By movement of data, we generally mean routing of data
over the network. The main protocol used at this layer is IP. While ICMP and IGMP are also used at
this layer.
This layer is also known as the Network Interface Layer. This layer normally consists of device
drivers in the OS and the network interface card attached to the system. Both the device drivers
and the network interface card take cake of the communication details with the media being used
to transfer the data over the network. In most of the cases, this media is in the form of cables. Some
of the famous protocols that are used at this layer include ARP (Address resolution protocol) and
PPP (Point to point protocol) etc
In a nutshell, a peer-to-peer (P2P) network is created when 2 or more PCs are connected and share
resources without going through a separate server computer. A P2P network can be an ad hoc
connection or a P2P network can be a network on a much greater scale in which applications set up
direct relationships among users over the internet. Most P2P programs are focused on media
sharing and hence P2P is often associated with software privacy and copyright violation. A famous
example of P2P application is BitTorrent and so we are going to discuss how BitTorrent actually
carries out
You open a Web page and click on a link for the file you want.
BitTorrent client software communicates with a tracker to find other computers running
BitTorrent that have the complete file (seed computers) and those with a portion of the file
(peers that are usually in the process of downloading the file).
The tracker identifies the swarm, which is the connected computers that have all of or a portion
of the file and are in the process of sending or receiving it.
The tracker helps the client software trade pieces of the file you want with other computers in
the swarm. Your computer receives multiple pieces of the file simultaneously.
If you continue to run the BitTorrent client software after your download is complete, others
can receive .torrent files from your computer; your future download rates improve because you
are ranked higher in the "tit-for-tat" system.
Downloading pieces of the file at the same time helps solve a common problem with other peer-to-peer
download methods: Peers upload at a much slower rate than they download. By downloading multiple
pieces at the same time, the overall speed is greatly improved. The more computers involved in the
swarm, the faster the file transfer occurs because there are more sources of each piece of the file. For
this reason, BitTorrent is especially useful for large, popular files.
Quiz
1. A protocol is:
a. a message sent from one computer to another
b. a program used to send messages between computers
c. a set of rules governing communication between computers
In this networking method, a connection called a circuit is set up between two devices, which
is used for the whole communication. Information about the nature of the circuit is maintained
by the network. The circuit may either be a fixed one that is always present, or it may be a
circuit that is created on an as-needed basis. Even if many potential paths through intermediate
devices may exist between the two devices communicating, only one will be used for any given
dialog. As illustrated below
Packet switching steps:
When you send an e-mail to a friend on the other side of the country, how does the message
know to end up on your friend’s computer rather than on one of the millions of other computers
in the world? Much of the work to get a message from one computer to another is done by
routers, because they’re the crucial devices that let messages flow between networks, rather
than within networks.
Imagine a small company that makes animated 3-D graphics for local television stations. There
are 10 employees of the company, each with a computer. 4 of the employees are animators,
while the other 6 are in sales, accounting and management. The animators will need to send
lots of large files back and forth to one another as they work on projects. To do this, they'll use
a network.
Each information packet sent from a computer is seen by all the other computers on the local
network. Each computer then examines the packet and decides whether it was meant for its
address. This keeps the basic plan of the network simple, but has performance consequences
as the size of the network or level of network activity increases. To keep the animators' work
from interfering with that of the folks in the front office, the company sets up two separate
networks, one for the animators and one for the rest of the company. A router links the two
networks and connects both networks to the Internet. Data travels across the internet in
packets. Each packet can carry a maximum of 1,500 bytes. Around these
packets is a wrapper with a header and a footer. The information contained in
the wrapper tells computers what kind of data is in the packet, how it fits
together with other data, where the data came from and the data's final
destination.
When you send an e-mail to someone, the message breaks up into packets
that travel across the network. Different packets from the same message don't
have to follow the same path. That's part of what makes the Internet so robust
and fast. Packets will travel from one machine to another until they reach their
destination. As the packets arrive, the computer receiving the data assembles
the packets like a puzzle, recreating the message.
All data transfers across the Internet work on this principle. It helps networks
manage traffic -- if one pathway becomes clogged with traffic, packets can go
through a different route. This is different from the traditional phone system,
which creates a dedicated circuit through a series of switches. All information
through the old analog phone system would pass back and forth between a
dedicated connections. If something happened to that connection, the call
would end.
That's not the case with traffic across IP networks. If one connection should
fail, data can travel across an alternate route. This works for individual
networks and the Internet as a whole. For instance, even if a packet doesn't
make it to the destination, the machine receiving the data can determine
which packet is missing by referencing the other packets. It can send a
message to the machine sending the data to send it again, creating
redundancy. This all happens in the span of just a few milliseconds.
Pros Cons
the circuit used is dedicated to the single it is not very flexible (for example, it will send
transmission only empty frames and it has to use a single,
dedicated line)
the whole of the bandwidth is available nobody else can use the circuit/channel even
when it is idle
the data transfer rate is faster than with the circuit is always there whether or not it is
packet switching used
the packets of data (frames) arrive at the if there is a failure/fault on the dedicated line,
destination in the same order as they were there is no alternative routing available
sent
a packet of data cannot get lost since all dedicated channels require a greater bandwidth
packets follow on in sequence along the
same single route
it works better than packet switching in real- prior to actual transmission, the time required
time applications to establish a link can be long
14.2.2 Packet switching
Packet switching was introduced in Chapter 2 when describing VoIP, together with a diagram to
show how the individual packets are routed from client to client.
Packet switching is a method of transmission in which a message is broken up into a number of
packets that can be sent independently to each other from start point to end point. The data
packets will need to be reassembled into their correct order at the destination. Figure 14.8 shows
an example of packet switching.
Note that
• each packet follows its own path
• routing selection depends on the number of datagram packets waiting to be processed at each
node (router)
• the shortest path available is selected
• packets can reach the destination in a different order to that in which they are sent.
As Figure 14.8 shows, the message sent by computer ‘A’ was split into four packets. The
original packet order was: and they arrived in the order: which means they need to
be reassembled in the correct order at the destination.
The pros and cons of packet switching are summarised in this table.
Pros Cons
no need to tie up a communication line the protocols for packet
switching can be more complex
than those for circuit switching
it is possible to overcome failed or faulty lines by simply if a packet is lost, the sender
re-routing packages must re-send the packet (which
wastes time)
it is easy to expand the traffic usage does not work well with real-
time data streams
circuit switching charges the user on the distance and the circuit/channel has to share
duration of a connection, but packet switching charges its bandwidth with other packets
users only for the duration of the connectivity
high data transmission is possible with packet switching there is a delay at the destination
while packets are reassembled
packet switching always uses digital networks which needs large amounts of RAM to
means digital data is transmitted directly to the destination handle the large amounts of data
CHAPTER : FLOATING - POINT NUMBERS
In decimal notation, the number “23.456” can be written as “0.23456x102 ” . This means that in P P
decimal notation, we only need to store the numbers “0.23456” and “2”. The number “0.23456”
is called the mantissa and the number “2” is called the exponent. This is what happens in
binary.
For example, consider the binary number “10111”. This could be represented as “0.10111x25 ” P P
Similarly, in decimal, 0.0000246 can be written as “0.246x10- 4” . Now the mantissa is “0.246”
P P
Thus, in binary, “0.00010101” can be written as “0.10101x2- 11” . Now the mantissa is P P
Converting binary floating-point real numbers into denary and vice versa
Converting the integral part is simple; you simply keep adding bits of increasing power
until you get the number that you want, so in this case:
As for the fractional part, you must do repeated multiplication by 2 until your remainder is
zero, so in this case:
0.625 (the fractional part)
- 0.625 x 2 = 1.25 1 (Generate “1”,continue with the rest)
- 0.5 x 2 = 1.0 1 (Generate “1” and nothing remains in the fractional part
So 2 10
R R = 10 2 R R and 0.625 10 = 0.101 2
R R R
0
- 0.40625 x 2 = 0.8125
- 0.8125 x 2 = 1.625 1
- 0.625 x 2 = 1.25 1
- 0.25 x 2 = 0.5 0
- 0.5 x 2 = 1.0 1
-0.6 x 2 = 1.2 1
…
This process seems to go on endlessly. The number “7/10”, which is a perfectly normal
decimal fraction, is a repeating fraction in binary, just as the fraction “1/3” is a repeating
fraction in decimal. (It repeats in binary as well.) We can’t represent this number precisely as
a floating point number. The closest we can get with four bits is “.1011”. Since we already
have a leading “14”, the best eight
bit number we can make is “1110.1011” So 14.7 10 = 1110.1011 2 (binary floating-point R R R R
real number)
1101 2
R R = 8+4+1 = 13 10
R
1 1 0 0 0 0 0 0
So 1101.1100 2 = 13.75 10 R R R R
In the above examples, the decimal point in the mantissa was always placed immediately before
the first non-zero digit. This is always done like this with positive numbers because it allows
us to use the maximum number of digits.
Suppose we use 8 bits to hold the mantissa and 8 bits to hold the exponent. The binary
number
Notice that the first digit of the mantissa is 0 and the second digit is 1. The mantissa is said to
be “normalized” if the first 2 digits are different. Thus, for a positive number, the first digit is
always 0 and the second digit is always 1. The exponent is always an integer and is help in 2’s
complement form.
Now consider the binary number “0.00000101011” which is “0.1010110 x 2- 101” . Thus the
P P
mantissa is “0.101011” and the exponent is “-101”. Again, using 8 bits for the mantissa and 8
bits for the exponent, we have:
The main reason that we normalize floating-point numbers is in order to have as high degree
of accuracy as possible.
Care must be taken when normalizing negative numbers. The easiest way to normalize
negative
numbers is to first normalize the positive version of the number. Consider the binary number
“-1011”. The positive version is “1011” = “0.1011 x 2100 ” and can be represented by:
P PP P
As another example, change the fraction “-11/32” into a normalized floating point binary
number.
11/32 = 0.34375
- 0.3475 x 2 = 0.6875 0
- 0.6875 x 2 = 1.375 1
- 0.375 x 2 = 0.75 0
- 0.75 x 2 = 1.50 1
- 0.50 x 2 = 1.00 1
= 0.01011 2R
R R R R
But it is not normalized, so we do that by removing a “0” from bit “1/2” in the
mantissa and subtracting that 1 location from exponent 0 revels -1.
That is:
R R R R
For “-11/32”, keep the exponent the same and in the mantissa, convert it into 2’s
complement.
R R R R
Figure 13.10
Figure 13.11
Figure 13.12
Figure 13.13
Rounding errors
Suppose you wish to represent the number 1/7 in decimal. This number goes on infinitely
repeating the sequence “0.142857”.
How would you represent this in 4 bit floating point? (3 bit mantissa and 1 bit exponent) 1.43
x 10- 1 is the closest you can get.
P P
Even with 10, 20, or 100 digits, you would need to do some rounding to represent an infinite
number in a finite space. If you have a lot of digits, your rounding error might seem
insignificant. But consider what happens if you add up these rounded numbers repeatedly for
a long period of time. If you round 1/7 to 1.42 x 10- 1 (0 .142) and add up this representation
P P
700 times, you would expect to get 100. (1/7 x 700 = 100) but instead you get 99.4 (0.142 x
700).
Relatively small rounding errors like the example above can have huge impacts. Knowing how
these rounding errors can occur and being conscious of them will help you become a better and
more precise programmer.
Patriot Missiles, which stand for Phased Array Tracking intercept of Target, were originally
designed to be mobile defenses against enemy aircraft. These missiles were supposed to
explode right before encountering an incoming object. However, on the 25th of February 1991,
PP
a Patriot Missile failed to intercept an incoming Iraqi Scud missile which then struck an army
barrack in Dhahran killing 28 American soldiers and injuring over 100 other people!
The system’s internal clock recorded passage of time in tenths of seconds. However, as
explained earlier, 1/10 has a non-terminating binary representation, which could lead to
problems. Let’s look into why this happens.
0.0001100110011001100110011001100...
The internal clock used by the computer system only saved 24 bits. So this is what was
saved every tenth of a second:
0.00011001100110011001100
Chopping off any digits beyond the first 24 bits introduced an error of about:
0.0000000000000000000000011001100
This is about 0.000000095 seconds for each tenth of a second
The missile’s battery had been running for about 100 hours before the incident. Imagine this
error accumulating for 100 hours (10 times each second!)
The small error for each tenth of a second was not believed to be a problem. However, the
missile’s battery had been running for over 100 hours, introducing an error of about 0.34
seconds!
Given the missile was supposed to intercept a Scud traveling 1,624 meters per second, 0.34
second delay turned out to be a huge problem!
CHAPTER : BOOLEAN ALGEBRA AND K-MAPS
Half -Adder
U
A half-adder is a combinational arithmetic circuit block that can be used to add 2 bits. Such a
circuit thus has 2 inputs that represent the 2 bits to be added and 2 outputs, with one
producing the SUM output and the other producing the CARRY output
Half-adders are the simplest of all adder circuits, but it has a major
disadvantage. The half adder can add only 2 input bits (A and B) and has nothing to
do with the carry if there is any as the input. So if the input to a half adder has a carry,
then it will be neglected. Hence the binary addition process is not complete and that’s
why it is called a HALF adder
Full -Adder
U
A full-adder circuit is a combinational arithmetic circuit block that can be used to add
3 bits to produce a SUM and CARRY output.
Becomes a necessity when it comes to adding binary numbers with a large number of
digits.
Overcomes the limitation of the half-adder, which could only add 2 bits.
While adding larger binary numbers, we also consider the carry bit from the last 2
binary numbers.
Example statements
If we take a simple statement we can start to see the operations of Boolean algebra:
“I will wear a coat if it is very cold or if it is raining.”
The proposition ‘I will wear a coat’ will be true if either of the propositions ‘It is very cold’ or
‘It is raining’ are true. We can represent this as a diagram:
We could then make our statement more complex by saying:
“If I do not go on the bus, I will wear a coat if it is very cold or if it is raining.”
Brackets
Boolean operations have an order in which they are carried out. This order is:
– NOT
– AND
– OR
However, anything that appears inside brackets has the normal mathematical rule apply, that
it is always carried out first.
We can begin to create a Boolean expression from our diagram. Note that the brackets
around the OR statement mean that this will be evaluated before the AND statement:
Coat = ((NOT Bus) AND (Very cold OR Raining))
In Boolean algebra, parts of a statement are often given a single letter. This means we can
write our statement as:
C = (( ) (V + R))
If we now consider the order that Boolean operators have to be carried out in, the NOT
statement will be evaluated first, so we can simplify our statement slightly by removing the
brackets:
C = (V + R)
Boolean laws
There are some further general rules (laws) in Boolean algebra. We will use the values A, B
and C to demonstrate these rules. The rules apply to the AND and OR logic gates:
Symbols
Boolean expressions often use symbols for the operators. Different symbols can be used,
but we will use:
Simplification (reduction) rules
There are also some simplifications we need to be aware of for Boolean operators:
AA=A
A+A=A
( ̅) = A
A=0
A+=1
By looking at our example and from understanding the rules that are applied and the
simplifications that can occur, we now have a greater understanding of the workings of
Boolean algebra.
How can a circuit “remember” anything, when it’s just a bunch of gates that
produce outputs according to the inputs?
The basic idea is to make a loop, so that the circuit outputs are also the inputs.
The most common memory element used is the latch which is made up of an assembly
of logic gates. Even though a logic gate by itself has no storage capacity, several can be
connected together in ways that permit information to be stored.
A storage element maintains binary state indefinitely (as long as power is applied), until
directed by an input signal to switch to its alternated state.
The S-R (Set-Reset) latch is the most basic type. It can be constructed from NOR gates
or NAND gates. With NOR gates, the latch responds to active-HIGH inputs; with NAND
gates, it responds to active-LOW inputs
Simple flip-flops can be built around a pair of cross-coupled inverting elements: vacuum
tubes, bipolar transistors, field effect transistors, inverters, and inverting logic gates have all been
used in practical circuits. Clocked devices are specially designed for synchronous systems; such
devices ignore their inputs except at the transition of a dedicated clock signal (known as clocking,
pulsing, or strobing). Clocking causes the flip-flop either to change or to retain its output signal
based upon the values of the input signals at the transition. Some flip-flops change output on the
rising edge of the clock, others on the falling edge.
Since the elementary amplifying stages are inverting, two stages can be connected in succession
(as a cascade) to form the needed non-inverting amplifier. In this configuration, each amplifier
may be considered as an active inverting feedback network for the other inverting amplifier. Thus
the two stages are connected in a non-inverting loop although the circuit diagram is usually
CHAPTER : PROCESSOR TYPES
MISD – multiple instruction single data, computer architecture which uses many processors
but the same shared data source.
MIMD – multiple instruction multiple data, computer architecture which uses many
processors, each of which can use a separate data source.
Cluster – a number of computers (containing SIMD processors) networked together.
Super computer – a powerful mainframe computer.
Massively parallel computers – the linking together of several computers effectively forming
one machine with thousands of processors.
CISC features RISC features
Many instruction formats are possible Uses fewer instruction formats/sets
There are more addressing modes Uses fewer addressing modes
Makes use of multi-cycle instructions Makes use of single-cycle
instructions
Instructions can be of a variable length Instructions are of a fixed length
Longer execution time for instructions Faster execution time for
instructions
Decoding of instructions is more complex Makes use of general multi-purpose
registers
It is more difficult to make pipelining work Easier to make pipelining function
correctly
The design emphasis is on the hardware The design emphasis is on the
software
Uses the memory unit to allow complex instructions to Processor chips require fewer
be carried out transistors
Table 15.1 The differences between CISC and RISC processors
Word/phrase Meaning
Boolean algebra Algebra in which variables may have one of
two values (True or False, 1 or 0)
Boolean operators System of operators used for logical
operations. The following symbols are used in
this guide:
• NOT: A, B, C
• AND: .
• OR: +
2.2.Worked examples
Use the following link to work through examples of two- and three-variable Karnaugh maps
and the simplification rules:
http://www.ee.surrey.ac.uk/Projects/Labview/minimisation/karnaugh.html
Pipelining
One of the major developments resulting from RISC architecture is pipelining. This is one of the
less complex ways of improving computer performance. Pipelining allows several instructions to
be processed simultaneously without having to wait for previous instructions to be completed. To
understand how this works, we need to split up the execution of a given instruction into its five
stages
1 instruction fetch cycle (IF)
2 instruction decode cycle (ID)
3 operand fetch cycle (OF)
4 instruction execution cycle (IE)
5 writeback result process (WB).
To demonstrate how pipelining works, we will consider a program which has six instructions (A,
B, C, D, E and F). Figure 15.1 shows the relationship between processor stages and the number
of required clock cycles when using pipelining. It shows how pipelining would be implemented
with each stage requiring one clock cycle to complete.
CHAPTER : OPERATING SYSTEM
Operating System Concepts (Chapter 7) Tasks/Processes – Process States New Ready Running Blocked
Exit – Process Control Block – Multitasking – Task Scheduling – CPU and IO Bound Tasks – Scheduling
Algorithms
Process states New: Process has been created but hasn’t yet got admission in ready queue. Ready:
Submitted for execution, all resources are assigned except CPU. Running: Currently running on CPU
Blocked: Cannot execute until specific event occurs Exit: Normal or abnormal termination of process
CPU Scheduling Criteria CPU Utilization: CPU should be kept as busy as possible. (40 to 90 percent)
Throughput: Work is being done. No. Of processes per unit time Turnaround time: For a particular process
how long it takes to execute. (Interval between time of submission to time of completion) Waiting time:
Total time process spends in ready queue. Response: First response of process after submission
CPU Scheduling FCFS or Co-operative SJF SRT first Priority Scheduling Round Robin Scheduling
SJF( Non Pre-emptive and Simultaneous arrival ProcessArrival TimeBurst Time P P P P SJF (non-
preemptive, simultaneous arrival) Average waiting time = ( )/4 = 4 Average turn-around time = ( )/4 = 8
P4P4 P3P3 P1P P2P2 24
SJF (Non pre-emptive and varied arrival) ProcessArrival TimeBurst Time P P P P SJF (non-preemptive,
varied arrival times) Average waiting time = ( (0 – 0) + (8 – 2) + (7 – 4) + (12 – 5) )/4 = ( )/4 = 4
Average turn-around time: = ( (7 – 0) + (12 – 2) + (8 - 4) + (16 – 5))/4 = ( )/4 = 8 P1P1 P3P3 P2P P4P4
812
SJF (Pre-emptive and varied arrival) SRTF ProcessArrival TimeBurst Time P P P P SJF (preemptive, varied
arrival times) Average waiting time = ( [(0 – 0) + (11 - 2)] + [(2 – 2) + (5 – 4)] + (4 - 4) + (7 – 5) )/4
= )/4 = 3 Average turn-around time = ( )/4 = 9.75 P1P1 P3P3 P2P P4P4 57 P2P2 P1P1 16
Priority Sceduling A priority number (integer) is associated with each process The CPU is allocated to the
process with the highest priority (smallest integer highest priority) – Preemptive – nonpreemptive SJF is
a priority scheduling where priority is the predicted next CPU burst time Problem Starvation – low priority
processes may never execute Solution Aging – as time progresses increase the priority of the process
Round Robin ProcessBurst Time P 1 53 P 2 17 P 3 68 P 4 24 Typically, higher average turnaround than
SJF, but better response time Average waiting time = ( [(0 – 0) + ( ) + (121 – 97)] + (20 – 0) + [(37 –
0) + ( ) + (134 – 117)] + [(57 – 0) + (117 – 77)] ) / 4 = ( ) ( ) + ( ) ) / 4 = ( )/4 = 292 / 4 = 73 Average
turn-around time = ) / 4 = P1P1 P2P2 P3P3 P4P4 P1P1 P3P3 P4P4 P1P1 P3P3 P3P
An operating system (OS) is a set of programs that manage computer resources and run in the
background on a computer system, giving an environment in which application software can be
executed.
Responsibilities of the OS include:
Managing between the hardware’s resources which include the processors, memory, data
storage and I/O devices.
Application Software – programs that help the user perform a particular task.
User Interfaces
The user interface is the interaction between the User and the Machine, letting the user
send commands with the expected results. Two types of interfaces are:
Command Line Interface (CLI)
A command-line interface allows the user to interact with the computer by typing in commands. The
computer displays a prompt, the user keys in the command and presses the enter button
Features of a command-line interface
Commands must be typed correctly and in the right order or the command will not work.
Experienced users who know the commands can work out very quickly without having to
find their way around the menus.
An advantage of command driven programs is that they do not need the memory and
processing power of the latest computer and will often run on lower spec machines.
A command-line interface can run many programs, for example a batch file could launch half
a dozen programs to do its task.
An inexperienced user can sometimes find a command driven program difficult to use
because of the number of commands that have to be learnt.
An example of command line interface is MS-DOS. The MS-DOS command to display all files on
the C drive would be “dir c:\”
Graphical user interface is sometimes shortened to GUI. The user chooses an option usually by pointing
a mouse at an icon representing that option
They enable you to easily exchange information between software using cut and paste
They use a lot of memory and processing power. It can be slower to use than a command-
line interface.
They can be irritating to experienced users when simple tasks require a number of operations.
Examples of GUIs are Windows XP, Windows 8, Apple OSX, and Ubuntu.
The operating system is designed to hide the complexity of the PC’s hardware. In order to achieve this, it
offers different features.
The system’s resources are managed by the OS and can also be manages by the user using the
OS.
Reads and writes from or to a disk, also providing an interface for the user to manage the files.
Scheduling U
The earliest operating systems were used to control single-user computer systems. In those days, the
operating system would read in one job, find the data and devices the job needed, let the job run to
completion, and then read in another job. In contrast, computers today with modern OS systems are
capable of multiprogramming or executing many programs simultaneously. With
multiprogramming, when a job cannot use the processor, the system can suspend, or interrupt the job
freeing the processor to work on another job.
OS makes multiprogramming possible by capturing and saving all the relevant information about the
interrupted program before allowing another program to execute. When the interrupted program is
ready to begin executing again, it can resume execution just where it left off. Multiprogramming allows
the OS to run thousands of programs simultaneously for users who might be working on different
projects at different physical locations around the world.
So, one of the tasks of the OS is to arrange the jobs that need to be done into an appropriate order. The
order may be chosen to ensure that maximum use is made of the processor; another order may make
one job more important than the other. In the latter case, the OS makes use of priorities.
Suppose the processor is required by program A, which is printing wage slips for the employees of a
large company, and by program B, which is analyzing the annual, world-wide sales of the company
which has a turnover of many millions of dollars.
Program A makes little use of the processor and is said to be “I/O bound”.
Program B makes a great deal of the processor and is said to be “Processor Bound”. B
If program B has priority over program A for use of the processor, it could be a long time before
program A can print any wage slips. This is shown in the diagram below.
B using processor
I/O B using processor
Priority. Give some jobs a greater priority than others when deciding which job should be
given access to the processor.
I/O or Processor Bound. If a processor bound job is given the main access to the processor,
it could prevent the I/O devices being serviced efficiently.
Type of job. Batch processing, on-line and real-time jobs all require different response times.
Resource requirements. The amount of time needed to complete the job, the memory
required, I/O and processor time.
Resources used so far. The amount of processor time used so far, how much I/O used so far.
Waiting time. The time the job has been waiting to use the system.
In order to understand how scheduling is accomplished, it is important to realize that any job may be
in one, and only one, of three states. A job may be ready to start, running on the system or blocked
because it is waiting for a peripheral, for example, the figure below shows how jobs may be moved
from one state to another. Note that a job can only enter the running state from the ready state. The
ready and blocked states are queue that may hold several jobs. On a standard single processor
computer, only one job can be in the running state. Also, all jobs entering the system normally enter
from the running state and (normally) only leave the system from the running state.
When entering the system, a job is placed in the ready queue by a part of the OS called the High level
Scheduler (HLS). The HLS makes sure that the system is not overloaded.
Sometimes it is necessary to swap jobs between the main memory and backing store (see
memory management later). This task is done by the Medium Level Scheduler (MLS).
Moving jobs in and out of the ready state is done by the Low Level Scheduler (LLS). The
LLS decides the order in which jobs are to be placed in the running state. There are many
policies that may be used to do scheduling, but they can all be placed in one of two classes.
These are pre-emptive and non-pre-emptive policies.
A pre-emptive scheme allows the LLS to remove a job from the running state so that another
job can be placed in the running state.
A non-pre-emptive scheme allows each job to run until it no longer requires the processor.
This may be because it has finished or it needs an I/O device.
Some common scheduling policies are:
FCFS
Simply means that the first job to enter the queue is the first to enter the
running state. This policy favors long jobs.
SJF
Simply means sort jobs in the ready queue in ascending order of time needed
by each job. New jobs are added to the queue in such a way as to preserve
this order.
RR
This gives each job a maximum length of processor time (called a time slice)
after which the job is put at the back of the ready queue and the job at the
front of the queue is given use of the processor. If a job is completed before
the maximum time is up, it leaves the system
SRT
The ready queue is sorted on the amount of expected time still required by a
job. This scheme favors short jobs even more than SJF. Also there is a danger
of long jobs being prevented from running.
MFQ
Involves several queues of different priorities with jobs migrating
downwards.
For SJF, the short jobs are processed first depending on the time required; they will be sorted
in this order once. While SRT will mean that once an interrupt is raised, the queue will be
reshuffled again with the shortest job first. In this way, SRT favors short jobs more than
SJF.
Although the two-state process management model is a perfectly valid design for an operating
system, the absence of a BLOCKED state means that the processor lies idle when the active
process changes from CPU cycles to I/O cycles. This design does not make efficient use of the
processor. The three-state process management model is designed to overcome this problem, by
introducing a new state called the BLOCKED state. This state describes any process which is
waiting for an I/O event to take place. In this case, an I/O event can mean the use of some device or
a signal from another process. The three states in this model are:
RUNNING → READY The most common reason for this transition is that the running process
has reached the maximum allowable time for uninterrupted execution; i.e. time-out occurs. Other
reasons can be the imposition of priority levels as determined by the scheduling policy used for
the Low Level Scheduler, and the arrival of a higher priority process into the READY state.
RUNNING → BLOCKED A process is put into the BLOCKED state if it requests something for
which it must wait. A request to the OS is usually in the form of a system call, (i.e. a call from the
running process to a function that is part of the OS code). For example, requesting a file from
disk or a saving a section of code or data from memory to a file on disk.
paging is a memory management scheme by which a computer stores and retrieves data
from secondary storage[a] for use in main memory.[1] In this scheme, the operating system retrieves
data from secondary storage in same-size blocks called pages. Paging is an important part of virtual
memory implementations in modern operating systems, using secondary storage to let programs
exceed the size of available physical memo
Virtual memory
1. Memory that appears to exist as main storage although most of it is supported by data
held in secondary storage, transfer between the two being made automatically as
required.
Disk Thrashing
Disk Thrashing is a problem that may occur when virtual memory is being used. As main
memory fills up, then more and more pages need to be swapped in and out of virtual memory.
This swapping leads to a very high rate of hard disk access.
Compared to RAM, moving a hard disk head takes thousands of times longer. And so
eventually more time is spent swapping the pages than on processing the data. If disk thrashing
continues then most likely program will lock-up or not run.
It doesn't do the hard disk any good either as it is being accessed rapidly (thousands of times
per second) by the memory manager. Sometimes you can hear the disk whirring away when
disk thrashing occurs.
Usually the operating system will warn the user of 'Low virtual memory' to encourage them to
shut down a few applications before everything grinds to a halt.
Memory Leaks
A program will demand some memory within a segment for storing its variables and data.
When a memory location is in use, the memory manager will mark it as being unavailable for
any other process. All well and good.
A well-written program will flush all its data away once it no longer needs it and the memory
manager will release that memory for other processes to use. But a badly written program will
not flush its data properly and so more and more memory gets locked up. This is called a
'memory leak'. If the leak is bad enough, then the system eventually runs out of memory. Before
that happens, the computer seems to run slower and slower. Usually the only recourse is to re-
boot the computer to ensure all its memory has been cleared.
Memory Crashes
The diagram shows two well-behaved programs running in memory. But what if Process 1 was
poorly coded and starts to write data into areas outside its allocated segment? The chances are,
the data will be written smack in the middle of another process, which will most likely result in
the renowned 'blue screen of death'.
Luckily memory management has become more sophisticated in modern operating systems and
a program trying to write outside its boundary will be prevented from doing so. This will still
probably crash the rogue program but at least all the other processes are still running safely.
Thrashing
With a computer, thrashing or disk thrashing describes when a hard
drive is being overworked by moving information between
the system memory and virtual memoryexcessively. Thrashing occurs
when the system does not have enough memory, the system swap file
is not properly configured, too much is running at the same time, or
has low system resources.
When thrashing occurs you will notice the computer hard drive always
working and a decrease in system performance. Thrashing is bad on the
hard drive because of the amount of work the hard drive has to do and if left
unfixed can cause an early hard drive failure.
How to eliminate thrashing
To resolve hard drive thrashing you can do any of the suggestions below.
1. Increase the amount of RAM in the computer.
2. Decrease the number of programs being run on the computer.
3. Adjust the size of the swap file.
A virtual machine is a software computer that, like a physical computer, runs an operating
system and various applications.
Virtual machines allow you to run an operating system in a window on your desktop. A
virtual machine could be used to run software made for other operating systems. For
example, running a program built for Ubuntu on your Windows 8 computer. The virtual
machine runs in a window on your main operating system
All in all, a virtual machine has its pros and cons and is mainly used by program developers.
One example that we will discuss in detail is the “Java Virtual Machine”
Java tackles this problem beautifully, you write a program once and run it anywhere on any
type of computer. In other words, Java is a platform independent language, which means that
any program written using Java will run on any type of computer which Java supports (majority
of computer do support java).
Etc…
The JVM understands Bytecode language, its microprocessor’s machine language, and knows
how to establish communication between operating system and the bytecode program.
The JVM converts bytecode into the machine language. And, once the program is into its
own machine language form, the computer can now run the program. This way, your bytecode
is executed by a JVM on all different type of computers. So this way, a program written using
Java can run on any computer or in other words, is a Platform independent language and the
reason that Java code can run on any type of computer is because of the JVM program.
This is also a reason why Java runs fairly slower compared to other high level programming
languages.
Word/phrase Meaning
byte code intermediate code that a virtual
machine converts into machine code
compiler a program that converts source code
into machine code
emulator software that runs a virtual machine
guest the emulated system/program
host the computer that runs the emulated
virtual machine
machine code code written as instructions that the
processor understands
process virtual machine an emulation of a single process
source code code written in a high-level language
system virtual machine an emulation of a complete computer
system/operating system
virtualisation the process of creating and running a
virtual machine
virtual machine software emulation of another
computer/program/operating system
What is a virtual machine?
Computers vary in the hardware they use, and run different programs and operating
systems. Sometimes we might want to have the functionality and capability of another
computer system running on our own computer. It may be that the hardware for the other
system no longer exists or is unavailable, or we may wish to have several versions of a
computer running simultaneously on one hardware system.
We can use software to emulate the other computer system so that our computer can
behave like that system. This emulation is known as a virtual machine.
The process of creating and running a virtual machine is known as virtualisation. The
computer that runs the virtual machine is known as the host. The virtualised system is
known as a guest. Virtualisation software, known as an emulator, creates a virtual
implementation of the guest system on the host computer, providing the host with the
functions and programs of the guest. The emulator translates the instructions of the guest
computer so that the host computer can understand them.
The emulator also provides a bridge that allows the virtual machine to use the user interface, via the
host operating system (I/O –
If a host system is powerful enough, several virtual machines can be run side-by-side on one
computer. A good example of this is the increasing use of virtual machines to run several
virtual servers, such as a domain name server, web server and print server, on one physical
server. The host server maintains the virtual machines and allows them to act as though
they are all separate computers.
2.5 The Java Virtual Machine
Many programs are written in high-level languages such as Visual Basic, C++, Python and
Java. Before these programs can be run they need compiling. A compiler takes the high-
level language source code and converts it into machine code. This machine code is a set
of instructions specific to the processor architecture that the compiler is running on. As a
result, this code can only be run on processors that share the same instruction set.
The Java compiler does not convert a high-level language into machine code, but into an
intermediate language called byte code. This still needs translating into machine code
before it can be run on a processor.
The Java Virtual Machine is a virtual machine that reads and translates byte code into
different machine codes for many different processor architectures and operating systems.
This means that Java programs can be run on any computer that has this virtual machine,
including desktop PCs, laptops, tablets, smartphones and smart TVs.
Benefits and limitations of virtual machines
Using virtual machines brings several benefits:
platforms
-independent programs to run on multiple platforms
can be easily backed up, copied and re-installed. This has particular benefits in
corporate environments where network downtime can lead to loss of revenue. The virtual
machines can be installed on another computer and be up and running again quickly.
software for another platform may find that they still cannot use the software correctly. For
example, an emulation of a driving video game that requires a steering wheel might not be
able to operate the game correctly
substantial amount of these resources, resulting in reduced performance from the host
computer
17 Security
Introduction
Stating the obvious, people and computers do not speak the same language.
People have to write programs in order to instruct a computer what to do. This text is
called the “source code” and most often it is written in a high level language that people
find easy to use and write. On the other hand, a computer runs only binary code
‘1010101011110101011’ within its registers which is almost incomprehensible to
people, this is called the “machine code” or the
“Object code”
When electronic computers were first used, the programs had to be written in machine
code. This code was comprised of simple instructions each of which was represented by
a binary pattern in the computer. To produce these programs, programmers had to write
the instructions in binary. This not only took a long time, it was also prone to errors.
To improve program writing, assembly languages were developed. Assembly languages
allowed the use of mnemonics and names for locations in the memory. Each assembly
instruction mapped to a single machine instruction which meant that it was fairly easy to
translate a program written in assembly.
To speed up this translation process, assemblers were written which could be loaded into
the computer and then the computer could translate the assembly language into a machine
code; this process was still tedious and took a long time.
After assembly languages, came high-level languages which programmers nowadays (and
maybe even you) use to write code instructions for the computer to carry out. Several high-
level languages were developed. FORTRAN (FORmula TRANslation) was developed for
science and engineering programs and it used formulae in the same way as would scientists
and engineers. Similarly COBOL (Common Business Oriented Language) was developed
After assembly languages, came high-level languages which programmers nowadays (and
maybe even you) use to write code instructions for the computer to carry out. Several high-
level languages were developed. FORTRAN (FORmula TRANslation) was developed for
science and engineering programs and it used formulae in the same way as would scientists
and engineers. Similarly COBOL (Common Business Oriented Language) was developed
Lexical Analysis
The lexical analyzer uses the source program as input and creates a stream of tokens for
the syntax analyzer. Tokens are normally 16-bit unsigned integers. Each group of
characters is replaced by a token. Single characters, which have a meaning in their own
right, are replaced by ASCII values. Multiple character tokens are represented by integers
greater than 255 because the ones up to 255 are reserved for the ASCII codes. Variable
names will need to have extra information stored about them; this is done by means of a
symbol table. This table is used throughout compilation to build up information about
names used in the program. During lexical analysis, only the variable’s name will be
noted. It is during syntax and semantic analysis that details such as the variable’s type
and scope are added. The lexical analyzer may output some error messages and
diagnostics. For example, it will report errors such as an identifier or variable name which
breaks the rules of the language.
At various stages during compilation, it will be necessary to look up details about the
names in the symbol table. This must be done efficiently! (so a linear search will not be
ideal for this task). In fact, it is usual to use a hash table and to calculate the position of a
name by hashing the name itself. When two names are hashed to the same address, a
linked list can be used to avoid the symbol table filling up.
The lexical analyzer also removes redundant characters such as white spaces (tabs,
spaces, etc… which we may find useful to make code more readable, but the computer
does not want) and comments. Often the lexical analysis takes longer than the other stages
of compilation. This is because it has to handle the original source code, which can have
many formats. For example, the following two pieces of code are equivalent although
their format is considerably different!
When the lexical analyzer has completed its task, the code will be in a standard format. This
means that the syntax analyzer (which is the next stage) can always expect the format of its
input to be the same
During this stage of compilation the code generated by the lexical analyzer is parsed
(broken into small units) to check if it is grammatically correct. All languages have rules
of grammar and computer languages are no exception. The grammar of programming
languages is defined by means of BNF notation. It is against these rules that the code has
to be checked.
Code U Generation:
It is at this stage, when all the errors due to incorrect use of the language have been
removed, that the program is translated into code suitable for use by the computer's
processor.
During lexical and syntax analysis a table of variables has been built up which includes
details of the variable name, its type and the block in which it is valid. The address of the
variable is now calculated and stored in the symbol table. This is done as soon as the
variable is encountered during code generation.
Before the final code can be generated, an intermediate code is produced. This
intermediate code can then be interpreted or translated into machine code. In the latter
case, the code can be saved and distributed to computer systems as an executable
program. Two methods can be used to represent the high-level language in machine code.
This syllabus does not require knowledge of either of these methods, but a brief outline
is given for those who may be interested.
One uses a tree structure and the other a three address code (TAC). TAC allows no more
than three operands. Instructions take the form:
Operand 1 := Operand 2
R R R R Operator Operand 3
R
A:= (B + C) * (D – E) / F
Can be evaluated in the following order, where “Ri” represents an intermediate result.
R1
R R := B + C
R2
R R := D – E
R3
R R := R1 * R2
A := R3 / F
Backus -Naur Form and Syntax Diagrams
U
A visual basic compiler would not understand the C++ syntax and vice versa. We
therefore need, for each language, a set of rules that specify precisely every part of the
language. These rules are specified using the Backus Naur Form (BNF) or syntax
diagrams.
All languages use integers, so we shall start with the definition of an integer. An integer
is a sequence of digits 0,1,2,3,….,9. Now the number of digits in an integer is arbitrary.
That is, it can be
any number. A particular compiler will restrict the number of digits only because of the
storage space set aside for an integer has been exceeded. But a computer language does
not restrict the number of digits.
0
2
415
3040513002976
0000000123
But we now must define a digit. A digit is 0 or 1 or 2 or…….. Or 9 and we write this
as:
<Digit>:= 0|1|2|3|4|5|6|7|8|9
The vertical line is read as OR. Notice that all the digits have to be specified and that
they are not inside angle brackets. (< and >) like <integer> and <digit> This is because
integer and digit have definitions elsewhere.
This is a single digit integer (1) followed by the integer 47. But 47 is a single digit integer
(4) followed by a single digit integer (7). Thus, all integers of more than one digit start
with a single digit and are followed by an integer. Eventually the final integer is a single
digit integer.
Thus, an indefinitely long integer is defined as: <integer> ::= <digit><integer>
To stop this ongoing process, we use the fact that eventually, <integer> is
So after reading the page above, you should be familiar with a few terms:
A public key is used to encrypt files, so that the data is un-readable to humans.
A private key is used to decrypt files, so that the encrypted file can be reverted
back to the normal human readable data.
Encryption is the process of translating human readable plain text into unreadable
cipher text for the purpose of data security.
Asymmetric key cryptography is the process of using public key and private key
to send data over the internet securely as described above.
In order to understand how public key cryptography works, suppose Alice and Bob wish to send
secure mail to each other:
First, both Bob and Alice need to create their public/private key pairs. This is usually
done with the help of a Certification Authority (CA).
Alice and Bob then exchange their keys. This is done by exchanging certificates.
Bob can then use his private key to digitally sign messages, and Alice can check his
signature using his public key.
Bob can use Alice’s public key to encrypt messages, so that only she can decrypt
messages for him to read.
Similarly, Alice can use Bob’s Public key to encrypt messages, so that only he can
decrypt her messages.
So after reading the page above, you should be familiar with a few terms:
A public key is used to encrypt files, so that the data is un-readable to humans.
A private key is used to decrypt files, so that the encrypted file can be reverted back
to the normal human readable data.
Encryption is the process of translating human readable plain text into unreadable cipher
text for the purpose of data security.
Asymmetric key cryptography is the process of using public key and private key to
send data over the internet securely as described above.
It is basically a way to ensure that an electronic document (e-mail, spreadsheet, text file, etc.) is
authentic. Authentic means that you know who created the document and you know that it has
not been altered in any way since that person created it.
Digital signatures rely on certain types of encryption to ensure authentication. You already know
what encryption is from the last section. Authentication as mentioned above is the process of
verifying that information is coming from a trusted source. Encryption and Authentication work
hand in hand for digital signatures.
There are several ways to authenticate a person or information on a computer:
Password – The use of a username and password provide the most common form of
authentication. You enter your name and password when prompted by the computer. It
checks the pair against a secure file to confirm. If either the name or password do not match,
then you are not allowed further access.
Public key encryption – It uses a combination of a private key and a public key. The
private key is known only to your computer while the public key is given by your computer
to any computer that wants to communicate securely with it. To decode an encrypted
message, a computer must use the public key provided by the originating computer and its
own private key.
What is a digital certificate?
An attachment to an electronic message used for security purposes. The most common use of a
digital certificate is to verify that a user sending a message is who he or she claims to be, and to
provide the receiver with the means to encode a reply.
An individual wishing to send an encrypted message applies for a digital certificate from a
Certificate Authority (CA). The CA issues an encrypted digital certificate containing the
applicant’s public key and a variety of other identification information. The CA makes its own
public key readily available through the internet.
The recipient of an encrypted message uses the public key to decode the digital certificate attached
to the message and then verify the key as a legitimate key from the CA. The recipient also receives
the public key and identification information held within the certificate. With this information, the
recipient can send an encrypted reply.
Today, there are a number of authorities which provide digital certificates; one of them is
“Verisign”
The procedure to obtain your own digital certificate is a
standard procedure and not a very hard one.
It starts with you opening your web browser and navigating to their website. There will be a form
which you have to fill in with your personal details and the type of certificate you wish to apply
for. Enter your details and pick the level of security you wish to attain on your certificate this
procedure usually asks for a fixed fee to be paid before you are entitled to actually use the
certificate.
After all the details have been approved and the fee has been paid, A pop-up window will show
with information about your certificate. Click on the Install button to install the certificate in your
web browser.
Now all the encryption and decryption of messages will be done automatically thanks to your new
digital certificate.
To re-iterate points, A digital certificate is used to bind public keys to people or other entities. If
there were no certificates, the signature could easily be faked because the recipient would be unable
to check if the public key belongs to the sender.
The value of the hash is unique to the hashed data. Any change in the data,
even changing or deleting a single character, results in a different value. This
attribute enables others to validate the integrity of the data by using the
signer's public key to decrypt the hash. If the decrypted hash matches a
second computed hash of the same data, it proves that the data hasn't
changed since it was signed. If the two hashes don't match, the data has
either been tampered with in some way (integrity) or the signature was
created with a private key that doesn't correspond to the public key presented
by the signer (authentication).
If
the two hash values match, the message has not been tampered with, and the receiver knows the
message is from sender.
Most modern email programs support the use of digital signatures and digital
certificates, making it easy to sign any outgoing emails and validate digitally
signed incoming messages. Digital signatures are also used extensively to
provide proof of authenticity, data integrity and non-repudiation of
communications and transactions conducted over the Internet.
SSL stands for Secure Sockets Layer. It provides a secure connection between internet
browsers and websites, allowing you to transmit private data online. SSL has become part of
an overall security protocol known as Transport Layer Security (TLS).
In your browser, you can tell when you are using a secure protocol, such as TLS, in a couple
of ways. You will notice that the “http” in the address line is replaces with “https”, and you
should see a small padlock as well when you are accessing sensitive information, such as an
online bank account, any site where you have to enter credit card number, sites like PayPal or
Google Checkout, etc… . You’ll know that your session is secure if you see a “https” and a
padlock symbol.
If these websites did not make arrangements for this kind of security, then there will be a good
chance that your personal information will be floating around on the internet for the public or
even hackers to use for bad purposes.
TLS and its predecessor SSL make significant use of certificate authorities. Once your browser
requests a secure page and adds the “s” onto the “http” the browser sends out the public key
and the certificate, checking three things:
So what is SSL used for? The SSL protocol is used by millions of e-Business providers to
protect their customers, ensuring their online transactions remain confidential. A web page
should use encryption expected to submit confidential data, including credit card details,
passwords or any personal information. All web browsers have the ability to interact with
secured sites so long as the site's certificate is from a recognized certificate authority.
Why do you need SSL?
U
The internet has spawned new global opportunities for enterprises conducting online
commerce. However, it has also attracted fraudsters and cyber criminals who are ready to
exploit any opportunity to steal consumer bank account numbers and card details. Unless the
connection between a client (e.g. internet browser) and a webserver is encrypted, then any
moderately skilled hacker can easily intercept and read the traffic.
How SSL uses both Asymmetric and Symmetric Encryption
U
In SSL communications, the server’s SSL Certificate contains an asymmetric public and
private key pair. The session key that the server and the browser create during the SSL
handshake is symmetric. This is explained further in the diagram below.
What is an SSL Certificate?
SSL stands for Secure Sockets Layer and, in short, it's the standard technology for keeping an
internet connection secure and safeguarding any sensitive data that is being sent between two
systems, preventing criminals from reading and modifying any information transferred, including
potential personal details. The two systems can be a server and a client (for example, a shopping
website and browser) or server to server (for example, an application with personal identifiable
information or with payroll information).
It does this by making sure that any data transferred between users and sites, or between two
systems remain impossible to read. It uses encryption algorithms to scramble data in transit,
preventing hackers from reading it as it is sent over the connection. This information could be
anything sensitive or personal which can include credit card numbers and other financial
information, names and addresses.
TLS (Transport Layer Security) is just an updated, more secure, version of SSL. We still refer to
our security certificates as SSL because it is a more commonly used term, but when you
are buying SSL from Symantec you are actually buying the most up to date TLS certificates with
the option of ECC, RSA or DSA encryption.
HTTPS (Hyper Text Transfer Protocol Secure) appears in the URL when a website is secured by
an SSL certificate. The details of the certificate, including the issuing authority and the corporate
name of the website owner, can be viewed by clicking on the lock symbol on the browser bar.
How does an SSL certificate work?
The basic principle is that when you install an SSL certificate on your server and a browser
connects to it, the presence of the SSL certificate triggers the SSL (or TLS) protocol, which will
encrypt information sent between the server and the browser (or between servers); the details are
obviously a little more complicated.
SSL operates directly on top of the transmission control protocol (TCP), effectively working as a
safety blanket. It allows higher protocol layers to remain unchanged while still providing a
secure connection. So underneath the SSL layer, the other protocol layers are able to function as
normal.
If an SSL certificate is being used correctly, all an attacker will be able to see is which IP and
port is connected and roughly how much data is being sent. They may be able to terminate the
connection but both the server and user will be able to tell this has been done by a third party.
However, they will not be able to intercept any information, which makes it essentially an
ineffective step.
The hacker may be able to figure out which host name the user is connected to but, crucially, not
the rest of the URL. As the connection is encrypted, the important information remains secure.
1
SSL starts to work after the TCP connection is established, initiating what is called an SSL
handshake.
2
The server sends its certificate to the user along with a number of specifications (including which
version of SSL/TLS and which encryption methods to use, etc.).
3
The user then checks the validity of the certificate, and selects the highest level of encryption
that can be supported by both parties and starts a secure session using these methods. There are a
good number of sets of methods available with various strengths - they are called cipher suites.
4
To guarantee the integrity and authenticity of all messages transferred, SSL and TLS protocols
also include an authentication process using message authentication codes (MAC). All of this
sounds lengthy and complicated but in reality it’s achieved almost instantaneously.
What is malware?
U
“Malware”, short for malicious software, is any software used to disrupt the computer’s
operation, gather sensitive information without your knowledge, or gain access to a private
computer. It includes any software that gets installed on your machine and performs unwanted
tasks, often for some third party’s benefit.
Malware programs can range from being simple annoyances (pop-up advertising) to causing
serious computer invasion and damage (stealing passwords and data or infecting other machines
on the network). Additionally, some malware programs are designed to transmit information about
your Web-browsing habits to advertisers or other concerned parties without your knowledge.
It is unfortunate that there are people out there with malicious intent, but it is good to be aware of
the fact. You can install several utilities that will seek and destroy the malicious programs they
find on your computer.
Types of malware
Virus – Software that can replicate itself and spread to other computers or are
programmed to damage a computer by deleting files, reformatting the hard disk, or using
up the computer memory. All computer viruses are man-made. Even a simple virus is
dangerous because it will quickly use all available memory and bring the system to a halt.
A virus requires the user to run an infected program in order for the virus to spread.
A good anti-virus program can protect you to some extent, but it’s not enough on its own as it is
hard to keep it up to date. Many modern worms change hourly and it can take a day or more to
create and distribute an anti-virus update.
You also need a firewall to help block the worm’s communications, but the most effective way to
prevent worm infection is to turn off JavaScript for normal web browsing. JavaScript is a powerful
tool that makes websites interactive, and is increasingly relied on by web designers. But it is also
the most common entry point that worms use to infect your computer. So there is a trade-off.
Turning off JavaScript for normal web browsing will limit your access to many websites, but is
the best form of protection against worm infection.
An interesting fact about worms:
In the 1980s, researchers were seeking ways of managing the growing internet remotely, using
programs that could distribute themselves automatically across it.
In the US, on 2 November 1988, a Cornell University student called Robert Morris released an
experimental self-replicating program onto the internet to find out how many computers were
currently connected to it. The program spread rapidly, installing itself on an estimated 10% of the
computers then connected.
Morris had no malicious intent, but a bug in his program caused many of the computers the worm
landed on to crash. He was prosecuted and expelled from Cornell, but worms had come of age and
have since evolved into an effective way of attacking systems connected to the internet!
Spam – It is flooding the Internet with many copies of the same message, in an attempt to
force the message on people who would not otherwise choose to receive it. Most spam is
commercial advertising, often for fake products, get-rich-schemes, etc... Some people
define spam as unsolicited email and you do not know who the sender is. Real spam is
generally email advertising for some product sent to a mailing list or a newsgroup.
In addition to wasting people’s time with unwanted e-mail, spam also eats up a lot of
bandwidth. Organizations are trying to fight spam, but because the Internet is public, there
is really little that can be done to prevent spam.
There are however, a few precautions the user can take to reduce the amount of spam he/she
gets on his/her email.
Use firewall software on your computer to stop attacks from people attempting to
compromise your system and possible use it to send spam.
Always check the sender and recipient information of suspicious messages. Spam
will typically be sent from falsified email addresses to conceal the real sender, with
a number of recipients in the BCC field of the message to hide the large number of
recipients.
Do not use real email addresses for signing up for (free) downloads of any kind
online.
Do not make purchases based on spam messages you receive, thus eliminating the spammers’
economic foundation
Phishing – The act of sending an email to a user falsely claiming to be an established legitimate
enterprise in an attempt to scam the user into surrendering private information that will be used
for identity theft.
Phishing email will direct the user to visit a
website where they are asked to update
personal information such as password,
credit card, social security, or bank account
numbers that the legitimate organization
already has. The website, however, is bogus
and set up only to steal the information the
users enter on the page.
18 Artificial Intelligence (AI)
18.1 Artificial Intelligence (AI)
Candidates should be able to: Notes and guidance
Show understanding of how graphs can be used to aid Purpose and structure of a graph
Artificial Intelligence (AI) Use A* and Dijkstra’s algorithms to perform searches
on a graph
Candidates will not be required to write algorithms to
set up, access, or perform searches on graphs
Show understanding of how artificial neural networks
have helped with machine learning
Show understanding of Deep Learning, Machine Understand machine learning categories, including
Learning and Reinforcement Learning and the reasons supervised learning, unsupervised learning
for using these methods.
Show understanding of back propagation of errors and
regression methods in machine learning
Figure 18.1
First, redraw the diagram, replacing the circled letters as per the key:
Figure 18.2
Set the final value of the start vertex (vertex A) to 0 (as per step 1 above).
The two vertices connected to A (B and C) are given the working values 5 and 6 respectively.
Make the smallest working value (vertex B) a final value. Then give the vertices connected to B
(D and E) working values based on the original distances. The working value for E is the final
value of B plus the value of B to E (5 + 6 = 11). The working value for D is the final value of B
plus the value of B to D (5 + 4 = 9).
The diagram now looks like this:
Figure 18.3
Figure 18.4
Vertex D now has the smallest working value (9), so this becomes a final value.
Vertices E and G are connected to D, so these are now assigned working values. Note that G has
the working value 21 since it is the final value of D plus the value of D to G (9 + 12 = 21); E
keeps the value of 11 since the final value of D plus the value of D to E is greater than 11 (9 + 11
= 20).
Figure 18.5
Vertex E now has the smallest working value (11), so this becomes a final value.
Vertices D, F and G are all connected to E.
D already has a final value so it can be ignored.
F retains its value of 12 since E + E to F = 16 (> 12).
G changes since E + E to G = 17 (< 21).
The diagram now looks like this:
Figure 18.6
Vertex F now has the smallest working value (12), so this becomes a final value.
G retains its value of 17 since F + F to G = 20 (> 17).
The final diagram now looks like this:
Figure 18.7
You will know if the shortest route is correct by applying this rule:
Path length is the same as the difference between final values at either end of the path.
If the path length between two points is not the same as the difference of the final values
between the same two points, then this is not a route that can be taken.
ACTIVITY 18A
The following graph shows the routes through a large park.
Use Dijkstra’s algorithm to find the shortest path from point A to point I.
18.1.2 A* algorithm
Dijkstra’s algorithm simply checks each vertex looking for the shortest path, even if that takes
you away from your destination – it pays no attention to direction. With larger, more complex
problems, that can be a time-consuming drawback.
A* algorithm is based on Dijkstra, but adds an extra heuristic (h) value – an ‘intelligent guess’
on how far we have to go to reach the destination most efficiently.
The A* algorithm can also find shortest path lengths for graphs similar to the type used in
Section 18.1.1.
Suppose we have the following graph made up of an 8 × 6 matrix. White squares show permitted
moves, and grey squares show blocked moves.
Figure 18.9
Each of the parts of the graph are called nodes (or vertices). Each node has four values
• h (the heuristic value)
• g (movement cost)
• f (sum of g and h values)
• n (previous node in the path).
Note that the weight of a node usually represents movement cost, which is the distance between
the two nodes.
First, find the heuristic values (distances) using the Manhattan method (named after the criss-
cross street arrangement in New York). The distance from the starting square (1, 1) to the end
square (8, 6) is 12 squares (follow the purple line in the diagram below). Similarly, the distance
from square (2, 4) to (8, 6) is eight squares (follow the orange line in the diagram below).
Note: you can ignore the blocked moves when calculating heuristic distance from each node to
the final node.
Figure 18.10
Use this method to find the heuristic distances for all permitted nodes:
Figure 18.11
Now, find the g-values (the movement costs). Since we can either move up/down, left/right or
diagonally, we can choose our g-values based on a right-angled triangle. To make the maths easy
we will use a triangle with sides 10, 10 and 14:
Figure 18.12
Figure 18.13
Figure 18.14
Figure 18.15
Figure 18.16
Figure 18.17
ACTIVITY 18B
1 Find the shortest route from node A to node J using the A* algorithm. Note that you are
given the heuristic values (h) in green, and the movement cost values (g), in red.
2 Use the A* algorithm to find the shortest route from the starting point (1, 3) to the end point
(6, 7). Note that you will need to calculate your own h-values and g-values in this question
using the method shown earlier.
3 The following network shows 11 places of interest in a city. The times (in minutes) to walk
between pairs of places of interest are shown on the vertices.
a) Using Dijkstra’s algorithm, find the shortest time to walk from E to G.
b) Write down the corresponding shortest route.
Total walking time is 288 minutes.
4 The road network below connects a number of towns. The distances shown are in kilometres
(km) between roads connecting the towns.
a) i) Use Dijkstra’s algorithm on the road network to find the minimum distance between
towns A and J.
ii) Write down the corresponding shortest route.
b) The road AJ is a dual carriageway where the speed limit is 95 kph.
The speed limit on all other roads is 80 kph.
Assuming Karl drives at the maximum speed limit on each road, calculate the minimum
time to drive from town A to town J.
18.2 Artificial intelligence, machine learning
and deep learning
Key terms
Machine learning – systems that learn without being programmed to learn.
Deep learning – machines that think in a way similar to the human brain. They handle huge
amounts of data using artificial neural networks.
Labelled data – data where we know the target answer and the data object is fully recognised.
Unlabelled data – data where objects are undefined and need to be manually recognised.
Supervised learning – system which is able to predict future outcomes based on past data. It
requires both input and output values to be used in the training process.
Unsupervised learning – system which is able to identify hidden patterns from input data –
the system is not trained on the ‘right’ answer.
Reinforcement learning – system which is given no training – learns on basis of ‘reward and
punishment’.
Semi-supervised (active) learning – system that interactively queries source data to reach the
desired result. It uses both labelled and unlabelled data, but mainly unlabelled data on cost
grounds.
Reward and punishment – improvements to a model based on whether feedback is positive
or negative; actions are optimised to receive an increase in positive feedback.
Web crawler – internet bot that systematically browses the world wide web to update its web
page content.
Artificial neural networks – networks of interconnected nodes based on the interconnections
between neurons in the human brain. The system is able to think like a human using these
neural networks, and its performance improves with more data.
Back propagation – method used in artificial neural networks to calculate error gradients so
that actual node/neuron weightings can be adjusted to improve the performance of the model.
Chatbot – computer program set up to simulate conversational interaction between humans
and a website.
Regression – statistical measure used to make predictions from data by finding learning
relationships between the inputs and outputs.
18.2.1 Artificial Intelligence (AI)
Figure 18.18 shows the link between artificial intelligence (AI), machine learning and deep
learning. Deep learning is a subset of machine learning, which is itself a subset of AI.
Figure 18.18
AI can be thought of as a machine with cognitive abilities such as problem solving and learning
from examples. All of these can be measured against human benchmarks such as reasoning,
speech and sight. AI is often split into three categories.
1 Narrow AI is when a machine has superior performance to a human when doing one specific
task.
2 General AI is when a machine is similar in its performance to a human in any intellectual
task.
3 Strong AI is when a machine has superior performance to a human in many tasks.
Examples of AI include
• news generation based on live news feeds (this will involve some form of human interaction)
• smart home devices (such as Amazon Alexa, Google Now, Apple Siri and Microsoft Cortana);
again these all involve some form of human interaction (see Figure 18.19).
Figure 18.19
In this example, the AI device interacts with a human by recognising their verbal commands.
The device will learn from its environment and the data it receives, becoming increasingly
sophisticated in its responses, showing the ability to use automated repetitive learning via
artificial neural networks.
18.2.2 Machine learning
Machine learning is a subset of AI, in which the algorithms are ‘trained’ and learn from their past
experiences and examples. It is possible for the system to make predictions or even take
decisions based on previous scenarios. They can offer fast and accurate outcomes due to very
powerful processing capability. One of the key factors is the ability to manage and analyse
considerable volumes of complex data – some of the tasks would take humans years, if they were
to analyse the data using traditional computing processing methods. A good example is a search
engine:
Figure 18.20
The search engine will learn from its past performance, meaning its ability to carry out searches
becomes more sophisticated and accurate.
Machine learning is a key part of AI and the various types of machine learning will be covered
later in this chapter.
Now, suppose you want to automatically count the types of birds seen in a bird sanctuary. The
proposed system will consider bird features such as shape of beak, colour of feathers, body size,
and so on. This requires the use of labelled data to allow the birds to be recognised by the system
(see Figure 18.21).
Figure 18.21
Machine learning recognises the birds as labelled data, allowing it to be trained. Once trained, it
is able to recognise each type of bird from the original data set. Algorithms are used to analyse
the incoming data (by comparing it to bird features already recognised by the model) and to learn
from this data. Informed decisions are then made based on what it has learned. Thus, in this
example, it is able to recognise new data and produce an output automatically showing how
many of each type of bird was detected.
Examples of machine learning include
• spam detection (the system learns to recognise spam emails without the need of any human
interactions)
• search engines refining searches based on earlier searches carried out (the system learns from
its mistakes).
Figure 18.23 shows an artificial neural network (with two hidden layers).
Figure 18.23 An artificial neural network
These systems are able to recognise objects, such as birds, by their shape and colour. With
machine learning, the objects form labelled data which can be used in the training process.
But how is it possible to recognise a bird if the data is unlabelled? By analysing pixel densities of
objects, for example, it is possible for a deep learning system to take unlabelled objects and then
recognise them through a sophisticated set of algorithms.
Deep learning using artificial neural networks can be used to recognise objects by looking at the
binary codes of each pixel, thus building up a picture of the object. For example, Figure 18.24
shows a close up of a face where each pixel can be assigned its binary value and, therefore, the
image could be recognised by deep learning algorithms as a person’s face.
Figure 18.24 Deep learning algorithms can recognise this as a person’s face
Large amounts of unlabelled data (data which is undefined and needs to be recognised) is input
into the model. One of the methods of object recognition, using pixel densities, was described
above. Using artificial neural networks, each of the objects is identified by the system. Labelled
data (data which has already been defined and is, therefore, recognised) is then entered into the
model to make sure it gives the correct responses. If the output is not sufficiently accurate, the
model is refined until it gives satisfactory results (known as back propagation – see Section
18.2.6). The refinement process may take several adjustments until it provides reliable and
consistent outputs.
Text mining
Suppose a warehouse contains hundreds of books. A system is being developed to translate the
text from each book and determine which genre the book belongs to, such as a car engine repair
manual. Each book could then be identified by the system and placed in its correct category.
How could this be done using deep learning and machine learning techniques?
Figure 18.26
Photograph enhancement
Some of the latest smartphones cameras use deep learning to give DSLR quality to the
photographs. The technology was developed by taking the same photos using a smartphone and
then using a DSLR camera. The deep learning system was then trained by comparing the two
photographs. A large number of photographs already taken by a DSLR camera (but not by the
smartphone) were then used to test the model. The results can be seen in Figure 18.27.
Figure 18.27 Original photo taken by smartphone (left); enhanced photo using deep learning model (right)
Figure 18.28 Deep learning can change black and white photographs to colour
The deep learning system is trained by searching websites for data which allows it to recognise
features and then map a particular colour to a photograph/object thus producing an accurate
coloured image.
Chatbots
Chatbots interact through instant messaging, artificially replicating patterns of human
interactions using machine learning. Typed messages or voice recordings make use of predefined
scripts and machine learning: when a question is asked, a chatbot responds using the information
known at the time.
Figure 18.29
18.2.4 Comparison between machine learning and deep
learning
machine learning deep learning
enables machines to make decisions enables machines to make decisions using an artificial
on their own based on past data neural network
needs only a small amount of data to the system needs large amounts of data during the
carry out the training training stages
most of the features in the data used deep learning machine learns the features of the data
need to be identified in advance and from the data itself and it does not need to be identified
then manually coded into the system in advance
a modular approach is taken to solve the problem is solved from beginning to end as a single
a given problem/task; each module entity
is then combined to produce the final
model
testing of the system takes a long testing of the system takes much less time to carry out
time to carry out
there are clear rules which explain since the system makes decisions based on its own
why each stage in the model was logic, the reasoning behind those decisions may be
made very difficult to understand (they are often referred to
as a black box)
Table 18.1 Comparison between machine learning and deep learning
18.2.5 What will happen in the future?
Table 18.2 lists some artificial intelligence, machine learning and deep learning developments
expected in the not too distant future.
Figure 18.30
The training program is iterative; the outputs produced from the system are compared to the
expected results and any differences in the two values/results are calculated. These errors are
propagated back into the neural network in order to update the initial network weightings.
This process (training) is repeated until the desired outputs are eventually obtained, or the errors
in the outputs are within acceptable limits.
Here is a summary of the back propagation process:
• The initial outputs from the system are compared to the expected outputs and the system
weightings are adjusted to minimise the difference between actual and expected results.
• Calculus is used to find the error gradient in the obtained outputs: the results are fed back into
the neural networks and the weightings on each neuron are adjusted (note: this can be used in
both supervised and unsupervised networks).
• Once the errors in the output have been eliminated (or reduced to acceptable limits) the neural
network is functioning correctly and the model has been successfully set up.
• If the errors are still too large, the weightings are altered – the process continues until
satisfactory outputs are produced.
Figure 18.31 shows the ultimate goal of the back propagation process.
Figure 18.31
Regression
Machine learning builds heavily on statistics; for example, regression is one way of analysing
data before it is input into a system or model. Regression is used to make predictions from given
data by learning some relationship between the input and the output. It helps in the understanding
of how the value of a dependent variable changes when the values of independent variables are
also changed. This makes it a valuable tool in prediction applications, such as weather
forecasting.
In machine learning, this is used to predict the outcome of an event based on any relationship
between variables obtained from input data and the hidden parameters.
ACTIVITY 18C
1 a) Explain the difference(s) between narrow AI, general AI and strong AI.
b) In machine learning, what is meant by reward and punishment? Give an example of its
use.
c) Explain the term artificial neural networks. Use diagrams to help in your explanation.
2 a) Explain the difference between supervised learning, unsupervised learning,
reinforcement learning and active learning.
b) i) Describe how back propagation (of errors) is used to train an AI model.
ii) Name two types of back propagation.
3 a) Give one use of each of the following.
i) supervised learning
ii) unsupervised learning
iii) reinforcement learning
iv) semi-supervised (active) learning
b) Name the ten terms, i)–x), being described.
i) Intelligent machines that think and behave like human beings.
ii) System that learns without being programmed to learn.
iii) Machines that process information in a similar way to the human brain; they handle
large amounts of data using artificial neural networks.
iv) Data where objects are undefined and need to be manually recognised.
v) An internet bot that systematically browses the world wide web to update its web
content.
vi) A computer program which is set up to automatically simulate a conversational
interaction between a human and a website.
vii) A statistical measure used in artificial neural networks to calculate error gradients so
that actual neuron weightings can be adjusted to improve the performance of the
model.
viii) A statistical measure used to make predictions from data by finding learning
relationships between input and output values.
ix) Data where we know the target answer and data objects are fully recognised and
identified.
x) Improvements made to a model based on negative and positive feedback: actions are
optimised to increase the amount of positive feedback.
7 The following graph shows the routes connecting buildings on a university campus. The
numbers represent the time taken (in minutes) to cycle from one building to another.
a) i) Use Dijkstra’s algorithm to find the minimum time to cycle from building A to
building L.
[8]
ii) Write down the corresponding shortest route.
[1]
b) It has been decided to construct a new cycle path, either from A directly to D (cycle time
30 minutes) or from A directly to I (cycle time 20 minutes).
Identify the option that would reduce the cycle time from building A to building L by the
greatest amount.
[4]
19 Computational thinking and problem
solving
In this chapter, you will learn about
• how to write algorithms to implement linear and binary searches
• the conditions necessary for the use of a binary search
• how the performance of a binary search varies according to the number of data items
• how to write algorithms to implement insertion and bubble sorts
• the use of abstract data types (ADTs) including finding, inserting and deleting items from
linked lists, binary trees, stacks and queues
• the key features of a graph and why graphs are used
• how to implement ADTs from other ADTs
• the comparison of algorithms including the use of Big O notation
• how recursion is expressed in a programming language
• when to use recursion
• writing recursive algorithms
• how a compiler implements recursion in a programming language.
19.1 Algorithms
WHAT YOU SHOULD ALREADY KNOW
In Chapter 10 you learnt about Abstract Data Types (ADTs) and searching and sorting arrays.
You should also have been writing programs in your chosen programming language (Python,
VB or Java). Try the following five questions to refresh your memory before you start to read
this chapter.
1 Explain what is meant by the terms
a) stack
b) queue
c) linked list.
2 a) Describe how it is possible to implement a stack using an array.
Key terms
Binary search – a method of searching an ordered list by testing the value of the middle item
in the list and rejecting the half of the list that does not contain the required value.
Insertion sort – a method of sorting data in an array into alphabetical or numerical order by
placing each item in turn in the correct position in the sorted list.
Binary tree – a hierarchical data structure in which each parent node can have a maximum of
two child nodes.
Graph – a non-linear data structure consisting of nodes and edges.
Dictionary – an abstract data type that consists of pairs, a key and a value, in which the key is
used to find the value.
Big O notation – a mathematical notation used to describe the performance or complexity of
an algorithm.
19.1.1 Understanding linear and binary searching
methods
Linear search
In Chapter 10, we looked at the linear search method of searching a list. In this method, each
element of an array is checked in order, from the lower bound to the upper bound, until the item
is found, or the upper bound is reached.
The pseudocode linear search algorithm and identifier table to find if an item is in the populated
1D array myList from Chapter 10 is repeated here.
Identifier Description
myList Array to be searched
upperBound Upper bound of the array
lowerBound Lower bound of the array
index Pointer to current array element
item Item to be found
found Flag to show when item has been found
Table 19.1
This method works for a list in which the items can be stored in any order, but as the size of the
list increases, the average time taken to retrieve an item increases correspondingly.
The Cambridge International AS & A Level Computer Science syllabus requires you to be able
to write code in one of the following programming languages: Python, VB and Java. It is very
important to practice writing different routines in the programming language of your choice; the
more routines you write, the easier it is to write programming code that works.
Here is a simple linear search program written in Python, VB and Java using a FOR loop.
Python
VB
Java
ACTIVITY 19A
Write the linear search in the programming language you are using, then change the code to
use a similar type of loop that you used in the pseudocode at the beginning of Section 19.1.1,
Linear search.
Binary search
A binary search is more efficient if a list is already sorted. The value of the middle item in the
list is first tested to see if it matches the required item, and the half of the list that does not
contain the required item is discarded. Then, the next item of the list to be tested is the middle
item of the half of the list that was kept. This is repeated until the required item is found or there
is nothing left to test.
For example, consider a list of the letters of the alphabet.
To find the letter W using a binary search there could be just three comparisons.
ACTIVITY 19B
Check how many comparisons for each type of search it takes to find the letter D. Find any
letters where the linear search would take less comparisons than the binary search.
A binary search usually takes far fewer comparisons than a linear search to find an item in a list.
For example, if a list had 1024 elements, the maximum number of comparisons for a binary
search would be 16, whereas a linear search could take up to 1024 comparisons.
Here is the pseudocode for the binary search algorithm to find if an item is in the populated 1D
array myList. The identifier table is the same as the one used for the linear search.
Identifier Description
myList Array to be searched
upperBound Upper bound of the array
lowerBound Lower bound of the array
index Pointer to current array element
item Item to be found
found Flag to show when item has been found
Table 19.2
The code structure for a binary search is very similar to the linear search program shown for each
of the programming languages. You will need to populate myList before searching for an item,
as well as the variables found, lowerBound and upperBound.
You will need to use a conditional loop like those shown in the table below.
Loop Language
Python uses a condition to
repeat the loop at the start of
the loop
VB uses a condition to stop the
loop at the end of the loop
Table 19.3
You will need to use If statements like those shown in the table below to test if the item is found,
or to decide which part of myList to use next, and to update the upperBound or lowerBound
accordingly.
If Language
Python using integer division
Table 19.4
ACTIVITY 19C
In your chosen programming language, write a short program to complete the binary search.
Use this sample data:
16, 19, 21, 27, 36, 42, 55, 67, 76, 89
Search for the values 19 and 77 to test your program.
19.1.2 Understanding insertion and bubble sorting
methods
Bubble sort
In Chapter 10, we looked at the bubble sort method of sorting a list. This is a method of sorting
data in an array into alphabetical or numerical order by comparing adjacent items and swapping
them if they are in the wrong order.
The bubble sort algorithm and identifier table to sort the populated 1D array myList from
Chapter 10 is repeated here.
Identifier Description
myList Array to be searched
upperBound Upper bound of the array
lowerBound Lower bound of the array
index Pointer to current array element
swap Flag to show when swaps have been made
top Index of last element to compare
temp Temporary storage location during swap
Table 19.5
Here is a simple bubble sort program written in Python, VB and Java, using a pre-condition loop
and a FOR loop in Python and post-condition loops and FOR loops in VB and Java.
Python
VB
Java
Insertion sort
The bubble sort works well for short lists and partially sorted lists. An insertion sort will also
work well for these types of list. An insertion sort sorts data in a list into alphabetical or
numerical order by placing each item in turn in the correct position in a sorted list. An insertion
sort works well for incremental sorting, where elements are added to a list one at a time over an
extended period while keeping the list sorted.
Here is the pseudocode and the identifier table for the insertion sort algorithm sorting the
populated 1D array myList.
Identifier Description
myList Array to be searched
upperBound Upper bound of the array
lowerBound Lower bound of the array
index Pointer to current array element
key Element being placed
place Position in array of element being moved
Table 19.6
Figure 19.3 shows the changes to the 1D array myList as the insertion sort is completed.
Figure 19.3
The element shaded blue is being checked and placed in the correct position. The elements
shaded yellow are the other elements that also need to be moved if the element being checked is
out of position. When sorting the same array, myList, the insert sort made 21 swaps and the
bubble sort shown in Chapter 10 made 38 swaps. The insertion sort performs better on partially
sorted lists because, when each element is found to be in the wrong order in the list, it is moved
to approximately the right place in the list. The bubble sort will only swap the element in the
wrong order with its neighbour.
As the number of elements in a list increases, the time taken to sort the list increases. It has been
shown that, as the number of elements increases, the performance of the bubble sort deteriorates
faster than the insertion sort.
The code structure for an insertion sort in each of the programming languages is very similar to
the bubble sort program. You will need to assign values to lowerBound and upperBound and use
a nested loop like those shown in the table below.
Nested loop Language
Python
VB
cannot use
key as a
variable
Java
Table 19.7
ACTIVITY 19D
In your chosen programming language write a short program to complete the insertion sort.
Stacks
In Chapter 10, we looked at the data and the operations for a stack using pseudocode. You will
need to be able to write a program to implement a stack. The data structures and operations
required to implement a similar stack using a fixed length integer array and separate sub routines
for the push and pop operations are set out below in each of the three prescribed programming
languages. If you are unsure how the operations work, look back at Chapter 10.
Python
empty
stack with
no
elements
VB
empty
stack with
no
elements
and
variables
set to
public for
subroutine
access
Java
empty
stack with
no
elements
and
variables
set to
public for
subroutine
access
Table 19.8
Python
global
used
within
subroutine
to access
variables
topPointer
points to
the top of
the stack
VB
topPointer
points to
the top of
the stack
Java
topPointer
points to
the top of
the stack
Table 19.9
VB
Java
Table 19.10
ACTIVITY 19E
In your chosen programming language, write a program using subroutines to implement a
stack with 10 elements. Test your program by pushing two integers 7 and 32 onto the stack,
popping these integers off the stack, then trying to remove a third integer, and by pushing the
integers 1, 2, 3, 4, 5, 6, 7, 8, 9 and 10 onto the stack, then trying to push 11 on to the stack.
Queues
In Chapter 10, we looked at the data and the operations for a circular queue using pseudocode.
You will need to be able to write a program to implement a queue. The data structures and
operations required to implement a similar queue using a fixed length integer array and separate
sub routines for the enqueue and dequeue operations are set out below in each of the three
programing languages. If you are unsure how the operations work, look back at Chapter 10.
Python
empty
queue
with no
items
VB
empty
queue
with no
items and
variables,
set to
public for
subroutine
access
Java
empty
queue
with no
elements
and
variables,
set to
public for
subroutine
access
Table 19.11
Queue enqueue (add item to queue) operation Language
Python
global used
within
subroutine to
access
variables
If the
rearPointer is
pointing to the
last element of
the array and
the queue is
not full, the
item is stored
in the first
element of the
array
VB
If the
rearPointer is
pointing to the
last element of
the array and
the queue is
not full, the
item is stored
in the first
element of the
array
Java
If the
rearPointer is
pointing to the
last element of
the array and
the queue is
not full, the
item is stored
in the first
element of the
array
Table 19.12
Python
If the
frontPointer
points to the
last element
in the array
and the
queue is not
empty, the
pointer is
updated to
point at the
first item in
the array
rather than
the next item
in the array
VB
If the
frontPointer
points to the
last element
in the array
and the
queue is not
empty, the
pointer is
updated to
point at the
first item in
the array
rather than
the next item
in the array
Java
If the
frontPointer
points to the
last element
in the array
and the
queue is not
empty, the
pointer is
updated to
point at the
first item in
the array
rather than
the next item
in the array
Table 19.13
ACTIVITY 19F
In your chosen programming language, write a program using subroutines to implement a
queue with 10 elements. Test your program by adding two integers 7 and 32 to the queue,
removing these integers from the queue, then trying to remove a third integer, and by adding
the integers 1, 2, 3, 4, 5, 6, 7, 8, 9 and 10 to the queue then trying to add 11 to the queue.
Linked lists
Finding an item in a linked list
In Chapter 10, we looked at defining a linked list as an ADT; now we need to consider writing
algorithms using a linked list. Here is the declaration algorithm and the identifier table from
Chapter 10.
The above code sets up a linked list ready for use. The identifier table is below.
Identifier Description
myLinkedList Linked list to be searched
myLinkedListPointers Pointers for linked list
startPointer Start of the linked list
heapStartPointer Start of the heap
index Pointer to current element in the linked list
Table 19.14
Figure 19.5 below shows an empty linked list and its corresponding pointers.
Figure 19.5
Figure 19.6 below shows a populated linked list and its corresponding pointers.
Figure 19.6
The algorithm to find if an item is in the linked list myLinkedList and return the pointer to the
item if found or a null pointer if not found, could be written as a function in pseudocode as
shown below.
The following programs use a function to search for an item in a populated linked list.
Python
VB
Java
The trace table below shows the algorithm being used to search for 42 in myLinkedList.
startPointer itemPointer searchItem
Already set to 4 4 42
3
Table 19.15 Trace table
ACTIVITY 19G
In the programming language of your choice, use the code given to write a program to set up
the populated linked list and find an item stored in it.
Identifier Description
startPointer Start of the linked list
heapStartPointer Start of the heap
nullPointer Null pointer set to -1
itemAdd Item to add to the list
tempPointer Temporary pointer
Table 19.16
Figure 19.7 below shows the populated linked list and its corresponding pointers again.
Figure 19.7
The trace table below shows the algorithm being used to add 18 to myLinkedList.
VB
Java
ACTIVITY 19H
Use the algorithm to add 25 to myLinkedList. Show this in a trace table and show
myLinkedList once 25 has been added. Add the insert procedure to your program, add code to
input an item, add this item to the linked list then print out the list and the pointers before and
after the item was added.
Identifier Description
startPointer Start of the linked list
heapStartPointer Start of the heap
nullPointer Null pointer set to −1
index Pointer to current list element
oldIndex Pointer to previous list element
itemDelete Item to delete from the list
tempPointer Temporary pointer
Figure 19.18
The trace table below shows the algorithm being used to delete 36 from myLinkedList.
Figure 19.9
Binary trees
A binary tree is another frequently used ADT. It is a hierarchical data structure in which each
parent node can have a maximum of two child nodes. There are many uses for binary trees; for
example, they are used in syntax analysis, compression algorithms and 3D video games.
Figure 19.10 shows the binary tree for the data stored in myList sorted in ascending order. Each
item is stored at a node and each node can have up to two branches with the rule if the value to
be added is less than the current node branch left, if the value to be added is greater than or equal
to the current node branch right.
ACTIVITY 19J
Draw the binary tree for the expression (x – y) / ( x * y + z).
The data structure for an ordered binary tree can be created in pseudocode as follows:
ACTIVITY 19K
Create the data structure in pseudocode for a binary tree to store a list of names. Your list must
be able to store at least 50 names.
Figure 19.12
The root pointer points to the first node in a binary tree. A null pointer is a value stored in the left
or right pointer in a binary tree to indicate that there are no nodes below this node on the left or
right.
Finding an item in a binary tree
The algorithm to find if an item is in the binary tree myTree and return the pointer to its node if
found or a null pointer if not found, could be written as a function in pseudocode, as shown.
Here is the identifier table for the binary tree search algorithm shown above.
Identifier Description
myTree Tree to be searched
node ADT for tree
rootPointer Pointer to the start of the tree
leftPointer Pointer to the left branch
rightPointer Pointer to the right branch
nullPointer Null pointer set to −1
itemPointer Pointer to current item
itemSearch Item being searched for
Table 19.20
The trace table below shows the algorithm being used to search for 42 in myTree.
ACTIVITY 19L
Use the algorithm to search for 55 and 75 in myTree. Show the results of each search in a trace
table.
Figure 19.13
The algorithm to insert an item at a new node in the binary tree myTree could be written as a
procedure in pseudocode as shown below.
Here is the identifier table.
Identifier Description
myTree Tree to be searched
node ADT for tree
rootPointer Pointer to the start of the tree
leftPointer Pointer to the left branch
rightPointer Pointer to the right branch
nullPointer Null pointer set to -1
itemPointer Pointer to current item in tree
itemAdd Item to add to tree
nextFreePointer Pointer to next free node
itemAddPointer Pointer to position in tree to store item to be added
oldPointer Pointer to leaf node that is going to point to item added
leftBranch Flag to identify whether to go down the left branch or the right branch
Table 19.22
The trace table below shows the algorithm being used to add 18 to myTree.
Table 19.23
ACTIVITY 19M
Use the algorithm to add 25 to myTree. Show this in a trace table and show myTree once 25
has been added.
Implementing binary trees in Python, VB.NET or Java requires the use of objects and recursion.
An example will be given in Chapter 20.
Graphs
A graph is a non-linear data structure consisting of nodes and edges. This is an ADT used to
implement directed and undirected graphs. A graph consists of a set of nodes and edges that join
a pair of nodes. If the edges have a direction from one node to the other it is a directed graph.
Figure 19.15
As we saw in Chapter 18, graphs are used to represent real life networks, such as
• bus routes, where the nodes are bus stops and the edges connect two stops next to each other
• websites, where each web page is a node and the edges show the links between each web page
• social media networks, where each node contains information about a person and the edges
connect people who are friends.
Each edge may have a weight; for example, in the bus route, the weight could be the distance
between bus stops or the cost of the bus fare.
A path is the list of nodes connected by edges between two given nodes and a cycle is a list of
nodes that return to the same node.
For example, a graph of the bus routes in a town could be as follows. The distance between each
bus stop in kilometres is shown on the graph.
Figure 19.16
ACTIVITY 19N
Find another path from School to Gardens. Find the shortest path from Town centre to Train
station. Find the shortest cycle from the Town centre.
A path from School to Gardens could be Path = (School, Train station, River, Gardens).
19.1.4 Implementing one ADT from another ADT
Every ADT is a collection of data and the methods used on the data. When an ADT is defined,
the definition can refer to other data types. For example, myLinkedList refers to the data type
INTEGER in its data definition.
A linked list type could be defined as follows.
ACTIVITY 19O
Write pseudocode to declare a linked list to store names. Use this to write pseudocode to set up
a linked list that will store 30 names. Write a program to store and display names in this linked
list.
The data types for a stack, queue and a binary tree have been defined using existing data types.
Another data type is a dictionary, which is an ADT that consists of pairs consisting of a key and
a value, where the key is used to find the value. Each key can only appear once. Keys in a
dictionary are unordered. A value is retrieved from a dictionary by specifying its corresponding
key. The same value may appear more than once. A dictionary differs from a set because the
values can be duplicated. As a dictionary is not an ordered list, it can be declared using a linked
list as part of the definition.
A dictionary type could be defined in pseudocode as follows.
And then used as follows.
Each of the programming languages used in Cambridge International A Level Computer Science
provide a dictionary data type, as shown in the table below.
VB
Java Dictionary is no
longer used in Java but can
be implemented using a
hash table
Table 19.24
ACTIVITY 19P
In the programming language of your choice, write a program to use a dictionary to store the
names of students as their keys and their examination scores as their values. Then find a
student’s examination score, add a student and score and delete a student and score.
19.1.5 Comparing algorithms
Big O notation is a mathematical notation used to describe the performance or complexity of an
algorithm in relation to the time taken or the memory used for the task. It is used to describe the
worst-case scenario; for example, how the maximum number of comparisons required to find a
value in a list using a particular search algorithm increases with the number of values in the list.
ACTIVITY 19Q
1 Using diagrams, describe the structure of
a) a binary tree
b) a linked list.
2 a) Explain what is meant by a dictionary data type.
b) Show how a dictionary data type can be constructed from a linked list.
3 Compare the performance of a linear search and a binary search using Big O notation.
19.2 Recursion
WHAT YOU SHOULD ALREADY KNOW
Remind yourself of the definitions of the following mathematical functions, which many of
you will be familiar with, and see how they are constructed.
• Factorials
• Arithmetic sequences
• Fibonacci numbers
• Compound interest
Key terms
Recursion – a process using a function or procedure that is defined in terms of itself and calls
itself.
Base case – a terminating solution to a process that is not recursive.
General case – a solution to a process that is recursively defined.
Winding – process which occurs when a recursive function or procedure is called until the
base case is found.
Unwinding – process which occurs when a recursive function finds the base case and the
function returns the values.
19.2.1 Understanding recursion
Recursion is a process using a function or procedure that is defined in terms of itself and calls
itself. The process is defined using a base case, a terminating solution to a process that is not
recursive, and a general case, a solution to a process that is recursively defined.
For example, a function to calculate a factorial for any positive whole number n! is recursive.
The definition for the function uses:
With recursive functions, the statements after the recursive function call are not executed until
the base case is reached; this is called winding. After the base case is reached and can be used in
the recursive process, the function is unwinding.
In order to understand how the winding and unwinding processes in recursion work, we can use
a trace table for a specific example: 3!
Table 19.27
Here is a simple recursive factorial program written in Python, VB and Java using a function.
Python
VB
Java
ACTIVITY 19R
Write the recursive factorial function in the programming language of your choice. Test your
program with 0! and 5!
Complete trace tables for 0! and 5! using the recursive factorial function written in pseudocode
and compare the results from your program with the trace tables.
Compound interest can be calculated using a recursive function. Where the principal is the
amount of money invested, rate is the rate of interest and years is the number of years the money
has been invested.
Table 19.29
ACTIVITY 19S
The Fibonacci series is defined as a sequence of numbers in which the first two numbers are 0
and 1, depending on the selected beginning point of the sequence, and each subsequent number
is the sum of the previous two.
Identify the base case and the general case for this series. Write a pseudocode algorithm to find
and output the nth term. Test your algorithm by drawing a trace table for the fourth term.
Benefits of recursion
Recursive solutions can contain fewer programming statements than an iterative solution. The
solutions can solve complex problems in a simpler way than an iterative solution. However, if
recursive calls to procedures and functions are very repetitive, there is a very heavy use of the
stack, which can lead to stack overflow. For example, factorial(100) would require 100 function
calls to be placed on the stack before the function unwinds.
19.2.2 How a compiler implements recursion
Recursive code needs to make use of the stack; therefore, in order to implement recursive
procedures and functions in a high-level programming language, a compiler must produce object
code that pushes return addresses and values of local variables onto the stack with each recursive
call, winding. The object code then pops the return addresses and values of local variables off the
stack, unwinding.
ACTIVITY 19T
1 Explain what is meant by recursion and give the benefits of using recursion in
programming.
2 Explain why a compiler needs to produce object code that uses the stack for a recursive
procedure.
ii) A special case is when NameList is already in order. The algorithm in part a) i) is
applied to this special case.
Explain how many iterations are carried out for each of the loops.
[3]
b) An alternative sort algorithm is a bubble sort:
i) As in part a) ii), a special case is when NameList is already in order. The algorithm in
part b) is applied to this special case.
Explain how many iterations are carried out for each of the loops.
[2]
ii) Rewrite the algorithm in part b), using pseudocode, to reduce the number of
unnecessary comparisons.
Use the same variable names where appropriate.
[5]
Cambridge International AS & A Level Computer Science 9608
Paper 41 Q5 June 2015
2 A Queue Abstract Data type (ADT) has these associated operations:
– create queue
– add item to queue
– remove item from queue
The queue ADT is to be implemented as a linked list of nodes.
Each node consists of data and a pointer to the next node.
a) The following operations are carried out:
Copy the diagram and add appropriate labels to show the final state of the queue. Use the
space on the left as a workspace.
Show your final answer in the node shapes on the right.
[3]
The statement
Key terms
Programming paradigm – a set of programming concepts.
Low-level programming – programming instructions that use the computer’s basic instruction
set.
Imperative programming – programming paradigm in which the steps required to execute a
program are set out in the order they need to be carried out.
Object-oriented programming (OOP) – a programming methodology that uses self-
contained objects, which contain programming statements (methods) and data, and which
communicate with each other.
Class – a template defining the methods and data of a certain type of object.
Attributes (class) – the data items in a class.
Method – a programmed procedure that is defined as part of a class.
Encapsulation – process of putting data and methods together as a single unit, a class.
Object – an instance of a class that is self-contained and includes data and methods.
Property – data and methods within an object that perform a named action.
Instance – An occurrence of an object during the execution of a program.
Data hiding – technique which protects the integrity of an object by restricting access to the
data and methods within that object.
Inheritance – process in which the methods and data from one class, a superclass or base
class, are copied to another class, a derived class.
Polymorphism – feature of object-oriented programming that allows methods to be redefined
for derived classes.
Overloading – feature of object-oriented programming that allows a method to be defined
more than once in a class, so it can be used in different situations.
Containment (aggregation) – process by which one class can contain other classes.
Getter – a method that gets the value of a property.
Setter – a method used to control changes to a variable.
Constructor – a method used to initialise a new object.
Destructor – a method that is automatically invoked when an object is destroyed.
Declarative programming – statements of facts and rules together with a mechanism for
setting goals in the form of a query.
Fact – a ‘thing’ that is known.
Rules – relationships between facts.
ACTIVITY 20A
A section of memory in a computer contains these denary values:
Give the value stored in the accumulator (ACC) and the index register (IX) after each of these
instructions have been executed and state the mode of addressing used.
ACTIVITY 20B
Write a pseudocode algorithm to calculate the areas of five different shapes (square, rectangle,
triangle, parallelogram and circle) using the basic imperative programming paradigm (no
procedures or functions, and using only global variables).
Rewrite the pseudocode algorithm in a more structured way using the procedural programming
paradigm (make sure you use procedures, functions, and local and global variables).
Write and test both algorithms using the programming language of your choice.
20.1.3 Object-oriented programming (OOP)
Object-oriented programming (OOP) is a programming methodology that uses self-contained
objects, which contain programming statements (methods) and data, and which communicate
with each other. This programming paradigm is often used to solve more complex problems as it
enables programmers to work with real life things. Many procedural programming languages
have been developed to support OOP. For example, Java, Python and Visual Basic all allow
programmers to use either procedural programming or OOP.
Object-oriented programming uses its own terminology, which we will explore here.
Class
A class is a template defining the methods and data of a certain type of object. The attributes are
the data items in a class. A method is a programmed procedure that is defined as part of a class.
Putting the data and methods together as a single unit, a class, is called encapsulation. To ensure
that only the methods declared can be used to access the data within a class, attributes need to be
declared as private and the methods need to be declared as public.
For example, a shape can have name, area and perimeter as attributes and the methods set
shape, calculate area, calculate perimeter. This information can be shown in a class
diagram (Figure 20.1).
Object
When writing a program, an object needs to be declared using a class type that has already been
defined. An object is an instance of a class that is self-contained and includes data and methods.
Properties of an object are the data and methods within an object that perform named actions.
An occurrence of an object during the execution of a program is called an instance.
For example, a class employee is defined and the object myStaff is instanced in these programs
using Python, VB and Java.
Python
VB
Java
Data hiding protects the integrity of an object by restricting access to the data and methods
within that object. One way of achieving data hiding in OOP is to use encapsulation. Data hiding
reduces the complexity of programming and increases data protection and the security of data.
Here is an example of a definition of a class with private attributes in Python, VB and Java.
Python
VB
Java
ACTIVITY 20C
Write a short program to declare a class, student, with the private attributes name,
dateOfBirth and examMark, and the public method displayExamMark. Declare an object
myStudent, with a name and exam mark of your choice, and use your method to display the
exam mark.
Inheritance
Inheritance is the process by which the methods and data from one class, a superclass or base
class, are copied to another class, a derived class.
Figure 20.2 shows single inheritance, in which a derived class inherits from a single superclass.
Multiple inheritance is where a derived class inherits from more than one superclass (Figure
20.3).
Python
VB
Java
Figure 20.4 shows the inheritance diagram for the base class employee and the derived classes
partTime and fullTime.
ACTIVITY 20D
Write a short program to declare a class, student, with the private attributes name,
dateOfBirth and examMark, and the public method displayExamMark.
VB
Java
ACTIVITY 20E
Write a short program to declare the class shape with the public method area.
Declare the derived classes circle, rectangle and square.
Use polymorphism to redefine the method area for these derived classes.
Declare objects for each derived class and instance them with suitable data.
Use your methods to display the areas for these shapes.
Example of overloading
One way of overloading a method is to use the method with a different number of parameters.
For example, a class greeting is defined with the method hello. The object myGreeting is
instanced and uses this method with no parameters or one parameter in this Python program.
This is how Python, VB and Java manage overloading.
Python
VB
Java
ACTIVITY 20F
Write a short program to declare the class greeting, with the public method hello, which can
be used without a name, with one name or with a first name and last name.
Declare an object and use the method to display each type of greeting.
Containment
Containment, or aggregation, is the process by which one class can contain other classes. This
can be presented in a class diagram.
When the class ‘aeroplane’ is defined, and the definition contains references to the classes – seat,
fuselage, wing, cockpit – this is an example of containment.
Figure 20.5
When deciding whether to use inheritance or containment, it is useful to think about how the
classes used would be related in the real world.
For example
• when looking at shapes, a circle is a shape – so inheritance would be used
• when looking at the aeroplane, an aeroplane contains wings – so containment would be used.
Consider the people on board an aeroplane for a flight. The containment diagram could look like
this if there can be up to 10 crew and 350 passengers on board:
Figure 20.6
ACTIVITY 20G
Draw a containment diagram for a course at university where there are up to 50 lectures, three
examinations and the final mark is the average of the marks for the three examinations.
Object methods
In OOP, the basic methods used during the life of an object can be divided into these types:
constructors, setters, getters, and destructors.
A constructor is the method used to initialise a new object. Each object is initialised when a new
instance is declared. When an object is initialised, memory is allocated.
For example, in the first program in Chapter 20, this is the method used to construct a new
employee object.
Constructor Language
Python
VB
Table 20.1
A setter is a method used to control changes to any variable that is declared within an object.
When a variable is declared as private, only the setters declared within the object’s class can be
used to make changes to the variable within the object, thus making the program more robust.
For example, in the employee base class, this code is a setter:
Setter Language
Python
VB
Java
Table 20.3
Getter Language
Python
VB
Java
Table 20.4
A destructor is a method that is invoked to destroy an object. When an object is destroyed the
memory is released so that it can be reused. Both Java and VB use garbage collection to
automatically destroy objects that are no longer used so that the memory used can be released. In
VB garbage collection can be invoked as a method if required but it is not usually needed.
For example, in any of the Python programs above, this could be used as a destructor:
Destructor Language
Python
VB – only if required, automatically called at
end of program
Table 20.6
VB – the function
returns the value
searched for if it is
found, otherwise it
returns Nothing
Java – the function
returns the value
searched for if it is
found, otherwise it
returns null
Table 20.9
ACTIVITY 20H
In your chosen programming language, write a program using objects and recursion to
implement a binary tree. Test your program by setting the root of the tree to 27, then adding the
integers 19, 36, 42 and 16 in that order.
Declarative programming uses statements of facts and rules together with a mechanism for
setting goals in the form of a query. A fact is a ‘thing’ that is known, and rules are relationships
between facts. Writing declarative programs is very different to writing imperative programs. In
imperative programming, the programmer writes a list of statements in the order that they will be
performed. But in declarative programming, the programmer can state the facts and rules in any
order before writing the query.
Prolog is a declarative programming language that uses predicate logic to write facts and rules.
For example, the fact that France is a country would be written in predicate logic as:
Note that all facts in Prolog use lower-case letters and end with a full stop.
Another fact about France – the language spoken in France is French – could be written as:
Note that a variable in Prolog – Country, in this example – begins with an uppercase-letter.
This would give the following results:
The results are usually shown in the order the facts are stored in the knowledge base.
A query about the languages spoken in a country, Switzerland, could look like this:
When a query is written as a statement, this statement is called a goal and the result is found
when the goal is satisfied using the facts and rules available.
ACTIVITY 20I
Use the facts above to write queries to find out which language is spoken in England and
which country speaks Japanese. Take care with the use of capital letters.
The results for the country Switzerland query would look like this in SWI-Prolog:
Most knowledge bases also make use of rules, which are also written using predicate logic.
Here is a knowledge base for the interest paid on bank accounts. The facts about each account
include the name of the account holder, the type of account (current or savings), and the amount
of money in the account. The facts about the interest rates are the percentage rate, the type of
account and the amount of money needed in the account for that interest rate to be paid.
ACTIVITY 20J
Carry out the following activities using the information above.
1 Write a query to find out the interest rate for Laila’s bank account.
2 Write a query to find who has savings accounts.
3 a) Set up a savings account for Robert with 300.00.
b) Set up a new fact about savings accounts allowing for an interest rate of 7% if there is
2000.00 or more in a savings account.
ACTIVITY 20K
1 Explain the difference between the four modes of addressing in a low-level programming
language. Illustrate your answer with assembly language code for each mode of addressing.
2 Compare and contrast the use of imperative (procedural) programming with OOP. Use the
shape programs you developed in Activities 20B and 20E to illustrate your answer with
examples to show the difference in the paradigms.
3 Use the knowledge base below to answer the following questions:
a) Write two new facts about Java, showing that it is a high-level language and uses OOP.
b) Show the results from these queries
i) teaching(X).
ii) teaching(masm).
c) Write a query to show all programming languages translated by an assembler.
20.2 File processing and exception handling
WHAT YOU SHOULD ALREADY KNOW
In Chapter 10, Section 10.3, you learnt about text files, and in Chapter 13, Section 13.2, you
learnt about file organisation and access. Review these sections, then try these three questions
before you read the second part of this chapter.
1 a) Write a program to set up a text file to store records like this, with one record on every
line.
Key terms
Read – file access mode in which data can be read from a file.
Write – file access mode in which data can be written to a file; any existing data stored in the
file will be overwritten.
Append – file access mode in which data can be added to the end of a file.
Open – file-processing operation; opens a file ready to be used in a program.
Close – file-processing operation; closes a file so it can no longer be used by a program.
Exception – an unexpected event that disrupts the execution of a program.
Exception handling – the process of responding to an exception within the program so that the
program does not halt unexpectedly.
20.2.1 File processing operations
Files are frequently used to store records that include data types other than string. Also, many
programs need to handle random access files so that a record can be found quickly without
reading through all the preceding records.
A typical record to be stored in a file could be declared like this in pseudocode:
If a sequential file was required, then the student records would need to be input into an array of
records first, then sorted on the key field registerNumber, before the array of records was
written to the file.
Here are programs in Python, VB and Java to write a single record to a file.
Python
VB
Java
(Java programs using files need to include exception handling – see Section 20.2.2 later in this
chapter.)
ACTIVITY 20L
In the programming language of your choice, write a program to
• input a student record and save it to a new serial file
• read a student record from that file
• extend your program to work for more than one record.
Note that you can directly append a record to the end of a file in a programming language by
opening the file in append mode, as shown in the table below.
ACTIVITY 20M
In the programming language of your choice, write a program to
• put a student record and append it to the end of a sequential file
• find and output a student record from a sequential file using the key field to identify the
record
• extend your program to check for record not found (if required).
The algorithm written in pseudocode below inserts a student record into a random file.
EXTENSION ACTIVITY 20G
In the programming language of your choice, write a program to input a student record and
save it to a random file.
Here are programs in Python, VB and Java to catch an integer division by zero exception.
Python
VB
Java
ACTIVITY 20N
In the programming language of your choice, write a program to check that a value input is an
integer.
ACTIVITY 20O
In the programming language of your choice, extend the file handling programs you wrote in
Section 20.2.1 to use exception handling to ensure that the files used exist and allow for the
condition unexpected end of file.
Clause Explanation
01 Ahmed is male
05 Aisha is female
08 Ahmed is a parent of Raul
14 A is the mother of B if A is female and A is a parent of B
returns
Write the result returned by the goal
[2]
c) Use the variable M to write the goal to find the mother of Gina.
[1]
d) Write the rule to show that F is the father of C.
[2]
i) State the pseudocode line number which causes the exception to be raised.
[1]
ii) Explain the purpose of the pseudocode on lines 11 and 12.
[3]
Cambridge International AS & A Level Computer Science 9608
Paper 42 Q5(b)–(d) June 2016