SlideShare una empresa de Scribd logo
1 de 19
Descargar para leer sin conexión
The Basic Concepts of Algorithms
                            R.C.T. Lee and Chin Lung Lu

                       Algorithms for Molecular Biology




By R.C.T. Lee and C.L. Lu                        The Basic Concepts of Algorithms p.1




     What is algorithm?
           A method that can be used by a computer for the
           solution of a problem.
           A sequence of computational steps that transform
           the input into the output.
           Examples of algorithms in the nature: DNA and
           cook book.
           The word ”algorithm” comes from the name of a
           Persian author, Abu Ja’far Mohammed ibn Musa al
           Khowarizmi (c. 825 A.D.), who wrote a textbook
           on mathematics.

By R.C.T. Lee and C.L. Lu                        The Basic Concepts of Algorithms p.2
Why should we study algorithms?
           A good algorithm implemented on a slow computer
           may perform much better than a bad algorithm
           implemented on a fast computer.
           f (n)  n    10        102     103
             log2 n     3.3       6.6      10
               n        10        102     103
            nlog2 n 0.33 × 102 0.7 × 103  104
               n2       102       104     106
               2n      1024    1.3 × 103 > 10100
               n!        36     > 10100 > 10100
By R.C.T. Lee and C.L. Lu                           The Basic Concepts of Algorithms p.3




     Minimal spanning tree problem
           Given a set of points, find a spanning tree with the
           shortest total length.
               a                    e           a                    e
                             b                           b
                                        d                                   d

           f                        c       f                        c
                            Input                        ❶
               a                    e           a                    e
                             b                           b
                                        d                                   d

           f                        c       f                        c
                             ❷                           ❸
By R.C.T. Lee and C.L. Lu                           The Basic Concepts of Algorithms p.4
Minimal spanning tree problem
           MST problem: given a set of points, find a
           spanning tree with the shortest total length
           Brute force method: enumerate all possible
           spanning trees and select the best one among them
           Given n points, there are nn−2 possible spanning
           trees for them.




By R.C.T. Lee and C.L. Lu                   The Basic Concepts of Algorithms p.5




     How to design a good algorithm?
           Efficient or not?
           (Efficient means short time and small space.)
           Strategies of algorithms:
           1. Greedy
           2. Divide & conquer
           3. Prune & search
           4. Dynamic programming
           5. Branch and bound
           6. Approximation
           7. Heuristics

By R.C.T. Lee and C.L. Lu                   The Basic Concepts of Algorithms p.6
Prim’s algorithm for MST
     Input: A weighted and connected graph G                                    = (V, E).
     Output: A minimum spanning tree of G.
     1: Let x be any vertex in V ;
        Let X = {x} and Y = V  {x};
     2: Select an edge (u, v) from E such that u ∈ X,
        v ∈ Y and (u, v) has the smallest weight among
        edges between X and Y ;
     3: Connect u to v;
        Let X = X ∪ {v} and Y = Y  {v};
     4: If Y is empty, terminate and the resulting tree
        is a minimal spanning tree;
        Otherwise, go to step 2;
By R.C.T. Lee and C.L. Lu                                            The Basic Concepts of Algorithms p.7




     Prim’s algorithm for MST
      a
                       15
                                           b               ❶                                     b
             18         c     3                            X = {b},
       10                              1                   Y = {a, c, e, d},                     1
              7
                        9
                              5                            (b, d) the shortest.                  d
       e                               d

       ❷                               3
                                                   b       ❸                                3
                                                                                                 b
       X = {b, d},                                         X = {b, d, c},
                                                   1                                             1
       Y = {a, c, e},                  c                   Y = {a, e},              7       c
       (b, c) the shortest.                        d       (c, e) the shortest.         e        d

                                   a                   b        a                                b
       ❹                               c                                       15
                                                                                c
                                                                     18                     3
       X = {b, d, c, e}           10           3       1        10                              1
       Y = {a},                        7                              7                     5
                                   e                   d
       (e, a) the shortest.                                     e
                                                                                9
                                                                                                d


By R.C.T. Lee and C.L. Lu                                            The Basic Concepts of Algorithms p.8
Kruskal’s algorithm for MST
     Input: A weighted and connected graph G = (V, E).
     Output: A minimum spanning tree of G.
     1: T = ∅;
     2: while T contains less than n − 1 edges do
         Choose an edge (v, w) from E of smallest weight;
         Delete (v, w) from E;
         if adding (v, w) does not create cycle in T then
             Add (v, w) to T ;
                else
                  Discard (v, w);
                end if
            end while

By R.C.T. Lee and C.L. Lu                                                             The Basic Concepts of Algorithms p.9




     Kruskal’s algorithm for MST
       a
                                      15
                                                        b
                                                                 ❶b
                    18                 c         3
       10                                              1                          (b, d) is selected
                                                                     1            and added.
                    7                            5
                                      9
       e                                               d             d

            ❷               b                                    ❸                        b
                3                                                             3
                    1
                                    (b, c) is selected                            1
                                                                                              (c, d) is selected
            c                       and added.                            c                   and discarded.
                            d                                                             d

            ❹                         b                          ❺a                               b
                            3                                                             3
                                           (c, e) is selected.       10                       1
                                                                                                  (a, e) is selected
                        c       1          and added.                         7       c           and added.
                    7
       e                              d                              e                            d


By R.C.T. Lee and C.L. Lu                                                         The Basic Concepts of Algorithms p.10
How to measure time complexity
     of algorithm A?
      1. Write a program for A and see how fast it runs.
           Not suitable for many factors unrelated to A,
           such as the capability of programmer, the used
           language and compiler, operating system, CPU’s
           speed etc.
      2. Choose the particular steps in A and determine the
         number of the needed steps
           The particular steps are time-consuming
           operations, like comparison of data, movement
           of data, +, −, ∗, / operations etc.

