G-34. Dijkstra's Algorithm - Why PQ and not Q, Intuition, Time Complexity Derivation - Part 3
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
---------------------------------------------------------------------------------------------------------------------------
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
Dil me baste ho tum yr ♥️ humare
Understood! Wow, your mathematical deductive reasoning was very sound & easily followed!
I appreciate the step by step analysis w/o skipping over any steps. 🙌
can you solve skiplist problem on leetcode
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.
understood!!
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.
indeed
True❤
true
Bro ne pura phd kar diya Time Complexity pe 😂.
Thanks bhai for such a nice explanation.
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.
Finally after days! We were awaiting for the new videos in the Graph playlist! Thank you so much for your immense efforts, Striver.
The effort you have put are shown clearly in this video !
Great explanations.
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.
The explanation of the time complexity is just amazing! Thank you so much!
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)
immense amount of efforts put in by you!! hats -off raj bhaiya.
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
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
@@janedoe1721 im not able to understand how v2=E. can you explain?
@@vaisakh5802 in densest graph there will be V nodes and each of them connected to V-1 nodes => total edges=v*v-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"
One of the best time complexity analysis explanation, thank you !! 🙏
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.
Understood! Wooow! Super fantastic explanation as always, thank you very much!!
this is the top 1 rating video from my perspective for understanding of working flow time complexity throughout the code.
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
Amazing explanation of the derivation .I have never seen any CZcamsr going into such a deep concept..
habibi tum time complexity mast samjai ho ....hum khush hui ..tum accha kaam karti
JUST MINDBLOWING!!! Thanks a ton!!!!
Understood.
Thank you Striver!
striver bhai, you will become a person which will be remembered in history forever.😊
Much needed Awesome explanation !!!!
brother u nailed it>>>>> RESPECT
this is a type of content that I would not give a second thought for even paying for it
Best Explaination of Time complexity!✨
What an Explanation man hat's off to you 🤯🤯🤯
Time complexity explanation is the best! Thanks!
Kya padhaya hain bhyii 🔥🔥🔥🔥🔥🔥🔥 aag ekdum
Understood. Thank you so much.
Understood. Thanks for all the effort.👌
you are the best youtuber in the world for coding ♥♥♥♥♥♥
really love your explanations
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♥
Thank you very much. You are a genius.
sir the time complexity explanation was very good. thank you :)
Understood! :)
Thank you for your invaluable efforts striver! _/\_ ^^
Understood Sir, Thank you very much
Awesome derivation, Hats off
Time Complexity explanation was awesome
fully understood. Thx a lot
TC explanation was on another level
Thank you so much for this amazing explanation Raj
Striver, Shiv Baba Bless you , you are like God's swarup
Mastering Graph ❤️🙌🏻
One of the best videos of Dijkstra's Algorithm on the internet.
you are always amazing man!!
Amazing Explanation Bhaiya
understood thank you for such amazing video
Amazing Explanation.
Great work !!
The derivation was marvelous
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
understood sir...amazing one
Great Explanation ❤
Dedication level op 🔥
This is really good❤❤❤❤❤❤
bhai content hai maja aagya ✨✨✨✨
Understood bhaiya!!!
Great Explanation
This was something great !!!
great explanation 😇
Very very very nice explanation....
Understood bhaiya 😀
commedable job once again striver!!!
God level explanantion
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
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.
Striver U are just Op..❤❤❤🙏
understood☺
Thank You!
Amazing explaination
Hey striver I have understood the time complexity very well can you please explain time complexity of bfs and dfs once🙂🙂
Great. Understood
Understood🔥🔥
Thank you sir ☺️
UNDERSTOOD.
Amazing explanation sir
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....
superb explaination
Understood🔥
amazing explanation
great explanation
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)
}
correct bro
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"
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.
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
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.
thanku understood😁
understood striver bhai
Understood bhaiya
Thankyou sir understood ✨❤🙇♂🙏
Understood Bhaiya
awesome, understood :)
Understood!
very nice explanation
understood all 3 videos. time complexity part was awesome
Hats off👏
understoood!!!!
Understood 😊
Best explanation
very indepth explaintion
Understood 👍
Understood❤
One word: *Bravo*