Top Banner
Complexity Bryce Boe 2013/10/21 CS24, Fall 2013
30

Complexity Bryce Boe 2013/10/21 CS24, Fall 2013. Outline Compiler Review Project 1 Questions Complexity (Big-O) C++?

Jan 05, 2016

Download

Documents

Mitchell Wilcox
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Complexity Bryce Boe 2013/10/21 CS24, Fall 2013. Outline Compiler Review Project 1 Questions Complexity (Big-O) C++?

Complexity

Bryce Boe2013/10/21

CS24, Fall 2013

Page 2: Complexity Bryce Boe 2013/10/21 CS24, Fall 2013. Outline Compiler Review Project 1 Questions Complexity (Big-O) C++?

Outline

• Compiler Review• Project 1 Questions• Complexity (Big-O)• C++?

Page 3: Complexity Bryce Boe 2013/10/21 CS24, Fall 2013. Outline Compiler Review Project 1 Questions Complexity (Big-O) C++?

COMPILER REVIEW

Page 4: Complexity Bryce Boe 2013/10/21 CS24, Fall 2013. Outline Compiler Review Project 1 Questions Complexity (Big-O) C++?

What does the following produce?

clang foobar.c

a.out(executable)

Page 5: Complexity Bryce Boe 2013/10/21 CS24, Fall 2013. Outline Compiler Review Project 1 Questions Complexity (Big-O) C++?

What does the following produce?

clang -c foobar.c

foobar.o(object file)

Page 6: Complexity Bryce Boe 2013/10/21 CS24, Fall 2013. Outline Compiler Review Project 1 Questions Complexity (Big-O) C++?

What does the following produce?

clang foobar.o

a.out(executable)

Page 7: Complexity Bryce Boe 2013/10/21 CS24, Fall 2013. Outline Compiler Review Project 1 Questions Complexity (Big-O) C++?

What does the following produce?

clang foobar.c blah.c

a.out(executable)

Page 8: Complexity Bryce Boe 2013/10/21 CS24, Fall 2013. Outline Compiler Review Project 1 Questions Complexity (Big-O) C++?

What does the following produce?

clang -g foobar.c

a.out(executable, debuggable)

Page 9: Complexity Bryce Boe 2013/10/21 CS24, Fall 2013. Outline Compiler Review Project 1 Questions Complexity (Big-O) C++?

What does the following produce?

clang foobar.c -o foo

foo(executable)

Page 10: Complexity Bryce Boe 2013/10/21 CS24, Fall 2013. Outline Compiler Review Project 1 Questions Complexity (Big-O) C++?

PROJECT 1 INFO

Page 11: Complexity Bryce Boe 2013/10/21 CS24, Fall 2013. Outline Compiler Review Project 1 Questions Complexity (Big-O) C++?

Resubmissions until 2PM Wednesday

• Keep working on project 1– Getting through it completely will help

tremendously on the other projects• I have an office hour tomorrow• Don’t forget about lab 4 (due Tuesday night)• We’ll go over a solution on Wednesday

Page 12: Complexity Bryce Boe 2013/10/21 CS24, Fall 2013. Outline Compiler Review Project 1 Questions Complexity (Big-O) C++?

COMPLEXITY

Page 13: Complexity Bryce Boe 2013/10/21 CS24, Fall 2013. Outline Compiler Review Project 1 Questions Complexity (Big-O) C++?

Space v. Time

• Space complexity is how much storage relative to the input does an algorithm require

• Time complexity is how many computations relative to the input does an algorithm require

• In computing we often trade space for time or time for space to get the desired performance

Page 14: Complexity Bryce Boe 2013/10/21 CS24, Fall 2013. Outline Compiler Review Project 1 Questions Complexity (Big-O) C++?

Think about it

• How much (extra) space is required to compute the sum of a list of numbers?

constant (perhaps 1 int)

Page 15: Complexity Bryce Boe 2013/10/21 CS24, Fall 2013. Outline Compiler Review Project 1 Questions Complexity (Big-O) C++?

Think about it

• How much time is required to compute the sum of a list of numbers?

linear(must sum up each)

Page 16: Complexity Bryce Boe 2013/10/21 CS24, Fall 2013. Outline Compiler Review Project 1 Questions Complexity (Big-O) C++?

Think about it

• How much (extra) space is required to sort a list of numbers?

• How much time?

varies depending on implementation

Page 17: Complexity Bryce Boe 2013/10/21 CS24, Fall 2013. Outline Compiler Review Project 1 Questions Complexity (Big-O) C++?

Think about it

• How much time is required to see if an item is contained in a sorted list?

logarithmic

Page 18: Complexity Bryce Boe 2013/10/21 CS24, Fall 2013. Outline Compiler Review Project 1 Questions Complexity (Big-O) C++?

Big-O

• In complexity analysis we typically care about the worst-case

• We also consider the input as it grows to infinity

• We refer to this worst-case as Big-O, or O(?)

Page 19: Complexity Bryce Boe 2013/10/21 CS24, Fall 2013. Outline Compiler Review Project 1 Questions Complexity (Big-O) C++?

Computing Big-O

• If something has an actual running time function of:

• T(n) = 2n3 + 100n2 + 1024n + 10trillion• O(T(n)) = O(n3)

• Consider only the fasted growing term, and remove any factors.

Page 20: Complexity Bryce Boe 2013/10/21 CS24, Fall 2013. Outline Compiler Review Project 1 Questions Complexity (Big-O) C++?

Common Ordered Complexities

• O(1) – constant• O(log(n)) – logarithmic• O(n) – linear• O(nlog(n)) – linearithmic• O(n2) – quadratic• O(2n) – exponential• O(n!) – factorial

Page 21: Complexity Bryce Boe 2013/10/21 CS24, Fall 2013. Outline Compiler Review Project 1 Questions Complexity (Big-O) C++?

O(?)

int a[1024]; // assume allocated and not static

if (a[0] == a[1])return a[2];

elsereturn a[0];

O(1)

Page 22: Complexity Bryce Boe 2013/10/21 CS24, Fall 2013. Outline Compiler Review Project 1 Questions Complexity (Big-O) C++?

O(?)

int a[4]; // assume allocated and not staticint n = sizeof(a) / sizeof(int);int sum = 0;for (int i = 0; i < n; ++i)

sum += a[i];return sum;

O(n)

Page 23: Complexity Bryce Boe 2013/10/21 CS24, Fall 2013. Outline Compiler Review Project 1 Questions Complexity (Big-O) C++?

O(?)

int a[4]; // assume allocated

return a[0] + a[1] + a[2] + a[3];

O(1)

Page 24: Complexity Bryce Boe 2013/10/21 CS24, Fall 2013. Outline Compiler Review Project 1 Questions Complexity (Big-O) C++?

O(?)

int a[1024]; // assume allocatedint n = sizeof(a) / sizeof(int);int dups = 0;for (int i = 0; i < n; ++i)

for (int j = 0; j < n; ++j)if (a[i] == a[j])

++dups;

O(n2)

Page 25: Complexity Bryce Boe 2013/10/21 CS24, Fall 2013. Outline Compiler Review Project 1 Questions Complexity (Big-O) C++?

O(?)

int a[1024]; // assume allocatedint n = sizeof(a) / sizeof(int);int dups = 0;for (int i = 0; i < n; ++i)

for (int j = i; j < n; ++j)if (a[i] == a[j])

++dups;

O(n2)

Page 26: Complexity Bryce Boe 2013/10/21 CS24, Fall 2013. Outline Compiler Review Project 1 Questions Complexity (Big-O) C++?

C++ INTRODUCTION

Page 27: Complexity Bryce Boe 2013/10/21 CS24, Fall 2013. Outline Compiler Review Project 1 Questions Complexity (Big-O) C++?

Why C++?

• Problems with C– Has a single global namespace– Cannot use the same name for functions with

different types (e.g., min(int, int) and min(double, double)) – called overloading

– Difficult to minimize source-code repetition for similar functions with different types

Page 28: Complexity Bryce Boe 2013/10/21 CS24, Fall 2013. Outline Compiler Review Project 1 Questions Complexity (Big-O) C++?

Some Differences

• #include <stdio.h> #include <iostream>– Or if you want fprintf, etc use #include <cstdio>

• printf(“Hello\n”); cout << “Hello\n”;• Rather than defining a struct which only

contains data, define a class which contains data and methods on the data

• throw exceptions rather than use return values to represent error cases

Page 29: Complexity Bryce Boe 2013/10/21 CS24, Fall 2013. Outline Compiler Review Project 1 Questions Complexity (Big-O) C++?

Classes

• Provide encapsulation– Combining a number of items, such as variables

and functions, into a single package, such as an object of some class (or instance of the class)

Page 30: Complexity Bryce Boe 2013/10/21 CS24, Fall 2013. Outline Compiler Review Project 1 Questions Complexity (Big-O) C++?

Scope Resolution Operator

• ClassName::method_name• Used to identify the scope, class in this case,

that the method belongs to as there may be more than 1 instance of method_name

• Scope resolution isn’t necessary if you are also a member of that class