Dijkstra's Algorithm Single Source Shortest Path Graph Algorithm

Поделиться
HTML-код
  • Опубликовано: 4 фев 2025

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

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

    Aapse acha teacher aaj tak nhi dekha...itna ache se kisi ne nhi smjhaya ye sab...you are really good ❤️❤️

  • @sidhantakumarpattnaik2962
    @sidhantakumarpattnaik2962 9 лет назад +74

    You are awesome .I viewed almost all videos and its clear my concept , which not being cleared by seeing other videos .
    Keep posting new videos and this will help us .

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

      bro do these videos help in company placement.does it helped you...

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

      Obviously!! He works at Apple. Hope this answers your question.

  • @sunnykeshshandilya1156
    @sunnykeshshandilya1156 8 лет назад +18

    Awesome man. You make any algorithms so easy and teaches 3hr things in 15 mins and concept given is very solid. One small thing missed in video.. explanation for time complexity O(E log V)

  • @Dreamazium
    @Dreamazium 7 лет назад +18

    You do seem to be able to explain things much better than others. Uni is rubbish at explaining things while you have given me at least given me a chance of understanding it.
    It seems much simpler now you have explained it. Thank you.

  • @BackToBackSWE
    @BackToBackSWE 6 лет назад +6

    Loved the video. This was a fundamental concept in our Object Oriented 2 class.

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

    One of the best and simplest videos for understandng dijkastra's algo.. Hats off Tushar... Great work!

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

    I just looked for a video from you about Dijkstra. You're the best teacher I've found in RUclips.

  • @wth5715
    @wth5715 8 лет назад +10

    I`m student in korea. Your good explanation Thanks Tushar Roy~

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

    you helped me in bachelors and need you again in masters god bless you

  • @anamigator
    @anamigator 6 лет назад +32

    If you are still guessing, why the O(E log (v)) complexity, here is an explanation. You visit each edge in this graph at-least once (without which you cannot reach all the vertices). That would be O(E). However, at each edge, you are looking at the priority queue (heap) and doing an extractMin() operation which takes log(n) time in general, for n elements in the heap. Since in the worst case, you will have as many as V elements in the heap, each edge traversal means, a O(log V) time to extractMin from the priority queue(heap). So E times log(v).

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

      What about construction of the heap? Isn't it O(v) ?

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

      Isnt extract min operation O(1) while insertion is O(logV)

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

      @@maksimmartianov5189 The highest degree term is considered in time complexity and hence E will be always >= V, therefore we can say total time complexity as O(E log (v))

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

      @@amurto630 Extract-Min in heap is an operation in which we extract root i.e. min element and then replace it with last level rightmost element and then heapify, so the total time to do this will be O(log V).

  • @mesodylaughter9327
    @mesodylaughter9327 15 дней назад

    Thought I will never get an idea till I found this channel 😂

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

    The other videos explaining the algorithm aren't bad, but this is the best one when trying to understand the pseudo code and data structures - thanks!

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

    Dude, these lessons are really simple and easy to grasp.
    Thanks a lot, definitely helping me in my quiz tomorrow!! KEEP IT UP! :)

  • @AvaisAhmad
    @AvaisAhmad 9 лет назад +9

    I really liked your videos.
    Can you please upload videos on some of following topics?
    ->asymtotic complexity
    -> n queen problem
    ->travelling salesman problem
    ->karp-rabin
    ->strassen matrix multiplication
    ->randomized algorithms las vegas and monte Carlo
    ->Amortized analysis
    ->LC Branch and Bound
    ->FIFO Branch and Bound
    ->Multistage Graph
    ->Flow Shop Scheduling
    ->binomial heap
    ->fibbonaci heap

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

    absolutely fantastic explanation! Thank you so much for going through even the most obvious bits as now I have a -rough- idea of how i'm actually going to implement part of my algorithmics C assignment! cheers.

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

    Very nicely explained, especially the complexity part. I could not understand that from other sources. thank you!!

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

    Roy's doing things for global audience, I benefit a lot from his videos as a Chinese student.

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

    Thanks God man. you saved me from quitting learning...

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

    Thanks for the video . I am new for learning in youtube . i felt lot of difference to learn from videos . i have seen many videos recently , yours is fantastic . with code base .

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

    Tushar, thank you so much for your effort. It helps me to understand many things and succesfully take my exams. Thank you!

  • @rafamassa
    @rafamassa 9 лет назад +3

    First Dijkstra's explanation ever that allowed me to visualize the data transformations!

  • @GabrielaSwanson
    @GabrielaSwanson 7 лет назад +2

    Great video! Thank you so much for your clear explanations and demonstrations.

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

    Your videos are so much better and informative than Siraj ravla's!! Great job!!

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

    Great Video and this channel is my bible for programming questions.

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

    Watched many of your videos, and most of these videos are well explained - good for freshers to computer science, as well as those who want to dust up academics in preparation for new opportunities. Want to commend you on all your great efforts.

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

    your lectures were very helpful me....thanks Tushar for your wonderful videos

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

    dude, your vids are like gold to self-learning coders :) Really appreciate the efforts!

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

    I like your videos a lot. This explanation of Dijkstra algorithm is one of the best I've ever seen.
    Have you ever thinking about make a video of A* algorithm or another search algorithm that uses heuristics?
    Keep making videos :) Thanks.

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

      Really? I didn't see it. Thanks :)

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

    Thanks man, for keeping it simple and clear unlike others

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

    Excellent, clear explanation. Well-done.

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

    U r not Tushar u r an ultimate solution

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

    HEY I want to thank you, I will forever be grateful if I pass my exams thanks to you! keep it up man!

  • @100vivasvan
    @100vivasvan 7 лет назад

    You are AWESOME!
    Thank you so much God bless you! awesome video! Helped me a lot in understanding this i had struggled a lot with this algo.

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

    One correction at 11:53 we apply extract min V times and decrease key E times. And not both E times.

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

    Great Explanation! Thank you so much Tushar!

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

    Thank you very much, Tushar. Really helpful tips and efficient code.

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

    Should use D-ary heap instead of Binary heap w/ Dijkstra's algo. D is tree order (i.e. max # of children of any node). D=log2(V) (D=E/V is another way, but log2(V) gives better performance). removemin and decrease are the 2 prominent OPs among which decrease is more frequent (O(E), removemin at O(V)). decrease causes swim whereas removemin causes sink. swim causes comparison b/w a node with it's parent, all the way till the root if required, whereas sink causes getting the smallest among all children and comparing that with the node, all the way till final level if required. So swim performs better with D-ary heap whereas sink performs better with binary heap. But since swims are more frequent than sinks, D-ary heap has to be used with D=log2(V) to strike a balance in the performance of sinks and swims. sink and swim run in O(log2(V)) time with binary heap. With D-ary heap they run in O(log(V)) time =~ O(log(V)) time

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

    Wish I could have viewed these videos earlier I could have been much better in algorithims .Thankyou sir !

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

    Awesome explanation man, you saved me for my final tomorrow :).

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

    Thanks for doing a video on Dijkstra!

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

    So helpful and clear. Thank you so much.

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

    Very nice explanation. Thank you.

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

    thankyou sooo much !!! an IP university student

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

    Nice Video... Please, Put captions above progress bar....

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

    at first i was using dfs to reach all vertex and update but that method was flawed thnx to him i used bfs and got correct ans for each test case

  • @kunalkathpal7167
    @kunalkathpal7167 9 лет назад +2

    buddy you are soo good AND you explain very well!!

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

    Clear explenation....verry helpful

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

    The legend in explaining and teaching :)

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

    Amazing explanation man .. that will really help me into my project.
    greetings ..

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

    Man, i am speechless. Awesome video.

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

    Hi Tushar,
    Your explanations on the algorithms are brilliant. Have a doubt on extracting minimum value from the map. Why are we extracting minimum value from the map? Picking up any random value/max value from the map will give the same final result. Since for every vertex, we are comparing already found distance and new distance. Only observation I found that using minimum one form the map reduces number of comparisons. Can you please put your thoughts on selecting the minimum one from the map instead of any other value?

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

      +madat brahma I think we extract the minimum because the distance cannot be updated once it has been removed. What I mean is, if you look at his example on the board, D is the last element to get extracted. If it was the second (after the source which is A), then it would be extracted with the distance 9. This is then used to compare other paths with and therefore should not be updated (as any path that relies on D would then have to have their distance updated as well). Extracting the minimum guarantees that all the data in the distance map is the smallest value possible for that vertex.
      To explain this better, lets say there is a graph of 4 elements ABCD, connected like a square. A is the source, so it is extracted first. It is connected to B and D. Let's say that the edges for AB is 2, BC is 3, CD is 2, and AD is 8. If you extract at random, D could be removed next, making D have a distance set to 8. Then when you eventually extract C, it would be seen that D's distance could be updated to 7, which is less than 8. This isn't a problem at first. But if there were 5 elements, with a node E connected to D but no others, then this would require you to update the distance of E as well, because it is reliant on D's distance. This would introduce another loop of O(v) time every time you update a distance (since you have to check all the nodes that are reliant on that nodes distance). This increases the time complexity since it would occur inside the for loops that are already O(E lg (v)) (I think that's the time complexity, I could be remembering that wrong). By extracting the minimum, it guarantees that anything dependent on that node will not have to have its distance updated once it is extracted.

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

    such a clear explanation thank you!!

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

    Why do we need a Min heap ? Can we not store in the Queue instead and have a visited array to keep track of which nodes are visited prior. Although you can modify the distance but do not put back to the queue if it is already visited.

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

    Why the playlist is not in organized manner ?
    But the video was awesome :)

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

    Hi Tushar, I watched many of your algo videos... Many of the concepts are clear now.
    First of all A BIG THANKS for your videos.
    In programming part in many videos, you have used Map, LinkedList, Queue and so on.
    Can you post some videos where programming is done only using Arrays and Objects? Arrays like any array of objects rather using predefined java classes. e.g. int[], Object[], etc.
    As i need to appear few exams where we can not import any predefined class apart from java.util.Scanner;

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

    Thanks for your nice tutorial. You have mentioned this method inside Vertex class. Can you please describe this method
    @Override
    public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + (int) (id ^ (id >>> 32));
    return result;
    }

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

    excellent....good explanation

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

    videos are awesome .
    After doing the concept part can you please add complexity of each algorithm?

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

      Complexity for
      travelling salesman problem
      0/1 Knapsack
      optimal BST
      N * queen problems

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

    Great videos buddy. Awesome job!

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

    Hi Tushar,
    Thank you for your excellent explanation- just as always. I have only one question- how is the space complexity O(E + V) when in all three data structures we only store at most V elements where V is the number of vertices in the graph? This is the only thing I don't understand. Thank you :)

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

      the graph you are searching contains a list of vertices and their edges so I guess the space complexity is just the size of the input data in this case because the other structures are smaller

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

      Hi

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

    Hi,
    Can you make a video explaining the code from the Graph, Vertex and Edge classes that you have in the link you've provided? To be specific, I want to know the significance of the overridden functions that you've written and why they're coded that way.
    Thanks!

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

    Really good explanation!

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

    love u from iran semnan university

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

    I hardly comment on videos. But man you are awesome. BTW are you IITian?

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

    at 8:30 time what would be the value of D in Map+heap table if AD edge have weight

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

      The value of D would be = Weight of edge AD(less than 7)

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

    Hey Tushar, we're not just extracting the minimum value but we're also removing it from the map + heap. However, in your video about Prim's, you haven't explained how to remove the minimum value from the data structure.
    Obviously, if we remove the top element (which is the minimum), the position of all the other elements change and in such a case, how do we update the map (which contains the positions)?

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

      Here's what I did: after removing the element whose index is 0 in the heap (since it would be the lowest value), I am decrementing the index of every other element in the hash map by 1.
      So the element whose value in the hash map was previously 1, now becomes 0. 2 becomes 1 and so on, which is also what happens to the heap array naturally after we pop out the element at the 0th position.
      Thank you so much Tushar, my code is working fine, this is the first time I've implemented Dijkstra's algorithm, feels good :)
      Even though my code is working fine, someone do let me know whether what I did was the correct thing to do. It may be possible that my code breaks in some special cases.

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

    Genius you helped me find the error

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

    u r excellent..Its helping ..

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

    awesome man, u r really helpful

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

    @1:30 "The link to that video is also attached in this video here"
    No it's not. What is a heap? I don't know

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

    Hello @Tushar Roy why are we not using java.util.PriorityQueue and instead our own version using BinaryMinHeap? can someone explain?

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

    How to find w when both value are same... Which one should be considered as minimum?

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

    Thanks, this helped me a lot !

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

    Great explanation..

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

    is Dijkstra's algo, a greedy method or dynamic or both?

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

    very well explained..

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

    can we use priority queue of java.util package in place of our min heap ??

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

    Perfect as always..!

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

    Your videos are top!

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

    in this vedio u used heap+map to store the information about all the non-evaluated node, but ur implementation is in java, i wanted to know do we have something like that in c++ too, i though of using an array but finding minimum item in it would be O(size of array) and that is a costly affair,
    hope to get some help.

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

      i had used set and map to implement that, i thought of using priority_queue but at the time i didn't know how to make a min priority_queue in c++(since it is max priority queue by default), although i now know that , anyways thanks

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

    Excellent explanation!

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

    how can we update the distances, once the variable is sent in priority queue?

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

      I think the easiest way is to remove the element and insert a new one.

  • @1gouravgg
    @1gouravgg 8 лет назад

    Vertex class not found in your java Dijkstra’ code.. how to fix this?

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

      Gourav Goyal it is declared in Graph class as inner class

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

    anyone know where the Vertex class came from in his code? I'm assuming its his own implementation

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

      Yes , its his own implementation. Graph ds can be implemented in lot of ways. Check his github github.com/mission-peace/interview/blob/master/src/com/interview/graph/Graph.java

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

    why didn't you just use set, or priorityQueue and prefer Heap and Map together

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

    Where is the link for graph class?

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

    Bro your voice tore my ear drums, but aside from that good tutorial!

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

    WHY WHY in The world when a programmer is born he says "Hello World" :)

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

    Awesome Video

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

    nice

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

    Great work

  • @NitinNataraj-gf3vx
    @NitinNataraj-gf3vx 7 лет назад

    god bless your soul

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

    You got the time complexity wrong. According to Fredman & Tarjan (1984), it should be O(E + V*log(V)) .

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

    Hi Tushar... Very Well Explained
    I have implemented the code from scratch after watching your explanation part.
    github.com/abhishekhandacse/Coding_Java/blob/master/src/geeksforgeeksimproved/graphs/DijkstraAlgorithm.java

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

    what is vertex class here

  • @AbhishekKumar-id4om
    @AbhishekKumar-id4om 7 лет назад

    how you build heap + map data structure. Can anyone explain

  • @AP-eh6gr
    @AP-eh6gr 8 лет назад

    awesome, thanks a lot

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

    Thank you.sir.

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

    well explained (y)

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

    You are awesome! Please share the codes in C++ too.

    • @VIKASCHOUDHARY-id5ez
      @VIKASCHOUDHARY-id5ez 7 лет назад

      #include
      using namespace std;
      vector dijkstra(int n,int source, vector G[]) {
      int INF = (int)1e9;
      vector D(n, INF);
      D[source] =0;
      set Q;
      Q.insert({0,source});
      while(!Q.empty()) {
      auto top = Q.begin();
      int u = top->second;
      Q.erase(top);
      for(auto next: G[u]) {
      int v = next.first;
      int weight = next.second;
      if( D[u] + weight < D[v] ) {
      if(Q.find( {D[v],v})!=Q.end())
      Q.erase(Q.find( {D[v], v} ));
      D[v] = D[u] + weight;
      Q.insert( {D[v], v} );
      }
      }
      }
      return D;
      }
      int main(){
      int n,m,s,x,y,z;
      cin>>n>>m>>s;
      //Input the number of nodes(0 based), number of edges and the source vertex.
      vector *G=new vector[n];
      vectorans;
      for(int i=0;i>x>>y>>z;
      //Input the starting vertex of the edge, the ending vertex and the cost of the edge.
      G[x].push_back(make_pair(y,z));
      }
      ans = dijkstra(n,s,G); //ans has the cost from source to all the vertices.
      for(int i=0;i