SlideShare a Scribd company logo
1 of 27
CS 213

Data Structures and Algorithms
Second Semester, 2011-2012
Sum of Squares
int sumOfSquares(int n){
   int sum = 0;
   for(int i=1; i<=n; i++)
        sum = sum + i*i;
   return sum;
}
 T(n) = 5n + 4
Selection Sort
Given the following numbers in an array:
           53 23 10 34 2 17
            2 23 10 34 53 17
            2 10 23 34 53 17
            2 10 17 34 53 23
            2 10 17 23 53 34
            2 10 17 23 34 53
Still on Selection Sort
void selectionSort(int A[], int n){
   for(int i=0; i<n; i++){
      int min = i;
      for(int j=i+1; j<n; j++){
             if(A[j] < A[min])
                min = j;
                                      What is this function’s T(n)? What
         }                            we need is to compute for T(n)
         int temp = A[i];             from the inner loop going
         A[i] = A[min];               outwards.
         A[min] = temp;
                                      We need to use the summation
   }                                  notation to solve the T(n).
}
The Summation Notation



    = x1 + x2 + x3 + x4
Still on the summation notation

4
                                     n
∑     i      = 1+2+3+4   = 10
                                    ∑i =    n ( n +1)
                                                2
                                    i =1
i=1
 5
                                     n
∑     3 = 3+3+3+3+3      = 15
                                    ∑c     =nc =15
i=1                                 i=1
Exercise
 for(int i=1; i<=5; i++) cout<<endl;
 for(int i=1; i<=n; i++) cout<<endl;
 for(int i=3; i<=m; i++) cout<<endl;
 for(int i=1; i<=6; i++)
       for(int j=1; j<=8; j++) cout<<endl;
 for(int i=1; i<=n; i++)
       for(int j=1; j<=n; j++) cout<<endl;
 for(int i=1; i<=m; i++)
       for(int j=1; j<=m; j++) cout<<endl;
 for(int i=1; i<=n; i++)
       for(int j=i; j<=n; j++) cout<<endl;
Going back to the Selection Sort
void selectionSort(int A[], int n){
   for(int i=0; i<n; i++){            n     n         n
      int min = i;
      for(int j=i+1; j<n; j++){       ∑ ∑ c =∑                  c(n-i+1-1)
             if(A[j] < A[min])        i=1 j=i+1 i=1
                min = j;
          }                           n              n
          int temp = A[i];
          A[i] = A[min];              ∑   c(n-i) = c   n-i∑
          A[min] = temp;              i=1                 i=1
   }
                                       n        n               n
}
                                      ∑ n-i = ∑ n - ∑ i
                                      i=1       i=1           i=1
Time Complexity
A function that maps problem size into the
 time required to solve the problem.
Typically, we are interested in the inherent
 complexity of computing the solution to
 problems in a particular class.
Lower Bound
 We might want to know how fast we can sort a list of n
  items, initially in an arbitrary order, regardless of the
  algorithm used.
 What is sought here is the lower bound, L(n), on sorting,
  a property of the sorting problem and not of any
  particular algorithm.
 This says that no algorithm can do the job in fewer than
  L(n) time units for arbitrary inputs.
Upper Bound
 We might also like to know how long it would
  take to sort such list using a known algorithm
  with a worst-case input.
 What is sought here is the upper bound, U(n),
  which says that for arbitrary inputs we can
  always sort in time at most U(n).
Goal of Time Complexity Analysis

 While there are apparently two complexity functions for
  problems, L(n) and U(n), the ultimate goal is to make
  these two bounds coincide.
 This is the optimal algorithm which has L(n) = U(n).
 For some of the problems, this goal has not been
  realized yet!
Invitation
Consider this, CS 213, as you journey into
 finding optimal solutions to classes of
 problems!
Who knows, you might win a million
 dollars ($$$) from Claymath Foundation!
Upper Bound Complexity
There are two ways in analyzing this
 bound:
  Counting instructions
  Solving recurrences
Both are used to find the worst case of an
 algorithm.
Big O-Notation (O(g(n)))
The O-notation is used to describe the
 worst-case running time of an algorithm.
O(n) means that the growth of the running
 time of the algorithm is a function of n.
O-notation computes for the upper bound.
O-Notation Defined
O(g(n)) = {f(n): ∃ c>0, n0>0 s.t. 0 ≤ f(n) ≤
 cg(n) for all n ≥ n0}.
Example: Check if (n2/2) – 3n ∈ O(n2)
  (n2/2) – 3n ≤ cn2
  ½ - 3/n ≤ c
  Choosing c = ½, n0 = 6 proves the claim.
Another Example
3n2 - 100n + 6 ∈ O(n2) ?
  3n2 - 100n + 6 ≤ cn2
  3 – 100/n + 6/n2 ≤ c
  At this point, we have to choose a c>0 and an
   n0
  What values will prove our claim?
Lower Bound Complexity
 This is the more difficult of the bounds.
 There is no algorithm to analyze.
 Ω(g(n)) is used to describe the lower bound of
  the running time of the algorithm or minimum
  possible running time of the algorithm.
Ω-Notation
Ω(g(n)) = {f(n): ∃ c>0, n0>0 s.t. 0 ≤ cg(n) ≤
 f(n) for all n ≥ n0}.
Example: Check if (n2/2) – 3n ∈ Ω(n2)
  cn2 ≤ (n2/2) – 3n
  c ≤ ½ - 3/n
  Choosing c = 1/14, n0 = 7 proves the claim.
Another Example
 Check if 3n2 - 100n + 6 ∈ Ω(n)
    cn ≤ 3n2 - 100n + 6
    c ≤ 3n – 100 + 6/n
 At this point we need to find a c and an n0 that will prove
  our claim.
 What values of c and n0 will suffice the inequality??
θ-Notation
Used to denote that the lower and upper
 bounds of the running time of the
 algorithm is tight, i.e. the growth rate of the
 upper and lower bounds are the same.
θ-Notation Defined
θ(g(n)) = {f(n): ∃ c1>0, c2>0, n0>0 s.t. 0 ≤
 c1g(n) ≤ f(n) ≤ c2g(n) for all n ≥ n0}.
f(n) ∈ θ(g(n)) if f(n) ∈ O(g(n)) and f(n) ∈
 Ω(g(n))
Complexity Classes
Description   O-notation

constant      O(1)

logarithmic   O(log n)

linear        O(n)

n log n       O(n log n)

quadratic     O(n2)

cubic         O(n3)

polynomial    O(nk), k≥1

exponential   O(an), a>1
Growth rate of complexity classes

    class       n=2       n=16        n=256       n=1024
            1         1           1           1              1
log n                 1           4           8            10
n                     2          16        256         1024
n log n               2          64       2048        10240
n^2                   4      256         65536      1048576


n^3                   8     4096      16777216     1.07E+09


2^n                   4    65536      1.16E+77    1.8E+308
Graph of the Growth Rates
      50                                                     n^2
      45
      40
                                                                                           n log n
      35
                                         2^n
      30
                        n^3
      25
      20
      15
      10                                                                                       n

      5
                                                                                               log n
      0
           1   2    3         4     5           6       7          8      9       10      11

log n      0   1   1.58       2    2.32        2.58    2.81        3     3.17    3.32    3.46
n          1   2    3         4      5           6       7         8       9      10      11
n log n    0   2   4.75       8    11.61       15.51   19.65       24    28.53   33.22   38.05
n^2        1   4    9         16    25          36      49         64     81     100     121
n^3        1   8   27         64   125         216     343         512   729     1000    1331
2^n        2   4    8         16    32          64     128         256   512     1024    2048
Bigger N
 2000
 1800
 1600
 1400
                                                                                      n^3
 1200
 1000
    800
    600
                                                                  2^n
    400
    200
                                                                                    n^2
      0                                                                           n log n
                                                                                    n n
                                                                                     log
          1   2    3     4     5       6       7      8      9           10      11

log n     0   1   1.58   2    2.32    2.58    2.81    3     3.17        3.32    3.46
n         1   2    3     4      5       6       7     8       9          10      11
n log n   0   2   4.75   8    11.61   15.51   19.65   24    28.53       33.22   38.05
n^2       1   4    9     16    25      36      49     64     81         100     121
n^3       1   8   27     64   125     216     343     512   729         1000    1331
2^n       2   4    8     16    32      64     128     256   512         1024    2048
Still on the Graph
      2000
      1800
      1600
      1400
                                                                                               n^3
      1200                                                                                    10*n^2
      1000
       800                                                                                 20*n log n
       600                                                                                       50*n
                                                                            2^n
       400                                                                                 100*log n
       200
         0
             1    2      3      4       5       6        7      8       9          10       11

100*log n    0    100   158.5   200   232.19   258.5   280.74   300   316.99      332.19   345.94
50*n         50   100   150     200    250     300      350     400    450         500      550
20*n log n   0    40    95.1    160   232.19   310.2   393.03   480   570.59      664.39   761.07
10*n^2       10   40     90     160    250     360      490     640    810        1000     1210
n^3          1    8      27     64     125     216      343     512    729        1000     1331
2^n          2    4       8     16     32       64      128     256    512        1024     2048

More Related Content

What's hot

Design and Analysis of Algorithms
Design and Analysis of AlgorithmsDesign and Analysis of Algorithms
Design and Analysis of AlgorithmsArvind Krishnaa
 
Algorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms IAlgorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms IMohamed Loey
 
Asymptotic Notations
Asymptotic NotationsAsymptotic Notations
Asymptotic NotationsRishabh Soni
 
Stressen's matrix multiplication
Stressen's matrix multiplicationStressen's matrix multiplication
Stressen's matrix multiplicationKumar
 
daa-unit-3-greedy method
daa-unit-3-greedy methoddaa-unit-3-greedy method
daa-unit-3-greedy methodhodcsencet
 
B trees in Data Structure
B trees in Data StructureB trees in Data Structure
B trees in Data StructureAnuj Modi
 
Analysis of algorithms
Analysis of algorithmsAnalysis of algorithms
Analysis of algorithmsGanesh Solanke
 
Algorithm Complexity and Main Concepts
Algorithm Complexity and Main ConceptsAlgorithm Complexity and Main Concepts
Algorithm Complexity and Main ConceptsAdelina Ahadova
 
Dijkstra s algorithm
Dijkstra s algorithmDijkstra s algorithm
Dijkstra s algorithmmansab MIRZA
 
Data structure - Graph
Data structure - GraphData structure - Graph
Data structure - GraphMadhu Bala
 
Prims and kruskal algorithms
Prims and kruskal algorithmsPrims and kruskal algorithms
Prims and kruskal algorithmsSaga Valsalan
 
Time and space complexity
Time and space complexityTime and space complexity
Time and space complexityAnkit Katiyar
 
Algorithm: Quick-Sort
Algorithm: Quick-SortAlgorithm: Quick-Sort
Algorithm: Quick-SortTareq Hasan
 
Greedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack ProblemGreedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack ProblemMadhu Bala
 

What's hot (20)

Design and Analysis of Algorithms
Design and Analysis of AlgorithmsDesign and Analysis of Algorithms
Design and Analysis of Algorithms
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
 
Algorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms IAlgorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms I
 
Asymptotic Notations
Asymptotic NotationsAsymptotic Notations
Asymptotic Notations
 
Merge Sort
Merge SortMerge Sort
Merge Sort
 
Master theorem
Master theoremMaster theorem
Master theorem
 
Stressen's matrix multiplication
Stressen's matrix multiplicationStressen's matrix multiplication
Stressen's matrix multiplication
 
Merge sort algorithm power point presentation
Merge sort algorithm power point presentationMerge sort algorithm power point presentation
Merge sort algorithm power point presentation
 
daa-unit-3-greedy method
daa-unit-3-greedy methoddaa-unit-3-greedy method
daa-unit-3-greedy method
 
B trees in Data Structure
B trees in Data StructureB trees in Data Structure
B trees in Data Structure
 
Analysis of algorithms
Analysis of algorithmsAnalysis of algorithms
Analysis of algorithms
 
Algorithm Complexity and Main Concepts
Algorithm Complexity and Main ConceptsAlgorithm Complexity and Main Concepts
Algorithm Complexity and Main Concepts
 
Time andspacecomplexity
Time andspacecomplexityTime andspacecomplexity
Time andspacecomplexity
 
Dijkstra s algorithm
Dijkstra s algorithmDijkstra s algorithm
Dijkstra s algorithm
 
Data structure - Graph
Data structure - GraphData structure - Graph
Data structure - Graph
 
Prims and kruskal algorithms
Prims and kruskal algorithmsPrims and kruskal algorithms
Prims and kruskal algorithms
 
Approximation Algorithms
Approximation AlgorithmsApproximation Algorithms
Approximation Algorithms
 
Time and space complexity
Time and space complexityTime and space complexity
Time and space complexity
 
Algorithm: Quick-Sort
Algorithm: Quick-SortAlgorithm: Quick-Sort
Algorithm: Quick-Sort
 
Greedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack ProblemGreedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack Problem
 

Viewers also liked

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
 
Operational research
Operational researchOperational research
Operational researchAlbi Thomas
 
9 big o-notation
9 big o-notation9 big o-notation
9 big o-notationirdginfo
 
Big o notation
Big o notationBig o notation
Big o notationkeb97
 
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
 
Radix 4 FFT algorithm and it time complexity computation
Radix 4 FFT algorithm and it time complexity computationRadix 4 FFT algorithm and it time complexity computation
Radix 4 FFT algorithm and it time complexity computationRaj Jaiswal
 
Lecture 1 data structures and algorithms
Lecture 1 data structures and algorithmsLecture 1 data structures and algorithms
Lecture 1 data structures and algorithmsAakash deep Singhal
 
DATA STRUCTURES
DATA STRUCTURESDATA STRUCTURES
DATA STRUCTURESbca2010
 
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 complexitiesLAKSHMITHARUN PONNAM
 
Asymptotes and holes 97
Asymptotes and holes 97Asymptotes and holes 97
Asymptotes and holes 97swartzje
 
Algorithm paradigms
Algorithm paradigmsAlgorithm paradigms
Algorithm paradigmssuresh5c2
 
Path cycle part1
Path cycle part1Path cycle part1
Path cycle part1guestb63941
 

Viewers also liked (20)

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
 
Operational research
Operational researchOperational research
Operational research
 
9 big o-notation
9 big o-notation9 big o-notation
9 big o-notation
 
Big o
Big oBig o
Big o
 
Big o notation
Big o notationBig o notation
Big o notation
 
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)
 
Radix 4 FFT algorithm and it time complexity computation
Radix 4 FFT algorithm and it time complexity computationRadix 4 FFT algorithm and it time complexity computation
Radix 4 FFT algorithm and it time complexity computation
 
Merge sort
Merge sortMerge sort
Merge sort
 
Lecture 1 data structures and algorithms
Lecture 1 data structures and algorithmsLecture 1 data structures and algorithms
Lecture 1 data structures and algorithms
 
DATA STRUCTURES
DATA STRUCTURESDATA STRUCTURES
DATA STRUCTURES
 
Data structure
Data structureData structure
Data structure
 
Heapsort ppt
Heapsort pptHeapsort ppt
Heapsort ppt
 
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
 
Linear search
Linear searchLinear search
Linear search
 
Computational Complexity
Computational ComplexityComputational Complexity
Computational Complexity
 
Asymptote
AsymptoteAsymptote
Asymptote
 
Asymptotes and holes 97
Asymptotes and holes 97Asymptotes and holes 97
Asymptotes and holes 97
 
