G-34. Dijkstra's Algorithm - Why PQ and not Q, Intuition, Time Complexity Derivation - Part 3

Sdílet
Vložit
  • čas přidán 26. 09. 2022
  • Disclaimer: Please watch Part-1 and Part-2
    Part-1: • G-32. Dijkstra's Algor...
    Part-2: • G-33. Dijkstra's Algor...
    GfG-Problem Link: bit.ly/3KeZZ7j
    C++/Java/Codes and Notes Link: takeuforward.org/data-structu...
    DP Series: • Striver's Dynamic Prog...
    SDE Sheet: takeuforward.org/interviews/s...
    Check out our Website for curated resources:
    Our Second Channel: / @striver_79
    In case you are thinking to buy courses, please check below:
    Code "takeuforward" for 15% off at GFG: practice.geeksforgeeks.org/co...
    Code "takeuforward" for 20% off on sys-design: get.interviewready.io?_aff=takeuforward
    Crypto, I use the Wazirx app: wazirx.com/invite/xexnpc4u
    Take 750 rs free Amazon Stock from me: indmoney.onelink.me/RmHC/idje...
    Earn 100 rs by making a Grow Account for investing: app.groww.in/v3cO/8hu879t0
    Linkedin/Instagram/Telegram: linktr.ee/takeUforward
    ---------------------------------------------------------------------------------------------------------------------------

Komentáře • 305

  • @takeUforward
    @takeUforward  Před rokem +69

    Let's continue the habit of commenting “understood” if you got the entire video. Please give it a like too, you don't 😞
    Do follow me on Instagram: striver_79

    • @unknown-cj8gv
      @unknown-cj8gv Před rokem +1

      Dil me baste ho tum yr ♥️ humare

    • @raysdev
      @raysdev Před rokem

      Understood! Wow, your mathematical deductive reasoning was very sound & easily followed!
      I appreciate the step by step analysis w/o skipping over any steps. 🙌

    • @rushikeshpol5844
      @rushikeshpol5844 Před rokem

      can you solve skiplist problem on leetcode

    • @deviprasad_bal
      @deviprasad_bal Před rokem

      At any time, a heap can't have same vertex twice, then the heapsize must be log(V) instead of log(V^2). therefore, the maximum heap size can be equal to the number of vertices in the graph, V, and not V^2. Please correct me if I am wrong.

    • @priyanshkumariitd
      @priyanshkumariitd Před měsícem

      understood!!

  • @ishanshah3309
    @ishanshah3309 Před rokem +77

    this TC explanation is brilliant. have never seen such an amazing video of calculating Time complexity. This Graph series I've seen so far is definitely the best (I bet even any paid course wont be able to beat it ) and is on another level.

  • @JayPatel-sn7it
    @JayPatel-sn7it Před rokem +18

    Bro ne pura phd kar diya Time Complexity pe 😂.
    Thanks bhai for such a nice explanation.

  • @codinghustler6771
    @codinghustler6771 Před rokem +35

    I Understood this by your previous graph series. But from this it just make everything clear. Thank you Raj bhaiya for all of your effort.

  • @visase2036
    @visase2036 Před rokem +3

    Finally after days! We were awaiting for the new videos in the Graph playlist! Thank you so much for your immense efforts, Striver.

  • @adityagandhi4712
    @adityagandhi4712 Před rokem +3

    The effort you have put are shown clearly in this video !
    Great explanations.

  • @algorithmsbyaditi
    @algorithmsbyaditi Před 9 měsíci +2

    You explained it really well ! I tried to understand it by reading a lot of blogs and discussions but was not able to but got your explanation. Thank you for this great great video.

  • @nikhilkrishna643
    @nikhilkrishna643 Před rokem +3

    The explanation of the time complexity is just amazing! Thank you so much!

  • @mdyousufgazi4030
    @mdyousufgazi4030 Před rokem +4

    amazing. i really appreciate it how you explained the whole dijktra's algorithm. and the last part where you showed the the time complexity is O(E log V)

  • @user-in2oy4zj2h
    @user-in2oy4zj2h Před rokem +8

    immense amount of efforts put in by you!! hats -off raj bhaiya.

  • @darkstudio3170
    @darkstudio3170 Před 7 měsíci +6

    Guys many of you having doubt that heap size is worst case V2 then while loop should also run V2 times. First of all, the assumption taken that Max Heap Size will be in worst case V2 is itself is wrong . Adding V2 node in any case is not possible. You can dry run , you will find that the distance condition wont allow V2 node to the queue in any case possible + the every node we will processed (pQ.poll()) wont be added to the queue again (you can marked those node vis also , wont have any effect on ans), In worst case heap size can go nV where nELogV. Period

    • @janedoe1721
      @janedoe1721 Před 5 měsíci

      I agree, the only difference I think is that a node at max will have V-1 edges, not E. so, V(logV + (V-1).logV) = V^2.logV=ElogV. Let me know if this makes sense

    • @vaisakh5802
      @vaisakh5802 Před měsícem

      @@janedoe1721 im not able to understand how v2=E. can you explain?

    • @jotsinghbindra8317
      @jotsinghbindra8317 Před měsícem

      @@vaisakh5802 in densest graph there will be V nodes and each of them connected to V-1 nodes => total edges=v*v-1

  • @sunny_23561
    @sunny_23561 Před 7 měsíci +1

    This video just blew my mind.... Huge thanks Raj bhaiya... Now I am really willing to spend next 2-3 hours to concrete all the concepts on my own regarding this Dijkstra's Algo and shortest path.... "UNDERSTOOD"

  • @chirayusanghvi8885
    @chirayusanghvi8885 Před 11 dny

    One of the best time complexity analysis explanation, thank you !! 🙏

  • @ntsequalifier341
    @ntsequalifier341 Před 3 měsíci +1

    I loved the way u said hit that like button you really work good. I mean u have upgraded the level of DSA taught by any Indian teacher.

  • @cinime
    @cinime Před rokem +1

    Understood! Wooow! Super fantastic explanation as always, thank you very much!!

  • @user-lw9dj8we7k
    @user-lw9dj8we7k Před 8 měsíci

    this is the top 1 rating video from my perspective for understanding of working flow time complexity throughout the code.

  • @sambhavagarwal5871
    @sambhavagarwal5871 Před rokem +6

    understood all 3 videos. amazing striver this graph Series is best in the best as well as paid courses
    Please upload more videos if its there thanks a lot striver

  • @nishanthazarikab9166
    @nishanthazarikab9166 Před 7 měsíci

    Amazing explanation of the derivation .I have never seen any CZcamsr going into such a deep concept..

  • @CodeMode9313
    @CodeMode9313 Před 11 měsíci +1

    habibi tum time complexity mast samjai ho ....hum khush hui ..tum accha kaam karti

  • @shreyarawatvlogs6920
    @shreyarawatvlogs6920 Před 6 měsíci +1

    JUST MINDBLOWING!!! Thanks a ton!!!!

  • @athangkulkarni8245
    @athangkulkarni8245 Před 5 měsíci

    Understood.
    Thank you Striver!

  • @Ritik_Mangal
    @Ritik_Mangal Před 12 dny

    striver bhai, you will become a person which will be remembered in history forever.😊

  • @lakshaysharma852
    @lakshaysharma852 Před rokem +1

    Much needed Awesome explanation !!!!

  • @amantripathi4100
    @amantripathi4100 Před rokem +3

    brother u nailed it>>>>> RESPECT

  • @abdulqadirboxwala5414
    @abdulqadirboxwala5414 Před rokem +2

    this is a type of content that I would not give a second thought for even paying for it

  • @piyushk_071
    @piyushk_071 Před 10 měsíci +1

    Best Explaination of Time complexity!✨

  • @shigoeditz7079
    @shigoeditz7079 Před 6 měsíci

    What an Explanation man hat's off to you 🤯🤯🤯

  • @vijethkashyap151
    @vijethkashyap151 Před měsícem

    Time complexity explanation is the best! Thanks!

  • @avi7629
    @avi7629 Před rokem +1

    Kya padhaya hain bhyii 🔥🔥🔥🔥🔥🔥🔥 aag ekdum

  • @Kokowangmini
    @Kokowangmini Před rokem +1

    Understood. Thank you so much.

  • @hemachel175
    @hemachel175 Před měsícem

    Understood. Thanks for all the effort.👌

  • @niteshverma8281
    @niteshverma8281 Před rokem +1

    you are the best youtuber in the world for coding ♥♥♥♥♥♥

  • @piyushsaxena6243
    @piyushsaxena6243 Před 10 měsíci +1

    really love your explanations

  • @adityan5302
    @adityan5302 Před měsícem +1

    I suspect that you're the creator of these Data Structures bro.... Just simply amazing... Love u bro. I succeded in one of the interviews because of u. This is because of you♥

  • @vakhariyajay2224
    @vakhariyajay2224 Před rokem +1

    Thank you very much. You are a genius.

  • @rohitn6333
    @rohitn6333 Před rokem +1

    sir the time complexity explanation was very good. thank you :)

  • @namamiNarayana
    @namamiNarayana Před rokem +2

    Understood! :)
    Thank you for your invaluable efforts striver! _/\_ ^^

  • @rishabhagarwal8049
    @rishabhagarwal8049 Před rokem +1

    Understood Sir, Thank you very much

  • @satraprathore5349
    @satraprathore5349 Před 26 dny

    Awesome derivation, Hats off

  • @prathamkapoor2513
    @prathamkapoor2513 Před rokem +1

    Time Complexity explanation was awesome

  • @aasifali9139
    @aasifali9139 Před rokem +1

    fully understood. Thx a lot

  • @RahulSharma-ht2xz
    @RahulSharma-ht2xz Před 11 měsíci

    TC explanation was on another level

  • @NiveditaKar-xj6ew
    @NiveditaKar-xj6ew Před 3 měsíci

    Thank you so much for this amazing explanation Raj

  • @amanasrani6405
    @amanasrani6405 Před měsícem

    Striver, Shiv Baba Bless you , you are like God's swarup

  • @Nirala_414
    @Nirala_414 Před rokem +1

    Mastering Graph ❤️🙌🏻

  • @piyushjaiswal9192
    @piyushjaiswal9192 Před rokem +1

    One of the best videos of Dijkstra's Algorithm on the internet.

  • @susmitsingh1775
    @susmitsingh1775 Před 8 měsíci

    you are always amazing man!!

  • @mriduljain6809
    @mriduljain6809 Před rokem

    Amazing Explanation Bhaiya

  • @anshumanbehera3706
    @anshumanbehera3706 Před rokem

    understood thank you for such amazing video

  • @BiswajitDas__0
    @BiswajitDas__0 Před měsícem

    Amazing Explanation.

  • @techgamer4291
    @techgamer4291 Před 3 měsíci

    Great work !!

  • @harshkilaji
    @harshkilaji Před 6 měsíci

    The derivation was marvelous

  • @rpdesigns3545
    @rpdesigns3545 Před měsícem

    I think the time complexity with queue will be same as of the priority queue just take the queueq and instead taking distance from the pair take it from the distance matrix. for the eg u took we will have node 3 twice in the queue and the distance matrix will have min distance of 3 to node 3. And we will calculate the distance to node 4 and 5 and pop and now when we will come to the next node 3 in queue the condition distance[node 3]+edgewt of 5

  • @b_01_aditidonode43
    @b_01_aditidonode43 Před 9 měsíci

    understood sir...amazing one

  • @prithvisen6560
    @prithvisen6560 Před měsícem

    Great Explanation ❤

  • @avi7629
    @avi7629 Před rokem

    Dedication level op 🔥

  • @yaswanthkosuru
    @yaswanthkosuru Před rokem +1

    This is really good❤❤❤❤❤❤

  • @visheshagrawal8676
    @visheshagrawal8676 Před rokem

    bhai content hai maja aagya ✨✨✨✨

  • @asthaastha9041
    @asthaastha9041 Před rokem

    Understood bhaiya!!!

  • @supratimnayek2776
    @supratimnayek2776 Před rokem

    Great Explanation

  • @suiwala
    @suiwala Před rokem

    This was something great !!!

  • @mahirneema136
    @mahirneema136 Před rokem +2

    great explanation 😇

  • @mightygerman
    @mightygerman Před 28 dny

    Very very very nice explanation....

  • @juniorboy1903
    @juniorboy1903 Před rokem +1

    Understood bhaiya 😀

  • @gauristar4094
    @gauristar4094 Před 27 dny

    commedable job once again striver!!!

  • @arjunju469
    @arjunju469 Před 5 měsíci +1

    God level explanantion

  • @top_g755
    @top_g755 Před rokem +1

    We take PQ instead of q to reduce no. Traversal (unnecessary) using PQ what we do is in initial traversal we used to choose the minimum one

  • @UCSSaloni
    @UCSSaloni Před 9 měsíci +2

    00:00 Explaining Dijkstra's algorithm and why priority queue is used
    01:58 Using a priority queue can save time in pathfinding
    03:47 Optimize distance calculation by using minimal distance
    05:40 Using priority queue to reach minimal nodes first reduces unnecessary exploration of parts.
    07:25 The while loop runs for V, which is the total number of nodes.
    09:16 Optimizing priority queue operations in graph algorithms
    11:12 Pushing nodes in worst-case scenario results in V^2 Heap size
    12:59 Explaining the time complexity of Dijkstra's algorithm
    Crafted by Merlin AI.

  • @AJ-xc3ks
    @AJ-xc3ks Před 8 měsíci

    Striver U are just Op..❤❤❤🙏

  • @againstcorruption9672

    understood☺
    Thank You!

  • @user-kl3qv1sc8k
    @user-kl3qv1sc8k Před měsícem

    Amazing explaination

  • @poojithkumar2283
    @poojithkumar2283 Před rokem +1

    Hey striver I have understood the time complexity very well can you please explain time complexity of bfs and dfs once🙂🙂

  • @channel-te5vk
    @channel-te5vk Před rokem

    Great. Understood

  • @Pooja-we3xs
    @Pooja-we3xs Před rokem

    Understood🔥🔥

  • @UECAshutoshKumar
    @UECAshutoshKumar Před 9 měsíci +1

    Thank you sir ☺️

  • @The_Shubham_Soni
    @The_Shubham_Soni Před 10 měsíci

    UNDERSTOOD.

  • @codingisfun-pranayharishch3001

    Amazing explanation sir

  • @itz_me_imraan02
    @itz_me_imraan02 Před rokem

    It got clear now...i was wondering the same dat even Queue gives the same answer...understood now dat it gets TLE due to too many unnecessary paths calculations....

  • @imajt5
    @imajt5 Před rokem

    superb explaination

  • @GhostVaibhav
    @GhostVaibhav Před 5 měsíci

    Understood🔥

  • @lokeshroy3944
    @lokeshroy3944 Před 10 měsíci

    amazing explanation

  • @chahatsingla5982
    @chahatsingla5982 Před 5 měsíci

    great explanation

  • @ekanshlohiya98
    @ekanshlohiya98 Před rokem +5

    If the outer loop is running while the heap is not empty then it will also run for the size of heap times i.e., O(v2). I think one optimization is possible here.
    maintain the count of visited nodes say ct and as soon as the ct becomes n, just break from the priority queue.
    Now we know, that priority queue picks only least cost path for the node, so mark it as visited since the least cost for that node is already done and increase the count of visited nodes.
    So, as soon as shortest path for all nodes is calculated from priority queue, the entries which are not optimal will not need to be popped out and we will save the extra time.
    vector dijkstra(int n, vector adj[], int src)
    {
    priority_queue pq;
    vector dist(n,INT_MAX);
    dist[src] = 0;
    pq.push({0,src});
    int ct = 0;
    vector vis(n,0);
    // vis[src]=1;
    while(!pq.empty()) //o(v)
    {
    if(ct==n) break;
    auto d = pq.top().first;
    auto node = pq.top().second;
    pq.pop();
    //log(heap size) -> log(v2)
    //worst case heap size O(v2)
    //everyone is connected to every other one so v-1 edges for v nodes each
    if(vis[node]) continue;
    vis[node]=1;
    ct++;
    for(auto it:adj[node]) //o(v-1)
    {
    int u = node;
    int v = it[0];
    int wt = it[1];
    if(!vis[v] and dist[v]>dist[u]+wt)
    {
    dist[v] = dist[u] + wt;
    pq.push({dist[v],v}); //log(v2)
    }
    }
    }
    return dist;
    //O(v * (log(v2)+(v-1)(log(v2))))
    //O(v * log(v2)*(v))
    //O(v2 log(v2))
    //O(2 v2logv)
    //O(ElogV)
    }

    • @spydycoder6668
      @spydycoder6668 Před rokem

      correct bro

    • @gouravgarg1069
      @gouravgarg1069 Před rokem

      if its so,why we need to check and update for same node again again i think pq picks "node with least cost from source" not "least cost path for the node"

    • @anmoljain8327
      @anmoljain8327 Před rokem +2

      I also had the same doubt, that if we will allow the loop to run till priority_queue becomes empty then, the normal queue and priority_queue will take the same time. you found the solution.

    • @darkstudio3170
      @darkstudio3170 Před 7 měsíci

      this wont work , you correct about the time complexity part though . The thing you may have V2 node in queue but not all of them will be processed hence V2 wont be multiplied to inner part . In generall in it is fair to say that nV node will be process where n

    • @prathyush6508
      @prathyush6508 Před měsícem

      You're right bro, but if that is the case then priority queue should be faster than set as we are still doing the same thing as set but not removing any elements.

  • @garimakumari4346
    @garimakumari4346 Před rokem +1

    thanku understood😁

  • @saibunny1253
    @saibunny1253 Před 3 měsíci

    understood striver bhai

  • @udaytewary3809
    @udaytewary3809 Před rokem

    Understood bhaiya

  • @_hulk748
    @_hulk748 Před rokem

    Thankyou sir understood ✨❤🙇‍♂🙏

  • @anubhavsingh8102
    @anubhavsingh8102 Před rokem

    Understood Bhaiya

  • @PushpSoodSde
    @PushpSoodSde Před 9 měsíci

    awesome, understood :)

  • @shyamalaravind5906
    @shyamalaravind5906 Před 9 měsíci

    Understood!

  • @user-fs8km9qc8f
    @user-fs8km9qc8f Před 27 dny

    very nice explanation

  • @abhijeetbasfore6816
    @abhijeetbasfore6816 Před rokem +2

    understood all 3 videos. time complexity part was awesome

  • @AbhishekMishra-jl9yh
    @AbhishekMishra-jl9yh Před 11 měsíci

    Hats off👏

  • @bhavya8608
    @bhavya8608 Před rokem

    understoood!!!!

  • @talkswithRishabh
    @talkswithRishabh Před rokem

    Understood 😊

  • @user-km8en8ym1h
    @user-km8en8ym1h Před 19 dny

    Best explanation

  • @parvahuja7618
    @parvahuja7618 Před měsícem

    very indepth explaintion

  • @-VLaharika
    @-VLaharika Před rokem

    Understood 👍

  • @suryakiran2970
    @suryakiran2970 Před rokem

    Understood❤

  • @SumitKeshariMCS
    @SumitKeshariMCS Před rokem

    One word: *Bravo*