I am searching a small graph by directed depth first search and in this test case it finds the most concise path (with fewest nodes) but not the shortest path by TotalDistance and OutdoorDistance. The map is: where the nodes are given a->b (weight1, weight2) weight1 = TotalDistance between nodes and weight2 = OutdoorDistance between nodes. The aim is to find the shortest path while staying below maxTotalDist and maxDistOutdoors constraints.
1->2 (5.0, 2.0)
3->5 (6.0, 3.0)
2->3 (20.0, 10.0)
2->4 (10.0, 5.0)
4->3 (2.0, 1.0)
4->5 (20.0, 10.0)
The test is:
def testSP4():
nodes = []
for name in range(1,6):
nodes.append(Node(str(name)))
g = WeightedDigraph()
for n in nodes:
g.addNode(n)
g.addEdge(WeightedEdge(nodes[0],nodes[1], 5.0, 2.0))
g.addEdge(WeightedEdge(nodes[2],nodes[4], 6.0, 3.0))
g.addEdge(WeightedEdge(nodes[1],nodes[2], 20.0, 10.0))
g.addEdge(WeightedEdge(nodes[1],nodes[3], 10.0, 5.0))
g.addEdge(WeightedEdge(nodes[3],nodes[2], 2.0, 1.0))
g.addEdge(WeightedEdge(nodes[3],nodes[4], 20.0, 10.0))
sp = directedDFS(g, '4', '5', 21, 11)
print sp
path = []
for node in sp:
path.append(Node(node))
print sumTotDistance(g, path), sumDistOutdoors(g, path)
And the search algorithm is essentially:
def DFSShortest(digraph, start, end, maxTotalDist, maxDistOutdoors, dist1 = 0, dist2 = 0, path = [], shortest = None):
#assumes digraph is a WeightedDigraph
#assumes start and end are nodes in digraph
path = path + [Node(start)]
if Node(start) == Node(end):
if dist1 <= maxTotalDist and dist2 <= maxDistOutdoors:
return path
for [node, (weight1, weight2)] in digraph.edges[Node(start)]:
if node not in path: #avoid cycles
if shortest == None or (sumTotDistance(digraph, path) < sumTotDistance(digraph, shortest) \
and sumDistOutdoors(digraph, path) < sumDistOutdoors(digraph, shortest)):
newPath = DFSShortest(digraph,node,end, maxTotalDist, maxDistOutdoors, dist1+weight1, dist2+weight2, path, shortest)
if newPath != None:
shortest = newPath
return shortest
The test testSP4() gives the path sp = ['4', '5'] with distances 20, 10 but the given shortest solution is the path ['4', '3', '5'] where the distances are 8, 4. The depth first search is based on a text book version but I've introduced the weights constraints conditions. I've tried various different arrangements of the conditions (such as changing < to >) but that makes other test cases incorrect. Can someone please help? Thanks in advance.