0

Not a technical question, just asking for a point in the right direction for my research.

Are there any models that address the following problem:

Finding the route starting from X that passes through A, B, C in the most efficient order. In the case below, X,A,C,B is the optimum path (12).

Thanks

  • Offtopic. Not a programming question. This is CS theory, so try http://cs.stackexchange.com – Marc B Oct 18 '16 at 18:15
  • 2
    [Traveling Salesman Problem](https://en.wikipedia.org/wiki/Travelling_salesman_problem) – amit Oct 18 '16 at 18:18
  • interesting. Pretty close to what i'm looking for except for that it returns to the origin city. – Will Donaghy Oct 18 '16 at 19:12
  • @WillDonaghy This does not change the complexity of the problem, and is still considered a variant of TSP. The solution is pretty much the same to this variant. – amit Oct 18 '16 at 21:05

2 Answers2

0

you may want to look at traveling salesman. There are a lot of resources for how to implement this as it is a very common programming problem. https://en.wikipedia.org/wiki/Travelling_salesman_problem

Jeff
  • 407
  • 4
  • 17
0

This is an implementation of Dijkstra's Algorithm in Python:

def find_all_paths(graph, start, end, path=[]):
    required=('A', 'B', 'C')
    path = path + [start]
    if start == end:
        return [path]
    if start not in graph:
        return []
    paths = []
    for node in graph[start]:
        if node not in path:
            newpaths = find_all_paths(graph, node, end, path)
            for newpath in newpaths:
                if all(e in newpath for e in required):
                    paths.append(newpath)
    return paths       

def min_path(graph, start, end):
    paths=find_all_paths(graph,start,end)
    mt=10**99
    mpath=[]
    print '\tAll paths:',paths
    for path in paths:
        t=sum(graph[i][j] for i,j in zip(path,path[1::]))
        print '\t\tevaluating:',path, t
        if t<mt: 
            mt=t
            mpath=path

    e1=' '.join('{}->{}:{}'.format(i,j,graph[i][j]) for i,j in zip(mpath,mpath[1::]))
    e2=str(sum(graph[i][j] for i,j in zip(mpath,mpath[1::])))
    print 'Best path: '+e1+'   Total: '+e2+'\n'  

if __name__ == "__main__":
    graph = {'X': {'A':5, 'B':8, 'C':10},
             'A': {'C':3, 'B':5},
             'C': {'A':3, 'B':4},
             'B': {'A':5, 'C':4}}
    min_path(graph,'X','B')

Prints:

    All paths: [['X', 'A', 'C', 'B'], ['X', 'C', 'A', 'B']]
        evaluating: ['X', 'A', 'C', 'B'] 12
        evaluating: ['X', 'C', 'A', 'B'] 18
Best path: X->A:5 A->C:3 C->B:4   Total: 12

The 'guts' is recursively finding all paths and filtering to only those paths that visit the required nodes ('A', 'B', 'C'). The paths are then summed up to find the minimum path expense.

There are certainly be more efficient approaches but it is hard to be simpler. You asked for a model, so here is a working implementation.

dawg
  • 98,345
  • 23
  • 131
  • 206
  • This (1) Does not answer the question (which does not ask how to implement it) (2) not efficient (there is significantly more efficient solution) (3) code without explanation answer, which has very little value imo. – amit Oct 18 '16 at 21:06
  • @amit: Thanks for the feedback. The OP asked for Dijkstra's path finding algorithm. This is Dijkstra's algorithm. He did not ask for *the most efficient*. -- just a model. I have added explanation, but the wikipedia entry on Dijkstra's algorithm is significantly better than what someone will find here. – dawg Oct 19 '16 at 00:04
  • this is not Dijkstra's algorithm, if it was it would not solve the problem which is TSP, not point to point shortest path. – amit Oct 19 '16 at 05:10