Data Structures Using C++ 2E

Slides:



Advertisements
Similar presentations
D = r  t 1) Two planes take off at the same time, departing in separate directions. One plane travels 3 times as fast as the other plane. After 3 hours,
Advertisements

Switch code for Lab 4.2 switch (input) { /* input is a variable that we will test. */ case 'M': printf("The prefix is equal to 1E6.\n"); break; case 'k':
SI23 Introduction to Computer Graphics
Lecture 11: Algorithms and Time Complexity I Discrete Mathematical Structures: Theory and Applications.
Starter 1 (1-D) A car travels 100 km from city A to city B. If the first half of the distance is driven at 50 km/h and the second half is driven at 100.
DISTANCE: (d=rt).
Speed Distance Time. Intermediate 1. Unit 2..
Data Structures Using C++ 2E The Big-O Notation. Data Structures Using C++ 2E2 Algorithm Analysis: The Big-O Notation Analyze algorithm after design Example.
Pseudo-Random Numbers: Linear Congruential Generators As digital computers are deterministic machines, they are incapable of generating random numbers.
12-Apr-15 Analysis of Algorithms. 2 Time and space To analyze an algorithm means: developing a formula for predicting how fast an algorithm is, based.
The Efficiency of Algorithms Chapter 4 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Time Distance Speed Calculations
P449. p450 Figure 15-1 p451 Figure 15-2 p453 Figure 15-2a p453.
CS 3610/5610N data structures Lecture: complexity analysis Data Structures Using C++ 2E1.
Copyright © Cengage Learning. All rights reserved. CHAPTER 11 ANALYSIS OF ALGORITHM EFFICIENCY ANALYSIS OF ALGORITHM EFFICIENCY.
Formula Practice Find the area for the figure below. 12 m 6 m Answer: #1.
Spring2012 Lecture#10 CSE 246 Data Structures and Algorithms.
Copyright © Cengage Learning. All rights reserved.
Array operations II manipulating arrays and measuring performance.
Section 4.4 Problem Solving Using Systems of Equations.
g = PO D g = × 21 = × g = 12g 12g = 882 ÷12.
Chapter 12 Recursion, Complexity, and Searching and Sorting
Big Oh Algorithms are compared to each other by expressing their efficiency in big-oh notation Big O notation is used in Computer Science to describe the.
Department of Computer Science 1 COSC 2320 Section Room 104 AH 5:30 – 7:00 PM TTh Professor Olin Johnson Office 596 PGH
Investigating Patterns. Vocabulary Variable- a letter used to represent one or more numbers Variable Expression- an expression that contains variables,
Data Structures Using C++ 2E Chapter 9 Searching and Hashing Algorithms.
Software Engineering Review CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science.
Data Structures Using C++ 2E Chapter 1 Software Engineering Principles and C++ Classes.
© 2010 Preston PowerPoints Constant Rate of Change Section 1.7.
Data Structures Using C++ 2E
Notes Over 4.8 Identifying Functions A relation where each input has exactly one output. Function Decide whether the relation is a function. If it is.
Word Problems: Distance, rate and time Type A: Same – Direction of travel A train leaves a train station at 1 pm. It travels at an average rate.
Data Structures Using C++ 2E Chapter 9 Searching and Hashing Algorithms.
Delivery Operations CONDUCTING A SUCCESSFUL PS FORM 3999.
Algorithm Analysis. What is an algorithm ? A clearly specifiable set of instructions –to solve a problem Given a problem –decide that the algorithm is.
Data Structures Using C++ 2E
Algorithm Analysis (Time complexity). Software development cycle -Four phases: 1.Analysis 2.Design Algorithm Design an algorithm to solve the problem.
CISC220 Spring 2010 James Atlas Lecture 07: Big O Notation.
Higher Computing Science 2016 Prelim Revision. Topics to revise Computational Constructs parameter passing (value and reference, formal and actual) sub-programs/routines,
Copyright © 2006 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Quadratic Inequalities ♦ Solve quadratic inequalities graphically ♦ Solve.
Analysis of Algorithms: Methods and Examples CSE 2320 – Algorithms and Data Structures Vassilis Athitsos Modified by Alexandra Stefan University of Texas.
Grandpa had to drive from Iowa to Texas so he could pick up a new horse for the farm. He drives a little faster than Grandma though.
Exploring Computer Science
Asymptotic Notations Algorithms perform f(n) basic operations to accomplish task Identify that function Identify size of problem (n) Count number of operations.
Chapter 1 The Phases of Software Development
Data Structures Using The Big-O Notation 1.
Using Formulae By the end of the lesson, you should have a good understanding of: The usefulness of formulae How to produce a formula How to rearrange.
Objectives Average Rate of Change
Lecture 13 review Explain how distance vector algorithm works.
CHAPTER 2: Analysis of Algorithms
Asymptotic Notations Algorithms perform f(n) basic operations to accomplish task Identify that function Identify size of problem (n) Count number of operations.
Speed Distance Time. Intermediate 1. Unit 2..
Programming and Data Structure
Analyzing an Algorithm Computing the Order of Magnitude Big O Notation
Dots 5 × TABLES MULTIPLICATION.
Dots 5 × TABLES MULTIPLICATION.
Dots 2 × TABLES MULTIPLICATION.
Dots 3 × TABLES MULTIPLICATION.
Summary Simple runtime problems ‘Counting’ instructions
Dots 6 × TABLES MULTIPLICATION.
Proportional and Non-proportional Relationships
Asymptotic Notations Algorithms perform f(n) basic operations to accomplish task Identify that function Identify size of problem (n) Count number of operations.
Dots 2 × TABLES MULTIPLICATION.
Figure 11-1.
CHAPTER 2: Analysis of Algorithms
Dots 4 × TABLES MULTIPLICATION.
Figure Overview.
Dots 3 × TABLES MULTIPLICATION.
Reading Graphs, Tables & Keys
Speed Distance Time. Intermediate 1. Unit 2..
Presentation transcript:

Data Structures Using C++ 2E The Big-O Notation and Array-Based Lists

Algorithm Analysis: The Big-O Notation Analyze algorithm after design Example 50 packages delivered to 50 different houses 50 houses one mile apart, in the same area FIGURE 1-1 Gift shop and each dot representing a house Data Structures Using C++ 2E

Algorithm Analysis: The Big-O Notation (cont’d.) Example (cont’d.) Driver picks up all 50 packages Drives one mile to first house, delivers first package Drives another mile, delivers second package Drives another mile, delivers third package, and so on Distance driven to deliver packages 1+1+1+… +1 = 50 miles Total distance traveled: 50 + 50 = 100 miles FIGURE 1-2 Package delivering scheme Data Structures Using C++ 2E

Algorithm Analysis: The Big-O Notation (cont’d.) Example (cont’d.) Similar route to deliver another set of 50 packages Driver picks up first package, drives one mile to the first house, delivers package, returns to the shop Driver picks up second package, drives two miles, delivers second package, returns to the shop Total distance traveled 2 * (1+2+3+…+50) = 2550 miles FIGURE 1-3 Another package delivery scheme Data Structures Using C++ 2E

Algorithm Analysis: The Big-O Notation (cont’d.) Example (cont’d.) n packages to deliver to n houses, each one mile apart First scheme: total distance traveled 1+1+1+… +n = 2n miles Function of n Second scheme: total distance traveled 2 * (1+2+3+…+n) = 2*(n(n+1) / 2) = n2+n Function of n2 Data Structures Using C++ 2E

Algorithm Analysis: The Big-O Notation (cont’d.) Analyzing an algorithm Count number of operations performed Not affected by computer speed TABLE 1-1 Various values of n, 2n, n2, and n2 + n Data Structures Using C++ 2E

Algorithm Analysis: The Big-O Notation (cont’d.) Example 1-1 Illustrates fixed number of executed operations 1 operation 2 operations 1 operation 1 operation 1 operation 3 operations Total of 9 operations Data Structures Using C++ 2E

Algorithm Analysis: The Big-O Notation Example 1-2 Illustrates dominant operations 2 operations 1 operation 1 operation 1 operation N+1 operations 2N operations N operations N operations 3 operations 1 operation 2 operations 1 operation 3 operation If the while loop executes N times then: 2+1+1+1+5*N + 1 + 3 + 1 + (2 ) + 3 = 5N+(15 ) Data Structures Using C++ 2E

For(i=1; i<=5; i++) Example For(j=1;j<=5; j++) { } Finally: 147 1 op 5 op 6 op For(i=1; i<=5; i++) For(j=1;j<=5; j++) { Cout<<“*”; Sum=sum+j; } For(i=1; i<=n; i++) For(j=1; j<=n; j++) { cout<<“*”; Sum=sum+j; } 30 op 25 op 5 op 25 op 2n+2+ 2n2+2n+ 3n2 = 5n2+ 4n+2 25 op 25 op Data Structures Using C++ 2E

Algorithm Analysis: The Big-O Notation (cont’d.) Search algorithm n: represents list size f(n): count function Number of comparisons in search algorithm c: units of computer time to execute one operation cf(n): computer time to execute f(n) operations Constant c depends computer speed (varies) f(n): number of basic operations (constant) Determine algorithm efficiency Knowing how function f(n) grows as problem size grows Data Structures Using C++ 2E

Algorithm Analysis: The Big-O Notation (cont’d.) TABLE 1-2 Growth rates of various functions Data Structures Using C++ 2E

Algorithm Analysis: The Big-O Notation (cont’d.) TABLE 1-3 Time for f(n) instructions on a computer that executes 1 billion instructions per second Figure 1-4 Growth rate of functions in Table 1-3 Data Structures Using C++ 2E

Algorithm Analysis: The Big-O Notation (cont’d.) Notation useful in describing algorithm behavior Shows how a function f(n) grows as n increases without bound Asymptotic Study of the function f as n becomes larger and larger without bound Examples of functions g(n)=n2 (no linear term) f(n)=n2 + 4n + 20 Data Structures Using C++ 2E

Algorithm Analysis: The Big-O Notation (cont’d.) As n becomes larger and larger Term 4n + 20 in f(n) becomes insignificant Term n2 becomes dominant term TABLE 1-4 Growth rate of n2 and n2 + 4n + 20n Data Structures Using C++ 2E

Algorithm Analysis: The Big-O Notation (cont’d.) If function complexity can be described by complexity of a quadratic function without the linear term We say the function is of O(n2) or Big-O of n2 Let f and g be real-valued functions Assume f and g nonnegative For all real numbers n, f(n) >= 0 and g(n) >= 0 f(n) is Big-O of g(n): written f(n) = O(g(n)) If there exists positive constants c and n0 such that f(n) <= cg(n) for all n >= n0 Data Structures Using C++ 2E

Algorithm Analysis: The Big-O Notation (cont’d.) TABLE 1-5 Some Big-O functions that appear in algorithm analysis Data Structures Using C++ 2E

Array-Based Lists List Length of a list Collection of elements of same type Length of a list Number of elements in the list Many operations may be performed on a list Store a list in the computer’s memory Using an array Data Structures Using C++ 2E

Array-Based Lists (cont’d.) Three variables needed to maintain and process a list in an array The array holding the list elements A variable to store the length of the list Number of list elements currently in the array A variable to store array size Maximum number of elements that can be stored in the array Desirable to develop generic code Used to implement any type of list in a program Make use of templates Data Structures Using C++ 2E

Array-Based Lists (cont’d.) Define class implementing list as an abstract data type (ADT) FIGURE 3-29 UML class diagram of the class arrayListType Data Structures Using C++ 2E

Array-Based Lists (cont’d.) Definitions of functions isEmpty, isFull, listSize and maxListSize Data Structures Using C++ 2E

Array-Based Lists (cont’d.) Template print (outputs the elements of the list) and template isItemAtEqual Data Structures Using C++ 2E

Array-Based Lists (cont’d.) Template insertAt Data Structures Using C++ 2E

Array-Based Lists (cont’d.) Template insertEnd and template removeAt Data Structures Using C++ 2E

Array-Based Lists (cont’d.) Template replaceAt and template clearList Data Structures Using C++ 2E

Array-Based Lists (cont’d.) Definition of the constructor and the destructor Data Structures Using C++ 2E

Array-Based Lists (cont’d.) Copy constructor Called when object passed as a (value) parameter to a function Called when object declared and initialized using the value of another object of the same type Copies the data members of the actual object into the corresponding data members of the formal parameter and the object being created Data Structures Using C++ 2E

Array-Based Lists (cont’d.) Copy constructor (cont’d.) Definition Data Structures Using C++ 2E

Array-Based Lists (cont’d.) Overloading the assignment operator Definition of the function template Data Structures Using C++ 2E

Array-Based Lists (cont’d.) Searching for an element Linear search example: determining if 27 is in the list Definition of the function template FIGURE 3-32 List of seven elements Data Structures Using C++ 2E

Array-Based Lists (cont’d.) Inserting an element Data Structures Using C++ 2E

Array-Based Lists (cont’d.) Removing an element Data Structures Using C++ 2E

Array-Based Lists (cont’d.) TABLE 3-1 Time complexity of list operations Data Structures Using C++ 2E