SlideShare a Scribd company logo
1 of 50
Download to read offline
Ramzi Shihadeh Alqrainy   qcs_2008@yahoo.com   1




Data Structure Advanced
Algorithm
2

     Well-defined sequence of computational steps.
     The word Algorithm refers to a precise method
     useable by a computer for solution of a problem.
     The Algorithm accepts a set of values as input
     and produces a set of values as output.




            Ramzi Shihadeh Alqrainy   qcs_2008@yahoo.com
Examples
3


     Algorithm written in Human language

    Input a,b,c
    Compute the sum of a,b,c
    Divide the Sum by 3
    Display the Result




            Ramzi Shihadeh Alqrainy   qcs_2008@yahoo.com
Example
4


     Algorithm written in Programming language.




            Ramzi Shihadeh Alqrainy   qcs_2008@yahoo.com
Example
5


      Algorithm written in Pseudocode Syntax.
    Step 1: Promt a,b,c
    Step 2: Let Sum     a+b+c
    Step 3: Let Avg      Sum / 3
    Step 4: Output Avg




             Ramzi Shihadeh Alqrainy   qcs_2008@yahoo.com
ALGORITHM
6

       What do we mean by input ?
    The word input interdependent on the type of
       problem.
    - Set of values or elements
               e.g in the array problem
    - A Set of bits
               e.g in the string problem
    - A Set of objects
               e.g in the graph problems the input may be the set
      of vertices and edges.

               Ramzi Shihadeh Alqrainy   qcs_2008@yahoo.com
The Properties of Algorithm
7



     It must be correct
     It must be composed of a series of concrete
     steps.
     There can be no ambiguity as to which step will
     be performed next.
     It must be terminate.




            Ramzi Shihadeh Alqrainy   qcs_2008@yahoo.com
Analysis of Algorithm
8

        What is a good algorithm ?
    -   Is a clear algorithm ??
    -   Has a minimum steps ??
    -   Decreases the CPU time
    -   Has a minimum memory (RAM) space ?
    -   It is a good mapping for the problem.
    -   Uses minimum spaces of disk I/O


    ** In this course the best Algorithm is the one that
      uses the minimum CPU resources
                 Ramzi Shihadeh Alqrainy   qcs_2008@yahoo.com
Study of Algorithm
9




       Design                                                Analysis

    Methods and technique which                   Mathematical comparison of
    yield a good and useful                         algorithm ( without
    Algorithm to solve a problem.                   actually implement )




            Ramzi Shihadeh Alqrainy   qcs_2008@yahoo.com
Algorithms
10
         Types , methods , and techniques of ALG :
     -   Incremental Algorithms
     -   Simple Recursive Algorithms
     -   Divide and Conquer Algorithms
     -   Greedy Algorithms
     -   Dynamic Algorithms
     -   Backtracking Algorithms
     -   Branch and Bound Algorithms
     -   Brute and Force Algorithms
     -   Randomized Algorithms

                 Ramzi Shihadeh Alqrainy   qcs_2008@yahoo.com
Analysis of Algorithm
11


      The analysis of an algorithm focuses on the time
      and space complexity.
      The Time Complexity is the amount of time
      required by an algorithm to run to completion.
      The Time Complexity is a function of input size
      “n”      T(n)
      The Time Complexity refers to the amount of time
      required by an ALG to run completion


             Ramzi Shihadeh Alqrainy   qcs_2008@yahoo.com
Time Complexity
12




           Ramzi Shihadeh Alqrainy   qcs_2008@yahoo.com
Time Complexity
13


         Different times can arise for the same algorithm
     -   Worse-Case time complexity
     is a function defined by the maximum amount of time needed by an
         algorithm for an input size “n”.
     -   Best-Case time complexity
     is a function defined by the minimum amount of time needed by an
         algorithm for an input size “n”.
     -   Avarage-Case time complexity
     is the execution of an algorithm having typical input of size “n”.




                  Ramzi Shihadeh Alqrainy   qcs_2008@yahoo.com
Notation for writting algorithm
14


      In our DS(2) course we will express all the algorithm by
      Pseudocodes representation.
      This means that the algorithms are language and machine
      independent.
      The Psuedocode hides the implementation details and
      focuses on the computational aspects of an algorithm
      There are no grammar rules in the Pseudocode, it consists of
      keywords and English. Like phrase which specify the flow of
      control.
      Pseudocode is not case sensitive.


               Ramzi Shihadeh Alqrainy   qcs_2008@yahoo.com
Time Complexity
15

       Each instruction takes only one unit of time.
     a=a+1                one unit time
     a=x^2+3*x-4               one unit time
     Algorithm ABC(x,y)
                                          cost         time
     SET Sum     x+y            c1                     1
     SET Mul      x*y            c2                     1
     Total Cost = c1 *1 + c2 * 1
                 = c1 + c2
      T(n)      = Cn0
     The complexity of this algorithm is Θ(n0)                 Θ(1)
                Ramzi Shihadeh Alqrainy   qcs_2008@yahoo.com
Time Complexity
16

         Find the running for the following codes :
                                             cost          time
      Sum 0                                  c1            1
      Sum       Sum + next               c2               1
      T(n)= c1 + c2 = c3
      -------------------------------------------------------------------------------------
                                               cost           time
      S     0                                    c1              1
      For i 2 to n Do                          c2               n
           S      s+I                           c3               n-1
      End For
      S S/3                                      c4               1
      T(n)= c1 + n c2 + (n-1) c3 + c4                    T(n) is Θ(n)
                   Ramzi Shihadeh Alqrainy    qcs_2008@yahoo.com
Time Complexity
17

                                                    cost       time
      For i      1 to n Do                          c1        n+1
           sum        sum + 1                      c2        n
      End For
      T(n) = (n+1) c1 + n c2 = (c1+ c2)n + c2
             = c3n + c1
      T(n) is Θ(n)
      OR       T(n) =     =n


              Ramzi Shihadeh Alqrainy   qcs_2008@yahoo.com
Time Complexity
18
                                                   cost        time
      For i       1 To n Do             c1                   n+1
            For j     1 To m Do         c2                   n(m+1)
                 anything()              c3                   mn
            End For
      End For
      T(n)= (n+1) c1 + (n)(m+1) c2 + (n)(m)c3
            = n c1 + c1 + mn c2 + n c2 + mn c3
      T(n) is Θ(mn)
      OR             =          = nm

              Ramzi Shihadeh Alqrainy   qcs_2008@yahoo.com
Time Complexity
19

       For j     1 To n Do
           For k     1 To j Do
               anything()
           End For
      End For

      T(n)=             =             =

      T(n) is Θ(n2)


                Ramzi Shihadeh Alqrainy   qcs_2008@yahoo.com
Time Complexity
20

     For i      1 To n Do
          For j     i To n Do
               anything()
          End For
     End For
     T(n)=          =                     =         -          +

                      = n2 -                  +n


                Ramzi Shihadeh Alqrainy   qcs_2008@yahoo.com
Time Complexity
21

       Suppose that the function Bar(n) requires Θ(n2) what is the
       complexity at the following algorithm ?

      Algorithm Foo(n)                             cost         time
          Set x      0
          For i      1 To n Do
               Set x       x + Bar(i)
          End For
     T(n) =


                 Ramzi Shihadeh Alqrainy   qcs_2008@yahoo.com
Time Complexity
22

     Algorithm Foo(n)
     Set s      0                                         +         =
     Repeat Until n=1 Do
              s      s+1                          (n-1) + (n-1) =
              n      n-1                             2n-2
     End Repeat
     For i    1 To s Do                              T(n) is Θ(n)
          anything()
     End For


                Ramzi Shihadeh Alqrainy   qcs_2008@yahoo.com
Time Complexity
23
                                                      cost     time
     Algorithm Foo(n)
     Set s      0
     Repeat Until n=1 Do
              s      s+1
              n      n/2
     End Repeat
     For i    1 To s Do
          anything()
     End For


                Ramzi Shihadeh Alqrainy   qcs_2008@yahoo.com
Insertion Sort
24



      Sort Array A using the Insertion-Sort algorithm




             Ramzi Shihadeh Alqrainy   qcs_2008@yahoo.com
Insertion Sort
25




            Ramzi Shihadeh Alqrainy   qcs_2008@yahoo.com
Insertion Sort
26

     INSERTION-SORT (A, n)                 ⊳A[1 . . n]
                                                                   cost   time
     For j ←2 to n do                                              c1      n
          Set key ←A[ j]                                           c2      n-1
          Set i ←j –1                                              c3      n-1
          Repeat While (i > 0 and A[i] > key) do                  c4      tj
                    Set A[i+1] ←A[i]                                c5         tj-1


                   Set i ←i –1                                      c6         tj-1
         End Repeat
         A[i+1] = key                                               c7         n-1
     End For
                 Ramzi Shihadeh Alqrainy     qcs_2008@yahoo.com
Insertion Sort
27

       The running time of I-S algorithm is
           ∑(cost of step) * (number of times step is executed)
     So
     T(n)= n c1 + (n-1) c2 + (n-1)c3 + tj c4 +                  tj-1 c5 +   tj-1 c6
        + (n-1) c7

     * To solve the above T(n) , we should know firstly the value of
        tj , and this is vary depends on the input.




                 Ramzi Shihadeh Alqrainy   qcs_2008@yahoo.com
Insertion Sort
28

       The Best-Case

     The array is already sorted

     tj = 1                 = (n-1)

     T(n) = nc1 + nc2 – c2 + nc3 – c3 + nc4 – c4 + nc7 – c7
           = (c1 + c2 + c3 + c4 + c7 )n + (-c2-c3-c4- c7)
           = An+B
     T(n) is Θ(n)
                 Ramzi Shihadeh Alqrainy   qcs_2008@yahoo.com
Insertion Sort
29

        The Worse-Case
     The array is in reverse stored order
     tj = j
     T(n)= n c1 + (n-1) c2 + (n-1)c3 + c4 + c5                    + c6   + (n-1) c7

        =            -1                         =

     T(n) is Θ(n2)




                      Ramzi Shihadeh Alqrainy       qcs_2008@yahoo.com
Homework
30

          Consider sorting n-numbers sorted in array A by first
          finding the smallest element of A and putting it in the first
          entry of another array B , then finding the second element
          of A and putting it in the second entry of B . Then continue
          in this manner for the n elements of B.

     1.    Write Pseudocode for this algorithm.
     2.    Compute the best and worse-case running time
     3.    Express these two running time using Θ-notation.



                   Ramzi Shihadeh Alqrainy   qcs_2008@yahoo.com
Asymptotic Notations
31

          These notations are used to express the complexity
          of a given algorithm, or used to compute two
          algorithms or more that are designed to solve the
          same problem.
     1)    Big-Oh      Ο()
     2)    Big-Omega Ω()
     3)    Bog-Theta Θ()
     4)    Little-Oh o ()
     5)    Little-Omega ω()


                 Ramzi Shihadeh Alqrainy   qcs_2008@yahoo.com
Asymptotic Notations
32


         Big-Oh
     -   Upper-Bound
     -   Express the worse-case

     Ο(g(n))={T(n):∃n0,c are positives, such that T(n) ≤ cg(n), ∀ 0}
                   ∃                                            ∀n≥n


     T(n)=2n+1 Є O(n)
     T(n)=10n Є O(n)
     T(n)=0.2n-7 Є O(n)

                   Ramzi Shihadeh Alqrainy   qcs_2008@yahoo.com
Asymptotic Notations
33


      Prove that
                            T(n)= 2n+1 is O(n)

                           T(n) ≤ cg(n)
                          2n+1 ≤ cn
                          2n+1 ≤ 2n+n                   ∀ n ≥1
                          2n+1 ≤ 3n                     where c=3,n0=1




              Ramzi Shihadeh Alqrainy   qcs_2008@yahoo.com
Asymptotic Notations
34

         Big-Omega
     - Lower-Bound
     - Express the best-case

     Ω(g(n))={T(n):∃n0,c are positives, such that T(n) ≥ cg(n), ∀n ≥ n0}
                   ∃                                            ∀
         Big-Theta
     -   Tight Bound
     -   Express the best-case , worst-case, Avg-case
     Θ(g(n))={T(n):∃n0,c1,c2 are positives, such that
     c1g(n) ≥ T(n) ≥ c2g(n), ∀n ≥ n0}


                   Ramzi Shihadeh Alqrainy   qcs_2008@yahoo.com
