Data Structures Using C++ 2E - The Big-O Notation PDF

Document Details

ProblemFreeCarolingianArt

Uploaded by ProblemFreeCarolingianArt

Tags

algorithm analysis big-o notation data structures computer science

Summary

This document provides a detailed explanation of the Big-O notation used in algorithm analysis, focusing on its application in the context of data structures and algorithms, especially in the C++ programming language. It includes examples to illustrate the concept and tables outlining different growth rates.

Full Transcript

Data Structures Using C++ 2E The Big-O Notation 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...

Data Structures Using C++ 2E The Big-O Notation 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 2 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 3 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 4 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 5 Algorithm Analysis: The Big-O Notation (cont’d.) TABLE 1-1 Various values of n, 2n, n2, and n2 + n (n) and (2n) are close, so we magnify (n) (n2) and (n2 + n) are close, so we magnify (n 2) 6 Data Structures Using C++ 2E Algorithm Analysis: The Big-O Notation (cont’d.) TABLE 1-1 Various values of n, 2n, n2, and n2 + n When n becomes too large, n and n 2 becomes very different Data Structures Using C++ 2E 7 Algorithm Analysis: The Big-O Notation (cont’d.) Analyzing an algorithm – Count number of operations performed Not affected by computer speed Data Structures Using C++ 2E 8 Algorithm Analysis: The Big-O Notation (cont’d.) Example 1-1 – Illustrates fixed number of executed operations 1 operation Only one of them will be executed 2 operations 1 operation 1 operation 1 operation 3 operations Total of 8 operations Data Structures Using C++ 2E Algorithm Analysis: The Big-O Notation Example 1-2 Illustrates dominant operations 2 operations 1 operation 1 operation N times the condition is TRUE + 1 time the condition is FALSE 1 operation N+1 operations 2N operations Executed while the cond. N operations is TRUE N operations 3 operations Only one of them will be 1 operation executed, take the max: 2 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 Algorithm Analysis: The Big-O Notation How to count for loops Times the cond. Is TRUE + 1 (when the condition is 1 op FALSE for (initialization; condition; increase){ Times the cond. statement1; Is TRUE statement2; Times the cond. Is TRUE... } Data Structures Using C++ 2E 11 Example Finally: 147 1 op 6 op 5 op for(i=1; i

Use Quizgecko on...
Browser
Browser