Algorithm paradigms
Algorithm paradigmsAlgorithm paradigms
Algorithm paradigms
 
Path cycle part1
Path cycle part1Path cycle part1
Path cycle part1
 

Similar to Time complexity

04. Growth_Rate_AND_Asymptotic Notations_.pptx
04. Growth_Rate_AND_Asymptotic Notations_.pptx04. Growth_Rate_AND_Asymptotic Notations_.pptx
04. Growth_Rate_AND_Asymptotic Notations_.pptxarslanzaheer14
 
1_Asymptotic_Notation_pptx.pptx
1_Asymptotic_Notation_pptx.pptx1_Asymptotic_Notation_pptx.pptx
1_Asymptotic_Notation_pptx.pptxpallavidhade2
 
Dr hasany 2467_16649_1_lec-2-zabist
Dr hasany 2467_16649_1_lec-2-zabistDr hasany 2467_16649_1_lec-2-zabist
Dr hasany 2467_16649_1_lec-2-zabistGatewayggg Testeru
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithmspppepito86
 
Unit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdfUnit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdfAmayJaiswal4
 
Design and analysis of algorithm ppt ppt
Design and analysis of algorithm ppt pptDesign and analysis of algorithm ppt ppt
Design and analysis of algorithm ppt pptsrushtiivp
 
Asymptotic analysis
Asymptotic analysisAsymptotic analysis
Asymptotic analysisSoujanya V
 
CMSC 56 | Lecture 8: Growth of Functions
CMSC 56 | Lecture 8: Growth of FunctionsCMSC 56 | Lecture 8: Growth of Functions
CMSC 56 | Lecture 8: Growth of Functionsallyn joy calcaben
 
Asymptotic Notation and Complexity
Asymptotic Notation and ComplexityAsymptotic Notation and Complexity
Asymptotic Notation and ComplexityRajandeep Gill
 
Algorithm And analysis Lecture 03& 04-time complexity.
 Algorithm And analysis Lecture 03& 04-time complexity. Algorithm And analysis Lecture 03& 04-time complexity.
Algorithm And analysis Lecture 03& 04-time complexity.Tariq Khan
 
Lec03 04-time complexity
Lec03 04-time complexityLec03 04-time complexity
Lec03 04-time complexityAbbas Ali
 
Algorithms required for data structures(basics like Arrays, Stacks ,Linked Li...
Algorithms required for data structures(basics like Arrays, Stacks ,Linked Li...Algorithms required for data structures(basics like Arrays, Stacks ,Linked Li...
Algorithms required for data structures(basics like Arrays, Stacks ,Linked Li...DebiPrasadSen
 
pradeepbishtLecture13 div conq
pradeepbishtLecture13 div conqpradeepbishtLecture13 div conq
pradeepbishtLecture13 div conqPradeep Bisht
 

Similar to Time complexity (20)

04. Growth_Rate_AND_Asymptotic Notations_.pptx
04. Growth_Rate_AND_Asymptotic Notations_.pptx04. Growth_Rate_AND_Asymptotic Notations_.pptx
04. Growth_Rate_AND_Asymptotic Notations_.pptx
 
Big O Notation.ppt
Big O Notation.pptBig O Notation.ppt
Big O Notation.ppt
 
1_Asymptotic_Notation_pptx.pptx
1_Asymptotic_Notation_pptx.pptx1_Asymptotic_Notation_pptx.pptx
1_Asymptotic_Notation_pptx.pptx
 
Dr hasany 2467_16649_1_lec-2-zabist
Dr hasany 2467_16649_1_lec-2-zabistDr hasany 2467_16649_1_lec-2-zabist
Dr hasany 2467_16649_1_lec-2-zabist
 
Asymptotic Notation
Asymptotic NotationAsymptotic Notation
Asymptotic Notation
 
AsymptoticAnalysis.ppt
AsymptoticAnalysis.pptAsymptoticAnalysis.ppt
AsymptoticAnalysis.ppt
 
Computer science-formulas
Computer science-formulasComputer science-formulas
Computer science-formulas
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithms
 
Unit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdfUnit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdf
 
Design and analysis of algorithm ppt ppt
Design and analysis of algorithm ppt pptDesign and analysis of algorithm ppt ppt
Design and analysis of algorithm ppt ppt
 
Asymptotic analysis
Asymptotic analysisAsymptotic analysis
Asymptotic analysis
 
CMSC 56 | Lecture 8: Growth of Functions
CMSC 56 | Lecture 8: Growth of FunctionsCMSC 56 | Lecture 8: Growth of Functions
CMSC 56 | Lecture 8: Growth of Functions
 
Asymptotic Notation and Complexity
Asymptotic Notation and ComplexityAsymptotic Notation and Complexity
Asymptotic Notation and Complexity
 
Algorithm And analysis Lecture 03& 04-time complexity.
 Algorithm And analysis Lecture 03& 04-time complexity. Algorithm And analysis Lecture 03& 04-time complexity.
Algorithm And analysis Lecture 03& 04-time complexity.
 
Lec03 04-time complexity
Lec03 04-time complexityLec03 04-time complexity
Lec03 04-time complexity
 
08 - Complexity
08 - Complexity08 - Complexity
08 - Complexity
 
Asymptotic notation
Asymptotic notationAsymptotic notation
Asymptotic notation
 
Algo complexity
Algo complexityAlgo complexity
Algo complexity
 
Algorithms required for data structures(basics like Arrays, Stacks ,Linked Li...
Algorithms required for data structures(basics like Arrays, Stacks ,Linked Li...Algorithms required for data structures(basics like Arrays, Stacks ,Linked Li...
Algorithms required for data structures(basics like Arrays, Stacks ,Linked Li...
 
pradeepbishtLecture13 div conq
pradeepbishtLecture13 div conqpradeepbishtLecture13 div conq
pradeepbishtLecture13 div conq
 

More from Katang Isip

Reflection paper
Reflection paperReflection paper
Reflection paperKatang Isip
 
Punctuation tips
Punctuation tipsPunctuation tips
Punctuation tipsKatang Isip
 
Class list data structure
Class list data structureClass list data structure
Class list data structureKatang Isip
 
Hash table and heaps
Hash table and heapsHash table and heaps
Hash table and heapsKatang Isip
 
Binary Search Tree and AVL
Binary Search Tree and AVLBinary Search Tree and AVL
Binary Search Tree and AVLKatang Isip
 

More from Katang Isip (7)

Reflection paper
Reflection paperReflection paper
Reflection paper
 
Punctuation tips
Punctuation tipsPunctuation tips
Punctuation tips
 
3 act story
3 act story3 act story
3 act story
 
Class list data structure
Class list data structureClass list data structure
Class list data structure
 
Stack and queue
Stack and queueStack and queue
Stack and queue
 
Hash table and heaps
Hash table and heapsHash table and heaps
Hash table and heaps
 
Binary Search Tree and AVL
Binary Search Tree and AVLBinary Search Tree and AVL
Binary Search Tree and AVL
 

Recently uploaded

Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsKarakKing
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxcallscotland1987
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxEsquimalt MFRC
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdfssuserdda66b
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701bronxfugly43
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the ClassroomPooky Knightsmith
 

Recently uploaded (20)

Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 

Time complexity

  • 1. CS 213 Data Structures and Algorithms Second Semester, 2011-2012
  • 2. Sum of Squares int sumOfSquares(int n){ int sum = 0; for(int i=1; i<=n; i++) sum = sum + i*i; return sum; }  T(n) = 5n + 4
  • 3. Selection Sort Given the following numbers in an array: 53 23 10 34 2 17 2 23 10 34 53 17 2 10 23 34 53 17 2 10 17 34 53 23 2 10 17 23 53 34 2 10 17 23 34 53
  • 4. Still on Selection Sort void selectionSort(int A[], int n){ for(int i=0; i<n; i++){ int min = i; for(int j=i+1; j<n; j++){ if(A[j] < A[min]) min = j; What is this function’s T(n)? What } we need is to compute for T(n) int temp = A[i]; from the inner loop going A[i] = A[min]; outwards. A[min] = temp; We need to use the summation } notation to solve the T(n). }
  • 5. The Summation Notation = x1 + x2 + x3 + x4
  • 6. Still on the summation notation 4 n ∑ i = 1+2+3+4 = 10 ∑i = n ( n +1) 2 i =1 i=1 5 n ∑ 3 = 3+3+3+3+3 = 15 ∑c =nc =15 i=1 i=1
  • 7. Exercise  for(int i=1; i<=5; i++) cout<<endl;  for(int i=1; i<=n; i++) cout<<endl;  for(int i=3; i<=m; i++) cout<<endl;  for(int i=1; i<=6; i++) for(int j=1; j<=8; j++) cout<<endl;  for(int i=1; i<=n; i++) for(int j=1; j<=n; j++) cout<<endl;  for(int i=1; i<=m; i++) for(int j=1; j<=m; j++) cout<<endl;  for(int i=1; i<=n; i++) for(int j=i; j<=n; j++) cout<<endl;
  • 8. Going back to the Selection Sort void selectionSort(int A[], int n){ for(int i=0; i<n; i++){ n n n int min = i; for(int j=i+1; j<n; j++){ ∑ ∑ c =∑ c(n-i+1-1) if(A[j] < A[min]) i=1 j=i+1 i=1 min = j; } n n int temp = A[i]; A[i] = A[min]; ∑ c(n-i) = c n-i∑ A[min] = temp; i=1 i=1 } n n n } ∑ n-i = ∑ n - ∑ i i=1 i=1 i=1
  • 9. Time Complexity A function that maps problem size into the time required to solve the problem. Typically, we are interested in the inherent complexity of computing the solution to problems in a particular class.
  • 10. Lower Bound  We might want to know how fast we can sort a list of n items, initially in an arbitrary order, regardless of the algorithm used.  What is sought here is the lower bound, L(n), on sorting, a property of the sorting problem and not of any particular algorithm.  This says that no algorithm can do the job in fewer than L(n) time units for arbitrary inputs.
  • 11. Upper Bound  We might also like to know how long it would take to sort such list using a known algorithm with a worst-case input.  What is sought here is the upper bound, U(n), which says that for arbitrary inputs we can always sort in time at most U(n).
  • 12. Goal of Time Complexity Analysis  While there are apparently two complexity functions for problems, L(n) and U(n), the ultimate goal is to make these two bounds coincide.  This is the optimal algorithm which has L(n) = U(n).  For some of the problems, this goal has not been realized yet!
  • 13. Invitation Consider this, CS 213, as you journey into finding optimal solutions to classes of problems! Who knows, you might win a million dollars ($$$) from Claymath Foundation!
  • 14. Upper Bound Complexity There are two ways in analyzing this bound: Counting instructions Solving recurrences Both are used to find the worst case of an algorithm.
  • 15. Big O-Notation (O(g(n))) The O-notation is used to describe the worst-case running time of an algorithm. O(n) means that the growth of the running time of the algorithm is a function of n. O-notation computes for the upper bound.
  • 16. O-Notation Defined O(g(n)) = {f(n): ∃ c>0, n0>0 s.t. 0 ≤ f(n) ≤ cg(n) for all n ≥ n0}. Example: Check if (n2/2) – 3n ∈ O(n2) (n2/2) – 3n ≤ cn2 ½ - 3/n ≤ c Choosing c = ½, n0 = 6 proves the claim.
  • 17. Another Example 3n2 - 100n + 6 ∈ O(n2) ? 3n2 - 100n + 6 ≤ cn2 3 – 100/n + 6/n2 ≤ c At this point, we have to choose a c>0 and an n0 What values will prove our claim?
  • 18. Lower Bound Complexity  This is the more difficult of the bounds.  There is no algorithm to analyze.  Ω(g(n)) is used to describe the lower bound of the running time of the algorithm or minimum possible running time of the algorithm.
  • 19. Ω-Notation Ω(g(n)) = {f(n): ∃ c>0, n0>0 s.t. 0 ≤ cg(n) ≤ f(n) for all n ≥ n0}. Example: Check if (n2/2) – 3n ∈ Ω(n2) cn2 ≤ (n2/2) – 3n c ≤ ½ - 3/n Choosing c = 1/14, n0 = 7 proves the claim.
  • 20. Another Example  Check if 3n2 - 100n + 6 ∈ Ω(n)  cn ≤ 3n2 - 100n + 6  c ≤ 3n – 100 + 6/n  At this point we need to find a c and an n0 that will prove our claim.  What values of c and n0 will suffice the inequality??
  • 21. θ-Notation Used to denote that the lower and upper bounds of the running time of the algorithm is tight, i.e. the growth rate of the upper and lower bounds are the same.
  • 22. θ-Notation Defined θ(g(n)) = {f(n): ∃ c1>0, c2>0, n0>0 s.t. 0 ≤ c1g(n) ≤ f(n) ≤ c2g(n) for all n ≥ n0}. f(n) ∈ θ(g(n)) if f(n) ∈ O(g(n)) and f(n) ∈ Ω(g(n))
  • 23. Complexity Classes Description O-notation constant O(1) logarithmic O(log n) linear O(n) n log n O(n log n) quadratic O(n2) cubic O(n3) polynomial O(nk), k≥1 exponential O(an), a>1
  • 24. Growth rate of complexity classes class n=2 n=16 n=256 n=1024 1 1 1 1 1 log n 1 4 8 10 n 2 16 256 1024 n log n 2 64 2048 10240 n^2 4 256 65536 1048576 n^3 8 4096 16777216 1.07E+09 2^n 4 65536 1.16E+77 1.8E+308
  • 25. Graph of the Growth Rates 50 n^2 45 40 n log n 35 2^n 30 n^3 25 20 15 10 n 5 log n 0 1 2 3 4 5 6 7 8 9 10 11 log n 0 1 1.58 2 2.32 2.58 2.81 3 3.17 3.32 3.46 n 1 2 3 4 5 6 7 8 9 10 11 n log n 0 2 4.75 8 11.61 15.51 19.65 24 28.53 33.22 38.05 n^2 1 4 9 16 25 36 49 64 81 100 121 n^3 1 8 27 64 125 216 343 512 729 1000 1331 2^n 2 4 8 16 32 64 128 256 512 1024 2048
  • 26. Bigger N 2000 1800 1600 1400 n^3 1200 1000 800 600 2^n 400 200 n^2 0 n log n n n log 1 2 3 4 5 6 7 8 9 10 11 log n 0 1 1.58 2 2.32 2.58 2.81 3 3.17 3.32 3.46 n 1 2 3 4 5 6 7 8 9 10 11 n log n 0 2 4.75 8 11.61 15.51 19.65 24 28.53 33.22 38.05 n^2 1 4 9 16 25 36 49 64 81 100 121 n^3 1 8 27 64 125 216 343 512 729 1000 1331 2^n 2 4 8 16 32 64 128 256 512 1024 2048
  • 27. Still on the Graph 2000 1800 1600 1400 n^3 1200 10*n^2 1000 800 20*n log n 600 50*n 2^n 400 100*log n 200 0 1 2 3 4 5 6 7 8 9 10 11 100*log n 0 100 158.5 200 232.19 258.5 280.74 300 316.99 332.19 345.94 50*n 50 100 150 200 250 300 350 400 450 500 550 20*n log n 0 40 95.1 160 232.19 310.2 393.03 480 570.59 664.39 761.07 10*n^2 10 40 90 160 250 360 490 640 810 1000 1210 n^3 1 8 27 64 125 216 343 512 729 1000 1331 2^n 2 4 8 16 32 64 128 256 512 1024 2048