Asymptotic Notations
35

           T/F
     -     T(n)= 3n2+4 is
     1.     T(n) Є O(n2)
     2.     T(n) Є O(n logn)
     3.     T(n) Є O(n5)
     4.     T(n) Є Ω(n2)
     5.     T(n) Є Ω(n5)
     6.     T(n) Є Ω(n logn)
     7.     T(n) Є Θ(n2)
     8.     T(n) Є Θ(n logn)
     9.     T(n) Є Θ(n5)
     10.    T(n) Є Θ(2n)

                     Ramzi Shihadeh Alqrainy   qcs_2008@yahoo.com
Divide and Conquer
36


         E.g : 1,1,2,3,5,8,13,…          ► The Fibonacci Series
     -   Incremental
     -   Recursive
     Set F1 1
     Set F2 1
     FOR i 3 To n Do
          Set F    F1 + F2
          Display F
          Set F1 F2                                   Incremental Approach
          Set F2    F
     End For




                     Ramzi Shihadeh Alqrainy   qcs_2008@yahoo.com
Divide and Conquer
37




     Function fib(n)
     {
         If (n 1 OR n          0)
             Return 1
        Else
          Call fib(n-1)+fib(n-2)
                                                          Recursive Approach
     }




                  Ramzi Shihadeh Alqrainy   qcs_2008@yahoo.com
Merge Sort
38




           Ramzi Shihadeh Alqrainy   qcs_2008@yahoo.com
Merge Sort
39

        Divide:
     Divide the problem into a number of subproblems that are themselves smaller
        instances of the same type of problem.
        Conquer:
     Recursively solving these subproblems. If the subproblems are small enough,
        solve them straightforward.
        Combine:
     Combine the solutions to the subproblems into the solution of original problem.




                    Ramzi Shihadeh Alqrainy   qcs_2008@yahoo.com
Merge Sort
40



       The running time of D-C algorithm can be
       expressed as :
            Θ(1) if n ≤ C
     T(n)
            a T( ) + D(n) + C(n)
     a : # of sub problem                  : size of each subproblem

     D(n) : Time to divide          C(n): Time to combine


                 Ramzi Shihadeh Alqrainy   qcs_2008@yahoo.com
Merge Sort
41


       The running time for the merge-sort algorithm is :

               Θ(1) n=1
     T(n)
               2 T( ) + Θ(n) n>1




               Ramzi Shihadeh Alqrainy   qcs_2008@yahoo.com
Merge Sort
42




           Ramzi Shihadeh Alqrainy   qcs_2008@yahoo.com
Master Method
43



          How to solve the recurrence equations ?
     There are many methods to solve the recurrence :


     1.    Master Method
     2.    Iteration Method
     3.    Recurrence Method
     4.    Substitution Method


                 Ramzi Shihadeh Alqrainy   qcs_2008@yahoo.com
Master Method
44

     T(n) = a T( )+f(n) where                              ≥1 , b >1 and
     d=       , then

                   Θ(nd)                      ,          if f(n) < nd
     T(n)         Θ(nd     )              ,                f(n) = nd
                   Θ(f(n))                        ,         f(n) > nd and
                                                              f( ) ≤ c f(n) where c<1


     Regularity condition

                    Ramzi Shihadeh Alqrainy           qcs_2008@yahoo.com
Master Method
45

         Find the complexity of the merge-sort algorithm
         using the master method
                            2 T( ) + Θ(n)
         =2,b=2,d=               =1
     -   Compare f(n) with nd
             n=n         f(n) = nd
     -    case #2 is satisfied
           T(n) is Θ (n      )


                Ramzi Shihadeh Alqrainy   qcs_2008@yahoo.com
Master Method
46


         T(n) = 9 T( ) + n

         =9,b=3,d=               =2
     -   Compare f(n) with nd
           n < n2         f(n) < nd
     -   case #1 is satisfied
          T(n) is Θ(n2)


                 Ramzi Shihadeh Alqrainy   qcs_2008@yahoo.com
Master Method
47


         T(n) = T (      ) +1

          =1,b= ,d=               =0
     -   Compare f(n) with nd
           1 = n0         f(n) = nd
     -   case #2 is satisfied
          T(n) is Θ (    )


                 Ramzi Shihadeh Alqrainy   qcs_2008@yahoo.com
Master Method
48
         T(n) = 2T( ) +
          =2 , b=5 , d =       = 0.43
     -   Compare f(n) with nd
          n0.5 > n 0.43
     -   Test 1 of case #3 is satisfied , we should check regularity
         condition
                                       2 f( ) ≤ c f(n)

                                      2       ≤c

                                              ≤ c               = 0.89
                                       T(n) is Θ(     )

                 Ramzi Shihadeh Alqrainy   qcs_2008@yahoo.com
Homework
49


      T(n) = 2 T( )+ n




             Ramzi Shihadeh Alqrainy   qcs_2008@yahoo.com
Thank you
50


      This course DS2 of lectures by Dr. Hassan Alserhan
      from BAU




              Ramzi Shihadeh Alqrainy   qcs_2008@yahoo.com

More Related Content

What's hot

Kolmogorov complexity and topological arguments
Kolmogorov complexity and topological argumentsKolmogorov complexity and topological arguments
Kolmogorov complexity and topological argumentsshenmontpellier
 
Time complexity (linear search vs binary search)
Time complexity (linear search vs binary search)Time complexity (linear search vs binary search)
Time complexity (linear search vs binary search)Kumar
 
Symbolic Execution as DPLL Modulo Theories
Symbolic Execution as DPLL Modulo TheoriesSymbolic Execution as DPLL Modulo Theories
Symbolic Execution as DPLL Modulo TheoriesQuoc-Sang Phan
 
18103010 algorithm complexity (iterative)
18103010 algorithm complexity (iterative)18103010 algorithm complexity (iterative)
18103010 algorithm complexity (iterative)AdityaKhandelwal58
 
how to calclute time complexity of algortihm
how to calclute time complexity of algortihmhow to calclute time complexity of algortihm
how to calclute time complexity of algortihmSajid Marwat
 
Lec 5 asymptotic notations and recurrences
Lec 5 asymptotic notations and recurrencesLec 5 asymptotic notations and recurrences
Lec 5 asymptotic notations and recurrencesAnkita Karia
 
Basics & asymptotic notations
Basics & asymptotic notationsBasics & asymptotic notations
Basics & asymptotic notationsRajendran
 
Lec03 04-time complexity
Lec03 04-time complexityLec03 04-time complexity
Lec03 04-time complexityAbbas Ali
 
Analysis of algorithn class 3
Analysis of algorithn class 3Analysis of algorithn class 3
Analysis of algorithn class 3Kumar
 
