You are on page 1of 50

Computer Science Fundamentals

FPT University 1

Agenda
1.1 Basic Theory of Information
1.2 Information and Logic
1.3 Data Structures
1.4 Algorithms

FPT University 2

Basic Theory of Information


Radix Conversion
Octal: 3 bits (0 through 7)
Hexadecimal: 4 bits (0 through F)

FPT University 3

1
Conversion of Decimal Integers to
Binary Numbers
The number is divided by 2 repeatedly until the
quotient becomes 0.

FPT University 4

Decimal fractions to binary


The number is multiplied by 2 repeatedly until
the quotient becomes 0.
Repeating binary fraction

FPT University 5

Binary to Hexa
group the binary number into blocks of 4 bits,
starting from the lowest bit (rightmost bit), and
then assign the corresponding hexadecimal digit
for each block

FPT University 6

2
Hexa to Binary
Assign the corresponding 4-bit binary number to
each digit of the given hexadecimal number.

FPT University 7

Decimal to Hexa
convert the decimal number to binary first, and
then convert the binary number to hexadecimal
number

FPT University 8

Q->
Hexa to Decimal
convert the hexadecimal number to binary first,
and then convert the binary number to decimal

FPT University 9

3
FAQ
There are many questions mixing multiple radices
(bases) such as Which of the following is the correct
result
(in decimal) of adding the hexadecimal and binary
numbers?
If the final result is to be represented in decimal, it is
better that you convert the original numbers to decimal
first and then calculate it.
If the final result is to be represented in a radix other
than 10 (binary, octal, hexadecimal, etc.), it is better that
you convert the original numbers to binary first and then
carry on the calculation

FPT University 10

Numerical Representations
Decimal numbers are represented in packed or
zoned format.
Binary numbers are represented in fixed-point or
floating-point format.

FPT University 11

Numerical Representations

FPT University 12

4
Decimal Number Representation
Zoned decimal format
Each number is represented by 1 byte (8 bits)

FPT University 13

Decimal Number Representation


Packed decimal format
Each number is represented by 4 bits
Padded by 4 leading 0s

FPT University 14

Fixed-Point Number Representation


Fixed-length binary
2s complement for negative values
-2n-1 through 2n-1 1, a total of 2n numbers, can
be represented by using n bits.

FPT University 15

5
Twos complement

FPT University 16

Floating-Point Number
Representation
A real number is represented in exponential
form ( a = +m x r e ) using a fixed-length binary
number
IEEE 754

FPT University 17

Floating point number


Overflow
This means that the positive exponent is too large to
fit in the exponent field.
Number is too large
Underflow
This occurs when the negative exponent is too
large to fit in the exponent field.
Number is too small

FPT University 18

6
Floating point number
Biased notation
IEEE 754 uses a bias of 127 for single precision
(-1)S x (1 + Fraction) x 2 (Exponent - Bias)
S is the signed bit 0 is positive
1 is negative

FPT University 19

Example
Show the IEEE 754 binary representation of the
number -0.7510 in single precision

Convert to binary -0.112


-1.1 x 2-1
(-1)1 x (1 + .1000 0000 0000 0000 000)
x 2 (126 - 127)

Exponent part

FPT University 20

Floating point addition

FPT University 21

7
Floating point addition example
Add 0.5 and -0.4375

FPT University 22

Floating point addition

FPT University 23

Floating point addition

FPT University 24

8
Floating point multiplication

FPT University 25

Floating point multiplication


Multiply 1.11010 x 1010 by 9.20010 x 10-5

FPT University 26

Floating point multiplication

FPT University 27

9
Floating point multiplication

FPT University 28

Floating point multiplication

FPT University 29

FAQ
There are many questions on converting a given
binary number into the corresponding negative
number and converting a given negative number
into the corresponding positive number.

FPT University 30

10
Non numeric representation
In general, each character is represented by 8
bits.
In multimedia, data associated with still image
data, moving picture data, and sound data is
handled

FPT University 31

Character representation
BCD Code (Binary Coded Decimal Code)
Each digit can be represented by 4 bits

FPT University 32

Standardizations of Character Codes


