SlideShare a Scribd company logo
1 of 36
Instructor:Sadia Arshid, DCS& SE 1
 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
 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
Instructor:Sadia Arshid, DCS& SE 4
 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
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
 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
 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
 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
 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
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
 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
 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
 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
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
Instructor:Sadia Arshid, DCS& SE 16
b
ea
d
c f
a
dc e
f
b
 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
 Data structures: stack vs. queue
 Implementation: recursion vs. explicit queue
manipulation
 Complexity: same
Instructor:Sadia Arshid, DCS& SE 18
Instructor:Sadia Arshid, DCS& SE 19
Function location(low,high)
if low<=high
mid=floor((low+high)/2)
if x=s[mid]
return mid
else if x<s[mid]
location=location(low,mid-1)
else location=location(mid+1,high)
20
Instructor: Sadia Arshid, DCS
&SE
 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
w(n)=w( n/2)+1
w(n/2)=w(n/4)+1
w(n/4)=w(n/8)+1
by backward substitution
w(n)=w(n/4)+2=w(n/8)+3=w(n/ 2k )+k
by taking 2k =n
w(n)=w(1)+k=k=lg(n)+1 є O(lgn)
22
Instructor: Sadia Arshid, DCS
&SE
 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
 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
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
Instructor:Sadia Arshid, DCS& SE 26
 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
 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
 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
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
procedure partition(low,high,pivotpoint)
pivotitem=s[low], j=low
for i=low+1 to high
if s[i]<pivotitm
j=j+1
exchange s[i] & s[j]
pivotpoint=j
exchange s[low] & s[pivotpoint]
Instructor:Sadia Arshid, DCS& SE 31
w(n)=w(n-1)+n-1
=w(n-2)+n-2+n-1
=w(n-3)+n-3+n-2+n-1
…..
=1+2+3+…..+n-1
=n(n-1)/2єӨ(n2 )
 Best Case
B(n)= n-1 єӨ(n )
Instructor:Sadia Arshid, DCS& SE 32
 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)
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
 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
 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

More Related Content

What's hot

Cs6402 daa-2 marks set 1
Cs6402 daa-2 marks set 1Cs6402 daa-2 marks set 1
Cs6402 daa-2 marks set 1
Vai Jayanthi
 
The sutherland hodgeman polygon clipping algorithm
The sutherland hodgeman polygon clipping algorithmThe sutherland hodgeman polygon clipping algorithm
The sutherland hodgeman polygon clipping algorithm
Mani Kanth
 

What's hot (20)

Asymptotic analysis
Asymptotic analysisAsymptotic analysis
Asymptotic analysis
 
Backtracking
BacktrackingBacktracking
Backtracking
 
Context free grammar
Context free grammarContext free grammar
Context free grammar
 
P, NP, NP-Complete, and NP-Hard
P, NP, NP-Complete, and NP-HardP, NP, NP-Complete, and NP-Hard
P, NP, NP-Complete, and NP-Hard
 
Cs6402 daa-2 marks set 1
Cs6402 daa-2 marks set 1Cs6402 daa-2 marks set 1
Cs6402 daa-2 marks set 1
 
Algorithms Lecture 1: Introduction to Algorithms
Algorithms Lecture 1: Introduction to AlgorithmsAlgorithms Lecture 1: Introduction to Algorithms
Algorithms Lecture 1: Introduction to Algorithms
 
asymptotic notations i
asymptotic notations iasymptotic notations i
asymptotic notations i
 
Pattern recognition and Machine Learning.
Pattern recognition and Machine Learning.Pattern recognition and Machine Learning.
Pattern recognition and Machine Learning.
 
Vector quantization
Vector quantizationVector quantization
Vector quantization
 
Cohen and Sutherland Algorithm for 7-8 marks
Cohen and Sutherland Algorithm for 7-8 marksCohen and Sutherland Algorithm for 7-8 marks
Cohen and Sutherland Algorithm for 7-8 marks
 
Generating function
Generating functionGenerating function
Generating function
 
Clipping
ClippingClipping
Clipping
 
A simple approach of lexical analyzers
A simple approach of lexical analyzersA simple approach of lexical analyzers
A simple approach of lexical analyzers
 
Minimum spanning Tree
Minimum spanning TreeMinimum spanning Tree
Minimum spanning Tree
 
Recursion tree method
Recursion tree methodRecursion tree method
Recursion tree method
 
Functions in discrete mathematics
Functions in discrete mathematicsFunctions in discrete mathematics
Functions in discrete mathematics
 
NLP_KASHK:Minimum Edit Distance
NLP_KASHK:Minimum Edit DistanceNLP_KASHK:Minimum Edit Distance
NLP_KASHK:Minimum Edit Distance
 
Graphs - Discrete Math
Graphs - Discrete MathGraphs - Discrete Math
Graphs - Discrete Math
 
Daa notes 1
Daa notes 1Daa notes 1
Daa notes 1
 
The sutherland hodgeman polygon clipping algorithm
The sutherland hodgeman polygon clipping algorithmThe sutherland hodgeman polygon clipping algorithm
The sutherland hodgeman polygon clipping algorithm
 

Similar to 08 decrease and conquer spring 15

Reasoning about laziness
Reasoning about lazinessReasoning about laziness
Reasoning about laziness
Johan Tibell
 
Integration techniques
Integration techniquesIntegration techniques
Integration techniques
Krishna Gali
 
3 4 character tables
3 4 character tables3 4 character tables
3 4 character tables
Alfiya Ali
 

Similar to 08 decrease and conquer spring 15 (20)

Disjoint sets
Disjoint setsDisjoint sets
Disjoint sets
 
