SAK 3117 Data Structures: Chapter 1: Introduction To Data Structure
SAK 3117 Data Structures: Chapter 1: Introduction To Data Structure
Road City
7. Graph
Like a tree but the relationship can be in any direction.
Example: Distance between two or more towns.
1.1.2 Operations in a Data Structure
Operations
Traversing – to access every record at least
once.
Searching – to find the location of the record
using key
Insertion – adding a new record.
Deletion – remove a record from the structure.
Combination
Updating
Sorting
Merging
1.2 Algorithm
Definition: steps to solve a problem.
Input Process Output
Data structure + Algorithm program
Example 1:
To find a summation of N numbers in an array
of A.
1 Set sum = 0
2 for j = 0 to N-1 do :
3 begin
sum = sum + A[j]
4 end
1.2 Algorithm
Example 2:
To find a summation of N numbers in an
array of A.
1 sum = 0
2 for j = 0 to N-1
sum = sum + A[j]
1.2 Algorithm
Example 3:
Multiply (Matrix a, Matrix b)
1. If column size of a equal to row size of b.
2. for i = 1 to row size of a
2.1 for j = 1 to column size of b
cij = 0
2.2 for k = 1 to column size of a
cij = cij + aik * bkj
1.3 Algorithm Analysis
To determine how long or how much spaces are
required by that algorithm to solve the same
problem.
Other measurements:
Effectiveness
Correctness
Termination
Effectiveness
Easy to understand
Easy to perform tracing.
The steps of logical execution are well
organized.
1.3 Algorithm Analysis
Correctness
Output is as expected or required and
correct.
Termination
Step of executions contains ending.
Termination will happen as being planned
and not because of problems like looping,
out of memory or infinite value.
1.3 Algorithm Analysis
Measurement of algorithm efficiency.
Running time
Memory usage
Correctness
1.3.1 Algorithm Complexity
Algorithm M complexity is a function, f(n)
where the running time and/or memory
storage are required for input data of size n.
In general, complexity refers to running
time.
If a program contains no loop, f depends on
the number of statements. Else f depends
on number of elements being process in the
loop.
1.3.1 Algorithm Complexity
Looping Functions can be categorized into 2
types:
Simple Loops
* Linear Loops
* Logarithmic Loops
Nested Loops
* Linear Logarithmic
* Dependent Quadratic
* Quadratic
1.3.1 Algorithm Complexity
Simple Loops
1. Linear Loops
Algo. 1.a:
i=1
loop (i <= 1000)
application code
i=1+1
end loop
The number of iterations directly proportional to the loop
factor (e.g. loop factor = 1000 times). The higher the
factor, the higher the no. of loops.
The complexity of this loop proportional to no. of
iterations. Determined by the formula: f(n) = n
1.3.1 Algorithm Complexity
Simple Loops
1. Linear Loops
Algo. 1.b:
i=1
loop (i <= 1000)
application code
i=1+2
end loop
Number of iterations half the loop factor (e.g. 1000/2 = 500
times).
Complexity is proportional to the half factor. f(n) = n/2
Both cases still consider Linear Loops - a straight line graphs.
1.3.1 Algorithm Complexity
Linear Loops
2. Logarithmic Loops
Consider a loop which controlling variables - multiplied or
divided:
Algo. 2.a & 2.b:
Multiply Loops Divide Loops
1 i=1 1 i = 1000
2 loop (i < 1000) 2 loop (i >=1)
1 application code 1 application code
2 i=ix2 2 i=i/2
3 end loop 3 end loop
1.3.1 Algorithm Complexity
Multiply Divide
1 1 1 1000
2 2 2 500
3 4 3 250
4 8 4 125
5 16 5 62
6 32 6 31
7 64 7 15
8 128 8 7
9 256 9 3
10 512 10 1
Gift
Shop
Gift
Shop
Gift
Shop
Figure 4: Another package delivery scheme
1.3.2 Big-O Notation
The driver delivers only one package at a time. After delivering a package, the driver
comes back to the shop to pick up and deliver the next package. Using this scheme, the
total distance travelled by this driver to deliver the packages and return to the store is:
2 * (1 + 2 + 3 + … + 50) = 2550 miles
Now suppose that are n packages to be delivered to n houses, and each house is one mile
apart from each other as shown in Figure 4. If the package are delivered using the first
scheme, the following equation gives the total distance travelled:
1 + 1 + 1 + … + 1 + n = 2n (1)
n times
If the packages are delivered using the second method, the distance travelled is:
2 * (1 + 2 + 3 + … + n) = 2 * (n(n + 1) / 2) = n2 + n (2)
1.3.2 Big-O Notation
In Equation 1, we say that the distance travelled is a function of n. Now consider
Equation 2. In this equation, for large values of n, we find that the term consisting of n 2
becomes the dominant term and the term containing n is negligible. In this case, the
distance travelled is a function of n2. Table 1 evaluates Equations 1 and 2 for certain
values of n. This table also shows the values of n, 2n, n2, and n2 + n.
Table 1: Values of n, 2n, n2, and n2 + n
n 2n n2 n2 + n
1 2 1 2
10 20 100 110
100 200 10000 10100
1000 2000 1000000 1001000
10000 20000 100000000 100010000
1.3.2 Big-O Notation
Order of magnitude of the result based-on run-time
efficiency
Expressed as O(N) @ O(f(n)) big-O
N – represents data, instructions, etc.
Sometimes refer as complexity degree of measurement:
* run-time , complexity
* comparison of fast and slow algorithms for the same
problem statement.
Time taken= Algorithm Execution = Running Time
1.3.2 Big-O Notation
Table 2: Measures of Efficiency
Nested Efficiency/ Algorithm Name Algorithm Type
Complexity
Degree
No Loop O(k) Constant
Simple Loop O(N) Linear Linear Search
O(log2N) Logarithmic Binary Search
Nested Loop O(N2) Quadratic Bubble Sort,
Selection Sort,
Insertion Sort
O(N log2N) Linear Merge Sort,
Logarithmic Quick Sort, Heap
Sort
1.3.2 Big-O Notation
Table 3: Intuitive interpretations of growth-rate function
1 A problem whose time requirement is constant and therefore,
independent of the problem’s size n.
log2n The time for the logarithmic algorithm increases slowly as the
problem size increases.
If you square the problem, you only double its time requirement.
The base of the log does not affect a logarithmic growth rate, do
you can omit it in a growth-rate function.
Ex: recursive
1st Method
f(n) = Linear loops
=n
Big-O O(n)
1.3.2 Big-O Notation
2nd Method
(1) < O(log2n) < O(n) < O(nlog2n) < O(n2) < O(n3)
Exercise 1