Code Name Explanation

EBCDIC Computer code defined by IBM for general


purpose computers
8 bits represent one character.
ASCII 7-bit code established by ANSI (American
National Standards Institute) Used in PCs, etc.

ISO code ISO646 published as a recommendation by


The International Organization for
Standardization (ISO), based on ASCII
7-bit code for information exchange

FPT University 33

11
Standardizations of Character Codes
Code Name Explanation

Unicode An industry standard allowing computers to


consistently represent characters used in
most of the countries
Every character is represented with 2 bytes
EUC 2-byte and 1-byte characters can be used
together on UNIX (extended UNIX code).
Chinese and Hangul characters are also
handled

FPT University 34

Image and Sound


Still Images

GIF Format to save graphics, 256


colors displayable

JPEG Compression format for color


still images, or the name of the
Joint organization of ISO and
ITU-T establishing this standard

FPT University 35

MPEG
Compression format for color moving pictures,
or the name of the joint organization of ISO and
IEC which established this standard

MPEG-1 Data stored mainly on CD-ROM

MPEG-2 Stored images like video; real-


time images

MPEG-4 Standardization for mobile


terminals

FPT University 36

12
Sound
Sound

PCM Converting analog signals


(sound, etc.) into digital signals

MIDI Interface to connect a musical


instrument with a computer

FPT University 37

FAQ
There have been many exam questions that
require some knowledge of organizations which
have established functions and standards
regarding JPEG and MPEG. Several keywords,
such as JPEG, ISO, and ITU-T for still images,
MPEG, ISO, and IEC for motion pictures, should
be checked prior to the exam.

FPT University 38

Operations and Accuracy


There are two types of shift operations:
arithmetic shift and logical shift.
Operations in computer depend on the number
of significant digits, so the result could have a
margin of error

FPT University 39

13
Shift operations
A shift operation is the operation of shifting
(moving) a bit string to the right or to the left.
Arithmetic left shift
Logical left shift
Arithmetic right shift
Logical right shift

FPT University 40

Arithmetic shift
Data is treated as numeric data with a positive
or negative sign
Right shift inserts a value identical to the sign bit
into the leftmost place

FPT University 41

Q->
Logical shift
Data is treated as bit string
0s are inserted in the vacated bits

FPT University 42

14
Errors
Since operations are executed by a computer
register with a limited number of digits,
numerical values that cannot be contained in the
register are be ignored.

FPT University 43

Rounding Error
Since computers cannot handle an infinite (non-
terminating) fraction, bits smaller than a certain
bit are rounded off, rounded down, or rounded
up to the value with the limited number of
significant digits. The difference between the
true value and the result of such rounding is
called the rounding error (or round-off
error).

FPT University 44

Cancellation of significant digits


When one number is subtracted from another
number almost identical to it, or when two
numbers, one positive and the other negative,
with almost identical absolute values are added
together, the number of significant digits could
drop drastically.

FPT University 45

15
Loss of trailing digits
When a very large number and a very small
number are added together, or when one is
subtracted from the other, some information (or
a part thereof) in the lower digits, which cannot
be contained in the mantissa, can be lost due to
the alignment of the numbers.

FPT University 46

FAQ
Understand the different characteristics of the
errors
Questions may be asked about the type of error
that may occur when values are manipulated

FPT University 47

Information and Logic


Logical Operations
Logical sum, logical product, logical negation,
and exclusive logical sum are the basic logic
operations.
The grammar of a programming language is
written in BNF

FPT University 48

16
Definitions of Logical Operation
Logical Notation Meaning
operation
Logical A B The result is 1 only when both bits
product (AND) are 1s

Logical sum A+B The result is 1 when at least one of


(OR) the bits is 1

Logical A Reversal of the bit (0 for 1; 1 for


negation A 0)
(NOT)

Exclusive The result is 0 if the bits are the


logical sum A B same, and 1 if the bits are not
(EOR, XOR) equal to each other.

FPT University 49

Expanded form of Exclusive sum


Exclusive logical sum can be expanded, as
shown below. Many questions can be easily
answered if you know the expanded form of
exclusive logical sum, so be sure to know the
expanded formula.

