SlideShare ist ein Scribd-Unternehmen logo
1 von 35
Algorithms Complexity and
 Data Structures Efficiency
Computational Complexity, Choosing Data Structures




Svetlin Nakov
Telerik Corporation
www.telerik.com
Table of Contents
1.   Algorithms Complexity and Asymptotic
     Notation
      Time and Memory Complexity
      Mean, Average and Worst Case
2.   Fundamental Data Structures – Comparison
      Arrays vs. Lists vs. Trees vs. Hash-Tables
3.   Choosing Proper Data Structure



                                                    2
Why Data Structures are
                             Important?
 Data structures and algorithms
                              are the
 foundation of computer programming
 Algorithmic thinking, problem solving  and
 data structures are vital for software engineers
   All .NET developers should know when to use
    T[], LinkedList<T>, List<T>, Stack<T>,
    Queue<T>, Dictionary<K,T>, HashSet<T>,
    SortedDictionary<K,T> and SortedSet<T>
 Computational complexity is important for
 algorithm design and efficient programming
                                                    3
Algorithms Complexity
     Asymtotic Notation
Algorithm Analysis
 Why we should analyze    algorithms?
  Predict the resources that the algorithm
   requires
    Computational time (CPU consumption)
    Memory space (RAM consumption)
    Communication bandwidth consumption
  The running time of an algorithm is:
    The total number of primitive operations
     executed (machine independent steps)
    Also known as algorithm complexity
                                                5
Algorithmic Complexity
 What to measure?

  Memory
  Time
  Number of steps
  Number of particular operations
    Number of disk operations
    Number of network packets
  Asymptotic complexity

                                            6
Time Complexity
 Worst-case

  An upper bound on the running time for any
   input of given size
 Average-case

  Assume all inputs of a given size are equally
   likely
 Best-case

  The lower bound on the running time

                                                   7
Time Complexity – Example
 Sequential search in a list   of size n
   Worst-case:
    n comparisons
                            … … … … … … …
   Best-case:
                                         n
    1 comparison
   Average-case:
    n/2 comparisons
 The algorithm runs in linear    time
   Linear number of operations
                                             8
Algorithms Complexity
   Algorithm complexity is rough estimation of the
    number of steps performed by given computation
    depending on the size of the input data
     Measured through asymptotic notation
       O(g) where g is a function of the input data size
     Examples:
       Linear complexity O(n) – all elements are
        processed once (or constant number of times)
       Quadratic complexity O(n2) – each of the
        elements is processed n times
                                                            9
Asymptotic Notation: Definition
   Asymptotic upper bound
     O-notation (Big O notation)
   For given function g(n), we denote by O(g(n))
    the set of functions that are different than g(n)
    by a constant
     O(g(n)) = {f(n): there exist positive constants c
     and n0 such that f(n) <= c*g(n) for all n >= n0}
   Examples:
     3 * n2 + n/2 + 12 ∈ O(n2)
     4*n*log2(3*n+1) + 2*n-1 ∈ O(n * log n)
                                                         10
Typical Complexities
Complexity Notation             Description
                     Constant number of
                     operations, not depending on
  constant    O(1)
                     the input data size, e.g.
                     n = 1 000 000  1-2 operations
                     Number of operations propor-
                     tional of log2(n) where n is the
logarithmic O(log n)
                     size of the input data, e.g. n =
                     1 000 000 000  30 operations
                     Number of operations
                     proportional to the input data
   linear     O(n)
                     size, e.g. n = 10 000  5 000
                     operations
                                                        11
Typical Complexities (2)
Complexity Notation             Description
                       Number of operations
                       proportional to the square of
 quadratic    O(n2)
                       the size of the input data, e.g.
                       n = 500  250 000 operations
                       Number of operations propor-
                       tional to the cube of the size
   cubic      O(n3)
                       of the input data, e.g. n =
                       200  8 000 000 operations
              O(2n),   Exponential number of
exponential   O(kn),   operations, fast growing, e.g.
              O(n!)    n = 20  1 048 576 operations

                                                          12
