The document discusses different decrease-and-conquer algorithms. It explains that decrease-and-conquer works by establishing a relationship between solving a problem for an instance of size n and solving a smaller instance of size n-c, where c is some constant. It then decomposes the problem size recursively until reaching a base case. The document provides examples of decrease by a constant, constant factor, and variable amounts. It also discusses insertion sort, binary search, and Euclid's algorithm as examples of decrease-and-conquer approaches.
2. Decrease-and-conquer algorithm works as
follows:
◦ Establish the relationship between a solution to a
given instance of a problem and a solution to a
smaller instance of the same problem.
◦ Exploit this relationship either top down
(recursively) or bottom up (without a recursion).
Instructor:Sadia Arshid, DCS& SE 2
3. Decrease by a constant
◦ The size of an instance is reduced by the same
constant (usually one) at each iteration of the
algorithm.
Decrease by a constant factor
◦ The size of a problem instance is reduced by the
same constant factor
◦ (usually two) on each iteration of the algorithm.
Variable size decrease
◦ A size reduction pattern varies from one iteration to
another
Instructor:Sadia Arshid, DCS& SE 3
5. Insertion sort is based on the decrease (by
one)-and-conquer approach:
◦ Provided that a smaller array A[1..n - 1] is already
sorted.
◦ Find an appropriate position for an element A[n ]
◦ among the sorted n - 1 elements and insert it
there.
Right-to-left scan:
◦ Scan the sorted subarray from right to left until the
first
element smaller than or equal to A[n ] is encountered
and then insert A[n ] right after that element.
Instructor:Sadia Arshid, DCS& SE 5
6. Algorithm InsertionSort (A[1..n ])
for i ← 2 to n do
v ← A[i]
j ← i - 1
while j > 0 and A[j] > v do
A[j + 1] ← A[j]
j ← j - 1
A[j + 1] ← v
Instructor:Sadia Arshid, DCS& SE 6
7. Input size: n
Basic operation: key comparison A[j] > v.
The worst case occurs when the input is
already an array of strictly decreasing values:
W(n)=∑ ∑ 1 = n(n-1)/2=є Ө (n 2 )
Best Case:
B(n)=∑ 1=n-1єӨ(n)
Instructor:Sadia Arshid, DCS& SE 7
n
i=2
j=1
i-1
n
i=2
8. for given i there can be i slots where x can be inserted, and
each slot is equally probable of holding this value, thus each
slot has probability 1/i,
following are number of comparison for each slot
Instructor:Sadia Arshid, DCS& SE 8
slot number of Comparison
i 1
i-1 2
i-2 3
. .
. .
. .
2 i-1
1 i-1
9. average number of comparisons needed to
insert x is
1(1/i)+2(1/i)+………..(i-1)(1/i)+(i-1)(1/i)
=1/i(1+2+…..i-1)+(i-1)(1/i)
=1/i(i(i-1)/2)+(i-1)(1/i)
=(i+1)/2-1/i……………………(eq1)
these are number of comparisons require in each for
loop iteration
A(n)=∑ (i+1)/2 -1/i
=1/2∑ (i+1) - ∑ 1/i
A(n)=(n+4)(n-1)/4-lgn≈n 2 /4єӨ(n 2 )
Instructor:Sadia Arshid, DCS& SE 9
n
i=
2
n
i=
2
n
i=2
10. When given a graph, we are often interested in
searching the vertices in the graph in some
organized way.
◦ Depth-first search (DFS) starts visiting a graph at some
arbitrary unvisited vertex.
◦ When a vertex is visited, a flag is marked to indicate
that it has been visited.
◦ At each vertex v, DFS recursively visits an unvisited
neighbour of v.
◦ If there are no unvisited neighbour, recursion step and
backs up.
It is convinenit to use stack in DFS
◦ push vertex on the stack when visited first time
◦ pop it when all of it becomes dead end
Instructor:Sadia Arshid, DCS& SE 10
11. set DFSnumber for all vertices to be -1
count = 0
for each unvisited vertex v (DFSnumber == -1)
dfs(v, count)
---
dfs(v, int &count)
{
set DFSnumber[v] = count++
for each w adjacent to v
if DFSnumber[w] == -1
dfs(w, count)
}
Instructor:Sadia Arshid, DCS& SE 11
12. Stack Ordering:
a16
c25 f44
d31 b53
e62
Instructor:Sadia Arshid, DCS& SE 12
b
ea
d
c f
a
e
b
f
d
c
13. Every vertex is traversed once.
◦ For each vertex, we need to process all its neighbours.
◦ Adjacency matrix: (n^2) operations
Yields two distinct ordering of vertices:
◦ preorder: as vertices are first encountered (pushed onto
stack)
◦ postorder: as vertices become dead-ends (popped off
stack)
Applications:
◦ checking connectivity, finding connected components
◦ checking acyclicity
◦ searching state-space of problems for solution (AI)
Instructor:Sadia Arshid, DCS& SE 13
14. Explore graph moving across to all the
neighbors of last visited vertex
Similar to level-by-level tree traversals
Instead of a stack, breadth-first uses queue
Applications: same as DFS, but can also find
paths from a vertex to all other vertices with
the smallest number of edges
Instructor:Sadia Arshid, DCS& SE 14
15. BFS(G)
count :=0
mark each vertex with 0
for each vertex v∈ V do
bfs(v)
----------------------------
bfs(v)
count := count + 1
mark v with count
initialize queue with v
while queue is not empty do
a := front of queue
for each vertex w adjacent to a do
if w is marked with 0
count := count + 1
mark w with count
add w to the end of the queue
remove a from the front of the queue
Instructor:Sadia Arshid, DCS& SE 15
17. Every vertex is traversed once.
◦ For each vertex, we need to process all its
neighbours.
◦ Adjacency matrix: (n^2) operations
Yields single ordering of vertices (order
added/deleted from queue is the same)
Applications
◦ Checking connectivity.
◦ Cycle detection.
◦ Shortest path (unweighted graph).
Instructor:Sadia Arshid, DCS& SE 17
18. Data structures: stack vs. queue
Implementation: recursion vs. explicit queue
manipulation
Complexity: same
Instructor:Sadia Arshid, DCS& SE 18
21. Case Complexity : Worst case
w(n)=floor(w(n/2))+1
◦ searching an array sized N requires one
examination of the element in the middle, followed
by a binary search of either the left half or the right
half of the array, each of which has N/2 elements.
terminating condition/initial condition :
w(1)=1
21
Instructor: Sadia Arshid, DCS
&SE
23. The fake coin problem is to detect a single
fake coin from a set of coins using a balance
scale.
Supposing the fake coin is lighter, we can
repeatedly compare one half of a set to the
other half, eliminating the heavier half.
This decreases the number of coins in half
each iteration, so only ≈ log2 n weighings are
needed.
Instructor:Sadia Arshid, DCS& SE 23
24. To multiply two pos. integers n · m by this
method:
m if n = 1
n.m = (n/2) · 2m if n is even
(⌊n/2⌋ · 2m + m if n is odd
Instructor:Sadia Arshid, DCS& SE 24
25. n m
50 65
25 130
12 260 +130
6 520
3 1040
1 2080 +1040
2080+(130+10
40)
3250
Instructor:Sadia Arshid, DCS& SE 25
27. Interpolation search (sometimes referred to
as extrapolation search) is an algorithm for
searching for a given key value in an indexed
array that has been ordered by the values of
the key.
◦ checking telephone index
◦ finding some word dictionary
Instructor:Sadia Arshid, DCS& SE 27
28. work for uniformly distributed data
calculation of mid is modified as
mid =low+(((x-s[low])(high-low))/s(high)-
s(low))
Average case: lg(lg(n))
worst Case: O(n)
Instructor:Sadia Arshid, DCS& SE 28
29. gap=floor((high-low) ½)
mid =low+(((x-s[low])(high-low))/s(high)-
s(low))
mid=min((high-gap),max(mid,low+gap))
index used for comparison is at least gap
position away from low and high
Instructor:Sadia Arshid, DCS& SE 29
30. Function selection(low, high,k)
if low==high
selection=s[low]
else
partition(low,high,pp)
if k==pp
selection =s[pp]
else if k<pp
selection=selection(low,pp-1,k)
else
selection=selection(pp+1,high,k)
Instructor:Sadia Arshid, DCS& SE 30
33. We assume that all inputs are equal likely to
be pivot point ie pp=k
Instructor:Sadia Arshid, DCS& SE 33
Input size in recursive call Number of outcomes yield that input size
0 n
1 pp=2, pp=n-1 and k=1 or k=n 2
2, pp=3, pp=n-2 and k=1,2 or n-1, n 4
3 6
i 2(i)
n-1 2(n-1)
34. A[n]=(sum of (number of outcomes yield that input size)(input size of
recursive call)/sum of numbers )+complexity of partition
=(nA(0)+2(A(1)+4A(2)+6A(3)……+2n-1(An-1)/n+2(1+2+3+…+n-
1))+n-1
A(0)=0
= 2(A(1)+2A(2)+3A(3)……+n-1(An-1))/n+2(n(n-1)/2) ))+n-1
=2(A(1)+2A(2)+3(A(3)……+n-1(An-1))/n2 ) +n-1
n2A(n)= 2(A(1)+2A(2)+3(A(3)……+n-1(An-1))+n2( n-
1)……………….eq(1)
replacing n with n-1
(n-1)2A(n-1)= 2(A(1)+2A(2)+3(A(3)……+n-2(An-2))+(n-1)2( n-
2)……………….eq(2)
subtracting eq2 from eq1
A(n)= (n2-1)A(n-1)/n2+(n-1)(3n-2)/n2
for larger n
A(n)≈A(n-1)+3
≈A(n-2)+3+3
≈A(n-i)+3i
by taking A(0)=0, i=n
≈3nєӨ(n) Instructor:Sadia Arshid, DCS& SE 34
35. Euclid’s s algorithm
gcd(m,n)= gcd( n, m mod n)
size, number, measured by the second
number
iterations are based on repeated application
of equality gcd( m, n)
Example: gcd(80,44) = gcd(44,36) = gcd(36,
8) = gcd(8,4) = gcd(4,0) 4 One can prove
that the size decreases at least by half after
two consecutive iterations
Instructor:Sadia Arshid, DCS& SE 35
36. m>n,
input size is reduced by m mod n in each
iteration, there could be two cases
Case 1:
◦ if n<m/2, (m mod n) < (n) <=m/2
Case2:
◦ if n>m/2, (m mod n)= (m-n) < m/2
◦ size is almost reduced by half
◦ so T(n)єӨ(lg n)
Instructor:Sadia Arshid, DCS& SE 36
Editor's Notes
the reason for comparison at location 1 is i-1 as in this case 1st while condition is false so basic operation is not evaluated.
i-1/2+i-1/i
i2-i+2i-2/2i
i2+i-2/2i
i+1/2-1/i
total number of terms/2(first term + last term)
n-1/2(3+n+1)
½ SUMMATION((n-1)(N+4)/2)
(n-1)(n+4)/4
1/i= lgn