A B = A B+ A B

FPT University 50

De Morgans Theorem

(A B)=A+B

(A +B)=A B

FPT University 51

17
FAQ
Many questions can be easily answered if you
know De Morgan's theorem. There are also
many
questions that can be easily answered if you
know the expanded form of exclusive logical
sum.

FPT University 52

Adder
An adder is a circuit that performs addition of 1-
bit binary numbers, consisting of AND, OR,
and NOT logic circuits.
Half adder
Full adder

FPT University 53

Half adder

A B C S
Truth table 0 0 0 0
0 1 0 1
C=A B
1 0 0 1
S=A B
1 1 1 0

FPT University 54

18
Full adder
There are three input values, one of which is the
carry-over from the lower bit.

X Y Z C S

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

FPT University 55

Karnaugh Maps
Karnaugh Maps

FPT University 56

BNF (Backus-Naur Form)


A means of strictly expressing the grammar of a
programming language.
The terminal symbols cannot be further
decomposed.
To define the grammar of a programming
language (syntax definition), expressions free
from any ambiguity are required. To express
such a grammar, BNF (Backus-Naur Form) is
used

FPT University 57

19
BNF
Sequence
<x >:: =<a><b>
This gives a definition which means the syntax element x
is a string of the character a and b.

The symbol ::= means is defined to be.

Repetition
<x >:: =<a>
This gives a definition which means the syntax element x
is a repetition of the character a, It also means that the
character a repeats once or more times.

FPT University 58

BNF
Selection
<x >:: =<a | b>
This gives a definition which means the syntax element x
is either the character a or the character b. If one of the
options is missing, the following expression is used:
<x >:: =[<a>]
This gives a definition which means the syntax element x
is either the character a or the null character (blank). The
symbols [ ] means that it can be omitted.

FPT University 59

BNF
Terminal and Non-Terminal Symbols
A syntax element already defined can be used to define
another element or even itself. These
syntax elements are called non-terminal
symbols. Characters that are used directly in
sentences are called terminal symbols.
In the following definitions, the underlined <x> is a non
terminal symbol whereas a, b, and c are terminal symbols.
<y >:: =<a><x >
<x >:: =<b><c >

FPT University 60

20
FAQ
An example of syntactical rules: often, questions
follow the pattern of selecting the sentence that
satisfies given syntactical rules.

FPT University 61

Reverse Polish Notation


Reverse Polish Notation is a way to
mechanically
interpret mathematical formulas.
It is characterized by two variables followed by
an operator.

FPT University 62

Conversion of Mathematical Formula


to Reverse Polish Notation
Reverse Polish Notation follows the order of
operations in the formula when converting.

FPT University 63

21
Polish Notation
Polish Notation places the operator in front of
the variables. The fundamental concept is the
same as that of Reverse Polish Notation

FPT University 64

FAQ
Conversion into Reverse Polish Notation or into
a
mathematical formula is a very frequent theme
on exams. It is best if you learn how to answer
these questions intuitively.

FPT University 65

Data Structures
Arrays can be used in every data structure.
Arrays are referred to by index.

FPT University 66

22
Array
An array is a data structure consisting of
multiple data of the same type.

FPT University 67

FAQ
There are hardly any questions directly on
arrays
themselves. However, any question on a data
structure or an algorithm always uses an array.
Hence, you must understand arrays properly.
More specifically, be sure that you understand
how to use the index.
Questions on algorithms on the exam may
have indexes starting at 0 or 1, so care must be
taken.

FPT University 68

Lists
Lists are characterized by being connected with
pointers.
Operations for a list are controlled by changing
the values of pointers
A list is a set of identical or similar data placed
logically linearly.

FPT University 69

23
Basic operations of list
Insertion

FPT University 70

Deletion
To delete an element from a given list, just as in
insertion, all we have to do is to change pointers.

FPT University 71

Garbage collection & Memory leak


Garbage collection: It is the procedure whereby
small, fragmented
unused memory and other areas not usable due
to memory leak are combined together in order
to increase usable memory space.
Memory leak: It means the situation wherein the
main memory secured dynamically by an
application is not released for some reason and
remains in the main memory.

