Traveling Salesman Problem (TSP) Implementation in Python

Поділитися
Вставка
  • Опубліковано 18 січ 2021
  • Hey Guys,
    In this video will learn how to solve the traveling salesman problem with the help of Python programming language.
    The traveling salesman problem asks the following question: "Given a list of cities and the distances between each pair of cities, what is the shortest possible route that visits each city exactly once and returns to the origin city?
    Source Code: github.com/pythonpool/python-...
    **************************************************************
    Related Questions:
    what is the traveling salesman problem, how to solve traveling salesman problem, what is traveling salesman problem, how to solve the traveling salesman problem, traveling salesman problem where the cost depends on the order visited, how will you write a monte carlo type of solver for traveling salesman problem?, what did people discover while working on the traveling salesman problem, traveling salesman problem how to solve, now, consider a traveling salesman problem in which the salesman starts at city 0 a, for the traveling salesman problem applied to seven cities, how many distinct tours are possible?
    Tags:
    #TravelingSalesmanProblem #TSP #TSPPython
    ***************************************************************
    Like👍 Share✔Comment👇🏻Subscribe❤❤❤❤
    Visit OUR website
    www.pythonpool.com
    Do Let us know if you have any problems in the comment section below.
    ==========Thanks for Watching =========
    See you with the next Video.

КОМЕНТАРІ • 25

  • @phillipbarbour4669
    @phillipbarbour4669 2 роки тому +8

    This appears to be a brute force algorithm since all permutations are evaluated for cost and the minimum of the lowest-so-far cost and current cost is saved after each permutation. Thank you for sharing.

  • @vasudevanrao8078
    @vasudevanrao8078 Рік тому +3

    Very well explained and indeed this can be achieved in two ways either on Dynamic prog or through Brute force algorithm (first evaluate all possible tours and afterwards choose the one for which we get the shortest distance). I ran this prog in Spyder (5.3.3) but the output for me 95 instead of 80. I am still figuring it out why the output is 95. Thnaks for sharing this code. Cheers

  • @namburukrishnakumari6765
    @namburukrishnakumari6765 Рік тому +1

    Helped me..thank you☺️

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

    sir which algorithm is been used ?

  • @theresa5878
    @theresa5878 Рік тому +1

    Can somebody tell me how to print the path you take through the nods ?

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

    which algorithm did you use?

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

    Thanks

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

    Would this work, if any node has same distance to the any other two nodes?

  • @art3misknight374
    @art3misknight374 3 роки тому +5

    what if its start from node 2 or 3 or 4??

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

      It is not important is TSP because we can say that 1-5-3-2-4-1 is equal to the 2-4-1-5-3-2. Tsp creates a circle.

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

    how can we print the path

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

    You lost me when started defining the second function, could someone explain this part a bit? what is this function actually doing?

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

    Can You use heurístic for tsp?

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

    Is there any way to print the path which gives the min cost?

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

      TRY THIS CODE!!!
      from sys import maxsize
      from itertools import permutations
      V = 4
      # implementation of traveling Salesman Problem
      def travellingSalesmanProblem(graph, s):
      # store all vertex apart from source vertex
      vertex = []
      for i in range(V):
      if i != s:
      vertex.append(i)
      # store minimum weight Hamiltonian Cycle
      min_path = maxsize
      a = []
      next_permutation = list(permutations(vertex))
      for i in next_permutation:
      # store current Path weight(cost)
      current_pathweight = 0
      # compute current path weight
      k = s
      for j in i:
      current_pathweight += graph[k][j]
      k = j
      current_pathweight += graph[k][s]
      # update minimum
      a.append(current_pathweight)
      min_path = min(min_path, current_pathweight)
      for i in range(len(a)):
      if min_path == a[i]:
      l = list(next_permutation[i])
      l.insert(0, s)
      l.append(s)
      print(l)
      return min_path
      # Driver Code
      if _name_ == "__main__":
      # matrix representation of graph
      graph = [[0, 10, 15, 20],
      [10, 0, 35, 25],
      [15, 35, 0, 30],
      [20, 25, 30, 0]]
      s = 0
      print(travellingSalesmanProblem(graph, s))

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

    Can u tell how can we print the path also

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

      TRY THIS CODE!!!
      from sys import maxsize
      from itertools import permutations
      V = 4
      # implementation of traveling Salesman Problem
      def travellingSalesmanProblem(graph, s):
      # store all vertex apart from source vertex
      vertex = []
      for i in range(V):
      if i != s:
      vertex.append(i)
      # store minimum weight Hamiltonian Cycle
      min_path = maxsize
      a = []
      next_permutation = list(permutations(vertex))
      for i in next_permutation:
      # store current Path weight(cost)
      current_pathweight = 0
      # compute current path weight
      k = s
      for j in i:
      current_pathweight += graph[k][j]
      k = j
      current_pathweight += graph[k][s]
      # update minimum
      a.append(current_pathweight)
      min_path = min(min_path, current_pathweight)
      for i in range(len(a)):
      if min_path == a[i]:
      l = list(next_permutation[i])
      l.insert(0, s)
      l.append(s)
      print(l)
      return min_path
      # Driver Code
      if __name__ == "__main__":
      # matrix representation of graph
      graph = [[0, 10, 15, 20],
      [10, 0, 35, 25],
      [15, 35, 0, 30],
      [20, 25, 30, 0]]
      s = 0
      print(travellingSalesmanProblem(graph, s))

  • @vatanmai3690
    @vatanmai3690 Рік тому +1

    Thanks for the code:) u need to really work on ur explanation. First, explain the logic and then the code.

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

    Second function is wrongly implemented instead of comparing vertices index u should have used cost from graph matrix. How can you say by comparing the vertex numbers at it would be next min path without considering the cost comparison in next min path

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

      Your code also does not check if there is valid edge present in between nodes in second function what if the 1-2 is 0

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

      Please can you demo running against these cases

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

    You should first explain your method before explaining it in code.. That's how followup become easy. BTW, thanks for the code

  • @maxfrischdev
    @maxfrischdev 8 місяців тому

    I know this might sound.. wrong.. but believe me, I know that learning a totally different language than your own is DIFFICULT, I am learning Indonesian.. But, why do Indians speaking english constantly repeat themself, saying the same 2 or 3 times in a slightly different way? 😅

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

    Hindi mai hie pdha deta jada aacha se smjh aata