Algorithms Complexity and Data Structures Efficiency

Slides:



Advertisements
Similar presentations
Computational Complexity, Choosing Data Structures Svetlin Nakov Telerik Corporation
Advertisements

Fall 2006CENG 7071 Algorithm Analysis. Fall 2006CENG 7072 Algorithmic Performance There are two aspects of algorithmic performance: Time Instructions.
Analysis of Algorithms (Chapter 4)
Analysis of Algorithms1 Estimate the running time Estimate the memory space required. Time and space depend on the input size.
Elementary Data Structures and Algorithms
Abstract Data Types (ADTs) Data Structures The Java Collections API
Overview of Data Structures and Basic Algorithms. Computational Complexity. Asymptotic Notation Svetlin Nakov Telerik Software Academy academy.telerik.com.
COMP s1 Computing 2 Complexity
Analysis of Performance
Program Performance & Asymptotic Notations CSE, POSTECH.
Week 2 CS 361: Advanced Data Structures and Algorithms
Mathematics Review and Asymptotic Notation
Analysis of Algorithms
Analysis of Algorithms These slides are a modified version of the slides used by Prof. Eltabakh in his offering of CS2223 in D term 2013.
Algorithm Analysis (Algorithm Complexity). Correctness is Not Enough It isn’t sufficient that our algorithms perform the required tasks. We want them.
Computational Complexity, Choosing Data Structures Svetlin Nakov Telerik Corporation
Program Efficiency & Complexity Analysis. Algorithm Review An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm.
Data Structure Introduction.
Java Methods Big-O Analysis of Algorithms Object-Oriented Programming
Computational Complexity of Fundamental Data Structures, Choosing a Data Structure.
E.G.M. PetrakisAlgorithm Analysis1  Algorithms that are equally correct can vary in their utilization of computational resources  time and memory  a.
SoftUni Team Technical Trainers Software University Data Structures, Algorithms and Complexity Analyzing Algorithm Complexity. Asymptotic.
BITS Pilani Pilani Campus Data Structure and Algorithms Design Dr. Maheswari Karthikeyan Lecture1.
Announcement We will have a 10 minutes Quiz on Feb. 4 at the end of the lecture. The quiz is about Big O notation. The weight of this quiz is 3% (please.
GC 211:Data Structures Week 2: Algorithm Analysis Tools Slides are borrowed from Mr. Mohammad Alqahtani.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Algorithm efficiency Prudence Wong.
Computational Complexity, Choosing Data Structures Svetlin Nakov Telerik Software Academy Manager Technical Trainer
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Algorithm efficiency Prudence Wong
Data Structures I (CPCS-204) Week # 2: Algorithm Analysis tools Dr. Omar Batarfi Dr. Yahya Dahab Dr. Imtiaz Khan.
Algorithm Analysis 1.
CMPT 438 Algorithms.
Analysis of Algorithms
Design and Analysis of Algorithms
Introduction to Analysis of Algorithms
Analysis of Algorithms
COMP9024: Data Structures and Algorithms
Analysis of Algorithms
GC 211:Data Structures Week 2: Algorithm Analysis Tools
COMP108 Algorithmic Foundations Algorithm efficiency
Combining Data Structures
Introduction to Algorithms
GC 211:Data Structures Algorithm Analysis Tools
Analysis of Algorithms
Analysis of Algorithms
DATA STRUCTURES Introduction: Basic Concepts and Notations
Complexity Analysis.
Teach A level Computing: Algorithms and Data Structures
Chapter 2 Fundamentals of the Analysis of Algorithm Efficiency
Algorithm design and Analysis
GC 211:Data Structures Algorithm Analysis Tools
Analysis of Algorithms
Introduction to Algorithms Analysis
Algorithm Efficiency Chapter 10.
Analysis of Algorithms
Data Structures Review Session
Analysis of Algorithms
Analysis of Algorithms
Analysis of Algorithms
Chapter 2.
Analysis of Algorithms
Algorithm Analysis Bina Ramamurthy CSE116A,B.
GC 211:Data Structures Algorithm Analysis Tools
Fundamentals of the Analysis of Algorithm Efficiency
Introduction to Data Structures
Analysis of Algorithms
At the end of this session, learner will be able to:
CS210- Lecture 2 Jun 2, 2005 Announcements Questions
Analysis of Algorithms
Analysis of Algorithms
Presentation transcript:

Algorithms Complexity and Data Structures Efficiency Computational Complexity, Choosing Data Structures Svetlin Nakov Telerik Corporation www.telerik.com

Table of Contents Algorithms Complexity and Asymptotic Notation * Table of Contents Algorithms Complexity and Asymptotic Notation Time and Memory Complexity Mean, Average and Worst Case Fundamental Data Structures – Comparison Arrays vs. Lists vs. Trees vs. Hash-Tables Choosing Proper Data Structure (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

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

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 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

Algorithmic Complexity What to measure? Memory Time Number of steps Number of particular operations Number of disk operations Number of network packets Asymptotic complexity (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

Time Complexity Worst-case Average-case Best-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 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

Time Complexity – Example Sequential search in a list of size n Worst-case: n comparisons Best-case: 1 comparison Average-case: n/2 comparisons The algorithm runs in linear time Linear number of operations … n (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

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

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 Examples: 3 * n2 + n/2 + 12 ∈ O(n2) 4*n*log2(3*n+1) + 2*n-1 ∈ O(n * log n) O(g(n)) = {f(n): there exist positive constants c and n0 such that f(n) <= c*g(n) for all n >= n0} (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

Typical Complexities Complexity Notation Description constant O(1) Constant number of operations, not depending on the input data size, e.g. n = 1 000 000  1-2 operations logarithmic O(log n) Number of operations propor-tional of log2(n) where n is the size of the input data, e.g. n = 1 000 000 000  30 operations linear O(n) Number of operations proportional to the input data size, e.g. n = 10 000  5 000 operations

Typical Complexities (2) Complexity Notation Description quadratic O(n2) Number of operations proportional to the square of the size of the input data, e.g. n = 500  250 000 operations cubic O(n3) Number of operations propor-tional to the cube of the size of the input data, e.g. n = 200  8 000 000 operations exponential O(2n), O(kn), O(n!) Exponential number of operations, fast growing, e.g. n = 20  1 048 576 operations

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

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

Polynomial Algorithms A polynomial-time algorithm is one whose worst-case time complexity is bounded above by a polynomial function of its input size 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 W(n) ∈ O(p(n)) (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

Analyzing Complexity of Algorithms Examples (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

Complexity Examples Runs in O(n) where n is the size of the array 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 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

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 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

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 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

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 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

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 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

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 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

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 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

Complexity Examples (8) decimal Fibonacci(int n) { if (n == 0) return 1; else if (n == 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 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

Comparing Data Structures Examples (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

Data Structures Efficiency Add Find Delete Get-by-index Array (T[]) O(n) O(1) Linked list (LinkedList<T>) Resizable array list (List<T>) Stack (Stack<T>) - Queue (Queue<T>)

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

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>)

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

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

* 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 (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

Algorithms Complexity and Data Structures Efficiency ? ? ? ? ? Questions? ? ? ? ? ? http://academy.telerik.com

Exercises A text file students.txt holds information about students and their courses in the following format: 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: Kiril | Ivanov | C# Stefka | Nikolova | SQL Stela | Mineva | Java Milena | Petrova | C# Ivan | Grigorov | C# Ivan | Kolev | SQL C#: Ivan Grigorov, Kiril Ivanov, Milena Petrova Java: Stela Mineva SQL: Ivan Kolev, Stefka Nikolova

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

Exercises (3) A text file phones.txt holds information about people, their town and phone number: 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 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