FPT University 72

24
FAQ
Many questions involve insertion into and
deletion from a list. You must carefully consider
which element it is whose pointer should be
stored

FPT University 73

Stacks
Stacks are data structures of LIFO (Last-In First-
Out).
Stacks are used to manage the return addresses
of subroutines
A stack is a data structure in which data
insertion and deletion both take place on the
same end of the list.

FPT University 74

Stack
Pop
Deletion is pop-up.
Push
Insertion is called push-down
The stack pointer (SP) is used to keep track of
where the top of the stack currently is

FPT University 75

25
Uses of the stack
When a main program calls a subprogram
(subroutine) or a function, often the return
address of the program being executed is stored
in a stack; when the subprogram is completed,
the return address of the main program is taken
from the stack to return the control.

FPT University 76

Stack Implementation using a list


Insert

Delete

FPT University 77

FAQ
Many questions involve stacks. The pattern is
that frequently there are questions asking what
happens to the contents of a given stack when
push and pop are repeated.

FPT University 78

26
Queues
Queues are data structures of FIFO (First-In
First-Out).
Queues are used for online transaction
processes.
A queue is a data structure in which insertion
takes place at one end while deletion (taking-
out) occurs at the other end

FPT University 79

Use of the queue


In multiple programming, programs waiting to
be executed are placed into the queue for
execution as long as their priorities are equal,
and they wait for the CPU to be available.

FPT University 80

Implementation of the queue


Implement of a queue using a list, a pointer is
used to indicate the position of the last
element of the list. Insertion is performed at the
end of the list, and deletion is performed at the
head.

FPT University 81

27
Operations of a queue
Insertion

Deletion

FPT University 82

Tree
Trees clearly indicate a hierarchical structure.
Among the various types of trees, binary trees
are to be thoroughly understood.
A tree is a data structure that expresses the
hierarchical structure between elements

FPT University 83

Tree

FPT University 84

28
Binary Tree
A tree in which each node has no more than two
children is called a binary tree.
Complete binary tree
All leaves are at the same depth, or if the
difference of depth between any two
leaves is 1 or less
The leaves are laid out
from the left

FPT University 85

Binary Search tree


A binary search tree is a binary tree such that the
value of an element is assigned to each node
under the following restriction
Value of the left child < Value of the parent
element < Value of the right child

FPT University 86

Heap
A binary tree where the node values are
assigned from the root level and from left
to right on the same level with the following
conditions:
value of a parent element > value of a child
element (max-heap) OR
value of a parent element < value of a child
element (min-heap)

FPT University 87

29
Q -> FAQ

In the Exam, questions involve only binary


trees.
Keep straight in your mind the characteristics
of various binary trees, such as complete
binary trees, binary search trees, and
heaps.

FPT University 88

Hash
Hash is the concept of using the key values
directly as the index.
Two methods to avoid collisions are
the open-address method
the chain method
Hash is the concept of using key values directly
as the storage locations of data.

FPT University 89

Hash
To convert key values to index numbers, a hash
function is used to calculate hash values, which
are then used as index numbers. The array that
stores elements using such a method is called
a hash table.

FPT University 90

30
Collision
When the same hash value is generated in this
way, it is called a collision.
Resolution is by
Chain Method (Open Hash Method)
Open Address Method (Closed Hash
Method)

FPT University 91

Chain Method (Open Hash Method)


This is the method of using a list to store
elements with the same hash value when a
collision occurs. The hash table contains in
advance only the pointer indicating the first data
of the list.

FPT University 92

Open Address Method (Closed Hash


Method)
This is the method of dealing with collisions with
re-hashing. Re-hashing refers to re-calculating
the storage location when a collision occurs and
storing the new data there if the location is
empty.

FPT University 93

31
Q FAQ
->

Many questions dealing with hash will ask you to


calculate the storage position, and a "mod
function is often used as the hash function in
such cases. mod (a,b) is the remainder of a
divided by b.

FPT University 94