Asymptotic notations(Big O, Omega, Theta )
Asymptotic notations(Big O, Omega, Theta )Asymptotic notations(Big O, Omega, Theta )
Asymptotic notations(Big O, Omega, Theta )swapnac12
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notationsEhtisham Ali
 

What's hot (20)

Time andspacecomplexity
Time andspacecomplexityTime andspacecomplexity
Time andspacecomplexity
 
Asymptotic Notation
Asymptotic NotationAsymptotic Notation
Asymptotic Notation
 
Kolmogorov complexity and topological arguments
Kolmogorov complexity and topological argumentsKolmogorov complexity and topological arguments
Kolmogorov complexity and topological arguments
 
Time complexity (linear search vs binary search)
Time complexity (linear search vs binary search)Time complexity (linear search vs binary search)
Time complexity (linear search vs binary search)
 
Symbolic Execution as DPLL Modulo Theories
Symbolic Execution as DPLL Modulo TheoriesSymbolic Execution as DPLL Modulo Theories
Symbolic Execution as DPLL Modulo Theories
 
Time complexity
Time complexityTime complexity
Time complexity
 
Computational Complexity
Computational ComplexityComputational Complexity
Computational Complexity
 
18103010 algorithm complexity (iterative)
18103010 algorithm complexity (iterative)18103010 algorithm complexity (iterative)
18103010 algorithm complexity (iterative)
 
Complexity of Algorithm
Complexity of AlgorithmComplexity of Algorithm
Complexity of Algorithm
 
how to calclute time complexity of algortihm
how to calclute time complexity of algortihmhow to calclute time complexity of algortihm
how to calclute time complexity of algortihm
 
Lec7
Lec7Lec7
Lec7
 
Lec 5 asymptotic notations and recurrences
Lec 5 asymptotic notations and recurrencesLec 5 asymptotic notations and recurrences
Lec 5 asymptotic notations and recurrences
 
Basics & asymptotic notations
Basics & asymptotic notationsBasics & asymptotic notations
Basics & asymptotic notations
 
Lec03 04-time complexity
Lec03 04-time complexityLec03 04-time complexity
Lec03 04-time complexity
 
Analysis of algorithn class 3
Analysis of algorithn class 3Analysis of algorithn class 3
Analysis of algorithn class 3
 
Analysis of Algorithm
Analysis of AlgorithmAnalysis of Algorithm
Analysis of Algorithm
 
Asymptotic notations(Big O, Omega, Theta )
Asymptotic notations(Big O, Omega, Theta )Asymptotic notations(Big O, Omega, Theta )
Asymptotic notations(Big O, Omega, Theta )
 
Kumegawa russia
Kumegawa russiaKumegawa russia
Kumegawa russia
 
Asymptotic notation
Asymptotic notationAsymptotic notation
Asymptotic notation
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notations
 

Viewers also liked

Purely Functional Data Structures ex3.3 leftist heap
Purely Functional Data Structures ex3.3 leftist heapPurely Functional Data Structures ex3.3 leftist heap
Purely Functional Data Structures ex3.3 leftist heapTetsuro Nagae
 
Advance data structure & algorithm
Advance data structure & algorithmAdvance data structure & algorithm
Advance data structure & algorithmK Hari Shankar
 
BTrees - Great alternative to Red Black, AVL and other BSTs
BTrees - Great alternative to Red Black, AVL and other BSTsBTrees - Great alternative to Red Black, AVL and other BSTs
BTrees - Great alternative to Red Black, AVL and other BSTsAmrinder Arora
 
Binomial Heaps and Fibonacci Heaps
Binomial Heaps and Fibonacci HeapsBinomial Heaps and Fibonacci Heaps
Binomial Heaps and Fibonacci HeapsAmrinder Arora
 
Binomial heap presentation
Binomial heap presentationBinomial heap presentation
Binomial heap presentationHafsa.Naseem
 

Viewers also liked (7)

Purely Functional Data Structures ex3.3 leftist heap
Purely Functional Data Structures ex3.3 leftist heapPurely Functional Data Structures ex3.3 leftist heap
Purely Functional Data Structures ex3.3 leftist heap
 
Advance data structure & algorithm
Advance data structure & algorithmAdvance data structure & algorithm
Advance data structure & algorithm
 
BTrees - Great alternative to Red Black, AVL and other BSTs
BTrees - Great alternative to Red Black, AVL and other BSTsBTrees - Great alternative to Red Black, AVL and other BSTs
BTrees - Great alternative to Red Black, AVL and other BSTs
 
Leftist heap
Leftist heapLeftist heap
Leftist heap
 
Binomial Heaps and Fibonacci Heaps
Binomial Heaps and Fibonacci HeapsBinomial Heaps and Fibonacci Heaps
Binomial Heaps and Fibonacci Heaps
 
Fibonacci Heap
Fibonacci HeapFibonacci Heap
Fibonacci Heap
 
Binomial heap presentation
Binomial heap presentationBinomial heap presentation
Binomial heap presentation
 

Similar to Advance Data Structure

Similar to Advance Data Structure (20)

Lec7.ppt
Lec7.pptLec7.ppt
Lec7.ppt
 
Lec7.ppt
Lec7.pptLec7.ppt
Lec7.ppt
 
analysis of algorithms
analysis of algorithmsanalysis of algorithms
analysis of algorithms
 
Analysis of Algorithum
Analysis of AlgorithumAnalysis of Algorithum
Analysis of Algorithum
 
Big oh Representation Used in Time complexities
Big oh Representation Used in Time complexitiesBig oh Representation Used in Time complexities
Big oh Representation Used in Time complexities
 
Dynamic Programming - Part 1
Dynamic Programming - Part 1Dynamic Programming - Part 1
Dynamic Programming - Part 1
 
3 analysis.gtm
3 analysis.gtm3 analysis.gtm
3 analysis.gtm
 
Asymptotic Notation
Asymptotic NotationAsymptotic Notation
Asymptotic Notation
 
Computational complexity
Computational complexityComputational complexity
Computational complexity
 
Tech Talks @NSU: DLang: возможности языка и его применение
Tech Talks @NSU: DLang: возможности языка и его применениеTech Talks @NSU: DLang: возможности языка и его применение
Tech Talks @NSU: DLang: возможности языка и его применение
 
Algorithms - Rocksolid Tour 2013
Algorithms  - Rocksolid Tour 2013Algorithms  - Rocksolid Tour 2013
Algorithms - Rocksolid Tour 2013
 
Data Structure: Algorithm and analysis
Data Structure: Algorithm and analysisData Structure: Algorithm and analysis
Data Structure: Algorithm and analysis
 
Lec1
Lec1Lec1
Lec1
 
Algo complexity
Algo complexityAlgo complexity
Algo complexity
 
Cis435 week01
Cis435 week01Cis435 week01
Cis435 week01
 
Class 18: Measuring Cost
Class 18: Measuring CostClass 18: Measuring Cost
Class 18: Measuring Cost
 
Lec1
Lec1Lec1
Lec1
 
Complexity Analysis
Complexity Analysis Complexity Analysis
Complexity Analysis
 
AsymptoticAnalysis.ppt
AsymptoticAnalysis.pptAsymptoticAnalysis.ppt
AsymptoticAnalysis.ppt
 
algorithmanalysis and effciency.pptx
algorithmanalysis and effciency.pptxalgorithmanalysis and effciency.pptx
algorithmanalysis and effciency.pptx
 

More from Ramzi Alqrainy

Non English Search as a Machine Learning Problem
Non English Search as a Machine Learning Problem Non English Search as a Machine Learning Problem
Non English Search as a Machine Learning Problem Ramzi Alqrainy
 
OpenSooq Image Recognition on AWS - AWS ML Lab
OpenSooq Image Recognition on AWS - AWS ML LabOpenSooq Image Recognition on AWS - AWS ML Lab
OpenSooq Image Recognition on AWS - AWS ML LabRamzi Alqrainy
 
A Few Milliseconds in the Life of an HTTP Request - AWS Summit 2019
A Few Milliseconds in the Life of an HTTP Request - AWS Summit 2019A Few Milliseconds in the Life of an HTTP Request - AWS Summit 2019
A Few Milliseconds in the Life of an HTTP Request - AWS Summit 2019Ramzi Alqrainy
 
Mastering Chaos - OpenSooq’s journey from Monolithic to Microservices
Mastering Chaos - OpenSooq’s journey from Monolithic to Microservices Mastering Chaos - OpenSooq’s journey from Monolithic to Microservices
Mastering Chaos - OpenSooq’s journey from Monolithic to Microservices Ramzi Alqrainy
 
Infrastructure OpenSooq Mobile @ Scale
Infrastructure OpenSooq Mobile @ ScaleInfrastructure OpenSooq Mobile @ Scale
Infrastructure OpenSooq Mobile @ ScaleRamzi Alqrainy
 
Choosing the Right Technologies for OpenSooq
Choosing the Right Technologies for OpenSooqChoosing the Right Technologies for OpenSooq
Choosing the Right Technologies for OpenSooqRamzi Alqrainy
 
Retrieving Information From Solr
Retrieving Information From SolrRetrieving Information From Solr
Retrieving Information From SolrRamzi Alqrainy
 
Arabic Content with Apache Solr
Arabic Content with Apache SolrArabic Content with Apache Solr
Arabic Content with Apache SolrRamzi Alqrainy
 
Recommender Systems, Part 1 - Introduction to approaches and algorithms
Recommender Systems, Part 1 - Introduction to approaches and algorithmsRecommender Systems, Part 1 - Introduction to approaches and algorithms
Recommender Systems, Part 1 - Introduction to approaches and algorithmsRamzi Alqrainy
 
Apache Solr 4 Part 1 - Introduction, Features, Recency Ranking and Popularity...
Apache Solr 4 Part 1 - Introduction, Features, Recency Ranking and Popularity...Apache Solr 4 Part 1 - Introduction, Features, Recency Ranking and Popularity...
Apache Solr 4 Part 1 - Introduction, Features, Recency Ranking and Popularity...Ramzi Alqrainy
 
Evaluating Search Engines
Evaluating Search EnginesEvaluating Search Engines
Evaluating Search EnginesRamzi Alqrainy
 
Starting From Zero - Winning Strategies for Zero Results Page
Starting From Zero - Winning Strategies for Zero Results PageStarting From Zero - Winning Strategies for Zero Results Page
Starting From Zero - Winning Strategies for Zero Results PageRamzi Alqrainy
 
Search Behavior Patterns
Search Behavior PatternsSearch Behavior Patterns
Search Behavior PatternsRamzi Alqrainy
 
Intel microprocessor history
Intel microprocessor historyIntel microprocessor history
Intel microprocessor historyRamzi Alqrainy
 
How to prevent the cache problem in AJAX
How to prevent the cache problem in AJAXHow to prevent the cache problem in AJAX
How to prevent the cache problem in AJAXRamzi Alqrainy
 
Linked stacks and queues
Linked stacks and queuesLinked stacks and queues
Linked stacks and queuesRamzi Alqrainy
 

More from Ramzi Alqrainy (20)

Non English Search as a Machine Learning Problem
Non English Search as a Machine Learning Problem Non English Search as a Machine Learning Problem
Non English Search as a Machine Learning Problem
 
OpenSooq Image Recognition on AWS - AWS ML Lab
OpenSooq Image Recognition on AWS - AWS ML LabOpenSooq Image Recognition on AWS - AWS ML Lab
OpenSooq Image Recognition on AWS - AWS ML Lab
 
A Few Milliseconds in the Life of an HTTP Request - AWS Summit 2019
A Few Milliseconds in the Life of an HTTP Request - AWS Summit 2019A Few Milliseconds in the Life of an HTTP Request - AWS Summit 2019
A Few Milliseconds in the Life of an HTTP Request - AWS Summit 2019
 
Mastering Chaos - OpenSooq’s journey from Monolithic to Microservices
Mastering Chaos - OpenSooq’s journey from Monolithic to Microservices Mastering Chaos - OpenSooq’s journey from Monolithic to Microservices
Mastering Chaos - OpenSooq’s journey from Monolithic to Microservices
 
Infrastructure OpenSooq Mobile @ Scale
Infrastructure OpenSooq Mobile @ ScaleInfrastructure OpenSooq Mobile @ Scale
Infrastructure OpenSooq Mobile @ Scale
 
Choosing the Right Technologies for OpenSooq
Choosing the Right Technologies for OpenSooqChoosing the Right Technologies for OpenSooq
Choosing the Right Technologies for OpenSooq
 
Retrieving Information From Solr
Retrieving Information From SolrRetrieving Information From Solr
Retrieving Information From Solr
 
MemSQL
MemSQLMemSQL
MemSQL
 
Arabic Content with Apache Solr
Arabic Content with Apache SolrArabic Content with Apache Solr
Arabic Content with Apache Solr
 
Recommender Systems, Part 1 - Introduction to approaches and algorithms
Recommender Systems, Part 1 - Introduction to approaches and algorithmsRecommender Systems, Part 1 - Introduction to approaches and algorithms
Recommender Systems, Part 1 - Introduction to approaches and algorithms
 
Apache Solr 4 Part 1 - Introduction, Features, Recency Ranking and Popularity...
Apache Solr 4 Part 1 - Introduction, Features, Recency Ranking and Popularity...Apache Solr 4 Part 1 - Introduction, Features, Recency Ranking and Popularity...
Apache Solr 4 Part 1 - Introduction, Features, Recency Ranking and Popularity...
 
Evaluating Search Engines
Evaluating Search EnginesEvaluating Search Engines
Evaluating Search Engines
 
Starting From Zero - Winning Strategies for Zero Results Page
Starting From Zero - Winning Strategies for Zero Results PageStarting From Zero - Winning Strategies for Zero Results Page
Starting From Zero - Winning Strategies for Zero Results Page
 
Search Behavior Patterns
Search Behavior PatternsSearch Behavior Patterns
Search Behavior Patterns
 
Intel microprocessor history
Intel microprocessor historyIntel microprocessor history
Intel microprocessor history
 
How to prevent the cache problem in AJAX
How to prevent the cache problem in AJAXHow to prevent the cache problem in AJAX
How to prevent the cache problem in AJAX
 
Linked stacks and queues
Linked stacks and queuesLinked stacks and queues
Linked stacks and queues
 
Hashing
HashingHashing
Hashing
 
Markov Matrix
Markov MatrixMarkov Matrix
Markov Matrix
 
STACK
STACKSTACK
STACK
 

Recently uploaded

TEACHER REFLECTION FORM (NEW SET........).docx
TEACHER REFLECTION FORM (NEW SET........).docxTEACHER REFLECTION FORM (NEW SET........).docx
TEACHER REFLECTION FORM (NEW SET........).docxruthvilladarez
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfVanessa Camilleri
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Projectjordimapav
 
Dust Of Snow By Robert Frost Class-X English CBSE
Dust Of Snow By Robert Frost Class-X English CBSEDust Of Snow By Robert Frost Class-X English CBSE
Dust Of Snow By Robert Frost Class-X English CBSEaurabinda banchhor
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxlancelewisportillo
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptxiammrhaywood
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfErwinPantujan2
 
Measures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataMeasures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataBabyAnnMotar
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4MiaBumagat1
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parentsnavabharathschool99
 
Oppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmOppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmStan Meyer
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 

Recently uploaded (20)

TEACHER REFLECTION FORM (NEW SET........).docx
TEACHER REFLECTION FORM (NEW SET........).docxTEACHER REFLECTION FORM (NEW SET........).docx
TEACHER REFLECTION FORM (NEW SET........).docx
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdf
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Project
 
Dust Of Snow By Robert Frost Class-X English CBSE
Dust Of Snow By Robert Frost Class-X English CBSEDust Of Snow By Robert Frost Class-X English CBSE
Dust Of Snow By Robert Frost Class-X English CBSE
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
 
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
 
Measures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataMeasures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped data
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parents
 
Oppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmOppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and Film
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 