Time Complexity and Speed
Complexity     10     20    50    100 1 000 10 000 100 000
   O(1)        <1s   <1s   <1s    <1s   <1s     <1s      <1s
 O(log(n))     <1s   <1s   <1s    <1s   <1s     <1s      <1s
   O(n)        <1s   <1s   <1s    <1s   <1s     <1s      <1s
O(n*log(n))    <1s   <1s   <1s    <1s   <1s     <1s      <1s
   O(n2)       <1s   <1s   <1s    <1s   <1s      2s    3-4 min
   O(n3)       <1s   <1s   <1s    <1s   20 s   5 hours 231 days
                           260
   O(2n)       <1s   <1s        hangs hangs hangs       hangs
                           days
   O(n!)       <1s   hangs hangs hangs hangs hangs      hangs
  O(nn)       3-4 min hangs hangs hangs hangs hangs     hangs

                                                                  13
Time and Memory Complexity
   Complexity can be expressed as formula on
    multiple variables, e.g.
     Algorithm filling a matrix of size n * m with natural
      numbers 1, 2, … will run in O(n*m)
     DFS traversal of graph with n vertices and m edges
      will run in O(n + m)
   Memory consumption should also be considered,
    for example:
     Running time O(n), memory requirement O(n2)
     n = 50 000  OutOfMemoryException
                                                              14
Polynomial Algorithms
 A polynomial-time algorithm  is one whose
 worst-case time complexity is bounded above
 by a polynomial function of its input size

               W(n) ∈ O(p(n))

 Example of worst-case time complexity

  Polynomial-time: log n, 2n, 3n3 + 4n, 2 * n log n
  Non polynomial-time : 2n, 3n, nk, n!
 Non-polynomial algorithms    don't work for
 large input data sets
                                                       15
Analyzing Complexity
   of Algorithms
       Examples
Complexity Examples
     int FindMaxElement(int[] array)
     {
         int max = array[0];
         for (int i=0; i<array.length; i++)
         {
             if (array[i] > max)
             {
                 max = array[i];
             }
         }
         return max;
     }


 Runs in O(n) where n is the size of the array

 The number of elementary steps is           ~n
Complexity Examples (2)
    long FindInversions(int[] array)
    {
        long inversions = 0;
        for (int i=0; i<array.Length; i++)
            for (int j = i+1; j<array.Length; i++)
                if (array[i] > array[j])
                    inversions++;
        return inversions;
    }


 Runs in O(n2) where n is the size of the array

 The number of elementary steps is
 ~ n*(n+1) / 2
Complexity Examples (3)
    decimal Sum3(int n)
    {
        decimal sum = 0;
        for (int a=0; a<n; a++)
            for (int b=0; b<n; b++)
                for (int c=0; c<n; c++)
                    sum += a*b*c;
        return sum;
    }


 Runs in cubic time O(n3)

 The number of elementary steps is       ~ n3
Complexity Examples (4)
    long SumMN(int n, int m)
    {
        long sum = 0;
        for (int x=0; x<n; x++)
            for (int y=0; y<m; y++)
                sum += x*y;
        return sum;
    }


 Runs in quadratic   time O(n*m)
 The number of elementary steps is   ~ n*m
Complexity Examples (5)
    long SumMN(int n, int m)
    {
        long sum = 0;
        for (int x=0; x<n; x++)
            for (int y=0; y<m; y++)
                if (x==y)
                    for (int i=0; i<n; i++)
                        sum += i*x*y;
        return sum;
    }


 Runs in quadratic   time O(n*m)
 The number of elementary steps is
 ~ n*m + min(m,n)*n
Complexity Examples (6)
    decimal Calculation(int n)
    {
        decimal result = 0;
        for (int i = 0; i < (1<<n); i++)
            result += i;
        return result;
    }


 Runs in exponential time O(2n)

 The number of elementary steps is        ~ 2n
Complexity Examples (7)
     decimal Factorial(int n)
     {
         if (n==0)
             return 1;
         else
             return n * Factorial(n-1);
     }


 Runs in linear   time O(n)
 The number of elementary steps is       ~n
Complexity Examples (8)
  decimal Fibonacci(int n)
  {
      if (n == 0)
          return 1;
      else if (n == 1)
          return 1;
      else
          return Fibonacci(n-1) + Fibonacci(n-2);
  }


 Runs in exponential time O(2n)

 The number of elementary steps is
 ~ Fib(n+1) where Fib(k) is the k-th
 Fibonacci's number
Comparing Data Structures
          Examples
Data Structures Efficiency
                                          Get-by-
 Data Structure        Add    Find Delete
                                           index
   Array (T[])         O(n) O(n)    O(n)   O(1)
   Linked list
                       O(1) O(n)    O(n)   O(n)
(LinkedList<T>)

Resizable array list
                       O(1) O(n)    O(n)   O(1)
    (List<T>)
Stack (Stack<T>)       O(1)    -    O(1)     -
Queue (Queue<T>)       O(1)    -    O(1)     -

                                                    26
Data Structures Efficiency (2)
                                             Get-by-
 Data Structure      Add      Find    Delete
                                              index
    Hash table
                     O(1)     O(1)     O(1)      -
(Dictionary<K,T>)
    Tree-based
dictionary (Sorted O(log n) O(log n) O(log n)    -
Dictionary<K,T>)
 Hash table based
                     O(1)     O(1)     O(1)      -
 set (HashSet<T>)
  Tree based set
                    O(log n) O(log n) O(log n)   -
 (SortedSet<T>)

                                                       27
Choosing Data Structure
 Arrays   (T[])
   Use when fixed number of elements should be
    processed by index
 Resizable array   lists (List<T>)
   Use when elements should be added and
    processed by index
 Linked lists   (LinkedList<T>)
   Use when elements should be added at the
    both sides of the list
   Otherwise use resizable array list (List<T>)
                                                   28
Choosing Data Structure (2)
   Stacks (Stack<T>)
     Use to implement LIFO (last-in-first-out) behavior
     List<T> could also work well
   Queues (Queue<T>)
     Use to implement FIFO (first-in-first-out) behavior
     LinkedList<T> could also work well
   Hash table based dictionary (Dictionary<K,T>)
     Use when key-value pairs should be added fast and
      searched fast by key
     Elements in a hash table have no particular order
                                                            29
Choosing Data Structure (3)
   Balanced search tree based dictionary
    (SortedDictionary<K,T>)
     Use when key-value pairs should be added fast,
      searched fast by key and enumerated sorted by key
 Hash table based set (HashSet<T>)

     Use to keep a group of unique values, to add
      and check belonging to the set fast
     Elements are in no particular order
 Search tree based set (SortedSet<T>)

     Use to keep a group of ordered unique values
                                                          30
Summary
   Algorithm complexity is rough estimation of the
    number of steps performed by given computation
     Complexity can be logarithmic, linear, n log n,
      square, cubic, exponential, etc.
     Allows to estimating the speed of given code
      before its execution
 Different data structures have different
    efficiency on different operations
     The fastest add / find / delete structure is the
      hash table – O(1) for all these operations
                                                         31
Algorithms Complexity and
  Data Structures Efficiency




Questions?

                http://academy.telerik.com
Exercises
1.   A text file students.txt holds information about
     students and their courses in the following format:
      Kiril    |   Ivanov     |   C#
      Stefka   |   Nikolova   |   SQL
      Stela    |   Mineva     |   Java
      Milena   |   Petrova    |   C#
      Ivan     |   Grigorov   |   C#
      Ivan     |   Kolev      |   SQL

     Using SortedDictionary<K,T> print the courses in
     alphabetical order and for each of them prints the
     students ordered by family and then by name:
      C#: Ivan Grigorov, Kiril Ivanov, Milena Petrova
      Java: Stela Mineva
      SQL: Ivan Kolev, Stefka Nikolova
                                                                33
