SlideShare a Scribd company logo
Presentation
Previous Year Question Solution(2005)
By: To:
Md. Mehdi Hasan Md. Mahfuz Reza
Id: CE-14002 Assistant Professor,
2nd Year 2nd Semister, Dept Of Cse,
Dept Of Cse,MBSTU. MBSTU
What Is Meant By Divide & Conquer
Algorithm???
• In divide and conquer approach, the problem in hand, is
divided into smaller sub-problems and then each
problem is solved independently. When we keep on
dividing the subproblems into even smaller sub-
problems, we may eventually reach a stage where no
more division is possible. Those "atomic" smallest
possible sub-problem (fractions) are solved. The solution
of all sub-problems is finally merged in order to obtain
the solution of an original problem.
• Broadly, we can understand divide-and-
conquer approach in a three-step process.
Advantages Of Divide & Conquer
Algorithm
• The first, and probably most recognizable benefit of the divide
and conquer paradigm is the fact that it allows us to solve
difficult and often impossible looking problems, such as the
Tower of Hanoi, which is a mathematical game or puzzle.
Being given a difficult problem can often be discouraging if
there is no idea how to go about solving it. However, with the
divide and conquer method, it reduces the degree of difficulty
since it divides the problem into sub problems that are easily
solvable, and usually runs faster than other algorithms would.
Another advantage to this paradigm is that it often plays a
part in finding other efficient algorithms, and in fact it was the
central role in finding the quick sort and merge sort
algorithms.
Example:
Min Max Using Divide & Conquer
Algorithm
• Divide the array into two parts and compare the maximums and minimums of the
the two parts to get the maximum and the minimum of the the whole array.
• Pair MaxMin(array, array_size) if array_size = 1 return element as both max and
min else if arry_size = 2 one comparison to determine max and min return that
pair else /* array_size > 2 */ recur for max and min of left half recur for max and
min of right half one comparison determines true max of the two candidates one
comparison determines true min of the two candidates return the pair of max and
min Implementation
• /* structure is used to return two values from minMax() */
• #include<stdio.h>
• struct pair
• {
• int min;
• int max;
• };
•
• struct pair getMinMax(int arr[], int low, int high)
• {
• struct pair minmax, mml, mmr;
• int mid;
•
• /* If there is only on element */
• if (low == high)
• {
• minmax.max = arr[low];
• minmax.min = arr[low];
• return minmax;
• }
•
• /* If there are two elements */
• if (high == low + 1)
• {
• if (arr[low] > arr[high])
• {
• minmax.max = arr[low];
• minmax.min = arr[high];
• }
• else
• {
• minmax.max = arr[high];
• minmax.min = arr[low];
• }
• return minmax;
• }
•
• /* If there are more than 2 elements */
• mid = (low + high)/2;
• mml = getMinMax(arr, low, mid);
• mmr = getMinMax(arr, mid+1, high);
•
• /* compare minimums of two parts*/
• if (mml.min < mmr.min)
• minmax.min = mml.min;
• else
• minmax.min = mmr.min;
•
• /* compare maximums of two parts*/
• if (mml.max > mmr.max)
• minmax.max = mml.max;
• else
• minmax.max = mmr.max;
•
• return minmax;
• }
•
• /* Driver program to test above function */
• int main()
• {
• int arr[] = {1000, 11, 445, 1, 330, 3000};
• int arr_size = 6;
• struct pair minmax = getMinMax(arr, 0, arr_size-1);
• printf("nMinimum element is %d", minmax.min);
• printf("nMaximum element is %d", minmax.max);
• getchar();
• }
Basic Algo
• Pair MaxMin(array, array_size) if array_size = 1
return element as both max and min else if
arry_size = 2 one comparison to determine
max and min return that pair else /*
array_size > 2 */ recur for max and min of left
half recur for max and min of right half one
comparison determines true max of the two
candidates one comparison determines true
min of the two candidates return the pair of
max and min.
Distinguish Between Divide & Conquer
& Dp
• Divide and Conquer basically works in three steps.
1. Divide - It first divides the problem into small chunks or
sub-problems.
2. Conquer - It then solve those sub-problems recursively
so as to obtain a separate result for each sub-problem.
3. Combine - It then combine the results of those sub-
problems to arrive at a final result of the main problem.
Some Divide and Conquer algorithms are Merge Sort,
Binary Sort, etc.
• Dynamic Programming is similar to Divide and Conquer when it
comes to dividing a large problem into sub-problems. But here,
each sub-problem is solved only once. There is no recursion. The
key in dynamic programming is remembering. That is why we store
the result of sub-problems in a table so that we don't have to
compute the result of a same sub-problem again and again.
Some algorithms that are solved using Dynamic Programming are
Matrix Chain Multiplication, Tower of Hanoi puzzle, etc..
Another difference between Dynamic Programming and Divide and
Conquer approach is that -
In Divide and Conquer, the sub-problems are independent of each
other while in case of Dynamic Programming, the sub-problems are
not independent of each other (Solution of one sub-problem may
be required to solve another sub-problem).
Advantages OF Dp
• Finding no. of ways kind of problems, can also be done by
combinatorial formulae, which means the input size can be as
big as 10^9(with some modulo in the end), but DP will time
out.
Finding optimal solution type of problems has the same
problem. Sometimes, greedy works, which means, the
complexity of solution can be much lower, and DP again times
out. Even though DP won't give a wrong solution, but if
greedy is O(n), DP will likely be more than O(n) , because DP
searches a large part of the solution space, which is usually
some orders bigger than n^1.
What Is Algorithm???
• An algorithm is defined as a step-by-step procedure or method for solving a
problem by a computer in a finite number of steps. Steps of an algorithm
definition may include branching or repetition depending upon what problem the
algorithm is being developed for.
• algorithm has five important features:
• Finiteness. An algorithm must always terminate after a finite number of steps.
• Definiteness. Each step of an algorithm must be precisely defined; the actions to
be carried out must be rigorously and unambiguously specified for each case.
• Input. An algorithm has zero or more inputs, i.e, quantities which are given to it
initially before the algorithm begins.
• Output. An algorithm has one or more outputs i.e, quantities which have a
specified relation to the inputs.
• Effectiveness. An algorithm is also generally expected to be effective.
Insertion Sort
• We take an unsorted array for our example.
• Insertion sort compares the first two elements.
• It finds that both 14 and 33 are already in
ascending order. For now, 14 is in sorted sub-list.
• Insertion sort moves ahead and compares 33
with 27.
• And finds that 33 is not in the correct position..
Algorithm in computer science
It swaps 33 with 27. It also checks with all the elements of sorted sub-list.
Here we see that the sorted sub-list has only one element 14, and 27 is
greater than 14. Hence, the sorted sub-list remains sorted after swapping.
By now we have 14 and 27 in the sorted sub-list. Next, it compares 33 with
10.
These values are not in a sorted order.
So we swap them.
However, swapping makes 27 and 10 unsorted.
Hence, we swap them too.
Again we find 14 and 10 in an unsorted order.
We swap them again. By the end of third iteration, we have a sorted sub-list
of 4 items.
Best Case Of Insertion sort
• Basically, it is saying:
-Suppose the insert function, at most,
performs 17 comparisons each time it is called
(because the array is almost sorted )
-A comparison costs c and we perform 17 of
them per insert, so the cost of an insert is 17 *
c
Worst Case
• Suppose that the array starts out in a random order. Then, on average,
we'd expect that each element is less than half the elements to its left. In
this case, on average, a call to insert on a subarray of kk elements would
slide k/2k/2 of them. The running time would be half of the worst-case
running time. But in asymptotic notation, where constant coefficients
don't matter, the running time in the average case would still
be Theta(n^2)Θ(n​2​​), just like the worst case.
• What if you knew that the array was "almost sorted": every element starts
out at most some constant number of positions, say 17, from where it's
supposed to be when sorted? Then each call to insert slides at most 17
elements, and the time for one call of insert on a subarray of kk elements
would be at most 17 cdot c17⋅c. Over all n-1n−1 calls to insert, the
running time would be 17 cdot c cdot (n-1)17⋅c⋅(n−1), which
is Theta(n)Θ(n), just like the best case. So insertion sort is fast when given
an almost-sorted array.
• To sum up the running times for insertion sort:
• Worst case: Theta(n^2)Θ(n​2​​).
• Best case: Theta(n)Θ(n).
• Average case for a random array: Theta(n^2)Θ(n​2​​).
• "Almost sorted" case: Theta(n)Θ(n).
•
• If you had to make a blanket statement that applies to all
cases of insertion sort, you would have to say that it runs
in O(n^2)O(n​2​​) time. You cannot say that it runs
in Theta(n^2)Θ(n​2​​) time in all cases, since the best case
runs in Theta(n)Θ(n) time. And you cannot say that it runs
in Theta(n)Θ(n) time in all cases, since the worst-case
running time is Theta(n^2)Θ(n​2​​).
Ad

