Professional Documents
Culture Documents
presentation
BITS Pilani Dr S. S. Chauhan
sansar@wilp.bits-pilani.ac.in
Pilani|Dubai|Goa|Hyderabad
BITS Pilani
Pilani|Dubai|Goa|Hyderabad
1. Introduction
2. Algorithms and it’s Specification,
3. Random Access Machine Model,
4. Counting Primitive Operations,
5. Correctness of Algorithms.
6. Notion of best case, average case and worst case.
4
Johannes Gutenberg (1398 - February 3, 1468)
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Two discoveries that changed the world !!
Algorithm 1: Algorithm 2:
Time
Space
c[0] -45
c[1] 6
Array
c[2] 0
Linearly Ordered Set c[3] 72
c[4] 1543
c[5] -89
c[6] 0
node c[8] -3
node c[9] 1
Data Next Data Next c[10] 6453
c[11] 78
object2 object
Position number
of the element 12
within array c
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Data Structures
Hash Tables
Stack
Set with delete operation
specified (LIFO)
Queue
Remove Insert
(Dequeue) front rear (Enqueue)
13
Binary Tree
Heap
26
17 41
14 21 30 47
Red Black Tree
10 16 19 23 28 38
7 12 15 20 35 39
14
Matrix Multiplication
Nearest Points
15
Efficient algorithm
to compute F(n)
Leonardo Fibonacci
1170-1250
16
17
19
u
3 2
Shortest Path Algorithms:
2
Dijkstra’s,
1 Floyd-Warshall’s
1 3
-3 All Pairs Shortest Path Algoirthm
1 v 2 MST Algorithms:
Kruskal & Prim (emphasis on Greedy approach )
Complexity Class P
Set of all decision problems (or languages) that can be solved in polynomial time
Complexity Class NP
Set of all decision problems (or languages) that can be verified by a polynomial-
time algorithm
NP NPC
NP-completeness
A language L is NP-complete if
L is in NP and
P
All other languages in NP are polynomially reducible to L
NP-hard
A language L is NP-hard if
All other languages in NP are polynomially reducible to L
21
Home city
Visit city 22
Hodge Conjecture
Navier-Stokes Equations
Is P = NP?
Poincaré Conjecture
Riemann Hypothesis
Yang-Mills Theory
23
25
Jon Kleinberg and Eva Tardos. Algorithm Design. Pearson Education. (2007)
26
29
30
31
• Correctness:-
Correct: Algorithms must produce correct result.
Approximation algorithm: Exact solution is not found,
but near optimal solution can be found out. (Applied to
optimization problem.)
• Less resource usage:
Algorithms should use less resources (time and space).
38
BITS Pilani, Pilani Campus
A survey suggest that we have gained more efficiency than
as compared to hardware or processor speed
development through algorithms instead.
Potentially unbounded
bank of memory
cells, each of which 2
1
can hold an arbitrary 0
number or character
Analysis of Algorithms 42
BITS Pilani, Pilani Campus
Our Machine Model
Generic Random Access Machine (RAM)
Executes operations sequentially
Set of primitive operations:
– Arithmetic. Logical, Comparisons, Function calls
L1.43
BITS Pilani, Pilani Campus
Primitive Operations
Examples:
– Evaluating an expression
– Assigning a value to a variable
– Indexing into an array
– Calling a method
– Returning from a method
Analysis of Algorithms 44
BITS Pilani, Pilani Campus
Analyzing pseudocode
(by counting)
1. For each line of pseudocode, count the number of
primitive operations in it. Pay attention to the word
"primitive" here; sorting an array is not a primitive operation.
2. Multiply this count with the number of times this line
is executed.
3. Sum up over all lines.
Analysis of Algorithms 45
BITS Pilani, Pilani Campus
Counting Primitive Operations
By inspecting the pseudocode, we can determine the
maximum number of primitive operations executed by an
algorithm, as a function of the input size
Algorithm arrayMax(A, n CostTimes
currentMax A[0] 2 1
for i 1 to n 1 do 2 n
if A[i] currentMax then 2 (n 1)
currentMax A[i] 2 (n 1)
{ increment counter i } 2 (n 1)
return currentMax 1 1
Total 8n 3
Analysis of Algorithms 46
BITS Pilani, Pilani Campus
Insertion Sort
47
BITS Pilani, Pilani Campus
Insertion Sort
input array
5 2 4 6 1 3
sorted unsorted
48
Insertion Sort
49
INSERTION-SORT
Alg.: INSERTION-SORT(A) 1 2 3 4 5 6 7 8
for j ← 2 to n a1 a2 a3 a4 a5 a6 a7 a8
do key ← A[ j ]
key
Insert A[ j ] into the sorted sequence A[1 . . j -1]
i←j-1
while i > 0 and A[i] > key
do A[i + 1] ← A[i]
i←i–1
A[i + 1] ← key
• Insertion sort – sorts the elements in place
50
Correctness of algorithms by loop
Invariant
Loop Invariant for Insertion Sort
Alg.: INSERTION-SORT(A)
for j ← 2 to n
do key ← A[ j ]
Insert A[ j ] into the sorted sequence A[1 . . j -1]
i←j-1
while i > 0 and A[i] > key
do A[i + 1] ← A[i]
i←i–1
A[i + 1] ← key
Invariant: at the start of the for loop the elements in A[1 . . j-1] are in
sorted order
52
Proving Loop Invariants
• Proving loop invariants works like induction
• Initialization (base case):
– It is true prior to the first iteration of the loop
• Maintenance (inductive step):
– If it is true before an iteration of the loop, it remains true before
the next iteration
• Termination:
– When the loop terminates, the invariant gives us a useful
property that helps show that the algorithm is correct
– Stop the induction when the loop terminates
53
Loop Invariant for Insertion Sort
• Initialization:
– Just before the first iteration, j
= 2:
the subarray A[1 . . j-1] =
A[1], (the element originally in
A[1]) – is sorted
54
Loop Invariant for Insertion Sort
• Maintenance:
– the while inner loop moves A[j -1], A[j -2],
A[j -3], and so on, by one position to the right
until the proper position for key (which has the
value that started out in A[j]) is found
– At that point, the value of key is placed into this
position.
55
Loop Invariant for Insertion Sort
• Termination:
– The outer for loop ends when j = n + 1 j-1 =
n
– Replace n with j-1 in the loop invariant:
• the subarray A[1 . . n] consists of the jelements
-1 j
originally in A[1 . . n], but in sorted order
Invariant: at the start of the for loop the elements in A[1 . . j-1] are in
sorted order
T (n) c1n c2 (n 1) c4 (n 1) c5 t j c6 t j 1 c7 t j 1 c8 (n 1)
n n n
j 2 j 2 j 2
57
Best Case Analysis
• The array is already sorted “while i > 0 and A[i] > key”
– A[i] ≤ key upon the first time the while loop test is run
(when i = j -1)
– tj = 1
= an + b = (n)
T (n) c1n c2 (n 1) c4 (n 1) c5 t j c6 t j 1 c7 t j 1 c8 (n 1)
n n n
j 2 j 2 j 2
58
Worst Case Analysis
• The array is in reverse sorted order“while i > 0 and A[i] > key”
– Always A[i] > key in while loop test
– Have to compare key with all elements to the left of the j-th
position compare with j-1 elements tj = j
n
n(n 1) n
n(n 1) n
n(n 1)
using
j 1
j
2
j
j 2 2
1 ( j 1)
j 2 2
we have:
n(n 1) n( n 1) n(n 1)
T (n ) c1n c2 (n 1) c4 (n 1) c5 1 c6 c7 c8 (n 1)
2 2 2
an 2 bn c a quadratic function of n
j 2 j 2 j 2 59
Comparisons and Exchanges in Insertion
Sort
cost times
INSERTION-SORT(A)
c1 n
for j ← 2 to n
c2 n-1
do key ← A[ j ]
Insert A[ j ] into the sorted sequence A[1 . . j -1] 0 n-1
c6
n
do A[i + 1] ← A[i] j 2
(t j 1)
i←i–1
n2/2 exchanges c7
n
j 2
(t j 1)
A[i + 1] ← key
c8 n-1
60
Insertion Sort - Summary
• Advantages
– Good running time for “almost sorted” arrays
(n)
• Disadvantages
– (n2) running time in worst and average case
– n2/2 comparisons and exchanges
61
Thank You!!
62