Algorithms
Search algorithms
There are two types of search algorithms: linear
search and binary search.
In binary search, the elements are be sorted
in ascending or descending order.

FPT University 95

FAQ
To express algorithms, questions on Morning
Exams use flowcharts, while questions on
Afternoon Exams use pseudo-language. Rules
on
the pseudo-language are not released, so it is a
good idea to look through them in advance.

FPT University 96

32
Linear Search
This is the method of searching for the desired
element in the table from the beginning of the
table in order.
Sentinel search
The same data (sentinel) as the data to
retrieve is placed at the end of a table
Decrease no of comparisons
Simplify the search algorithm

FPT University 97

Linear search

FPT University 98

Binary search
This method narrows down the target data while
successively dividing a search area into two
parts.
Number of comparisons reduced compared with
the linear search method
Elements must be in ascending or descending
order

FPT University 99

33
Binary search

FPT University 100

Q ->
Search efficiency
Mean number of comparisons = N /2
for linear search
Maximum number of comparisons = N
for linear search
Mean number of comparisons = [log2N]
for binary search
Maximum number of comparisons = [log2N] + 1
for binary search

FPT University 101

Q ->
Search efficiency
Slower Faster

Linear Binary
search Search
Average N/2 [log2N]
comparisons
Max no of N [log2N] + 1
comparisons

FPT University 102

34
FAQ
The average number of comparisons and the
maximum number of comparisons in binary
search are frequently asked on exams, so it is a
good idea to have these formulas memorized.

FPT University 103

Sorting algorithms
Be careful when manipulating index numbers in
bubble sort, selection sort, and insertion sort.
Recursive call is used in quick sort and merge
sort.

FPT University 104

Sorting
Sorting the contents of an area in a program,
such as an array, is called internal sorting
sorting data stored in an external device such as
records in a file is called external sorting

FPT University 105

35
Sorting algorithms
Bubble sort
Selection sort
Insertion sort
Quick sort
Merge sort
Shell sort
Heap sort

FPT University 106

FAQ
Questions involving internal sorting in the
exams are almost always about array
manipulation. Be careful not to switch the
index numbers when data is switched.

FPT University 107

Bubble sort
In bubble sort, each adjacent pair of elements is
sequentially compared and exchanged if
necessary.
One of the simplest sort methods
Efficiency is low since data item are
unconditionally compared even if they are sorted
correctly.
If the volume of data is large, the process is
time-consuming.

FPT University 108

36
Bubble sort

FPT University 109

Bubble sort
Average and worst times
N2

FPT University 110

Selection sort
Selection sort finds the maximum value (or the
minimum value) from the array and exchanges
it with the element at the end of the array.
Next, it finds the maximum (or minimum) value
from the array except for the last element and
exchanges it with the second-to-the-last element
of the array.

FPT University 111

37
Selection sort

FPT University 112

Selection sort
Average and worst times
N2

FPT University 113

FAQ
Bubble sort and selection sort very frequently
appear on the exams.
The questions are given in a variety of ways,
such as on the contents of an array at an
intermediate stage and filling in blanks of a
flowchart. Be sure that you understand the
algorithms well.

FPT University 114

38
Insertion sort
Insertion sort starts with an already sorted array,
compares the element to be inserted with the
elements in the array, starting from the back,
and inserts the element in the appropriate
location

FPT University 115

Insertion sort

FPT University 116

Insertion sort
Average and worst times
N2

FPT University 117

39
Quick sort
Quick sort selects a random value from the array
and uses its key value as the pivot;
The elements are divided into two groups: the
first group in which all elements are less than
the pivot and the second group in which all
elements are greater than the pivot (equal
values can go either way).

FPT University 118

Quick sort

FPT University 119

Quick sort
Average time
N log2 N
Worst time
N2

FPT University 120

40
Merge sort
In merge sort, two or more arrays, each of
which is already sorted, are merged together to
form one sorted array. In merge sort, splitting is
repeated until each group has only one element.

FPT University 121

Merge sort

FPT University 122

Merge sort
Average time and worst time
N log2 N

FPT University 123

41
FAQ
Quick sort and merge sort differ from each other
in the number of elements involved in splitting
processes, but they use the same method.
In such cases, a method known as recursive
call is used.

