CSC 305 Tutorial F
CSC 305 Tutorial F
CSC 305 Tutorial F
Algorithm
Problem
Data Structure
Concept of Problem
• Problem = Task + Constraints
- It is a general question to be answered. Usually
it possesses one or more parameters. It can be specified by
describing the form of parameters taken and the questions
about the parameters
- It is a task to be performed. It usually includes the
constraints on resources that may be consumed by acceptable
solution. It does not however include any constraint on how
the problem is to be solved
Instance of a problem
• An instance of a problem is an assignment of
values to the parameters
Mathematically :
a problem is a function that maps input domain
to an output range. The values making up an input
are called the parameters for the function while a
specific selection of values is known as instance of
the problem.
E.g y = f(x) ; y = x2 -x -2, where x=3
Algorithm
• An algorithm is a clearly specified set of
simple instructions to be followed to solve a
problem.
• It is a method of implementing a function
(transforming the input to an output)
• It is an ordered set of unambiguous, executable
steps that defines a terminating process.
Hence, a correct algorithm halts with the correct output
for every input instance.
Algorithm
• Informally: An algorithm is:
i. a set of rules that precisely defines a sequence
of operations
ii. a sequence of computational steps that
transform the input into output
iii. a tool for solving a well specified computational
problem
iv. any well-defined computational procedure that
takes some value(s) as input and produces some
value(s) as output
Algorithm Representations
?
From the name can you try to differentiate the
two approaches
Top-Down Design
iii. For a traveler with seven chain of gold, each of which is equivalent to its
hotel bill per night. He wishes to have minimum cut on the chain such
that he should be able to pay for his room on daily bases without paying
for room in advance
iv. For measure exactly four litters of Kerosene using eight litters and five
litters containers
v. For finding the defective balls in two weighing only if you have 8 balls,
one of which is defective and weighs less than others. When you have a
balance to measure balls against each other.
Algorithms Discovery II
vi. Determining the letter grades for
examination raw score
vii. Calculating GPA and CGPA for KWASU
Students
viii. Determining the available balance for a
current account if COT is =N=1 per =N=1,000
ix. Generating Amortization table for a 12 month
loan duration at 3% interest (No moratorium)
x. Borrowing Airtime from a GSM provider considering
subscriber’s history, type of subscription and
current balance
Algorithms Discovery III
xi. Fixing examination venues for a course
considering number of registered students,
room capacity and avoiding clashing
xii. Currency conversion – Naira to three
international currencies one must be in
Africa
xiii. Time conversion – CAT to four other regions
Sample Sorting Algorithms
Sorting: act of arranging data in specific
(ascending or descending) order.
Consider:
• Input: A sequence of n numbers a1, a2, . . . ,
an.
• Output: A permutation (reordering) a’1, a’2, .
. . , a’n of the input sequence such that
a’1≤ a’2≤ · · · ≤ a’n for Ascending order Or such
that a’1 ≥ a’2≥ · · · ≥ a’n for Descending order.
Insertion-Sort Algorithms
Insertion sort is an efficient algorithm for sorting a
small number of elements. It is built on an
incremental approach. It works the way many
people sort a hand of playing cards.
Data Structure
Primitive Non-Primitive
1 2 3
0 5 7 7
1 1 3 8
2 2 2 1
3 2 6 9
4 3 4 3
5 4 2 31
6 4 6 4
7 5 7 7
2. a. Formulate a Sparse Matrix A(4,6) with the first 6 alphabet sparsely distributed
b. Formulate the Sparse Array representation
STACK DATA STRUCTURE
Stack is a non-primitive linear and dynamic data
structure. It allowed data insertion and deletion
only from one end (top of the stack).
The stack’s mode of operations is known as Last in
First Out (LIFO). Hence Stack is also known as
LIFO list, Piles, or Push-down list.
Primitive Operations: PUSH and POP
PUSH ≡ Insertion at the top; Top ← Top + 1 or Over
flow for Full Stack .
POP ≡ Removal at the top; Top ← Top - 1 or Under
flow for Empty Stack
Illustration of Stack Operations
top
Empty
Stack Implementation
Implementation could either be Static using Array or
Dynamic using Pointers.
Static implementation is not flexible.
Examples:
i. (A+B)*C = [+AB]*C = *+ABC
ii. A+B*C = A+[*BC] = +A*BC
iii. (A+B) / (C-D)
NB. [ ] is used to indicate partial translation.
Evaluating arithmetic expressions- III
Postfix notation: Reversed Polish notation. It is an analogous
notation in which the operator is placed after the operand.
Valid expressions: AB±, GH*, MN/, AB+C*, AB*CD+-
Rule of converting Infix to Postfix
i. Parenthesize the expression from left to right
ii. During parenthesizing, the operands associated with operator
having higher precedence are first parenthesized
iii. The sub-expression which has been converted into postfix is to
treated as single operand
iv. Once the expression is converted to postfix form, remove the
parenthesis
Examples:
i. (A+B)*C = [AB+]*C = AB+C*
ii. A+B*C = A+[B*C] = A+[BC*] = A[BC*]+ = ABC*+
iii. (A+B) / (C-D) iv. A^B+C*D^E-F/G
STACK Evaluation of Infix Notation II
The two steps required are: i. infix-to- Postfix Conversion
ii. Evaluation of Postfix in Stack
The Algorithm for (ii)
1. Add a right parenthesis “)” as sentinel for P
2. While P <> sentinel “)”
3. Scan P from L to T into STACK
4. IF an operand is encountered THEN PUSH to Stack
5. IF an operator ʘ is encountered, THEN
a. POP A,B from STACK, where A is top and B is previous top
b. Evaluate BʘA
c. PUSH the result of b on STACK
6. Set value equal to the top element on STACK
STACK Evaluation of Infix Notation III
Example. Simulate in Stack the Evaluation of P:
P= 5,6,2,+,*,12,4,/,-
Solution: P= 5,6,2,+,*,12,4,/,-,)
S/N Scanned Elements STACK
1 5 5
2 6 5,6
3 2 5,6,2
4 + 5,8
5 * 40
6 12 40,12
7 4 40,12,4
8 / 40,3
9 - 37
10 )
STACK Evaluation Binary Conversion
Function binaryconversion (int n)
Stack s New stack 2
While n>0 do Example. Convert 23 to Base
int bit n modulo 2 S/ Scanned STACK STACK
(PUSH) (POP)
s.PUSH(bit) N. Elements
n floor (n/2) 2 11 1
3 5 1
4 2 1
While NotEmpty(s) do 5 1 0
6 0 1
s.POP()
STACK Exercises
1. Write the STACK-FULL(S) functions and rewrite the PUSH (S,x)
2. Generate the Prefix and Postfix for:
i. A + [(B+C) + (D+E)*F]/G
ii. (A+B)*C/D+E^F/G
iii. A*(B+D)/E-F*(G+H/K)
3. Consider P: 12,7,3,-,/,2,1,5,+,*,+
a. Translate P into the infix equivalent
b. Evaluate the infix expression
c. Simulate the evaluation of P in STACK
4. Simulate the evaluation of binary conversion of 27 in Stack Data
Structure
QUEUE Data structure- INTR.
A queue is a pile of items added at one end (rear) and
removed from the other end (front). The mode is
known as FIFO.
Applications of queue: Traffic, Customer services,
Printer spooling and Time sharing activities
Queue operations: ENQUEUE for Insert and DEQUEUE
for Delete. It uses a head and a tail pointers
The elements in the queue are in locations head[Q],
head*Q+ +1, . . . , tail*Q+ − 1
QUEUE Data structure – INTR. II
Initially, we have head[Q] = tail[Q] = 1. When the
queue is empty, an attempt to dequeue an element
causes the queue to underflow.
When head[Q] = tail[Q] + 1, the queue is full, and an
attempt to enqueue an element causes the queue
to overflow
ENQUEUE & DEQUEUE ALGORITHMS
ENQUEUE(Q, x)
1 Q*tail*Q++ ← x
2 if tail[Q] = length[Q]
3 then tail*Q+ ← 1
4 else tail*Q+ ← tail[Q] + 1
DEQUEUE(Q)
1 x ← Q*head*Q++
2 if head[Q] = length[Q]
3 then head*Q+ ← 1
4 else head*Q+ ← head*Q+ + 1
5 return x
QUEUE IMPLEMENTATION IN ARRAY
QUEUE ACTIVITIES
Using Figure 10.1 as a model, illustrate the result of
each operation in the sequence PUSH(S, 4), PUSH(S,
1), PUSH(S, 3), POP(S), PUSH(S, 8), and POP(S) on an
initially empty stack S stored in array S[1 . . 6].
ALLOCATE-OBJECT() FREE-OBJECT(x)
1 if free = NIL 1 next*x+ ← free
2 then error “out of space” 2 free ← x
3 else x ← free
4 free ← next*x+
5 return x
Allocating and freeing objects
(Matrix Representation)
Explanation of Matrix Representation
The free list uses only the next array, which stores
the next pointers within the list. The head of
the free list is held in the global variable free.
When the dynamic set represented by linked
list L is nonempty, the free list may be
intertwined with list L.
Linked List - Exercises
i. Draw a picture of the sequence 13, 4, 8, 19,
5, 11 stored as a doubly linked list using the
multiple-array representation.
ii. Read up the single-array representation and
draw its representation for the sequence in
(i).