05 dc1
05 dc105 dc1
05 dc1
 
Introduction to Neural Networks and Deep Learning from Scratch
Introduction to Neural Networks and Deep Learning from ScratchIntroduction to Neural Networks and Deep Learning from Scratch
Introduction to Neural Networks and Deep Learning from Scratch
 
Practical and Worst-Case Efficient Apportionment
Practical and Worst-Case Efficient ApportionmentPractical and Worst-Case Efficient Apportionment
Practical and Worst-Case Efficient Apportionment
 
Digital Signal Processing
Digital Signal ProcessingDigital Signal Processing
Digital Signal Processing
 
Shape1 d
Shape1 dShape1 d
Shape1 d
 
Reasoning about laziness
Reasoning about lazinessReasoning about laziness
Reasoning about laziness
 
module2_dIVIDEncONQUER_2022.pdf
module2_dIVIDEncONQUER_2022.pdfmodule2_dIVIDEncONQUER_2022.pdf
module2_dIVIDEncONQUER_2022.pdf
 
Integration techniques
Integration techniquesIntegration techniques
Integration techniques
 
Single source shortes path in dag
Single source shortes path in dagSingle source shortes path in dag
Single source shortes path in dag
 
Cs6402 design and analysis of algorithms may june 2016 answer key
Cs6402 design and analysis of algorithms may june 2016 answer keyCs6402 design and analysis of algorithms may june 2016 answer key
Cs6402 design and analysis of algorithms may june 2016 answer key
 
Lecture5
Lecture5Lecture5
Lecture5
 
Chap12alg
Chap12algChap12alg
Chap12alg
 
Chap12alg
Chap12algChap12alg
Chap12alg
 
Doubly Accelerated Stochastic Variance Reduced Gradient Methods for Regulariz...
Doubly Accelerated Stochastic Variance Reduced Gradient Methods for Regulariz...Doubly Accelerated Stochastic Variance Reduced Gradient Methods for Regulariz...
Doubly Accelerated Stochastic Variance Reduced Gradient Methods for Regulariz...
 
Cse41
Cse41Cse41
Cse41
 
3 4 character tables
3 4 character tables3 4 character tables
3 4 character tables
 
Conformable Chebyshev differential equation of first kind
Conformable Chebyshev differential equation of first kindConformable Chebyshev differential equation of first kind
Conformable Chebyshev differential equation of first kind
 
Solution manual for introduction to nonlinear finite element analysis nam-h...
Solution manual for introduction to nonlinear finite element analysis   nam-h...Solution manual for introduction to nonlinear finite element analysis   nam-h...
Solution manual for introduction to nonlinear finite element analysis nam-h...
 
Constraint propagation
Constraint propagationConstraint propagation
Constraint propagation
 

More from Hira Gul (11)

Final iwvc
Final iwvcFinal iwvc
Final iwvc
 
Oerating system project
Oerating system projectOerating system project
Oerating system project
 
project Judaism iwvc presentation
project Judaism iwvc presentation project Judaism iwvc presentation
project Judaism iwvc presentation
 
09d transform & conquer spring2015
09d transform & conquer spring201509d transform & conquer spring2015
09d transform & conquer spring2015
 
07 dc3
07 dc307 dc3
07 dc3
 
06 dc2
06 dc206 dc2
06 dc2
 
04 brute force
04 brute force04 brute force
04 brute force
 
03 mathematical anaylsis
03 mathematical anaylsis03 mathematical anaylsis
03 mathematical anaylsis
 
03 dc
03 dc03 dc
03 dc
 
02 order of growth
02 order of growth02 order of growth
02 order of growth
 
01 intro to algorithm--updated 2015
01 intro to algorithm--updated 201501 intro to algorithm--updated 2015
01 intro to algorithm--updated 2015
 

Recently uploaded

+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
masabamasaba
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
masabamasaba
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
masabamasaba
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
masabamasaba
 

Recently uploaded (20)

+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
 
tonesoftg
tonesoftgtonesoftg
tonesoftg
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 

08 decrease and conquer spring 15

  • 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
  • 16. Instructor:Sadia Arshid, DCS& SE 16 b ea d c f a dc e f b
  • 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
  • 20. Function location(low,high) if low<=high mid=floor((low+high)/2) if x=s[mid] return mid else if x<s[mid] location=location(low,mid-1) else location=location(mid+1,high) 20 Instructor: Sadia Arshid, DCS &SE
  • 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
  • 22. w(n)=w( n/2)+1 w(n/2)=w(n/4)+1 w(n/4)=w(n/8)+1 by backward substitution w(n)=w(n/4)+2=w(n/8)+3=w(n/ 2k )+k by taking 2k =n w(n)=w(1)+k=k=lg(n)+1 є O(lgn) 22 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
  • 31. procedure partition(low,high,pivotpoint) pivotitem=s[low], j=low for i=low+1 to high if s[i]<pivotitm j=j+1 exchange s[i] & s[j] pivotpoint=j exchange s[low] & s[pivotpoint] Instructor:Sadia Arshid, DCS& SE 31
  • 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

  1. 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.
  2. 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
  3. M ALWAYS GEATER
  4. n2A(n)-(n-1)2A(n-1)=2(n-1)A(n-1)+(n-1)(n2-(n-1)(n-2)) n2A(n) =(n-1)2A(n-1)+2(n-1)A(n-1)+(n-1)(n2-(n2-n-2n+2) n2A(n) =A(n-1)(n2-2n+1+2n-2))+(n-1)(n2-n2+3n-2) =(n2-1)A(n-1)+(n-1)(3n-2)
  5. 0