FPT University 124

Shell sort
This is an improved form of insertion sort; the
sorting is made faster by increasing the moving
distances of the elements.
First, the elements are sorted roughly by using
insertion sort with gaps of a certain size.
Then, insertion sort is used again to complete
the sorting operation.

FPT University 125

Shell sort

FPT University 126

42
Shell sort
Average time
N log2 N
Worst time
N2

FPT University 127

Heap sort
A heap is a binary tree in which every subtree
has the property that a parent has a value larger
than its children.
If the root element is picked, we can obtain the
maximum value while the remaining elements
can be re-structured to form a heap

FPT University 128

Heap sort
A "complete" tree is a minimum height tree with all the
nodes on the lowest level in their left-most positions. That
is, the tree is completely filled from the top with any extra
elements strictly on one side (left) of the lowest level.

A heap is (conceptually) a binary tree


that is complete and
exhibits the heap property:
the root, if non-null, is the largest (or smallest) key
in the tree,
its left and right subtrees are themselves heaps.

FPT University 129

43
Heap

FPT University 130

Heap sort
unordered array of items into a heap

FPT University 131

Comparison of sorting algorithms


Type of sort Average time Worst time

Bubble N2 N2

Selection N2 N2

Insertion N2 N2

Quick N log2 N N2

Merge N log2 N N log2 N

Shell N log2 N N2

Heap N log2 N N log2 N

FPT University 132

44
FAQ
For quick sort, merge sort, insertion sort, heap
sort, and shell sort, questions generally deal with
the concept of each, so you should understand
how each sort processes the data.

FPT University 133

String Search Algorithms


In general, string search compares one
character at a time.
Methods for string search
brute-force method
Boyer-Moore method
String search means the process of looking for a
designated sequence of characters in a text
(character string).

FPT University 134

Brute force (nave) method


Brute-force search is the method in which the
desired string is searched for by comparing the
characters one by one from the beginning of the
array in order.

FPT University 135

45
Brute force (nave) method

FPT University 136

Boyer-Moore Method (BM Method)


This is a method that takes the contents of the
pattern string into account to eliminate waste.
If the pattern and a string of the text do not
match up, the number of characters that can be
skipped depends on the right-end character of
the search range of the text being compared.

FPT University 137

Boyer-Moore Method (BM Method)


Analyzing string before search (as KMP).

Storing, for all possible characters, where they


first (right to left) appear in string.

Using this table to quickly find potential


matches.

FPT University 138

46
Boyer-Moore Method (BM Method)
If a given letter usually doesnt occur in a
string, only need approx N/M character
comparisons (N=length doc, M=length string).

Worst case N+M comparisons.

FPT University 139

Graph algorithms
A tree is a type of graph.
The order in which tree search is performed can
be breadth-first or depth-first.

FPT University 140

Graph
A graph consists of nodes and edges.
A node is a vertex that forms the graph.
An edge is a segment connecting a point to a
point.

FPT University 141

47
Breadth-First Order
The search begins at the root and traverses from
lower levels and from left to right.

FPT University 142

Breadth First Order


Visit the next unvisited vertex (if there is one)
thats adjacent to the current vertex, mark it,
and insert it into the queue.
If you cant carry out Rule 1 because there are
no more unvisited vertices, remove a vertex
from the queue (if possible) and make it the
current vertex.

FPT University 143

Depth-First Order
We start with the root and traverse from the left
child and from leaves.

FPT University 144

48
Depth First Search
Search Order in which nodes are traversed
method
Pre-order Parent, left child, right child, in this order

In-order Left subtree, parent, right subtree, in this order

Post-order Left subtree, right subtree, parent, in this order

FPT University 145

FAQ
Depth-first order frequently appears on the
exams.
Understand well how the nodes are
taken out in pre-order, in-order, and post-order.

FPT University 146

FAQ
In string search, there needs to be an index for
the string S and another index for the string R.
When answering a question, the crucial point is
to grasp how to use the indexes.

FPT University 147

49
Summary

FPT University 148

50

You might also like