By R.C.T. Lee and C.L. Lu                  The Basic Concepts of Algorithms p.11




     Time complexity of algorithm A
           Time complexity of an algorithm:
              Equal to number of operations in algorithm A
              Usually represented by a function of the size of
              the input
              Size of the input:
             1. sorting: number of items
             2. graph problems: number of vertices and edges
             3. multiplying two integers: number of bits
              Example: For MST problem,
              Prim’s algorithm = |V |2 time
              Kruskal’s algorithm = |E| log |E| + |V | time

By R.C.T. Lee and C.L. Lu                  The Basic Concepts of Algorithms p.12
O notation
     O(g(n)) = {f (n)| there exist positive constants c
     and n0 such that f (n) ≤ cg(n) for all n ≥ n0 }.
                                     cg(n)


                                              f (n)


                                 f (n) = O(g(n))
                                                       n
                            n0

By R.C.T. Lee and C.L. Lu                 The Basic Concepts of Algorithms p.13




     O notation
           O (g(n)) actually denotes a set of functions.
           f (n) = O (g(n)) indicates that f (n) is a
           member of O (g(n)).
           Example: Let f (n) = 1 n2 − 3n. Then
                                2
           1. f (n) = O (n2 )       (✔)
           2. f (n) = O (n3 )       (✔)
           3. f (n) = O (n)       (✘)
           ”The running time of an algorithm A is O(n2 )”
           means that the worst-case running time of A is
           O (n2 ).
By R.C.T. Lee and C.L. Lu                 The Basic Concepts of Algorithms p.14
Ω notation
     Ω(g(n)) = {f (n)| there exist positive constants c
     and n0 such that f (n) ≥ cg(n) for all n ≥ n0 }.
                                                    f (n)


                                                       cg(n)


                                         f (n) = Ω(g(n))
                                                                n
                                 n0

By R.C.T. Lee and C.L. Lu                          The Basic Concepts of Algorithms p.15




     Ω notation
           Ω(g(n)) actually denotes a set of functions.
           f (n) = Ω(g(n)) indicates that f (n) is a
           member of Ω(g(n)).
           Example: Let f (n) = 1 n2 + 3n. Then
                                2
           1.   f (n)       =   Ω(n2 )       (✔)
           2.   f (n)       =   Ω(n3 )       (✘)
           3.   f (n)       =   Ω(n)        (✔)
           4.   f (n)       =   Ω(1)        (✔)



By R.C.T. Lee and C.L. Lu                          The Basic Concepts of Algorithms p.16
Θ notation
     Θ(g(n)) = {f (n)| there exist positive constants
     c1 , c2 and n0 such that c1 g(n) ≤ f (n) ≤ c2 g(n)
     for all n ≥ n0 }.
                                          c2 g(n)

                                              f (n)

                                             c1 g(n)

                                 f (n) = Θ(g(n))
                                                        n
                            n0
By R.C.T. Lee and C.L. Lu                 The Basic Concepts of Algorithms p.17




     Θ notation
           Θ(g(n)) actually denotes a set of functions.
           f (n) = Θ(g(n)) indicates that f (n) is a
           member of Θ(g(n)).
           Let f (n) = 1 n2 − 3n. Then f (n) = Θ(n2 ),
                          2
           but f (n) = Θ(n) and f (n) = Θ(n3 ).
              Skill: ignore the lower-order terms and the
              coefficient of the highest-order term
           Any constant is denoted by Θ(1).
           (Any constant is a degree-0 polynomial, so it can
           be repressed as Θ(n0 ) of Θ(1).)

By R.C.T. Lee and C.L. Lu                 The Basic Concepts of Algorithms p.18
The constant hidden in O notation
           Let A1 and A2 be two algorithm of solving the
           same problem and their time complexities be O(n)
           and O (n3 ), respectively.
           If we ask the same person to write two programs,
           say P1 and P2 respectively, for A1 and A2 under
           the same programming environment,
           would P1 run faster than P2 ?
              P1 runs faster than P2 when n is large.
              P2 may run faster than P1 when n is small.
              (The constant hidden in O notation can not be
              ignored.)
By R.C.T. Lee and C.L. Lu                 The Basic Concepts of Algorithms p.19




     Types of complexity of algorithm
           Let T (I) be the time complexity of an algorithm
           A for instance I.
           1. Best case: min{T (I) : for all I}
           2. Average case: sum{T (I) · prob(I) : for all I}
                prob(I): probability of the occurrence of I
           3. Worst case: max{T (I) : for all I}
           Usually, we use O -notation and Ω-notation to
           denote the upper bound (worst case) and lower
           bound (best case) of algorithm A, respectively.



By R.C.T. Lee and C.L. Lu                 The Basic Concepts of Algorithms p.20
Insertion sorting algorithm
        Input: x1 , x2 , · · · , xn .
        Output: The sorted sequence of    x1 , x2 , · · · , xn .
        1. for j = 2 to n do /* Outer     loop */
        2.    i = j − 1;
        3.    x = xj ;
        4.    while x < xi and i > 0 do /* Inner loop */
        5.         xi+1 = xi ;
        6.         i = i − 1;
        7.    end while
        8.    xi+1 = x;
        9. end for

By R.C.T. Lee and C.L. Lu                   The Basic Concepts of Algorithms p.21




     An example of insertion sort
           Let the input sequence be 7, 5, 1, 4, 3, 2, 6.
           The process of insertion sorting is as follows.
             7 ⇐ 7,5,1,4,3,2,6      (Initial state)
             5, 7 ⇐ 5,1,4,3,2,6
             1, 5, 7 ⇐ 1,4,3,2,6
             1, 4, 5, 7 ⇐ 4,3,2,6
             1, 3, 4, 5, 7 ⇐ 3,2,6
             1, 2, 3, 4, 5, 7 ⇐ 2,6
             1, 2, 3, 4, 5, 6, 7 ⇐ 6       (Final state)


By R.C.T. Lee and C.L. Lu                   The Basic Concepts of Algorithms p.22
Complexity of insertion sorting
     Use the number of data movements as the time
     complexity measurement: X = n (2 + di )
                                    j=2

           Outer loop: x = xj , xi+1 = x (always executed)
           Inner loop: xi+1 = xi (not always executed)
           dj = |{xi : xi > xj , 1 ≤ i < j}|
           Best Case: sorted sequence (d1 = · · · = dn = 0)
           X = 2(n − 1) = O(n)
           Worst Case: reversely sorted sequence
           (d2 = 1, d3 = 2, · · · , dn = n − 1)
           X = (n−1)(n+4) = O(n2 )
                     2

By R.C.T. Lee and C.L. Lu                  The Basic Concepts of Algorithms p.23




     Complexity of insertion sorting
           Average Case: n j+3 = (n+8)(n−1) = O (n2 )
                                 j=2 2          4
             Let x1 , · · · , xj−1 be a sorted sequence and the
             next step is to insert xj .
             If xj is the ith largest number among the j
             numbers, there will be i − 1 movements in the
             inner loop (2 movements in the outer loop).
             The probability that xj is the ith largest among
             j numbers is 1 .  j
                Therefore, the average number of movement is
                2+0
                 j
                    + 2+1 + · · · + 2+j−1 = j+3 .
                        j               j      2

By R.C.T. Lee and C.L. Lu                  The Basic Concepts of Algorithms p.24
Polynomial/Exponetial algorithms
           Polynomial algorithm:
           whose complexity is bound by O(nk ),
           where n is the input size and k is a constant
           Exponential algorithm:
           whose complexity is bound by O(kn )
           Example: For MST problem,
             Prim and Kruskal’s methods: polynomial
             Brute force method: exponential
           Polynomial algorithms are better than exponential
           ones.

By R.C.T. Lee and C.L. Lu                  The Basic Concepts of Algorithms p.25




     How to measure the difficulty of a
     problem P?
           Is problem P solvable or not?
           If P is solvable, the time-complexity of P is
           min{T (A) : A is an algorithm of P},
           where T (A) is the time-complexity of A.
           Easy problem: solvable in polynomial time
             MST problem: greedy algorithm
           Difficult problem: impossibly find a polynomial
           time algorithm to solve it
              Traveling salesperson problem: NP-complete
              Halting problem: no algorithm

By R.C.T. Lee and C.L. Lu                  The Basic Concepts of Algorithms p.26
Upper/Lower bounds of problem
           Upper bound of problem P: the complexity of the
           best one among algorithms solving P
             Example: The upper bound of MST probelm is
             min{O(|V |2 ), O(|E| log |E|)}.
           Lower bound of P: use mathematical method to
           proof that any algorithm for P must have at least
           time-complexity f (n)
              Example: An trivial lower bound of MST
              problem is O(|V | + |E|).



By R.C.T. Lee and C.L. Lu                  The Basic Concepts of Algorithms p.27




     How to know that an algorithm A
     is optimal for a problem P?
           Is there any other better algorithm?
           A is optimal if there is no other better algorithm.
           A is optimal if the time-complexity of A is equal
           to the lower bound of P.

                                          Upper bound of P
                                          (Complexity of A)
                               Optimal

                                          Lower bound of P

By R.C.T. Lee and C.L. Lu                  The Basic Concepts of Algorithms p.28
Decision/Optimization problems
           Decision problem: the problem whose solution is
           simply ”yes” or ”no”
              Example: Traveling salesperson decision
              problem: Given a set of points and a constant c,
              is there a tour starting from any point v0 whose
              total length is less than c?
           Optimization problem: the problem of finding a
           solution whose value is optimal
              Example: Traveling salesperson problem: Given
              a set of points, find a shortest tour which starts
              from any point v0 .

By R.C.T. Lee and C.L. Lu                     The Basic Concepts of Algorithms p.29




     Decision/Optimization problems
           The optimization problems are more difficult than
           their corresponding decision problems.
                If we can solve the traveling salesperson problem,
                then we can solve the traveling salesperson
                decision problem, but not vice versa.
                If the traveling salesperson decision problem can
                not be solved by polynomial algorithms, then we
                can conclude that the traveling salesperson
                problem can not be solved by polynomial
                algorithms.


By R.C.T. Lee and C.L. Lu                     The Basic Concepts of Algorithms p.30
The Satisfiability Problem (SAT)
           Given a Boolean formula, determine whether this
           formula is satisfiable or not.
           Consider the following formula:
                              (x1 ∨ x2 ∨ x3 )
                          ∧ (−x1 )
                          ∧ (−x2 )
           The following assignment makes the formula true.
                                x1 ← F
                                x2 ← F
                                x3 ← T
By R.C.T. Lee and C.L. Lu                    The Basic Concepts of Algorithms p.31




     Time-complexity of SAT problem
           If there are n variables, then there are 2n possible
           assignments for the SAT problem.
           Up to now, for the best available algorithms for the
           SAT problem, they cost exponential time in worst
           cases.
           Is there any possibility that the SAT problem
           can be solved in polynomial time?
           By the theory of NP-completeness, if the SAT
           problem can be solved in polynomial time, then all
           NP problems can be solved in polynomial time.

By R.C.T. Lee and C.L. Lu                    The Basic Concepts of Algorithms p.32
Nondeterministic algorithm
           We may consider a nondeterministic algorithm as a
           algorithm consisting of two phases guessing and
           checking.
           Given two numbers x(1) = 7 and x(2) = 7,
           determine if there is a number which equals 7.
           Guessing: i = choice(1, 2);
           Checking: if x(i) = 7 then SUCCESS
                     else FAILURE.
           Nondeterministic polynomial algorithm:
           a nondeterministic algorithm whose checking stage
           can be done in polynomial time
By R.C.T. Lee and C.L. Lu                 The Basic Concepts of Algorithms p.33




     P and NP problems
           P problem: a decision problem which can be solved
           by a polynomial algorithm, such as the MST
           decision problem and the longest common
           subsequence decision problem
           NP problem: a decision problem which can be
           solved by a nondeterministic polynomial algorithm,
           such as the SAT problem and the traveling
           salesperson decision problem
           Every P problem must be an NP problem
           (i.e., P ⊆ NP).


