AoA-Segment-1 Introduction
AoA-Segment-1 Introduction
Mathematics
Mathematical Background
• Sets
– A set is a collection of distinct elements that we wish
to treat as a single object.
– Usually the elements are of the same type
– A set has no inherent order
– {a, b, c} and {b, c, a} are same
• Sequence
– A group of elements in a specified order is called a
sequence.
– (1, 2, 3), (1, 3, 5), and (a, c, b) are distinct sequences
• Series
– A series is the sum of a sequence.
i
i 1
i
i 1
2
6
• Power of 2 k
– This is a frequently occurring case of a
geometric series: i 0
2 i
2 k 1
1
• Geometric Series n
r n 1
1
– The general case of geometric series:
i 0
i
r
r1
Abasyn University Peshawar 18
Series
• Harmonic Series
n
i 1
1
i
ln( n) .577
• Arithmetic-Geometric
Series n
i 2
i 1
i
( k 1) 2 k 1
2
• Fibonacci Numbers
– Although this is not a
summation, the series
Fn Fn 1 Fn 2 for n 2,
occurs frequently in
analysis of algorithms. F0 0, F1 1.
Pseudocode Conventions
Pseudocode Conventions
ASSIGNMENT NO. 1
6 2
Your friend = 2 10 instructio ns 2000seconds
109 instructio ns / second
You = 50 106 lg106 instructio ns
7
100seconds
10 instructio ns / second
20 times better!!
Abasyn University Peshawar 52
Analysis of Algorithm
– T(n) =1 [first step] + (n) [for loop] + (n-1) [if condition] + (n-1)
[the assignment]
– T(n) = 3n - 1
1: A=[n] 1
2: for i ← 1 to len(A) n+1
3: print (A[i]) n
1: a =[n] 1
2: for i ← 1 to len(a) n+1
3: if (a[i] == x) n
4: return (i);
T(n)=1+(n+1)+(n(n+1))+(n2)+(n2(n+1))+n3+n2
T(n)=2n3+4n2+2n+2
• log n (logarithmic)
– A big problem is solved by cutting the original problem in smaller
sizes, by a constant fraction at each step
• n (linear)
– A small amount of processing is done on each input element
• n log n
– A problem is solved by dividing it into smaller problems, solving
them independently and combining the solution
• n3 (cubic)
– Processing of triples of data (triple nested loops)
• nK (polynomial)
• 2n (exponential)
– Few exponential algorithms are appropriate for practical use
2 1 2 2 4 8 4 4
4 2 4 8 16 64 16 256 100000000
8 3 8 24 64 512 256 16777216
1.84467E+1
16 4 16 64 256 4096 65536 9 10000000
10000
1000
100
10
1
10 100 1000
Time (micro sec)
lg n n nlg n n2 n3
• Chapter 1
• Appendix A
• Chapter 2: Section 2.1, 2.2