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.