G-35. Print Shortest Path - Dijkstra's Algorithm

แชร์
ฝัง
  • เผยแพร่เมื่อ 29 พ.ย. 2024

ความคิดเห็น • 226

  • @takeUforward
    @takeUforward  2 ปีที่แล้ว +40

    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

  • @muskangupta5873
    @muskangupta5873 6 หลายเดือนก่อน +46

    This question is updated on GFG , if you look at strivers screen its just asking path but in updated question you need to append the dist[n] also and then reverse

    • @ABHIMANYUTHAPLIYAL
      @ABHIMANYUTHAPLIYAL 5 หลายเดือนก่อน

      yeah, i was confused where's the error in my code , then did this and passed all tetscases. Thanks!!

    • @subhambachar
      @subhambachar 4 หลายเดือนก่อน

      Thanks for sharing 👍

    • @prakharm614
      @prakharm614 2 หลายเดือนก่อน +2

      just do -
      reverse(path.begin(), path.end());
      // Insert the total distance as the first element of the result
      path.insert(path.begin(), dist[n]);
      return path;

    • @MohdFaizan-sw8ic
      @MohdFaizan-sw8ic 2 หลายเดือนก่อน

      @@prakharm614 thanks buddy if u dont mind can you give me ur discord...
      💫

    • @jayantgurusrivastava8256
      @jayantgurusrivastava8256 หลายเดือนก่อน

      Thanks a lot was confused about this!!!

  • @Roshansingh-fe6oh
    @Roshansingh-fe6oh ปีที่แล้ว +7

    we can print the path with the help of distance array only (no need of parent array)
    if(dis[n]==1e9){
    return {-1};
    }
    vectorans;
    int curr_node=n;
    while(curr_node!=1){
    ans.push_back(curr_node);
    for(auto it:al[curr_node]){
    if(dis[it.first]==dis[curr_node]-it.second){
    curr_node=it.first;
    }
    }
    }
    ans.push_back(1);
    reverse(ans.begin(),ans.end());
    return ans;

  • @harleenkaur7751
    @harleenkaur7751 ปีที่แล้ว +21

    Thanks a lot, striver. Ur content is far better than paid courses. U literally explained us everything in such a depth. Thanks again for all your amazing playlists.

  • @princenagar1686
    @princenagar1686 4 หลายเดือนก่อน +4

    Solved this code without watching any approach in this video just when he said try it out first, i go and solved it. Thanks Striver.

  • @Anony.musk01
    @Anony.musk01 ปีที่แล้ว +13

    I just love the confidence and the clarity with which you code the solution. I have started to recite the solution similar to your style while coding alone ^^

  • @mayank_rampuriya
    @mayank_rampuriya ปีที่แล้ว +9

    My approach similar to WordLadder-2, in set along with dist store currentList too...
    vector shortestPath(int n, int m, vector& edges) {
    // Code here
    vector adj[n+1];
    for(int i=0; i

  • @malvado8267
    @malvado8267 8 หลายเดือนก่อน +8

    NOTE
    Now the question is updated ,we also have to pushback the weight of shortest path.
    typedef pair pip;
    vector shortestPath(int n, int m, vector& edges) {
    vector< pair >adj[n+1];
    for(auto &it : edges)
    {
    adj[it[0]].push_back({it[1],it[2]});
    adj[it[1]].push_back({it[0],it[2]});
    }
    priority_queue< pip, vector , greater< pip > >pq;
    pq.push({0,1});
    vectordist(n+1,1e9);
    dist[1] = 0;
    vectorparent(n+1);
    for(int i = 1;i 0)
    {
    auto it = pq.top();
    int node = it.second;
    int dis = it.first;
    pq.pop();
    for(auto &it : adj[node])
    {
    int adjnode = it.first;
    int edgeweight = it.second;
    if(dis + edgeweight < dist[adjnode])
    {
    dist[adjnode] = dis + edgeweight;
    pq.push({dis+edgeweight , adjnode});
    parent[adjnode] = node;
    }
    }
    }
    if(dist[n] == 1e9) return {-1};
    vectorpath;
    int node = n;
    while(parent[node] != node)
    {
    path.push_back(node);
    node = parent[node];
    }
    path.push_back(1);
    path.push_back(dist[n]);
    reverse(path.begin(),path.end());
    return path;
    }

    • @loveyoubts2002
      @loveyoubts2002 7 หลายเดือนก่อน

      it's showing tle error in the last test case

    • @bhaktisharma9681
      @bhaktisharma9681 7 หลายเดือนก่อน

      Thank you so much!!! Was struggling to find my mistake!

    • @loveyoubts2002
      @loveyoubts2002 7 หลายเดือนก่อน

      @@bhaktisharma9681 did u find the solution ?? Then pls share it here 🙏

    • @vaisakh5802
      @vaisakh5802 6 หลายเดือนก่อน +1

      Can you explain the code for graph creation? I'm not able to understand it

    • @TON-108
      @TON-108 2 หลายเดือนก่อน

      @@vaisakh5802 Watch initial videos of the series and understand how adjacency list maintains its adjacent nodes and weight

  • @uncoding-rohit
    @uncoding-rohit ปีที่แล้ว +1

    Hi sir, I solved this question by own and this is happened because of you. Before your videos graph is like impossible thing for me but now I can solve medium level questions easily

  • @tatipamularaviteja5245
    @tatipamularaviteja5245 ปีที่แล้ว +15

    after line 20 we should push pair {0,1} initially to priority queue

    • @aastikofficial6100
      @aastikofficial6100 7 หลายเดือนก่อน

      you can but here we push {0,1} as we store in pq and we store in adj list hope you get this

  • @secretvibz6300
    @secretvibz6300 6 หลายเดือนก่อน +3

    Using that parent array was indeed a smart move 😀 !!

  • @tusharbhart7018
    @tusharbhart7018 2 ปีที่แล้ว +14

    Another short approach using Dijkstra's Algorithm (in this we will store the path in priority queue itself just like word ladder II)
    vector shortestPath(int n, int m, vector& edges) {
    vector adj[n + 1];
    for(auto e : edges) adj[e[0]].push_back({e[1], e[2]}), adj[e[1]].push_back({e[0], e[2]});
    vector d(n + 1, INT_MAX);
    d[1] = 0;
    priority_queue pq;
    pq.push({0, {1}});
    int minD = INT_MAX;
    vector ans = {-1};
    while(pq.size()) {
    vector path = pq.top().second;
    int dis = pq.top().first; pq.pop();
    int node = path.back();
    if(node == n && minD > dis) minD = dis, ans = path;
    for(auto ad : adj[node]) {
    if(d[node] + ad.second < d[ad.first]) {
    d[ad.first] = d[node] + ad.second;
    path.push_back(ad.first);
    pq.push({d[ad.first], path});
    path.pop_back();
    }
    }
    }
    return ans;
    }

  • @wervana
    @wervana 2 หลายเดือนก่อน

    Solved this without watching the video. Took 2 hours but was worth it.

  • @nagame859
    @nagame859 ปีที่แล้ว +1

    simple solution again.. thanks Striver. This Graph series feels easier than any other..❤

  • @blitzkrieg7453
    @blitzkrieg7453 ปีที่แล้ว +12

    GFG Apparantly have updated the testcases and this solution no longer is getting accepted

    • @at9443
      @at9443 5 หลายเดือนก่อน

      you just have to append the shortest path distance at the beginning of the answer array

  • @aakashbansal6627
    @aakashbansal6627 ปีที่แล้ว +2

    TreeSet solution in Java (Just need to override compareTo, equals and hashCode methods of the SortedSet interface):
    class Pair implements Comparable{
    int node, dist;
    public Pair(int dist, int node){
    this.node = node;
    this.dist = dist;
    }
    @Override
    public boolean equals(Object e){
    Pair other = (Pair) e;
    return node == other.node;
    }
    @Override
    public int hashCode(){
    return node;
    }
    @Override
    public int compareTo(Pair p){
    if(p.dist == this.dist)
    return this.node - p.node;
    return this.dist - p.dist;
    }
    }
    class Solution
    {
    static int[] dijkstra(int V, ArrayList graph, int S)
    {
    //dist, node -> sort by smaller distance from src. If dist same, then sort by smaller node
    TreeSet set = new TreeSet();
    set.add(new Pair(0, S));
    int[] dist = new int[V];
    Arrays.fill(dist, Integer.MAX_VALUE);
    dist[S] = 0;
    while(!set.isEmpty()){
    Pair cur = set.pollFirst();
    int src = cur.node, distToSrc = cur.dist;
    for(ArrayList edge : graph.get(src)){
    int dest = edge.get(0), curDist = edge.get(1);
    if(dist[dest] > distToSrc + curDist){
    dist[dest] = distToSrc + curDist;
    set.add(new Pair(dist[dest], dest));
    }
    }
    }
    return dist;
    }
    }

  • @cinime
    @cinime 2 ปีที่แล้ว +6

    Understood! Such a wonderful explanation as always, thank you very much!!

  • @manojraj1332
    @manojraj1332 6 หลายเดือนก่อน

    Question is updated in GFG, it is undirected so we need to add u->v and v->u both in the adjacency List.
    Also Need to add the dist[n] to the path and need to reverse it as the question is asking the dist and path of src to N node.

  • @anshulsharma3137
    @anshulsharma3137 2 ปีที่แล้ว +23

    Hi Striver, With your hint I was able figure out that this algo is same as we used to print LIS/LCS in DP. Can't think of anyone teaching DSA better than you. Well its not wrong to say that you are the Netflix of DSA xD

    • @krishanpratap3286
      @krishanpratap3286 2 ปีที่แล้ว +2

      same same
      which year?

    • @chethanrao8827
      @chethanrao8827 2 ปีที่แล้ว +2

      And I solved it seeing ur hint (print LIS problem in dp)

    • @anshulsharma3137
      @anshulsharma3137 ปีที่แล้ว

      @@krishanpratap3286 2020 passed out lol

    • @shubhamraj25
      @shubhamraj25 ปีที่แล้ว +1

      So are you looking to switch?

  • @techbucks7339
    @techbucks7339 ปีที่แล้ว

    We can also use pq of dist and the path we have so far to solve it without needing the parent array.
    vector shortestPath(int N, int m, vector& edges) {
    priority_queue pq ;
    vector dist(N+1,INT_MAX) ;
    vectoradj[N+1] ;
    for (int i =0 ; i< m ;i ++) {
    int u = edges[i][0] ;
    int v = edges[i][1];
    int wt =edges[i][2] ;
    adj[u].push_back({v,wt}) ;
    adj[v].push_back({u,wt}) ;
    }
    pq.push({0,{1}}) ;
    //bool flag = false ;
    while(!pq.empty()) {
    auto x = pq.top() ;
    pq.pop() ;
    vector path = x.second ;
    int node = path.back();
    int dis = x.first ;
    if (node==N) {
    // flag = true ;
    return path ;
    }
    for (auto x : adj[node]) {
    int adjNode = x.first ;
    int adjDis = x.second ;
    if (dist[adjNode]>adjDis+dis) {
    dist[adjNode] = adjDis + dis ;
    path.push_back(adjNode);
    pq.push({adjDis + dis, path});
    path.pop_back();
    }
    }
    }
    return {-1} ;
    }

  • @sharathkumar8338
    @sharathkumar8338 2 ปีที่แล้ว +45

    one major doubt. How can we come up with these kind of solutions on our own???

    • @takeUforward
      @takeUforward  2 ปีที่แล้ว +68

      Practice, and practice...

    • @SatyamEdits
      @SatyamEdits 2 ปีที่แล้ว +12

      because if you just keep practicing with closed eyes it would take an eternity to become a good problem solver.....

    • @sharathkumar8338
      @sharathkumar8338 2 ปีที่แล้ว

      @@SatyamEdits Thank you

    • @sharathkumar8338
      @sharathkumar8338 2 ปีที่แล้ว

      @@takeUforward Thank you

    • @VishnuKumar-cz8tp
      @VishnuKumar-cz8tp ปีที่แล้ว +3

      Aa jata hai bro if you practice many questions

  • @AJ-xc3ks
    @AJ-xc3ks ปีที่แล้ว

    Just love ur way of solution Problem ❤️❤️

    • @TON-108
      @TON-108 2 หลายเดือนก่อน

      *Problem Solution

  • @stith_pragya
    @stith_pragya 11 หลายเดือนก่อน +1

    Thank You So Much for this wonderful video...........🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻

  • @ng390
    @ng390 6 หลายเดือนก่อน

    Please use a proper compiler which shows output also so it will be more understandable.

  • @harshitsen5479
    @harshitsen5479 9 หลายเดือนก่อน

    We basically hashing the node for each updatation in the distances[adjNode]. I guess we can use HashMap too in this case.

  • @dilsedhoni9229
    @dilsedhoni9229 ปีที่แล้ว +1

    Was able to come up with the solution without watching the video. Here's my implementation :
    vector shortestPath(int n, int m, vector& edges) {
    // Code here
    vectoradj[n+1];
    for(int i = 0 ; i < edges.size() ; i++){
    adj[edges[i][0]].push_back({edges[i][1],edges[i][2]});
    adj[edges[i][1]].push_back({edges[i][0],edges[i][2]});
    }
    priority_queue pq;
    pq.push({0,{1,{1}}});
    vectordist(n+1,INT_MAX);
    dist[1]=0;
    while(!pq.empty()){
    pair ki=pq.top();
    pq.pop();
    int dis=ki.first;
    int k=ki.second.first;
    vectorvec=ki.second.second;
    vectorvy=vec;
    if(k==n) return vec;

    for(int i = 0 ; i < adj[k].size() ; i++){
    if(dis+adj[k][i][1]

    • @vikasbeniwal6825
      @vikasbeniwal6825 ปีที่แล้ว

      almost same type of solution but got tle on case 4

  • @apmotivationakashparmar722
    @apmotivationakashparmar722 18 วันที่ผ่านมา

    Thank you So much Striver !

  • @sdexstarlord
    @sdexstarlord 2 ปีที่แล้ว +2

    Your Code's output is:
    1 46 11 51
    It's Correct output is:
    1 46 11 51
    Output Difference
    1 46 11 51

    • @Cityscapes411
      @Cityscapes411 9 หลายเดือนก่อน

      😂😂😂

  • @kumarsatyam8527
    @kumarsatyam8527 ปีที่แล้ว +4

    what if we need to find all the shortest paths from src to destination? (eg if there are 3 paths from src->dest with total wt=5,how to get all three)

  • @skt7088
    @skt7088 2 ปีที่แล้ว +6

    What if there are multiple shortest path and we have to print all of them? In that case what should we do?

  • @nilimsankarbora5911
    @nilimsankarbora5911 11 หลายเดือนก่อน

    Masterpiece Explanation

  • @hxveelbadtv
    @hxveelbadtv ปีที่แล้ว

    Litterly saved me, deserves a subscription if you ask me!!!

  • @manasranjanmahapatra3729
    @manasranjanmahapatra3729 2 ปีที่แล้ว

    Understood! amazing Explanation.

  • @stranger1967
    @stranger1967 ปีที่แล้ว +1

    how to handle multiple shorest path . This question was asked in my college intership OA of atlassian. PLss tell how to handle ?

  • @abdulnisar7716
    @abdulnisar7716 2 หลายเดือนก่อน

    My Attempt ->
    vector shortestPath(int n, int m, vector& edges) {
    vector adjList(n+1);
    vector distances(n+1,{INT_MAX,vector{}});
    distances[1] = {0,{1}};
    for(int i=0;i distances[node].first) continue;
    vector path = distances[node].second;
    for(auto pair : adjList[node]){
    int adjNode = pair.first;
    int d = pair.second;

    if(distances[adjNode].first > d + dist) {
    distances[adjNode].first = d + dist;
    vector newPath = path;
    newPath.push_back(adjNode);
    distances[adjNode].second = newPath;
    pq.push({d + dist, adjNode});
    }
    }
    }
    vector result;
    if(distances[n].first == INT_MAX) return {-1};
    else {
    result.push_back(distances[n].first);
    for(auto node : distances[n].second) result.push_back(node);
    return result;
    }
    }

  • @codewithme-ss6zl
    @codewithme-ss6zl 10 หลายเดือนก่อน +5

    I am unable to find this Question on GFG

    • @ooofrg4492
      @ooofrg4492 3 หลายเดือนก่อน

      Same

  • @vishnu9134
    @vishnu9134 9 หลายเดือนก่อน

    Huge effort 🤩

  • @amanasrani6405
    @amanasrani6405 5 หลายเดือนก่อน

    Understood Bhaiya

  • @kasaiop
    @kasaiop 2 ปีที่แล้ว +4

    Understood
    Suggestion:- ek sath itne sare video mat upload karo channel ke reach Kam hoti hai
    Ho sake to ek ek hour ke video schedule kardo
    And amazing approach striver Bhai 😀
    Edit:- mai galat tha
    #keep_striving

    • @takeUforward
      @takeUforward  2 ปีที่แล้ว +23

      Areh pdhai ki channel ki reach hoti v nai h, word of mouth pe hi chalta h

    • @smile8510
      @smile8510 2 ปีที่แล้ว

      @@takeUforward true

    • @abdulsakibshekhayyub3498
      @abdulsakibshekhayyub3498 2 ปีที่แล้ว

      @@takeUforward still if u upload 1 or 2 video daily that will make us consistent

  • @vakhariyajay2224
    @vakhariyajay2224 ปีที่แล้ว

    Thank you very much. You are a genius.

  • @kaichang8186
    @kaichang8186 2 หลายเดือนก่อน

    understood, thanks for the great video

  • @_hulk748
    @_hulk748 ปีที่แล้ว

    Understood sir thankyou❤✨🙏🙇‍♂

  • @sahelidebnath5085
    @sahelidebnath5085 ปีที่แล้ว

    As the note not yet updated.
    Just for resource : java code
    public static List shortestPath(int n, int m, int edges[][]) {
    ArrayList adj = new ArrayList();
    for(int i = 0;i

  • @many987
    @many987 ปีที่แล้ว +1

    hey Striver bhaiya, i have a doubt that instead of storing the parents, can't we do this.
    if anyone else can help , then please.
    int node = n;
    while(node != 1){
    for(auto it : adj[node]){
    int adj_node = it.first;
    int adj_wt = it.second;
    if(dist[node] == dist[adj_node] + adj_wt){
    ans.push_back(adj_node);
    node = adj_node;
    }
    }
    }
    intution is simple that whose adjNode's distance + weight gives the node's distance will be the parent node.

  • @UECAshutoshKumar
    @UECAshutoshKumar ปีที่แล้ว +1

    Thank you sir 🙏

  • @arpitkhanulia7
    @arpitkhanulia7 ปีที่แล้ว

    we dont have to initialize the whole parent array, just do parent [source] = source ;

  • @rishabhraj8233
    @rishabhraj8233 ปีที่แล้ว

    hey striver , I solved this problem using a different approach where my pq consists of distance and vector(containing path until now) runs just fine
    class Solution {
    public:
    vector shortestPath(int n, int m, vector& edges) {
    // Code here
    vectorans;
    vectoradj[n+1];
    for(int i=0;i

  • @The_Shubham_Soni
    @The_Shubham_Soni ปีที่แล้ว

    UNDERSTOOD.

  • @after_dark_777
    @after_dark_777 7 หลายเดือนก่อน +1

    TLE on the last test case?

  • @AravinthRavichandran-n1k
    @AravinthRavichandran-n1k ปีที่แล้ว

    if the weight of both edges is same, we need to handle that based on nodes in Priority Queue

  • @parshchoradia9909
    @parshchoradia9909 ปีที่แล้ว

    UnderStood Sir!

  • @shaddyrogue9430
    @shaddyrogue9430 2 ปีที่แล้ว +2

    Hey instead of using extra array of parent can't we store a Pair in the dist array itself and do the backtrack from there. This way we can save some space.

    • @kushaldas1703
      @kushaldas1703 2 ปีที่แล้ว

      #tuf and take forward for crowdwork...#leetcode more solution needed

    • @sypher4735
      @sypher4735 ปีที่แล้ว +1

      How can it have a better time complexity?, If observed closely the total number of integers will be equal, so I don't think it's better from any angle.

    • @HONOREDONE-hk7pt
      @HONOREDONE-hk7pt ปีที่แล้ว

      lol, pair of size n is nothing but two arrays of size n clubbed together.

  • @averylazyandweirdhuman
    @averylazyandweirdhuman 5 หลายเดือนก่อน

    Hi striver, can the datatype of distance array be a class which stores node distance and parent ? so that we will not take two separate arrays?

  • @priyanshusrivastava2145
    @priyanshusrivastava2145 ปีที่แล้ว +1

    its 3:54 am and busy creating my future😁😄

  • @viswanathvuppala4526
    @viswanathvuppala4526 ปีที่แล้ว

    Sir you forgot the case of returning list of -1s if path's not possible

  • @DevashishJose
    @DevashishJose 9 หลายเดือนก่อน

    understood, thank you so much.

  • @-VLaharika
    @-VLaharika ปีที่แล้ว

    Understood 👍

  • @saitejaballa6759
    @saitejaballa6759 ปีที่แล้ว +5

    What if there are two shortest path 1->2->4->5 and 1->3->5 with the same dist value , will it print 1->3->5?

    • @yaswanthkosuru
      @yaswanthkosuru ปีที่แล้ว

      No it's based on graph
      If you want smallest path (or) all paths then store parent=list of set of values
      Ex:parent=[0,{1, 2}] dist=[0,1]
      Again if we get dist 1 of index 1 from other path then simply add that node to parent [1] then apply dfs not while loop.

  • @luckygamers8068
    @luckygamers8068 2 ปีที่แล้ว

    Very much helpful 😍😍

  • @gautamsaxena4647
    @gautamsaxena4647 6 หลายเดือนก่อน

    understood bhaiya

  • @adarshkumarrao3478
    @adarshkumarrao3478 ปีที่แล้ว

    UNDERSTOOD

  • @priyanshvatsal9791
    @priyanshvatsal9791 ปีที่แล้ว

    Understood 😇

  • @suryakiran2970
    @suryakiran2970 ปีที่แล้ว

    Understood❤

  • @isheep9025
    @isheep9025 ปีที่แล้ว

    One doubt! Why we need to initialise parent[ ] with idx themselves? does it help in anyway? my all test cases were passed without doing that (I did on Java )
    because if the given graph is a NULL graph then each node is a parent of itself,it doesn't make a diiference here since we only want the path if initial vertex and final vertex is connected,but at the end of the program doing this yields us the correct parent array no matter weather path exists or not

    • @HONOREDONE-hk7pt
      @HONOREDONE-hk7pt ปีที่แล้ว

      doesn't matter what you store at the parent caue when you will get the path it will automatically get updated with the new parents.

  • @Learnprogramming-q7f
    @Learnprogramming-q7f 6 หลายเดือนก่อน

    Thank you bhaiya

  • @karthik-varma-1579
    @karthik-varma-1579 4 วันที่ผ่านมา

    Here's the Java Code:-
    class Solution {
    class pair{
    int v;
    int w;
    pair(int v,int w){
    this.v = v;
    this.w = w;
    }
    }
    class pqpair{
    int distance;
    int node;
    pqpair(int distance,int node){
    this.distance = distance;
    this.node = node;
    }
    }
    public List shortestPath(int n, int m, int edges[][]) {
    // Code Here.
    ArrayList adj = new ArrayList();
    for(int i=0;i

  • @azizazmir2159
    @azizazmir2159 ปีที่แล้ว

    Huge love from south❤

  • @Sumeet_100
    @Sumeet_100 ปีที่แล้ว

    just amazing !!

  • @RahulGuptaa29
    @RahulGuptaa29 ปีที่แล้ว

    One doubt! Why we need to initialise parent[ ] with idx themselves? does it help in anyway? my all test cases were passed without doing that (I did on Java )

    • @isheep9025
      @isheep9025 ปีที่แล้ว

      because if the given graph is a NULL graph then each node is a parent of itself,it doesn't make a diiference here since we only want the path if initial vertex and final vertex is connected,but at the end of the program doing this yields us the correct parent array no matter weather path exists or not

  • @lineshmalkam224
    @lineshmalkam224 ปีที่แล้ว

    understtooooddddd

  • @smile8510
    @smile8510 2 ปีที่แล้ว

    Understood!

  • @AnkurKumar-jx5my
    @AnkurKumar-jx5my ปีที่แล้ว

    Understood!!

  • @NANUVALAADITHYA
    @NANUVALAADITHYA ปีที่แล้ว

    adj[it[0]] is a pair, how can we do a push_back to a pair??

  • @nisargpatel1443
    @nisargpatel1443 5 หลายเดือนก่อน

    Always remember, where you are coming from!

  • @abhireshray8331
    @abhireshray8331 ปีที่แล้ว

    nice:) explanation sir

  • @botsbucketlist9515
    @botsbucketlist9515 2 ปีที่แล้ว +1

    understood

  • @subodhkasaudhaniitk796
    @subodhkasaudhaniitk796 ปีที่แล้ว

    Golden❤

  • @adityaverma4902
    @adityaverma4902 ปีที่แล้ว

    why cant we take max priority queue? its giving tle .

  • @p38_amankuldeep75
    @p38_amankuldeep75 2 ปีที่แล้ว

    understood💙💙💙

  • @raghavendrakalkutaki5175
    @raghavendrakalkutaki5175 8 หลายเดือนก่อน

    why we need to push 5 at the end to run this code on gfg
    vector shortestPath(int n, int m, vector& edges) {
    vector adj[n+1];
    for(auto it:edges){
    adj[it[0]].push_back({it[1],it[2]});
    adj[it[1]].push_back({it[0],it[2]});
    }

    priority_queue pq;
    vector dist(n+1,1e9);
    vector parent(n+1);
    for(int i=1;i

    • @tasneemayham974
      @tasneemayham974 7 หลายเดือนก่อน

      Because they asked for the shortest distance value as well. So, they want you to first give them the shortest distance as the first index then the graph path.

  • @akshayanagelli5962
    @akshayanagelli5962 5 หลายเดือนก่อน

    what about no path case (-1)?

  • @suheabkhan2546
    @suheabkhan2546 2 ปีที่แล้ว

    Understood

  • @rayyansyed2998
    @rayyansyed2998 ปีที่แล้ว

    “understood”

  • @noobplays3362
    @noobplays3362 29 วันที่ผ่านมา

    mujhe ye question striver's sheet me q nhi dikh raha :-\

  • @sharathkumar8338
    @sharathkumar8338 2 ปีที่แล้ว

    class Solution {
    static class Node {
    int node, distance;
    public Node(int node, int distance) {
    this.node = node;
    this.distance = distance;
    }
    }
    static class Pair implements Comparable{
    int distance, node;
    public Pair(int distance, int node) {
    this.distance = distance;
    this.node = node;
    }
    public int compareTo(Pair o) {
    return this.distance - o.distance;
    }
    }
    public static List shortestPath(int n, int m, int edges[][]) {
    // code here
    ArrayList adjList = new ArrayList();
    for(int i=0;i

  • @brajeshmohanty2558
    @brajeshmohanty2558 ปีที่แล้ว

    Where can we find the java code for this video

  • @saagerbabung5652
    @saagerbabung5652 ปีที่แล้ว

    am i missing something?
    since its undirected graph there definitely exists a path right?
    then y print (-1)

    • @gamerversez5372
      @gamerversez5372 ปีที่แล้ว

      What if the Node is Unreachable ?

    • @saagerbabung5652
      @saagerbabung5652 ปีที่แล้ว

      @@gamerversez5372 undirected graph means it's reachable every where.
      Also there is only one component

  • @mohdkhaleeq7468
    @mohdkhaleeq7468 2 ปีที่แล้ว

    i am not able to write code by myself what to do?

  • @abhinavsidharth1421
    @abhinavsidharth1421 10 หลายเดือนก่อน

    can someone share link of question , it seems to be broken

  • @sambhavagarwal5871
    @sambhavagarwal5871 2 ปีที่แล้ว +3

    understood
    Even if my result and expected result is same its not passing test case. Test case number - 63
    pin this comment so everyone can save their time
    Your Code's output is:
    1 46 11 51
    It's Correct output is:
    1 46 11 51
    Output Difference
    1 46 11 51

  • @TarunKumarSaraswat
    @TarunKumarSaraswat 2 หลายเดือนก่อน

    THank you

  • @lokeshroy3944
    @lokeshroy3944 ปีที่แล้ว

    amazing

  • @satyareddy8671
    @satyareddy8671 ปีที่แล้ว

    understood
    sir

  • @devabakare357
    @devabakare357 2 ปีที่แล้ว

    JAVA Solution
    class Pair implements Comparable{
    int src;
    int wt;
    public Pair(int src, int wt){
    this.src = src;
    this.wt = wt;
    }
    public int compareTo(Pair pair){
    return this.wt - pair.wt;
    }
    }
    class Node{
    int dest;
    int cost;
    public Node(int dest, int cost){
    this.dest = dest;
    this.cost = cost;
    }
    }
    class Solution {
    public static List shortestPath(int n, int m, int edges[][]) {
    int memo[] = new int[n+1];
    int distance[] = new int[n+1];
    PriorityQueue pq = new PriorityQueue();
    ArrayList graph = new ArrayList();
    for(int i = 0; i

  • @anubhavjasoria4335
    @anubhavjasoria4335 2 ปีที่แล้ว

    very easy approach
    vector shortestPath(int n, int m, vector& edges) {
    // CREATE ADJACENECY MATRIX
    vectoradj[n+1];
    for(auto i:edges){
    int a=i[0];
    int b=i[1];
    int c=i[2];
    adj[a].push_back({b,c});
    adj[b].push_back({a,c});
    }
    // display(adj,n);
    sets; //DISTANCE , NODE
    vectordis(n+1,INT_MAX);
    unordered_mappar;
    s.insert({0,1});
    dis[1]=0;
    while(!s.empty()){
    auto temp=*(s.begin());
    int a=temp.first; // DISTANCE
    int b=temp.second; // NODE
    s.erase({a,b});
    for(auto i:adj[b]){
    int x=i.first; // NODE
    int y=i.second; // WEIGHT
    int nd=a+y;
    if(nd

  • @sanchittripathi1537
    @sanchittripathi1537 ปีที่แล้ว

    look at this
    vector shortestPath(int n, int m, vector& edges) {
    // Code here
    vectoradj[n+1];
    for(auto it:edges)
    {
    adj[it[0]].push_back({it[1],it[2]});
    adj[it[1]].push_back({it[0],it[2]});
    }
    queueq;
    q.push(1);
    vectordis(n+1,1e9);
    dis[1]=0;
    vectorvec(n+1);
    vectortemp;
    vec[1]={1};
    while(q.size()!=0)
    {
    int node=q.front();
    vectort=vec[node];
    q.pop();
    for(auto i:adj[node])
    {
    if(dis[i.first]>dis[node]+i.second)
    {
    dis[i.first]=dis[node]+i.second;
    temp=t;
    temp.push_back(i.first);
    q.push(i.first);
    vec[i.first]=temp;
    }
    }
    }
    if(vec[n].size()==0)return {-1};
    else return vec[n];
    }

  • @tusarmundhra5560
    @tusarmundhra5560 ปีที่แล้ว

    awesome

  • @aniruddhadas1778
    @aniruddhadas1778 2 ปีที่แล้ว

    Please anyone tell me from where could I learn the use of priority queue of min heap in java

  • @karthik53r35
    @karthik53r35 ปีที่แล้ว

    For same code but using unordered map insted of vector for getting path is giving me tle on 7th testcase itself😮!! Can anyone tell me the reason

  • @loveyoubts2002
    @loveyoubts2002 7 หลายเดือนก่อน

    I've understood the algo and the code but it's showing TLE error in gfg for last 2 cases, both with priority_queue and set...anyone else having same problem as me ?

    • @tasneemayham974
      @tasneemayham974 7 หลายเดือนก่อน

      Yes, I solved it alone and got TLE using Java. But when I solved it using C++ it ran well!!