SlideShare una empresa de Scribd logo
1 de 32
Descargar para leer sin conexión
Review
Overview
Fundamentals of Analysis of Algorithm Efficiency
Algorithmic Techniques
  Divide-and-Conquer, Decrease-and-Conquer
  Dynamic Programming
  Greedy Technique
Data Structures
  Heaps
  Graphs– adjacency matrices & adjacency linked lists
  Trees




                                                        2
Fundamentals of Analysis of
      Algorithm Efficiency
Basic operations
Worst-, Best-, and Average-case time
efficiencies
Orders of growth
Efficiency of non-recursive algorithms
Efficiency of recursive algorithms


                                         3
Worst-Case, Best-Case, and
       Average-Case Efficiency
Worst case efficiency
  Efficiency (# of times the basic operation will be executed) for the
  worst case input of size n, for which
  The algorithm runs the longest among all possible inputs of size n.
Best case
  Efficiency (# of times the basic operation will be executed) for the
  best case input of size n, for which
  The algorithm runs the fastest among all possible inputs of size n.
Average case:
  Efficiency (#of times the basic operation will be executed) for a
  typical/random input
  NOT the average of worst and best case
  How to find the average case efficiency?                            4
Orders of Growth
Three notations used to compare orders of
growth of algorithms
  O(g(n)): class of functions f(n) that grow no
  faster than g(n)
  Θ (g(n)): class of functions f(n) that grow at
  same rate as g(n)
  Ω(g(n)): class of functions f(n) that grow at least
  as fast as g(n)

                                                  5
Theorem
 If t1(n) ∈ O(g1(n)) and t2(n) ∈ O(g2(n)), then
 t1(n) + t2(n) ∈ O(max{g1(n), g2(n)}).
 The analogous assertions are true for the Ω-
 notation and Θ-notation.
 The algorithm’s overall efficiency will be
 determined by the part with a larger order of
 growth.
   5n2 + 3n + 4


                                             6
Using Limits for Comparing Orders of
                   Growth

                      0       order of growth of T(n)    <       order of growth of g(n)


                      c>0       order of growth of T(n)      =    order of growth of g(n)
limn→∞ T(n)/g(n) =
                          ∞    order of growth of T(n)       >   order of growth of g(n)


   Examples:
   • 10n        vs.               2n2

   • n(n+1)/2   vs.               n2

   • logb n     vs.              logc n
                                                                                      7
Summary of How to Establish Orders of
      Growth of an Algorithm

Method 1: Using limits.
Method 2: Using the theorem.
Method 3: Using the definitions of O-,
Ω-, and Θ-notation.




                                         8
Basic Efficiency classes
fast
             1         constant    High time efficiency

           log n     logarithmic
             n           linear
           n log n      n log n
              n2      quadratic
              n3         cubic
             2n      exponential
slow
             n!        factorial   low time efficiency
                                                  9
Time Efficiency Analysis of Nonrecursive
Algorithms

Steps in mathematical analysis of nonrecursive algorithms:
  Decide on parameter n indicating input size

  Identify algorithm’s basic operation

  Determine worst, average, and best case for input of size n

  Set up summation for C(n) reflecting the number of times the
  algorithm’s basic operation is executed.

  Simplify summation using standard formulas (see Appendix A)



                                                                 10
Time Efficiency Analysis of Recursive
                 Algorithms
Decide on parameter n indicating input size

Identify algorithm’s basic operation

Determine worst, average, and best case for input of size n

Set up a recurrence relation and initial condition(s) for C(n)-the
number of times the basic operation will be executed for an
input of size n (alternatively count recursive calls).

Solve the recurrence or estimate the order of magnitude of the
solution (see Appendix B)

                                                                11
Master’s Theorem

T(n) = aT(n/b) + f (n)    where f (n) ∈ Θ(nk)
1. a < bk         T(n) ∈ Θ(nk)
2. a = bk         T(n) ∈ Θ(nk lg n )
3. a > bk         T(n) ∈ Θ(nlog b a)
Note: the same results hold with O instead of Θ.




                                               12
Divide-and-Conquer
Three Steps of The Divide and
Conquer Approach
The most well known algorithm design strategy:
1. Divide the problem into two or more smaller
   subproblems.

2.   Conquer the subproblems by solving them
     recursively(or recursively).

3.   Combine the solutions to the subproblems
     into the solutions for the original problem.

                                                    14
Divide-and-Conquer Technique
                  a problem of size n



 subproblem 1                            subproblem 2
   of size n/2                             of size n/2



  a solution to                           a solution to
 subproblem 1                            subproblem 2




                      a solution to
                  the original problem                    15
Divide and Conquer Examples
Sorting algorithms
   Mergesort
       In-place?
       Worst-case efficiency?
   Quicksort
       In-place?
       Worst-case , best-case and average-case efficiency?
Binary Tree algorithms
    Definitions
       What is a binary tree?
       A node’s/tree’s height?
       A node’s level?
   Pre-order, post-order, and in-order traversal
   Find the height
   Find the total number of leaves.
   …


                                                             16
Decrease-and-Conquer
Decrease and Conquer
 Exploring the relationship between a solution
 to a given instance of a problem and a
 solution to a smaller instance of the same
 problem.
 Use top down(recursive) or bottom up
 (iterative) to solve the problem.
 Example, an
   A top down (recursive) solution
   A bottom up (iterative) solution


                                            18
Examples of Decrease and Conquer
Decrease by one: the size of the problem is reduced by the same
constant on each iteration/recursion of the algorithm.
   Insertion sort
       In-place?
       Worst-case , best-case and average-case efficiency?
   Graph search algorithms:
      DFS
      BFS

Decrease by a constant factor: the size of the problem is reduced by
the same constant factor on each iteration/recursion of the algorithm.




                                                                  19
A Typical Decrease by One Technique

                     a problem of size n



  subproblem
    of size n-1



 a solution to the
  subproblem




                         a solution to
                     the original problem   20
A Typical Decrease by a Constant Factor
            (half) Technique

                    a problem of size n



 subproblem
   of size n/2



a solution to the
  subproblem




                        a solution to
                    the original problem   21
What’s the Difference?
Consider the problem of exponentiation:
  Compute an
  Divide and conquer:
   an= an/2 * an/2
  Decrease by one:
   an= an-1* a (top down)   an= a*a*a*a*...*a (bottom up)
  Decrease by a constant factor:
   an= (an/2)2


                                                     22
Depth-First Search
The idea
   traverse “deeper” whenever possible.
   When reaching a dead end, the algorithm backs up one edge to the
   parent and tries to continue visiting unvisited vertices from there.
   Break the tie by the alphabetic order of the vertices
   It’s convenient to use a stack to track the operation of depth-first
   search.
DFS forest/tree and the two orderings of DFS
DFS can be implemented with graphs represented as:
   Adjacency matrices: Θ(V2)
   Adjacency linked lists: Θ(V+E)
Applications:
   Topological sorting
   checking connectivity, finding connected components

                                                                   23
Breadth-First Search
The idea
   Traverse “wider” whenever possible.
   Discover all vertices at distance k from s (on level k) before
   discovering any vertices at distance k +1 (at level k+1)
   Similar to level-by-level tree traversals
   It’s convenient to use a queue to track the operation of
   depth-first search.
BFS forest/tree and the one ordering of BFS
BFS has same efficiency as DFS and can be
implemented with graphs represented as:
   Adjacency matrices: Θ(V2)
   Adjacency linked lists: Θ(V+E)
Applications:
   checking connectivity, finding connected components          24
Heapsort
Heaps
 Definition
 Representation
 Properties
 Heap algorithms
   Heap construction
     Top-down
     Bottom-up
   Root deletion
   Heapsort
     In-place?
     Time efficiency?
                        26
Examples of Dynamic Programming
             Algorithms

Main idea:
    solve several smaller (overlapping) subproblems
    record solutions in a table so that each subproblem is only
  solved once
    final state of the table will be (or contain) solution

VS. Divide and Conquer

Computing binomial coefficients

Warshall’s algorithm for transitive closure

Floyd’s algorithms for all-pairs shortest paths
                                                                  27
Greedy Algorithms
Greedy algorithms
Constructs a solution through a sequence of steps, each expanding
  a partially constructed solution obtained so far, until a complete
  solution to the problem is reached. The choice made at each step
  must be:
   Feasible
      Satisfy the problem’s constraints
   locally optimal
      Be the best local choice among all feasible choices
   Irrevocable
      Once made, the choice can’t be changed on subsequent
      steps.
Greedy algorithms do not always yield optimal solutions.

                                                                29
Examples of the Greedy Strategy
Minimum Spanning Tree (MST)
  Definition of spanning tree and MST
  Prim’s algorithm
  Kruskal’s algorithm
Single-source shortest paths
  Dijkstra’s algorithm




                                        30
P, NP, and NP-Complete Problems
 Tractable and intractable problems
 The class P
 The class NP
 The relationship between P and NP
 NP-complete problems



                                      31
Backtracking and Branch-and-Bound
 They guarantees solving the problem exactly
 but doesn’t guarantee to find a solution in
 polynomial time.

 Similarity and difference between
 backtracking and branch-and-bound




                                          32

Más contenido relacionado

La actualidad más candente

Sure interview algorithm-1103
Sure interview algorithm-1103Sure interview algorithm-1103
Sure interview algorithm-1103
Sure Interview
 
Algorithm chapter 11
Algorithm chapter 11Algorithm chapter 11
Algorithm chapter 11
chidabdu
 

La actualidad más candente (20)

Np cooks theorem
Np cooks theoremNp cooks theorem
Np cooks theorem
 
Sure interview algorithm-1103
Sure interview algorithm-1103Sure interview algorithm-1103
Sure interview algorithm-1103
 
Graph Traversal Algorithms - Breadth First Search
Graph Traversal Algorithms - Breadth First SearchGraph Traversal Algorithms - Breadth First Search
Graph Traversal Algorithms - Breadth First Search
 
R exam (B) given in Paris-Dauphine, Licence Mido, Jan. 11, 2013
R exam (B) given in Paris-Dauphine, Licence Mido, Jan. 11, 2013R exam (B) given in Paris-Dauphine, Licence Mido, Jan. 11, 2013
R exam (B) given in Paris-Dauphine, Licence Mido, Jan. 11, 2013
 
algorithm Unit 2
algorithm Unit 2 algorithm Unit 2
algorithm Unit 2
 
Bq25399403
Bq25399403Bq25399403
Bq25399403
 
Algorithm chapter 11
Algorithm chapter 11Algorithm chapter 11
Algorithm chapter 11
 
Joco pavone
Joco pavoneJoco pavone
Joco pavone
 
Daa chapter6
Daa chapter6Daa chapter6
Daa chapter6
 
Ch10 Recursion
Ch10 RecursionCh10 Recursion
Ch10 Recursion
 
Unit 7 dynamic programming
Unit 7   dynamic programmingUnit 7   dynamic programming
Unit 7 dynamic programming
 
Data Structure and Algorithm - Divide and Conquer
Data Structure and Algorithm - Divide and ConquerData Structure and Algorithm - Divide and Conquer
Data Structure and Algorithm - Divide and Conquer
 
Branch and bound technique
Branch and bound techniqueBranch and bound technique
Branch and bound technique
 
design and analysis of algorithm
design and analysis of algorithmdesign and analysis of algorithm
design and analysis of algorithm
 
Recursion
RecursionRecursion
Recursion
 
Dynamic Programming - Part 1
Dynamic Programming - Part 1Dynamic Programming - Part 1
Dynamic Programming - Part 1
 
NP Complete Problems in Graph Theory
NP Complete Problems in Graph TheoryNP Complete Problems in Graph Theory
NP Complete Problems in Graph Theory
 
Chapter 2 ds
Chapter 2 dsChapter 2 ds
Chapter 2 ds
 
NP Complete Problems -- Internship
NP Complete Problems -- InternshipNP Complete Problems -- Internship
NP Complete Problems -- Internship
 
Approx
ApproxApprox
Approx
 

Similar a Algorithm review

Sienna 13 limitations
Sienna 13 limitationsSienna 13 limitations
Sienna 13 limitations
chidabdu
 
Algorithm chapter 2
Algorithm chapter 2Algorithm chapter 2
Algorithm chapter 2
chidabdu
 
lecture 1
lecture 1lecture 1
lecture 1
sajinsc
 
Jörg Stelzer
Jörg StelzerJörg Stelzer
Jörg Stelzer
butest
 
Algorithm chapter 5
Algorithm chapter 5Algorithm chapter 5
Algorithm chapter 5
chidabdu
 
4optmizationtechniques-150308051251-conversion-gate01.pdf
4optmizationtechniques-150308051251-conversion-gate01.pdf4optmizationtechniques-150308051251-conversion-gate01.pdf
4optmizationtechniques-150308051251-conversion-gate01.pdf
BechanYadav4
 

Similar a Algorithm review (20)

Sienna 13 limitations
Sienna 13 limitationsSienna 13 limitations
Sienna 13 limitations
 
Algorithm chapter 2
Algorithm chapter 2Algorithm chapter 2
Algorithm chapter 2
 
Unit7
Unit7Unit7
Unit7
 
Unit 5
Unit 5Unit 5
Unit 5
 
Unit 5
Unit 5Unit 5
Unit 5
 
Analysis and Design of Algorithms notes
Analysis and Design of Algorithms  notesAnalysis and Design of Algorithms  notes
Analysis and Design of Algorithms notes
 
lecture 1
lecture 1lecture 1
lecture 1
 
dynamic programming Rod cutting class
dynamic programming Rod cutting classdynamic programming Rod cutting class
dynamic programming Rod cutting class
 
Cs6402 design and analysis of algorithms may june 2016 answer key
Cs6402 design and analysis of algorithms may june 2016 answer keyCs6402 design and analysis of algorithms may june 2016 answer key
Cs6402 design and analysis of algorithms may june 2016 answer key
 
Anlysis and design of algorithms part 1
Anlysis and design of algorithms part 1Anlysis and design of algorithms part 1
Anlysis and design of algorithms part 1
 
Slide2
Slide2Slide2
Slide2
 
CS345-Algorithms-II-Lecture-1-CS345-2016.pdf
CS345-Algorithms-II-Lecture-1-CS345-2016.pdfCS345-Algorithms-II-Lecture-1-CS345-2016.pdf
CS345-Algorithms-II-Lecture-1-CS345-2016.pdf
 
Jörg Stelzer
Jörg StelzerJörg Stelzer
Jörg Stelzer
 
algo_vc_lecture8.ppt
algo_vc_lecture8.pptalgo_vc_lecture8.ppt
algo_vc_lecture8.ppt
 
Lecture 5 6_7 - divide and conquer and method of solving recurrences
Lecture 5 6_7 - divide and conquer and method of solving recurrencesLecture 5 6_7 - divide and conquer and method of solving recurrences
Lecture 5 6_7 - divide and conquer and method of solving recurrences
 
Algorithm chapter 5
Algorithm chapter 5Algorithm chapter 5
Algorithm chapter 5
 
Unit 1
Unit 1Unit 1
Unit 1
 
Parallel Computing 2007: Bring your own parallel application
Parallel Computing 2007: Bring your own parallel applicationParallel Computing 2007: Bring your own parallel application
Parallel Computing 2007: Bring your own parallel application
 
analysis.ppt
analysis.pptanalysis.ppt
analysis.ppt
 
4optmizationtechniques-150308051251-conversion-gate01.pdf
4optmizationtechniques-150308051251-conversion-gate01.pdf4optmizationtechniques-150308051251-conversion-gate01.pdf
4optmizationtechniques-150308051251-conversion-gate01.pdf
 

Más de chidabdu

Sienna 12 huffman
Sienna 12 huffmanSienna 12 huffman
Sienna 12 huffman
chidabdu
 
Sienna 11 graphs
Sienna 11 graphsSienna 11 graphs
Sienna 11 graphs
chidabdu
 
Sienna 10 dynamic
Sienna 10 dynamicSienna 10 dynamic
Sienna 10 dynamic
chidabdu
 
Sienna 9 hashing
Sienna 9 hashingSienna 9 hashing
Sienna 9 hashing
chidabdu
 
Sienna 8 countingsorts
Sienna 8 countingsortsSienna 8 countingsorts
Sienna 8 countingsorts
chidabdu
 
Sienna 7 heaps
Sienna 7 heapsSienna 7 heaps
Sienna 7 heaps
chidabdu
 
Sienna 6 bst
Sienna 6 bstSienna 6 bst
Sienna 6 bst
chidabdu
 
Sienna 5 decreaseandconquer
Sienna 5 decreaseandconquerSienna 5 decreaseandconquer
Sienna 5 decreaseandconquer
chidabdu
 
Sienna 4 divideandconquer
Sienna 4 divideandconquerSienna 4 divideandconquer
Sienna 4 divideandconquer
chidabdu
 
Sienna 3 bruteforce
Sienna 3 bruteforceSienna 3 bruteforce
Sienna 3 bruteforce
chidabdu
 
Sienna 2 analysis
Sienna 2 analysisSienna 2 analysis
Sienna 2 analysis
chidabdu
 
Sienna 1 intro
Sienna 1 introSienna 1 intro
Sienna 1 intro
chidabdu
 
Unit 3 basic processing unit
Unit 3   basic processing unitUnit 3   basic processing unit
Unit 3 basic processing unit
chidabdu
 
Unit 5 I/O organization
Unit 5   I/O organizationUnit 5   I/O organization
Unit 5 I/O organization
chidabdu
 
Algorithm chapter 10
Algorithm chapter 10Algorithm chapter 10
Algorithm chapter 10
chidabdu
 
Algorithm chapter 9
Algorithm chapter 9Algorithm chapter 9
Algorithm chapter 9
chidabdu
 
Algorithm chapter 8
Algorithm chapter 8Algorithm chapter 8
Algorithm chapter 8
chidabdu
 
Algorithm chapter 7
Algorithm chapter 7Algorithm chapter 7
Algorithm chapter 7
chidabdu
 
Algorithm chapter 6
Algorithm chapter 6Algorithm chapter 6
Algorithm chapter 6
chidabdu
 
Unit 2 arithmetics
Unit 2   arithmeticsUnit 2   arithmetics
Unit 2 arithmetics
chidabdu
 

Más de chidabdu (20)

Sienna 12 huffman
Sienna 12 huffmanSienna 12 huffman
Sienna 12 huffman
 
Sienna 11 graphs
Sienna 11 graphsSienna 11 graphs
Sienna 11 graphs
 
Sienna 10 dynamic
Sienna 10 dynamicSienna 10 dynamic
Sienna 10 dynamic
 
Sienna 9 hashing
Sienna 9 hashingSienna 9 hashing
Sienna 9 hashing
 
Sienna 8 countingsorts
Sienna 8 countingsortsSienna 8 countingsorts
Sienna 8 countingsorts
 
Sienna 7 heaps
Sienna 7 heapsSienna 7 heaps
Sienna 7 heaps
 
Sienna 6 bst
Sienna 6 bstSienna 6 bst
Sienna 6 bst
 
Sienna 5 decreaseandconquer
Sienna 5 decreaseandconquerSienna 5 decreaseandconquer
Sienna 5 decreaseandconquer
 
Sienna 4 divideandconquer
Sienna 4 divideandconquerSienna 4 divideandconquer
Sienna 4 divideandconquer
 
Sienna 3 bruteforce
Sienna 3 bruteforceSienna 3 bruteforce
Sienna 3 bruteforce
 
Sienna 2 analysis
Sienna 2 analysisSienna 2 analysis
Sienna 2 analysis
 
Sienna 1 intro
Sienna 1 introSienna 1 intro
Sienna 1 intro
 
Unit 3 basic processing unit
Unit 3   basic processing unitUnit 3   basic processing unit
Unit 3 basic processing unit
 
Unit 5 I/O organization
Unit 5   I/O organizationUnit 5   I/O organization
Unit 5 I/O organization
 
Algorithm chapter 10
Algorithm chapter 10Algorithm chapter 10
Algorithm chapter 10
 
Algorithm chapter 9
Algorithm chapter 9Algorithm chapter 9
Algorithm chapter 9
 
Algorithm chapter 8
Algorithm chapter 8Algorithm chapter 8
Algorithm chapter 8
 
Algorithm chapter 7
Algorithm chapter 7Algorithm chapter 7
Algorithm chapter 7
 
Algorithm chapter 6
Algorithm chapter 6Algorithm chapter 6
Algorithm chapter 6
 
Unit 2 arithmetics
Unit 2   arithmeticsUnit 2   arithmetics
Unit 2 arithmetics
 

Último

EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
Earley Information Science
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 

Último (20)

Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 

Algorithm review

  • 2. Overview Fundamentals of Analysis of Algorithm Efficiency Algorithmic Techniques Divide-and-Conquer, Decrease-and-Conquer Dynamic Programming Greedy Technique Data Structures Heaps Graphs– adjacency matrices & adjacency linked lists Trees 2
  • 3. Fundamentals of Analysis of Algorithm Efficiency Basic operations Worst-, Best-, and Average-case time efficiencies Orders of growth Efficiency of non-recursive algorithms Efficiency of recursive algorithms 3
  • 4. Worst-Case, Best-Case, and Average-Case Efficiency Worst case efficiency Efficiency (# of times the basic operation will be executed) for the worst case input of size n, for which The algorithm runs the longest among all possible inputs of size n. Best case Efficiency (# of times the basic operation will be executed) for the best case input of size n, for which The algorithm runs the fastest among all possible inputs of size n. Average case: Efficiency (#of times the basic operation will be executed) for a typical/random input NOT the average of worst and best case How to find the average case efficiency? 4
  • 5. Orders of Growth Three notations used to compare orders of growth of algorithms O(g(n)): class of functions f(n) that grow no faster than g(n) Θ (g(n)): class of functions f(n) that grow at same rate as g(n) Ω(g(n)): class of functions f(n) that grow at least as fast as g(n) 5
  • 6. Theorem If t1(n) ∈ O(g1(n)) and t2(n) ∈ O(g2(n)), then t1(n) + t2(n) ∈ O(max{g1(n), g2(n)}). The analogous assertions are true for the Ω- notation and Θ-notation. The algorithm’s overall efficiency will be determined by the part with a larger order of growth. 5n2 + 3n + 4 6
  • 7. Using Limits for Comparing Orders of Growth 0 order of growth of T(n) < order of growth of g(n) c>0 order of growth of T(n) = order of growth of g(n) limn→∞ T(n)/g(n) = ∞ order of growth of T(n) > order of growth of g(n) Examples: • 10n vs. 2n2 • n(n+1)/2 vs. n2 • logb n vs. logc n 7
  • 8. Summary of How to Establish Orders of Growth of an Algorithm Method 1: Using limits. Method 2: Using the theorem. Method 3: Using the definitions of O-, Ω-, and Θ-notation. 8
  • 9. Basic Efficiency classes fast 1 constant High time efficiency log n logarithmic n linear n log n n log n n2 quadratic n3 cubic 2n exponential slow n! factorial low time efficiency 9
  • 10. Time Efficiency Analysis of Nonrecursive Algorithms Steps in mathematical analysis of nonrecursive algorithms: Decide on parameter n indicating input size Identify algorithm’s basic operation Determine worst, average, and best case for input of size n Set up summation for C(n) reflecting the number of times the algorithm’s basic operation is executed. Simplify summation using standard formulas (see Appendix A) 10
  • 11. Time Efficiency Analysis of Recursive Algorithms Decide on parameter n indicating input size Identify algorithm’s basic operation Determine worst, average, and best case for input of size n Set up a recurrence relation and initial condition(s) for C(n)-the number of times the basic operation will be executed for an input of size n (alternatively count recursive calls). Solve the recurrence or estimate the order of magnitude of the solution (see Appendix B) 11
  • 12. Master’s Theorem T(n) = aT(n/b) + f (n) where f (n) ∈ Θ(nk) 1. a < bk T(n) ∈ Θ(nk) 2. a = bk T(n) ∈ Θ(nk lg n ) 3. a > bk T(n) ∈ Θ(nlog b a) Note: the same results hold with O instead of Θ. 12
  • 14. Three Steps of The Divide and Conquer Approach The most well known algorithm design strategy: 1. Divide the problem into two or more smaller subproblems. 2. Conquer the subproblems by solving them recursively(or recursively). 3. Combine the solutions to the subproblems into the solutions for the original problem. 14
  • 15. Divide-and-Conquer Technique a problem of size n subproblem 1 subproblem 2 of size n/2 of size n/2 a solution to a solution to subproblem 1 subproblem 2 a solution to the original problem 15
  • 16. Divide and Conquer Examples Sorting algorithms Mergesort In-place? Worst-case efficiency? Quicksort In-place? Worst-case , best-case and average-case efficiency? Binary Tree algorithms Definitions What is a binary tree? A node’s/tree’s height? A node’s level? Pre-order, post-order, and in-order traversal Find the height Find the total number of leaves. … 16
  • 18. Decrease and Conquer Exploring the relationship between a solution to a given instance of a problem and a solution to a smaller instance of the same problem. Use top down(recursive) or bottom up (iterative) to solve the problem. Example, an A top down (recursive) solution A bottom up (iterative) solution 18
  • 19. Examples of Decrease and Conquer Decrease by one: the size of the problem is reduced by the same constant on each iteration/recursion of the algorithm. Insertion sort In-place? Worst-case , best-case and average-case efficiency? Graph search algorithms: DFS BFS Decrease by a constant factor: the size of the problem is reduced by the same constant factor on each iteration/recursion of the algorithm. 19
  • 20. A Typical Decrease by One Technique a problem of size n subproblem of size n-1 a solution to the subproblem a solution to the original problem 20
  • 21. A Typical Decrease by a Constant Factor (half) Technique a problem of size n subproblem of size n/2 a solution to the subproblem a solution to the original problem 21
  • 22. What’s the Difference? Consider the problem of exponentiation: Compute an Divide and conquer: an= an/2 * an/2 Decrease by one: an= an-1* a (top down) an= a*a*a*a*...*a (bottom up) Decrease by a constant factor: an= (an/2)2 22
  • 23. Depth-First Search The idea traverse “deeper” whenever possible. When reaching a dead end, the algorithm backs up one edge to the parent and tries to continue visiting unvisited vertices from there. Break the tie by the alphabetic order of the vertices It’s convenient to use a stack to track the operation of depth-first search. DFS forest/tree and the two orderings of DFS DFS can be implemented with graphs represented as: Adjacency matrices: Θ(V2) Adjacency linked lists: Θ(V+E) Applications: Topological sorting checking connectivity, finding connected components 23
  • 24. Breadth-First Search The idea Traverse “wider” whenever possible. Discover all vertices at distance k from s (on level k) before discovering any vertices at distance k +1 (at level k+1) Similar to level-by-level tree traversals It’s convenient to use a queue to track the operation of depth-first search. BFS forest/tree and the one ordering of BFS BFS has same efficiency as DFS and can be implemented with graphs represented as: Adjacency matrices: Θ(V2) Adjacency linked lists: Θ(V+E) Applications: checking connectivity, finding connected components 24
  • 26. Heaps Definition Representation Properties Heap algorithms Heap construction Top-down Bottom-up Root deletion Heapsort In-place? Time efficiency? 26
  • 27. Examples of Dynamic Programming Algorithms Main idea: solve several smaller (overlapping) subproblems record solutions in a table so that each subproblem is only solved once final state of the table will be (or contain) solution VS. Divide and Conquer Computing binomial coefficients Warshall’s algorithm for transitive closure Floyd’s algorithms for all-pairs shortest paths 27
  • 29. Greedy algorithms Constructs a solution through a sequence of steps, each expanding a partially constructed solution obtained so far, until a complete solution to the problem is reached. The choice made at each step must be: Feasible Satisfy the problem’s constraints locally optimal Be the best local choice among all feasible choices Irrevocable Once made, the choice can’t be changed on subsequent steps. Greedy algorithms do not always yield optimal solutions. 29
  • 30. Examples of the Greedy Strategy Minimum Spanning Tree (MST) Definition of spanning tree and MST Prim’s algorithm Kruskal’s algorithm Single-source shortest paths Dijkstra’s algorithm 30
  • 31. P, NP, and NP-Complete Problems Tractable and intractable problems The class P The class NP The relationship between P and NP NP-complete problems 31
  • 32. Backtracking and Branch-and-Bound They guarantees solving the problem exactly but doesn’t guarantee to find a solution in polynomial time. Similarity and difference between backtracking and branch-and-bound 32