22
Lecture 11: Algorithms and Time Complexity I Discrete Mathematical Structures: Theory and Applications

Lecture 11: Algorithms and Time Complexity I Discrete Mathematical Structures: Theory and Applications

Embed Size (px)

Citation preview

Page 1: Lecture 11: Algorithms and Time Complexity I Discrete Mathematical Structures: Theory and Applications

Lecture 11: Algorithms and Time Complexity I

Discrete Mathematical Structures:

Theory and Applications

Page 2: Lecture 11: Algorithms and Time Complexity I Discrete Mathematical Structures: Theory and Applications

Discrete Mathematical Structures: Theory and Applications 2

Learning Objectives

Learn about algorithm analysis

Become familiar with big-O, omega, and theta notations

Explore various algorithms and their time complexity

Page 3: Lecture 11: Algorithms and Time Complexity I Discrete Mathematical Structures: Theory and Applications

Discrete Mathematical Structures: Theory and Applications 3

Algorithm Analysis

A gift company that delivers calculates the shortest distance from the shop to a particular destination and hands the route to the driver.

50 packages are to be delivered to 50 different houses.

The 50 houses are located 1 mile apart and are in the same area.

The first house is also 1 mile from the shop.

Page 4: Lecture 11: Algorithms and Time Complexity I Discrete Mathematical Structures: Theory and Applications

Discrete Mathematical Structures: Theory and Applications 4

Algorithm Analysis

Each dot represents a house and the distance between houses is 1 mile, as shown in Figure 9.2.

Page 5: Lecture 11: Algorithms and Time Complexity I Discrete Mathematical Structures: Theory and Applications

Discrete Mathematical Structures: Theory and Applications 5

Algorithm Analysis

To deliver 50 packages to their destinations, one of the drivers picks up all 50 packages, drives 1 mile to the first house, and delivers the first package.

Then the driver drives another mile and delivers the second package, drives another mile and delivers the third package, and so on.

Page 6: Lecture 11: Algorithms and Time Complexity I Discrete Mathematical Structures: Theory and Applications

Discrete Mathematical Structures: Theory and Applications 6

Algorithm Analysis

Using this scheme, the distance driven to deliver the packages is:

1 + 1 + 1 +· · ·+ 1 = 50 miles

Therefore, the total distance traveled to deliver the packages and then return to the shop is:

50 + 50 = 100 miles

Page 7: Lecture 11: Algorithms and Time Complexity I Discrete Mathematical Structures: Theory and Applications

Discrete Mathematical Structures: Theory and Applications 7

Algorithm Analysis

Another driver is given a similar route to deliver another set of 50 packages. The driver looks at the route and delivers the packages as follows:

The driver picks up the first package, drives 1 mile to the first house, delivers the package, and then returns to the shop.

Next, the driver picks up the second package, drives 2 miles, delivers the package, and returns to the shop.

The driver then picks up the third package, drives 3 miles, delivers the package, and returns to the shop.

Page 8: Lecture 11: Algorithms and Time Complexity I Discrete Mathematical Structures: Theory and Applications

Discrete Mathematical Structures: Theory and Applications 8

Algorithm Analysis

This driver delivers only one package at a time. After delivering a package, the driver comes back to the shop to pick up and deliver the second package. Using this scheme, the total distance traveled is: 2 · (1 + 2 + 3+· · ·+ 50) = 2550 miles

Now suppose that there are n packages to be delivered to n houses, and each house is 1 mile apart as shown in Figure 9.2. If the packages are delivered using the first scheme, the following equation gives the total distance traveled: 1 + 1+· · ·+1 + n = 2n (9.1)

Page 9: Lecture 11: Algorithms and Time Complexity I Discrete Mathematical Structures: Theory and Applications

Discrete Mathematical Structures: Theory and Applications 9

Algorithm Analysis

Page 10: Lecture 11: Algorithms and Time Complexity I Discrete Mathematical Structures: Theory and Applications

Discrete Mathematical Structures: Theory and Applications 10

Algorithm Analysis

When analyzing a particular algorithm, count the number of operations that the algorithm executes.

Focus on the number of operations, not on the actual computer time to execute the algorithm. This is because a given algorithm can be implemented on a variety of computers and the speed of the computer can affect the execution time.

However, the number of operations performed by the algorithm would be the same on each computer.

Page 11: Lecture 11: Algorithms and Time Complexity I Discrete Mathematical Structures: Theory and Applications

Discrete Mathematical Structures: Theory and Applications 11

Page 12: Lecture 11: Algorithms and Time Complexity I Discrete Mathematical Structures: Theory and Applications

Discrete Mathematical Structures: Theory and Applications 12

Page 13: Lecture 11: Algorithms and Time Complexity I Discrete Mathematical Structures: Theory and Applications

Discrete Mathematical Structures: Theory and Applications 13

Page 14: Lecture 11: Algorithms and Time Complexity I Discrete Mathematical Structures: Theory and Applications

Discrete Mathematical Structures: Theory and Applications 14

Page 15: Lecture 11: Algorithms and Time Complexity I Discrete Mathematical Structures: Theory and Applications

Discrete Mathematical Structures: Theory and Applications 15

Algorithm Analysis

Page 16: Lecture 11: Algorithms and Time Complexity I Discrete Mathematical Structures: Theory and Applications

Discrete Mathematical Structures: Theory and Applications 16

Page 17: Lecture 11: Algorithms and Time Complexity I Discrete Mathematical Structures: Theory and Applications

Discrete Mathematical Structures: Theory and Applications 17

Page 18: Lecture 11: Algorithms and Time Complexity I Discrete Mathematical Structures: Theory and Applications

Discrete Mathematical Structures: Theory and Applications 18

Page 19: Lecture 11: Algorithms and Time Complexity I Discrete Mathematical Structures: Theory and Applications

Discrete Mathematical Structures: Theory and Applications 19

Algorithm Analysis

Page 20: Lecture 11: Algorithms and Time Complexity I Discrete Mathematical Structures: Theory and Applications

Discrete Mathematical Structures: Theory and Applications 20

Page 21: Lecture 11: Algorithms and Time Complexity I Discrete Mathematical Structures: Theory and Applications

Discrete Mathematical Structures: Theory and Applications 21

Page 22: Lecture 11: Algorithms and Time Complexity I Discrete Mathematical Structures: Theory and Applications

Discrete Mathematical Structures: Theory and Applications 22