Exercises (2)
2.   A large trade company has millions of articles, each
     described by barcode, vendor, title and price.
     Implement a data structure to store them that
     allows fast retrieval of all articles in given price range
     [x…y]. Hint: use OrderedMultiDictionary<K,T>
     from Wintellect's Power Collections for .NET.
3.   Implement a data structure PriorityQueue<T>
     that provides a fast way to execute the following
     operations: add element; extract the smallest element.
4.   Implement a class BiDictionary<K1,K2,T> that
     allows adding triples {key1, key2, value} and fast
     search by key1, key2 or by both key1 and key2.
     Note: multiple values can be stored for given key.
                                                                  34
Exercises (3)
5.   A text file phones.txt holds information about
     people, their town and phone number:
      Mimi Shmatkata          |   Plovdiv    |   0888 12 34 56
      Kireto                  |   Varna      |   052 23 45 67
      Daniela Ivanova Petrova |   Karnobat   |   0899 999 888
      Bat Gancho              |   Sofia      |   02 946 946 946

     Duplicates can occur in people names, towns and
     phone numbers. Write a program to execute a
     sequence of commands from a file commands.txt:
      find(name) – display all matching records by given
       name (first, middle, last or nickname)
      find(name, town) – display all matching records by
       given name and town
                                                                     35

Weitere ähnliche Inhalte

Was ist angesagt?

Signal Processing Introduction using Fourier Transforms
Signal Processing Introduction using Fourier TransformsSignal Processing Introduction using Fourier Transforms
Signal Processing Introduction using Fourier Transforms
Arvind Devaraj
 
Chapter 9 computation of the dft
Chapter 9 computation of the dftChapter 9 computation of the dft
Chapter 9 computation of the dft
mikeproud
 
Digital Signal Processing Tutorial:Chapt 3 frequency analysis
Digital Signal Processing Tutorial:Chapt 3 frequency analysisDigital Signal Processing Tutorial:Chapt 3 frequency analysis
Digital Signal Processing Tutorial:Chapt 3 frequency analysis
Chandrashekhar Padole
 
Data Structures - Lecture 8 - Study Notes
Data Structures - Lecture 8 - Study NotesData Structures - Lecture 8 - Study Notes
Data Structures - Lecture 8 - Study Notes
Haitham El-Ghareeb
 
Csr2011 june15 12_00_davydow
Csr2011 june15 12_00_davydowCsr2011 june15 12_00_davydow
Csr2011 june15 12_00_davydow
CSR2011
 

Was ist angesagt? (16)

Signal Processing Introduction using Fourier Transforms
Signal Processing Introduction using Fourier TransformsSignal Processing Introduction using Fourier Transforms
Signal Processing Introduction using Fourier Transforms
 
A Proposition for Business Process Modeling
A Proposition for Business Process ModelingA Proposition for Business Process Modeling
A Proposition for Business Process Modeling
 
Lec4
Lec4Lec4
Lec4
 
Lec1
Lec1Lec1
Lec1
 
Lecture09 recursion
Lecture09 recursionLecture09 recursion
Lecture09 recursion
 
Lec22
Lec22Lec22
Lec22
 
Chapter 9 computation of the dft
Chapter 9 computation of the dftChapter 9 computation of the dft
Chapter 9 computation of the dft
 
DSP_FOEHU - MATLAB 02 - The Discrete-time Fourier Analysis
DSP_FOEHU - MATLAB 02 - The Discrete-time Fourier AnalysisDSP_FOEHU - MATLAB 02 - The Discrete-time Fourier Analysis
DSP_FOEHU - MATLAB 02 - The Discrete-time Fourier Analysis
 
Lec5
Lec5Lec5
Lec5
 
Digital Signal Processing Tutorial:Chapt 3 frequency analysis
Digital Signal Processing Tutorial:Chapt 3 frequency analysisDigital Signal Processing Tutorial:Chapt 3 frequency analysis
Digital Signal Processing Tutorial:Chapt 3 frequency analysis
 
Decimation in time and frequency
Decimation in time and frequencyDecimation in time and frequency
Decimation in time and frequency
 
Options and trade offs for parallelism and concurrency in Modern C++
Options and trade offs for parallelism and concurrency in Modern C++Options and trade offs for parallelism and concurrency in Modern C++
Options and trade offs for parallelism and concurrency in Modern C++
 
Data Structures - Lecture 8 - Study Notes
Data Structures - Lecture 8 - Study NotesData Structures - Lecture 8 - Study Notes
Data Structures - Lecture 8 - Study Notes
 
Csr2011 june15 12_00_davydow
Csr2011 june15 12_00_davydowCsr2011 june15 12_00_davydow
Csr2011 june15 12_00_davydow
 
Digital Signal Processing[ECEG-3171]-Ch1_L02
Digital Signal Processing[ECEG-3171]-Ch1_L02Digital Signal Processing[ECEG-3171]-Ch1_L02
Digital Signal Processing[ECEG-3171]-Ch1_L02
 
Stacks queues lists
Stacks queues listsStacks queues lists
Stacks queues lists
 

Ähnlich wie 19. algorithms and-complexity

19. algorithms and-complexity
19. algorithms and-complexity19. algorithms and-complexity
19. algorithms and-complexity
showkat27
 
Lec03 04-time complexity
Lec03 04-time complexityLec03 04-time complexity
Lec03 04-time complexity
Abbas Ali
 
DS Unit-1.pptx very easy to understand..
DS Unit-1.pptx very easy to understand..DS Unit-1.pptx very easy to understand..
DS Unit-1.pptx very easy to understand..
KarthikeyaLanka1
 
Advanced Datastructures and algorithms CP4151unit1b.pdf
Advanced Datastructures and algorithms CP4151unit1b.pdfAdvanced Datastructures and algorithms CP4151unit1b.pdf
Advanced Datastructures and algorithms CP4151unit1b.pdf
Sheba41
 
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
Sajid Marwat
 

Ähnlich wie 19. algorithms and-complexity (20)

19. algorithms and-complexity
19. algorithms and-complexity19. algorithms and-complexity
19. algorithms and-complexity
 
Computational Complexity.pptx
Computational Complexity.pptxComputational Complexity.pptx
Computational Complexity.pptx
 
Annotations.pdf
Annotations.pdfAnnotations.pdf
Annotations.pdf
 
Introducción al Análisis y diseño de algoritmos
Introducción al Análisis y diseño de algoritmosIntroducción al Análisis y diseño de algoritmos
Introducción al Análisis y diseño de algoritmos
 
Alg1
Alg1Alg1
Alg1
 
Lec1
Lec1Lec1
Lec1
 
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.
 
18103010 algorithm complexity (iterative)
18103010 algorithm complexity (iterative)18103010 algorithm complexity (iterative)
18103010 algorithm complexity (iterative)
 
Lec7
Lec7Lec7
Lec7
 
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
 
Lec03 04-time complexity
Lec03 04-time complexityLec03 04-time complexity
Lec03 04-time complexity
 
Data Structure and Algorithms
Data Structure and Algorithms Data Structure and Algorithms
Data Structure and Algorithms
 
time_complexity_list_02_04_2024_22_pages.pdf
time_complexity_list_02_04_2024_22_pages.pdftime_complexity_list_02_04_2024_22_pages.pdf
time_complexity_list_02_04_2024_22_pages.pdf
 
DS Unit-1.pptx very easy to understand..
DS Unit-1.pptx very easy to understand..DS Unit-1.pptx very easy to understand..
DS Unit-1.pptx very easy to understand..
 
Analysis of Algorithum
Analysis of AlgorithumAnalysis of Algorithum
Analysis of Algorithum
 
Time complexity.ppt
Time complexity.pptTime complexity.ppt
Time complexity.ppt
 
Advanced Datastructures and algorithms CP4151unit1b.pdf
Advanced Datastructures and algorithms CP4151unit1b.pdfAdvanced Datastructures and algorithms CP4151unit1b.pdf
Advanced Datastructures and algorithms CP4151unit1b.pdf
 
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
 

Kürzlich hochgeladen

Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
Chris Hunter
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdf
SanaAli374401
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
MateoGardella
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
PECB
 

Kürzlich hochgeladen (20)

Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
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
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdf
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
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
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 

19. algorithms and-complexity

  • 1. Algorithms Complexity and Data Structures Efficiency Computational Complexity, Choosing Data Structures Svetlin Nakov Telerik Corporation www.telerik.com
  • 2. Table of Contents 1. Algorithms Complexity and Asymptotic Notation  Time and Memory Complexity  Mean, Average and Worst Case 2. Fundamental Data Structures – Comparison  Arrays vs. Lists vs. Trees vs. Hash-Tables 3. Choosing Proper Data Structure 2
  • 3. Why Data Structures are Important?  Data structures and algorithms are the foundation of computer programming  Algorithmic thinking, problem solving and data structures are vital for software engineers  All .NET developers should know when to use T[], LinkedList<T>, List<T>, Stack<T>, Queue<T>, Dictionary<K,T>, HashSet<T>, SortedDictionary<K,T> and SortedSet<T>  Computational complexity is important for algorithm design and efficient programming 3
  • 4. Algorithms Complexity Asymtotic Notation
  • 5. Algorithm Analysis  Why we should analyze algorithms?  Predict the resources that the algorithm requires  Computational time (CPU consumption)  Memory space (RAM consumption)  Communication bandwidth consumption  The running time of an algorithm is:  The total number of primitive operations executed (machine independent steps)  Also known as algorithm complexity 5
  • 6. Algorithmic Complexity  What to measure?  Memory  Time  Number of steps  Number of particular operations  Number of disk operations  Number of network packets  Asymptotic complexity 6
  • 7. Time Complexity  Worst-case  An upper bound on the running time for any input of given size  Average-case  Assume all inputs of a given size are equally likely  Best-case  The lower bound on the running time 7
  • 8. Time Complexity – Example  Sequential search in a list of size n  Worst-case:  n comparisons … … … … … … …  Best-case: n  1 comparison  Average-case:  n/2 comparisons  The algorithm runs in linear time  Linear number of operations 8
  • 9. Algorithms Complexity  Algorithm complexity is rough estimation of the number of steps performed by given computation depending on the size of the input data  Measured through asymptotic notation  O(g) where g is a function of the input data size  Examples:  Linear complexity O(n) – all elements are processed once (or constant number of times)  Quadratic complexity O(n2) – each of the elements is processed n times 9
  • 10. Asymptotic Notation: Definition  Asymptotic upper bound  O-notation (Big O notation)  For given function g(n), we denote by O(g(n)) the set of functions that are different than g(n) by a constant O(g(n)) = {f(n): there exist positive constants c and n0 such that f(n) <= c*g(n) for all n >= n0}  Examples:  3 * n2 + n/2 + 12 ∈ O(n2)  4*n*log2(3*n+1) + 2*n-1 ∈ O(n * log n) 10
  • 11. Typical Complexities Complexity Notation Description Constant number of operations, not depending on constant O(1) the input data size, e.g. n = 1 000 000  1-2 operations Number of operations propor- tional of log2(n) where n is the logarithmic O(log n) size of the input data, e.g. n = 1 000 000 000  30 operations Number of operations proportional to the input data linear O(n) size, e.g. n = 10 000  5 000 operations 11
  • 12. Typical Complexities (2) Complexity Notation Description Number of operations proportional to the square of quadratic O(n2) the size of the input data, e.g. n = 500  250 000 operations Number of operations propor- tional to the cube of the size cubic O(n3) of the input data, e.g. n = 200  8 000 000 operations O(2n), Exponential number of exponential O(kn), operations, fast growing, e.g. O(n!) n = 20  1 048 576 operations 12
  • 13. Time Complexity and Speed Complexity 10 20 50 100 1 000 10 000 100 000 O(1) <1s <1s <1s <1s <1s <1s <1s O(log(n)) <1s <1s <1s <1s <1s <1s <1s O(n) <1s <1s <1s <1s <1s <1s <1s O(n*log(n)) <1s <1s <1s <1s <1s <1s <1s O(n2) <1s <1s <1s <1s <1s 2s 3-4 min O(n3) <1s <1s <1s <1s 20 s 5 hours 231 days 260 O(2n) <1s <1s hangs hangs hangs hangs days O(n!) <1s hangs hangs hangs hangs hangs hangs O(nn) 3-4 min hangs hangs hangs hangs hangs hangs 13
  • 14. Time and Memory Complexity  Complexity can be expressed as formula on multiple variables, e.g.  Algorithm filling a matrix of size n * m with natural numbers 1, 2, … will run in O(n*m)  DFS traversal of graph with n vertices and m edges will run in O(n + m)  Memory consumption should also be considered, for example:  Running time O(n), memory requirement O(n2)  n = 50 000  OutOfMemoryException 14
  • 15. Polynomial Algorithms  A polynomial-time algorithm is one whose worst-case time complexity is bounded above by a polynomial function of its input size W(n) ∈ O(p(n))  Example of worst-case time complexity  Polynomial-time: log n, 2n, 3n3 + 4n, 2 * n log n  Non polynomial-time : 2n, 3n, nk, n!  Non-polynomial algorithms don't work for large input data sets 15
  • 16. Analyzing Complexity of Algorithms Examples
  • 17. Complexity Examples int FindMaxElement(int[] array) { int max = array[0]; for (int i=0; i<array.length; i++) { if (array[i] > max) { max = array[i]; } } return max; }  Runs in O(n) where n is the size of the array  The number of elementary steps is ~n
  • 18. Complexity Examples (2) long FindInversions(int[] array) { long inversions = 0; for (int i=0; i<array.Length; i++) for (int j = i+1; j<array.Length; i++) if (array[i] > array[j]) inversions++; return inversions; }  Runs in O(n2) where n is the size of the array  The number of elementary steps is ~ n*(n+1) / 2
  • 19. Complexity Examples (3) decimal Sum3(int n) { decimal sum = 0; for (int a=0; a<n; a++) for (int b=0; b<n; b++) for (int c=0; c<n; c++) sum += a*b*c; return sum; }  Runs in cubic time O(n3)  The number of elementary steps is ~ n3
  • 20. Complexity Examples (4) long SumMN(int n, int m) { long sum = 0; for (int x=0; x<n; x++) for (int y=0; y<m; y++) sum += x*y; return sum; }  Runs in quadratic time O(n*m)  The number of elementary steps is ~ n*m
  • 21. Complexity Examples (5) long SumMN(int n, int m) { long sum = 0; for (int x=0; x<n; x++) for (int y=0; y<m; y++) if (x==y) for (int i=0; i<n; i++) sum += i*x*y; return sum; }  Runs in quadratic time O(n*m)  The number of elementary steps is ~ n*m + min(m,n)*n
  • 22. Complexity Examples (6) decimal Calculation(int n) { decimal result = 0; for (int i = 0; i < (1<<n); i++) result += i; return result; }  Runs in exponential time O(2n)  The number of elementary steps is ~ 2n
  • 23. Complexity Examples (7) decimal Factorial(int n) { if (n==0) return 1; else return n * Factorial(n-1); }  Runs in linear time O(n)  The number of elementary steps is ~n
  • 24. Complexity Examples (8) decimal Fibonacci(int n) { if (n == 0) return 1; else if (n == 1) return 1; else return Fibonacci(n-1) + Fibonacci(n-2); }  Runs in exponential time O(2n)  The number of elementary steps is ~ Fib(n+1) where Fib(k) is the k-th Fibonacci's number
  • 26. Data Structures Efficiency Get-by- Data Structure Add Find Delete index Array (T[]) O(n) O(n) O(n) O(1) Linked list O(1) O(n) O(n) O(n) (LinkedList<T>) Resizable array list O(1) O(n) O(n) O(1) (List<T>) Stack (Stack<T>) O(1) - O(1) - Queue (Queue<T>) O(1) - O(1) - 26
  • 27. Data Structures Efficiency (2) Get-by- Data Structure Add Find Delete index Hash table O(1) O(1) O(1) - (Dictionary<K,T>) Tree-based dictionary (Sorted O(log n) O(log n) O(log n) - Dictionary<K,T>) Hash table based O(1) O(1) O(1) - set (HashSet<T>) Tree based set O(log n) O(log n) O(log n) - (SortedSet<T>) 27
  • 28. Choosing Data Structure  Arrays (T[])  Use when fixed number of elements should be processed by index  Resizable array lists (List<T>)  Use when elements should be added and processed by index  Linked lists (LinkedList<T>)  Use when elements should be added at the both sides of the list  Otherwise use resizable array list (List<T>) 28
  • 29. Choosing Data Structure (2)  Stacks (Stack<T>)  Use to implement LIFO (last-in-first-out) behavior  List<T> could also work well  Queues (Queue<T>)  Use to implement FIFO (first-in-first-out) behavior  LinkedList<T> could also work well  Hash table based dictionary (Dictionary<K,T>)  Use when key-value pairs should be added fast and searched fast by key  Elements in a hash table have no particular order 29
  • 30. Choosing Data Structure (3)  Balanced search tree based dictionary (SortedDictionary<K,T>)  Use when key-value pairs should be added fast, searched fast by key and enumerated sorted by key  Hash table based set (HashSet<T>)  Use to keep a group of unique values, to add and check belonging to the set fast  Elements are in no particular order  Search tree based set (SortedSet<T>)  Use to keep a group of ordered unique values 30
  • 31. Summary  Algorithm complexity is rough estimation of the number of steps performed by given computation  Complexity can be logarithmic, linear, n log n, square, cubic, exponential, etc.  Allows to estimating the speed of given code before its execution  Different data structures have different efficiency on different operations  The fastest add / find / delete structure is the hash table – O(1) for all these operations 31
  • 32. Algorithms Complexity and Data Structures Efficiency Questions? http://academy.telerik.com
  • 33. Exercises 1. A text file students.txt holds information about students and their courses in the following format: Kiril | Ivanov | C# Stefka | Nikolova | SQL Stela | Mineva | Java Milena | Petrova | C# Ivan | Grigorov | C# Ivan | Kolev | SQL Using SortedDictionary<K,T> print the courses in alphabetical order and for each of them prints the students ordered by family and then by name: C#: Ivan Grigorov, Kiril Ivanov, Milena Petrova Java: Stela Mineva SQL: Ivan Kolev, Stefka Nikolova 33
  • 34. Exercises (2) 2. A large trade company has millions of articles, each described by barcode, vendor, title and price. Implement a data structure to store them that allows fast retrieval of all articles in given price range [x…y]. Hint: use OrderedMultiDictionary<K,T> from Wintellect's Power Collections for .NET. 3. Implement a data structure PriorityQueue<T> that provides a fast way to execute the following operations: add element; extract the smallest element. 4. Implement a class BiDictionary<K1,K2,T> that allows adding triples {key1, key2, value} and fast search by key1, key2 or by both key1 and key2. Note: multiple values can be stored for given key. 34
  • 35. Exercises (3) 5. A text file phones.txt holds information about people, their town and phone number: Mimi Shmatkata | Plovdiv | 0888 12 34 56 Kireto | Varna | 052 23 45 67 Daniela Ivanova Petrova | Karnobat | 0899 999 888 Bat Gancho | Sofia | 02 946 946 946 Duplicates can occur in people names, towns and phone numbers. Write a program to execute a sequence of commands from a file commands.txt:  find(name) – display all matching records by given name (first, middle, last or nickname)  find(name, town) – display all matching records by given name and town 35