SlideShare a Scribd company logo
1 of 34
Algorithm Strategies
Presented By M.Talha
MCS-13-08
Algorithms Strategy in Assignment
Decrease and Conquer
Greedy Approach
Backtracking
Transform and Conquer
Roadmap of Every Strategy
 Definition
 Types
 Example
 Advantages
 Disadvantages
 Algorithm
 Working
 Complexity
Decrease and Conquer
Definition
 _Change an instance into one smaller instance of the problem.
 _ Solve the smaller instance.
 _ Convert the solution of the smaller instance into a solution for the larger
Instance.
Decrease and Conquer
 Decrease by a Constant
 insertion sort
 graph traversal algorithms (DFS and BFS)
 topological sorting
 Decrease by a Constant Factor
 The Fake-coin problem
 Variable-size decrease
 Euclid’s algorithm
Decrease and Conquer
 Advantages
 Solve Smaller Instance
 Take less time
 Use for shortest path
 Disadvantages
 Depends on efficiency of sorting
Breadth-first search Algorithm
Definition
Breadth-first search (BFS) is an algorithm for traversing or
searching tree or graph data structures. It starts at the tree
root (or some arbitrary node of a graph, sometimes referred
to as a `search key'[1]) and explores the neighbor nodes first,
before moving to the next level neighbors.
Algorithm of BFS
 Procedure bfs (v)
 q: = make queue ()
 Enqueue (q, v)
 Mark v as visited
 While q is not empty
 v = dequeue (q)
 Process v
 for all unvisited vertices v' adjacent to v
 Mark v' as visited
 enqueue(q,v')
Order in which the nodes are expanded
Complexity
Θ (V2) if represented by an adjacency table,
Θ (|V| + |E|) if represented by adjacency lists
Greedy Approach
 Greedy algorithms are simple and straightforward.
 They are shortsighted in their approach in the sense that they take decisions on
the basis of information at hand without worrying about the effect these decisions
may have in the future.
 They are easy to invent, easy to implement and most of the time quite efficient.
 Many problems cannot be solved correctly by greedy approach.
 Greedy algorithms are used to solve optimization problems
Uses of Greedy Approach
 Greedy algorithms are often used in ad hoc mobile networking to efficiently
route packets with the fewest number of hops and the shortest delay
possible.
 They are also used in machine learning,
 business intelligence (BI),
 artificial intelligence (AI) and
 programming.
 Making Change Algorithm
 Huffman Encoding Algorithm
Greedy Approach
 Advantages :
 Very Large number of Feasible Solution.
 Easy to Implement.
 Disadvantages :
 It is much Slower.
 Does not give optimum result for all problems.
Huffman Encoding
 Huffman code is a particular type of optimal prefix code that is commonly
used for lossless data compression.

 The process of finding and/or using such a code proceeds by means of
Huffman codding.
 The output from Huffman's algorithm can be viewed as a variable-length code
table for encoding a source symbol (such as a character in a file).
http://en.wikipedia.org/wiki/Huffman_coding
Huffman Encoding Algorithm
 procedure Huffman(f)
 Input: An array f[1 _ _ _ n] of frequencies
 Output: An encoding tree with n leaves
 let H be a priority queue of integers, ordered by f
 for i = 1 to n: insert(H; i)
 for k = n + 1 to 2n 􀀀 1:
 i = deletemin(H); j = deletemin(H)
 create a node numbered k with children i; j
 f[k] = f[i] + f[j]
 insert(H; k)
http://en.wikipedia.org/wiki/Huffman_coding
Working of the Huffman Encoding Algorithm
http://en.wikipedia.org/wiki/Huffman_coding
Complexity
 we use a greedy O(n*log(n))
 http://www.siggraph.org/education/materials/HyperGraph/video/mpeg/mpe
gfaq/huffman_tutorial.html
Knapsack Problem
 You have a knapsack that has capacity (weight) W.
 You have several items 1 to n.
 Each item Ij has a weight wj and a benefit bj.
 You want to place a certain number of copies of each item
Ij in the knapsack so that :
 The knapsack weight capacity in not exceeded .
 the total benefits is maximal.
http://en.wikipedia.org/wiki/Knapsack_problem#Applic
ations
Knapsack Problem
http://en.wikipedia.org/wiki/Knapsack_problem#/medi
a/File:Knapsack.svg
Knapsack Problem Variants
 0/1 knapsack Problem
 The item cannot be divided.
 Fractional knapsack problem
 For instance, items are liquid or powders
 Solvable with a greedy algorithm
http://en.wikipedia.org/wiki/Knapsack_problem#Applic
ations
Greedy Knapsack - Fractional
 Much Easier
 For item Ij, let ratioj=bj/wj. This gives you the
benefit per measure of weight.
 Sort the items in descending order of rj.
 If the next item cannot fit into the knapsack
,break it and pick it partially just to fill the
knapsack.
http://en.wikipedia.org/wiki/Knapsack_problem#Applic
ations
Greedy Knapsack Algorithm
 P and W are arrays contain the
profit and weight n objects
ordered such that
p[i]/w[i]=>p[i+1]/w[1+i] that is in
decreasing order , m is the
knapsack size and x is the solution
vector.
 GreedyKnapsack(m,n)
• For(i=0;i<n;i++) {
• X[i]=0; }
• Int total=m;
• For (i=0; i<n; i++) {
• If(w[i] < = total){
• X[i]= 1;
• Total= total + w[i]; }
• Else
• X[i] = total / w[i];
• } // end Greedyknapsack
http://java90.blogspot.com/2012/02/knapsack-
problem-in-java.html
Example - Greedy Knapsack
Item A B C D
Benefit 50 140 60 60
weight 5 20 10 12
Ratio = B/W 10 7 6 5
•Example: Knapsack Capacity W = 30 and
http://www.radford.edu/~nokie/classes/360/greedy.ht
ml
Solution
All of A, all of B, and ((30-25)/10) of C (and none of D)
Weight: 5 + 20 + 10*(5/10) = 30
Value: 50 + 140 + 60*(5/10) = 190 + 30 = 220
http://www.radford.edu/~nokie/classes/360/greedy.ht
ml
Analysis
 If the items are already sorted into descending order of pi/wi
 Then the for-loop takes a time in O(n).
 Therefore , the Total time including the sort is in O(n log n).
http://java90.blogspot.com/2012/02/knapsack-
problem-in-java.html
Backtracking
 Backtracking is a technique for systematically trying all paths through a state
space.
 Backtracking search begins at the start state and pursues a path until it
reaches either a goal or a “dead end.”
 If it finds a goal, it quits and returns the solution path.
 If it reaches a dead end, it “backtracks” to the most recent node on the path
having unexamined siblings and continues down one of these branches,
en.wikipedia.org/wiki/Backtracking
Backtracking
 Backtracking is an important tool for solving
 constraint satisfaction problems, such as
 crosswords,
 verbal arithmetic,
 Sudoku,
 and many other puzzles.
 8 Queen Puzzle Algorithm is an example of Backtracking
en.wikipedia.org/wiki/Backtracking
Backtracking
 Advantages:
 Quick test
 Pair Matching
 Following real life Concept
 Disadvantages :
 Not widely Implemented
 Cannot Express Left Recursion
 More Time and Complexity
http://www.cl.cam.ac.uk/~mr10/backtrk.pdf
Eight queens puzzle
 The eight queens puzzle is the problem of placing eight chess
queens on an 8×8 chessboard so that no two queens threaten
each other.
 Thus, a solution requires that no two queens share the same
row, column, or diagonal.
http://en.wikipedia.org/wiki/Eight_queens_puzzle
Eight queens puzzle Algorithm
 Function backtrack;
 Begin
 SL: = [Start]; NSL: = [Start]; DE: = []; CS: = Start; % initialize:
 While NSL ≠ [ ] do % while there are states to be tried
 Begin
 If CS = goal (or meets goal description) then return SL; % on success,
return list of states in path.
 If CS has no children (excluding nodes already on DE, SL, and NSL) then
 Begin
 While SL is not empty and CS = the first element of SL do
Eight queens puzzle Algorithm
 Begin
 Add CS to DE;
 Remove first element from SL;
 Remove first element from NSL;
 CS: ~ first element of NSL:
 End
 Add CS to SL;
 End
Eight queens puzzle Algorithm
 Else begin
 Place children of CS (except nodes already on DE, SL, or NSL) on NSL;
 CS: = first element of NSL;
 Add CS to SL;
 End
 End;
Eight queens puzzle Algorithm
http://www.codeproject.com/Articles/806248/Eight-
Queens-Puzzle-Tree-Algorithm
Complexity
 The running time of your algorithm is at most N(N−1)(N−2)⋯(N−K+1), i.e.,
N!/(N−K)!. This is O(NK), i.e., exponential in K
 http://www.chegg.com/homework-help/questions-and-answers/poor-man-s-
n-queens-problemn-queens-arranged-n-x-n-chessboard-way-queen-checks-
queen-queen-q1009394

More Related Content

What's hot

Asymptotic Notation and Data Structures
Asymptotic Notation and Data StructuresAsymptotic Notation and Data Structures
Asymptotic Notation and Data StructuresAmrinder Arora
 
Dynamic programming - fundamentals review
Dynamic programming - fundamentals reviewDynamic programming - fundamentals review
Dynamic programming - fundamentals reviewElifTech
 
Daa:Dynamic Programing
Daa:Dynamic ProgramingDaa:Dynamic Programing
Daa:Dynamic Programingrupali_2bonde
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programmingJay Nagar
 
daa-unit-3-greedy method
daa-unit-3-greedy methoddaa-unit-3-greedy method
daa-unit-3-greedy methodhodcsencet
 
5.1 greedyyy 02
5.1 greedyyy 025.1 greedyyy 02
5.1 greedyyy 02Krish_ver2
 
Dynamic Programming - Part II
Dynamic Programming - Part IIDynamic Programming - Part II
Dynamic Programming - Part IIAmrinder Arora
 
design and analysis of algorithm
design and analysis of algorithmdesign and analysis of algorithm
design and analysis of algorithmMuhammad Arish
 
4 greedy methodnew
4 greedy methodnew4 greedy methodnew
4 greedy methodnewabhinav108
 
5.3 dynamic programming 03
5.3 dynamic programming 035.3 dynamic programming 03
5.3 dynamic programming 03Krish_ver2
 
Lecture 8 dynamic programming
Lecture 8 dynamic programmingLecture 8 dynamic programming
Lecture 8 dynamic programmingOye Tu
 
lecture 26
lecture 26lecture 26
lecture 26sajinsc
 
Algorithm Design and Complexity - Course 1&2
Algorithm Design and Complexity - Course 1&2Algorithm Design and Complexity - Course 1&2
Algorithm Design and Complexity - Course 1&2Traian Rebedea
 
Skiena algorithm 2007 lecture16 introduction to dynamic programming
Skiena algorithm 2007 lecture16 introduction to dynamic programmingSkiena algorithm 2007 lecture16 introduction to dynamic programming
Skiena algorithm 2007 lecture16 introduction to dynamic programmingzukun
 
Greedy Algorithm
Greedy AlgorithmGreedy Algorithm
Greedy AlgorithmWaqar Akram
 

What's hot (20)

Divide and Conquer
Divide and ConquerDivide and Conquer
Divide and Conquer
 
Asymptotic Notation and Data Structures
Asymptotic Notation and Data StructuresAsymptotic Notation and Data Structures
Asymptotic Notation and Data Structures
 
Dynamic programming - fundamentals review
Dynamic programming - fundamentals reviewDynamic programming - fundamentals review
Dynamic programming - fundamentals review
 
Daa:Dynamic Programing
Daa:Dynamic ProgramingDaa:Dynamic Programing
Daa:Dynamic Programing
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
 
daa-unit-3-greedy method
daa-unit-3-greedy methoddaa-unit-3-greedy method
daa-unit-3-greedy method
 
5.1 greedyyy 02
5.1 greedyyy 025.1 greedyyy 02
5.1 greedyyy 02
 
Dynamic Programming - Part II
Dynamic Programming - Part IIDynamic Programming - Part II
Dynamic Programming - Part II
 
design and analysis of algorithm
design and analysis of algorithmdesign and analysis of algorithm
design and analysis of algorithm
 
4 greedy methodnew
4 greedy methodnew4 greedy methodnew
4 greedy methodnew
 
5.3 dynamic programming 03
5.3 dynamic programming 035.3 dynamic programming 03
5.3 dynamic programming 03
 
Chapter 17
Chapter 17Chapter 17
Chapter 17
 
Lecture 8 dynamic programming
Lecture 8 dynamic programmingLecture 8 dynamic programming
Lecture 8 dynamic programming
 
lecture 26
lecture 26lecture 26
lecture 26
 
Daa unit 2
Daa unit 2Daa unit 2
Daa unit 2
 
Algorithm Design and Complexity - Course 1&2
Algorithm Design and Complexity - Course 1&2Algorithm Design and Complexity - Course 1&2
Algorithm Design and Complexity - Course 1&2
 
Greedy Algorithms with examples' b-18298
Greedy Algorithms with examples'  b-18298Greedy Algorithms with examples'  b-18298
Greedy Algorithms with examples' b-18298
 
Skiena algorithm 2007 lecture16 introduction to dynamic programming
Skiena algorithm 2007 lecture16 introduction to dynamic programmingSkiena algorithm 2007 lecture16 introduction to dynamic programming
Skiena algorithm 2007 lecture16 introduction to dynamic programming
 
algorithm unit 1
algorithm unit 1algorithm unit 1
algorithm unit 1
 
Greedy Algorithm
Greedy AlgorithmGreedy Algorithm
Greedy Algorithm
 

Viewers also liked

Algorithm analysis (All in one)
Algorithm analysis (All in one)Algorithm analysis (All in one)
Algorithm analysis (All in one)jehan1987
 
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...Tosin Amuda
 
Longest common subsequences in Algorithm Analysis
Longest common subsequences in Algorithm AnalysisLongest common subsequences in Algorithm Analysis
Longest common subsequences in Algorithm AnalysisRajendran
 
Greedy Knapsack Problem - by Y Achchuthan
Greedy Knapsack Problem  - by Y AchchuthanGreedy Knapsack Problem  - by Y Achchuthan
Greedy Knapsack Problem - by Y AchchuthanAchchuthan Yogarajah
 
Itec 299 final project suzie parada
Itec 299 final project suzie paradaItec 299 final project suzie parada
Itec 299 final project suzie paradaSuzie_Parada
 
Malawi Alternative Technology in Education Pilot
Malawi Alternative Technology in Education PilotMalawi Alternative Technology in Education Pilot
Malawi Alternative Technology in Education PilotMatt Finholt-Daniel
 
Careers Education Consultant hew 7 21.06.2013 (1)
Careers Education Consultant hew 7 21.06.2013 (1)Careers Education Consultant hew 7 21.06.2013 (1)
Careers Education Consultant hew 7 21.06.2013 (1)Joanne Tyler
 
Visueel comfort, thema licht bni dec 2009
Visueel comfort, thema licht bni dec 2009Visueel comfort, thema licht bni dec 2009
Visueel comfort, thema licht bni dec 2009maarten de regt
 
Quí serà la princesa?
Quí serà la princesa?Quí serà la princesa?
Quí serà la princesa?funmonkeybars
 
globalleadershipreadiness_wp_ddi final v4
globalleadershipreadiness_wp_ddi final v4globalleadershipreadiness_wp_ddi final v4
globalleadershipreadiness_wp_ddi final v4Jill George, Ph.D.
 
Learning styles
Learning stylesLearning styles
Learning stylesHisaAlali
 
Czy mobile sprzedaje w offline bez płatności mobilnych?
Czy mobile sprzedaje w offline bez płatności mobilnych?Czy mobile sprzedaje w offline bez płatności mobilnych?
Czy mobile sprzedaje w offline bez płatności mobilnych?scoupon
 

Viewers also liked (20)

Algorithm analysis (All in one)
Algorithm analysis (All in one)Algorithm analysis (All in one)
Algorithm analysis (All in one)
 
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
 
Longest common subsequences in Algorithm Analysis
Longest common subsequences in Algorithm AnalysisLongest common subsequences in Algorithm Analysis
Longest common subsequences in Algorithm Analysis
 
Randomized algorithms ver 1.0
Randomized algorithms ver 1.0Randomized algorithms ver 1.0
Randomized algorithms ver 1.0
 
Greedy Knapsack Problem - by Y Achchuthan
Greedy Knapsack Problem  - by Y AchchuthanGreedy Knapsack Problem  - by Y Achchuthan
Greedy Knapsack Problem - by Y Achchuthan
 
Knapsack problem
Knapsack problemKnapsack problem
Knapsack problem
 
Dtc
DtcDtc
Dtc
 
Itec 299 final project suzie parada
Itec 299 final project suzie paradaItec 299 final project suzie parada
Itec 299 final project suzie parada
 
Malawi Alternative Technology in Education Pilot
Malawi Alternative Technology in Education PilotMalawi Alternative Technology in Education Pilot
Malawi Alternative Technology in Education Pilot
 
Careers Education Consultant hew 7 21.06.2013 (1)
Careers Education Consultant hew 7 21.06.2013 (1)Careers Education Consultant hew 7 21.06.2013 (1)
Careers Education Consultant hew 7 21.06.2013 (1)
 
49629012 tgfu
49629012 tgfu49629012 tgfu
49629012 tgfu
 
Glb&amp;glbb&amp;gv
Glb&amp;glbb&amp;gvGlb&amp;glbb&amp;gv
Glb&amp;glbb&amp;gv
 
Loxantra
LoxantraLoxantra
Loxantra
 
Visueel comfort, thema licht bni dec 2009
Visueel comfort, thema licht bni dec 2009Visueel comfort, thema licht bni dec 2009
Visueel comfort, thema licht bni dec 2009
 
Quí serà la princesa?
Quí serà la princesa?Quí serà la princesa?
Quí serà la princesa?
 
globalleadershipreadiness_wp_ddi final v4
globalleadershipreadiness_wp_ddi final v4globalleadershipreadiness_wp_ddi final v4
globalleadershipreadiness_wp_ddi final v4
 
15 164-2-pb
15 164-2-pb15 164-2-pb
15 164-2-pb
 
Learning styles
Learning stylesLearning styles
Learning styles
 
Dhtic
DhticDhtic
Dhtic
 
Czy mobile sprzedaje w offline bez płatności mobilnych?
Czy mobile sprzedaje w offline bez płatności mobilnych?Czy mobile sprzedaje w offline bez płatności mobilnych?
Czy mobile sprzedaje w offline bez płatności mobilnych?
 

Similar to Comparitive Analysis of Algorithm strategies

Algorithms Design Patterns
Algorithms Design PatternsAlgorithms Design Patterns
Algorithms Design PatternsAshwin Shiv
 
Addressing Scenario
Addressing ScenarioAddressing Scenario
Addressing ScenarioTara Hardin
 
LECTURE 6 DESIGN, DEBUGGING, INTERFACES.pdf
LECTURE 6 DESIGN, DEBUGGING, INTERFACES.pdfLECTURE 6 DESIGN, DEBUGGING, INTERFACES.pdf
LECTURE 6 DESIGN, DEBUGGING, INTERFACES.pdfSHASHIKANT346021
 
Threaded Programming
Threaded ProgrammingThreaded Programming
Threaded ProgrammingSri Prasanna
 
LECTURE 6 DESIGN, DEBasd, INTERFACES.pdf
LECTURE 6 DESIGN, DEBasd, INTERFACES.pdfLECTURE 6 DESIGN, DEBasd, INTERFACES.pdf
LECTURE 6 DESIGN, DEBasd, INTERFACES.pdfShashikantSathe3
 
computer notes - Data Structures - 1
computer notes - Data Structures - 1computer notes - Data Structures - 1
computer notes - Data Structures - 1ecomputernotes
 
ScalaDays 2013 Keynote Speech by Martin Odersky
ScalaDays 2013 Keynote Speech by Martin OderskyScalaDays 2013 Keynote Speech by Martin Odersky
ScalaDays 2013 Keynote Speech by Martin OderskyTypesafe
 
Shape Safety in Tensor Programming is Easy for a Theorem Prover -SBTB 2021
Shape Safety in Tensor Programming is Easy for a Theorem Prover -SBTB 2021Shape Safety in Tensor Programming is Easy for a Theorem Prover -SBTB 2021
Shape Safety in Tensor Programming is Easy for a Theorem Prover -SBTB 2021Peng Cheng
 
SystemVerilog OOP Ovm Features Summary
SystemVerilog OOP Ovm Features SummarySystemVerilog OOP Ovm Features Summary
SystemVerilog OOP Ovm Features SummaryAmal Khailtash
 
Data structures cs301 power point slides lecture 01
Data structures   cs301 power point slides lecture 01Data structures   cs301 power point slides lecture 01
Data structures cs301 power point slides lecture 01shaziabibi5
 
Parallel Programming: Beyond the Critical Section
Parallel Programming: Beyond the Critical SectionParallel Programming: Beyond the Critical Section
Parallel Programming: Beyond the Critical SectionTony Albrecht
 
Bca2020 data structure and algorithm
Bca2020   data structure and algorithmBca2020   data structure and algorithm
Bca2020 data structure and algorithmsmumbahelp
 
Scala as a Declarative Language
Scala as a Declarative LanguageScala as a Declarative Language
Scala as a Declarative Languagevsssuresh
 
"How was it to switch from beautiful Perl to horrible JavaScript", Viktor Tur...
"How was it to switch from beautiful Perl to horrible JavaScript", Viktor Tur..."How was it to switch from beautiful Perl to horrible JavaScript", Viktor Tur...
"How was it to switch from beautiful Perl to horrible JavaScript", Viktor Tur...Fwdays
 

Similar to Comparitive Analysis of Algorithm strategies (20)

Algorithms Design Patterns
Algorithms Design PatternsAlgorithms Design Patterns
Algorithms Design Patterns
 
Addressing Scenario
Addressing ScenarioAddressing Scenario
Addressing Scenario
 
LECTURE 6 DESIGN, DEBUGGING, INTERFACES.pdf
LECTURE 6 DESIGN, DEBUGGING, INTERFACES.pdfLECTURE 6 DESIGN, DEBUGGING, INTERFACES.pdf
LECTURE 6 DESIGN, DEBUGGING, INTERFACES.pdf
 
Threaded Programming
Threaded ProgrammingThreaded Programming
Threaded Programming
 
LECTURE 6 DESIGN, DEBasd, INTERFACES.pdf
LECTURE 6 DESIGN, DEBasd, INTERFACES.pdfLECTURE 6 DESIGN, DEBasd, INTERFACES.pdf
LECTURE 6 DESIGN, DEBasd, INTERFACES.pdf
 
L04 Software Design Examples
L04 Software Design ExamplesL04 Software Design Examples
L04 Software Design Examples
 
computer notes - Data Structures - 1
computer notes - Data Structures - 1computer notes - Data Structures - 1
computer notes - Data Structures - 1
 
ScalaDays 2013 Keynote Speech by Martin Odersky
ScalaDays 2013 Keynote Speech by Martin OderskyScalaDays 2013 Keynote Speech by Martin Odersky
ScalaDays 2013 Keynote Speech by Martin Odersky
 
Shape Safety in Tensor Programming is Easy for a Theorem Prover -SBTB 2021
Shape Safety in Tensor Programming is Easy for a Theorem Prover -SBTB 2021Shape Safety in Tensor Programming is Easy for a Theorem Prover -SBTB 2021
Shape Safety in Tensor Programming is Easy for a Theorem Prover -SBTB 2021
 
SystemVerilog OOP Ovm Features Summary
SystemVerilog OOP Ovm Features SummarySystemVerilog OOP Ovm Features Summary
SystemVerilog OOP Ovm Features Summary
 
Data structures cs301 power point slides lecture 01
Data structures   cs301 power point slides lecture 01Data structures   cs301 power point slides lecture 01
Data structures cs301 power point slides lecture 01
 
Java tut1
Java tut1Java tut1
Java tut1
 
Tutorial java
Tutorial javaTutorial java
Tutorial java
 
Java Tut1
Java Tut1Java Tut1
Java Tut1
 
Java Tutorial
Java TutorialJava Tutorial
Java Tutorial
 
Parallel Programming: Beyond the Critical Section
Parallel Programming: Beyond the Critical SectionParallel Programming: Beyond the Critical Section
Parallel Programming: Beyond the Critical Section
 
Bca2020 data structure and algorithm
Bca2020   data structure and algorithmBca2020   data structure and algorithm
Bca2020 data structure and algorithm
 
Scala as a Declarative Language
Scala as a Declarative LanguageScala as a Declarative Language
Scala as a Declarative Language
 
"How was it to switch from beautiful Perl to horrible JavaScript", Viktor Tur...
"How was it to switch from beautiful Perl to horrible JavaScript", Viktor Tur..."How was it to switch from beautiful Perl to horrible JavaScript", Viktor Tur...
"How was it to switch from beautiful Perl to horrible JavaScript", Viktor Tur...
 
supervised.pptx
supervised.pptxsupervised.pptx
supervised.pptx
 

Recently uploaded

How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 

Recently uploaded (20)

How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 

Comparitive Analysis of Algorithm strategies

  • 2. Algorithms Strategy in Assignment Decrease and Conquer Greedy Approach Backtracking Transform and Conquer
  • 3. Roadmap of Every Strategy  Definition  Types  Example  Advantages  Disadvantages  Algorithm  Working  Complexity
  • 4. Decrease and Conquer Definition  _Change an instance into one smaller instance of the problem.  _ Solve the smaller instance.  _ Convert the solution of the smaller instance into a solution for the larger Instance.
  • 5. Decrease and Conquer  Decrease by a Constant  insertion sort  graph traversal algorithms (DFS and BFS)  topological sorting  Decrease by a Constant Factor  The Fake-coin problem  Variable-size decrease  Euclid’s algorithm
  • 6. Decrease and Conquer  Advantages  Solve Smaller Instance  Take less time  Use for shortest path  Disadvantages  Depends on efficiency of sorting
  • 7. Breadth-first search Algorithm Definition Breadth-first search (BFS) is an algorithm for traversing or searching tree or graph data structures. It starts at the tree root (or some arbitrary node of a graph, sometimes referred to as a `search key'[1]) and explores the neighbor nodes first, before moving to the next level neighbors.
  • 8. Algorithm of BFS  Procedure bfs (v)  q: = make queue ()  Enqueue (q, v)  Mark v as visited  While q is not empty  v = dequeue (q)  Process v  for all unvisited vertices v' adjacent to v  Mark v' as visited  enqueue(q,v')
  • 9. Order in which the nodes are expanded
  • 10. Complexity Θ (V2) if represented by an adjacency table, Θ (|V| + |E|) if represented by adjacency lists
  • 11. Greedy Approach  Greedy algorithms are simple and straightforward.  They are shortsighted in their approach in the sense that they take decisions on the basis of information at hand without worrying about the effect these decisions may have in the future.  They are easy to invent, easy to implement and most of the time quite efficient.  Many problems cannot be solved correctly by greedy approach.  Greedy algorithms are used to solve optimization problems
  • 12. Uses of Greedy Approach  Greedy algorithms are often used in ad hoc mobile networking to efficiently route packets with the fewest number of hops and the shortest delay possible.  They are also used in machine learning,  business intelligence (BI),  artificial intelligence (AI) and  programming.  Making Change Algorithm  Huffman Encoding Algorithm
  • 13. Greedy Approach  Advantages :  Very Large number of Feasible Solution.  Easy to Implement.  Disadvantages :  It is much Slower.  Does not give optimum result for all problems.
  • 14. Huffman Encoding  Huffman code is a particular type of optimal prefix code that is commonly used for lossless data compression.   The process of finding and/or using such a code proceeds by means of Huffman codding.  The output from Huffman's algorithm can be viewed as a variable-length code table for encoding a source symbol (such as a character in a file). http://en.wikipedia.org/wiki/Huffman_coding
  • 15. Huffman Encoding Algorithm  procedure Huffman(f)  Input: An array f[1 _ _ _ n] of frequencies  Output: An encoding tree with n leaves  let H be a priority queue of integers, ordered by f  for i = 1 to n: insert(H; i)  for k = n + 1 to 2n 􀀀 1:  i = deletemin(H); j = deletemin(H)  create a node numbered k with children i; j  f[k] = f[i] + f[j]  insert(H; k) http://en.wikipedia.org/wiki/Huffman_coding
  • 16. Working of the Huffman Encoding Algorithm http://en.wikipedia.org/wiki/Huffman_coding
  • 17. Complexity  we use a greedy O(n*log(n))  http://www.siggraph.org/education/materials/HyperGraph/video/mpeg/mpe gfaq/huffman_tutorial.html
  • 18. Knapsack Problem  You have a knapsack that has capacity (weight) W.  You have several items 1 to n.  Each item Ij has a weight wj and a benefit bj.  You want to place a certain number of copies of each item Ij in the knapsack so that :  The knapsack weight capacity in not exceeded .  the total benefits is maximal. http://en.wikipedia.org/wiki/Knapsack_problem#Applic ations
  • 20. Knapsack Problem Variants  0/1 knapsack Problem  The item cannot be divided.  Fractional knapsack problem  For instance, items are liquid or powders  Solvable with a greedy algorithm http://en.wikipedia.org/wiki/Knapsack_problem#Applic ations
  • 21. Greedy Knapsack - Fractional  Much Easier  For item Ij, let ratioj=bj/wj. This gives you the benefit per measure of weight.  Sort the items in descending order of rj.  If the next item cannot fit into the knapsack ,break it and pick it partially just to fill the knapsack. http://en.wikipedia.org/wiki/Knapsack_problem#Applic ations
  • 22. Greedy Knapsack Algorithm  P and W are arrays contain the profit and weight n objects ordered such that p[i]/w[i]=>p[i+1]/w[1+i] that is in decreasing order , m is the knapsack size and x is the solution vector.  GreedyKnapsack(m,n) • For(i=0;i<n;i++) { • X[i]=0; } • Int total=m; • For (i=0; i<n; i++) { • If(w[i] < = total){ • X[i]= 1; • Total= total + w[i]; } • Else • X[i] = total / w[i]; • } // end Greedyknapsack http://java90.blogspot.com/2012/02/knapsack- problem-in-java.html
  • 23. Example - Greedy Knapsack Item A B C D Benefit 50 140 60 60 weight 5 20 10 12 Ratio = B/W 10 7 6 5 •Example: Knapsack Capacity W = 30 and http://www.radford.edu/~nokie/classes/360/greedy.ht ml
  • 24. Solution All of A, all of B, and ((30-25)/10) of C (and none of D) Weight: 5 + 20 + 10*(5/10) = 30 Value: 50 + 140 + 60*(5/10) = 190 + 30 = 220 http://www.radford.edu/~nokie/classes/360/greedy.ht ml
  • 25. Analysis  If the items are already sorted into descending order of pi/wi  Then the for-loop takes a time in O(n).  Therefore , the Total time including the sort is in O(n log n). http://java90.blogspot.com/2012/02/knapsack- problem-in-java.html
  • 26. Backtracking  Backtracking is a technique for systematically trying all paths through a state space.  Backtracking search begins at the start state and pursues a path until it reaches either a goal or a “dead end.”  If it finds a goal, it quits and returns the solution path.  If it reaches a dead end, it “backtracks” to the most recent node on the path having unexamined siblings and continues down one of these branches, en.wikipedia.org/wiki/Backtracking
  • 27. Backtracking  Backtracking is an important tool for solving  constraint satisfaction problems, such as  crosswords,  verbal arithmetic,  Sudoku,  and many other puzzles.  8 Queen Puzzle Algorithm is an example of Backtracking en.wikipedia.org/wiki/Backtracking
  • 28. Backtracking  Advantages:  Quick test  Pair Matching  Following real life Concept  Disadvantages :  Not widely Implemented  Cannot Express Left Recursion  More Time and Complexity http://www.cl.cam.ac.uk/~mr10/backtrk.pdf
  • 29. Eight queens puzzle  The eight queens puzzle is the problem of placing eight chess queens on an 8×8 chessboard so that no two queens threaten each other.  Thus, a solution requires that no two queens share the same row, column, or diagonal. http://en.wikipedia.org/wiki/Eight_queens_puzzle
  • 30. Eight queens puzzle Algorithm  Function backtrack;  Begin  SL: = [Start]; NSL: = [Start]; DE: = []; CS: = Start; % initialize:  While NSL ≠ [ ] do % while there are states to be tried  Begin  If CS = goal (or meets goal description) then return SL; % on success, return list of states in path.  If CS has no children (excluding nodes already on DE, SL, and NSL) then  Begin  While SL is not empty and CS = the first element of SL do
  • 31. Eight queens puzzle Algorithm  Begin  Add CS to DE;  Remove first element from SL;  Remove first element from NSL;  CS: ~ first element of NSL:  End  Add CS to SL;  End
  • 32. Eight queens puzzle Algorithm  Else begin  Place children of CS (except nodes already on DE, SL, or NSL) on NSL;  CS: = first element of NSL;  Add CS to SL;  End  End;
  • 33. Eight queens puzzle Algorithm http://www.codeproject.com/Articles/806248/Eight- Queens-Puzzle-Tree-Algorithm
  • 34. Complexity  The running time of your algorithm is at most N(N−1)(N−2)⋯(N−K+1), i.e., N!/(N−K)!. This is O(NK), i.e., exponential in K  http://www.chegg.com/homework-help/questions-and-answers/poor-man-s- n-queens-problemn-queens-arranged-n-x-n-chessboard-way-queen-checks- queen-queen-q1009394