G-38. Cheapest Flights Within K Stops

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

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

  • @takeUforward
    @takeUforward  2 года назад +54

    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

    • @ThisisCuriousclue
      @ThisisCuriousclue 2 года назад +2

      hey bhaiya, i might be wrong but i think it has exponential tc and sc under few circumstances.
      "like for edge cases"
      and gfg might have weak test cases

    • @vaibhavpratapsingh9956
      @vaibhavpratapsingh9956 Год назад +3

      the complexity will be O(V+E*K) and not O(V+E) if the complexity of using a queue was O(E) then why would people use dijkstras which takes a complexity of O((V+E)log(V)).

    • @harshitagrahari9241
      @harshitagrahari9241 Год назад +1

      Path 0-3-1 with cost 4 and steps 2 can also be pushed in the queue....then how this solution is handling this?? pls help

    • @tooovisibletosee
      @tooovisibletosee Год назад

      @takeUforward hey!! What happens if we djikstras is followed prioritizing stops over cost?

    • @pranshu_awasthi
      @pranshu_awasthi 10 месяцев назад

      I did the Dijsktra's same as you showed in the first step.
      Just tweaked a little, I reversed the edges and then started the Dijsktra's from Destination towards source so that in case there is a path with less cost but more stops towards destination, that will be rejected midway and the path taking more cost but less stops will be considered.
      Here is the code -
      class Solution {
      public:
      int findCheapestPrice(int n, vector& flights, int src, int dst, int k) {
      unordered_map graph;
      for(int i=0;i k) continue;
      vector connectedNodes = graph[node];
      for(int i=0;i newCost) {
      cost[newNode] = newCost;
      pq.push({newCost, newStops, newNode});
      }
      }
      }

      return -1;
      }
      };

  • @saumyaranjan9256
    @saumyaranjan9256 Год назад +41

    This is the best question if you want to understand the inner-workings of both bfs and djikstra and the difference between them, however I feel that this question needed an in depth explanation on why we are doing the specific things that we are doing in both the queue case and the priority queue case.

    • @sid1993ful
      @sid1993ful 18 дней назад

      So as per my understanding the only difference between this algo and basic dijkstra's is that instead of distance we give priority to nodes in the queue, correct? In other words, difference in just the way we store the tuple in queue. Or am I missing something?

  • @anonymouse3488
    @anonymouse3488 2 года назад +212

    @Striver at 15:20, we will consider (as per dry run and algorithm) taking the path from 0->3->1 , as the dist would be less from prev stored dist[ 1 ] and also push it in the priority queue.
    I think it has to be taken because djikstra's will not only give us the the shortest path with valid number of stops to node 2 (dst) but it will also generate shortest path with valid number of stops to all the nodes in graph. So path from 0->3->1 should be the shortest path(dist = 4) which will also be covered under valid number of stops(2) rather than path 0->1 (where dist = 5, stops = 1)
    Edit: valid number of stops instead of least number of stops

    • @vikrambabariya5166
      @vikrambabariya5166 2 года назад +7

      Same question

    • @PiyushBajaj05
      @PiyushBajaj05 2 года назад +18

      Yes, right there is a slight mistake in the video for this, cost of 4 should be considered instead of 5 from the time 15:15 to 15:20, while calculating this.
      Hope @Striver will fix this.

    • @amanbhadani8840
      @amanbhadani8840 2 года назад +5

      @@PiyushBajaj05 Yes it was a mistake there, but we people are mature enough to understand.

    • @abhimanyuraizada7713
      @abhimanyuraizada7713 2 года назад +4

      yup 2+2 should be 4 instead of 5

    • @SaiKumarSays
      @SaiKumarSays 2 года назад +4

      yes.i feel .your point "Edit: valid number of stops instead of least number of stops" exactly correct

  • @Dontpushyour_luck
    @Dontpushyour_luck Год назад +6

    An excellent approach to this problem. I had solved this same problem using pq and maintaining minimum cost to reach every node within each number of stops upto k, and it had worked but your solution has a way better tc and sc.

  • @usmanganimakandar9293
    @usmanganimakandar9293 Год назад +39

    15:30 0->3->1 is 4 and not 5 so we cant ignore that iteration

    • @I_U_I_T_C
      @I_U_I_T_C Год назад +2

      Yes

    • @AJ-xc3ks
      @AJ-xc3ks Год назад +2

      Yess..

    • @rohitkumaram
      @rohitkumaram 11 месяцев назад

      bro, he is prioritizing steps at 1st not distance. That what he explained the mutation in normal Dijekstra .

    • @akhilbaratam2560
      @akhilbaratam2560 11 месяцев назад +1

      @@rohitkumaramNo the prioritizing will be in priority queue not in the mindistance array he should include that too in the priority queue but anyway the answer will be same

    • @Pranauv
      @Pranauv 7 месяцев назад +1

      yes you are right,... i was confused for a second but thought everybody can make mistakes :shrug

  • @pulkitchausali1354
    @pulkitchausali1354 Год назад +18

    replace a condition if(curCityStop > k) continue; with below line
    if(curCityStop > k) break;
    because their is no need to proceed further as all further node will have stops > k
    #best content in youtube #striver

    • @imPriyansh77
      @imPriyansh77 8 месяцев назад +2

      Thank you

    • @rahulnegi4027
      @rahulnegi4027 3 месяца назад +1

      do we even need this condition as we are checking for stops

  • @rishavkumar2182
    @rishavkumar2182 2 года назад +14

    This is the best problem to get a grasp on dijkstra's algorithm so far!! Thanks striver for such a wonderful explanation :)

  • @no_num_no_gum
    @no_num_no_gum Год назад +59

    I feel that the part which needs more attention is quickly skipped and some basic stuff which is quite intuitive is over stretched. Not sure whether everyone understood the correct reason to make a queue of stops, nodes and distance and not a priority queue of distance, nodes and stops

    • @saumyaranjan9256
      @saumyaranjan9256 Год назад +22

      true, he should have explained more on why we do consider even larger distance paths in the case of djikstra as long as it is a valid one and push it back as compared to bfs where we don't need to do so.
      In the bfs, it is inherent that if we come through a path which has a larger distance than the minimum found till now for a node, then the minimum would have been found earlier by a path which had stops

    • @Aks-47
      @Aks-47 Год назад +1

      @@saumyaranjan9256 hi, confused here regarding your bfs explanation, lets take 2 paths, 3->4->1 and 3->5->6->1, suppose path 1 has 40 as distance and path 2 has 20 as distance, "it is inherent that if we come through a path which has a larger distance than the minimum found till now for a node, then the minimum would have been found earlier by a path which had stops

    • @kushalkollu8628
      @kushalkollu8628 5 месяцев назад

      did you find the answer for the "reason to make a queue of stops, nodes and distance and not a priority queue of distance, nodes and stops" ?

    • @Ayanoo_7
      @Ayanoo_7 5 месяцев назад

      ​@@kushalkollu8628Just image if you are starting from node 0 - - >3 and between 3 - - 4 there will be extra node but with small distance.
      So if you take priority Queue with distance, The node 4 will never be accessed by node 1 even if it have sufficient stop.

  • @ashfaqmurshed503
    @ashfaqmurshed503 2 года назад +122

    Just make sure in the if check you put the condition as (cost + edW < dist[adjNode]) and not (dist[Node] + edW < dist[adjNode]) or else edge case will fail
    MISCONCEPTION: we can save some space in the queue nd do queue coz distance is already obtainable from dis[ ], so why maintain it specifically in the queue?
    ANS: no coz in the queue we store the distance coming from a specific parent node ie a particular path , nd that path is used for further exploration when we take it out of the queue nd consider that paths distance ...but there can be other paths as well...dis[ ] can't maintain the paths, it just stores the path with minimum distance, which may not guarantee the path with least stops(which is our top priority)...hence we have to maintain the instance of the distance of a particular node in the queue

    • @takeUforward
      @takeUforward  2 года назад +20

      Well potrayed.

    • @Yash6189
      @Yash6189 2 года назад +2

      Well said.

    • @balajiarumugam1876
      @balajiarumugam1876 2 года назад +11

      this comment is worth the video explanation !!, been stuck on it for a long time. thanks for putting the light in place. Striver content quality -> 100%

    • @gauravkungwani
      @gauravkungwani Год назад +3

      Yes this is mistake commonly done

    • @sunkuthanmai729
      @sunkuthanmai729 Год назад +2

      I was doing this and getting wrong ans for last test cases. Thank you for the explanation

  • @amanbhadani8840
    @amanbhadani8840 2 года назад +3

    This was the some level good problem bhaiya which forced us to think why simply we can not use dijkstra algorithm and also normal queue can be used to solve the problem.

  • @rudrakhare1158
    @rudrakhare1158 5 месяцев назад +3

    At 15:26 We will consider (1,3,2) with stop=1,node=3,cost=2. This is pointing to 1 with cost +2, so we will get (2,1,4) and append it to the P-Queue.
    In next iteration we will pop (2,1,4) ,with stop=2,node=2cost=4,that is the minimum from heap. This pointing to node 5 with cost +5 and will give(3,2,9) and to node 4 with cost +1 and will give(3,4,5). Since we reached 2 with better cost of 9 we will update it from 10 to 9. and append both (3,2,9) and (3,4,5) to the PQ.
    Now, we will pop (2,4,6) with stop=2,node=4,cost=6 (from 2nd iteration). This is pointing to node 2 with cost +1 and we will get (3,2,7).
    Again we reached node 2 with lesser cost of 7 within 3 stops.
    This is the most minimum we could go. As all the element left in PQ are having more stops. Hence, Striver is always right, just he missed something for which he already made us ready.
    Waiting for tomorrow. 7/08/2024 for TUF+.

  • @rethickpavan4264
    @rethickpavan4264 22 дня назад

    Glad that I found the optimal approach without watching video , Thanks Striver bhai

  • @AMANZAKIRHUSAINMODAN
    @AMANZAKIRHUSAINMODAN 7 месяцев назад +14

    My intuition is on vacation !! How many have same issue?

  • @sakinobashi168
    @sakinobashi168 2 года назад +4

    21:31 you can also use 'break' it will still work and obviously a bit faster

    • @virusdgamer8804
      @virusdgamer8804 Год назад +1

      for all those jinko reason nhi chamka for this: because all the incoming iterations will have >= the number of stops in the current one, and hence they will all exceed the maximum allowed limit.

    • @gaganagarwal7218
      @gaganagarwal7218 Год назад

      @@virusdgamer8804 Your statement contradicts itself. You're right when u say ">=" but this implies that the upcoming iterations can have lesser cost with the same number of stops (there is "=" in ">=")

  • @ashmitgupta8039
    @ashmitgupta8039 2 месяца назад

    Best Explanation I got over the web.

  • @riddhiroy1428
    @riddhiroy1428 9 месяцев назад +2

    While deciding to insert in priority queue, we need to sort by lowest cost and keep a check to see if cost is greater, atleast number of stops are less or not.

  • @AjaySingh-xy9zq
    @AjaySingh-xy9zq 2 года назад +3

    Thank You For providing Such an Amazing series on Graph !!!
    Literally Its Great

  • @kanishq8684
    @kanishq8684 4 месяца назад +3

    I don't understand why people find this problem confusing. Sure, it can be solved with a priority queue, but since the number of stops increases by one at each step, using a regular queue can help reduce the time complexity somehow. Another important point is that we're using the number of stops as the main criterion. If you reach a destination with a smaller distance but more stops, it's not useful. Therefore, it's better to prioritize routes with fewer stops first. Even after that, we still check routes with more stops, but only if the number of stops is within the allowed limit (≤ k). In such cases, we store those routes as well.

  • @stith_pragya
    @stith_pragya Год назад +3

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

  • @mohmedfaizrozdarsyed1234
    @mohmedfaizrozdarsyed1234 11 месяцев назад

    The way you explain is amazing , have been solving the problems in leet code with the help of your explanation sir.

  • @skt7088
    @skt7088 2 года назад +20

    Why cant time complexity be O(E + V) ?

  • @aryanmalguri3275
    @aryanmalguri3275 Год назад +20

    Dijkstra with PQ will also work, just don't push the value inside the queue, if value of stops > k, because we are sure that, it won't yield us the ans.

    • @vm1662
      @vm1662 5 месяцев назад +8

      I tried with PQ but it will fail in below case if we don't put the value in the queue if value of stops > k,
      [[0,1,5],[1,2,5],[0,3,2],[3,1,2],[1,4,1],[4,2,1]]
      src = 0 and dst = 2
      k =2
      The PQ will give ans = 9 but it should be 7

    • @MayankSharma-wx4mf
      @MayankSharma-wx4mf 4 месяца назад

      @@vm1662 It works correctly and answers 7.

    • @ravimishra3169
      @ravimishra3169 2 месяца назад

      @@vm1662 No, it will work(sharing the code)
      class Solution {
      public int findCheapestPrice(int n, int[][] flights, int src, int dst, int k) {
      //making graph
      ArrayList adj=new ArrayList();
      for(int i=0;i

    • @shujauddinsiddiqui8923
      @shujauddinsiddiqui8923 Месяц назад

      @@vm1662
      Just sort the priority queue one the basis of stops instead of price, It will work fine.
      PriorityQueue q = new PriorityQueue((x,y) -> x.s - y.s);
      class Pair {
      int n, p, s;
      Pair(int n, int p, int s) {
      this.n = n;
      this.p = p;
      this.s = s;
      }
      }
      here n is vertex, p is price till that vertex and s is stops till that vertex.

    • @sjain6320
      @sjain6320 12 дней назад

      @@vm1662 same man

  • @krishpatel6741
    @krishpatel6741 5 месяцев назад +2

    1.Make Adj List and Distance Vector.
    2.Push Src Node into queue.
    3.move levelwise and increment cnt on each level.
    4.when you rech dest and your count is k then break it and return distance.

    • @defensiveespeon7734
      @defensiveespeon7734 26 дней назад

      yes this is how I solved it as well. Seems easier to modify bfs to skip paths with a lower value from same or lower level, than to modify Dijkstra to make it not greedy and storing triplet variables to keep track of the level.

  • @divyatejaswinivengada6368
    @divyatejaswinivengada6368 Год назад +1

    Thank you this is really the best way of learning dijkstra limitations and work arounds!!

  • @dolbyagarwal9316
    @dolbyagarwal9316 2 года назад +7

    Thanks Striver, I had tried this question before and was trying to do a priority queue but with few added conditions as per checking distances and both stop, though that had worked but was very time consuming solution. This one is much faster and without priority queue which totally makes more sense.

  • @AmitkumarSaw-uc5bu
    @AmitkumarSaw-uc5bu Год назад +2

    coincidence was that when I was solving this problem using Dijkstra I failed in the test case which was taken as an example by Striver Bhaiya

  • @secretvibz6300
    @secretvibz6300 7 месяцев назад

    fantastic Brother !! You just get us feel how we should improve algorithms based on ques requirement😍

  • @gyanaranjansahoo6258
    @gyanaranjansahoo6258 Год назад

    This is the test case where i actually struggled...... thanks striver :) understood ❤

  • @averylazyandweirdhuman
    @averylazyandweirdhuman 7 месяцев назад +2

    Hi, Striver why are we checking stops k?

  • @AngadSharma
    @AngadSharma Год назад

    PriorityQueue can be implemeted on this problem
    PriorityQueue pq=new PriorityQueue((x,y)->x.stop-y.stops);
    just need to compare stops in minHeap instead of distance
    like explained on 12:00

  • @Moch117
    @Moch117 Год назад +4

    For anyone stuck with Dijkstras:
    Stops has priority over distance. Build the PQ based on stops

    • @muditkhanna8164
      @muditkhanna8164 Год назад +1

      priority queue might give the answer but will take a long time in pq {STOPS,{NODE,DISTANCE}}
      we are prioritising the less number of stops and it might be the case where the stops are greater but still the distance is less and we will take them after. size of pq will be bigger thus larger number of iterations
      its better to do BFS +edge relaxation with queue.

    • @Moch117
      @Moch117 Год назад

      @@muditkhanna8164 yes you're' correct. PQ with stops adds unncessary log time complexity but it will work.
      Using standard queue with stops will also work because the priority here are stops and each time we add elements to the queue, we increase stops
      Both solutions work

    • @iamnoob7593
      @iamnoob7593 Год назад

      @@muditkhanna8164 i would just use queue with no edge relaxation , we need mini distance of destination only. No of nodes in queue might be more than edge relaxed approach , But more intuitive.

  • @beinghappy9223
    @beinghappy9223 2 года назад +2

    Solved on my own . Thanks Striver for the amazing character development in Dijkstra Algo

  • @ameypate9610
    @ameypate9610 7 месяцев назад

    This is a good question to try out multiple algorithms like BFS, Dijkstra, and Bellman Ford and compare their relative time/space complexity.

  • @vikustorm3411
    @vikustorm3411 8 месяцев назад +2

    at 15:20 , i have proved your point wrong by following code.
    /*
    for a same node_1:-
    if i take less distance to reach node_1, then it can take more stops.
    vice versa if it takes more distance to reach node_1, THEN IT HAS take less no of stops.
    this can happen in a case,
    where arrived at destination with less distance but stops>k,
    but we can arrive destination with more distance and stops distance_top + edge_weight){
    dist[adj_node] = distance_top + edge_weight;
    pq.push({dist[adj_node], {stop+1, adj_node}});
    stops[adj_node] = stop+1;
    }

    // visiting node again but with more distance and less no of stops
    else if(distance_top + edge_weight > dist[adj_node] && stops[adj_node]>stop+1){
    int new_adj_distance = distance_top + edge_weight;
    pq.push({new_adj_distance, {stop+1, adj_node}});
    }
    }
    }

    // i can't reach end destination with valid no of stops
    return -1;
    }

    int findCheapestPrice(int n, vector& flights, int src, int dst, int k) {

    return dijkstra(flights, n, src, dst, k);
    }
    };
    */

  • @kasamadhu3509
    @kasamadhu3509 2 года назад +6

    we should not use a priority queue that's it right. Because it always chooses the path with a minimum distance
    , we can't travel in other possible paths. With queue, we can travel all the paths.

    • @visheshagrawal8676
      @visheshagrawal8676 Год назад +1

      take the pq with stops then it will choose the path with minimum stops

    • @imPriyansh77
      @imPriyansh77 8 месяцев назад

      @@visheshagrawal8676 yes

  • @ashishkumaryadav5252
    @ashishkumaryadav5252 2 года назад +13

    Those who are still getting a wrong answer on implementing the code by their own:-
    instead of getting distance of current node from dist vector, get it from the queue
    int cost = it.second.second;
    int cost = dist[node]; // this line will give wrong answer use above line only
    Don't know why this is happening, please tell if somebody has any idea about it.

    • @amanbhadani8840
      @amanbhadani8840 2 года назад +15

      Dist vector just store the minimum distance among various paths, but in queue we store the distance of our current path till that node and it still needs to be further explored, so we can not the minimum distance from dist vector for our current path which store the minimum distance from some other path.

    • @atindraask8074
      @atindraask8074 4 месяца назад

      Thx bro❤

    • @atindraask8074
      @atindraask8074 4 месяца назад

      Because some different path with more steps but less than k will affect our answer

  • @yashshukla1637
    @yashshukla1637 Месяц назад

    Really Really Good Question!. Striver OP!!!

  • @hwp438
    @hwp438 Месяц назад +1

    In line no 27, it's better to use 'break' instead of continue.

  • @sanasultana2988
    @sanasultana2988 2 месяца назад

    Thank you so much bro. you are doing wonderful job

  • @KS0607
    @KS0607 Год назад

    The way u explained ....djikstra will not work...is amazing and rare...

  • @AkashSharma-ij4sn
    @AkashSharma-ij4sn 2 года назад +9

    we are worried only about valid number of stops not the less number of stops so if its the valid number of stops then we can push it into priority queue and increase the stops. so--> {0->3->1} stops(2) we can reach here dist=4 as less than {0-1} with dist=5 stops=1 but its valid only b'coz atmost we can make 2 stops so 0->3->1 is best option with minimum distance

  • @ishangujarathi10
    @ishangujarathi10 Год назад

    loveddd the intuition

  • @vishakhajadhav7064
    @vishakhajadhav7064 6 месяцев назад

    Thank you so much Striver❤

  • @vampconnoisseur
    @vampconnoisseur Месяц назад +2

    yes, striver did a mistake and according to the code he wrote also, {2, 1, 4} will go into the queue. But still it won't affect the right answer which is 7, why, because {2, 1, 4} will stay in the queue along with {1,1,5}, notice we aren't removing it when a shorter distance path came. So both will stay in the queue and at the end due to both of them {3, 2, 9} and {3, 2, 7} and {2,2,10} will be in the queue and we dont just stop, so eventually the minimum will get considered which is 7. So the point is, yes by strivers code only, {2, 1, 4} will go into the queue but it wont get considered in final, its just trying all the possibilities. Notice that in this crooked Dijkstra logic, the priority queue is non existent, so its not even a greedy algorithm, its simply trying all possibilities right by normal bfs. Hope it helps.

  • @sangdilbiswal30
    @sangdilbiswal30 3 месяца назад

    its a fancy bfs... great explanation!!!!!!!!!!!!!!

  • @ironeagle1709
    @ironeagle1709 2 года назад +10

    Solved this problem by own before watching the video 🤩. That feeling 🤩🤩Thanks Striver!!!

  • @cinime
    @cinime 2 года назад

    Understood! So wonderful explanation as always, thank you very much!!

  • @priytoshtripathi8000
    @priytoshtripathi8000 2 года назад +1

    This was an awesome explaination. Thanks!!

  • @kartikdalai7998
    @kartikdalai7998 Год назад +3

    @takeUForward same question same src and dst from your video but increase k to 3.
    still it will give the path which we got for k=2 but it should give us 0-3-1-4-2 with cost of 6

    • @sahiljain7871
      @sahiljain7871 8 месяцев назад

      Yes, I was thinking the same.

    • @skblabla
      @skblabla 5 месяцев назад

      @@sahiljain7871 I was thinking same and finally understood after doing a dry run, its quite difficult to understand that bit in first go, but even though dist[1] will be 4, that doesn't mean nodes 2 and 4 will have path with 3, since the queue will have other paths that will help reach to destination 2 within k stops. dist[i] array is shortest path in k stops to reach i and dist[i] does not mean dist[i+1] will be via i.

  • @zaiem6981
    @zaiem6981 3 месяца назад

    I see some of the comments were they are mentioning that we can omit the cost variable from the queue and take it from the distance[]. Well, that will not work. Let me tell you why:
    1. Two different paths to the same node can have different numbers of stops. In some cases, a path with more stops may offer a cheaper price. The distance[] array may not properly capture these relationships between cost and stops, leading to incorrect results.
    2. Let’s say you have two paths to a destination:
    Path A reaches the destination with 2 stops and a cost of 100.
    Path B reaches the same destination with 3 stops and a cost of 90.
    If you only use distance[], after processing Path B, the distance[] array will record 90 as the minimum cost to reach the destination, overwriting Path A's information. This could lead to an invalid solution if your algorithm later tries to use Path B (which exceeds the stop limit k).

  • @kaichang8186
    @kaichang8186 4 месяца назад

    understood, thanks for the great explanation

  • @GauravKumar-xc4kr
    @GauravKumar-xc4kr 9 дней назад

    well well what if we put a condition to insert in heap with the condition to insert when
    stops < k
    wont this work?
    edit -> it wont work because reaching a destination 'k' could take 0 stops with greater distance than taking 1 stops with lesser distance
    it would consume our 1 stop
    hence traversing every possibility is required in this question

  • @akshatsharma5426
    @akshatsharma5426 Год назад

    I was trying to solve this problem on gfg and I was getting wrong answer on exactly this test case.Now I understand the mistake made by me.

  • @phuonganho9302
    @phuonganho9302 11 месяцев назад

    Thank you for your explaining.

  • @RohithChandra.Kae21b054
    @RohithChandra.Kae21b054 4 месяца назад

    At 15:32 if we have more stops to spare, we can chose the path with minimum cost even though the stops taken are more. The algorithm will fail in that case right?

  • @Ramu9119
    @Ramu9119 Год назад

    excellent problem and explanation as well

  • @yoMisterWhyte
    @yoMisterWhyte 11 месяцев назад

    If folks had questions of what happens in case we ended up updating the distance array for a node, in the path that is not going to make it to the destination, like in Neetcode's example, distance to C node can be 200 if K=1, but 500 if k=0. Extending the graph in that example to C to D with edge weight 100 and D to E with edge weight 100, if k is given as 1, then there might be a concern that C will be updated to 200 in the path A-B-C and it will contribute to wrong distance going forward all the way to E. But the subtle point is, because it's a longer path, the train from A -> C had already passed it and is already calculating shortest distance for nodes after C. In this case even though distance array for C is updated to 200, it doesn't affect the calculation of the train that went from A to C and further since C was only one hop away and in BFS got discovered first. So we will end up getting the correct distance at the destination node even though the final distance array doesnt match up. On the other hand, had k been greater than 3 then that A-B-C path would have been valid and distance 200 would have been valid and will contribute to the final stop at E.

  • @CaptainJack56784
    @CaptainJack56784 2 года назад

    Needed that kind of explaination🤩🤩

  • @iltabishkhan
    @iltabishkhan Год назад +1

    At 15:17 when node3 go to 1 it takes 4 cost not 5.

  • @rushidesai2836
    @rushidesai2836 9 месяцев назад +1

    Brilliant question tbh

  • @poornimajd5124
    @poornimajd5124 24 дня назад

    Simply awesome!!!

  • @UECAshutoshKumar
    @UECAshutoshKumar Год назад +1

    Thank you sir 🙏

  • @aniljoshi8331
    @aniljoshi8331 6 дней назад

    In this problem, please note that the same edge can be traversed multiple times. It's because we can arrive at a node once again with higher number of stops but lesser cost and hence it can be pushed into the queue once again. When removing such elements from the queue all the adjacent neighbours will also be traversed again. Hence the worst case time complexity has to be O(V + E*K), each edge can be traversed at most K times.

  • @sameerbhardwaj8949
    @sameerbhardwaj8949 Год назад +1

    Time complexity for Dijkstra is O(E * logV) since it is a greedy algorithm and we mark nodes as visited. Here we are visiting each node multiple times, since we are not marking them as visited, so time complexity cannot be better than Dijkstra. I think time complexity in the worst case would be (E * N) for this solution, since we can visit each 'edge' starting from each node. Please correct me if I am wrong.

    • @mohammadkaif8143
      @mohammadkaif8143 Год назад

      yep, you are right. Max it can ve E*N

    • @mohammadkaif8143
      @mohammadkaif8143 Год назад

      You can also use Priority queue but only update the distance if the new distance is minm as well as the stops is less than equal to.

  • @nikee-ux5kl
    @nikee-ux5kl Год назад +1

    I have used distance for the priority queue and and added a greedy condition where i pushed when a node can be reached in a higher cost than the one in distance array but in less no of steps
    //{ Driver Code Starts
    #include
    using namespace std;
    // } Driver Code Ends
    class Solution {
    public:
    int CheapestFLight(int n, vector& flights, int src, int dst, int K) {
    vector edges( n );
    if(src == dst)
    return 0;
    for(int i = 0 ; i < flights.size() ;i++){
    edges[flights[i][0]].push_back({flights[i][1] , flights[i][2]});
    }
    priority_queue pq;
    pq.push({0 , {0 , src}});
    vector dist( n , {1e9 , 0});
    dist[src] = {0,0};
    while(!pq.empty()){
    pair top = pq.top();
    pq.pop();
    int base = top.first;
    int stops = top.second.first;
    int city = top.second.second;
    for(auto it : edges[city]){
    int adj = it.first;
    int price = it.second;
    if(base + price < dist[adj].first && stops > tc;
    while (tc--) {
    int n; cin>>n;
    int edge; cin>>edge;
    vector flights;
    for(int i=0; ix;
    temp.push_back(x);
    }
    flights.push_back(temp);
    }
    int src,dst,k;
    cin>>src>>dst>>k;
    Solution obj;
    cout

  • @rajneeshdubey5779
    @rajneeshdubey5779 10 месяцев назад

    At 15:20 we have to add {2,1,4 } in queue ,taking the path from 0->3->1. lets take a example where in a above example just change the cost between vertex 1->2 from 5 to 1 and then apply the same logic if we won't add it in queue this will give wrong answer

  • @mohammadkaif8143
    @mohammadkaif8143 Год назад +3

    Summary & Logic::
    It is quite straightforward that Dijakstras algo will fail because it might happen that we arrive at some node at a lesser distance but number of stops taken can be more and hence we can't move further to destination.
    Second questn is Why Queue logic is working here:: If we move step by step and if there is a valid answer then first time whenever we will be reaching at the destination store that distance in an answer. Now question arises what if we took more number of steps and it also reaches the destination within K stops, my answer my is it will then updated our ans. Second questn is what if we took more number of steps and it does not reaches the destination within K stops then my answer is it will just affect the intermediate node dist and as stops get exhausted it will reach not the dest and hence it will not update the final destn answer.

  • @lavanya_m01
    @lavanya_m01 10 месяцев назад

    Shouldn't the time complexity be O(V+E) ? Coz we're using bfs, the queue can hold V nodes and all can traverse it's adjacent nodes. Thankyou so much for your efforts Striver! I'm not sure if someone could come up with such a solution on their own when asked in an interview

  • @adarshrajshrivastava1113
    @adarshrajshrivastava1113 7 месяцев назад +1

    So if I set k=n-1, then problem will simplify to "finding shortest distance between src and dst". Now, using shown algorithm which uses queue as data structure will take O(E+V) time complexity to find the shortest distance, while dijkstra will take (E+V)logV, which is worse than the proposed algo. So, that means we can use above algorithm in place of dijkstra?

  • @prasannasippa5962
    @prasannasippa5962 2 года назад +3

    Understood striver ! wonderful explanation as always, thank you very much for your efforts to make us understand !! now my feeling is graph questions are very easy 😅
    Thank you so much Striver!!!

  • @karthikkrishna1837
    @karthikkrishna1837 3 месяца назад

    Why do we store stops as the first priority and not price? What if we store price as the first priority but before inserting neighbors into the priority queue we just check if the number of stops > k and if it is then dont insert into PQ itself? What is the error in this approach

  • @VimanshMahajan
    @VimanshMahajan 10 месяцев назад

    if(stops>k) not required we are already checking stops

  • @nithishkumar8658
    @nithishkumar8658 Год назад +2

    i am not clear about y cant we stop when we reach the destination can anyone help me!!

  • @Nothing-eg9ol
    @Nothing-eg9ol 2 года назад

    just amazing keep uploading❣

  • @nba_nerd
    @nba_nerd 11 месяцев назад +1

    Time complexity seems incorrect. In worst case it could go up to n^k as there could be multiple paths of almost kth length possible to any node.

  • @badaljha6257
    @badaljha6257 2 года назад +1

    Thank u striver😌😌

  • @abhisheksingh-np8yi
    @abhisheksingh-np8yi Год назад

    No need for stops k never lets value greater than k passes

  • @solitucedagar4298
    @solitucedagar4298 7 месяцев назад

    I don't get it if we skip the 0->3->1 path, if the destination had been 4, then the answer that is getting stored for 4 is incorrect.

  • @vakhariyajay2224
    @vakhariyajay2224 2 года назад

    Thank you very much. You are a genius.

  • @amanasrani6405
    @amanasrani6405 7 месяцев назад

    Thank you soooooo much bhaiya❤

  • @AquaRegia-i3u
    @AquaRegia-i3u Год назад

    One another way can be use max PriorityQueue instead of min PriorityQueue. Calculate maximum price first and reduce it based on stops. However its slower.

  • @josephstark5810
    @josephstark5810 Год назад

    in this algo we only avoid the nodes the visited previously less edge count and weight note alter them

  • @samranayesrab5546
    @samranayesrab5546 Год назад +1

    Is it necessary to have an extra check of stops K then continue(line 27)?

    • @Dkk10_supra
      @Dkk10_supra Год назад +1

      @samrana, I too find it unnecessary

  • @shadanhussain5107
    @shadanhussain5107 Год назад +1

    Why is (2,4,6) getting executed before (2,2,10) in the priority queue at 15:47 , can anyone give valid explanation
    Edit: basically later in the video he told to use queue which resolves the issue, so yeah it won't work with pq but will work with queue

  • @1tav0
    @1tav0 2 года назад

    understood this is awesome explanations

  • @ApiiiitaaaaPakoooodaaaaaa
    @ApiiiitaaaaPakoooodaaaaaa 7 месяцев назад

    why do we write continue statement over there ?

  • @ankitraheja9524
    @ankitraheja9524 5 месяцев назад

    What is the advantage of using PQ? Queue should have been enough. I ran the same program with PQ as well as Queue, did not find any difference in runtime. Can u please elaborate in this particular question what advantage PQ has brought? Thanks in Advance!!

  • @omkarkadam309
    @omkarkadam309 6 месяцев назад

    isn't this just like a BFS of level-wise traversal, along with array tracking distance ??

  • @rishabhagarwal8049
    @rishabhagarwal8049 2 года назад

    Understood Sir, Thank you sir

  • @gypsygirlkigypsylife
    @gypsygirlkigypsylife 11 месяцев назад

    should it not be if node==destination then continue,as we are considering for stop+1 also ,while it not skip the stop+1=3 (in example) with the statement if (stop>k) continue;

  • @sakshisinghal1669
    @sakshisinghal1669 2 года назад +6

    How many videos are you planning to make in this playlist and what all questions are you planning to cover?

    • @takeUforward
      @takeUforward  2 года назад +9

      You can find out all the details in the A2Z Sheet in the graphs section.

    • @sakshisinghal1669
      @sakshisinghal1669 2 года назад

      @@takeUforward Sure, can you please help me with that sheet? I have your Striver's SDE sheet but the graph questions there are not the same you are covering here. Definitely I am missing something.

    • @RahulKishore8
      @RahulKishore8 2 года назад

      @@sakshisinghal1669 there's another sheet called A2Z sheet

    • @sakshisinghal1669
      @sakshisinghal1669 2 года назад

      @@RahulKishore8 thanks got it.

  • @jaishriharivishnu
    @jaishriharivishnu 7 месяцев назад

    i believe we don't need a Distance array..... Simply we will do bfs on the number of stops, and keep decreasing the destination's distance.

  • @reggiehurley1479
    @reggiehurley1479 Год назад

    imo the algo would be more clear if we kept track of the length of the path outside of the tuple to emphasize the level order nature

  • @hrushi_borhade
    @hrushi_borhade Год назад

    understood striver!

  • @Tech.IT.e
    @Tech.IT.e 2 года назад +1

    Striver Sir, Could you please elaborate the java code also.

  • @mayankjain-901
    @mayankjain-901 9 месяцев назад

    Why haven't we consider maintaing a visited array ?

  • @keertilata20
    @keertilata20 2 года назад +1

    15:20 slight mistake here the distance is 4 not 5

  • @rbk.technology4747
    @rbk.technology4747 2 года назад +2

    Striver one doubt please reply and clarify.
    Time complexity should be O(EV) right
    Bcoz see the outer while loop can run at Max for E.
    Then comes inner for loop iteration of adj nodes. A node at Max can have V-1 neighbours.
    So totally it boils to O(EV) right?
    If not please give me counter example and clarification over it.
    This judgement of time complexity is bit tougher for me.

    • @vaibhavagrawal3381
      @vaibhavagrawal3381 2 года назад

      I have the same doubt.

    • @vaibhavagrawal3381
      @vaibhavagrawal3381 2 года назад

      vector < int > cost(n,1e9);
      cost[src] = 0;
      for(int i=0;i cost2 = cost;
      for(auto x: flights){
      if(cost2[x[1]] > cost[x[0]] + x[2]) cost2[x[1]] = cost[x[0]] + x[2];
      }
      cost = cost2;
      }
      return cost[dst] == 1e9 ? -1 : cost[dst];
      This can be simple solution with same complexity.

    • @abhirupbasu3386
      @abhirupbasu3386 Год назад +1

      it should O(V+E).think just like normal bfs.

    • @user-le6ts6ci7h
      @user-le6ts6ci7h Год назад

      ​@@abhirupbasu3386 it is not like normal bfs , in normal bfs u are vsiiting each node and edgr as ingle time , so the time complexity bis v+e

    • @cscoded
      @cscoded Год назад

      @@abhirupbasu3386 no man in normal bfs we maintain visited array and visit once ,, here the time complexity should be o(ek)

  • @shreyasharma7987
    @shreyasharma7987 11 месяцев назад

    Can someone tell me why the number of stops has to be given priority and not the shortest distance? couldn't understand.

  • @ARYAN-mg3rr
    @ARYAN-mg3rr Год назад

    why to use continue in line 27?