2
4 units course
Lectures 60%
Lab 20%
Tutorials 20%
Evaluation
Course work 30%
Assignments, and quizzes
Tests 70%
Test 1&2 20%
Final exam 50%
3
What EKT334 to: (Course outcomes)
1.
2.
3.
4.
Ability to design algorithm for problem solving
Ability to write C code in data structures and analyze the efficiency of their implementations
Ability to implement and evaluate the sorting and searching techniques
Ability to implement and evaluate Tree and Graph
4
Contents :
Algorithms
Arrays and pointers
Structure and union
Linear data structure
Sorting techniques
Searching techniques
Trees
Graphs
5
What would be in the labs:
Arrays and pointers
Structure, union, and stack
Queue, and linked lists
Sorting
Searching
Tree
Graph
6
How to study:
Lecture notes
Lab modules
Handouts, and extra materials
And of course a text book:
Data structures and algorithms, concepts, techniques, and applications’ by
G. A. V. Pai http://www.mhhe.com/pai/dsa .
Please do early reading, and solve all assignments, tutorials, and quizzs
7
How to catch up with me:
Ph: 0175171894
Email: elshaikh@unimap.edu.my
Office: KKF8A (bp5)
8
Q and A
9
•
•
•
•
•
10
Computer science and other discipline
Most technologies engaged to computer science
Solving problems
Analyzing problems
Storing and retrieving data
Computerization
Automation
Others..
11
Industry
Computer
Transportation
Space Technology
Weather
Science
12
Computer discipline
Machines
What are the appropriate machine
Language
How to chose the proper language
Language limitations and the solution requirements
Foundation
Problem definition
Solution modeling
Technology
Web base solution
Mobile application
Machine coding
High or low end peripheral device
13
Discipline of Computer science from the view point of problem solving
Technologies
Foundations Machines
Languages
14
Definition An algorithm may be defined as a finite sequence of instructions each of which has a clear meaning and can be performed with a finite amount of effort in a finite length of time
The first algorithm was designed by (Abu Jafar Mohammed Ibn Musa Al Khwarizmi, Al
Khwarizmi)
The solution to any computing problem involves executing series of actions in a specific order
Algorithms are represented in:
Pseudo code : artificial and informal language that helps programmers develop algorithms
Flow charts
15
Example
To find the addition of two numbers, calls for summing up the digits occurring at specific positions and previous carry digit, representatively moving from the least significant digit to the most significant digits have been exhausted.
9 8 7+
6 7
9 8 7+
6 7
9 8 7+
6 7
(carry 1) 3 (carry 1) 6 3
1 0 6 3
16
An algorithm has the following structure :
input step assignment step decision step repetitive step output step
Example consider the demonstration of Al Khwarizmi’s algorithm for the addition of two numbers, what are:
Input , assignment, decision, repetitive, and output steps?
17
An algorithm is endowed with the following properties:
Finiteness
Algorithm must terminates after a finite number of steps
Definiteness
Precisely defined or unambiguously specified
Generality
Solving all problems of a particular class
Effectiveness
Not too complex to warrant another algorithm
Input-Output
18
The steps involved in the development of an algorithm are as follows:
(i) problem statement
(ii) model formulation
(iii) algorithm design
(iv) algorithm correctness
(v) implementation
(vi) algorithm analysis
(vii) program testing
(viii) documentation
19
design of an efficient algorithm for the solution of the problem calls for the inclusion of appropriate data structures
The proper structure of data contributes to the efficiency of the solution
Consider the problem of searching for a telephone number in the telephone directory
It is easy since the numbers are sorted in an alphabetical oreder
What if, the phone number are sorted according to the phone numbers
Or, according to the subscription of the telephone
The subject of data structures is intrinsically connected with the design and implementation of efficient algorithms. Data structures deals with the study of methods, techniques and tools to organize or structure data.
20
A data type refers to the type of values that variables in a programming language hold.
Thus the data types of integer, real, character, Boolean which are inherently provided in programming languages are referred to as primitive data types.
A list of elements is called as a data object.
For example, we could have a list of integers or list of alphabetical strings as data objects .
The data objects which comprise the data structure, and their fundamental operations are known as Abstract Data Type (ADT).
In other words, an ADT is defined as a set of data objects D defined over a domain L and supporting a list of operations O.
21
Data objects:
Set of all positive integers D
Operations:
Addition of positive integers INT1 and INT2 into RESULT
ADD ( INT1, INT2, RESULT)
Subtraction of positive integers INT1 and INT2 into RESULT
SUBTRACT ( INT1, INT2, RESULT)
Check if a number INT1 is a positive integer
CHECK_POSITIVE( INT1) (Boolean function)
22
23
Apriori estimation is interested in the following for the computation of efficiency
:
(i) the number of times the statement is executed in the program, known as the frequency count of the statement, and
(ii) the time taken for a single execution of the statement
Program segment A
… x = x +
2;
…
Program statements
… x = x + 2;
…
Total frequency count
1
1
Frequency count
24
Program segment B
… for k = 1 to n do x = x + 2; end
…
Program segment C
… for j = 1 to n do for k = 1 to n do x = x + 2; end end
…
Program statements
… for k = 1 to n do x = x + 2; end
…
Total frequency count
Program statements
… for j = 1 to n do for k = 1 to n do x = x + 2; end end
…
Total frequency count
Frequency count
(n+1) n n
3n+1
Frequency count
(n+1) n 2 n
3n 2 +3n+1
25
f ( n )
O ( g ( n )) ( read as f of n
is “big oh” of g of n ), iff there exists a positive integer n
0 and a positive number C such that f ( n )
C g ( n ) , for all n
n
0
. f ( n )
( g ( n )) ( read as f of n is omega of g of n ), iff there exists a positive integer n
0 and a positive number C such that f ( n )
C g ( n ) , for all n
n
0
. f ( n )
( g ( n )) (read as f on n is theta of g of n ) iff there exist two positive constants c
1
and c
2
, and a positive integer n
0
such that c
1 g ( n )
f ( n )
c
2 g ( n ) for all n
n
0
. f ( n )
o ( g ( n )) (read as f of n
is “little oh” of g of n) iff f ( n )
O ( g ( n )) and f ( n )
( g ( n )) .
26
Algorithms reporting O(1) time complexity indicate constant running time
The time complexities of O(n), O(n 2 ) and O(n 3 ) are called linear, quadratic and cubic time complexities respectively. O(logn) time complexity is referred to as logarithmic
In general, time complexities of the type O(n k ) are called polynomial time complexities.
Time complexities such as O(2 n
complexities.
), O(3 n ), in general O(k n ) are called as exponential time
Some of the commonly occurring time complexities in their ascending orders of magnitude are listed below:
O(1)≤ O(log n) ≤ O(n) ≤ O(n.log n) ≤ O(n 2 ) ≤ O(n 3 ) ≤ O(2 n )
27
150
100
50
3
4
1
2
0
1 2 3 4 5 6 7 8 9 10 11 12
Input size
1 : n 2 2: 2 n 3: n.log
2 n 4: log
2 n
Growth rate of some computing time functions
28
Comparison of polynomial and exponential algorithms
Size
Time complexity function n 2 n 3
2 n
3 n
10 20 50
10 -4 sec 4 X 10 -4 sec
10 -3 sec 8 X 10 -3 sec
10 -3 sec 1 sec
6 X 10 sec
-2
25 X 10 sec
125 X 10 sec
-4
35 years
58 mins 2 X 10 3 centuries
-3
29
That input instance (or instances) for which the algorithm takes the maximum possible time is called the worst case and the time complexity in such a case is referred to as worst case time complexity
That input instance for which the algorithm takes the minimum possible time is called the best case and the time complexity in such a case is referred to as best
case time complexity.
All other input instances which are neither of the two are categorized as average cases and the time complexity of the algorithm in such cases is referred to as
average case complexity.
30
Let T(n) be the running time of the recursive function FACTORIAL(n). The running times of lines 1 and 2 is O(1).
The running time for line 3 is given by O(1) + T(n-1).
Here T(n-1) is the time complexity of the call to the recursive function FACTORIAL(n-1).
Thus for some constants c, d,
T(n) = c + T(n-1), if n > 1
= d, if n ≤ 1
The recursive terms in the recurrence relation are replaced so as to move the relation closer to the base criterion viz., T(n) = 1, n≤ 1 . The approximation of the closed form solution obtained viz.,
T(n) = (n-1)c +d yields O(n).
31
Peg S Peg I Peg D
Tower of Hanoi puzzle ( initial configuration)
32 function TRANSFER(N, S, I, D)
/* N disks are to be transferred from peg S to peg D with peg I as the intermediate peg*/ if N is 0 then exit(); else
{ TRANSFER(N-1, S, D, I); /* transfer N-1 disks from peg S to peg I with peg D as the intermediate peg*/
Transfer disk from S to D; /* move the disk which is the last and the largest disk, from peg S to peg D*/
TRANSFER(N-1, I, S, D); /* transfer N-1 disks from peg I to peg D with peg S as the intermediate peg*/
} end TRANSFER.
33
The recurrence relation is given by,
T(N) = 0, if N =0
= 2. T(N-1) + 1, if N > 0
Solution of the recurrence relation for the Tower of Hanoi puzzle,
T ( N )
2 N T ( 0 )
2 ( N
1 )
2 ( N
2 )
..........
...
2 3
2 2
2
1
2 N .
0
( 2 N
1 )
2 N
1
O ( 2 N )
.........( step N )