By R.C.T. Lee and C.L. Lu                 The Basic Concepts of Algorithms p.34
P and NP problems
           Are all problems NP problems?
             Halting problem: given an arbitrary program
             with an arbitrary input data, will the program
             terminate or not?
             Halting problem is not an NP problem because it
             is undecidable.
           Are all NP problems P problems? (P = NP ?)
             P ⊆ NP ? (yes)
             P ⊇ NP ? (?): the SAT problem and the
             traveling salesperson decision problem can be
             solved in O(2n ) and O(n!) time, respectively.
By R.C.T. Lee and C.L. Lu                  The Basic Concepts of Algorithms p.35




     NP-complete problem
           In discussing NP problems, we shall only discuss
           decision problems.
           Problem P1 reduces to problem P2 (P1 ∝ P2 ):
             P1 can be solved in polynomial time by using a
             polynomial time algorithm solving P2 .
             P2 is more difficult than P1 .
           A problem P is NP-complete if
           1. P ∈ NP and
           2. P is NP-hard (every NP problem reduces to P).
           The SAT problem was the first found NP-complete
           problem by Cook (1971).
By R.C.T. Lee and C.L. Lu                  The Basic Concepts of Algorithms p.36
Theory of NP-Completeness
                                  If any NP-complete
                                  problem can be solved in
                 NP-Complete      polynomial time, NP = P.
               P                 Up to now, no NP-complete
                       NP        problem has any worst case
                                 polynomial algorithm.
           There are thousands of problems proved to be
           NP-complete problems.
           If the decision version of an optimization problem is
           NP-complete, this optimization problem is called
           NP-hard.
By R.C.T. Lee and C.L. Lu                   The Basic Concepts of Algorithms p.37

Más contenido relacionado

La actualidad más candente

On fuzzy compact open topology
On fuzzy compact open topologyOn fuzzy compact open topology
On fuzzy compact open topologybkrawat08
 
1 2 3
1 2 31 2 3
1 2 3wfke
 
Matrix Models of 2D String Theory in Non-trivial Backgrounds
Matrix Models of 2D String Theory in Non-trivial BackgroundsMatrix Models of 2D String Theory in Non-trivial Backgrounds
Matrix Models of 2D String Theory in Non-trivial BackgroundsUtrecht University
 
Chapter 06 boolean algebra 2o-p
Chapter 06 boolean algebra 2o-pChapter 06 boolean algebra 2o-p
Chapter 06 boolean algebra 2o-pIIUI
 
Topological Inference via Meshing
Topological Inference via MeshingTopological Inference via Meshing
Topological Inference via MeshingDon Sheehy
 
Practical computation of Hecke operators
Practical computation of Hecke operatorsPractical computation of Hecke operators
Practical computation of Hecke operatorsMathieu Dutour Sikiric
 
Local Volatility 1
Local Volatility 1Local Volatility 1
Local Volatility 1Ilya Gikhman
 
Entropy-Driven Evolutionary Approaches to the Mastermind Problem
Entropy-Driven Evolutionary Approaches to the Mastermind ProblemEntropy-Driven Evolutionary Approaches to the Mastermind Problem
Entropy-Driven Evolutionary Approaches to the Mastermind ProblemJuan J. Merelo
 
Module 11 Tansformation
Module 11  TansformationModule 11  Tansformation
Module 11 Tansformationguestcc333c
 
BALANCING BOARD MACHINES
BALANCING BOARD MACHINESBALANCING BOARD MACHINES
BALANCING BOARD MACHINESbutest
 
Module 7 The Straight Lines
Module 7 The Straight LinesModule 7 The Straight Lines
Module 7 The Straight Linesguestcc333c
 
Lesson 19: The Mean Value Theorem
Lesson 19: The Mean Value TheoremLesson 19: The Mean Value Theorem
Lesson 19: The Mean Value TheoremMatthew Leingang
 
Monfort Emath Paper1_printed
Monfort Emath Paper1_printedMonfort Emath Paper1_printed
Monfort Emath Paper1_printedFelicia Shirui
 

La actualidad más candente (18)

Approx
ApproxApprox
Approx
 
On fuzzy compact open topology
On fuzzy compact open topologyOn fuzzy compact open topology
On fuzzy compact open topology
 
1 2 3
1 2 31 2 3
1 2 3
 
Matrix Models of 2D String Theory in Non-trivial Backgrounds
Matrix Models of 2D String Theory in Non-trivial BackgroundsMatrix Models of 2D String Theory in Non-trivial Backgrounds
Matrix Models of 2D String Theory in Non-trivial Backgrounds
 
Chapter 06 boolean algebra 2o-p
Chapter 06 boolean algebra 2o-pChapter 06 boolean algebra 2o-p
Chapter 06 boolean algebra 2o-p
 
Bd32360363
Bd32360363Bd32360363
Bd32360363
 
5th Semester Electronic and Communication Engineering (2013-June) Question Pa...
5th Semester Electronic and Communication Engineering (2013-June) Question Pa...5th Semester Electronic and Communication Engineering (2013-June) Question Pa...
5th Semester Electronic and Communication Engineering (2013-June) Question Pa...
 
Topological Inference via Meshing
Topological Inference via MeshingTopological Inference via Meshing
Topological Inference via Meshing
 
Practical computation of Hecke operators
Practical computation of Hecke operatorsPractical computation of Hecke operators
Practical computation of Hecke operators
 
Local Volatility 1
Local Volatility 1Local Volatility 1
Local Volatility 1
 
Entropy-Driven Evolutionary Approaches to the Mastermind Problem
Entropy-Driven Evolutionary Approaches to the Mastermind ProblemEntropy-Driven Evolutionary Approaches to the Mastermind Problem
Entropy-Driven Evolutionary Approaches to the Mastermind Problem
 
Module 11 Tansformation
Module 11  TansformationModule 11  Tansformation
Module 11 Tansformation
 
Module 5 Sets
Module 5 SetsModule 5 Sets
Module 5 Sets
 
BALANCING BOARD MACHINES
BALANCING BOARD MACHINESBALANCING BOARD MACHINES
BALANCING BOARD MACHINES
 