More Related Content

What's hot (20)

Algorithm Design and Complexity - Course 4 - Heaps and Dynamic Progamming
Algorithm Design and Complexity - Course 4 - Heaps and Dynamic ProgammingAlgorithm Design and Complexity - Course 4 - Heaps and Dynamic Progamming
Algorithm Design and Complexity - Course 4 - Heaps and Dynamic Progamming
Traian Rebedea
 
test pre
test pretest pre
test pre
farazch
 
Introduction to the AKS Primality Test
Introduction to the AKS Primality TestIntroduction to the AKS Primality Test
Introduction to the AKS Primality Test
Pranshu Bhatnagar
 
5.1 greedyyy 02
5.1 greedyyy 025.1 greedyyy 02
5.1 greedyyy 02
Krish_ver2
 
Chapter 17
Chapter 17Chapter 17
Chapter 17
ashish bansal
 
Optimization using soft computing
Optimization using soft computingOptimization using soft computing
Optimization using soft computing
Purnima Pandit
 
36 greedy
36 greedy36 greedy
36 greedy
Ikram Khan
 
01. design & analysis of agorithm intro & complexity analysis
01. design & analysis of agorithm intro & complexity analysis01. design & analysis of agorithm intro & complexity analysis
01. design & analysis of agorithm intro & complexity analysis
Onkar Nath Sharma
 
daa-unit-3-greedy method
daa-unit-3-greedy methoddaa-unit-3-greedy method
daa-unit-3-greedy method
hodcsencet
 
Riemann Hypothesis and Natural Functions
Riemann Hypothesis and Natural FunctionsRiemann Hypothesis and Natural Functions
Riemann Hypothesis and Natural Functions
Kannan Nambiar
 
Riemann Hypothesis
Riemann HypothesisRiemann Hypothesis
Riemann Hypothesis
barnetdh
 
Sienna 13 limitations
Sienna 13 limitationsSienna 13 limitations
Sienna 13 limitations
chidabdu
 
Searching techniques
Searching techniquesSearching techniques
Searching techniques
Prof.Dharmishtha R. Chaudhari
 
Skiena algorithm 2007 lecture08 quicksort
Skiena algorithm 2007 lecture08 quicksortSkiena algorithm 2007 lecture08 quicksort
Skiena algorithm 2007 lecture08 quicksort
zukun
 
Ch2 probability and random variables pg 81
Ch2 probability and random variables pg 81Ch2 probability and random variables pg 81
Ch2 probability and random variables pg 81
Prateek Omer
 
Signal Processing Assignment Help
Signal Processing Assignment HelpSignal Processing Assignment Help
Signal Processing Assignment Help
Matlab Assignment Experts
 
Design and analysis of ra sort
Design and analysis of ra sortDesign and analysis of ra sort
Design and analysis of ra sort
ijfcstjournal
 
1 6
1 61 6
1 6
manish katara
 
Merge sort
Merge sortMerge sort
Merge sort
lakshitha perera
 
Numerical Methods
Numerical MethodsNumerical Methods
Numerical Methods
ESUG
 
Algorithm Design and Complexity - Course 4 - Heaps and Dynamic Progamming
Algorithm Design and Complexity - Course 4 - Heaps and Dynamic ProgammingAlgorithm Design and Complexity - Course 4 - Heaps and Dynamic Progamming
Algorithm Design and Complexity - Course 4 - Heaps and Dynamic Progamming
Traian Rebedea
 
test pre
test pretest pre
test pre
farazch
 
Introduction to the AKS Primality Test
Introduction to the AKS Primality TestIntroduction to the AKS Primality Test
Introduction to the AKS Primality Test
Pranshu Bhatnagar
 
5.1 greedyyy 02
5.1 greedyyy 025.1 greedyyy 02
5.1 greedyyy 02
Krish_ver2
 
Optimization using soft computing
Optimization using soft computingOptimization using soft computing
Optimization using soft computing
Purnima Pandit
 
01. design & analysis of agorithm intro & complexity analysis
01. design & analysis of agorithm intro & complexity analysis01. design & analysis of agorithm intro & complexity analysis
01. design & analysis of agorithm intro & complexity analysis
Onkar Nath Sharma
 
daa-unit-3-greedy method
daa-unit-3-greedy methoddaa-unit-3-greedy method
daa-unit-3-greedy method
hodcsencet
 
Riemann Hypothesis and Natural Functions
Riemann Hypothesis and Natural FunctionsRiemann Hypothesis and Natural Functions
Riemann Hypothesis and Natural Functions
Kannan Nambiar
 
Riemann Hypothesis
Riemann HypothesisRiemann Hypothesis
Riemann Hypothesis
barnetdh
 
Sienna 13 limitations
Sienna 13 limitationsSienna 13 limitations
Sienna 13 limitations
chidabdu
 
Skiena algorithm 2007 lecture08 quicksort
Skiena algorithm 2007 lecture08 quicksortSkiena algorithm 2007 lecture08 quicksort
Skiena algorithm 2007 lecture08 quicksort
zukun
 
Ch2 probability and random variables pg 81
Ch2 probability and random variables pg 81Ch2 probability and random variables pg 81
Ch2 probability and random variables pg 81
Prateek Omer
 
Design and analysis of ra sort
Design and analysis of ra sortDesign and analysis of ra sort
Design and analysis of ra sort
ijfcstjournal
 
Numerical Methods
Numerical MethodsNumerical Methods
Numerical Methods
ESUG
 

Similar to Algorithm in computer science (20)

Divide and Conquer / Greedy Techniques
Divide and Conquer / Greedy TechniquesDivide and Conquer / Greedy Techniques
Divide and Conquer / Greedy Techniques
Nirmalavenkatachalam
 
