How to use Dijkstra's Algorithm with Code

Поділитися
Вставка
  • Опубліковано 25 сер 2024
  • This is a tutorial on the Dijkstra's algorithm, also known as the single source shortest path algorithm. It is extensively used to solve graph problems. We use an example to understand the algorithm and then walk through the the code.
    Dijkstra's uses both concepts of Greedy(Finding the next city to pick), and DP(using previously calculated distances).
    Code:
    algs4.cs.princ...
    References:
    www.geeksforgee...
    web.engr.orego...
    math.mit.edu/~r...

КОМЕНТАРІ • 112

  • @sunnysrivastava7575
    @sunnysrivastava7575 7 років тому +71

    The most beautiful part about you that you know the real struggle of a student and you focus on that part specially XD Thank you for the tutorial

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

      Thanks Sunny!

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

      Sir can you make a tutorial about advance use of Binary Search for solving questions. Since as mentioned in the TopCoders's tutorial, it can be use to reject a part of search space where the required solution wont have probability to exist. Thank you

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

      I made a playlist for Binary search with the intention of discussing Binary Search in detail. You can find it here:
      Binary Search: ua-cam.com/play/PLMCXHnjXnTnvYoBsAzrwigA-HSvfVXNNG.html

  • @Rahul-lg1nw
    @Rahul-lg1nw 3 роки тому +13

    idk why this channel is not growing, this is real gold .,, love you Gaurav

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

    The best Dijkstra's Algorithm explanation on youtube. Simply Outstanding. Keep it up

  • @sreekanths26
    @sreekanths26 6 років тому +13

    Can you please make a complete vid tutorial on Graph data structure please. I haven't been able to find a good one so far. Thanks in advance. Your explanation is simple n clear :)

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

      Thanks Sreekanth! Will get to this soon :)

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

      good idea my man

  • @sharathish
    @sharathish 3 роки тому +3

    Hello, Gaurav this was really helpful, specifically, the way you addressed the critical points that a student has to take out of the algorithm is simply spectacular !!

  • @ApoorvaPakhle
    @ApoorvaPakhle 4 роки тому +1

    You were so excited with the introduction +100

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

    your introduction is welcoming.

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

    This is exactly what was happening to me, I didn't get how the hell it was working, thank Gaurav!!:) This is an amazing explanation i would surely recommend this to all my batchmates @IIITB

  • @NikitaSharma-bs4gg
    @NikitaSharma-bs4gg 3 роки тому

    this was THE BEST explanation for this algorithm- nowhere else ! woow

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

      Thank you 😁

  • @ATHYGUNDA
    @ATHYGUNDA 5 років тому +6

    Hey man! I have an exam tomorrow and this video has saved me in this peak time of crisis. Amazing work buddy! good going.

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

      Thanks 😊

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

      Dude learn for the sake of applying in the industry not for the purpose of passing exam

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

    So if we have all edge weights equal to a same value. We can directly use BFS algorithm for calculating shortest path. Am i correct??

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

      Yup!

  • @calebpark7385
    @calebpark7385 4 роки тому +1

    very well explained! thanks so much :)

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

    You are a great teacher with a good intuitive understanding and articulate presentation of what you intend to teach.
    Please try to make a 'part 2 / extension' of most videos wherein you actually Code the whole thing in Java. That's where I and many others fell in love with your channel (referring to your Java C.P. training playlist).

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

      Thanks Akarsh, will try to incorporate that in future!

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

    what's up with the skewed views:like:dislike ratio?

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

    Thanks for all the awesome contents !!!

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

      Thank you :-)

  • @AjeetKumar-he9lk
    @AjeetKumar-he9lk 6 років тому +7

    the initial part pertaining theory was really awesome but Code is where it all went south :(

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

      Sad to hear that Ajeeth. Any pointers?

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

      Feels funny how all software engineers travel south to get a life yet they cant resist south XD

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

      @@gkcs I think you could've explained a couple of iterations with actual figures like you did with the explanation.

  • @KAUSHIKAKALI
    @KAUSHIKAKALI 5 років тому +39

    It's actually pronounced as Daaikstra !!!

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

    Every thing you taught was FAB!
    But brother the worst time complexity for Dijkstra Algo is O(n^3).

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

    9:40 All the unvisited nodes will have distance infinity. How are we comparing distances and choosing node with minimum distance?

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

      The 0th index in distance array has been set to 0. So it will be the minimum

  • @ShaliniNegi24
    @ShaliniNegi24 4 роки тому +1

    Happy teachers' day to the amazing teacher!

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

    Code starts at: 08:47

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

    What does the variable L[u][v] address?

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

    For adjacent vertices you have to check if they're not visited, I think you've missed this condition!

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

    i really appreciate your work that you're doing. Thank you for this.
    Really Helpful.!! 👌👌😎😎

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

      Thanks Akash!

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

    Hi your videos are actually very clear ,What i suggest is Make a seperate lecture on the most often used algos and DS in codechef LONG challenge,If there is any rarely used algos or Datastructures ,or any difficult mathematical induction,please do explain that as well,Ur videos are very Good

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

      Thanks!

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

    Thanks a ton!

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

    In this example you explained the distance between d(0,4) is 24, but the shortest distance between 0->4 is 22 (0->6->7->4), similarly from (0 to 3) is 28 ( 0 -> 6 -> 7 -> 4 -> 3 )

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

      Rakesh Patil I don't think so. At 7:50 you can see how the two paths lengths are computed

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

      OK....I got it

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

    who the fuck disliked this video

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

    Why infinity.?why not zero initially

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

    The spelling of Dijkstra's algorithm on the video thumbnail is worng 'r' is missing.
    Your videos are great you are helping many students to become great programmers.

    • @gkcs
      @gkcs  7 років тому +4

      Thanks for pointing that out Kapil! And thanks for the feedback :-)

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

    Does dijiktra could also be solved via prism's by providing given node as source

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

    I don't know why the number of subscribers to your channel are less than other channels , even though the content is so much better. !! I have loved each one of your videos, specially High Level Design videos. There are no better videos on youtube for HLD. And thank you for replying to all queries in comments . I don't know if anyone else does that! I am going to share this channel with all my friends preparing for interviews :)

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

      Thanks Neha 😁

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

    Is this using matrix or list priority queue?

  • @arungoli9060
    @arungoli9060 11 місяців тому

    I guess, it is graph with greedy algorithm

  • @ManishThakur-jp7zm
    @ManishThakur-jp7zm 4 роки тому +1

    bhai isme proof kahan tha?

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

    We know the ds used in prims and dijkstras is min heap but how the min heap works with dijkstras and prims can u plzz explain

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

    Hello I would just like to say this is a very good video and you have explained this very well, but do you know how you would code this in VB. I already have made up a network of verticies but i dont know how to get it into an array?? please help thank you!!
    (I only want to have 6 nodes and 8 paths but i do not know how to code the algorithm)

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

    Awesome :)

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

    For what f variable taken

  • @shanthureddy4234
    @shanthureddy4234 5 років тому +4

    Crystal Clear :) Thanks bud ....The most confusing part is its pronunciation lol

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

      It's pronounced as how it is written. Di-jik-stra! Lol

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

      @@hawktomnia007 it's actually di-ks-tra

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

    you are really awesome bro :)

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

      Thanks Mahim!

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

    implement it using any language like c, java , etc then i will believe it works. my point is it does not work

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

    Sir pls help us by teaching Kruskals algorithm(Union-Find by Rank).

  • @MAITRY
    @MAITRY Місяць тому

    Abdul Bari's explanation is better.

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

    it's a greedy algorithm and not a dynamic programming algorithm.

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

      It is a dynamic programming algorithm :)

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

      @@gkcs dijkstra is a greedy algorithm

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

      @@gkcs It is actually both greedy and dynamic programming algo. It uses greedy approach to select a node and while exploring its neighbours it uses dynamic programming approach to update distances.

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

    is it dijik or dijk?

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

    nice

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

    Hi Gaurav!
    I have a doubt.
    is the while condition correct?
    since our source node is not there in remaining collection of nodes in graph.
    would not it be N-1?
    i.e visited.size() < N-1 ?
    or else should not we add the line visited.insert(source) just below the set creation?

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

    Hey!, Can you tell how should i go on solving problems. I get the concepts and have solved 50 problems on SPOJ but still struggle on most basic problems. It would be helpful if you make some tutorials on other graph algorithms and suggest some good problems to solve in the end of videos.

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

      Hi Tanveer! You could try to regularly participate in contests and pick up on one problem that you couldn't solve. That teaches us new algorithms pretty fast.

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

      Hey, thanks man.

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

    Does dijiktra also forms minimum cost spanning tree ?

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

      No, It finds out the minimum path from the source nodes and for finding minimum cost edges you need to tweak the weight update part a bit

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

    Virgil Van Dijk...

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

    Could you please explain the difference between O(Nsquared) and O(ElogN)?
    Is the difference in complexity happening due to the size of the graph?
    Or is it happening due to the minExctract() function which can be minimised using a min-heap to obtain lower complexity. ?
    Waiting for your reply Gaurav 😇

  • @ThatControlUser
    @ThatControlUser 11 місяців тому

    its pronounced "dike-struh"

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

    Not a good explantion i think.. You should make your viewer's to visualize the whole thing in their mind.. and you also getting sometimes confused.. Sorry bro., what i realized..

    • @psn999100
      @psn999100 4 роки тому +1

      Lol dude. It was good. You can read more about it and fill in the gaps. I do agree the explanation was not up to Gauravs standard, but was good enough
      Solve this questions using Djikistras algo to truly learn it how it works
      leetcode.com/problems/network-delay-time/

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

    when to use dijkstra and when to use dvr?

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

      What is DVR?

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

    Great!!

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

      Thanks Rushikesh!

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

      Gaurav Sen Can you make videos on other algorithms like flood fill,KMP advance sorting algorithm such as bucket or radix sort?
      It would be great!

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

    how come that 30 became 28 it should remain 30 only????

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

    why does this algo work tho......

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

      Very good question 😛

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

    Isn't the pronunciation of "Dijkstra" something like Dykstra???!!!

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

      Yeah, he misspelled and mispronounced the name. It's spelled Dijkstra and is pronounced something like Di-ek-straa.

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

    Dude.. Are you on Codechef.??.

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

      You can have a look at the link on the channel, or search for gkcs there :-)

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

      Thanks:)

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

    Damn !!

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

    Who's here hours before the exam ?

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

      well I guess you failed it then xD

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

      @@Endrit719 negative piece of shit.

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

      @@Endrit719 the joke here is that exactly in 3 hours I have the final paper for the same exam..😂

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

    dijkstra*

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

    Its Dijkstra's not Dijikstra!! Correctly pronounced as [Dai-stra-'s].

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

      I know 🙈

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

    nice