Module 7 The Straight Lines
Module 7 The Straight LinesModule 7 The Straight Lines
Module 7 The Straight Lines
 
Final
Final Final
Final
 
Lesson 19: The Mean Value Theorem
Lesson 19: The Mean Value TheoremLesson 19: The Mean Value Theorem
Lesson 19: The Mean Value Theorem
 
Monfort Emath Paper1_printed
Monfort Emath Paper1_printedMonfort Emath Paper1_printed
Monfort Emath Paper1_printed
 

Similar a Algorithm

Answers withexplanations
Answers withexplanationsAnswers withexplanations
Answers withexplanationsGopi Saiteja
 
Nbhm m. a. and m.sc. scholarship test 2011
Nbhm m. a. and m.sc. scholarship test 2011Nbhm m. a. and m.sc. scholarship test 2011
Nbhm m. a. and m.sc. scholarship test 2011MD Kutubuddin Sardar
 
Set theory and relation
Set theory and relationSet theory and relation
Set theory and relationankush_kumar
 
Ee693 sept2014midsem
Ee693 sept2014midsemEe693 sept2014midsem
Ee693 sept2014midsemGopi Saiteja
 
Lattices, sphere packings, spherical codes
Lattices, sphere packings, spherical codesLattices, sphere packings, spherical codes
Lattices, sphere packings, spherical codeswtyru1989
 
Core 4 Parametric Equations 2
Core 4 Parametric Equations 2Core 4 Parametric Equations 2
Core 4 Parametric Equations 2davidmiles100
 
Sin cos questions
Sin cos questionsSin cos questions
Sin cos questionsGarden City
 
Sin cos questions
Sin cos questionsSin cos questions
Sin cos questionsGarden City
 
Ee693 questionshomework
Ee693 questionshomeworkEe693 questionshomework
Ee693 questionshomeworkGopi Saiteja
 
Low-rank tensor approximation (Introduction)
Low-rank tensor approximation (Introduction)Low-rank tensor approximation (Introduction)
Low-rank tensor approximation (Introduction)Alexander Litvinenko
 

Similar a Algorithm (20)

Unit 3
Unit 3Unit 3
Unit 3
 
Unit 3
Unit 3Unit 3
Unit 3
 
Answers withexplanations
Answers withexplanationsAnswers withexplanations
Answers withexplanations
 
m.tech final
m.tech finalm.tech final
m.tech final
 
Nbhm m. a. and m.sc. scholarship test 2011
Nbhm m. a. and m.sc. scholarship test 2011Nbhm m. a. and m.sc. scholarship test 2011
Nbhm m. a. and m.sc. scholarship test 2011
 
Set theory and relation
Set theory and relationSet theory and relation
Set theory and relation
 
Ee693 sept2014midsem
Ee693 sept2014midsemEe693 sept2014midsem
Ee693 sept2014midsem
 
Chemisty Stream (2013-January) Question Papers
Chemisty  Stream (2013-January) Question PapersChemisty  Stream (2013-January) Question Papers
Chemisty Stream (2013-January) Question Papers
 
Paper
PaperPaper
Paper
 
Kuiz 1 & 2
Kuiz 1 & 2Kuiz 1 & 2
Kuiz 1 & 2
 
Lattices, sphere packings, spherical codes
Lattices, sphere packings, spherical codesLattices, sphere packings, spherical codes
Lattices, sphere packings, spherical codes
 
Core 4 Parametric Equations 2
Core 4 Parametric Equations 2Core 4 Parametric Equations 2
Core 4 Parametric Equations 2
 
Sin cos questions
Sin cos questionsSin cos questions
Sin cos questions
 
Sin cos questions
Sin cos questionsSin cos questions
Sin cos questions
 
Ee693 questionshomework
Ee693 questionshomeworkEe693 questionshomework
Ee693 questionshomework
 
Low-rank tensor approximation (Introduction)
Low-rank tensor approximation (Introduction)Low-rank tensor approximation (Introduction)
Low-rank tensor approximation (Introduction)
 
Mtc ssample05
Mtc ssample05Mtc ssample05
Mtc ssample05
 
Mtc ssample05
Mtc ssample05Mtc ssample05
Mtc ssample05
 
F.Y.B.Sc(2013 pattern) Old Question Papers:Dr.Kshirsagar
F.Y.B.Sc(2013 pattern) Old Question Papers:Dr.KshirsagarF.Y.B.Sc(2013 pattern) Old Question Papers:Dr.Kshirsagar
F.Y.B.Sc(2013 pattern) Old Question Papers:Dr.Kshirsagar
 
4th Semester (June-2016) Computer Science and Information Science Engineering...
4th Semester (June-2016) Computer Science and Information Science Engineering...4th Semester (June-2016) Computer Science and Information Science Engineering...
4th Semester (June-2016) Computer Science and Information Science Engineering...
 

Más de Xavient Information Systems (10)

Mobility in network
Mobility in networkMobility in network
Mobility in network
 
Software engineering
Software engineeringSoftware engineering
Software engineering
 
Binary searchtrees
Binary searchtreesBinary searchtrees
Binary searchtrees
 
Red black-trees-4
Red black-trees-4Red black-trees-4
Red black-trees-4
 
16 mergesort
16 mergesort16 mergesort
16 mergesort
 
Train name index
Train name indexTrain name index
Train name index
 
Osi model
Osi modelOsi model
Osi model
 
Discrete math mathematics for computer science 2
Discrete math   mathematics for computer science 2Discrete math   mathematics for computer science 2
Discrete math mathematics for computer science 2
 
Harsh embedded systems
Harsh embedded systemsHarsh embedded systems
Harsh embedded systems
 
Harsh
HarshHarsh
Harsh
 

Último

Zeshan Sattar- Assessing the skill requirements and industry expectations for...
Zeshan Sattar- Assessing the skill requirements and industry expectations for...Zeshan Sattar- Assessing the skill requirements and industry expectations for...
Zeshan Sattar- Assessing the skill requirements and industry expectations for...itnewsafrica
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfNeo4j
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS:  6 Ways to Automate Your Data IntegrationBridging Between CAD & GIS:  6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integrationmarketing932765
 
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...Nikki Chapple
 
Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security ObservabilityGlenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security Observabilityitnewsafrica
 
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Alkin Tezuysal
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...itnewsafrica
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...Wes McKinney
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch TuesdayIvanti
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityIES VE
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesThousandEyes
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotesMuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotesManik S Magar
 