Module 2_ Divide and Conquer Approach.pptx
Module 2_ Divide and Conquer Approach.pptxModule 2_ Divide and Conquer Approach.pptx
Module 2_ Divide and Conquer Approach.pptx
nikshaikh786
 
algorithm Unit 2
algorithm Unit 2 algorithm Unit 2
algorithm Unit 2
Monika Choudhery
 
Unit 2 in daa
Unit 2 in daaUnit 2 in daa
Unit 2 in daa
Nv Thejaswini
 
Lecture 8 dynamic programming
Lecture 8 dynamic programmingLecture 8 dynamic programming
Lecture 8 dynamic programming
Oye Tu
 
Design and analysis of algorithm in Computer Science
Design and analysis of algorithm in Computer ScienceDesign and analysis of algorithm in Computer Science
Design and analysis of algorithm in Computer Science
secularistpartyofind
 
DS & Algo 6 - Dynamic Programming
DS & Algo 6 - Dynamic ProgrammingDS & Algo 6 - Dynamic Programming
DS & Algo 6 - Dynamic Programming
Mohammad Imam Hossain
 
Data analysis and algorithms - UNIT 2.pptx
Data analysis and algorithms - UNIT 2.pptxData analysis and algorithms - UNIT 2.pptx
Data analysis and algorithms - UNIT 2.pptx
sgrishma559
 
Bt0080 fundamentals of algorithms1
Bt0080 fundamentals of algorithms1Bt0080 fundamentals of algorithms1
Bt0080 fundamentals of algorithms1
Techglyphs
 
TIME EXECUTION OF DIFFERENT SORTED ALGORITHMS
TIME EXECUTION   OF  DIFFERENT SORTED ALGORITHMSTIME EXECUTION   OF  DIFFERENT SORTED ALGORITHMS
TIME EXECUTION OF DIFFERENT SORTED ALGORITHMS
Tanya Makkar
 
Are there trends, changes in the mi.pptx
Are there trends, changes in the mi.pptxAre there trends, changes in the mi.pptx
Are there trends, changes in the mi.pptx
priyaaajadhav31
 
Big O Notation
Big O NotationBig O Notation
Big O Notation
Marcello Missiroli
 
Ada notes
Ada notesAda notes
Ada notes
VIKAS SINGH BHADOURIA
 
DS & Algo 3 - Divide and Conquer
DS & Algo 3 - Divide and ConquerDS & Algo 3 - Divide and Conquer
DS & Algo 3 - Divide and Conquer
Mohammad Imam Hossain
 
algorithm assignmenteeeeeee.pptx
algorithm assignmenteeeeeee.pptxalgorithm assignmenteeeeeee.pptx
algorithm assignmenteeeeeee.pptx
kassahungebrie
 
Analysis and Design of Algorithms notes
Analysis and Design of Algorithms  notesAnalysis and Design of Algorithms  notes
Analysis and Design of Algorithms notes
Prof. Dr. K. Adisesha
 
Exploring Algorithms
Exploring AlgorithmsExploring Algorithms
Exploring Algorithms
Sri Prasanna
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
Jay Nagar
 
Discrete structure ch 3 short question's
Discrete structure ch 3 short question'sDiscrete structure ch 3 short question's
Discrete structure ch 3 short question's
hammad463061
 
Design and analysis of algorithms unit1.pptx
Design and analysis of algorithms unit1.pptxDesign and analysis of algorithms unit1.pptx
Design and analysis of algorithms unit1.pptx
ShivaniSharma335055
 
Divide and Conquer / Greedy Techniques
Divide and Conquer / Greedy TechniquesDivide and Conquer / Greedy Techniques
Divide and Conquer / Greedy Techniques
Nirmalavenkatachalam
 
Module 2_ Divide and Conquer Approach.pptx
Module 2_ Divide and Conquer Approach.pptxModule 2_ Divide and Conquer Approach.pptx
Module 2_ Divide and Conquer Approach.pptx
nikshaikh786
 
Lecture 8 dynamic programming
Lecture 8 dynamic programmingLecture 8 dynamic programming
Lecture 8 dynamic programming
Oye Tu
 
Design and analysis of algorithm in Computer Science
Design and analysis of algorithm in Computer ScienceDesign and analysis of algorithm in Computer Science
Design and analysis of algorithm in Computer Science
secularistpartyofind
 
Data analysis and algorithms - UNIT 2.pptx
Data analysis and algorithms - UNIT 2.pptxData analysis and algorithms - UNIT 2.pptx
Data analysis and algorithms - UNIT 2.pptx
sgrishma559
 
Bt0080 fundamentals of algorithms1
Bt0080 fundamentals of algorithms1Bt0080 fundamentals of algorithms1
Bt0080 fundamentals of algorithms1
Techglyphs
 
TIME EXECUTION OF DIFFERENT SORTED ALGORITHMS
TIME EXECUTION   OF  DIFFERENT SORTED ALGORITHMSTIME EXECUTION   OF  DIFFERENT SORTED ALGORITHMS
TIME EXECUTION OF DIFFERENT SORTED ALGORITHMS
Tanya Makkar
 
