Travelling Salesman Problem using Dynamic Programming - Easiest Approach with Code

Поділитися
Вставка
  • Опубліковано 26 лис 2017
  • Check courses on - online.codingblocks.com [Free Trial Available]
    Coding Blocks is pleased to announce courses like C++ and Java, Data Structures and Algorithms, Web and Android Development(Java and Kotlin), Competitive Programming, Coding Interview Preparation and Machine Learning, AI and more.
    #CodingBlocks #ProgrammingMadeEasy #LearnCodingOnline
    Code - codingblocks.com/ide/#/s/3899
    Like our FaceBook Page - / codingblocksindia
    Follow us on Instagram - / codingblocks
    Follow us on Twitter - / codingblocksin
    Source code available on -github.com/coding-blocks
    For more interesting tutorials - / @codingblocksindia

КОМЕНТАРІ • 143

  • @AbhishekKumar-yv6ih
    @AbhishekKumar-yv6ih 3 роки тому +55

    I watched all the videos of TSP (Abdul bari, William Fiset, Tushar Roy) . This is the best Explaination along with code.

    • @bestsaurabh
      @bestsaurabh 3 роки тому +14

      I think those vids were not useless. They helped in installing the idea and some part of the logic in your mind and then finally in this vid you got the Ahha! moment. :D

    • @ayushvats1808
      @ayushvats1808 3 роки тому +3

      @@bestsaurabh exactly this happens lot of time yr

    • @pool477
      @pool477 2 роки тому

      Prateeek bhaiya op

    • @business7624
      @business7624 Рік тому

      bro can you help where is the code, im not geeting it, i have submission today?

  • @Firebolt68
    @Firebolt68 5 років тому +16

    I used the Hamiltonian cycle algorithm for my university's delivery service final project. It was the fastest delivery service out of every project submitted.

  • @ragingpahadi
    @ragingpahadi 4 роки тому +9

    Bit Manipulation explanation saved lot of hair : ]. Thanks for such a elegant and simple explanation

  • @Thompzful
    @Thompzful 4 роки тому +17

    Amazing tutorial, looked online for hours but this is definitely the best introduction to the tsp problem. :)

  • @rahulkumarsinha4239
    @rahulkumarsinha4239 3 роки тому +3

    Its really brilliant lectures on youtube. Coding blocks always rocks..

  • @yatnbangad7893
    @yatnbangad7893 3 роки тому +1

    You've explained it soo nicely!!! Thank you so much!

  • @neerajverma5260
    @neerajverma5260 3 місяці тому

    You've explained it soo nicely!!! Thank you so much!🙏

  • @vm1662
    @vm1662 4 роки тому +4

    Thank you so much for the video! I had been looking for a good DP solution for a while now. And you explained it in great depth. Appreciate the work you have put in :)

  • @ayushbhardwaj6783
    @ayushbhardwaj6783 Рік тому

    I loved it. You explained the bitmask concept soo well. :)

  • @proprogrammer7976
    @proprogrammer7976 4 роки тому

    You are great bro understood in one go .

  • @katsaronas
    @katsaronas 4 роки тому +15

    Very intuitive video. Can you give some information on how we can get a path in output?

    • @yezhou2284
      @yezhou2284 Рік тому

      Do you have the answer now?

  • @anusatyachoudhary7382
    @anusatyachoudhary7382 3 роки тому +1

    Excellent explanation.

  • @Endermore
    @Endermore 6 років тому +1

    very helpful video, thank you!! just one question, how would one modify this code so that it could visit 1000 cities? n would be 1000, and so it would throw a shift counter overflow error.

  • @engulfing_marubozu
    @engulfing_marubozu Рік тому

    Great.. loved it!

  • @gauravraj7005
    @gauravraj7005 4 роки тому

    Amazing explanation.. thank you

  • @chakalimadhu6928
    @chakalimadhu6928 Рік тому

    Simply superb approach

  • @rayanbag3017
    @rayanbag3017 2 місяці тому

    I just loved it. Thank you very much.

  • @anuragkandulna9546
    @anuragkandulna9546 4 роки тому

    This is one of the best tutorial that I have ever seen when it comes to TSP using DP with the code side by side. Respect _/\_

  • @asmitpapney4671
    @asmitpapney4671 3 роки тому +4

    usually i don't comment much .... but this is seriously beautiful :)

  • @tusharrawat8768
    @tusharrawat8768 4 роки тому +5

    Time complexity is O(n^2 * 2 ^ n) as we are filling each value in the dp table and to compute the optimal value for each cell in dp table we are traversing to all possible next cities which are n.

  • @murugappank2833
    @murugappank2833 4 роки тому +1

    Nice video..how do we reconstruct the optimal tour?

  • @kumarashutosh6402
    @kumarashutosh6402 3 роки тому +1

    Can we use bool array for keeping track of visited nodes,if no then why not?

  • @zahidirshad82
    @zahidirshad82 5 років тому +1

    Being an algorithm analyst what do you think, "If TSP problem is solved by using dynamic programming approach, will it provide feasible solution better than greedy approach?

  • @alexbeh2061
    @alexbeh2061 2 роки тому +2

    The base case is a bit confusing to me. I think the correct value to return should be dist[pos][origin_pos] instead of dist[pos][0]. The answer is still correct for this example in this video because the origin_pos is queal to 0.
    This is one of the best tutorial to explain the TSP solution🎉🎉🎉

    • @utkarshgangwar2173
      @utkarshgangwar2173 2 роки тому

      bro, we always get a hamiltonian cycle and cycle means no start point, so if you are doing this problem from any node as start point, it doesnt matter, answer will be the same, so if you are using 0 as start node then base case will be returning to 0 as in that case you have confirmed that you have found a cycle, so dp[pos][0]

    • @Sam-rz5hw
      @Sam-rz5hw Рік тому

      @@utkarshgangwar2173 nah if you start from the city 2 you will want to end at city 2, but in the code 0 is hardcoded, which will not produce the desired result, so alex is correct

  • @misaelsarmiento8931
    @misaelsarmiento8931 5 років тому

    I have a question i hope you could answer to me. How would you print all the visited nodes besides printing the total distance? What could be (and where should be) the line of code? Thanks in advance!

  • @ekjotnanda6832
    @ekjotnanda6832 2 роки тому

    this is great, thanks 👍🏻

  • @adhaarsharma753
    @adhaarsharma753 3 роки тому

    Best Explaination on Travelling Salesman Problem.

  • @shwetagiri1690
    @shwetagiri1690 7 місяців тому

    Thank you !

  • @dhananjaydhawale5579
    @dhananjaydhawale5579 Рік тому

    Best Explanation, thanks

  • @NareshMeena-oj3du
    @NareshMeena-oj3du 4 роки тому

    After visiting all node why just return the weight of direct edge to source from the destination (mask==all_visiting, return adj[mask][pos] ), there may be some indirect path to the source of minimum weight from the destination. can you explain it a little further?

  • @Srivatsan1999_
    @Srivatsan1999_ 4 роки тому +1

    How to use dynamic programming if there are more cities than number of bits in integer variable?

  • @amankumarkashyap400
    @amankumarkashyap400 5 років тому

    what if there is no direct path from the last node to the first node . in that case the Dist will be 0.but than here we should some other path from last city to the first city, instead of just returning the Dist[last][first].

  • @RaviKant-ll6ck
    @RaviKant-ll6ck 3 роки тому +2

    Nice explanation but you should have printed path also.

  • @abdullahkhalid9581
    @abdullahkhalid9581 5 років тому +2

    Thank you very much sir!

  • @ezhilkannan7596
    @ezhilkannan7596 4 роки тому +1

    What is the time complexity of this implementation?

  • @MegaModelina
    @MegaModelina 5 років тому

    Do we ever even hit cache for such small # of cities as in this example? What # of cities would need to hit cache?

  • @qaseemhasan3594
    @qaseemhasan3594 4 роки тому +1

    Dp array of 2^n by n would cause MLE for even a small n

  • @chetanpatidar6776
    @chetanpatidar6776 2 роки тому +1

    I got to know about bitmasking here,
    used in my lab question of tsp with pickup and delivery
    boom
    11 sec code got accepted in just 4 sec.
    loved it!!

  • @navneetkumar5152
    @navneetkumar5152 4 роки тому

    best explanation :)

  • @aribakhtar7281
    @aribakhtar7281 11 місяців тому

    Great explanation keep it up

  • @aishwaryasingh246
    @aishwaryasingh246 4 роки тому +3

    Hey! Great video. Quick question though - do we make use of the values in the dp table ie. memoization? I tired the below code (java implementation of the code in the video), seems like we never make use of the dp table values.
    Please correct me if I am wrong. Appreciate any help. Thanks for the video!
    import java.util.* ;
    class Salesman{
    int travel(int a[][], int start){
    int n = a.length;
    int row = 1

    • @TEKONEL
      @TEKONEL 4 роки тому

      sen adamsın alfaların şahısın daşşanı gezdiren yesin

    • @ritikagupta8847
      @ritikagupta8847 3 роки тому

      I think the same, the dp optimisation is probably of no use. The recursive code itself is of O(n^2 2^n) time complexity
      .

  • @ayusharora2019
    @ayusharora2019 3 роки тому +1

    Can you pin in chat the time complexity before and the after the memoization?

  • @sudhamahajan2272
    @sudhamahajan2272 3 роки тому

    what is benefit of storing the results in dp table..??it is never used again in the code..

  • @kumarravi09
    @kumarravi09 4 роки тому

    Simple and best so far

  • @chaluman9325
    @chaluman9325 3 роки тому

    Is this the held-karp algo? @CodingBlocks

  • @dhevasrishan3967
    @dhevasrishan3967 2 роки тому

    can anyone explain if there is no route from one distance to another and must take another route, what integer should be in the matrix or in the row

  • @gamesfornerds1
    @gamesfornerds1 4 роки тому +1

    How can we print the path taken ?

  • @vasusharma1192
    @vasusharma1192 3 роки тому +2

    Best tutorial for TSP

  • @NickKMusic
    @NickKMusic 4 роки тому

    Is it possible to find also the path and not only the weight ?

  • @pursottamsah6430
    @pursottamsah6430 5 місяців тому

    Great solution

  • @izzah1893
    @izzah1893 2 роки тому

    auuuuuu thannkkyouuuuu for the detailed explanation!!!! looking for hours to understand this topic . Can i ask is anybody know how to print output the path that has the min cost?

  • @amankumarkashyap400
    @amankumarkashyap400 5 років тому

    Why can't we just use a boolean vector to store visited state of the nodes in the graph instead of using bitmask

  • @akarj
    @akarj 3 роки тому

    👏👏👏 the explanation!!

  • @sarthakjain2813
    @sarthakjain2813 2 роки тому

    Sir, how do we convert this code to iterative bottom up dp?

  • @LastManRising
    @LastManRising Рік тому

    good explanation of code

  • @manasbohat6883
    @manasbohat6883 2 роки тому

    best explanation

  • @jawadsaleem7495
    @jawadsaleem7495 7 місяців тому

    Is there a way to keep track of the shortest path cycle also??

  • @curse_breaker_05
    @curse_breaker_05 Рік тому

    visited all(1

  • @ayushraj-zb6sv
    @ayushraj-zb6sv 3 роки тому

    can you share some problem based on this concept for practise ?

  • @shivamojha7999
    @shivamojha7999 4 роки тому

    Thankx

  • @VishalKumar-kr9me
    @VishalKumar-kr9me 9 місяців тому

    It was very nice, and so beautifully explained.
    But you didn't tell the time complexity of the DP solution and also didn't show the overlapping example.

  • @prateekkanaujia7341
    @prateekkanaujia7341 Рік тому

    Best tutorial :)

  • @AbhishekKumar-yv6ih
    @AbhishekKumar-yv6ih 3 роки тому +1

    At 16:45, I can't see any overlapping subproblems. Can you give some example where I can see that?

  • @aymen6765
    @aymen6765 3 роки тому

    Hi, is that GRASP Metaheuristic ?

  • @user-gp8fr1nd3w
    @user-gp8fr1nd3w 5 років тому

    How do we get the path now?

  • @SagarGupta-yx8hn
    @SagarGupta-yx8hn 5 років тому

    Here you have taken the origin as always node 0 for all permutations. What will be the solution if that wasn't the case? i.e. we could start from any node and come back to it after travelling all the nodes.

    • @aroo1377
      @aroo1377 2 роки тому

      n*(n!) in case of bruteforce and for dp part n^2(2^n) I suppose

  • @aneezaimtiaz7837
    @aneezaimtiaz7837 4 роки тому +2

    Please provide a code for displaying the path thank u

  • @business7624
    @business7624 Рік тому

    bro can you help where is the code, im not geeting it, i have submission today?

  • @Akashdeepkashyap
    @Akashdeepkashyap 3 роки тому +1

    you showed us how to find cost i need to know how do i find the path

  • @himanshubisht6957
    @himanshubisht6957 6 років тому

    The MASK created is of type Integer, so it can store max of 32 bit, or 80 with long long. But what if the number of cities is too large(1000) , in that case MASK won't be able to show, how many cities are visited, we will have a overflow. So what to do???.

    • @CodingBlocksIndia
      @CodingBlocksIndia  6 років тому

      Use array but time will also increase as no of cities increase

    • @piyushshekdar4270
      @piyushshekdar4270 Рік тому

      @@CodingBlocksIndia How to store an array in a DP cache ?

  • @amitavamozumder73
    @amitavamozumder73 3 роки тому

    DOnt try to do this is NodeJS, bit manipulation is weird there, it backflows way before the max safe int limit for some reason.

  • @shikharsaxena9989
    @shikharsaxena9989 4 роки тому

    what if edge d to a is not present

  • @fr3sm4k3r
    @fr3sm4k3r 6 років тому +2

    Nice video all works fine but tell me how can I get a path to this result?

    • @user-gp8fr1nd3w
      @user-gp8fr1nd3w 5 років тому

      did you figure it out?

    • @user-gp8fr1nd3w
      @user-gp8fr1nd3w 5 років тому

      @@rahulduggal4799 did you figure it out?

    • @NickKMusic
      @NickKMusic 4 роки тому

      @@user-gp8fr1nd3w hey man, did you find a solution of how i am getting the path ?

  • @sumanthVEVO
    @sumanthVEVO 4 роки тому

    Hi sir,
    For the below test case, the given output is 11 as per your code. But the actual answer is 20.
    {
    { 0, 1, 2, 0, 8},
    { 1, 0, 3, 5, 6},
    { 2, 3, 0, 4, 0},
    { 0, 5, 4, 0, 7},
    { 8, 6, 0, 7, 0}
    }

    • @kapil8965
      @kapil8965 4 роки тому +4

      Hey the problem arises because (If my assumption is correct) you are denoting unreachable nodes weight by 0 so in that case the algo will take the path A->D->C->E->B->A with a total cost of 11.Instead what you could do is take the unreachable nodes weight as infinity(or some big number) then the algo will produce the desired result . Hope this helps :)

  • @akshaygunshetti2519
    @akshaygunshetti2519 5 років тому

    How to find the path ?

  • @3darpan
    @3darpan 3 роки тому

    shouldn't the source be changed everytime?

  • @muhammadtahsinamin8255
    @muhammadtahsinamin8255 3 роки тому

    Can someone tell me what this "10" does in "dist[10][10]"

  • @manthankhorwal1477
    @manthankhorwal1477 3 роки тому +2

    I dont think DP do any optimization because same sub problem will never occur with same mask value and same node

  • @jatinpopli5617
    @jatinpopli5617 3 роки тому

    Sir path kaise print hoga ismein ?

  • @manumanoj1569
    @manumanoj1569 6 років тому +3

    please explain how we can get a path in output.

  • @user-gp8fr1nd3w
    @user-gp8fr1nd3w 5 років тому

    This doesn't work for cities more than 8. Anyone has a solution for that?

    • @CodingBlocksIndia
      @CodingBlocksIndia  5 років тому

      check this out ua-cam.com/video/JK31jQ632fw/v-deo.html

  • @ankishgupta1517
    @ankishgupta1517 4 роки тому

    bhai 22:25 pr mask& kyu kra hai ??

  • @rituchandna4268
    @rituchandna4268 5 років тому +1

    Are u Rishab sir?

  • @kshitijgupta7991
    @kshitijgupta7991 5 років тому

    Can you provide the solution of how to print the shortest path

    • @user-gp8fr1nd3w
      @user-gp8fr1nd3w 5 років тому

      did you figure it out?

    • @kshitijgupta7991
      @kshitijgupta7991 5 років тому

      @@user-gp8fr1nd3w yes but i didn't got the answer

    • @user-gp8fr1nd3w
      @user-gp8fr1nd3w 5 років тому

      @@kshitijgupta7991 What did you exactly figure out then? I was able to print out the cost but I'm having a problem with the path itself. I tried so many things but nothing is working.

    • @kshitijgupta7991
      @kshitijgupta7991 5 років тому

      @@user-gp8fr1nd3w ya i also tried but didn't get the path.It was always showing 0-0-0-0-0.The path was not coming by any method

    • @user-gp8fr1nd3w
      @user-gp8fr1nd3w 5 років тому

      Kshitij Gupta where did you put your code and what was it? Maybe I’ll be able to fix it.

  • @akshikagarg9774
    @akshikagarg9774 Рік тому

    woww

  • @-johnny340
    @-johnny340 4 роки тому

    How do I do that in Java?

    • @aishwaryasingh246
      @aishwaryasingh246 4 роки тому +1

      This might help. Implemented the code given in the video in java.
      import java.util.* ;
      class Salesman{
      int travel(int a[][], int start){
      int n = a.length;
      int row = 1

    • @suprabhatkumar151
      @suprabhatkumar151 4 роки тому

      @@aishwaryasingh246 I think there is an error in your code if starting point becomes 1 then your code will wrong output. Have a look and correct me if I am wrong.

  • @shivamdubey4783
    @shivamdubey4783 Рік тому

    plzz make in python also

  • @curse_breaker_05
    @curse_breaker_05 Рік тому

    why is it 2^n -1 instead of 2^n

    • @VishalKumar-kr9me
      @VishalKumar-kr9me 9 місяців тому

      @user-pc8ou4uu3t for visited all you are asking?

  • @arism.accola7301
    @arism.accola7301 5 років тому +2

    How to get the path now?

    • @user-gp8fr1nd3w
      @user-gp8fr1nd3w 5 років тому +1

      did you figure it out?

    • @NickKMusic
      @NickKMusic 4 роки тому

      hey man, did you find a solution to your answer , cause i need it please!

  • @ishwargowda
    @ishwargowda 3 роки тому

    everything was fine until i hit DP part, i don't see sub problems

  • @adarshnair9846
    @adarshnair9846 6 років тому

    doesnt work for 5 cities??

    • @CodingBlocksIndia
      @CodingBlocksIndia  6 років тому

      Above code assumes all 5 nodes form a complete graph. It should work then. Or add infinite distance edges if two nodes are not connected.

    • @gunjanmanore6285
      @gunjanmanore6285 6 років тому

      actually i dont understand can u el elaborate this .we require this code for 5 cities

    • @gunjanmanore6285
      @gunjanmanore6285 6 років тому

      and how to display path as well

    • @akashs6816
      @akashs6816 6 років тому

      even i wantd to get the path. u got any idea??

  • @ShivamSharma-xz5je
    @ShivamSharma-xz5je 4 роки тому

    I'm sure that the algorithm fails for 30 cities or more.

    • @makealemonade
      @makealemonade 4 роки тому +1

      shivam sharma use long long then

    • @makealemonade
      @makealemonade 4 роки тому

      That should be enough for any competitive cases

  • @MustafaKHAN-sw1xb
    @MustafaKHAN-sw1xb 6 років тому

    Sir plz hindi mai bhi bano

  • @luckysingla5017
    @luckysingla5017 5 років тому

    dp[mask][pos] will aplway be -1, we are not updating it anywhere.

  • @karthikreddy8221
    @karthikreddy8221 2 роки тому

    can any send me code pls pls