Time Complexity analysis of recursion - Fibonacci Sequence

Поделиться
HTML-код
  • Опубликовано: 8 сен 2024
  • See complete series on recursion here
    • Recursion
    In this lesson, we will analyze time complexity of a recursive implementation of Fibonacci sequence.
    Prerequisite: basic knowledge of recursion as programming concept, basic understanding time complexity analysis.

Комментарии • 134

  • @charvakpatel962
    @charvakpatel962 10 лет назад +79

    You are a very good teacher.
    I watched all your pointer related videos and I feel like i have a strong concept in it.
    Now i m learning recursion concept
    You teach very good and i m gonna watch all your videos and learn a lot of stuff
    Because of you i m studying in my vacation
    It's fun and a tones of fun.

    • @mycodeschool
      @mycodeschool  10 лет назад +15

      Thanks a lot patel charvak :)

  • @LegitGamer2345
    @LegitGamer2345 3 года назад +5

    I have entered my first year of CSE this very year , and was not able to find anything good on time and space complexity of recursive programs , this is GOLD!!

  • @NintendoGearSage
    @NintendoGearSage 6 лет назад +8

    Over and over again, you always prove you're the best at teaching this material over everyone else on RUclips.

    • @orangeshoes
      @orangeshoes 4 года назад

      have you tried MIT OCW?

    • @blasttrash
      @blasttrash 4 года назад

      @@orangeshoes MIT videos are too technical and dont go in depth like some of the other youtube channels. The students at MIT get to meet Teaching Assistants, Professors etc where they can go in depth during office hours, but these sessions are never captured on line.

  • @mycodeschool
    @mycodeschool  11 лет назад +19

    We are trying to reduce the expression. If T(n) = 2*T(n-2) +C, you must understand that n is a variable here. So, n can be anything. We canalso say,|
    T(n-2) = 2* T(n-4) + C and T(n-4) = T(n-6) + c and T(n-6) = T(n-8) +C
    On right hand side, we have to decrease the expression by 2. And we are going on reducing the expression at each step till we reach T(0) and T(1) that are known to us. I want to get T(n) in terms of T(0) or T(1) that are constants.

    • @stonecoldcold2941
      @stonecoldcold2941 6 лет назад +1

      dude ? I have a doubt , will u reply tho > ?

    • @pankajzanzane12
      @pankajzanzane12 4 года назад +10

      Confusing part is, why T(n-2) can be written as "2* T(n-4) + C" ?

  • @animeshnayan1
    @animeshnayan1 11 лет назад +5

    Hi Sabin,
    Yes, ideally, we should use theta notation. Its just that big-oh notation is more famous. Most of the time, when we say big-oh of a function, we can also say theta of that function. In this case also, we could have said theta. The main intent here was to show how to reduce the running time equation for recursion. Once you have done that, you have figured out the time complexity that you can express in terms of one of these asymptotic notations.

  • @anonymous_4276
    @anonymous_4276 3 года назад +1

    I have derived an exact formula for T(n). It's equal to 5F(n+1)-4. Here F(n) is the nth term of the Fibonacci sequence if F(1)=0 and F(2)=1. Fibonacci sequence grows as (golden ratio)^n. So this sequence (Tn) too grows like that.

  • @hashaamshahid3100
    @hashaamshahid3100 Год назад +1

    for those who are struggling at reduction part its just he's plugging t(n-2) to t((n-2)-2) as in replacing n with n-2 in the function.

  • @mycodeschool
    @mycodeschool  11 лет назад +2

    2{ 2T(n - 4) + c} + c = 4T(n - 4) + 2c + c after removing the brackets. the c inside the bracket is actually 2c

  • @mycodeschool
    @mycodeschool  11 лет назад +2

    Hi Avinash,
    You can use "Camtasia" for screen recording. To write, you would need a pen tablet and any image editing software like ms-paint.

  • @chemselassil742
    @chemselassil742 7 месяцев назад

    thank you so much this is the best chanel ever

  • @kenway346
    @kenway346 Год назад +1

    I wonder why I did not came across this channel ages ago.. feels really sad when a person who is doing MS in CS still does not understand recursion :(

  • @theshivuvlogs
    @theshivuvlogs 4 года назад

    thanks alot . iam really very confused regarding the topic of complexity. bt now all matter is clear.

  • @dharani8871
    @dharani8871 Год назад +1

    Such good content 10 years ago.....oh my

  • @channaseeb6070
    @channaseeb6070 8 лет назад +4

    Excellent stuff. looking forward to more videos on complexity analysis especially w.r.t exponential time algorithms, the one that you mentioned in the last part of this video.

  • @HsenagNarawseramap
    @HsenagNarawseramap 9 лет назад +6

    8:51 Exponential time is still better than factorial time (e.g. travelling salesman) :)

  • @amitbisht587
    @amitbisht587 3 года назад

    well till now i was wondering that in case of t(0) and t(1) the time was given by return n i.e. 0 for t(0) and t(1) and you cleared my this misconception sir tysm

  • @Jaan_Mustafa
    @Jaan_Mustafa 5 месяцев назад

    Thanks for explaining , it help me a lot

  • @ericescobar8886
    @ericescobar8886 7 лет назад +1

    Thank you for excellent teaching. Very clear and concise.

  • @youcefjousef3732
    @youcefjousef3732 9 лет назад +50

    at 03:37 why did you put T(n)=2(2T(n-4)+C)+c i didn't understand

    • @ranyalbegwein7470
      @ranyalbegwein7470 9 лет назад +72

      Youcef Jousef You continue to expand T(n-x) and use the proportion assumption for the lower bound. For example: T(n-2) = T(n-3) + T(n-4) + C. Since we are talking about the lower bound here, we assume the proportion T(n-3) ~ T(n-4), thus you can write T(n-2) = T(n-4) + T(n-4) +C => T(n-2) = 2T(n-4) + C. Now we can use this expanded expression here: T(n) = 2T(n-2) + C and we will get exactly this: T(n) = 2(2T(n-4)+C)+C = 4T(n-4) + 3C. Then you can continue and expand T(n-4) to be T(n-4) = T(n-5) + T(n-6) + C, and again say that T(n-5) ~ T(n-6) since you are currently describing the lower bound, thus T(n-4) = T(n-6) + T(n-6) + C = 2T(n-6) + C. And, now again replace T(n-4) in the previous equation, and you'll get T(n) = 4(2T(n-6)+C)+C = 8T(n-6)+5C.

    • @SergeyFominVL
      @SergeyFominVL 5 лет назад

      @@ranyalbegwein7470 yes, but in the video it's 8T(n-6)+ 7C (not 5C). WhY???

    • @ishanpand3y
      @ishanpand3y 5 лет назад

      ​@@SergeyFominVL 4T(n-4)+3c can be expanded as 4(2T(n-6)+c)+3c = 8T(n-6)+7c

    • @gauravjaiswal4683
      @gauravjaiswal4683 4 года назад

      @@SergeyFominVL I guess, it's because of the recursive call...

    • @harshitha3867
      @harshitha3867 4 года назад +1

      @@ranyalbegwein7470 You explain better than that video.

  • @avatarmage34
    @avatarmage34 6 лет назад

    Brilliant and you have a very calm voice. Love it.

  • @SmartProgramming
    @SmartProgramming 6 лет назад

    very helpful video, thanks a lot sir, keep it up 👍👍

  • @sekelian
    @sekelian 11 лет назад +10

    I just really don't understand the series of steps you take starting at 3:05 in the video. I understand T(n-1) ~ T(n-2), therefore T(n) = 2T(n-2) + C, but after that I have no idea what's going on.

  • @samynashabeh
    @samynashabeh 9 лет назад

    Brilliant bro ,,, Very simple and clear explanation

  • @VishalGupta_1829
    @VishalGupta_1829 6 лет назад +1

    Superb explanation 😙😁😁 thanks a lot

  • @thanhphamthitan5342
    @thanhphamthitan5342 3 года назад

    best explanation ever !! Thank you so much!

  • @haoliu0708
    @haoliu0708 9 лет назад

    Best explanation so far

  • @sandeepannits86
    @sandeepannits86 8 лет назад +4

    Did not understand how do you say at 4:27 "n-2k=0". Rest all is clear. Thanks!

    • @BrianDyannMusic
      @BrianDyannMusic 8 лет назад +1

      he expedited the recursion to T(n) in terms of T(0). that's why he equated it to get the value of k that translates this

    • @udaykadam5455
      @udaykadam5455 5 лет назад

      We are solving it for a base case since we know the exact value of T(0) and T(1).

  • @satya592
    @satya592 10 лет назад +2

    Well explained, loved to the core:)

  • @madhusmithamuduli5362
    @madhusmithamuduli5362 4 года назад +1

    why are we approximating T(n-1)=T(n-2) ??
    cant we solve without the approximation?

  • @akashtandon1
    @akashtandon1 9 лет назад +1

    Brilliantly explained. Was this one of Harsha's videos?
    Humblefool, you are and will be missed dearly by the programming community.

  • @SabinBajracharya
    @SabinBajracharya 11 лет назад +2

    Very nice Tutorial! But why did you take upper bound instead of lower bound? Also if time complexity lies between Upper Bound and Lower Bound then why not use Theta Notation? Please correct me if I'm wrong, please reply.

  • @corporalwaffles
    @corporalwaffles 8 лет назад +2

    When substituting the base case into the time calculation, why do you use T(0) rather than T(1)?

  • @avinashsajjan
    @avinashsajjan 11 лет назад +2

    Thank you very much for such lucid explanation. May I know what all devices you guys using for writing & recording ?

  • @shubhamprasad6910
    @shubhamprasad6910 2 года назад

    Great explanation 👌

  • @Dan-tg3gs
    @Dan-tg3gs 3 года назад +1

    is the space complexity of fibonacci using recursion o(n) since you are only using n recursive calls on stack at a time?

  • @faustofeles8912
    @faustofeles8912 8 лет назад

    Amazing explanation, thank you so much!

  • @SiddharthPanda_infinity_crew
    @SiddharthPanda_infinity_crew 5 лет назад

    You mentioned t(n-1) > t(n-2) {time taken}...but it should be other way around since n-2 is a smaller set than n-1.

  • @spicytuna08
    @spicytuna08 6 лет назад

    thanks for the video. i really appreciate it. fyi f(0) = f(1) = 1 in fib series. i make that mistake all the time.

  • @harkeeratdhunda2160
    @harkeeratdhunda2160 Год назад

    Wow, thank you sm!

  • @sekelian
    @sekelian 11 лет назад +1

    Firstly, thank you for your helpful tutorials. Secondly, I'm get quite lost when you 'reduce' the equation. Could you point me to some resources where I can learn what's happening there?

  • @nevilholmes5900
    @nevilholmes5900 3 года назад

    Thanks

  • @AkshayKumar-xh2ob
    @AkshayKumar-xh2ob Год назад

    Whenever I come back to mycodeschool, it's always so sad to remember the sad story behind all this.

  • @romxstar
    @romxstar 10 лет назад +3

    mycodeschool at 03:28 why did you put T(n)=2T(n-2)+C i didn't understand where " 2 " come from !

    • @563woodcock
      @563woodcock 10 лет назад +18

      Here, we have assumed that time taken by T(n-1) is almost equal to T(n-2). So, in the original equation T(n) = T(n-1) + T(n-2) + 4, we can substitute T(n-1) with T(n-2). Which becomes, T(n) = T(n-2) + T(n-2) + 4. Which is simplified as, T(n) = 2T(n-2) + 4. Here 4 is a constant, so we can write, T(n) = 2T(n-2) + C

  • @malavikam5593
    @malavikam5593 8 лет назад

    the video was very useful sir ..

  • @soumilyade1057
    @soumilyade1057 2 года назад

    The video was made and posted when I was in secondary school...and at present I'm done with Masters :3 feels like time travel :3

  • @Jaan_Mustafa
    @Jaan_Mustafa 5 месяцев назад

    Damm , 11 year old video , you were ahead of curve .

  • @mycodeschool
    @mycodeschool  11 лет назад +1

    Hi sekelian,
    Where exactly are you getting lost? i can try explaining. Its just mathematics and i do not have any specific resource to point you to.

  • @gladwinrojer273
    @gladwinrojer273 4 года назад

    One Serious question. How did you know that O(2^50) will take years to give result?? I mean how to convert Big O time complexities to actual real time??

  • @mostafakhoshbash2478
    @mostafakhoshbash2478 4 года назад

    That was very helpful thank you :)

  • @gauravtiwari1658
    @gauravtiwari1658 7 лет назад

    Truly Helpful. Thank u......!

  • @RAGEEcs
    @RAGEEcs Год назад

    great explanation

  • @tejasdharmadhikari
    @tejasdharmadhikari 5 лет назад

    So beautifully explained ! Thanks a lot 😊

  • @DanielVazquez
    @DanielVazquez 3 года назад

    The induction proof is way easier, we can even get a tighter bound with it.

  • @aman8628
    @aman8628 2 года назад

    why t(n)=2t(n-2)+c lower bound and why t(n)=2t(n-1)+c is upper bound?

  • @animeshnath1087
    @animeshnath1087 3 года назад

    nice explanation

  • @aryanshridhar8517
    @aryanshridhar8517 4 года назад +1

    How good else can be an explanation ? :)

  • @wolverinelogan9851
    @wolverinelogan9851 9 лет назад

    Nice explanation!!!!.Thanks for the video. At 3:02 you said that time calculate for T(n-1) is greater than time to calculate T(n-2). How do we arrive at that conclusion???

    • @rasaq123utube
      @rasaq123utube 9 лет назад +5

      Time to calculate T(n-1) is greater than time to calculate T(n-2) because n-1 is greater than n-2, therefore the time to recurse from n-1 to the base cases (0 and 1) is more than it would take n-2 to get there. You can also think of it in terms of writing out the series => 0 1 1 2 3 where n=5 assuming you start from n=0. It takes less time to get n=3 which evaluates to 1 than it does to get n=4 which evaluates to 2.

  • @priyankasubramanyam8412
    @priyankasubramanyam8412 5 лет назад +1

    @mycodeschool Return statements take 1 unit too right?

  • @narayanbhat3279
    @narayanbhat3279 5 лет назад +1

    What is with that lower bound and upper bound?

  • @ysonu1010
    @ysonu1010 6 лет назад

    Why the time complexity of return is not added anywhere, where in starting it was told to add 1 unit for return.?

  • @pazuzujindrich3001
    @pazuzujindrich3001 3 года назад

    Can someone explain why n-k can be set to zero when calculating upper bound?

  • @apawar3099
    @apawar3099 4 года назад

    I miss you ....@MyCodeSchool..

  • @user-ry4wk9on2p
    @user-ry4wk9on2p 3 года назад

    Thank you !! bb

  • @silvadexter3222
    @silvadexter3222 6 лет назад

    excellent
    TY so much

  • @pankajzanzane12
    @pankajzanzane12 4 года назад

    At 3:28 , How did we arrive at this conclusion that, T(n-2) = 2* T(n-4) + C ?

  • @msp9413
    @msp9413 3 года назад +1

    @ 6:21 isn't t(0) = 0 why is it 1 ?

  • @aindrilasaha1592
    @aindrilasaha1592 5 лет назад

    Wonderful explanation!

  • @wardahahmed2834
    @wardahahmed2834 3 года назад

    Can you explain please why (1+C)*2n/2 -c suddenly over here?

  • @abdelrhmanahmed1378
    @abdelrhmanahmed1378 3 года назад

    should we also count the return coast as constant time !!!

  • @momedalhouma14
    @momedalhouma14 10 лет назад

    Hi mycodeschool , Thanks for the tutorial
    from the 4:24 and from 6:09minuts ididn't understand what you did , can you explain why you write in term of t(0) for wich purpose

    • @lasredchris
      @lasredchris 9 лет назад

      The base case. t(0) is an operation that takes constant time. You use this in the recurrence relation.

  • @bhaveshshah9487
    @bhaveshshah9487 6 лет назад +2

    Hi. !! At 3:00, its said that time taken to calculate T(n-1) > T(n-2). Can you please explain why so? Also, how to decide if its lower bound or whether its upper bound? Thanks.

    • @narayanbhat3279
      @narayanbhat3279 5 лет назад +2

      T(n-1) when expanded also has T(n-2) in it, so it does make sense to take more time to process the extra piece of code

  • @vineethnaroju5871
    @vineethnaroju5871 7 лет назад

    Thanks man.

  • @lalatdas
    @lalatdas 10 лет назад

    you should add this video to the time complexity play list you have in your channel...

  • @hammadmaqsood6395
    @hammadmaqsood6395 7 лет назад

    Good Explanation...But I confuse from one point when you equal the n-k to T(0) you write n-k=0 , but in equation of Time complexity you but T(0)= 1. Why?

    • @hammadmaqsood6395
      @hammadmaqsood6395 7 лет назад

      waiting for reply

    • @pawanbishnoi2149
      @pawanbishnoi2149 7 лет назад

      What we have:: T(0) = T(1) = 1 - that is what we know, right ?
      What we are trying to find out:: T(n)
      And we were able to deduce that:: T(n) = 2^k T(n-k) + (2^k -1 ) * c
      Now the question for us is: How can we use our knowledge i.e. T(0) and T(1) here to get to our answer.
      Simple! we find out at what value of k, n-k becomes 0?
      n-k = 0
      or k = n
      continue from 6:18

    • @msp9413
      @msp9413 3 года назад

      @@pawanbishnoi2149 isn't T(0) = 0?

  • @sriramyavyasabhattu5623
    @sriramyavyasabhattu5623 Год назад

    How t(0)=t(1)=1 can you please explain

  • @Rakesh-ms7tn
    @Rakesh-ms7tn 6 лет назад

    Which software are you using for teaching ? Openboard or one note ?

  • @rafilushan1988
    @rafilushan1988 3 года назад

    Why dont you make videos anymore?

  • @mehreenashfaq
    @mehreenashfaq 6 лет назад +2

    how T(n-2) can be written as 2T(n-4)?kindly explain this at 3:33

    • @NWS189
      @NWS189 5 лет назад +4

      It's a substitution of T(n-2) = T(n-3) + T(n-4) + 4. We then make the same assumption as T(n-1) ~= T(n-2), such that T(n-3) ~= T(n-4). So T(n-3) + T(n-4) becomes 2T(n-4), and T(n-2) = 2T(n-4) + 4.

  • @MinhLe-ho6fj
    @MinhLe-ho6fj 5 лет назад

    very clear, thankyou

  • @vortyx090
    @vortyx090 9 лет назад

    Ty man

  • @marvel438
    @marvel438 6 лет назад

    what do you mean by lower bound?

  • @senya1413
    @senya1413 3 года назад

    2:55
    I didn't understand, why is T(n-1) is in reality greater than T(n-2)?

    • @iftekharul_islam_ifat
      @iftekharul_islam_ifat 3 года назад

      Put a specific value for n and it will be easier to understand. For example, determining Fib(5) will take slightly more time than that of determining Fib(4). In other words, T(5) is greater than T(4).
      Hope this helps. :)

  • @sayantaniguha8519
    @sayantaniguha8519 3 года назад

    Why is he taking n-k = 0
    How is it possible to 0 elements as i/p ?

    • @amitbisht587
      @amitbisht587 3 года назад

      ask yourself can fibonacci series take input n=0 and i think it can therefore he does that and n=0 doesnt means that i/p element is 0 it means that value of input can be 0

  • @vikram5010
    @vikram5010 10 лет назад +2

    Good one!
    Somewhere, I came across following thing:
    'Big O of recursive fibonacci is actually O(1.6^n)'
    Is this correct? If yes, how?

    • @hieudoan7711
      @hieudoan7711 5 лет назад

      Maybe it helps
      www.geeksforgeeks.org/time-complexity-recursive-fibonacci-program/

    • @anonymous_4276
      @anonymous_4276 3 года назад

      @@hieudoan7711 you are 6 years too late

    • @hieudoan7711
      @hieudoan7711 3 года назад

      @@anonymous_4276 I know you will read it, so it isn't late at all

    • @anonymous_4276
      @anonymous_4276 3 года назад

      @@hieudoan7711 thanks

  • @simbeJob
    @simbeJob 4 года назад

    can any one why c=4b for me. Thank you

  • @fragatahugo
    @fragatahugo 9 лет назад +1

    >algórtem

  • @soumitramehrotra5547
    @soumitramehrotra5547 4 года назад

    Why in reality time taken by T(n-1) > T(n-2) ?

    • @demonslayer3071
      @demonslayer3071 4 года назад

      because In calculating T(n-1) we also have to calculate T(n-2) while returning T(n-1) so T(n-1) is considered bigger

  • @bodhibriar9683
    @bodhibriar9683 3 года назад

    He is one in a million, best among many, most trusted , I almost gave up on trading then I met him through a friend, Austin is the most trusted trading expert who helped the life of my family and I ,.

    • @bodhibriar9683
      @bodhibriar9683 3 года назад

      -+/1/5/3/0/4/2/8/5/8/70/W/h/a/t/s/A/p/p/< With> /
      A/U/s/t/In/.

  • @johnnybatafljeska6368
    @johnnybatafljeska6368 7 лет назад

    Cant time complexity be calculated trough simple homogeneous differential equation?

  • @lmnop291
    @lmnop291 4 года назад

    You shouldn't skip how you're getting ur numbers thats the reason we're watcing this. Remake pls

  • @creativegravedigger7289
    @creativegravedigger7289 4 года назад

    you made it look so difficult

  • @c.danielpremkumar8495
    @c.danielpremkumar8495 7 лет назад

    It is not subSTRaction. It is subTRaction.

  • @chrisauret3785
    @chrisauret3785 9 лет назад

    your reduction explanation from 3:05 onward is very poor

  • @Chowow
    @Chowow 5 лет назад

    I still don't understand a PEEEP

  • @Lastrevio
    @Lastrevio 5 лет назад

    ew light mode you should write with white on black not the other way around

  • @tagzeeez
    @tagzeeez 5 лет назад

    You have no Idea how much I hate this stupid math expressions and shortcuts that never make sense to me..... it makes me very angry..... but great vid man thanks.

    • @madhavshrotriya2020
      @madhavshrotriya2020 4 года назад

      i can help if you still dont get
      the one in this video

    • @sonj2970
      @sonj2970 4 года назад

      @@madhavshrotriya2020 All I am confused with is what k is. n is just a variable representing the input of the fibonacci function. But what variable does k represent?

    • @Torabshaikh
      @Torabshaikh 3 года назад

      @@sonj2970 it represents number of iteration we have done. Think of it as 'i' we use in loops.