Are there trends, changes in the mi.pptx
Are there trends, changes in the mi.pptxAre there trends, changes in the mi.pptx
Are there trends, changes in the mi.pptx
priyaaajadhav31
 
algorithm assignmenteeeeeee.pptx
algorithm assignmenteeeeeee.pptxalgorithm assignmenteeeeeee.pptx
algorithm assignmenteeeeeee.pptx
kassahungebrie
 
Analysis and Design of Algorithms notes
Analysis and Design of Algorithms  notesAnalysis and Design of Algorithms  notes
Analysis and Design of Algorithms notes
Prof. Dr. K. Adisesha
 
Exploring Algorithms
Exploring AlgorithmsExploring Algorithms
Exploring Algorithms
Sri Prasanna
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
Jay Nagar
 
Discrete structure ch 3 short question's
Discrete structure ch 3 short question'sDiscrete structure ch 3 short question's
Discrete structure ch 3 short question's
hammad463061
 
Design and analysis of algorithms unit1.pptx
Design and analysis of algorithms unit1.pptxDesign and analysis of algorithms unit1.pptx
Design and analysis of algorithms unit1.pptx
ShivaniSharma335055
 
Ad

More from Riazul Islam (7)

Introduction wireless communication network
Introduction wireless communication networkIntroduction wireless communication network
Introduction wireless communication network
Riazul Islam
 
Data link control in computer networks
Data link control in computer networksData link control in computer networks
Data link control in computer networks
Riazul Islam
 
Optical communication in communication engineering
Optical communication in communication engineeringOptical communication in communication engineering
Optical communication in communication engineering
Riazul Islam
 
Channel capacity and coding in communication engineering
Channel capacity and coding in communication engineeringChannel capacity and coding in communication engineering
Channel capacity and coding in communication engineering
Riazul Islam
 
Finite Automata in compiler design
Finite Automata in compiler designFinite Automata in compiler design
Finite Automata in compiler design
Riazul Islam
 
Regular Expression in Compiler design
Regular Expression in Compiler designRegular Expression in Compiler design
Regular Expression in Compiler design
Riazul Islam
 
Compiler Design LR parsing SLR ,LALR CLR
Compiler Design LR parsing SLR ,LALR CLRCompiler Design LR parsing SLR ,LALR CLR
Compiler Design LR parsing SLR ,LALR CLR
Riazul Islam
 
Introduction wireless communication network
Introduction wireless communication networkIntroduction wireless communication network
Introduction wireless communication network
Riazul Islam
 
Data link control in computer networks
Data link control in computer networksData link control in computer networks
Data link control in computer networks
Riazul Islam
 
Optical communication in communication engineering
Optical communication in communication engineeringOptical communication in communication engineering
Optical communication in communication engineering
Riazul Islam
 
Channel capacity and coding in communication engineering
Channel capacity and coding in communication engineeringChannel capacity and coding in communication engineering
Channel capacity and coding in communication engineering
Riazul Islam
 
Finite Automata in compiler design
Finite Automata in compiler designFinite Automata in compiler design
Finite Automata in compiler design
Riazul Islam
 
Regular Expression in Compiler design
Regular Expression in Compiler designRegular Expression in Compiler design
Regular Expression in Compiler design
Riazul Islam
 
Compiler Design LR parsing SLR ,LALR CLR
Compiler Design LR parsing SLR ,LALR CLRCompiler Design LR parsing SLR ,LALR CLR
Compiler Design LR parsing SLR ,LALR CLR
Riazul Islam
 
Ad

Recently uploaded (20)

U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptxU3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
Mayuri Chavan
 
UPMVLE migration to ARAL. A step- by- step guide
UPMVLE migration to ARAL. A step- by- step guideUPMVLE migration to ARAL. A step- by- step guide
UPMVLE migration to ARAL. A step- by- step guide
abmerca
 
How to Manage Upselling in Odoo 18 Sales
How to Manage Upselling in Odoo 18 SalesHow to Manage Upselling in Odoo 18 Sales
How to Manage Upselling in Odoo 18 Sales
Celine George
 
Bridging the Transit Gap: Equity Drive Feeder Bus Design for Southeast Brooklyn
Bridging the Transit Gap: Equity Drive Feeder Bus Design for Southeast BrooklynBridging the Transit Gap: Equity Drive Feeder Bus Design for Southeast Brooklyn
Bridging the Transit Gap: Equity Drive Feeder Bus Design for Southeast Brooklyn
i4jd41bk
 
The role of wall art in interior designing
The role of wall art in interior designingThe role of wall art in interior designing
The role of wall art in interior designing
meghaark2110
 
puzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tensepuzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tense
OlgaLeonorTorresSnch
 
Cultivation Practice of Garlic in Nepal.pptx
Cultivation Practice of Garlic in Nepal.pptxCultivation Practice of Garlic in Nepal.pptx
Cultivation Practice of Garlic in Nepal.pptx
UmeshTimilsina1
 
