Introduction to Recursion (Data Structures & Algorithms #6)

แชร์
ฝัง
  • เผยแพร่เมื่อ 15 เม.ย. 2018
  • Recursion explained. Java & Python sample code below.
    Check out Brilliant.org (brilliant.org/CSDojo/), a website for learning math and computer science concepts through solving problems. First 200 subscribers will get 20% off through the link above.
    Special thanks to Brilliant for sponsoring this video.
    Find sample code in Python and Java here: www.csdojo.io/recursion
    This was #6 of my data structures & algorithms series. You can find the entire series in a playlist here: goo.gl/wy3CWF
    You can check out my video about dynamic programming here: • What Is Dynamic Progra...
    Also, keep in touch on Facebook: / entercsdojo
    If you want to find my introduction to recursion, you can check it out here: • Introduction to Recurs...

ความคิดเห็น • 741

  • @CSDojo
    @CSDojo  6 ปีที่แล้ว +115

    Here's my answer to the frog problem at the end: th-cam.com/video/5o-kdjv7FD0/w-d-xo.html

    • @supamdeepbains5172
      @supamdeepbains5172 6 ปีที่แล้ว

      how does function continues to call itself in return statement , im confused because return means stop!
      please explain bro

    • @raghavendraprabhu9540
      @raghavendraprabhu9540 5 ปีที่แล้ว

      Thank you so much.You rock

    • @andreandrews6237
      @andreandrews6237 5 ปีที่แล้ว +11

      I'm late but here goes:
      yes, the return statement means 'stop', but stop once all code is run and your result is ready. because you're making a function call, your code will return the END of that function call. so it will go as far as the final return and then return the final result AFTER all function calls have been made.
      so return func(n - 1) will work like this:
      func(4) -> func(3) -> func(2) -> func(1) and lets just say you have a conditional that says if n

    • @tuxmusicman
      @tuxmusicman 5 ปีที่แล้ว +5

      @@supamdeepbains5172 Return doesn't mean stop. It is used to return values if you have your function set up that way. Many people use it to stop the function in a function that doesn't return anything.

    • @meghanavaitla7610
      @meghanavaitla7610 5 ปีที่แล้ว +1

      I Think the recursion is easy when we know the initial values we are taking and with correct logic

  • @yukiueda6019
    @yukiueda6019 4 ปีที่แล้ว +463

    Recursion is fascinating but it gives me a lot of headaches. Great though.

    • @alextallen8019
      @alextallen8019 3 ปีที่แล้ว +12

      Yeah, I struggle with it and I don't know why

    • @umar1099
      @umar1099 2 ปีที่แล้ว +4

      Mee 2 bro

    • @sadamth6714
      @sadamth6714 2 ปีที่แล้ว +2

      S bro, still i am struggling to understand

    • @unknownunknown8699
      @unknownunknown8699 2 ปีที่แล้ว +2

      they made these algorithms by using the book
      and
      now they want us to make better algorithms without the use of books.

    • @jiakai7254
      @jiakai7254 2 ปีที่แล้ว +14

      A good analogy that may help you is Russian dolls. Think of the recursion function as opening a Russian doll only to get smaller dolls after smaller dolls. Soon, you will reach a doll so small you can't open it. That is when the terminal condition is met and the recursion stops.

  • @ironrose6
    @ironrose6 3 ปีที่แล้ว +134

    Great info! The only thing I have to add is that when it comes to fibonacci numbers and you're in an interview scenario, *always* make sure you ask the interviewer where they consider the fibonacci sequence to start. Some consider 0, 1 to be the first two numbers in the sequence, others consider 1, 1 to be the first two numbers. It reflects poorly on you if you make the wrong assumption.

  • @erikvandeven100
    @erikvandeven100 3 ปีที่แล้ว +4

    I came up with 144 ways to cross the river:
    Python:
    def frogjumps(n):
    if n > 0 and n < 3:
    return n
    if n < 1:
    return 0
    else:
    return frogjumps(n-2) + frogjumps(n-1)
    Though it works, I had to try it a few times, cause I cannot really get my head around it yet. But it seemed like the most logical solution and in the end it seemed to work ( I tested it till number 6, which I just did on paper). Would be great to see the more mathematical solution which might make more sense. Definitely going to see the next video.

  • @gabrielle5171
    @gabrielle5171 3 ปีที่แล้ว +19

    Even though English isn't my first language, I FINALLY understand a recursion concept by this video. Thank you so much Dojo! You're my favorite teacher on youtube

  • @haydhn1474
    @haydhn1474 6 ปีที่แล้ว +131

    by far the best explanation of recursion on youtube!!! Thank you, I was struggling to wrap my head around this, but when you walked through factorial(4) it clicked.

    • @anthonymit5560
      @anthonymit5560 3 ปีที่แล้ว +2

      You should make a video of your understanding.

    • @magpinybo7524
      @magpinybo7524 ปีที่แล้ว

      Same here man

    • @jinahpark1908
      @jinahpark1908 ปีที่แล้ว

      literally the same here, clicked at fact(4) ... a lot of youtubers forget to go backward.. I want to know the CS behind it not just the outcome .

  • @cnsc8941
    @cnsc8941 5 ปีที่แล้ว +18

    Great explaination! I've been struggling with the concept of recursive for a while but your video made it really clear, thank you.

  • @mrjnutube
    @mrjnutube 2 ปีที่แล้ว +5

    Brilliant explanation.
    I've looked at quite a few videos on recursions, and there are not many teachers who've mastered your level of clarity and simplicity. Your's is truly an art.
    Keep them coming!

  • @apporvaarya
    @apporvaarya 5 ปีที่แล้ว +16

    You are my favourite teacher yk.. I had been troubling solving recursion questions..but u cleared its fundamental concepts so well.thanx.

  • @melissayamawaki9319
    @melissayamawaki9319 5 ปีที่แล้ว +8

    You are a great teacher. Congrats and thank you for helping me understand a bit of recursion!

  • @amitsinghparihar4627
    @amitsinghparihar4627 5 ปีที่แล้ว +26

    This is the best explanation I have seen of recursion after attending a lecture, reading 3 articles, and 4 other videos!! You are awesome dude!! I am freaking subscribing!!

  • @CSDojo
    @CSDojo  6 ปีที่แล้ว +37

    Hey everyone! In case you’re curious about why 0! = 1, I’d recommend Numberphile’s video about this topic: th-cam.com/video/Mfk_L4Nx2ZI/w-d-xo.html

    • @gittysachin
      @gittysachin 6 ปีที่แล้ว +6

      Hello YK, please make a video on - "What kind of projects to make to get job at top companies"

    • @vinayjoshi5712
      @vinayjoshi5712 6 ปีที่แล้ว

      Please make videos on how to get into big 4 companies. Their interview process and other stuffs.

    • @glennmiller394
      @glennmiller394 6 ปีที่แล้ว

      Why not if(n > 0)?

    • @MartinloveMary
      @MartinloveMary 6 ปีที่แล้ว

      Glenn Miller if n

  • @ankitkumarsingh9815
    @ankitkumarsingh9815 5 ปีที่แล้ว +39

    U being a great Teacher ..lots of respect and love from India

  • @rosamarvo
    @rosamarvo 2 ปีที่แล้ว

    You are the most effective instructor in DS & Algorithms I've listened to so far. Thank you so much!

  • @semperfortis_semperfidelis
    @semperfortis_semperfidelis 6 ปีที่แล้ว +2

    Thank you, your videos has awaken the love for programming once more. Greetings from Colombia

  • @fafza1
    @fafza1 3 ปีที่แล้ว +42

    I just want you to know that this is the second video from your channel that my professor has added to online CS course at a public college. I appreciate the way you teach.

  • @ananthramvijayaraj4554
    @ananthramvijayaraj4554 3 ปีที่แล้ว +1

    Man, you are amazing, especially in explaining topics that require a lot of examples to understand. thanks a lot, you just earned a sub!

  • @timothyhan9735
    @timothyhan9735 4 ปีที่แล้ว +2

    Excellent job in explaining recursion by implementing theory with code! Great step-by-step example!

  • @kashifhasnainVTU
    @kashifhasnainVTU 5 ปีที่แล้ว +5

    Cs dojo. You are awesome man and the way you explain programming concepts is like anybody can code after watching your sessions...
    Recursion used to be a nightmare for me.. now the concept is clear.. thanks alot .. kindly bring tutorials for arrays application using example codes

  • @Qornv
    @Qornv 6 ปีที่แล้ว +4

    Hey Dojo, glad you're focusing on higher difficulty topics, there aren't as many that explain as well as you do.

  • @ahmed_raaphat
    @ahmed_raaphat 3 ปีที่แล้ว +1

    The simplest explanation i have ever seen about recursion. Thanks a lot.

  • @bernadettealvarado6804
    @bernadettealvarado6804 3 ปีที่แล้ว

    Not skipping ads to give back to CS Dojo. You're an awesome teacher!

  • @emirhankaplan2632
    @emirhankaplan2632 3 ปีที่แล้ว +1

    Amazing content! I had trouble getting my mind around Recursion and your videos helped a lot along the way. Thanks a lot!!!

  • @hgjghjkhify
    @hgjghjkhify 2 ปีที่แล้ว

    Good recursion example showing what happens in the stack with the actual calls of code. I've watched a lot of videos that gloss over the stack call without explaining or showing what happens, so this was incredibly useful. Thank you.

  • @mariacamilabaronamartinez6748
    @mariacamilabaronamartinez6748 3 ปีที่แล้ว +2

    men you're awesome, I not even a English Native and I understand more from you than my courses of Algotihms. Keep going!!!!

  • @angeldenilsongilgarcia8309
    @angeldenilsongilgarcia8309 3 ปีที่แล้ว

    You are brilliant !!!! could not undestand after two hours of class and you clarify everything un 22 minutes !! you are a beast!!

  • @simoo7755
    @simoo7755 5 ปีที่แล้ว +1

    Thank you so much for the great video!Keep them coming!

  • @joMama952
    @joMama952 5 ปีที่แล้ว

    Great job on this topic. I didn't even need to watch the second half of the video after how well you explained the factorial implementation.

  • @mizuhashitsunoka
    @mizuhashitsunoka 6 ปีที่แล้ว +3

    Looking forward to tutorials on graphs! It's really difficult to find videos where you like both the way of teaching of the concept, as well as the fast pace of learning. My finals practical will probably be long past by the time a graph video comes up, but it just shows that how much me, and a lot of people probably, will still watch your videos ^^
    And if you actually notice this question, then I kinda wanted to ask which of the data structures would you be covering in the future? Would love to watch advanced concepts like heaps and disjoint sets as well.

  • @amitdhawan579
    @amitdhawan579 3 ปีที่แล้ว

    There are a lot of explanations for recursion but I think yours is probably one of the clearest. Good job bro.

  • @hondedtij6761
    @hondedtij6761 3 ปีที่แล้ว +42

    Sad part is that these kind of talented people who can make you understand concepts as easily as that are not working on colleges, they are on top companies giving them all their talent.

    • @cortisol_induced_coma
      @cortisol_induced_coma 3 ปีที่แล้ว +17

      To be fair though, those top companies pay them more than many colleges would.

    • @doomshot7324
      @doomshot7324 2 ปีที่แล้ว

      yes colleges don't pay well sometimes

  • @cartapo2669
    @cartapo2669 6 ปีที่แล้ว +1

    Thanks man. I didn't really understand the CS50 tutorial for recursion so this helped me enough to go back to the CS50 video and totally understand it. Keep it up!

  • @thekogrimmer527
    @thekogrimmer527 3 ปีที่แล้ว +1

    Amazing teacher and layout compared to other videos. Thanks for this!

  • @DeidaraC5
    @DeidaraC5 4 ปีที่แล้ว +3

    I had to watch your videos to finally understand recursion. Please do more videos on basic algorithms and ds, you are a genius!!!! PLEASE!

  • @brainy3429
    @brainy3429 3 ปีที่แล้ว +1

    Thanks YK,u r a lifesaver.I was learning abt recursion this afternoon and I don't understand it and I ve been disappointed but now I understand it well.Lots of love and respect 🍁

  • @Dswilsonangel
    @Dswilsonangel 3 ปีที่แล้ว

    CS Dojo, you are AMAZING!!!!! Thank you SO MUCH for your channel! I am DEFINITELY subscribing!!!! :)

  • @kingston_tim7957
    @kingston_tim7957 2 ปีที่แล้ว

    You are the best teacher I have ever meet, just wanna say thank you Professor Dojo

  • @TharaMesseroux1
    @TharaMesseroux1 2 ปีที่แล้ว

    You're so talented! You explain so simply! Thank you so much!

  • @xivus11
    @xivus11 2 ปีที่แล้ว

    I finally find the problem of my mindset when dealing with recursion. Thank you sooo much!

  • @yiqingzhu8503
    @yiqingzhu8503 4 ปีที่แล้ว

    Super clean explanation of the flow of recursion

  • @stiksnaki
    @stiksnaki 3 ปีที่แล้ว

    This is the best Video that I have been seen about recursion.

  • @ucantseeme1161
    @ucantseeme1161 6 ปีที่แล้ว

    Hey dojo.... get a series on algorithms....much needed
    Btw great work!!!!

  • @yigitcanayaz5509
    @yigitcanayaz5509 3 ปีที่แล้ว

    You're killing it bro!

  • @potatoitis3326
    @potatoitis3326 3 ปีที่แล้ว

    Thank you so much. I was struggling so bad with recursion.. this video was so helpful.

  • @pallaviprabhu5265
    @pallaviprabhu5265 4 ปีที่แล้ว

    Love the way you narrate. Kuddos👏👏

  • @Pravasith
    @Pravasith 2 ปีที่แล้ว

    Great content! Love the series

  • @simeonveselinski3997
    @simeonveselinski3997 3 ปีที่แล้ว

    life-saver!Thanks you for the clear explanation!

  • @limlimminhkhai
    @limlimminhkhai 4 ปีที่แล้ว

    I love it how did you explain Recursion algorithms :)

  • @jasonbelcher4476
    @jasonbelcher4476 5 ปีที่แล้ว

    wow your teaching style is great! thanks!

  • @aliahed7951
    @aliahed7951 4 ปีที่แล้ว

    You are an excellent teacher and I finally understand recursion a bit better!
    Here is my recursion solution for the fibonacci sequence (when you told us to give it a go):
    def fib(n):
    if n>=3:
    return fib(n-1)+fib(n-2)
    else:
    return 1
    I was so relieved when the code worked as it took me forever to get it right!

  • @thyiacolt5335
    @thyiacolt5335 4 ปีที่แล้ว

    DUUUDDEEE THANKU SO MUCH!! It all makes sense now!!

  • @jamesv1able
    @jamesv1able ปีที่แล้ว

    This was a really helpful video. It was hard to get my head around but this video made it a lot easier. Thanks

  •  6 ปีที่แล้ว +1

    man, you are amazing, grettings from chile!

  • @marco.nascimento
    @marco.nascimento 5 ปีที่แล้ว +2

    awesome series

  • @irfancalsr7982
    @irfancalsr7982 3 ปีที่แล้ว

    I was just thought the way CS Dojo has solved the fibbonacci problem is not the efficientiest way, just then he mentioned about dinamic programming that I hadnt knew. Great, thanks.

  • @roberthoffmann930
    @roberthoffmann930 3 ปีที่แล้ว

    Dear CS Dojo, thank you for rescuing my life! many greetings from zurich.

  • @taiwanpix1796
    @taiwanpix1796 4 ปีที่แล้ว

    very helpful and clear explanation step by step, thanks!

  • @sarthakshah3699
    @sarthakshah3699 4 หลายเดือนก่อน

    great explanation and break-down. thank you for this

  • @paranoia77
    @paranoia77 6 ปีที่แล้ว

    thankyou very much for making free lessons, it's very helpful to me, never stop for doing this..

  • @xtanion
    @xtanion 3 ปีที่แล้ว

    solving these problems gives massive satisfaction :)
    thanks YK .

  • @aminabouzar4416
    @aminabouzar4416 3 ปีที่แล้ว

    Nice explanation, cleared everything for me!

  • @KM-sf6zy
    @KM-sf6zy 4 ปีที่แล้ว

    wow what an explanation
    clear and precise

  • @Kidpunk98
    @Kidpunk98 3 ปีที่แล้ว

    Excellent explanation, you are clearly incredibly intelligent and you know how to relay that knowledge as well!

  • @babuldebbarma3395
    @babuldebbarma3395 6 ปีที่แล้ว

    Very clearly and easiest way explained. It is similar process in loop function if I'm not wrong. Anyway got very clear in recursion:) Thank You and keep going;)

  • @anokaggrey3109
    @anokaggrey3109 2 ปีที่แล้ว

    Best explanation of recursion for me.

  • @chinemelumj5349
    @chinemelumj5349 2 ปีที่แล้ว

    Thank you so much, I'm starting to grasp this concept❤.

  • @joro353
    @joro353 5 ปีที่แล้ว

    Excellent video. Thanks!

  • @IPLOFFICIAL-ml4fw
    @IPLOFFICIAL-ml4fw 6 ปีที่แล้ว

    thanks buddy...You are really hopeful...I am your regular some kind of fan..i am learning python from your video.thanks buddy

  • @emptyspace2685
    @emptyspace2685 3 ปีที่แล้ว

    The imagery of recursion in my head is so satisfying. It’s like the computer opening a larger and larger function until it reaches the else statement and then closes it. It’s kinda like that domino thing where they all fall on to each other initially, and then when it hits the end they all fall again

  • @realking4918
    @realking4918 3 ปีที่แล้ว +1

    This guy has excellent explanation skills

  • @sipp5657
    @sipp5657 6 ปีที่แล้ว

    Amazing video as usual

  • @pratyushtripathy2750
    @pratyushtripathy2750 6 ปีที่แล้ว

    Very cool bro! Keep going.

  • @samglick8479
    @samglick8479 4 ปีที่แล้ว

    very cool video, taught me a lot about recursion

  • @user-lg2ty6kn6x
    @user-lg2ty6kn6x ปีที่แล้ว

    Great explanation!!! Thank you

  • @luistovar6916
    @luistovar6916 3 ปีที่แล้ว +4

    I tried to solve the exercise, this was my solution in kotlin:
    fun fibRec(position: Int):Int {
    return if(position

  • @raghuop8675
    @raghuop8675 6 ปีที่แล้ว

    Thank you so much sir....I'm always waiting for your videos..please don't stop your teaching sir...its my humble request...

  • @mengwang6936
    @mengwang6936 5 ปีที่แล้ว +10

    JESUS! This is the first time that i can code fibbonacci within 10 seconds! Thanks man!

  • @amycross5430
    @amycross5430 3 ปีที่แล้ว

    Dude...you are the greatest...

  • @ed2023bc
    @ed2023bc 6 หลายเดือนก่อน

    Very clear explanation, thank you

  • @maharaniputrisuari8783
    @maharaniputrisuari8783 3 ปีที่แล้ว

    THANK YOU SO MUCH I'M CLEARLY UNDERSTAND THIS

  • @kokowin5851
    @kokowin5851 4 ปีที่แล้ว

    Thank you soo much... I am a freshman CS student and I don't understand anything my prof is saying. You are videos are really clear and easy to understand. Please make more videos on Data Structures and Algorithms.

  • @graykaufmann
    @graykaufmann 3 ปีที่แล้ว

    Thank you for your explanation. I think the mathematical explanation in the beginning helped a lot to really understand, what the recursive function really does.
    I didn't understand, that the function is calling itself till the if statement becomes true.

  • @renwarbaxtyar5638
    @renwarbaxtyar5638 4 ปีที่แล้ว

    i can't imagine what a wonderful teaching you are really better than my teacher .

  • @muhammadkumaylabbas8513
    @muhammadkumaylabbas8513 ปีที่แล้ว

    Awesome video thank you very much!

  • @paulshekhovtsov7031
    @paulshekhovtsov7031 2 ปีที่แล้ว

    Thanks, for great explanation!

  • @bluebee9670
    @bluebee9670 5 ปีที่แล้ว

    Thank you for showing how to break down the problem :)

  • @kenanaosithole1586
    @kenanaosithole1586 ปีที่แล้ว

    This is really amazing stuff especially for a self learner

  • @samnath1267
    @samnath1267 6 ปีที่แล้ว +6

    love the way u explain!

  • @RajatSingh-rh5hg
    @RajatSingh-rh5hg 4 ปีที่แล้ว

    even though english is not my native language i can understand ur concepts very easily..tq for ur fabulous explaination

  • @bhaskarjyotidas6619
    @bhaskarjyotidas6619 6 ปีที่แล้ว +2

    Sir... Please continue this series in detailed. For explaining some data structure examples my preference for the language used is to be JAVA .

  • @zombie-blood-490gaming5
    @zombie-blood-490gaming5 5 ปีที่แล้ว +10

    i freakin love you!! that shit made no sense to me in class

  • @karnaghose4784
    @karnaghose4784 7 หลายเดือนก่อน

    Great Explanation

  • @divyajyotibose5769
    @divyajyotibose5769 5 ปีที่แล้ว

    Awesome video and explicitly clear explanation . One request can you make videos on specific language like Java for example

  • @jamessun6249
    @jamessun6249 3 ปีที่แล้ว

    Great tutorial! You are awesome.

  • @sajeesyed3384
    @sajeesyed3384 2 ปีที่แล้ว

    a great example of how recursion works programmatically, it solved it in my head though

  • @thereaper489
    @thereaper489 ปีที่แล้ว +1

    def fibSeq(n):
    a = 0
    b = 1
    for i in range(2, n):
    c = a + b
    print(c)
    a, b = b, c
    fibSeq(100)

  • @mvrius.9
    @mvrius.9 2 ปีที่แล้ว

    The best video if you want to understand recursion for real!

  • @suryasuresh533
    @suryasuresh533 5 ปีที่แล้ว +1

    static int fib(int n) {
    int count = 2;
    int first = 1;
    int second = 1;
    int next = 0;
    if (n == 1 || n == 2) {
    return 1;
    }
    while(count < n) {
    next = first + second;
    first = second;
    second = next;
    count++;
    }
    return next;
    }
    Here's my code. Thank you CS Dojo for all the help so far!

    • @thereaper489
      @thereaper489 ปีที่แล้ว

      def fibSeq(n):
      a = 0
      b = 1
      for i in range(2, n):
      c = a + b
      print(c)
      a, b = b, c
      fibSeq(100)

  • @chandraprakashsingh2530
    @chandraprakashsingh2530 5 ปีที่แล้ว

    Nicely taught brother it was very clear to me thanks

  • @nijhumpaul9472
    @nijhumpaul9472 4 ปีที่แล้ว

    Great video. Thank you so much