cs261 3-bigo 2cs.pugetsound.edu/~dchiu/cs261/notes/cs261_3-bigo_2.pdfcsci 261: computer science ii -...

22
CSCI 261: Computer Science II - 3 - Complexity Analysis and the Big-O Notation Outline Modeling Running Time of Algorithm Formalizing Complexity: The Big-O Notation In-Class Exercises A Potential Pitfall Conclusion 25

Upload: others

Post on 24-Mar-2020

11 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS261 3-bigO 2cs.pugetsound.edu/~dchiu/cs261/notes/CS261_3-bigO_2.pdfCSCI 261: Computer Science II - 3 - Complexity Analysis and the Big-O Notation ‣ Classify algorithms in terms

CSCI 261: Computer Science II - 3 - Complexity Analysis and the Big-O Notation

Outline

‣ Modeling Running Time of Algorithm

‣ Formalizing Complexity: The Big-O Notation

‣ In-Class Exercises

‣ A Potential Pitfall

‣ Conclusion

25

Page 2: CS261 3-bigO 2cs.pugetsound.edu/~dchiu/cs261/notes/CS261_3-bigO_2.pdfCSCI 261: Computer Science II - 3 - Complexity Analysis and the Big-O Notation ‣ Classify algorithms in terms

CSCI 261: Computer Science II - 3 - Complexity Analysis and the Big-O Notation

‣ Classify algorithms in terms of growth rate of running-time• Want to say things like, "My solution runs in linear-time."

26

Yardstick

Page 3: CS261 3-bigO 2cs.pugetsound.edu/~dchiu/cs261/notes/CS261_3-bigO_2.pdfCSCI 261: Computer Science II - 3 - Complexity Analysis and the Big-O Notation ‣ Classify algorithms in terms

CSCI 261: Computer Science II - 3 - Complexity Analysis and the Big-O Notation

Silly Example

‣ How many simple statements are executed?

27

public static void silly(int num) { for (int i = 0; i < num; i++) { for (int j = 0; j < num; j++) { System.out.println("Hello!"); } }

for (int k = 0; k < num; k++) { System.out.println("I like the number 1"); System.out.println("I like the number 2"); System.out.println("I like the number 3"); System.out.println("I like the number 4"); System.out.println("I like the number 5"); }

System.out.println("I also like the number 6"); System.out.println("I also like the number 7"); // (22 statements omitted...) System.out.println("Finally, I like the number 30"); }

T (n) = n2 + 5n+ 25????

Page 4: CS261 3-bigO 2cs.pugetsound.edu/~dchiu/cs261/notes/CS261_3-bigO_2.pdfCSCI 261: Computer Science II - 3 - Complexity Analysis and the Big-O Notation ‣ Classify algorithms in terms

CSCI 261: Computer Science II - 3 - Complexity Analysis and the Big-O Notation

‣ Let's plot silly()'s running time,

Classify Runtime by Rate of Growth

28

T (n) = n2 + 5n+ 25

Runn

ing

Tim

e

Input size (n)

T (n) = n2 + 5n+ 25

Page 5: CS261 3-bigO 2cs.pugetsound.edu/~dchiu/cs261/notes/CS261_3-bigO_2.pdfCSCI 261: Computer Science II - 3 - Complexity Analysis and the Big-O Notation ‣ Classify algorithms in terms

CSCI 261: Computer Science II - 3 - Complexity Analysis and the Big-O Notation

‣ Want to classify silly's T(n) somehow...• Compare T(n)'s growth rate to known functions. First up,

Classify Runtime by Rate of Growth

29

Runn

ing

Tim

e

Input size (n)

log2 n<latexit sha1_base64="I2BAUyPejWsOUTlZYSgWzq+A798=">AAAB73icbVBNSwMxEJ3Ur1q/qh69BIvgqexWQY9FLx4r2A9ol5JNs21oNlmTrFCW/gkvHhTx6t/x5r8xbfegrQ8GHu/NMDMvTAQ31vO+UWFtfWNzq7hd2tnd2z8oHx61jEo1ZU2qhNKdkBgmuGRNy61gnUQzEoeCtcPx7cxvPzFtuJIPdpKwICZDySNOiXVSpyfUsF/Dsl+ueFVvDrxK/JxUIEejX/7qDRRNYyYtFcSYru8lNsiItpwKNi31UsMSQsdkyLqOShIzE2Tze6f4zCkDHCntSlo8V39PZCQ2ZhKHrjMmdmSWvZn4n9dNbXQdZFwmqWWSLhZFqcBW4dnzeMA1o1ZMHCFUc3crpiOiCbUuopILwV9+eZW0alX/olq7v6zUb/I4inACp3AOPlxBHe6gAU2gIOAZXuENPaIX9I4+Fq0FlM8cwx+gzx9lQI+L</latexit>

log2 n<latexit sha1_base64="I2BAUyPejWsOUTlZYSgWzq+A798=">AAAB73icbVBNSwMxEJ3Ur1q/qh69BIvgqexWQY9FLx4r2A9ol5JNs21oNlmTrFCW/gkvHhTx6t/x5r8xbfegrQ8GHu/NMDMvTAQ31vO+UWFtfWNzq7hd2tnd2z8oHx61jEo1ZU2qhNKdkBgmuGRNy61gnUQzEoeCtcPx7cxvPzFtuJIPdpKwICZDySNOiXVSpyfUsF/Dsl+ueFVvDrxK/JxUIEejX/7qDRRNYyYtFcSYru8lNsiItpwKNi31UsMSQsdkyLqOShIzE2Tze6f4zCkDHCntSlo8V39PZCQ2ZhKHrjMmdmSWvZn4n9dNbXQdZFwmqWWSLhZFqcBW4dnzeMA1o1ZMHCFUc3crpiOiCbUuopILwV9+eZW0alX/olq7v6zUb/I4inACp3AOPlxBHe6gAU2gIOAZXuENPaIX9I4+Fq0FlM8cwx+gzx9lQI+L</latexit>

T (n) = n2 + 5n+ 25

Page 6: CS261 3-bigO 2cs.pugetsound.edu/~dchiu/cs261/notes/CS261_3-bigO_2.pdfCSCI 261: Computer Science II - 3 - Complexity Analysis and the Big-O Notation ‣ Classify algorithms in terms

CSCI 261: Computer Science II - 3 - Complexity Analysis and the Big-O Notation

‣ Want to classify silly's T(n) somehow...• Compare T(n)'s growth rate to known functions. Next up,

Classify Runtime by Rate of Growth

30

Runn

ing

Tim

e

Input size (n)

n<latexit sha1_base64="2QmInd+nHO60uhS7AYCvgpiU4a8=">AAAB6HicbVBNS8NAEJ3Ur1q/qh69LBbBU0mqoMeiF48t2FpoQ9lsJ+3azSbsboQS+gu8eFDEqz/Jm//GbZuDtj4YeLw3w8y8IBFcG9f9dgpr6xubW8Xt0s7u3v5B+fCoreNUMWyxWMSqE1CNgktsGW4EdhKFNAoEPgTj25n/8IRK81jem0mCfkSHkoecUWOlpuyXK27VnYOsEi8nFcjR6Je/eoOYpRFKwwTVuuu5ifEzqgxnAqelXqoxoWxMh9i1VNIItZ/ND52SM6sMSBgrW9KQufp7IqOR1pMosJ0RNSO97M3E/7xuasJrP+MySQ1KtlgUpoKYmMy+JgOukBkxsYQyxe2thI2ooszYbEo2BG/55VXSrlW9i2qteVmp3+RxFOEETuE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CS261 3-bigO 2cs.pugetsound.edu/~dchiu/cs261/notes/CS261_3-bigO_2.pdfCSCI 261: Computer Science II - 3 - Complexity Analysis and the Big-O Notation ‣ Classify algorithms in terms

CSCI 261: Computer Science II - 3 - Complexity Analysis and the Big-O Notation

‣ Want to classify silly's T(n) somehow...• Maybe we're aiming too low! Try comparing with

Classify Runtime by Rate of Growth

31

Runn

ing

Tim

e

Input size (n)

n<latexit sha1_base64="2QmInd+nHO60uhS7AYCvgpiU4a8=">AAAB6HicbVBNS8NAEJ3Ur1q/qh69LBbBU0mqoMeiF48t2FpoQ9lsJ+3azSbsboQS+gu8eFDEqz/Jm//GbZuDtj4YeLw3w8y8IBFcG9f9dgpr6xubW8Xt0s7u3v5B+fCoreNUMWyxWMSqE1CNgktsGW4EdhKFNAoEPgTj25n/8IRK81jem0mCfkSHkoecUWOlpuyXK27VnYOsEi8nFcjR6Je/eoOYpRFKwwTVuuu5ifEzqgxnAqelXqoxoWxMh9i1VNIItZ/ND52SM6sMSBgrW9KQufp7IqOR1pMosJ0RNSO97M3E/7xuasJrP+MySQ1KtlgUpoKYmMy+JgOukBkxsYQyxe2thI2ooszYbEo2BG/55VXSrlW9i2qteVmp3+RxFOEETuEcPLiCOtxBA1rAAOEZXuHNeXRenHfnY9FacPKZY/gD5/MH2F+M9g==</latexit>

log2 n<latexit sha1_base64="I2BAUyPejWsOUTlZYSgWzq+A798=">AAAB73icbVBNSwMxEJ3Ur1q/qh69BIvgqexWQY9FLx4r2A9ol5JNs21oNlmTrFCW/gkvHhTx6t/x5r8xbfegrQ8GHu/NMDMvTAQ31vO+UWFtfWNzq7hd2tnd2z8oHx61jEo1ZU2qhNKdkBgmuGRNy61gnUQzEoeCtcPx7cxvPzFtuJIPdpKwICZDySNOiXVSpyfUsF/Dsl+ueFVvDrxK/JxUIEejX/7qDRRNYyYtFcSYru8lNsiItpwKNi31UsMSQsdkyLqOShIzE2Tze6f4zCkDHCntSlo8V39PZCQ2ZhKHrjMmdmSWvZn4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6jBLdShAQwG8Ayv8OZI58V5dz7mrStOPnMEf+B8/gD9Oo2a</latexit>

T (n) = n2 + 5n+ 25

Page 8: CS261 3-bigO 2cs.pugetsound.edu/~dchiu/cs261/notes/CS261_3-bigO_2.pdfCSCI 261: Computer Science II - 3 - Complexity Analysis and the Big-O Notation ‣ Classify algorithms in terms

CSCI 261: Computer Science II - 3 - Complexity Analysis and the Big-O Notation

‣ Want to classify silly's T(n) somehow...• Let's compare T(n) with its dominant term

Classify Runtime by Rate of Growth

32

Runn

ing

Tim

e

Input size (n)

n<latexit sha1_base64="2QmInd+nHO60uhS7AYCvgpiU4a8=">AAAB6HicbVBNS8NAEJ3Ur1q/qh69LBbBU0mqoMeiF48t2FpoQ9lsJ+3azSbsboQS+gu8eFDEqz/Jm//GbZuDtj4YeLw3w8y8IBFcG9f9dgpr6xubW8Xt0s7u3v5B+fCoreNUMWyxWMSqE1CNgktsGW4EdhKFNAoEPgTj25n/8IRK81jem0mCfkSHkoecUWOlpuyXK27VnYOsEi8nFcjR6Je/eoOYpRFKwwTVuuu5ifEzqgxnAqelXqoxoWxMh9i1VNIItZ/ND52SM6sMSBgrW9KQufp7IqOR1pMosJ0RNSO97M3E/7xuasJrP+MySQ1KtlgUpoKYmMy+JgOukBkxsYQyxe2thI2ooszYbEo2BG/55VXSrlW9i2qteVmp3+RxFOEETuEcPLiCOtxBA1rAAOEZXuHNeXRenHfnY9FacPKZY/gD5/MH2F+M9g==</latexit>

log2 n<latexit sha1_base64="I2BAUyPejWsOUTlZYSgWzq+A798=">AAAB73icbVBNSwMxEJ3Ur1q/qh69BIvgqexWQY9FLx4r2A9ol5JNs21oNlmTrFCW/gkvHhTx6t/x5r8xbfegrQ8GHu/NMDMvTAQ31vO+UWFtfWNzq7hd2tnd2z8oHx61jEo1ZU2qhNKdkBgmuGRNy61gnUQzEoeCtcPx7cxvPzFtuJIPdpKwICZDySNOiXVSpyfUsF/Dsl+ueFVvDrxK/JxUIEejX/7qDRRNYyYtFcSYru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SF5ozlGNLKNPC3krYkGrK0KZTtCF4iy8vk2a14p1XqncX5dp1HkcBjuEEzsCDS6jBLdShAQwG8Ayv8OZI58V5dz7mrStOPnMEf+B8/gD9so2a</latexit>

2n<latexit sha1_base64="u4VFVjgtR3RpXlTdSJ3AptCbWaM=">AAAB6nicbVBNS8NAEJ34WetX1aOXxSJ4KkkV9Fj04rGi/YA2ls120y7dbMLuRCihP8GLB0W8+ou8+W/ctjlo64OBx3szzMwLEikMuu63s7K6tr6xWdgqbu/s7u2XDg6bJk414w0Wy1i3A2q4FIo3UKDk7URzGgWSt4LRzdRvPXFtRKwecJxwP6IDJULBKFrpvvqoeqWyW3FnIMvEy0kZctR7pa9uP2ZpxBUySY3peG6CfkY1Cib5pNhNDU8oG9EB71iqaMSNn81OnZBTq/RJGGtbCslM/T2R0ciYcRTYzoji0Cx6U/E/r5NieOVnQiUpcsXmi8JUEozJ9G/SF5ozlGNLKNPC3krYkGrK0KZTtCF4iy8vk2a14p1XqncX5dp1HkcBjuEEzsCDS6jBLdShAQwG8Ayv8OZI58V5dz7mrStOPnMEf+B8/gD9Oo2a</latexit>

T (n) = n2 + 5n+ 25

Page 9: CS261 3-bigO 2cs.pugetsound.edu/~dchiu/cs261/notes/CS261_3-bigO_2.pdfCSCI 261: Computer Science II - 3 - Complexity Analysis and the Big-O Notation ‣ Classify algorithms in terms

CSCI 261: Computer Science II - 3 - Complexity Analysis and the Big-O Notation

What's T(n)'s Upper Bound? (Big-O Notation)

‣ Goal: We want to find a function and a constant such that provides an upper-bound for

33

f(n)<latexit sha1_base64="pG5iFsNQm8e14VQk0dg/VxXte+4=">AAAB63icbVBNSwMxEJ2tX7V+VT16CRahXspuK+ix6MVjBfsB7VKyabYNTbJLkhXK0r/gxYMiXv1D3vw3Zts9aOuDgcd7M8zMC2LOtHHdb6ewsbm1vVPcLe3tHxwelY9POjpKFKFtEvFI9QKsKWeStg0znPZiRbEIOO0G07vM7z5RpVkkH80spr7AY8lCRrDJpLAqL4fliltzF0DrxMtJBXK0huWvwSgiiaDSEI617ntubPwUK8MIp/PSINE0xmSKx7RvqcSCaj9d3DpHF1YZoTBStqRBC/X3RIqF1jMR2E6BzUSvepn4n9dPTHjjp0zGiaGSLBeFCUcmQtnjaMQUJYbPLMFEMXsrIhOsMDE2npINwVt9eZ106jWvUas/XFWat3kcRTiDc6iCB9fQhHtoQRsITOAZXuHNEc6L8+58LFsLTj5zCn/gfP4AXnuNyw==</latexit>

T (n)<latexit sha1_base64="vM8hsiJM1mIid8s1EMCG6OMcapM=">AAAB63icbVBNSwMxEJ2tX7V+VT16CRahXspuFfRY9OKxQr+gXUo2zbahSXZJskJZ+he8eFDEq3/Im//GbLsHbX0w8Hhvhpl5QcyZNq777RQ2Nre2d4q7pb39g8Oj8vFJR0eJIrRNIh6pXoA15UzStmGG016sKBYBp91gep/53SeqNItky8xi6gs8lixkBJtMalXl5bBccWvuAmideDmpQI7msPw1GEUkEVQawrHWfc+NjZ9iZRjhdF4aJJrGmEzxmPYtlVhQ7aeLW+fowiojFEbKljRoof6eSLHQeiYC2ymwmehVLxP/8/qJCW/9lMk4MVSS5aIw4chEKHscjZiixPCZJZgoZm9FZIIVJsbGU7IheKsvr5NOveZd1eqP15XGXR5HEc7gHKrgwQ004AGa0AYCE3iGV3hzhPPivDsfy9aCk8+cwh84nz9C/Y25</latexit>

T (n) = n2 + 5n+ 25

Runn

ing

Tim

e

Input size (n)

Always within a constant factor (c = 8.09)times apart!

c<latexit sha1_base64="0jIMiY3Xg6FeHydWT6UzrJgEy0o=">AAAB6HicbVBNS8NAEJ3Ur1q/qh69LBbBU0mqoMeiF48t2FpoQ9lsJ+3azSbsboQS+gu8eFDEqz/Jm//GbZuDtj4YeLw3w8y8IBFcG9f9dgpr6xubW8Xt0s7u3v5B+fCoreNUMWyxWMSqE1CNgktsGW4EdhKFNAoEPgTj25n/8IRK81jem0mCfkSHkoecUWOlJuuXK27VnYOsEi8nFcjR6Je/eoOYpRFKwwTVuuu5ifEzqgxnAqelXqoxoWxMh9i1VNIItZ/ND52SM6sMSBgrW9KQufp7IqOR1pMosJ0RNSO97M3E/7xuasJrP+MySQ1KtlgUpoKYmMy+JgOukBkxsYQyxe2thI2ooszYbEo2BG/55VXSrlW9i2qteVmp3+RxFOEETuEcPLiCOtxBA1rAAOEZXuHNeXRenHfnY9FacPKZY/gD5/MHx7OM6w==</latexit>

8.09n2<latexit sha1_base64="R4VnV8eKDBAcg95KuAKToGWh0+M=">AAAB7nicbVBNSwMxEJ2tX7V+VT16CRbB07JbBeut6MVjBfsB7VqyabYNzSZLkhXK0h/hxYMiXv093vw3pu0etPXBwOO9GWbmhQln2njet1NYW9/Y3Cpul3Z29/YPyodHLS1TRWiTSC5VJ8SaciZo0zDDaSdRFMchp+1wfDvz209UaSbFg5kkNIjxULCIEWys1K653rV4rPbLFc/15kCrxM9JBXI0+uWv3kCSNKbCEI617vpeYoIMK8MIp9NSL9U0wWSMh7RrqcAx1UE2P3eKzqwyQJFUtoRBc/X3RIZjrSdxaDtjbEZ62ZuJ/3nd1ES1IGMiSQ0VZLEoSjkyEs1+RwOmKDF8YgkmitlbERlhhYmxCZVsCP7yy6ukVXX9C7d6f1mp3+RxFOEETuEcfLiCOtxBA5pAYAzP8ApvTuK8OO/Ox6K14OQzx/AHzucPztmOkQ==</latexit>

After this point... red line isalways greater than blue line!

cf(n)<latexit sha1_base64="9exILpm8evLSWRMgmPQ5YGA6cHc=">AAAB7XicbVBNSwMxEJ2tX7V+VT16CRahXspuFfRY9OKxgv2AdinZNNvGZpMlyQpl6X/w4kERr/4fb/4bs+0etPXBwOO9GWbmBTFn2rjut1NYW9/Y3Cpul3Z29/YPyodHbS0TRWiLSC5VN8CaciZoyzDDaTdWFEcBp51gcpv5nSeqNJPiwUxj6kd4JFjICDZWahMUVsX5oFxxa+4caJV4OalAjuag/NUfSpJEVBjCsdY9z42Nn2JlGOF0VuonmsaYTPCI9iwVOKLaT+fXztCZVYYolMqWMGiu/p5IcaT1NApsZ4TNWC97mfif10tMeO2nTMSJoYIsFoUJR0ai7HU0ZIoSw6eWYKKYvRWRMVaYGBtQyYbgLb+8Str1mndRq99fVho3eRxFOIFTqIIHV9CAO2hCCwg8wjO8wpsjnRfn3flYtBacfOYY/sD5/AFxW45i</latexit>

Page 10: CS261 3-bigO 2cs.pugetsound.edu/~dchiu/cs261/notes/CS261_3-bigO_2.pdfCSCI 261: Computer Science II - 3 - Complexity Analysis and the Big-O Notation ‣ Classify algorithms in terms

CSCI 261: Computer Science II - 3 - Complexity Analysis and the Big-O Notation

What's T(n)'s Upper Bound? (Big-O Notation)

‣ Once we identify the upper bound , then we say that the algorithm runs on the order of -time

34

Runn

ing

Tim

e

Input size (n)

8.09n2<latexit sha1_base64="R4VnV8eKDBAcg95KuAKToGWh0+M=">AAAB7nicbVBNSwMxEJ2tX7V+VT16CRbB07JbBeut6MVjBfsB7VqyabYNzSZLkhXK0h/hxYMiXv093vw3pu0etPXBwOO9GWbmhQln2njet1NYW9/Y3Cpul3Z29/YPyodHLS1TRWiTSC5VJ8SaciZo0zDDaSdRFMchp+1wfDvz209UaSbFg5kkNIjxULCIEWys1K653rV4rPbLFc/15kCrxM9JBXI0+uWv3kCSNKbCEI617vpeYoIMK8MIp9NSL9U0wWSMh7RrqcAx1UE2P3eKzqwyQJFUtoRBc/X3RIZjrSdxaDtjbEZ62ZuJ/3nd1ES1IGMiSQ0VZLEoSjkyEs1+RwOmKDF8YgkmitlbERlhhYmxCZVsCP7yy6ukVXX9C7d6f1mp3+RxFOEETuEcfLiCOtxBA5pAYAzP8ApvTuK8OO/Ox6K14OQzx/AHzucPztmOkQ==</latexit>

After this point... red line isalways greater than blue line!

T (n) = O(f(n))

cf(n)<latexit sha1_base64="M+QzBAZ51lz0frvo9w7+lDy0ipA=">AAAB7HicbVBNS8NAEJ3Ur1q/qh69LBahXkpSBT0WvXisYGqhDWWznbRLN5uwuxFK6W/w4kERr/4gb/4bt20O2vpg4PHeDDPzwlRwbVz32ymsrW9sbhW3Szu7e/sH5cOjlk4yxdBniUhUO6QaBZfoG24EtlOFNA4FPoaj25n/+IRK80Q+mHGKQUwHkkecUWMln0VVed4rV9yaOwdZJV5OKpCj2St/dfsJy2KUhgmqdcdzUxNMqDKcCZyWupnGlLIRHWDHUklj1MFkfuyUnFmlT6JE2ZKGzNXfExMaaz2OQ9sZUzPUy95M/M/rZCa6DiZcpplByRaLokwQk5DZ56TPFTIjxpZQpri9lbAhVZQZm0/JhuAtv7xKWvWad1Gr319WGjd5HEU4gVOoggdX0IA7aIIPDDg8wyu8OdJ5cd6dj0VrwclnjuEPnM8fGwyOOA==</latexit>

Always within a constant factor (c = 8.09)times apart!

T (n) = n2 + 5n+ 25

Page 11: CS261 3-bigO 2cs.pugetsound.edu/~dchiu/cs261/notes/CS261_3-bigO_2.pdfCSCI 261: Computer Science II - 3 - Complexity Analysis and the Big-O Notation ‣ Classify algorithms in terms

CSCI 261: Computer Science II - 3 - Complexity Analysis and the Big-O Notation

Bounding the Growth Rate

‣ Big-O Notation• refers to a set of running times that are bounded within a

constant factor of

• (Formally defined on the board)

‣ Nomenclature• If an algorithm's runtime is bounded by ,

- We write: or more accurately,

- We say, "Algorithm ABC runs on the order of "

35

T (n) = O(f(n))

f(n)<latexit sha1_base64="pG5iFsNQm8e14VQk0dg/VxXte+4=">AAAB63icbVBNSwMxEJ2tX7V+VT16CRahXspuK+ix6MVjBfsB7VKyabYNTbJLkhXK0r/gxYMiXv1D3vw3Zts9aOuDgcd7M8zMC2LOtHHdb6ewsbm1vVPcLe3tHxwelY9POjpKFKFtEvFI9QKsKWeStg0znPZiRbEIOO0G07vM7z5RpVkkH80spr7AY8lCRrDJpLAqL4fliltzF0DrxMtJBXK0huWvwSgiiaDSEI617ntubPwUK8MIp/PSINE0xmSKx7RvqcSCaj9d3DpHF1YZoTBStqRBC/X3RIqF1jMR2E6BzUSvepn4n9dPTHjjp0zGiaGSLBeFCUcmQtnjaMQUJYbPLMFEMXsrIhOsMDE2npINwVt9eZ106jW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H1mfPzUrkF8=</latexit>

T (n) 2 O(f(n))<latexit sha1_base64="rNP7Ui0mmEK6L0IYwu/ZkfrncFw=">AAAB+XicbZDLSsNAFIZP6q3WW9Slm8EitJuStIIui27cWaE3aEOZTCft0MkkzEwKJfRN3LhQxK1v4s63cdpmoa0/DHz85xzOmd+POVPacb6t3Nb2zu5efr9wcHh0fGKfnrVVlEhCWyTikez6WFHOBG1ppjntxpLi0Oe040/uF/XOlErFItHUs5h6IR4JFjCCtbEGtt0siTLqM4EeS4HB8sAuOhVnKbQJbgZFyNQY2F/9YUSSkApNOFaq5zqx9lIsNSOczgv9RNEYkwke0Z5BgUOqvHR5+RxdGWeIgkiaJzRaur8nUhwqNQt90xliPVbrtYX5X62X6ODWS5mIE00FWS0KEo50hBYxoCGTlGg+M4CJZOZWRMZYYqJNWAUTgrv+5U1oVyturVJ9ui7W77I48nABl1ACF26gDg/QgBYQmMIzvMKblVov1rv1sWrNWdnMOfyR9fkDdyqRmg==</latexit>

f(n)<latexit sha1_base64="pG5iFsNQm8e14VQk0dg/VxXte+4=">AAAB63icbVBNSwMxEJ2tX7V+VT16CRahXspuK+ix6MVjBfsB7VKyabYNTbJLkhXK0r/gxYMiXv1D3vw3Zts9aOuDgcd7M8zMC2LOtHHdb6ewsbm1vVPcLe3tHxwelY9POjpKFKFtEvFI9QKsKWeStg0znPZiRbEIOO0G07vM7z5RpVkkH80spr7AY8lCRrDJpLAqL4fliltzF0DrxMtJBXK0huWvwSgiiaDSEI617ntubPwUK8MIp/PSINE0xmSKx7RvqcSCaj9d3DpHF1YZoTBStqRBC/X3RIqF1jMR2E6BzUSvepn4n9dPTHjjp0zGiaGSLBeFCUcmQtnjaMQUJYbPLMFEMXsrIhOsMDE2npINwVt9eZ106jWvUas/XFWat3kcRTiDc6iCB9fQhHtoQRsITOAZXuHNEc6L8+58LFsLTj5zCn/gfP4AXnuNyw==</latexit>

Page 12: CS261 3-bigO 2cs.pugetsound.edu/~dchiu/cs261/notes/CS261_3-bigO_2.pdfCSCI 261: Computer Science II - 3 - Complexity Analysis and the Big-O Notation ‣ Classify algorithms in terms

CSCI 261: Computer Science II - 3 - Complexity Analysis and the Big-O Notation

David's 3-Step Method for Finding Big-O

‣ Finding Big-O for an algorithm• Step 1: Determine the function that expresses the number of

simple statements the algorithm takes- Different function for best/worst/avg case

- But we usually only focus on worst and average cases

• Step 2: Let denote the largest term in - Claim that

• Step 3: Verify correctness of step 2 by finding and

36

T (n)

T (n)

c n0

T (n) = O(f(n))<latexit sha1_base64="h1Yj9ZF0n3gR65RLOjaQGurZx04=">AAAB9XicbZDLSgMxFIbP1Futt6pLN8EitJsy0wq6EYpu3FmhN2jHkkkzbWgmMyQZpQx9DzcuFHHru7jzbUzbWWjrD4GP/5zDOfm9iDOlbfvbyqytb2xuZbdzO7t7+wf5w6OWCmNJaJOEPJQdDyvKmaBNzTSnnUhSHHictr3xzazefqRSsVA09CSiboCHgvmMYG2sh0ZRlNAVuiv6Bkr9fMEu23OhVXBSKECqej//1RuEJA6o0IRjpbqOHWk3wVIzwuk014sVjTAZ4yHtGhQ4oMpN5ldP0ZlxBsgPpXlCo7n7eyLBgVKTwDOdAdYjtVybmf/VurH2L92EiSjWVJDFIj/mSIdoFgEaMEmJ5hMDmEhmbkVkhCUm2gSVMyE4y19ehVal7FTLlfvzQu06jSMLJ3AKRXDgAmpwC3VoAgEJz/AKb9aT9WK9Wx+L1oyVzhzDH1mfPzUrkF8=</latexit>

f(n)<latexit sha1_base64="pG5iFsNQm8e14VQk0dg/VxXte+4=">AAAB63icbVBNSwMxEJ2tX7V+VT16CRahXspuK+ix6MVjBfsB7VKyabYNTbJLkhXK0r/gxYMiXv1D3vw3Zts9aOuDgcd7M8zMC2LOtHHdb6ewsbm1vVPcLe3tHxwelY9POjpKFKFtEvFI9QKsKWeStg0znPZiRbEIOO0G07vM7z5RpVkkH80spr7AY8lCRrDJpLAqL4fliltzF0DrxMtJBXK0huWvwSgiiaDSEI617ntubPwUK8MIp/PSINE0xmSKx7RvqcSCaj9d3DpHF1YZoTBStqRBC/X3RIqF1jMR2E6BzUSvepn4n9dPTHjjp0zGiaGSLBeFCUcmQtnjaMQUJYbPLMFEMXsrIhOsMDE2npINwVt9eZ106jWvUas/XFWat3kcRTiDc6iCB9fQhHtoQRsITOAZXuHNEc6L8+58LFsLTj5zCn/gfP4AXnuNyw==</latexit>

Page 13: CS261 3-bigO 2cs.pugetsound.edu/~dchiu/cs261/notes/CS261_3-bigO_2.pdfCSCI 261: Computer Science II - 3 - Complexity Analysis and the Big-O Notation ‣ Classify algorithms in terms

CSCI 261: Computer Science II - 3 - Complexity Analysis and the Big-O Notation

David's 3-Step Method for Finding Big-O

‣ Finding Big-O for an algorithm• Step 1: Determine the function that expresses the number of

simple statements the algorithm takes- Different function for best/worst/avg case

- But we usually only focus on worst and average cases

‣ For silly(), we already determined it was

37

T (n)

T (n) = n2 + 5n+ 25

= O(n2)

Page 14: CS261 3-bigO 2cs.pugetsound.edu/~dchiu/cs261/notes/CS261_3-bigO_2.pdfCSCI 261: Computer Science II - 3 - Complexity Analysis and the Big-O Notation ‣ Classify algorithms in terms

CSCI 261: Computer Science II - 3 - Complexity Analysis and the Big-O Notation

David's 3-Step Method for Finding Big-O

‣ Finding Big-O for an algorithm• Step 2: Let denote the largest term in

- Claim that

‣ For silly(), we already determined it was

38

T (n) = n2 + 5n+ 25

= O(n2)

T (n)

T (n) = O(f(n))<latexit sha1_base64="h1Yj9ZF0n3gR65RLOjaQGurZx04=">AAAB9XicbZDLSgMxFIbP1Futt6pLN8EitJsy0wq6EYpu3FmhN2jHkkkzbWgmMyQZpQx9DzcuFHHru7jzbUzbWWjrD4GP/5zDOfm9iDOlbfvbyqytb2xuZbdzO7t7+wf5w6OWCmNJaJOEPJQdDyvKmaBNzTSnnUhSHHictr3xzazefqRSsVA09CSiboCHgvmMYG2sh0ZRlNAVuiv6Bkr9fMEu23OhVXBSKECqej//1RuEJA6o0IRjpbqOHWk3wVIzwuk014sVjTAZ4yH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syRDtEsAdRnkhLNJ4ZgIpm5FZEhlphok1POhOAsv7xKmpWyc16uPFwUqjdpHFk4gVMoggOXUIU7qEEDCDzBM7zCmzW2Xqx362PRmrHSmWP4A+vzB9IgkC4=</latexit>

Claim:

Page 15: CS261 3-bigO 2cs.pugetsound.edu/~dchiu/cs261/notes/CS261_3-bigO_2.pdfCSCI 261: Computer Science II - 3 - Complexity Analysis and the Big-O Notation ‣ Classify algorithms in terms

CSCI 261: Computer Science II - 3 - Complexity Analysis and the Big-O Notation

David's 3-Step Method for Finding Big-O

‣ Finding Big-O for an algorithm• Step 3: Verify correctness of the claim by finding and

‣ Next, we need to prove that is in

39

c n0

T (n) = n2 + 5n+ 25

= O(n2)

O(n2)

T (n) = n2 + 5n+ 25

Runn

ing

Tim

e

Find positiveconstant c

Find n0(where do they intersect on horizontal axis?)

cn2<latexit sha1_base64="HJGrz/aYh79gyTggVGXvrX09Jco=">AAAB63icbVBNS8NAEJ3Ur1q/qh69LBbBU0mqoMeiF48V7Ae0sWy2m3bp7ibsboQS+he8eFDEq3/Im//GTZqDtj4YeLw3w8y8IOZMG9f9dkpr6xubW+Xtys7u3v5B9fCoo6NEEdomEY9UL8CaciZp2zDDaS9WFIuA024wvc387hNVmkXywcxi6gs8lixkBJtMIvKxMazW3LqbA60SryA1KNAaVr8Go4gkgkpDONa677mx8VOsDCOcziuDRNMYkyke076lEguq/TS/dY7OrDJCYaRsSYNy9fdEioXWMxHYToHNRC97mfif109MeO2nTMaJoZIsFoUJRyZC2eNoxBQlhs8swUQxeysiE6wwMTaeig3BW355lXQade+i3ri/rDVvijjKcAKncA4eXEET7qAFbSAwgWd4hTdHOC/Ou/OxaC05xcwx/IHz+QO53o4H</latexit>

Page 16: CS261 3-bigO 2cs.pugetsound.edu/~dchiu/cs261/notes/CS261_3-bigO_2.pdfCSCI 261: Computer Science II - 3 - Complexity Analysis and the Big-O Notation ‣ Classify algorithms in terms

CSCI 261: Computer Science II - 3 - Complexity Analysis and the Big-O Notation

Step 3: Finding c and n0

‣ Need to find positive constants and such that,for all :• Not necessarily a unique pair of and

- Let c = 1, solve for n0... it doesn't exist

- Let c = 2, solve for n0... 8.09, or

- Let c = 3, solve for n0... 5.0

- ...

- (the important thing is some choice of c and n0 exists!)

40

c n0

n > n0

c n0

cf(n) � T (n)<latexit sha1_base64="8F0fmHrMyBEd5hZB1uC08v/FCHc=">AAAB9XicbZBNTwIxEIZn8QvxC/XopZGY4IXsookeiV48YsJXAki6ZRYaut1N29UQwv/w4kFjvPpfvPlvLLAHBd+kyZN3ZjLT148F18Z1v53M2vrG5lZ2O7ezu7d/kD88augoUQzrLBKRavlUo+AS64Ybga1YIQ19gU1/dDurNx9RaR7JmhnH2A3pQPKAM2qs9cCCojwnnQGSmoVevuCW3LnIKngpFCBVtZf/6vQjloQoDRNU67bnxqY7ocpwJnCa6yQaY8pGdIBti5KGqLuT+dVTcmadPgkiZZ80ZO7+npjQUOtx6NvOkJqhXq7NzP9q7cQE190Jl3FiULLFoiARxERkFgHpc4XMiLEFyhS3txI2pIoyY4PK2RC85S+vQqNc8i5K5fvLQuUmjSMLJ3AKRfDgCipwB1WoAwMFz/AKb86T8+K8Ox+L1oyTzhzDHzmfP0JAkQ0=</latexit>

Page 17: CS261 3-bigO 2cs.pugetsound.edu/~dchiu/cs261/notes/CS261_3-bigO_2.pdfCSCI 261: Computer Science II - 3 - Complexity Analysis and the Big-O Notation ‣ Classify algorithms in terms

CSCI 261: Computer Science II - 3 - Complexity Analysis and the Big-O Notation

Graphing Calculators Are Helpful

‣ Here's a good one• https://www.desmos.com/calculator

‣ Steps:• Incrementally try some values of (= 1, 2, ...)

• Plug in and in graphing calculator

• Look for point where the lines intersect- Horizontal-axis value is

41

n0<latexit sha1_base64="e4KKTxhMmSwZKYQChLM7KpBg/6c=">AAAB6nicbVBNS8NAEJ3Ur1q/qh69LBbBU0mqoMeiF48V7Qe0oWy2m3bpZhN2J0IJ/QlePCji1V/kzX/jts1BWx8MPN6bYWZekEhh0HW/ncLa+sbmVnG7tLO7t39QPjxqmTjVjDdZLGPdCajhUijeRIGSdxLNaRRI3g7GtzO//cS1EbF6xEnC/YgOlQgFo2ilB9V3++WKW3XnIKvEy0kFcjT65a/eIGZpxBUySY3pem6CfkY1Cib5tNRLDU8oG9Mh71qqaMSNn81PnZIzqwxIGGtbCslc/T2R0ciYSRTYzojiyCx7M/E/r5tieO1nQiUpcsUWi8JUEozJ7G8yEJozlBNLKNPC3krYiGrK0KZTsiF4yy+vklat6l1Ua/eXlfpNHkcRTuAUzsGDK6jDHTSgCQyG8Ayv8OZI58V5dz4WrQUnnzmGP3A+fwD8L42Z</latexit>

c

cf(n)<latexit sha1_base64="M+QzBAZ51lz0frvo9w7+lDy0ipA=">AAAB7HicbVBNS8NAEJ3Ur1q/qh69LBahXkpSBT0WvXisYGqhDWWznbRLN5uwuxFK6W/w4kERr/4gb/4bt20O2vpg4PHeDDPzwlRwbVz32ymsrW9sbhW3Szu7e/sH5cOjlk4yxdBniUhUO6QaBZfoG24EtlOFNA4FPoaj25n/+IRK80Q+mHGKQUwHkkecUWMln0VVed4rV9yaOwdZJV5OKpCj2St/dfsJy2KUhgmqdcdzUxNMqDKcCZyWupnGlLIRHWDHUklj1MFkfuyUnFmlT6JE2ZKGzNXfExMaaz2OQ9sZUzPUy95M/M/rZCa6DiZcpplByRaLokwQk5DZ56TPFTIjxpZQpri9lbAhVZQZm0/JhuAtv7xKWvWad1Gr319WGjd5HEU4gVOoggdX0IA7aIIPDDg8wyu8OdJ5cd6dj0VrwclnjuEPnM8fGwyOOA==</latexit>

T (n)

Page 18: CS261 3-bigO 2cs.pugetsound.edu/~dchiu/cs261/notes/CS261_3-bigO_2.pdfCSCI 261: Computer Science II - 3 - Complexity Analysis and the Big-O Notation ‣ Classify algorithms in terms

CSCI 261: Computer Science II - 3 - Complexity Analysis and the Big-O Notation

David's 3-Step Method for Finding Big-O

‣ Finding Big-O for an algorithm• Step 1: Determine the function that expresses the number of

simple statements the algorithm takes- Different function for best/worst/avg case

- But we usually only focus on worst and average cases

• Step 2: Identify the dominant term of and use that for Big-O

• Step 3: Verify correctness of step 2 by finding and - Problem: There's not really a universal (one-size-fits-all) method to find them

42

T (n)

T (n)

c n0

Page 19: CS261 3-bigO 2cs.pugetsound.edu/~dchiu/cs261/notes/CS261_3-bigO_2.pdfCSCI 261: Computer Science II - 3 - Complexity Analysis and the Big-O Notation ‣ Classify algorithms in terms

CSCI 261: Computer Science II - 3 - Complexity Analysis and the Big-O Notation

‣ Finding Big-O for an algorithm• Step 1: Determine the function that expresses the number of

simple statements the algorithm takes- Different function for best/worst/avg case

- But we usually only focus on worst and average cases

• Step 2: Identify the dominant term of and use that for Big-O

• Step 3: Verify correctness of step 2 by finding and

T (n)

T (n)

c n0

David's 2-Step Method for Finding Big-O

43

Theorem (from K&W book):

If is in the form of a polynomial of degree (the highest exponent) then T (n) = O(nd)

<latexit sha1_base64="antUYhAuCNZocYK6nC/fyUaM1zM=">AAAB9HicbVBNS8NAEJ3Ur1q/qh69LBahvZSkCnoRil68WaFf0May2WzapZtN3N0USunv8OJBEa/+GG/+G7dtDtr6YODx3gwz87yYM6Vt+9vKrK1vbG5lt3M7u3v7B/nDo6aKEklog0Q8km0PK8qZoA3NNKftWFIcepy2vOHtzG+NqFQsEnU9jqkb4r5gASNYG8mtF0UJXaP7onj0S718wS7bc6BV4qSkAClqvfxX149IElKhCcdKdRw71u4ES80Ip9NcN1E0xmSI+7RjqMAhVe5kfvQUnRnFR0EkTQmN5urviQkOlRqHnukMsR6oZW8m/ud1Eh1cuRMm4kRTQRaLgoQjHaFZAshnkhLNx4ZgIpm5FZEBlphok1POhOAsv7xKmpWyc16uPFwUqjdpHFk4gVMoggOXUIU7qEEDCDzBM7zCmzWyXqx362PRmrHSmWP4A+vzBx4pkGA=</latexit>

T (n)<latexit sha1_base64="vM8hsiJM1mIid8s1EMCG6OMcapM=">AAAB63icbVBNSwMxEJ2tX7V+VT16CRahXspuFfRY9OKxQr+gXUo2zbahSXZJskJZ+he8eFDEq3/Im//GbLsHbX0w8Hhvhpl5QcyZNq777RQ2Nre2d4q7pb39g8Oj8vFJR0eJIrRNIh6pXoA15UzStmGG016sKBYBp91gep/53SeqNItky8xi6gs8lixkBJtMalXl5bBccWvuAmideDmpQI7msPw1GEUkEVQawrHWfc+NjZ9iZRjhdF4aJJrGmEzxmPYtlVhQ7aeLW+fowiojFEbKljRoof6eSLHQeiYC2ymwmehVLxP/8/qJCW/9lMk4MVSS5aIw4chEKHscjZiixPCZJZgoZm9FZIIVJsbGU7IheKsvr5NOveZd1eqP15XGXR5HEc7gHKrgwQ004AGa0AYCE3iGV3hzhPPivDsfy9aCk8+cwh84nz9C/Y25</latexit>

d<latexit sha1_base64="i4T7bfCHibYNJNngwp/p1+TXADk=">AAAB6HicbVBNS8NAEJ3Ur1q/qh69LBbBU0mqoMeiF48t2FpoQ9lsJu3azSbsboRS+gu8eFDEqz/Jm//GbZuDtj4YeLw3w8y8IBVcG9f9dgpr6xubW8Xt0s7u3v5B+fCorZNMMWyxRCSqE1CNgktsGW4EdlKFNA4EPgSj25n/8IRK80Tem3GKfkwHkkecUWOlZtgvV9yqOwdZJV5OKpCj0S9/9cKEZTFKwwTVuuu5qfEnVBnOBE5LvUxjStmIDrBrqaQxan8yP3RKzqwSkihRtqQhc/X3xITGWo/jwHbG1Az1sjcT//O6mYmu/QmXaWZQssWiKBPEJGT2NQm5QmbE2BLKFLe3EjakijJjsynZELzll1dJu1b1Lqq15mWlfpPHUYQTOIVz8OAK6nAHDWgBA4RneIU359F5cd6dj0VrwclnjuEPnM8fyTeM7A==</latexit>

Hooray: Don't need to do this step inpractice!!!!!

Page 20: CS261 3-bigO 2cs.pugetsound.edu/~dchiu/cs261/notes/CS261_3-bigO_2.pdfCSCI 261: Computer Science II - 3 - Complexity Analysis and the Big-O Notation ‣ Classify algorithms in terms

CSCI 261: Computer Science II - 3 - Complexity Analysis and the Big-O Notation

‣ THINK BIG INPUT SIZES (As n approaches infinity)• How does silly() behave for larger input sizes?

44

n^2 5n 25

1 (3%) 5 (16%) 25 (81%)

4 (10%) 10 (26%) 25 (64%)

9 (18%) 15 (30%) 25 (51%)

16 (26%) 20 (32%) 25 (41%)

25 (33%) 25 (33%) 25 (33%)

100 (57%) 50 (29%) 25 (14%)

10000 (95%) 500 (5%) 25 (0.2%)

1000000 (99.5%) 5000 (0.5%) 25 (0.002%)

... ... ...

(99.99995%) (0.000005%) 0.00%

Breakdown of each term in T(n)Time Complexity

n T(n) = n^2+5n+25

1 31

2 39

3 49

4 61

5 75

10 175

100 10525

1000 1005025

... ...

1000000 1000005000025

Intuition for the Theorem

Page 21: CS261 3-bigO 2cs.pugetsound.edu/~dchiu/cs261/notes/CS261_3-bigO_2.pdfCSCI 261: Computer Science II - 3 - Complexity Analysis and the Big-O Notation ‣ Classify algorithms in terms

CSCI 261: Computer Science II - 3 - Complexity Analysis and the Big-O Notation

‣ Important!

‣ We can also say that an algorithm runs in "linear" or "quadratic"-time

Finally, a Classification Scheme

45

Big-O

O(1) Constant-time

O(log n) Logarithmic-time

O(n) Linear-time

O(n log n) Log-linear-time

O(n2) Quadratic-time

O(n3) Cubic-time

O(2n) Exponential-time

O(n!) Factorial-time

Fast

erSl

ower

Page 22: CS261 3-bigO 2cs.pugetsound.edu/~dchiu/cs261/notes/CS261_3-bigO_2.pdfCSCI 261: Computer Science II - 3 - Complexity Analysis and the Big-O Notation ‣ Classify algorithms in terms

CSCI 261: Computer Science II - 3 - Complexity Analysis and the Big-O Notation

Summary of Notation

46

n The input size to some algorithm. It is assumed that n >= 0.

T(n) The number of statements an algorithm takes as a function of the number of inputs, n.

f(n) Any function of n. Generally, f(n) represents a simpler function than T(n).

O(f(n))The set of functions that grow no faster than f(n). We say T(n) = O(f(n)) to indicate that the growth of T(n) is bounded by the growth of f(n) by a constant factor.