Topic Number 2 Efficiency – Complexity Algorithm Analysis


Typical Big O Functions – "Grades"



Download 1,07 Mb.
bet4/8
Sana14.06.2022
Hajmi1,07 Mb.
#669861
1   2   3   4   5   6   7   8
Bog'liq
topic2Efficiency Complexity AlgorithmAnalysis (1)

Typical Big O Functions – "Grades"

  • CS 314
  • Function
  • Common Name
  • N!
  • factorial
  • 2N
  • Exponential
  • Nd, d > 3
  • Polynomial
  • N3
  • Cubic
  • N2
  • Quadratic
  • N N
  • N Square root N
  • N log N
  • N log N
  • N
  • Linear
  • N
  • Root - n
  • log N
  • Logarithmic
  • 1
  • Constant
  • Running time grows 'slowly' with more input.
  • Running time grows 'quickly' with more input.

Clicker 4

  • Which of the following is true? Recall T(N)total = 3N + 4
  • Method total is O(N1/2)
  • Method total is O(N)
  • Method total is O(N2)
  • Two of A – C are correct
  • All of three of A – C are correct
  • CS 314
  • Efficiency - Complexity

Showing Order More Formally …

  • Show 10N2 + 15N is O(N2)
  • Break into terms.
  • 10N2 < 10N2
  • 15N < 15N2 for N > 1 (Now add)
  • 10N2 + 15N < 10N2 + 15N2 for N > 1
  • 10N2 + 15N < 25N2 for N > 1
  • c = 25, N0 = 1
  • Note, the choices for c and N0 are not unique.
  • CS 314
  • Efficiency - Complexity

Dealing with other methods

  • CS 314
  • Efficiency - Complexity
  • What do I do about method calls?
  • double sum = 0.0;
  • for (int i = 0; i < n; i++)
  • sum += Math.sqrt(i);
  • Long way
    • go to that method or constructor and count statements
  • Short way
    • substitute the simplified Big O function for that method.
    • if Math.sqrt is constant time, O(1), simply count sum += Math.sqrt(i); as one statement.

Dealing With Other Methods

  • CS 314
  • Efficiency - Complexity
  • public int foo(int[] data) {
  • int total = 0; for (int i = 0; i < data.length; i++)
  • total += countDups(data[i], data);
  • return total;
  • }
  • // method countDups is O(N) where N is the
  • // length of the array it is passed
  • Clicker 5, What is the Big O of foo?
  • O(1) B. O(N) C. O(NlogN)
  • D. O(N2) E. O(N!)

Independent Loops

  • // from the Matrix class
  • public void scale(int factor) {
  • for (int r = 0; r < numRows(); r++)
  • for (int c = 0; c < numCols(); c++)
  • iCells[r][c] *= factor;
  • }
  • Assume numRows() = numCols() = N.
  • In other words, a square matrix. numRows and numCols are O(1)
  • What is the T(N)? Clicker 6, What is the Order?
  • O(1) B. O(N) C. O(NlogN)
  • D. O(N2) E. O(N!)
  • Bonus question. What if numRows is O(N)?

Just Count Loops, Right?

  • CS 314
  • Efficiency - Complexity
  • // Assume mat is a 2d array of booleans.
  • // Assume mat is square with N rows,
  • // and N columns. public static void count(boolean[][] mat, int row, int col) {
  • int numThings = 0;
  • for (int r = row - 1; r <= row + 1; r++)
  • for (int c = col - 1; c <= col + 1; c++)
  • if (mat[r][c])
  • numThings++;
  • Clicker 7, What is the order of the method count?
  • O(1) B. O(N0.5) C. O(N) D. O(N2) E. O(N3)

It is Not Just Counting Loops

  • CS 314
  • Efficiency - Complexity
  • // "Unroll" the loop of method count:
  • int numThings = 0;
  • if (mat[r-1][c-1]) numThings++;
  • if (mat[r-1][c]) numThings++;
  • if (mat[r-1][c+1]) numThings++;
  • if (mat[r][c-1]) numThings++;
  • if (mat[r][c]) numThings++;
  • if (mat[r][c+1]) numThings++;
  • if (mat[r+1][c-1]) numThings++;
  • if (mat[r+1][c]) numThings++;
  • if (mat[r+1][c+1]) numThings++;

Just Count Loops, Right?

  • Clicker 8, What is the order of method mystery?
  • O(1) B. O(N0.5) C. O(N) D. O(N2) E. O(N3)
  • N = data.length

Sidetrack, the logarithm

  • CS 314
  • Efficiency - Complexity
  • Thanks to Dr. Math
  • 32 = 9
  • likewise log3 9 = 2
    • "The log to the base 3 of 9 is 2."
  • The way to think about log is:
    • "the log to the base x of y is the number you can raise x to to get y."
    • Say to yourself "The log is the exponent." (and say it over and over until you believe it.)
    • In CS we work with base 2 logs, a lot
  • log2 32 = ? log2 8 = ? log2 1024 = ? log10 1000 = ?

When Do Logarithms Occur

  • The base of the log is typically not included as we can switch from one base to another by multiplying by a constant factor.
  • Algorithms tend to have a logarithmic term when they use a divide and conquer technique
  • the size of the data set keeps getting divided by 2
  • public int foo(int n) { // pre n > 0 int total = 0; while (n > 0) { n = n / 2; total++; } return total; }
  • Clicker 9, What is the order of the above code?
  • O(1) B. O(logN) C. O(N)
  • D. O(Nlog N) E. O(N2)

Download 1,07 Mb.

Do'stlaringiz bilan baham:
1   2   3   4   5   6   7   8




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish