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
@KiranKumar-sb3ti5 ай бұрын
understood
@U-DAY2 жыл бұрын
Striver we love you so much... and you r my big motivation and i learn english and expalining skills from u along with the concepts and It became like a habit for me whenever i solve a problem , i start hey striver listen and after explaining to u , i will go for code that much deeply connected i am.. and I was from south so i dont even know hindi that much but after watching no of videos of urs , hindi and english and ur slang i too adopted from u ... Please don't go from u tube we need to daily hear you and see you and take care my Brother...❤
Bhaiya apne itna acha samjhaya mera chota bhai bada hogaya
@ManishKumar-dk8hl6 ай бұрын
@@shashwatkumar6965 😂😂
@knowevertythingaboutyou5 ай бұрын
kuch bhi🤣🤣🤣
@nidhinishad7801 Жыл бұрын
People who are coding in python can use the sortedcontainers and import SortedSet from that. Here is the code: from sortedcontainers import SortedSet class Solution: #Function to find the shortest distance of all the vertices #from the source vertex S. def dijkstra(self, V, adj, S): s = SortedSet() dist = [float('inf') for i in range(V)] # dis,node s.add((0,S)) dist[S]=0 while s: dis,node = s[0] s.remove((dis,node)) for i in adj[node]: adjNode,edgW = i if dis+edgW
@AshmitIITH5 ай бұрын
thanks bro
@harshitpandey83042 жыл бұрын
Comparison b/w PQ and set : Incase of PQ, the maximum heap size can go upto E = number of edges, leading to complexity = O(E*logE). Incase of set, the maximum set size can go upto V =. number of vertices, leading to complexity = O(E*logV). Happy to be corrected :)
@lakshsinghania Жыл бұрын
After watching the G-34 video and commenting this for the first one: as u said the maximum heap size can go upto E thats V² as considering the extreme case that striver have taken in G-34 for every node its connected to V-1 nodes and there are V nodes in the graph so (V-1)*V = V² thats the max heap size so the while(!pq.empty()) line runs for V²=E or V only ? i think u r correct as the example taken in G-32 video the pq is taking 7 items and there are only 6 nodes so this line cant run for like max V times O(V² * (Pop vertex from pq) + no of edges on each node * Push vertex into pq) O(V² * (log(heap size) + ne * log(heap size)) // taking log(heap size) as common term O(V² * (log(heap size) * (V-1 + 1)) O(V² * (log(heap size) * (V)) O(V²*V * (log(heap size)) O(V²*V x (log(V²)) and V²=E ( total no of edges) therefore its *O(E*V * Log E)* im not getting *O(E*logE)* this for pq
@lakshsinghania Жыл бұрын
could u say whether im wrong or not ? if yes where ?
@nagame859 Жыл бұрын
thank you!
@chillkro25 Жыл бұрын
Hii bro kis year me ho
@harshitpandey83047 ай бұрын
Working. Total ~5 YOE Joined Salesforce 4 months back. Was at Amazon before that
@coding8000 Жыл бұрын
When Striver gave his google interview, 😂"The Interviewer might be thinking, am I a "INTERVIWER" or a "STUDENT" of him. 😂"
@vijaykumarbille77279 ай бұрын
😂
@Ayush372626 ай бұрын
Bery Bery Funny Bro, I can't control my laugh 🤣🤣🤣🤣🤣🤣 Bruhhhhhh 🤡🤡🤡
@suryanshrastogi13402 ай бұрын
@@Ayush37262 youtube comments trying to be funny turns out to be actually very lame
@AyushVerma-ui7re2 ай бұрын
@@Ayush37262 😂
@kthamim75528 ай бұрын
i told about you when i attend my zoho interview in HR round and now i am developer at zoho 😇
@BipinDas-kj2op7 ай бұрын
Refer kardo😂
@rajkumarvb519710 ай бұрын
Your videos are one of a kind man. Thanks a lot for everything you provided to people like us.
@kopalpareek658 ай бұрын
I don't have words for this kinda EXPLAINATION !!🙇♀🙇♀🙇♀🙇♀🙇♀🙇♀🙇♀
@durgaprasad-gn4dk6 ай бұрын
Ofcourse, I can proudly say that I learnt all these DSA concepts from Striver in Take U Forward channel. Thank you
@techcraving6641 Жыл бұрын
we are greatful to have someone like you on youtube 🙏
@abhishekgautam15972 жыл бұрын
In Java, I think we can use TreeSet, since it inserts elements in a sorted order so the minimum will always be the first element of the TreeSet. We might need to write a custom comparator for the sorting order based on distances.
@mrudul5018 Жыл бұрын
I tried this but I faced a problem. By Using Treeset we will be able to run comparator only on one field of the pair that we are storing. Thus either we can store unique sorted vertices or unique sorted distances. But we want unique vertices with sorted distances.
@saad5527 Жыл бұрын
@@mrudul5018 The below code is working fine, you just need to implement compareTo method of Comparable interface static int[] dijkstra(int V, ArrayList adj, int S){ TreeSet set=new TreeSet(); int[] dist=new int[V]; for(int i=0; i0){ Pair p=set.pollFirst(); int currNode=p.node; int currDist=p.dist; for(int i=0; i
@optCode7 ай бұрын
@@mrudul5018 Can't we use a custom comparator like this ? SortedSet set = new TreeSet((Pair a, Pair b) -> { // Compare based on the first element of the Pair if (a.first == b.first) { // If the first elements are equal, compare based on the second element return Integer.compare(a.second, b.second); } // If the first elements are different, directly compare them return Integer.compare(a.first, b.first); });
@gaurishaaaa9 ай бұрын
Grateful to you and ya i'd definately tell the interviewer that i learnt it from you. 😂 Youre so great at what you do. HUGE respect striver.
@babulalyadav43054 ай бұрын
00:04 Dijkstra's algorithm is implemented using the set data structure. 01:45 Explaining Dijkstra's Algorithm through a dry run on a set data structure 03:21 Using Dijkstra's Algorithm to find the shortest path 04:39 Updating the minimum distance values 06:15 Using a set to erase unnecessary paths in Dijkstra's Algorithm 07:49 Using set data structure can provide better complexity for certain operations 09:18 Implement Dijkstra's algorithm using a set 10:57 Implementing Dijkstra's Algorithm using a set .
@shivanshkhare2724 Жыл бұрын
if you just use visited array along with priority queue, then you can skip the extra iterations , and that is more closer to what djikstra algo is than using sets unneccesarily
@no_name45689 Жыл бұрын
Understood with good depth, like always. Thanks Striver sir.
@Lovekush-kx5kz Жыл бұрын
Ofcourse striver i am going to tell the interviewer If we have time during interview cuz you are the only one cuz of which I can gain intrest in coding otherwise I am zero in it 🥺💗
@amanasrani5075 ай бұрын
Striver, without you DSA would be so toughhhhh, thank You 🙏
@studyonline3236 Жыл бұрын
In python, the set is unordered, unlike in C++ where it is sorted. To get the max use from PQ in python just use a visited list for the vertices. Mark the src node visited and you only push the adj nodes, that are unvisited, into the PQ. (OFC with extra space complexity due to the implementation of set in python)
@shivanshuagrawal95325 ай бұрын
ANYONE HAVE IDEA ABOUT JAVA
@dharanyuvi69514 ай бұрын
Thanks bro
@shashwatkumar69656 ай бұрын
We can even use unordered_set/hashset to get O(1) amortized time. Because we don't need to keep the pairs sorted on distance, because if we get any distance less than already existing distance for a node, we can erase it from our unordered set. Though we have to specifically write a hash function to store pairs in unordered set. struct pair_hash { inline std::size_t operator()(const std::pair & v) const { return v.first*37+v.second; } }; unordered_set ust;
@abhijitkumarsinha5 ай бұрын
i didn't know that we can use pairs in unordered_set , i have to learn this.Thanks
@chetanraghavvАй бұрын
@@abhijitkumarsinha yup as far as I remember, anything that can be compared can be type of set as it is internally implemented as red-black tree but in case of unordered set, hash function is used which is by default only defined for primitive types like int, long, float and some non-primitive like string
@subhranilnandy042 ай бұрын
Understood :) We can apparently use the TreeSet as a sorted set in Java... Below is the code: class Solution { static class Pair { int dist; int node; Pair(int _dist, int _node) { dist = _dist; node = _node; } } //Function to find the shortest distance of all the vertices static int[] dijkstra(int V, ArrayList adj, int S) { // Write your code here TreeSet set = new TreeSet( (p1,p2)-> p1.dist == p2.dist ? p1.node-p2.node : p1.dist-p2.dist ); int dist[] = new int[V]; Arrays.fill(dist, Integer.MAX_VALUE); dist[S] = 0; set.add(new Pair(0,S)); while(!set.isEmpty()) { Pair curr = set.pollFirst(); for(ArrayList arr : adj.get(curr.node)) { // (node, wt) int nei = arr.get(0); int wt = arr.get(1); int newDist = curr.dist + wt; if(newDist < dist[nei]) { set.add(new Pair(newDist, nei)); if(dist[nei] < Integer.MAX_VALUE) set.remove(new Pair(dist[nei], nei)); // For removal dist[nei] = newDist; } } } return dist; } }
@sidhantkumar9281 Жыл бұрын
amazed by your energy ...
@Saurabh-lb1uv Жыл бұрын
God level explaination! Thanks
@aryashjain78932 жыл бұрын
:) awesome obviously gonna tell the interviewer ,my teacher is RAJ NaaM to suna hi hoga
@cinime2 жыл бұрын
Understood! Super amazing explanation as always, thank you very much!!
@ayushjindal49812 ай бұрын
instead of removing the entry fro the set, just ignore the entry when you encounter it. This would save both removal time and extra iterations time.
@sajal..saraf...2455 Жыл бұрын
Understood all lectures till this one Thanks sir
@sriharsha586711 ай бұрын
The below Java code with TreeSet implementation works for Dijskstra's Algorithm static int[] dijkstra(int V, ArrayList adj, int S) { // Write your code here TreeSet set = new TreeSet((node1, node2) -> { if (node1.value != node2.value && node1.dist == node2.dist){ return 1; } return node1.dist - node2.dist; }); set.add(new Node(S, 0)); int[] dist = new int[V]; for (int i = 0; i < V; i++) { dist[i] = (int)1e9; } dist[S] = 0; while (!set.isEmpty()) { Node node = set.pollFirst(); for (ArrayList adjNodes : adj.get(node.value)) { int adjNode = adjNodes.get(0); int adjNodeDist = adjNodes.get(1); if (dist[node.value] + adjNodeDist < dist[adjNode]) { set.add(new Node(adjNode, dist[node.value] + adjNodeDist)); dist[adjNode] = dist[node.value] + adjNodeDist; } } } return dist; }
@AyushMishra-b8w11 ай бұрын
use visited array in priority queue implementation and whenever you reach a node that is already visited skip that it will work similar to set vector dijkstra(vector adj[],int v,int src){ vector vis(v,false); vector dist(v,INT_MAX); //{x1,x2}=> x1-weight ,x2-edge priority_queue pq; pq.push({0,src}); dist[src]=0; while(!pq.empty()){ int u=pq.top().second; pq.pop(); if(vis[u] == true) continue; vis[u]=true; for(auto x:adj[u]) if(vis[x.first] == false && dist[x.first]>dist[u]+x.second){ dist[x.first]=dist[u]+x.second; pq.push({dist[x.first],x.first}); } }
@sanyattaneja8551 Жыл бұрын
Rather than erasing from the set can't we just change the distance stored in the set then least distance one would be stored and we don't have to call erase and there would be no iterations for the same bigger distance nodes Happy to be corrected
@TheRandomPerson494 ай бұрын
we can't directly change values as we do in array. We have to traverse it. so erasing is the right option
@simplymaths54872 жыл бұрын
Sera bojhao dada 😎
@parul8334 Жыл бұрын
I love u so much can't explain in words, thanks for providing so much good content
@tiktikgoes99 ай бұрын
Understood, Thank you striver!!
@smile85102 жыл бұрын
Understood! awesome explanation Got the concept in one go
@travellersseek59682 жыл бұрын
Hey Striver! If possible, please do include some more videos in DP playlist.
@jivanmainali17423 ай бұрын
could not understand like even in pq you can delete that node which is already there when we found better option , so how come set makes thing different ?
@KOTAGIRICHAITHANYAKOTAGIRICHAI2 ай бұрын
same doubt
@ShubhamA_3 ай бұрын
Understood . Thanks for the explaination
@kshithi28642 жыл бұрын
Hello Striver!Thank you yor crystal clear explanation .UNDERSTOOD
@pratimkundu48292 жыл бұрын
Hey! just had one doubt will the T.C of the priority queue method be O(ElogE) instead of O(ElogV) as there can be more than one instance of a node in the priority queue whereas in the treeset method it will be O(ElogV) ?
@takeUforward2 жыл бұрын
Yes
@rijumondal68768 ай бұрын
Excellent job
@yashsomani3042 Жыл бұрын
Understood Very well explained , Thank you so much!
@jyothiyadav259510 ай бұрын
Understoood
@prasenjitsutradhar33685 ай бұрын
This is code for Java guys: In java, we have to implement a custom Pair class that implements comparable interface along with our own implementation of 1. "compareTo" method for comparing two pairs 2. "equals" method for equality of pairs you can implement "hasCode" & "toString", but not needed! // Here is the code: class Pair implements Comparable { int dist; int node; Pair(int dist, int node) { this.dist = dist; this.node = node; } @Override public int compareTo(Pair other) { if(this.dist != other.dist) { return Integer.compare(this.dist, other.dist); } else { return Integer.compare(this.node, other.node); } } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Pair pair = (Pair) o; return this.dist == pair.dist && this.node == pair.node; } } class Solution { //Function to find the shortest distance of all the vertices //from the source vertex S. static int[] dijkstra(int V, ArrayList adj, int S) { TreeSet st = new TreeSet(); int[] dist = new int[V]; for(int i=0; i 0) { Pair curr = st.pollFirst(); int currNode = curr.node; int currDist = curr.dist; for(List adjacent : adj.get(currNode)) { int adjNode = adjacent.get(0); int adjDist = adjacent.get(1); if(dist[currNode] + adjDist < dist[adjNode]) { // erase if it exists if(dist[adjNode] != 1e9) st.remove(new Pair(dist[adjNode], adjNode)); dist[adjNode] = dist[currNode] + adjDist; st.add(new Pair(dist[adjNode], adjNode)); } } } return dist; } }
@Sp_Global-oc4tf4 ай бұрын
better than Bhaiya did's course.
@CodeMode9313 Жыл бұрын
quite good explanation
@hemanthkumar516 Жыл бұрын
for java use this data structure as alternative to pair in c++ TreeSet ts=new TreeSet(new Comparator(){ @Override public int compare(Map.Entry a,Map.Entry b){ return a.getKey()-b.getKey(); } });
@sayakbasak5876 ай бұрын
can you please tell why set cant be used in java
@rishitmehrotra32013 ай бұрын
hats off striver
@lyricallava60147 ай бұрын
In Java, we can remove from the priority queue only. See my implementation below. class Pair{ int d; // distance int n; // node Pair(int d,int n){ this.d=d; this.n=n; } } class Solution { //Function to find the shortest distance of all the vertices //from the source vertex S. static int[] dijkstra(int v, ArrayList adj, int s) { // Write your code here PriorityQueue pq = new PriorityQueue((a,b)->a.d - b.d); int distance[] = new int[v]; for(int i=0;i
@tasneemayham9747 ай бұрын
Dang it! So, Java really can't implement the set huh?? Is it because there is no get method? I tried implementing it, and that's where I got stuck. No way to get the pair out.
@KOTAGIRICHAITHANYAKOTAGIRICHAI2 ай бұрын
in java treesets why are we unable to remove the duplicate pair? is there any method to remove ? or it doesnt work in treesets
@chiragyadav5467 Жыл бұрын
great explaination bhaiya :))))))))))
@aditya_raj78278 ай бұрын
understood bhaiya❤❤
@parvathivanapilli8572 Жыл бұрын
Understood great explanation ❤
@vakhariyajay2224 Жыл бұрын
Thank you very much. You are a genius.
@KUMARSAURABH-s5i5 ай бұрын
godly explaination!!
@UECAshutoshKumar Жыл бұрын
Thank you sir 😁
@somsk563 ай бұрын
You teach us well striver and You want from me to tell the interviewer "I have learned all this from striver" If , Interviewer hates you then ? you know nah... some people hates some people with out any reason thats like " ise toh dekhke hi gussa hoga he"... Just kidding I will dfnly tell the interviewer :)
@rishabhagarwal80492 жыл бұрын
Understood Sir, Thank you very much
@parthh396321 күн бұрын
i watched this whole video then realised i code in java :)
@AkashRaj-vj6br Жыл бұрын
great explanation
@DevashishJose9 ай бұрын
Understood, Thank you so much.
@Saurabh-fe2bg Жыл бұрын
why am i forgetting the previously visited videos 😪
@gautamsaxena46476 ай бұрын
understood bhaiya
@harshsoni481211 ай бұрын
nice beard striver ,thanks for video i understood the assignment
@Learnprogramming-q7f6 ай бұрын
Thank you forward
@sudhadevi6692 Жыл бұрын
Thx bhaiya for all this❤
@vishwajeet_singh__ Жыл бұрын
You are amzing
@googleit2490 Жыл бұрын
Understood :) Sep'2, 2023 11:14 am
@b_01_aditidonode43 Жыл бұрын
understood sir, thanks a lot
@Aniketyadav-b6s9 ай бұрын
In priority queue, we can use visited array to remove the multiple occurrence like {10,5} after executing {8,5}. I do not understand why need of set, hope get to know in future.
@Manmohanshukla4206 ай бұрын
same question, did you figured it out?
@datastructuresandalgorithm93702 ай бұрын
I think,tehre is no such diffenrce.There are mutiple ways to choose Data structures.So,we can use both set and Priority queue.
@-VLaharika Жыл бұрын
Understood 👍
@The_Shubham_Soni Жыл бұрын
UNDERSTOOD.
@kozukioden1177Ай бұрын
How about use the PQ and then whenever you pop out the top element just do a simple check. Is the distance for this node in the array better than what I got in the PQ? if yes you can simply skip exploring this path. This takes care of redundantly exploring sub optimal paths and gives us the benefit of faster retrievals of a min heap : )
@bhavyabansal7145 Жыл бұрын
Why we are not able to erase from PQ?? Amazing Explanation btw. Understood..
@pratikshadhole6694 Жыл бұрын
understood..always
@sanchitasingh42486 ай бұрын
Understood!! yayyyy!!
@mugambo55052 жыл бұрын
nice explanation brother 😍
@anushkachauhan110 Жыл бұрын
Understood!
@priyanshvatsal9791 Жыл бұрын
Understood 😇
@shambhavikhare9850 Жыл бұрын
amazing video
@sysfailureexe60386 ай бұрын
Understood !!
@suryakiran2970 Жыл бұрын
Understood❤
@sarthakkabra81562 жыл бұрын
gazab explanation
@gnes048 ай бұрын
Striver your content is very helpful but one suggestion... Please make the videos shorter and more concise. I feel most of us can understand still, and we won't get bored. Plus it's easier to watch to revise if the vids are shorter.
@abhishekanand94306 ай бұрын
thanks
@pratip4754 ай бұрын
I think we can use TreeMap here in java
@Manmohanshukla4206 ай бұрын
In the priority queue implementation, we will first receive (8,5) from the top and process the neighbors of Node 5. The distance of Node 5 would have also updated to 8 while making that entry in the priority queue. So later when we receive (10,5) we can simply check if the already updated distance of Node 5 (which will be 8) is lesser than the new distance (10). If so, we simply continue the loop and not process Node 5. OR We can also use a vector to keep track of visited nodes, and if the node is already visited (via (8,5)) we ignore the current entry (10, 5). Any suggestions whether that will be a better approach than deleting elements in the set?
@akuma_1682 ай бұрын
I guess yeah we could just simply avoid processing (10,5) if we have already stored a smaller dist.
@LBK3 Жыл бұрын
understood ❤
@parshchoradia9909 Жыл бұрын
UnderStood Sir!
@akashsahu2571 Жыл бұрын
yes
@KuchBhi-bs7sc Жыл бұрын
better than paid ones
@TrendyGamer0072 жыл бұрын
11:55 if in future ...when I appear for interview ... and my interviewer asks me this ... I will proudly say him ... that i learned this from @striver @takeUforward
@keertilata202 жыл бұрын
thank you so much
@mriduljain6809 Жыл бұрын
Thanks😀😀
@uknowme69420 Жыл бұрын
pls add this video in graph series, also there are many videos not added in the playlist
@akashsingh9301 Жыл бұрын
thnku
@rayyansyed2998 Жыл бұрын
"understood"
@Deepanshupal-fx9db6 ай бұрын
Nice way of explaining complex thing .But I have a doubt ,why to erase in set ,won't it be automatically done while using set ,we can just update ,it will update at that existing node only,I might be wrong. please confirm
@princenagar16864 ай бұрын
This was not for Java people but yeah " Understood "
@maneetgupta67492 жыл бұрын
Understood
@abinashbiswal3511 Жыл бұрын
can't we just use a visited array with the PQ to achieve the same functionality...?
@abhisheknag19298 ай бұрын
yes please anyone answer
@arnabbanik64036 ай бұрын
if we keep a boolean visited array to check if we have computed other distances from a particular node or not then can we not reduce the no. of iterations that you were talking about and also not have to use a log(n) time complexity for removing anything?