Data governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationData governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationKnoldus Inc.
 

Último (20)

Zeshan Sattar- Assessing the skill requirements and industry expectations for...
Zeshan Sattar- Assessing the skill requirements and industry expectations for...Zeshan Sattar- Assessing the skill requirements and industry expectations for...
Zeshan Sattar- Assessing the skill requirements and industry expectations for...
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdf
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS:  6 Ways to Automate Your Data IntegrationBridging Between CAD & GIS:  6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integration
 
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
 
Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security ObservabilityGlenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
 
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch Tuesday
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a reality
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotesMuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
 
Data governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationData governance with Unity Catalog Presentation
Data governance with Unity Catalog Presentation
 

Algorithm

  • 1. The Basic Concepts of Algorithms R.C.T. Lee and Chin Lung Lu Algorithms for Molecular Biology By R.C.T. Lee and C.L. Lu The Basic Concepts of Algorithms p.1 What is algorithm? A method that can be used by a computer for the solution of a problem. A sequence of computational steps that transform the input into the output. Examples of algorithms in the nature: DNA and cook book. The word ”algorithm” comes from the name of a Persian author, Abu Ja’far Mohammed ibn Musa al Khowarizmi (c. 825 A.D.), who wrote a textbook on mathematics. By R.C.T. Lee and C.L. Lu The Basic Concepts of Algorithms p.2
  • 2. Why should we study algorithms? A good algorithm implemented on a slow computer may perform much better than a bad algorithm implemented on a fast computer. f (n) n 10 102 103 log2 n 3.3 6.6 10 n 10 102 103 nlog2 n 0.33 × 102 0.7 × 103 104 n2 102 104 106 2n 1024 1.3 × 103 > 10100 n! 36 > 10100 > 10100 By R.C.T. Lee and C.L. Lu The Basic Concepts of Algorithms p.3 Minimal spanning tree problem Given a set of points, find a spanning tree with the shortest total length. a e a e b b d d f c f c Input ❶ a e a e b b d d f c f c ❷ ❸ By R.C.T. Lee and C.L. Lu The Basic Concepts of Algorithms p.4
  • 3. Minimal spanning tree problem MST problem: given a set of points, find a spanning tree with the shortest total length Brute force method: enumerate all possible spanning trees and select the best one among them Given n points, there are nn−2 possible spanning trees for them. By R.C.T. Lee and C.L. Lu The Basic Concepts of Algorithms p.5 How to design a good algorithm? Efficient or not? (Efficient means short time and small space.) Strategies of algorithms: 1. Greedy 2. Divide & conquer 3. Prune & search 4. Dynamic programming 5. Branch and bound 6. Approximation 7. Heuristics By R.C.T. Lee and C.L. Lu The Basic Concepts of Algorithms p.6
  • 4. Prim’s algorithm for MST Input: A weighted and connected graph G = (V, E). Output: A minimum spanning tree of G. 1: Let x be any vertex in V ; Let X = {x} and Y = V {x}; 2: Select an edge (u, v) from E such that u ∈ X, v ∈ Y and (u, v) has the smallest weight among edges between X and Y ; 3: Connect u to v; Let X = X ∪ {v} and Y = Y {v}; 4: If Y is empty, terminate and the resulting tree is a minimal spanning tree; Otherwise, go to step 2; By R.C.T. Lee and C.L. Lu The Basic Concepts of Algorithms p.7 Prim’s algorithm for MST a 15 b ❶ b 18 c 3 X = {b}, 10 1 Y = {a, c, e, d}, 1 7 9 5 (b, d) the shortest. d e d ❷ 3 b ❸ 3 b X = {b, d}, X = {b, d, c}, 1 1 Y = {a, c, e}, c Y = {a, e}, 7 c (b, c) the shortest. d (c, e) the shortest. e d a b a b ❹ c 15 c 18 3 X = {b, d, c, e} 10 3 1 10 1 Y = {a}, 7 7 5 e d (e, a) the shortest. e 9 d By R.C.T. Lee and C.L. Lu The Basic Concepts of Algorithms p.8
  • 5. Kruskal’s algorithm for MST Input: A weighted and connected graph G = (V, E). Output: A minimum spanning tree of G. 1: T = ∅; 2: while T contains less than n − 1 edges do Choose an edge (v, w) from E of smallest weight; Delete (v, w) from E; if adding (v, w) does not create cycle in T then Add (v, w) to T ; else Discard (v, w); end if end while By R.C.T. Lee and C.L. Lu The Basic Concepts of Algorithms p.9 Kruskal’s algorithm for MST a 15 b ❶b 18 c 3 10 1 (b, d) is selected 1 and added. 7 5 9 e d d ❷ b ❸ b 3 3 1 (b, c) is selected 1 (c, d) is selected c and added. c and discarded. d d ❹ b ❺a b 3 3 (c, e) is selected. 10 1 (a, e) is selected c 1 and added. 7 c and added. 7 e d e d By R.C.T. Lee and C.L. Lu The Basic Concepts of Algorithms p.10
  • 6. How to measure time complexity of algorithm A? 1. Write a program for A and see how fast it runs. Not suitable for many factors unrelated to A, such as the capability of programmer, the used language and compiler, operating system, CPU’s speed etc. 2. Choose the particular steps in A and determine the number of the needed steps The particular steps are time-consuming operations, like comparison of data, movement of data, +, −, ∗, / operations etc. By R.C.T. Lee and C.L. Lu The Basic Concepts of Algorithms p.11 Time complexity of algorithm A Time complexity of an algorithm: Equal to number of operations in algorithm A Usually represented by a function of the size of the input Size of the input: 1. sorting: number of items 2. graph problems: number of vertices and edges 3. multiplying two integers: number of bits Example: For MST problem, Prim’s algorithm = |V |2 time Kruskal’s algorithm = |E| log |E| + |V | time By R.C.T. Lee and C.L. Lu The Basic Concepts of Algorithms p.12
  • 7. O notation O(g(n)) = {f (n)| there exist positive constants c and n0 such that f (n) ≤ cg(n) for all n ≥ n0 }. cg(n) f (n) f (n) = O(g(n)) n n0 By R.C.T. Lee and C.L. Lu The Basic Concepts of Algorithms p.13 O notation O (g(n)) actually denotes a set of functions. f (n) = O (g(n)) indicates that f (n) is a member of O (g(n)). Example: Let f (n) = 1 n2 − 3n. Then 2 1. f (n) = O (n2 ) (✔) 2. f (n) = O (n3 ) (✔) 3. f (n) = O (n) (✘) ”The running time of an algorithm A is O(n2 )” means that the worst-case running time of A is O (n2 ). By R.C.T. Lee and C.L. Lu The Basic Concepts of Algorithms p.14
  • 8. Ω notation Ω(g(n)) = {f (n)| there exist positive constants c and n0 such that f (n) ≥ cg(n) for all n ≥ n0 }. f (n) cg(n) f (n) = Ω(g(n)) n n0 By R.C.T. Lee and C.L. Lu The Basic Concepts of Algorithms p.15 Ω notation Ω(g(n)) actually denotes a set of functions. f (n) = Ω(g(n)) indicates that f (n) is a member of Ω(g(n)). Example: Let f (n) = 1 n2 + 3n. Then 2 1. f (n) = Ω(n2 ) (✔) 2. f (n) = Ω(n3 ) (✘) 3. f (n) = Ω(n) (✔) 4. f (n) = Ω(1) (✔) By R.C.T. Lee and C.L. Lu The Basic Concepts of Algorithms p.16
  • 9. Θ notation Θ(g(n)) = {f (n)| there exist positive constants c1 , c2 and n0 such that c1 g(n) ≤ f (n) ≤ c2 g(n) for all n ≥ n0 }. c2 g(n) f (n) c1 g(n) f (n) = Θ(g(n)) n n0 By R.C.T. Lee and C.L. Lu The Basic Concepts of Algorithms p.17 Θ notation Θ(g(n)) actually denotes a set of functions. f (n) = Θ(g(n)) indicates that f (n) is a member of Θ(g(n)). Let f (n) = 1 n2 − 3n. Then f (n) = Θ(n2 ), 2 but f (n) = Θ(n) and f (n) = Θ(n3 ). Skill: ignore the lower-order terms and the coefficient of the highest-order term Any constant is denoted by Θ(1). (Any constant is a degree-0 polynomial, so it can be repressed as Θ(n0 ) of Θ(1).) By R.C.T. Lee and C.L. Lu The Basic Concepts of Algorithms p.18
  • 10. The constant hidden in O notation Let A1 and A2 be two algorithm of solving the same problem and their time complexities be O(n) and O (n3 ), respectively. If we ask the same person to write two programs, say P1 and P2 respectively, for A1 and A2 under the same programming environment, would P1 run faster than P2 ? P1 runs faster than P2 when n is large. P2 may run faster than P1 when n is small. (The constant hidden in O notation can not be ignored.) By R.C.T. Lee and C.L. Lu The Basic Concepts of Algorithms p.19 Types of complexity of algorithm Let T (I) be the time complexity of an algorithm A for instance I. 1. Best case: min{T (I) : for all I} 2. Average case: sum{T (I) · prob(I) : for all I} prob(I): probability of the occurrence of I 3. Worst case: max{T (I) : for all I} Usually, we use O -notation and Ω-notation to denote the upper bound (worst case) and lower bound (best case) of algorithm A, respectively. By R.C.T. Lee and C.L. Lu The Basic Concepts of Algorithms p.20
  • 11. Insertion sorting algorithm Input: x1 , x2 , · · · , xn . Output: The sorted sequence of x1 , x2 , · · · , xn . 1. for j = 2 to n do /* Outer loop */ 2. i = j − 1; 3. x = xj ; 4. while x < xi and i > 0 do /* Inner loop */ 5. xi+1 = xi ; 6. i = i − 1; 7. end while 8. xi+1 = x; 9. end for By R.C.T. Lee and C.L. Lu The Basic Concepts of Algorithms p.21 An example of insertion sort Let the input sequence be 7, 5, 1, 4, 3, 2, 6. The process of insertion sorting is as follows. 7 ⇐ 7,5,1,4,3,2,6 (Initial state) 5, 7 ⇐ 5,1,4,3,2,6 1, 5, 7 ⇐ 1,4,3,2,6 1, 4, 5, 7 ⇐ 4,3,2,6 1, 3, 4, 5, 7 ⇐ 3,2,6 1, 2, 3, 4, 5, 7 ⇐ 2,6 1, 2, 3, 4, 5, 6, 7 ⇐ 6 (Final state) By R.C.T. Lee and C.L. Lu The Basic Concepts of Algorithms p.22
  • 12. Complexity of insertion sorting Use the number of data movements as the time complexity measurement: X = n (2 + di ) j=2 Outer loop: x = xj , xi+1 = x (always executed) Inner loop: xi+1 = xi (not always executed) dj = |{xi : xi > xj , 1 ≤ i < j}| Best Case: sorted sequence (d1 = · · · = dn = 0) X = 2(n − 1) = O(n) Worst Case: reversely sorted sequence (d2 = 1, d3 = 2, · · · , dn = n − 1) X = (n−1)(n+4) = O(n2 ) 2 By R.C.T. Lee and C.L. Lu The Basic Concepts of Algorithms p.23 Complexity of insertion sorting Average Case: n j+3 = (n+8)(n−1) = O (n2 ) j=2 2 4 Let x1 , · · · , xj−1 be a sorted sequence and the next step is to insert xj . If xj is the ith largest number among the j numbers, there will be i − 1 movements in the inner loop (2 movements in the outer loop). The probability that xj is the ith largest among j numbers is 1 . j Therefore, the average number of movement is 2+0 j + 2+1 + · · · + 2+j−1 = j+3 . j j 2 By R.C.T. Lee and C.L. Lu The Basic Concepts of Algorithms p.24
  • 13. Polynomial/Exponetial algorithms Polynomial algorithm: whose complexity is bound by O(nk ), where n is the input size and k is a constant Exponential algorithm: whose complexity is bound by O(kn ) Example: For MST problem, Prim and Kruskal’s methods: polynomial Brute force method: exponential Polynomial algorithms are better than exponential ones. By R.C.T. Lee and C.L. Lu The Basic Concepts of Algorithms p.25 How to measure the difficulty of a problem P? Is problem P solvable or not? If P is solvable, the time-complexity of P is min{T (A) : A is an algorithm of P}, where T (A) is the time-complexity of A. Easy problem: solvable in polynomial time MST problem: greedy algorithm Difficult problem: impossibly find a polynomial time algorithm to solve it Traveling salesperson problem: NP-complete Halting problem: no algorithm By R.C.T. Lee and C.L. Lu The Basic Concepts of Algorithms p.26
  • 14. Upper/Lower bounds of problem Upper bound of problem P: the complexity of the best one among algorithms solving P Example: The upper bound of MST probelm is min{O(|V |2 ), O(|E| log |E|)}. Lower bound of P: use mathematical method to proof that any algorithm for P must have at least time-complexity f (n) Example: An trivial lower bound of MST problem is O(|V | + |E|). By R.C.T. Lee and C.L. Lu The Basic Concepts of Algorithms p.27 How to know that an algorithm A is optimal for a problem P? Is there any other better algorithm? A is optimal if there is no other better algorithm. A is optimal if the time-complexity of A is equal to the lower bound of P. Upper bound of P (Complexity of A) Optimal Lower bound of P By R.C.T. Lee and C.L. Lu The Basic Concepts of Algorithms p.28
  • 15. Decision/Optimization problems Decision problem: the problem whose solution is simply ”yes” or ”no” Example: Traveling salesperson decision problem: Given a set of points and a constant c, is there a tour starting from any point v0 whose total length is less than c? Optimization problem: the problem of finding a solution whose value is optimal Example: Traveling salesperson problem: Given a set of points, find a shortest tour which starts from any point v0 . By R.C.T. Lee and C.L. Lu The Basic Concepts of Algorithms p.29 Decision/Optimization problems The optimization problems are more difficult than their corresponding decision problems. If we can solve the traveling salesperson problem, then we can solve the traveling salesperson decision problem, but not vice versa. If the traveling salesperson decision problem can not be solved by polynomial algorithms, then we can conclude that the traveling salesperson problem can not be solved by polynomial algorithms. By R.C.T. Lee and C.L. Lu The Basic Concepts of Algorithms p.30
  • 16. The Satisfiability Problem (SAT) Given a Boolean formula, determine whether this formula is satisfiable or not. Consider the following formula: (x1 ∨ x2 ∨ x3 ) ∧ (−x1 ) ∧ (−x2 ) The following assignment makes the formula true. x1 ← F x2 ← F x3 ← T By R.C.T. Lee and C.L. Lu The Basic Concepts of Algorithms p.31 Time-complexity of SAT problem If there are n variables, then there are 2n possible assignments for the SAT problem. Up to now, for the best available algorithms for the SAT problem, they cost exponential time in worst cases. Is there any possibility that the SAT problem can be solved in polynomial time? By the theory of NP-completeness, if the SAT problem can be solved in polynomial time, then all NP problems can be solved in polynomial time. By R.C.T. Lee and C.L. Lu The Basic Concepts of Algorithms p.32
  • 17. Nondeterministic algorithm We may consider a nondeterministic algorithm as a algorithm consisting of two phases guessing and checking. Given two numbers x(1) = 7 and x(2) = 7, determine if there is a number which equals 7. Guessing: i = choice(1, 2); Checking: if x(i) = 7 then SUCCESS else FAILURE. Nondeterministic polynomial algorithm: a nondeterministic algorithm whose checking stage can be done in polynomial time By R.C.T. Lee and C.L. Lu The Basic Concepts of Algorithms p.33 P and NP problems P problem: a decision problem which can be solved by a polynomial algorithm, such as the MST decision problem and the longest common subsequence decision problem NP problem: a decision problem which can be solved by a nondeterministic polynomial algorithm, such as the SAT problem and the traveling salesperson decision problem Every P problem must be an NP problem (i.e., P ⊆ NP). By R.C.T. Lee and C.L. Lu The Basic Concepts of Algorithms p.34
  • 18. P and NP problems Are all problems NP problems? Halting problem: given an arbitrary program with an arbitrary input data, will the program terminate or not? Halting problem is not an NP problem because it is undecidable. Are all NP problems P problems? (P = NP ?) P ⊆ NP ? (yes) P ⊇ NP ? (?): the SAT problem and the traveling salesperson decision problem can be solved in O(2n ) and O(n!) time, respectively. By R.C.T. Lee and C.L. Lu The Basic Concepts of Algorithms p.35 NP-complete problem In discussing NP problems, we shall only discuss decision problems. Problem P1 reduces to problem P2 (P1 ∝ P2 ): P1 can be solved in polynomial time by using a polynomial time algorithm solving P2 . P2 is more difficult than P1 . A problem P is NP-complete if 1. P ∈ NP and 2. P is NP-hard (every NP problem reduces to P). The SAT problem was the first found NP-complete problem by Cook (1971). By R.C.T. Lee and C.L. Lu The Basic Concepts of Algorithms p.36
  • 19. Theory of NP-Completeness If any NP-complete problem can be solved in NP-Complete polynomial time, NP = P. P Up to now, no NP-complete NP problem has any worst case polynomial algorithm. There are thousands of problems proved to be NP-complete problems. If the decision version of an optimization problem is NP-complete, this optimization problem is called NP-hard. By R.C.T. Lee and C.L. Lu The Basic Concepts of Algorithms p.37