Coding Challenge #35.3: Traveling Salesperson with Lexicographic Order

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

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

  • @MuhammadTajammulZia
    @MuhammadTajammulZia 3 ปีที่แล้ว +8

    Hey! Thank you for making such amazing videos with so much explanation. I love every one of them.
    At 14:30, I think it wasn't just the last one. The distance will be same for 0,1,2,3 and 3,2,1,0 so if you had encountered 0,1,2,3 before and 3,2,1,0 at the end, it would seem as if it was the last one that was the best but actually there were two best.

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

    Hey Daniel, these are some great videos. I can't wait to see how the genetic algorithm solves this problem.

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

    You could optimize the code to be twice as fast by ignoring symmetric permutations of the array. IE 0,1,2 is the same as 2,1,0 so dont bother checking 2,1,0

  • @JinTsen
    @JinTsen 7 ปีที่แล้ว +28

    I'm curious, don't you kinda check everyone twice? Like, [0, 1, 2, 3] = [3, 2, 1, 0]. So one could half the computing time by excluding that, right?

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

      In this case, yes, because we have a non oriented graph. In the general case of a digraph, going from city 1 to city 2 doesn't need to have the same "cost" as going from 2 to 1

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

      Checking whether something needs to be excluded would probably be pretty complicated, so I don’t think you’d save much if any time overall.

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

      @@KnakuanaRka Nah. Just cut it from middle as it is arranged in an alphabetical order.

    • @KnakuanaRka
      @KnakuanaRka 5 ปีที่แล้ว

      @@abhishekjmathew5732 D'oh! *facepalm*

  • @Nerdthagoras
    @Nerdthagoras 7 ปีที่แล้ว +27

    17:20 OOOOPS!!!

    • @alexanderfl-ts3171
      @alexanderfl-ts3171 3 ปีที่แล้ว +1

      Copper nitro telluride

    • @kevnar
      @kevnar 3 ปีที่แล้ว

      C U Next Tuesday!

  • @quadgen8049
    @quadgen8049 6 ปีที่แล้ว

    Hello Everyone, i was doing this in processing and i wrote , if(dis

  • @bighugejake
    @bighugejake 7 ปีที่แล้ว +19

    I would love to see the genetic algorithm implementation of this. Going through every single possibility seems hugely inefficient.

    • @TheCodingTrain
      @TheCodingTrain  7 ปีที่แล้ว +28

      Yes, I plan to get to this soon!

    • @MarcusAseth
      @MarcusAseth 7 ปีที่แล้ว

      I would like to know more about this Gene Ethic algorithm too

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

      +1 to this request! I’ve been working on differential growth algorithms (inspired by inconvergent.net/shepherding-random-growth/) and keep thinking to myself "If only Shiffman had a k-d tree video."
      (p.s. thank you for all these videos-it’s a huge inspiration!)

    • @gonengazit
      @gonengazit 7 ปีที่แล้ว

      it's coming out soon

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

      I know I'm about 4 years too late but Sebastian Lague has a really good video on that concept!

  • @greyfox492
    @greyfox492 7 ปีที่แล้ว

    you deserve more views, man
    very cool vids, I can't stop watching even though I learn java, not js
    greetings from Ukraine

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

    Hello!
    Love the videos, you explain everything so well!! Just adding to the voices asking for the #4 :D

    • @TheCodingTrain
      @TheCodingTrain  7 ปีที่แล้ว

      I think I'm going to do it tomorrow!

  • @karanjeswani306
    @karanjeswani306 6 ปีที่แล้ว

    About the lexicographic order TSP algorithm that you showed.
    If you only check total_permutations / 2 times, you can get the solution in half the time. Because what matters here is that we get the minimum distance route.
    Example: for 3 cities, the distance for order [0, 1, 2] would be same as [2, 1, 0]. So why check both?
    If you have any further query regarding my premise, draw these two routes and check for yourself.

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

    Loved this video series.
    Are we gonna get the Genetic Algorithm (part 4)??
    Hope you will release or make this soon.

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

      Eventually yes! Will try to get to it soon.

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

    Hi, Dan, this is a really incredible series! I've already used hello.processing to introduce students and colleagues to the platform, and these are a really fun, accessible extension to show them once their interest is piqued.
    A question: Would it be a huge pain in the kiester to dump these coding challenge videos into your Vimeo account, too? I'm a public high school art teacher (as are a lot of my colleagues who I evangelize to about including creative code in their curricula), and public schools have a nasty habit of blanket-blocking TH-cam, while other streaming services like Vimeo tend to be permitted.
    If that would be a huge timesink for you, I totally get it, though (and I can just keep ripping MP4s to show in my classroom...).

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

      I can share a google drive folder with you which might be easier. I want to do this, but don't really have the time to maintain both platforms and also worry about splitting the audience. I need to think about this more, can you contact me via e-mail or twitter?

  • @oanna.m3125
    @oanna.m3125 7 ปีที่แล้ว +8

    Hello! Thanks lot for all the amazing videos! But I can't find the video with the implementation of a genetic lgorithm in this example. Is it somewhere?

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

    Hi Dan, you are amazing Teacher and keep up the good work

  • @loading.........
    @loading......... 7 ปีที่แล้ว +1

    for(int i=0 ; i < totalCity ; i++){
    g.drawOval(city[i].x , city[i].y , city[i].width , city[i].height);
    n = order[i];
    if(n < totalCity-1){
    g.drawLine(city[n].x , city[n].y , city[n+1].x , city[n+1].y);
    }
    }
    this is java code..
    here "order array" is changing correctly but path between cities are NOT updating..it was working before Lexicographic Order..
    i am not changing "city array" anywere.
    plz help

  • @FrancoAguileradonillan
    @FrancoAguileradonillan 4 ปีที่แล้ว

    Would it be more efficient if you search for the most separated point of the group as a starting point, and from there you search the closest point ?

  • @Jimanator
    @Jimanator 8 ปีที่แล้ว

    you could probably use some of the code from the poisson disc sampling video to make this quicker.

  • @sergiom.p.s.junior8146
    @sergiom.p.s.junior8146 4 ปีที่แล้ว

    Have you ever wanted to try another approach to this problem? I know you made the GA solution, but there is a set of really good solvers, such as ALNS, that are known to solve this problem in polynomial time. I would love to see you implementing LKH or something like that in your challenges. Sorry to bother here, i don't know if you got a official local to talk about this.

  • @jiaojiaogao2582
    @jiaojiaogao2582 5 ปีที่แล้ว

    It's really an amazing explanation, but I am wondering why the result was not 100% after the loop stopped.

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

    How does Google Maps do it so quickly then (i.e. calculate the best or shortest route between several points on a map)?

    • @gmbueno
      @gmbueno 6 ปีที่แล้ว +3

      There are much better algorithms. The one presented in this video is all about brute force (testing all possibilities). It is slow because for N entries, we have to check N! (N factorial) possibilities. Look up for Dijkstra's shortest path algorithm. With this one, we have to check N^2 (N squared) possibilities, much better...
      For 10 cities, for example: Brute Force = 3.628.800 checsk, Dijkstra = 100 checks. The difference grows with the growth of N.
      Today there is a better implementation of Dijkstra that runs in (N*logN or M, M beeing the number of edges)...

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

      @@gmbueno Dijkstra can't solve this problem, it only gives you the best path between 2 cities. The TSP requires the optimal route to travel all the cities. For bigger cases , ie: google maps, it's used heuristics that can approximate the result from the optimal solution. Currently it's possible to calculate the best route of milions of cities with < 3% difference from the optimal solution in a reasonable time.

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

    I didn't like the video before it auto played so I had to come back.

  • @nerdnexus4756
    @nerdnexus4756 8 ปีที่แล้ว

    great series. keep it (Y)

  • @E_rich
    @E_rich 3 ปีที่แล้ว

    You're a mad man

  • @ThePizzabrothersGaming
    @ThePizzabrothersGaming 6 ปีที่แล้ว

    factorial 0 is also 1 btw :P with your current code if it somehow reached 0 it would loop forever.

  • @E_rich
    @E_rich 3 ปีที่แล้ว

    Dude I almost just started crying this is so cool

  • @stevenb121212
    @stevenb121212 7 ปีที่แล้ว

    I believe part of the travelling salesman is the salesman has to return to his home city and so completes a loop. Any ideas on including this in the next one as well?

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

    Does somebody has its back to start version?

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

    It would probably run faster if it was calculated in a function outside the draw loop, and only displayed how far it got, every 0.02 seconds (50 frames).😀

  • @raayaansahu2525
    @raayaansahu2525 3 ปีที่แล้ว

    hey @ 9:55 how do i solve this when the number of cities is arbitrary?

  • @Jkauppa
    @Jkauppa 3 ปีที่แล้ว

    try to determine what is the deviance from the assumed optimal compared to a quickly chosen average round trip

    • @Jkauppa
      @Jkauppa 3 ปีที่แล้ว

      benefit of the optimization

    • @Jkauppa
      @Jkauppa 3 ปีที่แล้ว

      a spiral path should be quite short compared to the average random

    • @Jkauppa
      @Jkauppa 3 ปีที่แล้ว

      non-overlapping path

    • @Jkauppa
      @Jkauppa 3 ปีที่แล้ว

      floating point

  • @BuildEver
    @BuildEver 8 ปีที่แล้ว

    Wouldn't this program be quicker if you do this?
    1. Start at chosen city.
    2. Look the distance between this city and all others.
    3. Chosse city with shortest distance and draw path.
    4. Exclude star city from looking pool.
    5. Set city from point 3 as start city, go to point 2.

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

      Imagine if the cities were spread out like the letter L, and the starting point was in the middle of the horizontal line, but the distance to the right was a bit larger than the distance to the left city. It would then start going left, continue upwards (vertical line), and then jump from the top of the L to the bottom (right next to the starting point), and then take the last few. The jump will be like the hypotenuse in a triangle, and therefore make this alt potentially longer then first going right, then jump back to start, and then take the rest. But if processing power costs more then the potentially longer distance, then I would go with your solution!

    • @quillaja
      @quillaja 6 ปีที่แล้ว

      "For example, a greedy strategy for the traveling salesman problem (which is of a high computational complexity) is the following heuristic: "At each stage visit an unvisited city nearest to the current city". This heuristic need not find a best solution, but terminates in a reasonable number of steps; finding an optimal solution typically requires unreasonably many steps." --wikipedia (Greedy_algorithm)

  • @Lellba47
    @Lellba47 5 ปีที่แล้ว

    You could check half of them if you take into account that ABC and CBA are the same thing for this program :p

  • @Mavhawk64
    @Mavhawk64 4 ปีที่แล้ว

    What are the specs on your computer?

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

    Hear about the blonde travelling salesman who couldn't figure out the TSP between 2 cities?
    Ba-dum-tss...

    • @KnakuanaRka
      @KnakuanaRka 4 ปีที่แล้ว

      kevnar Is the “ba dum tss” the punchline? Because I don’t understand it at all.

    • @kmcvideos1
      @kmcvideos1 4 ปีที่แล้ว

      @@KnakuanaRka lol no people say that at the end of the joke. Finding the TSP between 2 cities is just a straight line which is very easy so its a blonde joke

    • @KnakuanaRka
      @KnakuanaRka 4 ปีที่แล้ว

      Christopher Barber D’oh!

  • @FirSouL3
    @FirSouL3 8 ปีที่แล้ว

    Any chances for a minesweeper? :)

    • @TheCodingTrain
      @TheCodingTrain  8 ปีที่แล้ว

      Great idea, suggest here: github.com/CodingRainbow/Rainbow-Topics/issues

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

      I made Minesweeper, you can check it out and try to see how it works at pitt.edu/~ksa18/minesweeper/

  • @shimlaDnB
    @shimlaDnB 8 ปีที่แล้ว

    love these series, what could be interesting, if you'd might run out of idea's for these series or something; is lumance based effects on pixel inputs, like video. delaunay triangulation for example or Voronoi tessellation like this: th-cam.com/video/mTzbax2daKs/w-d-xo.html

  • @TheFireBrozTFB
    @TheFireBrozTFB 7 ปีที่แล้ว

    Here's a simpler way to find the path: nest loop all the cities, like for each city, loop all of the cities that are not connected to it, find shortest distance, connect. Perform for next city

    • @quillaja
      @quillaja 6 ปีที่แล้ว

      That "greedy algorithm" won't necessarily find the shortest path, though it's likely to produce a better solution than a random path.

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

    Wait, couldn't he just use Djikstra's algorithm for this?

    • @joni.r3947
      @joni.r3947 7 ปีที่แล้ว +5

      No because we have to pass through all the cities and no just find the shortest path.

  • @johndaviddeatherage2232
    @johndaviddeatherage2232 7 ปีที่แล้ว

    where is the TSP problem done with a Genetic Algorithm?

  • @joshstafford3444
    @joshstafford3444 7 ปีที่แล้ว +8

    17:20 cheeky dig

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

    this is really inefficient. you are checking every permutation twice because 0123 is the same as 3210 and 2310 is the same as 0132 etc.

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

      No they are not, the distance between each city in each case is different

    • @ThePizzabrothersGaming
      @ThePizzabrothersGaming 6 ปีที่แล้ว

      they are literally the reverse path, but honestly setting up a check for reverses would not be worth it

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

      @@ThePizzabrothersGaming The reversed paths are just the last half of the paths checked. Totally worth just stopping half way.