Ajanta Paintings: Study as a Source of History
Ajanta Paintings: Study as a Source of HistoryAjanta Paintings: Study as a Source of History
Ajanta Paintings: Study as a Source of History
Virag Sontakke
 
How to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 PurchaseHow to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 Purchase
Celine George
 
LDMMIA Reiki News Ed3 Vol1 For Team and Guests
LDMMIA Reiki News Ed3 Vol1 For Team and GuestsLDMMIA Reiki News Ed3 Vol1 For Team and Guests
LDMMIA Reiki News Ed3 Vol1 For Team and Guests
LDM Mia eStudios
 
Transform tomorrow: Master benefits analysis with Gen AI today webinar, 30 A...
Transform tomorrow: Master benefits analysis with Gen AI today webinar,  30 A...Transform tomorrow: Master benefits analysis with Gen AI today webinar,  30 A...
Transform tomorrow: Master benefits analysis with Gen AI today webinar, 30 A...
Association for Project Management
 
Myopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduateMyopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduate
Mohamed Rizk Khodair
 
Origin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theoriesOrigin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theories
PrachiSontakke5
 
*"The Segmented Blueprint: Unlocking Insect Body Architecture"*.pptx
*"The Segmented Blueprint: Unlocking Insect Body Architecture"*.pptx*"The Segmented Blueprint: Unlocking Insect Body Architecture"*.pptx
*"The Segmented Blueprint: Unlocking Insect Body Architecture"*.pptx
Arshad Shaikh
 
antiquity of writing in ancient India- literary & archaeological evidence
antiquity of writing in ancient India- literary & archaeological evidenceantiquity of writing in ancient India- literary & archaeological evidence
antiquity of writing in ancient India- literary & archaeological evidence
PrachiSontakke5
 
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptxTERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
PoojaSen20
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
Nguyen Thanh Tu Collection
 
Ancient Stone Sculptures of India: As a Source of Indian History
Ancient Stone Sculptures of India: As a Source of Indian HistoryAncient Stone Sculptures of India: As a Source of Indian History
Ancient Stone Sculptures of India: As a Source of Indian History
Virag Sontakke
 
Overview Well-Being and Creative Careers
Overview Well-Being and Creative CareersOverview Well-Being and Creative Careers
Overview Well-Being and Creative Careers
University of Amsterdam
 
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptxU3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
Mayuri Chavan
 
UPMVLE migration to ARAL. A step- by- step guide
UPMVLE migration to ARAL. A step- by- step guideUPMVLE migration to ARAL. A step- by- step guide
UPMVLE migration to ARAL. A step- by- step guide
abmerca
 
How to Manage Upselling in Odoo 18 Sales
How to Manage Upselling in Odoo 18 SalesHow to Manage Upselling in Odoo 18 Sales
How to Manage Upselling in Odoo 18 Sales
Celine George
 
Bridging the Transit Gap: Equity Drive Feeder Bus Design for Southeast Brooklyn
Bridging the Transit Gap: Equity Drive Feeder Bus Design for Southeast BrooklynBridging the Transit Gap: Equity Drive Feeder Bus Design for Southeast Brooklyn
Bridging the Transit Gap: Equity Drive Feeder Bus Design for Southeast Brooklyn
i4jd41bk
 
The role of wall art in interior designing
The role of wall art in interior designingThe role of wall art in interior designing
The role of wall art in interior designing
meghaark2110
 
puzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tensepuzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tense
OlgaLeonorTorresSnch
 
Cultivation Practice of Garlic in Nepal.pptx
Cultivation Practice of Garlic in Nepal.pptxCultivation Practice of Garlic in Nepal.pptx
Cultivation Practice of Garlic in Nepal.pptx
UmeshTimilsina1
 
Ajanta Paintings: Study as a Source of History
Ajanta Paintings: Study as a Source of HistoryAjanta Paintings: Study as a Source of History
Ajanta Paintings: Study as a Source of History
Virag Sontakke
 
How to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 PurchaseHow to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 Purchase
Celine George
 
LDMMIA Reiki News Ed3 Vol1 For Team and Guests
LDMMIA Reiki News Ed3 Vol1 For Team and GuestsLDMMIA Reiki News Ed3 Vol1 For Team and Guests
LDMMIA Reiki News Ed3 Vol1 For Team and Guests
LDM Mia eStudios
 
Transform tomorrow: Master benefits analysis with Gen AI today webinar, 30 A...
Transform tomorrow: Master benefits analysis with Gen AI today webinar,  30 A...Transform tomorrow: Master benefits analysis with Gen AI today webinar,  30 A...
Transform tomorrow: Master benefits analysis with Gen AI today webinar, 30 A...
Association for Project Management
 
Myopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduateMyopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduate
Mohamed Rizk Khodair
 
Origin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theoriesOrigin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theories
PrachiSontakke5
 
*"The Segmented Blueprint: Unlocking Insect Body Architecture"*.pptx
*"The Segmented Blueprint: Unlocking Insect Body Architecture"*.pptx*"The Segmented Blueprint: Unlocking Insect Body Architecture"*.pptx
*"The Segmented Blueprint: Unlocking Insect Body Architecture"*.pptx
Arshad Shaikh
 
antiquity of writing in ancient India- literary & archaeological evidence
antiquity of writing in ancient India- literary & archaeological evidenceantiquity of writing in ancient India- literary & archaeological evidence
antiquity of writing in ancient India- literary & archaeological evidence
PrachiSontakke5
 
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptxTERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
PoojaSen20
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
Nguyen Thanh Tu Collection
 
Ancient Stone Sculptures of India: As a Source of Indian History
Ancient Stone Sculptures of India: As a Source of Indian HistoryAncient Stone Sculptures of India: As a Source of Indian History
Ancient Stone Sculptures of India: As a Source of Indian History
Virag Sontakke
 
Overview Well-Being and Creative Careers
Overview Well-Being and Creative CareersOverview Well-Being and Creative Careers
Overview Well-Being and Creative Careers
University of Amsterdam
 

Algorithm in computer science

  • 1. Presentation Previous Year Question Solution(2005) By: To: Md. Mehdi Hasan Md. Mahfuz Reza Id: CE-14002 Assistant Professor, 2nd Year 2nd Semister, Dept Of Cse, Dept Of Cse,MBSTU. MBSTU
  • 2. What Is Meant By Divide & Conquer Algorithm??? • In divide and conquer approach, the problem in hand, is divided into smaller sub-problems and then each problem is solved independently. When we keep on dividing the subproblems into even smaller sub- problems, we may eventually reach a stage where no more division is possible. Those "atomic" smallest possible sub-problem (fractions) are solved. The solution of all sub-problems is finally merged in order to obtain the solution of an original problem. • Broadly, we can understand divide-and- conquer approach in a three-step process.
  • 3. Advantages Of Divide & Conquer Algorithm • The first, and probably most recognizable benefit of the divide and conquer paradigm is the fact that it allows us to solve difficult and often impossible looking problems, such as the Tower of Hanoi, which is a mathematical game or puzzle. Being given a difficult problem can often be discouraging if there is no idea how to go about solving it. However, with the divide and conquer method, it reduces the degree of difficulty since it divides the problem into sub problems that are easily solvable, and usually runs faster than other algorithms would. Another advantage to this paradigm is that it often plays a part in finding other efficient algorithms, and in fact it was the central role in finding the quick sort and merge sort algorithms.
  • 5. Min Max Using Divide & Conquer Algorithm • Divide the array into two parts and compare the maximums and minimums of the the two parts to get the maximum and the minimum of the the whole array. • Pair MaxMin(array, array_size) if array_size = 1 return element as both max and min else if arry_size = 2 one comparison to determine max and min return that pair else /* array_size > 2 */ recur for max and min of left half recur for max and min of right half one comparison determines true max of the two candidates one comparison determines true min of the two candidates return the pair of max and min Implementation • /* structure is used to return two values from minMax() */ • #include<stdio.h> • struct pair • { • int min; • int max; • }; •
  • 6. • struct pair getMinMax(int arr[], int low, int high) • { • struct pair minmax, mml, mmr; • int mid; • • /* If there is only on element */ • if (low == high) • { • minmax.max = arr[low]; • minmax.min = arr[low]; • return minmax; • } •
  • 7. • /* If there are two elements */ • if (high == low + 1) • { • if (arr[low] > arr[high]) • { • minmax.max = arr[low]; • minmax.min = arr[high]; • }
  • 8. • else • { • minmax.max = arr[high]; • minmax.min = arr[low]; • } • return minmax; • } • • /* If there are more than 2 elements */ • mid = (low + high)/2; • mml = getMinMax(arr, low, mid); • mmr = getMinMax(arr, mid+1, high); • • /* compare minimums of two parts*/ • if (mml.min < mmr.min) • minmax.min = mml.min;
  • 9. • else • minmax.min = mmr.min; • • /* compare maximums of two parts*/ • if (mml.max > mmr.max) • minmax.max = mml.max; • else • minmax.max = mmr.max; • • return minmax; • } •
  • 10. • /* Driver program to test above function */ • int main() • { • int arr[] = {1000, 11, 445, 1, 330, 3000}; • int arr_size = 6; • struct pair minmax = getMinMax(arr, 0, arr_size-1); • printf("nMinimum element is %d", minmax.min); • printf("nMaximum element is %d", minmax.max); • getchar(); • }
  • 11. Basic Algo • Pair MaxMin(array, array_size) if array_size = 1 return element as both max and min else if arry_size = 2 one comparison to determine max and min return that pair else /* array_size > 2 */ recur for max and min of left half recur for max and min of right half one comparison determines true max of the two candidates one comparison determines true min of the two candidates return the pair of max and min.
  • 12. Distinguish Between Divide & Conquer & Dp • Divide and Conquer basically works in three steps. 1. Divide - It first divides the problem into small chunks or sub-problems. 2. Conquer - It then solve those sub-problems recursively so as to obtain a separate result for each sub-problem. 3. Combine - It then combine the results of those sub- problems to arrive at a final result of the main problem. Some Divide and Conquer algorithms are Merge Sort, Binary Sort, etc.
  • 13. • Dynamic Programming is similar to Divide and Conquer when it comes to dividing a large problem into sub-problems. But here, each sub-problem is solved only once. There is no recursion. The key in dynamic programming is remembering. That is why we store the result of sub-problems in a table so that we don't have to compute the result of a same sub-problem again and again. Some algorithms that are solved using Dynamic Programming are Matrix Chain Multiplication, Tower of Hanoi puzzle, etc.. Another difference between Dynamic Programming and Divide and Conquer approach is that - In Divide and Conquer, the sub-problems are independent of each other while in case of Dynamic Programming, the sub-problems are not independent of each other (Solution of one sub-problem may be required to solve another sub-problem).
  • 14. Advantages OF Dp • Finding no. of ways kind of problems, can also be done by combinatorial formulae, which means the input size can be as big as 10^9(with some modulo in the end), but DP will time out. Finding optimal solution type of problems has the same problem. Sometimes, greedy works, which means, the complexity of solution can be much lower, and DP again times out. Even though DP won't give a wrong solution, but if greedy is O(n), DP will likely be more than O(n) , because DP searches a large part of the solution space, which is usually some orders bigger than n^1.
  • 15. What Is Algorithm??? • An algorithm is defined as a step-by-step procedure or method for solving a problem by a computer in a finite number of steps. Steps of an algorithm definition may include branching or repetition depending upon what problem the algorithm is being developed for. • algorithm has five important features: • Finiteness. An algorithm must always terminate after a finite number of steps. • Definiteness. Each step of an algorithm must be precisely defined; the actions to be carried out must be rigorously and unambiguously specified for each case. • Input. An algorithm has zero or more inputs, i.e, quantities which are given to it initially before the algorithm begins. • Output. An algorithm has one or more outputs i.e, quantities which have a specified relation to the inputs. • Effectiveness. An algorithm is also generally expected to be effective.
  • 16. Insertion Sort • We take an unsorted array for our example. • Insertion sort compares the first two elements. • It finds that both 14 and 33 are already in ascending order. For now, 14 is in sorted sub-list. • Insertion sort moves ahead and compares 33 with 27. • And finds that 33 is not in the correct position..
  • 18. It swaps 33 with 27. It also checks with all the elements of sorted sub-list. Here we see that the sorted sub-list has only one element 14, and 27 is greater than 14. Hence, the sorted sub-list remains sorted after swapping. By now we have 14 and 27 in the sorted sub-list. Next, it compares 33 with 10. These values are not in a sorted order. So we swap them. However, swapping makes 27 and 10 unsorted. Hence, we swap them too. Again we find 14 and 10 in an unsorted order. We swap them again. By the end of third iteration, we have a sorted sub-list of 4 items.
  • 19. Best Case Of Insertion sort • Basically, it is saying: -Suppose the insert function, at most, performs 17 comparisons each time it is called (because the array is almost sorted ) -A comparison costs c and we perform 17 of them per insert, so the cost of an insert is 17 * c
  • 20. Worst Case • Suppose that the array starts out in a random order. Then, on average, we'd expect that each element is less than half the elements to its left. In this case, on average, a call to insert on a subarray of kk elements would slide k/2k/2 of them. The running time would be half of the worst-case running time. But in asymptotic notation, where constant coefficients don't matter, the running time in the average case would still be Theta(n^2)Θ(n​2​​), just like the worst case. • What if you knew that the array was "almost sorted": every element starts out at most some constant number of positions, say 17, from where it's supposed to be when sorted? Then each call to insert slides at most 17 elements, and the time for one call of insert on a subarray of kk elements would be at most 17 cdot c17⋅c. Over all n-1n−1 calls to insert, the running time would be 17 cdot c cdot (n-1)17⋅c⋅(n−1), which is Theta(n)Θ(n), just like the best case. So insertion sort is fast when given an almost-sorted array.
  • 21. • To sum up the running times for insertion sort: • Worst case: Theta(n^2)Θ(n​2​​). • Best case: Theta(n)Θ(n). • Average case for a random array: Theta(n^2)Θ(n​2​​). • "Almost sorted" case: Theta(n)Θ(n). • • If you had to make a blanket statement that applies to all cases of insertion sort, you would have to say that it runs in O(n^2)O(n​2​​) time. You cannot say that it runs in Theta(n^2)Θ(n​2​​) time in all cases, since the best case runs in Theta(n)Θ(n) time. And you cannot say that it runs in Theta(n)Θ(n) time in all cases, since the worst-case running time is Theta(n^2)Θ(n​2​​).
  翻译: