IT 4043 Data Structures and Algorithms

Similar documents
Introduction to Analysis of Algorithms

Chapter 11. Analysis of Algorithms

PROGRAM EFFICIENCY & COMPLEXITY ANALYSIS

Introduction to Data Structure

Sorting Pearson Education, Inc. All rights reserved.

[ 11.2, 11.3, 11.4] Analysis of Algorithms. Complexity of Algorithms. 400 lecture note # Overview

Algorithm Analysis. CENG 707 Data Structures and Algorithms

Algorithm efficiency can be measured in terms of: Time Space Other resources such as processors, network packets, etc.

Module 1: Asymptotic Time Complexity and Intro to Abstract Data Types

Analysis of Algorithms Chapter 11. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

9/10/2018 Algorithms & Data Structures Analysis of Algorithms. Siyuan Jiang, Sept

Nested Loops. A loop can be nested inside another loop.

Algorithm Analysis. (Algorithm Analysis ) Data Structures and Programming Spring / 48

Chapter 2: Complexity Analysis

Overview of Data. 1. Array 2. Linked List 3. Stack 4. Queue

Algorithm. Algorithm Analysis. Algorithm. Algorithm. Analyzing Sorting Algorithms (Insertion Sort) Analyzing Algorithms 8/31/2017

CS/ENGRD 2110 Object-Oriented Programming and Data Structures Spring 2012 Thorsten Joachims. Lecture 10: Asymptotic Complexity and

Algorithm Performance. (the Big-O)

Test 1 SOLUTIONS. June 10, Answer each question in the space provided or on the back of a page with an indication of where to find the answer.

Algorithm Efficiency & Sorting. Algorithm efficiency Big-O notation Searching algorithms Sorting algorithms

IT 4043 Data Structures and Algorithms. Budditha Hettige Department of Computer Science

CS 231 Data Structures and Algorithms Fall Algorithm Analysis Lecture 16 October 10, Prof. Zadia Codabux

Computer Algorithms. Introduction to Algorithm

ECE250: Algorithms and Data Structures Midterm Review

Complexity Analysis of an Algorithm

CSCA48 Winter 2018 Week 10:Algorithm Analysis. Marzieh Ahmadzadeh, Nick Cheng University of Toronto Scarborough

Complexity of Algorithms

DATA STRUCTURES AND ALGORITHMS. Asymptotic analysis of the algorithms

CMPT 125: Practice Midterm Answer Key

Scientific Computing. Algorithm Analysis

Lecture 2: Algorithm Analysis

CSE 373 APRIL 3 RD ALGORITHM ANALYSIS

RUNNING TIME ANALYSIS. Problem Solving with Computers-II

Why study algorithms? CS 561, Lecture 1. Today s Outline. Why study algorithms? (II)

Algorithms in Systems Engineering IE172. Midterm Review. Dr. Ted Ralphs

Internal Sorting by Comparison

Complexity of Algorithms. Andreas Klappenecker

Algorithmic Analysis. Go go Big O(h)!

CS 445: Data Structures Final Examination: Study Guide

LECTURE 9 Data Structures: A systematic way of organizing and accessing data. --No single data structure works well for ALL purposes.

Theory and Algorithms Introduction: insertion sort, merge sort

Remember, to manage anything, we need to measure it. So, how do we compare two implementations? ArrayBag vs. LinkedBag Which is more efficient?

Agenda. The worst algorithm in the history of humanity. Asymptotic notations: Big-O, Big-Omega, Theta. An iterative solution

The Running Time of Programs

CS 261 Data Structures. Big-Oh Analysis: A Review

Department of Computer Science and Engineering. CSE 2011: Fundamentals of Data Structures Winter 2009, Section Z

How do we compare algorithms meaningfully? (i.e.) the same algorithm will 1) run at different speeds 2) require different amounts of space

Data Structures and Algorithms. Part 2

Data Structures Lecture 8

Choice of C++ as Language

Analysis of Algorithms. CS 1037a Topic 13

What is an algorithm?

Analysis of Algorithm. Chapter 2

CMPSCI 187: Programming With Data Structures. Lecture 5: Analysis of Algorithms Overview 16 September 2011

csci 210: Data Structures Program Analysis

UNIT 1 ANALYSIS OF ALGORITHMS

4.1 Real-world Measurement Versus Mathematical Models

CSE 332 Spring 2013: Midterm Exam (closed book, closed notes, no calculators)

Algorithm Efficiency & Sorting. Algorithm efficiency Big-O notation Searching algorithms Sorting algorithms

How fast is an algorithm?

Algorithm Analysis. College of Computing & Information Technology King Abdulaziz University. CPCS-204 Data Structures I

CSE 143. Complexity Analysis. Program Efficiency. Constant Time Statements. Big Oh notation. Analyzing Loops. Constant Time Statements (2) CSE 143 1

Algorithm Analysis and Design

Measuring algorithm efficiency

Algorithms and Data Structures

Data Structure Lecture#5: Algorithm Analysis (Chapter 3) U Kang Seoul National University

Computational thinking, problem-solving and programming:

Elementary Sorting Algorithms

Chapter 6 INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS

Checking for duplicates Maximum density Battling computers and algorithms Barometer Instructions Big O expressions. John Edgar 2

6/12/2013. Introduction to Algorithms (2 nd edition) Overview. The Sorting Problem. Chapter 2: Getting Started. by Cormen, Leiserson, Rivest & Stein

Data Structures Question Bank Multiple Choice

CS : Data Structures

Internal Sorting by Comparison

Sorting. Popular algorithms: Many algorithms for sorting in parallel also exist.

Ceng 111 Fall 2015 Week 12b

CS302 Topic: Algorithm Analysis. Thursday, Sept. 22, 2005

namibia UniVERSITY OF SCIEnCE AnD TECHnOLOGY

Computer Science 210 Data Structures Siena College Fall Topic Notes: Complexity and Asymptotic Analysis

CS171 Midterm Exam. October 29, Name:

asymptotic growth rate or order compare two functions, but ignore constant factors, small inputs

Data Structures and Algorithms Chapter 2

You should know the first sum above. The rest will be given if you ever need them. However, you should remember that,, and.

Datatypes. List of data structures. Datatypes ISC

Position Sort. Anuj Kumar Developer PINGA Solution Pvt. Ltd. Noida, India ABSTRACT. Keywords 1. INTRODUCTION 2. METHODS AND MATERIALS

Remember to also pactice: Homework, quizzes, class examples, slides, reading materials.

CSCI 136 Data Structures & Advanced Programming. Lecture 7 Spring 2018 Bill and Jon

O(n): printing a list of n items to the screen, looking at each item once.

Section 05: Solutions

Outline and Reading. Analysis of Algorithms 1

Assignment 1 (concept): Solutions

Table of Contents. Course Minutiae. Course Overview Algorithm Design Strategies Algorithm Correctness Asymptotic Analysis 2 / 32

DATA STRUCTURES AND ALGORITHMS

Algorithm. Lecture3: Algorithm Analysis. Empirical Analysis. Algorithm Performance

CS S-02 Algorithm Analysis 1

CS 103 Unit 8b Slides

Analysis of Algorithms - Introduction -

Module 07: Efficiency

For searching and sorting algorithms, this is particularly dependent on the number of data elements.

CPSC 211, Sections : Data Structures and Implementations, Honors Final Exam May 4, 2001

Transcription:

IT 4043 Data Structures and Algorithms Budditha Hettige Department of Computer Science 1

Syllabus Introduction to DSA Abstract Data Types Arrays List Operation Using Arrays Recursion Stacks Queues Link List Sorting Algorithms Analysis 2

Introduction to Analysis of Algorithms

Introduction to Analysis of Algorithms One aspect of software quality is the efficient use of computer resources : CPU time Memory usage We frequently want to analyse algorithms with respect to execution time Called time complexity analysis For example, to decide which sorting algorithm will take less time to run 1-4 1-4

Time Complexity Analysis of time taken is based on: Problem size (e.g. number of items to sort) Key operations (e.g. comparison of two values) What we want to analyse is the relationship between The size of the problem, n And the time it takes to solve the problem, t(n) Note that t(n) is a function of n, so it depends on the size of the problem 1-5 1-5

Worst, Average, Best Case Worst case analysis: considers the maximum of the time over all inputs of size n Used to find an upper bound on algorithm performance for large problems (large n) Average case analysis: considers the average of the time over all inputs of size n Determines the average (or expected) performance Best case analysis: considers the minimum of the time over all inputs of size n 12-6

Discussion What are some difficulties with average case analysis? Hard to determine Depends on distribution of inputs (they might not be evenly distributed) So, we usually use worst case analysis (why not best case analysis?) 12-7

Growth Functions This t(n) is called a growth function What does a growth function look like? Example of a growth function for some algorithm: t(n) = 15n 2 + 45 n See the next slide to see how t(n) changes as n gets bigger! 1-8 1-8

Example: 15n 2 + 45 n No. of items n 15n 2 45n 15n 2 + 45n 1 15 45 60 2 60 90 150 5 375 225 600 10 1,500 450 1,950 100 150,000 4,500 154,500 1,000 15,000,000 45,000 15,045,000 10,000 1,500,000,000 450,000 1,500,450,000 100,000 150,000,000,000 4,500,000 150,004,500,000 1,000,000 15,000,000,000,000 45,000,000 15,000,045,000,000 1-9

Comparison of Terms in 15n 2 + 45 n When n is small, which term is larger? But, as n gets larger, note that the 15n 2 term grows more quickly than the 45n term Also, the constants 15 and 45 become irrelevant as n increases We say that the n 2 term is dominant in this expression 1-10

Big-O Notation It is not usually necessary to know the exact growth function The key issue is the asymptotic complexity of the function : how it grows as n increases This is determined by the dominant term in the growth function (the term that increases most quickly as n increases) Constants and secondary terms become irrelevant as n increases 1-11 1-11

Big-O Notation The asymptotic complexity of the function is referred to as the order of the algorithm, and is specified by using Big-O notation Example: O(n 2 ) means that the time taken by the algorithm grows like the n 2 function as n increases O(1) means constant time, regardless of the size of the problem Def: f (n) is O(g(n)) if there exist positive numbers c and N such that f (n) cg(n) for all n N 1-12 1-12

Some Growth Functions and Their Asymptotic Complexities Growth Function t(n) = 17 t(n) = 20n - 4 Order O(1) O(n) t(n) = 12n * log 2 n + 100n O(n*log 2 n) t(n) = 3n 2 + 5n - 2 O(n 2 ) t(n) = 2 n + 18n 2 + 3n O(2 n ) 1-13 1-13

Comparison of Some Typical Growth Functions 1200 t(n) = n 3 t(n) = n 2 1000 800 600 t(n) = nlog 2 n 400 200 t(n) = n 10 20 30 40 50 60 70 80 90 100 n 1-14 1-14

Exercise: Asymptotic Complexities Growth Function Order t(n) = 5n 2 + 3n? t(n) = n 3 + log 2 n 4? t(n) = log 2 n * 10n + 5? t(n) = 3n 2 + 3n 3 + 3? t(n) = 2 n + 18n 100? 1-15 1-15

Determining Time Complexity Algorithms frequently contain sections of code that are executed over and over again, i.e. loops So, analysing loop execution is basic to determining time complexity 1-16

Analysing Loop Execution A loop executes a certain number of times (say n), so the time complexity of the loop is n times the time complexity of the body of the loop Example: what is the time complexity of the following loop, in Big-O notation? x = 0; for (int i=0; i<n; i++) x = x + 1; 1-17 1-17

Nested loops: the body of the outer loop includes the inner loop Example: what is the time complexity of the following loop, in Big-O notation? for (int i=0; i<n; i++) { x = x + 1; for (int j=0; j<n; j++) } y = y 1; 1-18 1-18

More Loop Analysis Examples x = 0; for (int i=0; i<n; i=i+2) { x = x + 1; } x = 0; for (int i=1; i<n; i=i*2) { x = x + 1; } 1-19 1-19

More Loop Analysis Examples x = 0; for (int i=0; i<n; i++) for (int j = i, j < n, j ++) { x = x + 1; } 1-20 1-20

Analysis of Stack Operations Stack operations are generally efficient, because they all work on only one end of the collection But which is more efficient: the array implementation or the linked list implementation? 1-21

Analysis of Stack Operations n is the number of items on the stack push operation for ArrayStack: O(1) if array is not full (why?) What would it be if the array is full? (worst case) push operation for LinkedStack: O(1) (why?) pop operation for each? peek operation for each? 1-22

Example Bubble Sort Shell Sort Selection Sort Quick Sort 23

Discussion Suppose we want to perform a sort that is O(n 2 ). What happens if the number of items to be sorted is 100000? Compare this to a sort that is O(n log 2 (n)). Now what can we expect? Is an O(n 3 ) algorithm practical for large n? What about an O(2 n ) algorithm, even for small n? e.g. for a Pentium, runtimes are: n = 30 n = 40 n = 50 n = 60 11 sec. 3 hours 130 days 365 years 12-24