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

Daa:Dynamic Programing
Daa:Dynamic ProgramingDaa:Dynamic Programing
Daa:Dynamic Programing
rupali_2bonde
 
daa-unit-3-greedy method
daa-unit-3-greedy methoddaa-unit-3-greedy method
daa-unit-3-greedy method
hodcsencet
 
4 greedy methodnew
4 greedy methodnew4 greedy methodnew
4 greedy methodnew
abhinav108
 
Lecture 8 dynamic programming
Lecture 8 dynamic programmingLecture 8 dynamic programming
Lecture 8 dynamic programming
Oye Tu
 
lecture 26
lecture 26lecture 26
lecture 26
sajinsc
 
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
zukun
 

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

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
 
Itec 299 final project suzie parada
Itec 299 final project suzie paradaItec 299 final project suzie parada
Itec 299 final project suzie parada
Suzie_Parada
 
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 2009
maarten de regt
 
globalleadershipreadiness_wp_ddi final v4
globalleadershipreadiness_wp_ddi final v4globalleadershipreadiness_wp_ddi final v4
globalleadershipreadiness_wp_ddi final v4
Jill George, Ph.D.
 
Learning styles
Learning stylesLearning styles
Learning styles
HisaAlali
 

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

LECTURE 6 DESIGN, DEBUGGING, INTERFACES.pdf
LECTURE 6 DESIGN, DEBUGGING, INTERFACES.pdfLECTURE 6 DESIGN, DEBUGGING, INTERFACES.pdf
LECTURE 6 DESIGN, DEBUGGING, INTERFACES.pdf
SHASHIKANT346021
 
Threaded Programming
Threaded ProgrammingThreaded Programming
Threaded Programming
Sri Prasanna
 
LECTURE 6 DESIGN, DEBasd, INTERFACES.pdf
LECTURE 6 DESIGN, DEBasd, INTERFACES.pdfLECTURE 6 DESIGN, DEBasd, INTERFACES.pdf
LECTURE 6 DESIGN, DEBasd, INTERFACES.pdf
ShashikantSathe3
 
computer notes - Data Structures - 1
computer notes - Data Structures - 1computer notes - Data Structures - 1
computer notes - Data Structures - 1
ecomputernotes
 
Bca2020 data structure and algorithm
Bca2020   data structure and algorithmBca2020   data structure and algorithm
Bca2020 data structure and algorithm
smumbahelp
 

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

The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
heathfieldcps1
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdf
SanaAli374401
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
PECB
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 

Recently uploaded (20)

The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdf
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 

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