简体   繁体   中英

Modified shortest path algorithm - vertices have points

Right now I have an undirected graph. Each edge represents the distance between the vertices. Each vertex contains a number (lets call them points). I am trying to get the maximum number of points while using the minimum distance. I have a constraint on the maximum distance I can go, thus I do not need to reach every vertex. I can start at any vertex and end at any vertex (I do not need to go back to the origin).

Right now I'm thinking that it would be possible through dynamic programming but I'm not entirely sure how to set up the problem.

Any help on how to set it up/the right algorithm to use would be greatly appreciated!

I think a good approach could be to build a minimum spanning tree on the graph. After that you can pick a root node (the selection is an algorithm/heuristic of its own I guess) and traverse the tree. However this will probably not be the optimal solution as I think the problem is NP complete. Alternatively you can try to search for solution algorithms of the TSP (travelling salesman problem) and extract a path that gives the most points.

Using a recursive function , you can iterate through points, and execute the same function with each of the points the first point connects to, and so on.

pts = [[(1,8), (2,5), (3,6)...]...] //each sublist of points needs to be sorted by the second index in each value contained
paths = []
def Branch(history, distance):
  for index, dist in pts[history[-1]][1:tree_branches_per_iteration]: //make the shorter distances go first
    if not index in history: //check for repetition
      new_distance = distance + dist
      if new_distance > limit: //check for distance limit
        paths.append((history + [index], new_distance))
      else:
        Branch(history + [index], new_distance)
    else:
      paths.append((history, distance))

Branch([0], 0)

This function calls on itself, assembling chains of points in all the possible directions (and not going to the same point twice) and terminates when it reaches the same points twice, or reaches the distance limit.

The distances are generated by basically making a matrix of all the distances from each coord to every other.

This approach uses the greedy salesperson.

So after this experience, I found that you should look into the ant colony algorithm and apply it to the traveling salesman problem. You could alternatively use a modified greedy algorithm approach that starts at a random point and chooses the next location based on a random sided die based on the locations that give you the best points. You could then modify your algorithm further to store previous results and iterate to get better results.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM