Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our User Agreement and Privacy Policy.

Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our Privacy Policy and User Agreement for details.

19. Algorithms Complexity and Efficiency of Data Structures

Complexity of Algorithms
Efficiency and Comparison of Data Structures
How to Choose the Right Data Structure?
Choosing the Right Data Structures: Examples
Exercises: Choosing the Most Efficient Data Structure

7.
Time Complexity <ul><li>Worst-case </li></ul><ul><ul><li>An upper bound on the running time for any input of given size </li></ul></ul><ul><li>Average-case </li></ul><ul><ul><li>Assume all inputs of a given size are equally likely </li></ul></ul><ul><li>Best-case </li></ul><ul><ul><li>The lower bound on the running time </li></ul></ul>

9.
Algorithms Complexity <ul><li>Algorithm complexity is rough estimation of the number of steps performed by given computation depending on the size of the input data </li></ul><ul><ul><li>Measured through asymptotic notation </li></ul></ul><ul><ul><ul><li>O(g) where g is a function of the input data size </li></ul></ul></ul><ul><ul><li>Examples: </li></ul></ul><ul><ul><ul><li>Linear complexity O(n) – all elements are processed once (or constant number of times) </li></ul></ul></ul><ul><ul><ul><li>Quadratic complexity O(n 2 ) – each of the elements is processed n times </li></ul></ul></ul>

28.
Choosing Data Structure <ul><li>Arrays ( T[] ) </li></ul><ul><ul><li>Use when fixed number of elements should be processed by index </li></ul></ul><ul><li>Resizable array lists ( List<T> ) </li></ul><ul><ul><li>Use when elements should be added and processed by index </li></ul></ul><ul><li>Linked lists ( LinkedList<T> ) </li></ul><ul><ul><li>Use when elements should be added at the both sides of the list </li></ul></ul><ul><ul><li>Otherwise use resizable array list ( List<T> ) </li></ul></ul>

29.
Choosing Data Structure (2) <ul><li>Stacks ( Stack<T> ) </li></ul><ul><ul><li>Use to implement LIFO (last-in-first-out) behavior </li></ul></ul><ul><ul><li>List<T> could also work well </li></ul></ul><ul><li>Queues ( Queue<T> ) </li></ul><ul><ul><li>Use to implement FIFO (first-in-first-out) behavior </li></ul></ul><ul><ul><li>LinkedList<T> could also work well </li></ul></ul><ul><li>Hash table based dictionary ( Dictionary<K,T> ) </li></ul><ul><ul><li>Use when key-value pairs should be added fast and searched fast by key </li></ul></ul><ul><ul><li>Elements in a hash table have no particular order </li></ul></ul>

30.
Choosing Data Structure (3) <ul><li>Balanced search tree based dictionary ( SortedDictionary<K,T> ) </li></ul><ul><ul><li>Use when key-value pairs should be added fast, searched fast by key and enumerated sorted by key </li></ul></ul><ul><li>Hash table based set ( HashSet<T> ) </li></ul><ul><ul><li>Use to keep a group of unique values, to add and check belonging to the set fast </li></ul></ul><ul><ul><li>Elements are in no particular order </li></ul></ul><ul><li>Search tree based set ( SortedSet<T> ) </li></ul><ul><ul><li>Use to keep a group of ordered unique values </li></ul></ul>

31.
Summary <ul><li>Algorithm complexity is rough estimation of the number of steps performed by given computation </li></ul><ul><ul><li>Complexity can be logarithmic, linear, n log n, square, cubic, exponential, etc. </li></ul></ul><ul><ul><li>Allows to estimating the speed of given code before its execution </li></ul></ul><ul><li>Different data structures have different efficiency on different operations </li></ul><ul><ul><li>The fastest add / find / delete structure is the hash table – O(1) for all these operations </li></ul></ul>

34.
Exercises (2) <ul><li>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. </li></ul><ul><li>Implement a data structure PriorityQueue<T> that provides a fast way to execute the following operations: add element; extract the smallest element. </li></ul><ul><li>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. </li></ul>