Time Complexity of Code Using Summations

Поділитися
Вставка
  • Опубліковано 4 лип 2015
  • Compute the following code fragments time complexity using summations.
    To see how to convert other such programs checkout:
    • Compute The Time Compl...
    and
    • Prove The Time Complex...
    and
    • Big-Oh Of The Code Seg...
    Please subscribe !
    ►Website: everythingcomputerscience.com/
    ►Support this channel on Patreon: / randerson112358
    ►Support this channel for FREE by doing your Amazon shopping through this link (bookmark it!): www.amazon.com/?tag=randerson1...

КОМЕНТАРІ • 155

  • @TaarLps
    @TaarLps 7 років тому +132

    Finally someone that actually does something else than "Well you see here, it is in O(n^2)."

  • @armaganvideos
    @armaganvideos 6 років тому +22

    Words cannot describe how much I love you for making this video. Your tips and notes and little explanations, the live "troubleshooting" throughout the whole video, it helped me here in Germany struggling with datastructures and algorithms in University..
    A big, big thank you, if I pass the test in 4-6 weeks, I make sure to donate something to you.

    • @randerson112358
      @randerson112358  6 років тому

      Thank you very much for those kind words.

    • @GregEads
      @GregEads 6 років тому

      How did you get the N^2 - N in N^2 - N - ((N-1)(N)/2)? @6:00

    • @armaganvideos
      @armaganvideos 6 років тому +7

      Pretty easy, look at the upper bound of each sigma sign and calculate how much it runs for the inner loop, therefore, for the first sigma sign for example: Σn from i=0 to n-1, means "upper bound minus lower bound + 1":
      n - 1 - 0 + 1 = n. Now you know how much it runs for the inner loop, multiply this with the inner runtime which is "N" (as you stated it) and you get n*n = n^2. Replicate this for the last two sigma signs and you get the result. Rule/Note: Σi from i=0 to n can be solved by using the gaussian summation rule which is:
      Σi from i=0 to n = n(n+1)/2 while you have to replace the 'n's in the gaussian summation rule by replacing the n with the upper bound of your sigma sign. Thats it!

    • @GregEads
      @GregEads 6 років тому

      ArmaganVideos thank you!

    • @armaganvideos
      @armaganvideos 6 років тому +2

      Yo Randerson! I passed the exam! Please contact me with your Paypal or something similiar, I want to thank you. We can work on designs for your channel aswell, instead of a donation.
      Much love from Germany!

  • @vasanth23
    @vasanth23 5 років тому +3

    One of the most meaningful videos I have seen lately. Thank you very much !

  • @martinhabsburger4929
    @martinhabsburger4929 7 років тому +14

    I LOVE IT!
    I for example am pretty bad at math. I take this algorithm/complexity course without knowing basic Analysis or anything. And you always write NOTE: and explain what exactly you do to those summation formulas! That's freaking awesome man!
    Keep up the good work!

  • @baraasaad2366
    @baraasaad2366 5 років тому

    you just don't know how much you helped me with this video, thank you very much sir.

  • @feyisayo
    @feyisayo 7 років тому +1

    Currently taking an Algorithm Design course at school, Thank you so much for these videos!!

  • @amrmoneer5881
    @amrmoneer5881 4 роки тому +3

    you've made this really easy to understand, writing down all the rules and stuff. you're really helpful man thanks :)

  • @jg12357
    @jg12357 4 роки тому +2

    Thank you for all of your videos. Incredibly helpful!

  • @clockus8023
    @clockus8023 5 років тому +1

    Thank you very much I was stuck in a question of algorithms and data structures. May God bless you.

  • @yaldayazdanpanah2104
    @yaldayazdanpanah2104 7 років тому +3

    This video solved many of my problems! Thank you really!

  • @aminelagab4830
    @aminelagab4830 8 років тому +5

    this video worth gold , good job sir , thank you :D

  • @imersaovisual
    @imersaovisual 7 років тому +16

    Your videos are the best - period.

  • @flying_Color
    @flying_Color 7 років тому

    Thank you, that is very good tutorial. I like delicate explanation and help me a lot.

  • @HughbInc
    @HughbInc 8 років тому +2

    Do You have more videos of Time complexity of code fragments?

  • @Anne-ug4jv
    @Anne-ug4jv 7 років тому +2

    does the n + m + 1 only apply if you have a constant number? what if its the summation ranging from n, to j where j = 1. then instead of a constant number, it's j(j +1)? do you substitute 1 with j(j+1)?

  • @anishprabhu8592
    @anishprabhu8592 6 років тому +1

    Exactly what I was looking for! Keep it up

  • @slomotionaction
    @slomotionaction 4 роки тому +3

    I wish not only did we go to school together but we studied together.

  • @randerson112358
    @randerson112358  8 років тому +2

    +Hughbert HanLon
    You can see more videos on time complexity below:
    1) ua-cam.com/video/UwWTE3u7FFk/v-deo.html
    2) ua-cam.com/video/AL7yO-I5kFU/v-deo.html

  • @shyom1d676
    @shyom1d676 6 років тому +3

    You’re my hero
    Thank u from the bottom of my heart 😭❤️❤️❤️❤️❤️

    • @randerson112358
      @randerson112358  6 років тому

      Haha thanks, I know this stuff can be hard to understand sometimes, but keep learning it will all be worth it in the end.

  • @iPhanman
    @iPhanman 4 роки тому

    Does the summation proof only apply for for loops? Can we do it with while loops?

  • @movieshome2387
    @movieshome2387 3 місяці тому

    Great Work Clear Explanation... ❤

  • @benja303
    @benja303 8 років тому

    Had to log in just to thank you. I don't usually but Good ass video brehhh. Lol you teach very well and your videos are criminally under-viewed.

    • @randerson112358
      @randerson112358  8 років тому

      +benja303 thank you for taking time to leave such a pleasent comment.

  • @CaptainAwesome923
    @CaptainAwesome923 6 років тому +1

    Dude these videos literally saved my ass thank you so much

  • @banevuk890
    @banevuk890 7 років тому +1

    Great explanation!

  • @kokunutt76
    @kokunutt76 7 років тому +1

    Thank you so much!! this helps me a lot, keep uploading :)

  • @ProfessionalTycoons
    @ProfessionalTycoons 6 років тому +1

    very good video, thank you !

  • @honk-
    @honk- 2 роки тому +1

    you're a goat man, please let me know if theres any place I can support you

    • @randerson112358
      @randerson112358  2 роки тому

      Thank Honk, I appreciate that !
      I also appreciate that you would like to support me, below are two places where you can do that:
      1. PayPal:
      www.paypal.com/donate?token=vjtNH8bGGvC8zN90Xc9C3bC3PR95cQM19ncFnz9sqdejeFnJNdW9BWWv4MUHQYpyO4xusuBo5wBwuwHO
      2. Patreon.com:
      www.patreon.com/randerson112358
      Thanks for watching !

  • @petermcgill3405
    @petermcgill3405 8 місяців тому

    Dude, you're amazing

  • @mrpwnr24
    @mrpwnr24 6 років тому

    YESSSS thank you much appreciated!!!!

  • @ge4149
    @ge4149 6 років тому

    Hello.. Thanks for the tutorial. I'm a bit confused. It's right that sum of i = n(n+1)/2 .. But in the last of the board u write n(n-1)/2.. Is it corect? Bcs i do calculate the answer should be (n2-3n) / 2 ..
    Thanks for the video anyway.. I really understand it now:)

  • @weezybusy
    @weezybusy 7 років тому +1

    Thank you, sir.

  • @EpicVideoGameMaster
    @EpicVideoGameMaster 3 роки тому +1

    Thank you soooo much my cs course makes so much more sense now

    • @lukeleedy3713
      @lukeleedy3713 3 роки тому

      What class are u watching this for? I'm trying to make sense of Foundations I: Discrete Structures at OSU.

  • @danielmihu1552
    @danielmihu1552 6 років тому +1

    Great example! Thank you!

  • @salahelwerfally8810
    @salahelwerfally8810 7 років тому +1

    your the best man, i love you

  • @nitsalnand
    @nitsalnand 4 роки тому

    Hey man
    Can you specifically define in a new video about worst, best, and average time complexity of an algorithm using for loop

  • @brianramaswami9889
    @brianramaswami9889 5 років тому +16

    6:03 why do you get n^2? what's he simplifying or combining?

    • @mire_cs
      @mire_cs 5 років тому +3

      i was confused here as well. i believe it is because the loop runs N times, and it adds N each time, therefore the sum would be N*N.

    • @andrewamado5729
      @andrewamado5729 4 роки тому +4

      You can also use the formula from before. You would need to factor out the n and would be left with n * (the summation). You can then use the formula he erased first, (n - 1) + 0 + 1 = n. Remember we factored out the n, so we have to multiply it back in, so n*n = n^2

  • @FireboxTrainingCourses
    @FireboxTrainingCourses 3 роки тому +1

    Excellent explanation!!

  • @jingjang21
    @jingjang21 4 роки тому

    Great stuff!

  • @thetedmang
    @thetedmang 6 років тому +1

    Math guy here, those are some crisply written sigmas my friend.

  • @freesoul2677
    @freesoul2677 7 років тому +1

    Thank you

  • @kokalti
    @kokalti 5 років тому

    Would this be considered an actual proof?

  • @victorasmad7305
    @victorasmad7305 3 роки тому +1

    You are the best, thank you man

    • @randerson112358
      @randerson112358  3 роки тому

      Thank you Victor ! I may not be the best, but I am glad that my explanation of the problem was helpful to you and I think it's always good to get different perspectives from different people to solve a problem.
      Just know that I truly appreciate your comment, thank you !

  • @MrAlikdv
    @MrAlikdv 6 років тому +1

    Thanks !!

  • @unix.geektarektalaat1631
    @unix.geektarektalaat1631 2 роки тому

    can you explain how to get the best and worst results ?

  • @TimStark
    @TimStark 2 роки тому

    how does (i + 1) become i - 1 @4:06?

  • @DOC0OC
    @DOC0OC 8 років тому

    thank you very much.

  • @Burak-pl1jl
    @Burak-pl1jl 6 років тому

    *Wouldn't be the upper bound of the first summation "n" ?*
    Since your first loop header also means :
    _for(int i = 0 ; i < n ; i++) { // .. }_
    which checks for n-0+1 number of times, which equals to n+1 and then *the summation formula for the inner loop would begin from the lower bound i = 0 to the upper bound n* ( which is 1 less then the outer loop header ) and so on...
    Or am I missing something?

    • @randerson112358
      @randerson112358  6 років тому

      Hi, I am not sure I understand your question, but the outer loop runs O(n) time.

    • @Burak-pl1jl
      @Burak-pl1jl 6 років тому

      How come? Do you mean the header or the body? Doesn't the outer loop *header* run for n+1 times?

    • @randerson112358
      @randerson112358  6 років тому +2

      If an algorithm runs n+ 1 then it is O(n).
      Another example would be if an algorithm runs n+ 45 or 2n or 2n +1 all of those equations are O(n).
      The outer for loop itself would technically n+1, and everything within the for loop (if it is a constant) would run n times, because the loop runs from i=0 to n-1.
      For example if n= 3, then our loop runs
      i=0
      i=1
      i=2
      So it runs 3 times or n times, and same goes for anything within the loop that is a constant.

    • @Burak-pl1jl
      @Burak-pl1jl 6 років тому

      You are right but I was just talking about the exact number of times. I mean without asymptotic notation. So shouldn't your upper bound at the very first summation formula be n instead of n-1?

    • @randerson112358
      @randerson112358  6 років тому +1

      Ah, I see.
      No the summation is correct because we are looking at 'loop body' which is within the outer loop and the inner loop. If I was only looking at the outer for loop itself then yes it runs n+1 times exactly, but any constants inside that loop would run n times.
      The outer for loop itself runs n + 1 times only because it has to check the condition for when to stop.

  • @dawid7260
    @dawid7260 Рік тому

    Can you do something like this but with triple for loop and the third loop is k

  • @bushraw66
    @bushraw66 3 місяці тому

    thank you so much

  • @brettmccausland880
    @brettmccausland880 6 років тому

    awesome thank you

  • @WaleedNaeem
    @WaleedNaeem 8 років тому

    do you have any other videos regarding Algorithms ?

    • @randerson112358
      @randerson112358  8 років тому

      +Sheikh Waleed Naeem
      Here is a playlist:
      ua-cam.com/video/4XkHbNi1ZL4/v-deo.html

    • @WaleedNaeem
      @WaleedNaeem 8 років тому

      Thnank you !

  • @denverrivera4968
    @denverrivera4968 2 роки тому

    Thank you so mucchhh

  • @anyod5625
    @anyod5625 5 років тому +1

    Love you so much ^^

  • @aaallami
    @aaallami 7 років тому +5

    Great and well don but I am still confusing about the last step why did you make it n^2 and discarded the other part of the last expression thanks

    • @randerson112358
      @randerson112358  7 років тому +10

      I am not sure which part you are talking about specifically, but I am guessing at @5:58
      Here the equation was:
      Σn - Σ1 - Σi
      I said that was equal to:
      n^2 - n - [ (n-1)(n) / 2]
      Converting Σn to n^2:
      Σn = n x Σ1 = n x n = n^2.
      Converting Σ1 to n:
      Σ1 = n, For example if n=5 then the summation from i=0 to n would be (1 + 1 + 1 + 1 + 1 + 1 = 6)
      and
      Since our value goes to n-1, in this case if n = 5 the summation would look like this (1 + 1+ 1+ 1+ 1 = 5)
      So in the second case we see that the value we get back is n or in this case the number 5.
      NOTE: Σ means summation from i=0 to (n-1)
      NOTE: Σ1 means summation from i=0 to (n-1) of 1
      NOTE: Σn means summation from i=0 to (n-1) of n
      I hope that helps !
      randerson112358

    • @kevinryzack5712
      @kevinryzack5712 7 років тому

      These videos seem great, but I got lost in the summation notation. In particular, what you just tried to explain above. I watched some basic summation notation videos on UA-cam but none went over what you did in this video. Do you know of a good youtube video to watch, so I can better understand this video? Thanks!

    • @randerson112358
      @randerson112358  7 років тому

      Hi,
      Thanks for the comment. What is it that you don't understand in this video, maybe I can help you.
      Also I have more videos on topics like this below:
      Running Time of Code Fragment:
      ua-cam.com/video/h3ld8FbD7FU/v-deo.html
      Big-Oh of the code segment:
      ua-cam.com/video/nbtPmejK_C8/v-deo.html
      Prove Time Complexity of the following Program:
      ua-cam.com/video/UwWTE3u7FFk/v-deo.html
      Compute the Time Complexity of the following Code:
      ua-cam.com/video/AL7yO-I5kFU/v-deo.html
      Thanks;
      randerson112358

    • @kevinryzack5712
      @kevinryzack5712 7 років тому

      Confused about what you did at 6:00. Maybe I'm missing some rules of summation, but I don't know how you simplified those summations.

    • @randerson112358
      @randerson112358  7 років тому +2

      Yes, I used summation properties/formulas:
      en.wikipedia.org/wiki/Summation
      I have a video on Summations (hope it'll help):
      ua-cam.com/video/hXeb8cM5o_0/v-deo.html
      I am not sure which part you don't understand exactly, for example if it's how I was able to move over the summation notation from sum( n - 1 - i) = sum(n) - sum(1) - sum(i) or if it's how I got the following:
      sum(n) = n^2 ,
      sum(1)= n,
      sum(i) = (n-1)(n) / 2.
      Where sum() is the summation from i=0 to (n-1)
      So first I used the property of summations (You must understand properties of summations to work with them):
      sum( n - 1 - i) = sum(n) - sum(1) - sum(i) [by propery of summations]
      = n * sum(1) - sum(1) - sum(i) [Here I was able to take out the n, by property of summation]
      = (n * n) - n - sum(i) [ sum(1) = n, by summation formula from i=0 to n]
      = (n ^2) - n - sum(i) [ just making the equation look nice by multiplying the n's]
      = (n ^2) - n - (n-1)(n-1 + 1) / 2 [sum(i) = (n-1)(n-1 + 1) / 2, by summation formula]
      NOTE: Their is an formula for
      sum(i) from i=0 to n = (n)(n+1)/2, notice the similarities,
      I plugged in "n-1" for "n".
      = (n ^2) - n - (n-1)(n) / 2 [Just did some addition ]
      I hope that helps;
      randerson112358

  • @bharlesCabbage
    @bharlesCabbage Рік тому

    Hello. What if I have to write the time for line 1, line 2, and line 3 individually? :(

    • @jatindergill1875
      @jatindergill1875 Рік тому

      Bro I’m in the same situation as you did you manage to me pass this class or find videos lol

  • @ayushjindal6885
    @ayushjindal6885 7 років тому

    What if loop condition is only less than (

  • @rezzuu
    @rezzuu 2 роки тому +1

    good stuff !!

  • @corporalwaffles
    @corporalwaffles 7 років тому +1

    Super helpful, thanks :)

  • @dimanxyou93
    @dimanxyou93 3 роки тому

    It doesn't affect the final answer but on your last step you changed a - to a + which should give you (n^2 -3n)/2

    • @randerson112358
      @randerson112358  3 роки тому

      I am not sure which step you are referring to.
      Before the final answer I had the following equation:
      [ (2n^2 - 2n) / 2 ] - [(n^2 - n)/ 2]
      Simplifying this would give:
      [ (2n^2 - 2n) - (n^2 - n)] / 2
      Simplifying the above would give:
      [ 2n^2 - 2n - n^2 - (- n) ] / 2
      Simplifying the above would give:
      [ 2n^2 - 2n - n^2 + n ] / 2
      Simplifying/rearranging the above would give:
      [ 2n^2 - n^2 + n - 2n ] / 2
      Simplifying the above would give:
      [ n^2 + n - 2n ] / 2
      Simplifying the above would give:
      [ n^2 + - n ] / 2
      Simplifying the above would give:
      ( n^2 - n ) / 2
      Please let me know if I was wrong in any of theses steps or if you missed something or if this was not the part of the equation you were talking about and thanks for watching !

  • @noreenmekky230
    @noreenmekky230 7 років тому +2

    thanks a lot for this video I really appreciate it but I don't get the n^2 at 6:05
    Thanks in advance.

    • @randerson112358
      @randerson112358  7 років тому

      The first summation is equal to n^2.

    • @CabCallawayMusic
      @CabCallawayMusic 6 років тому

      I think he is asking how you arrived to that conclusion...I have the same question

    • @user-ch2ni9lt9i
      @user-ch2ni9lt9i 6 років тому

      Summation rule : upper(n-1) by n = n*n = n^2

    • @Crzynoob
      @Crzynoob 6 років тому +1

      Wouldn't it be (n^2-n) and the next one [summation of 1] is n-1?

    • @VIp2011x
      @VIp2011x 5 років тому +2

      see, you know its n-m+1 , here its (n-1) - 0 +1 and we multiply that by n )cuz its sum of n , so it Will be ((n-1) - 0 +1) n ,,, so it be come (n)n and that equals@@Crzynoob to n^2 . ^_^

  • @MovieZaddy
    @MovieZaddy 5 років тому +1

    this might be a stupid question, but why do you use theta ??

    • @randerson112358
      @randerson112358  5 років тому

      This isn't a stupid question. To help explain why here is a video that I have on Big Theta that will explain what it is, and then you will know why I use it !
      ua-cam.com/video/Vzqaz4MDGvc/v-deo.html
      The short answer is because it's the tight bound.
      Thanks for watching !
      randerson112358

  • @MuhammadKashif-ty4pn
    @MuhammadKashif-ty4pn Рік тому

    awesome

  • @battsogtbatgerelt1583
    @battsogtbatgerelt1583 5 років тому +1

    thank you very much ^_^

  • @RawwestHide
    @RawwestHide 8 років тому +1

    thanks bruh

  • @alirezaayinmehr7854
    @alirezaayinmehr7854 6 років тому +1

    Cool video, but I still can't understand :((
    Something is wrong with me or maths...

  • @bobjames1521
    @bobjames1521 7 років тому +34

    finally a video that doesn't begin with "Ello Frents duday we are looking ad comblexity of alogoritum."

    • @nickp7526
      @nickp7526 6 років тому +8

      Well you gotta give it to them... people from India are very keen about their programming ;)

    • @Y0d3lingy3ti
      @Y0d3lingy3ti 5 років тому

      "THAT'S RACIST!" I feel you bruh

  • @angelamunyao7877
    @angelamunyao7877 5 років тому

    greeaaaat video

  • @parvathydharmarajan4180
    @parvathydharmarajan4180 7 років тому +1

    g8t work....

  • @unknownunknown8699
    @unknownunknown8699 2 роки тому

    6:20 n^2 - n that seems fishy
    i think its n^2 - 2n -1
    summation of i = 0 to n -1 and n is n (n -1)

    • @randerson112358
      @randerson112358  2 роки тому

      Thanks for the comment but after checking my math everything looks correct, if you think it's wrong could you please show the math ?

  • @TheZainullahkHan
    @TheZainullahkHan 7 років тому

    how did we get n^2 - n ... ??

    • @randerson112358
      @randerson112358  7 років тому

      I don't know which part of the video that you are referring to.

    • @TheZainullahkHan
      @TheZainullahkHan 7 років тому

      at 6:05

    • @randerson112358
      @randerson112358  7 років тому

      That's what the summations are equal to.
      sum from i=0 to n-1 of n = n^2
      sum from i=0 to n-1 of 1= n
      To figure out how those are equal you can use the summation formulas. I have the formula in the video @4:15
      ua-cam.com/video/4XkHbNi1ZL4/v-deo.html
      For sum from i=0 to n-1 of 1= n
      m = 0
      n = (n -1 )
      Then plug in the values for (n - m + 1) = ( (n-1) - 0 + 1) = n - 1 + 1 = n + 0 = n
      sum from i=0 to n-1 of n = n^2
      This is just n * (sum from i=0 to n-1 of 1) = n * n = n^2
      I hope that helps
      Thanks for watching;
      randerson112358

    • @TheZainullahkHan
      @TheZainullahkHan 7 років тому

      still confusing becoz formula you told for summation solving was n-m+1 then how get we n^2 over here ??

    • @randerson112358
      @randerson112358  7 років тому

      Do you understand summations ?
      If not I would probably start there. Look up summation operations and formulas.
      Okay let me show you how I got n^2.
      summation from i=0 to n-1 of n
      = n * [ summation from i=0 to n-1 of 1] (by summation properties)
      = n * [(n-1) - 0 + 1 ]
      = n * n
      = n^2
      I have a video on Summations if that is what you do not understand:
      ua-cam.com/video/hXeb8cM5o_0/v-deo.html

  • @ristovskiv
    @ristovskiv 8 років тому

    Isn't this Big Oh, instead of Big Theta?

  • @linaa6681
    @linaa6681 5 років тому

    I dont get why its n^2 v.v

    • @VIp2011x
      @VIp2011x 5 років тому

      same problem

  • @swaytha7
    @swaytha7 5 років тому

    At 5:35 did you mean to say
    n-1 n
    ∑ i = ∑ i
    i=0 i=1

    • @randerson112358
      @randerson112358  5 років тому

      No I did not.

    • @swaytha7
      @swaytha7 5 років тому

      @@randerson112358 Then how will
      n n
      ∑ i = ∑ i
      . Could you please explain?
      i=0 i=1

    • @randerson112358
      @randerson112358  5 років тому +1

      @@swaytha7
      From my video:
      n n
      ∑ i = ∑ i
      i=0 i=1
      Let n = 3 for example, then we get the following summation:
      3 3
      ∑ i = ∑ i
      i=0 i=1
      The first summation = 0 + 1 + 2 + 3 = 6
      The second summation = 1 + 2 + 3 = 6
      Both are equal.

    • @swaytha7
      @swaytha7 5 років тому +1

      @@randerson112358 Ah got it :). Thanks so much for the video and clear explanation. Really helpful.

  • @user-dj9iu2et3r
    @user-dj9iu2et3r 2 роки тому

    I love that you made this video but there seem to be some clear mistakes, no? Like some of your jumps and your note about the summations being equal when they clearly were not.
    sory of confused me more lol

  • @rajawikiaa
    @rajawikiaa 5 років тому

    You didn't add the Sum(1) from i=0 to n-1...great video though!