Advance Data Structure

  • 1. Ramzi Shihadeh Alqrainy qcs_2008@yahoo.com 1 Data Structure Advanced
  • 2. Algorithm 2 Well-defined sequence of computational steps. The word Algorithm refers to a precise method useable by a computer for solution of a problem. The Algorithm accepts a set of values as input and produces a set of values as output. Ramzi Shihadeh Alqrainy qcs_2008@yahoo.com
  • 3. Examples 3 Algorithm written in Human language Input a,b,c Compute the sum of a,b,c Divide the Sum by 3 Display the Result Ramzi Shihadeh Alqrainy qcs_2008@yahoo.com
  • 4. Example 4 Algorithm written in Programming language. Ramzi Shihadeh Alqrainy qcs_2008@yahoo.com
  • 5. Example 5 Algorithm written in Pseudocode Syntax. Step 1: Promt a,b,c Step 2: Let Sum a+b+c Step 3: Let Avg Sum / 3 Step 4: Output Avg Ramzi Shihadeh Alqrainy qcs_2008@yahoo.com
  • 6. ALGORITHM 6 What do we mean by input ? The word input interdependent on the type of problem. - Set of values or elements e.g in the array problem - A Set of bits e.g in the string problem - A Set of objects e.g in the graph problems the input may be the set of vertices and edges. Ramzi Shihadeh Alqrainy qcs_2008@yahoo.com
  • 7. The Properties of Algorithm 7 It must be correct It must be composed of a series of concrete steps. There can be no ambiguity as to which step will be performed next. It must be terminate. Ramzi Shihadeh Alqrainy qcs_2008@yahoo.com
  • 8. Analysis of Algorithm 8 What is a good algorithm ? - Is a clear algorithm ?? - Has a minimum steps ?? - Decreases the CPU time - Has a minimum memory (RAM) space ? - It is a good mapping for the problem. - Uses minimum spaces of disk I/O ** In this course the best Algorithm is the one that uses the minimum CPU resources Ramzi Shihadeh Alqrainy qcs_2008@yahoo.com
  • 9. Study of Algorithm 9 Design Analysis Methods and technique which Mathematical comparison of yield a good and useful algorithm ( without Algorithm to solve a problem. actually implement ) Ramzi Shihadeh Alqrainy qcs_2008@yahoo.com
  • 10. Algorithms 10 Types , methods , and techniques of ALG : - Incremental Algorithms - Simple Recursive Algorithms - Divide and Conquer Algorithms - Greedy Algorithms - Dynamic Algorithms - Backtracking Algorithms - Branch and Bound Algorithms - Brute and Force Algorithms - Randomized Algorithms Ramzi Shihadeh Alqrainy qcs_2008@yahoo.com
  • 11. Analysis of Algorithm 11 The analysis of an algorithm focuses on the time and space complexity. The Time Complexity is the amount of time required by an algorithm to run to completion. The Time Complexity is a function of input size “n” T(n) The Time Complexity refers to the amount of time required by an ALG to run completion Ramzi Shihadeh Alqrainy qcs_2008@yahoo.com
  • 12. Time Complexity 12 Ramzi Shihadeh Alqrainy qcs_2008@yahoo.com
  • 13. Time Complexity 13 Different times can arise for the same algorithm - Worse-Case time complexity is a function defined by the maximum amount of time needed by an algorithm for an input size “n”. - Best-Case time complexity is a function defined by the minimum amount of time needed by an algorithm for an input size “n”. - Avarage-Case time complexity is the execution of an algorithm having typical input of size “n”. Ramzi Shihadeh Alqrainy qcs_2008@yahoo.com
  • 14. Notation for writting algorithm 14 In our DS(2) course we will express all the algorithm by Pseudocodes representation. This means that the algorithms are language and machine independent. The Psuedocode hides the implementation details and focuses on the computational aspects of an algorithm There are no grammar rules in the Pseudocode, it consists of keywords and English. Like phrase which specify the flow of control. Pseudocode is not case sensitive. Ramzi Shihadeh Alqrainy qcs_2008@yahoo.com
  • 15. Time Complexity 15 Each instruction takes only one unit of time. a=a+1 one unit time a=x^2+3*x-4 one unit time Algorithm ABC(x,y) cost time SET Sum x+y c1 1 SET Mul x*y c2 1 Total Cost = c1 *1 + c2 * 1 = c1 + c2 T(n) = Cn0 The complexity of this algorithm is Θ(n0) Θ(1) Ramzi Shihadeh Alqrainy qcs_2008@yahoo.com
  • 16. Time Complexity 16 Find the running for the following codes : cost time Sum 0 c1 1 Sum Sum + next c2 1 T(n)= c1 + c2 = c3 ------------------------------------------------------------------------------------- cost time S 0 c1 1 For i 2 to n Do c2 n S s+I c3 n-1 End For S S/3 c4 1 T(n)= c1 + n c2 + (n-1) c3 + c4 T(n) is Θ(n) Ramzi Shihadeh Alqrainy qcs_2008@yahoo.com
  • 17. Time Complexity 17 cost time For i 1 to n Do c1 n+1 sum sum + 1 c2 n End For T(n) = (n+1) c1 + n c2 = (c1+ c2)n + c2 = c3n + c1 T(n) is Θ(n) OR T(n) = =n Ramzi Shihadeh Alqrainy qcs_2008@yahoo.com
  • 18. Time Complexity 18 cost time For i 1 To n Do c1 n+1 For j 1 To m Do c2 n(m+1) anything() c3 mn End For End For T(n)= (n+1) c1 + (n)(m+1) c2 + (n)(m)c3 = n c1 + c1 + mn c2 + n c2 + mn c3 T(n) is Θ(mn) OR = = nm Ramzi Shihadeh Alqrainy qcs_2008@yahoo.com
  • 19. Time Complexity 19 For j 1 To n Do For k 1 To j Do anything() End For End For T(n)= = = T(n) is Θ(n2) Ramzi Shihadeh Alqrainy qcs_2008@yahoo.com
  • 20. Time Complexity 20 For i 1 To n Do For j i To n Do anything() End For End For T(n)= = = - + = n2 - +n Ramzi Shihadeh Alqrainy qcs_2008@yahoo.com
  • 21. Time Complexity 21 Suppose that the function Bar(n) requires Θ(n2) what is the complexity at the following algorithm ? Algorithm Foo(n) cost time Set x 0 For i 1 To n Do Set x x + Bar(i) End For T(n) = Ramzi Shihadeh Alqrainy qcs_2008@yahoo.com
  • 22. Time Complexity 22 Algorithm Foo(n) Set s 0 + = Repeat Until n=1 Do s s+1 (n-1) + (n-1) = n n-1 2n-2 End Repeat For i 1 To s Do T(n) is Θ(n) anything() End For Ramzi Shihadeh Alqrainy qcs_2008@yahoo.com
  • 23. Time Complexity 23 cost time Algorithm Foo(n) Set s 0 Repeat Until n=1 Do s s+1 n n/2 End Repeat For i 1 To s Do anything() End For Ramzi Shihadeh Alqrainy qcs_2008@yahoo.com
  • 24. Insertion Sort 24 Sort Array A using the Insertion-Sort algorithm Ramzi Shihadeh Alqrainy qcs_2008@yahoo.com
  • 25. Insertion Sort 25 Ramzi Shihadeh Alqrainy qcs_2008@yahoo.com
  • 26. Insertion Sort 26 INSERTION-SORT (A, n) ⊳A[1 . . n] cost time For j ←2 to n do c1 n Set key ←A[ j] c2 n-1 Set i ←j –1 c3 n-1 Repeat While (i > 0 and A[i] > key) do c4 tj Set A[i+1] ←A[i] c5 tj-1 Set i ←i –1 c6 tj-1 End Repeat A[i+1] = key c7 n-1 End For Ramzi Shihadeh Alqrainy qcs_2008@yahoo.com
  • 27. Insertion Sort 27 The running time of I-S algorithm is ∑(cost of step) * (number of times step is executed) So T(n)= n c1 + (n-1) c2 + (n-1)c3 + tj c4 + tj-1 c5 + tj-1 c6 + (n-1) c7 * To solve the above T(n) , we should know firstly the value of tj , and this is vary depends on the input. Ramzi Shihadeh Alqrainy qcs_2008@yahoo.com
  • 28. Insertion Sort 28 The Best-Case The array is already sorted tj = 1 = (n-1) T(n) = nc1 + nc2 – c2 + nc3 – c3 + nc4 – c4 + nc7 – c7 = (c1 + c2 + c3 + c4 + c7 )n + (-c2-c3-c4- c7) = An+B T(n) is Θ(n) Ramzi Shihadeh Alqrainy qcs_2008@yahoo.com
  • 29. Insertion Sort 29 The Worse-Case The array is in reverse stored order tj = j T(n)= n c1 + (n-1) c2 + (n-1)c3 + c4 + c5 + c6 + (n-1) c7 = -1 = T(n) is Θ(n2) Ramzi Shihadeh Alqrainy qcs_2008@yahoo.com
  • 30. Homework 30 Consider sorting n-numbers sorted in array A by first finding the smallest element of A and putting it in the first entry of another array B , then finding the second element of A and putting it in the second entry of B . Then continue in this manner for the n elements of B. 1. Write Pseudocode for this algorithm. 2. Compute the best and worse-case running time 3. Express these two running time using Θ-notation. Ramzi Shihadeh Alqrainy qcs_2008@yahoo.com
  • 31. Asymptotic Notations 31 These notations are used to express the complexity of a given algorithm, or used to compute two algorithms or more that are designed to solve the same problem. 1) Big-Oh Ο() 2) Big-Omega Ω() 3) Bog-Theta Θ() 4) Little-Oh o () 5) Little-Omega ω() Ramzi Shihadeh Alqrainy qcs_2008@yahoo.com
  • 32. Asymptotic Notations 32 Big-Oh - Upper-Bound - Express the worse-case Ο(g(n))={T(n):∃n0,c are positives, such that T(n) ≤ cg(n), ∀ 0} ∃ ∀n≥n T(n)=2n+1 Є O(n) T(n)=10n Є O(n) T(n)=0.2n-7 Є O(n) Ramzi Shihadeh Alqrainy qcs_2008@yahoo.com
  • 33. Asymptotic Notations 33 Prove that T(n)= 2n+1 is O(n) T(n) ≤ cg(n) 2n+1 ≤ cn 2n+1 ≤ 2n+n ∀ n ≥1 2n+1 ≤ 3n where c=3,n0=1 Ramzi Shihadeh Alqrainy qcs_2008@yahoo.com
  • 34. Asymptotic Notations 34 Big-Omega - Lower-Bound - Express the best-case Ω(g(n))={T(n):∃n0,c are positives, such that T(n) ≥ cg(n), ∀n ≥ n0} ∃ ∀ Big-Theta - Tight Bound - Express the best-case , worst-case, Avg-case Θ(g(n))={T(n):∃n0,c1,c2 are positives, such that c1g(n) ≥ T(n) ≥ c2g(n), ∀n ≥ n0} Ramzi Shihadeh Alqrainy qcs_2008@yahoo.com
  • 35. Asymptotic Notations 35 T/F - T(n)= 3n2+4 is 1. T(n) Є O(n2) 2. T(n) Є O(n logn) 3. T(n) Є O(n5) 4. T(n) Є Ω(n2) 5. T(n) Є Ω(n5) 6. T(n) Є Ω(n logn) 7. T(n) Є Θ(n2) 8. T(n) Є Θ(n logn) 9. T(n) Є Θ(n5) 10. T(n) Є Θ(2n) Ramzi Shihadeh Alqrainy qcs_2008@yahoo.com
  • 36. Divide and Conquer 36 E.g : 1,1,2,3,5,8,13,… ► The Fibonacci Series - Incremental - Recursive Set F1 1 Set F2 1 FOR i 3 To n Do Set F F1 + F2 Display F Set F1 F2 Incremental Approach Set F2 F End For Ramzi Shihadeh Alqrainy qcs_2008@yahoo.com
  • 37. Divide and Conquer 37 Function fib(n) { If (n 1 OR n 0) Return 1 Else Call fib(n-1)+fib(n-2) Recursive Approach } Ramzi Shihadeh Alqrainy qcs_2008@yahoo.com
  • 38. Merge Sort 38 Ramzi Shihadeh Alqrainy qcs_2008@yahoo.com
  • 39. Merge Sort 39 Divide: Divide the problem into a number of subproblems that are themselves smaller instances of the same type of problem. Conquer: Recursively solving these subproblems. If the subproblems are small enough, solve them straightforward. Combine: Combine the solutions to the subproblems into the solution of original problem. Ramzi Shihadeh Alqrainy qcs_2008@yahoo.com
  • 40. Merge Sort 40 The running time of D-C algorithm can be expressed as : Θ(1) if n ≤ C T(n) a T( ) + D(n) + C(n) a : # of sub problem : size of each subproblem D(n) : Time to divide C(n): Time to combine Ramzi Shihadeh Alqrainy qcs_2008@yahoo.com
  • 41. Merge Sort 41 The running time for the merge-sort algorithm is : Θ(1) n=1 T(n) 2 T( ) + Θ(n) n>1 Ramzi Shihadeh Alqrainy qcs_2008@yahoo.com
  • 42. Merge Sort 42 Ramzi Shihadeh Alqrainy qcs_2008@yahoo.com
  • 43. Master Method 43 How to solve the recurrence equations ? There are many methods to solve the recurrence : 1. Master Method 2. Iteration Method 3. Recurrence Method 4. Substitution Method Ramzi Shihadeh Alqrainy qcs_2008@yahoo.com
  • 44. Master Method 44 T(n) = a T( )+f(n) where ≥1 , b >1 and d= , then Θ(nd) , if f(n) < nd T(n) Θ(nd ) , f(n) = nd Θ(f(n)) , f(n) > nd and f( ) ≤ c f(n) where c<1 Regularity condition Ramzi Shihadeh Alqrainy qcs_2008@yahoo.com
  • 45. Master Method 45 Find the complexity of the merge-sort algorithm using the master method 2 T( ) + Θ(n) =2,b=2,d= =1 - Compare f(n) with nd n=n f(n) = nd - case #2 is satisfied T(n) is Θ (n ) Ramzi Shihadeh Alqrainy qcs_2008@yahoo.com
  • 46. Master Method 46 T(n) = 9 T( ) + n =9,b=3,d= =2 - Compare f(n) with nd n < n2 f(n) < nd - case #1 is satisfied T(n) is Θ(n2) Ramzi Shihadeh Alqrainy qcs_2008@yahoo.com
  • 47. Master Method 47 T(n) = T ( ) +1 =1,b= ,d= =0 - Compare f(n) with nd 1 = n0 f(n) = nd - case #2 is satisfied T(n) is Θ ( ) Ramzi Shihadeh Alqrainy qcs_2008@yahoo.com
  • 48. Master Method 48 T(n) = 2T( ) + =2 , b=5 , d = = 0.43 - Compare f(n) with nd n0.5 > n 0.43 - Test 1 of case #3 is satisfied , we should check regularity condition 2 f( ) ≤ c f(n) 2 ≤c ≤ c = 0.89 T(n) is Θ( ) Ramzi Shihadeh Alqrainy qcs_2008@yahoo.com
  • 49. Homework 49 T(n) = 2 T( )+ n Ramzi Shihadeh Alqrainy qcs_2008@yahoo.com
  • 50. Thank you 50 This course DS2 of lectures by Dr. Hassan Alserhan from BAU Ramzi Shihadeh Alqrainy qcs_2008@yahoo.com