Shortest path not returning correct solution












0















Been trying to implement Dijkstras algorithm in a user defined graph. But it keeps giving incorrect solutions. Anyway you guys can take a look and help me out?



I have been trying to use this graph as my test graph where A is the start Node and G is the end node. It should return the path A,C,D,F,G, but it actually returns A,B,D,E,G. For some reason it skips out the C...



 def ShortestPath(self, Source, Dest):
Distances = {}
Previous = {}

for EachNode in self.NodeList.keys():
Distances[EachNode] = -1
Previous[EachNode] = ""

Distances[Source] = 0
UnseenNodes = self.NodeList.keys()
while len(UnseenNodes) > 0:
ShortestDistance = None
Node = ""
for TempNode in UnseenNodes:
if ShortestDistance == None:
ShortestDistance = Distances[TempNode]
Node = TempNode
elif Distances[TempNode] < ShortestDistance:
ShortestDistance = Distances[TempNode]
Node = TempNode
UnseenNodes.remove(Node)

for Neighbour, NeighbourValue in self.NodeList[Node].Connections.items():
NeighbourID = Neighbour.GetID()
print NeighbourID
if Distances[NeighbourID] < Distances[Node] + NeighbourValue:
Distances[NeighbourID] = Distances[Node] + NeighbourValue
Previous[NeighbourID] = Node

print Previous
Path =
Node = Dest
while not (Node == Source):
if Path.count(Node) == 0:
Path.insert(0,Node)
Node = Previous[Node]
else:
break
Path.insert(0,Source)
print Path









share|improve this question

























  • Btw have you tried to debug it? For example Pycharm has nice python debugger, can put breakpoints to each of your steps and investigate more.

    – pagep
    Nov 26 '16 at 12:37
















0















Been trying to implement Dijkstras algorithm in a user defined graph. But it keeps giving incorrect solutions. Anyway you guys can take a look and help me out?



I have been trying to use this graph as my test graph where A is the start Node and G is the end node. It should return the path A,C,D,F,G, but it actually returns A,B,D,E,G. For some reason it skips out the C...



 def ShortestPath(self, Source, Dest):
Distances = {}
Previous = {}

for EachNode in self.NodeList.keys():
Distances[EachNode] = -1
Previous[EachNode] = ""

Distances[Source] = 0
UnseenNodes = self.NodeList.keys()
while len(UnseenNodes) > 0:
ShortestDistance = None
Node = ""
for TempNode in UnseenNodes:
if ShortestDistance == None:
ShortestDistance = Distances[TempNode]
Node = TempNode
elif Distances[TempNode] < ShortestDistance:
ShortestDistance = Distances[TempNode]
Node = TempNode
UnseenNodes.remove(Node)

for Neighbour, NeighbourValue in self.NodeList[Node].Connections.items():
NeighbourID = Neighbour.GetID()
print NeighbourID
if Distances[NeighbourID] < Distances[Node] + NeighbourValue:
Distances[NeighbourID] = Distances[Node] + NeighbourValue
Previous[NeighbourID] = Node

print Previous
Path =
Node = Dest
while not (Node == Source):
if Path.count(Node) == 0:
Path.insert(0,Node)
Node = Previous[Node]
else:
break
Path.insert(0,Source)
print Path









share|improve this question

























  • Btw have you tried to debug it? For example Pycharm has nice python debugger, can put breakpoints to each of your steps and investigate more.

    – pagep
    Nov 26 '16 at 12:37














0












0








0








Been trying to implement Dijkstras algorithm in a user defined graph. But it keeps giving incorrect solutions. Anyway you guys can take a look and help me out?



I have been trying to use this graph as my test graph where A is the start Node and G is the end node. It should return the path A,C,D,F,G, but it actually returns A,B,D,E,G. For some reason it skips out the C...



 def ShortestPath(self, Source, Dest):
Distances = {}
Previous = {}

for EachNode in self.NodeList.keys():
Distances[EachNode] = -1
Previous[EachNode] = ""

Distances[Source] = 0
UnseenNodes = self.NodeList.keys()
while len(UnseenNodes) > 0:
ShortestDistance = None
Node = ""
for TempNode in UnseenNodes:
if ShortestDistance == None:
ShortestDistance = Distances[TempNode]
Node = TempNode
elif Distances[TempNode] < ShortestDistance:
ShortestDistance = Distances[TempNode]
Node = TempNode
UnseenNodes.remove(Node)

for Neighbour, NeighbourValue in self.NodeList[Node].Connections.items():
NeighbourID = Neighbour.GetID()
print NeighbourID
if Distances[NeighbourID] < Distances[Node] + NeighbourValue:
Distances[NeighbourID] = Distances[Node] + NeighbourValue
Previous[NeighbourID] = Node

print Previous
Path =
Node = Dest
while not (Node == Source):
if Path.count(Node) == 0:
Path.insert(0,Node)
Node = Previous[Node]
else:
break
Path.insert(0,Source)
print Path









share|improve this question
















Been trying to implement Dijkstras algorithm in a user defined graph. But it keeps giving incorrect solutions. Anyway you guys can take a look and help me out?



I have been trying to use this graph as my test graph where A is the start Node and G is the end node. It should return the path A,C,D,F,G, but it actually returns A,B,D,E,G. For some reason it skips out the C...



 def ShortestPath(self, Source, Dest):
Distances = {}
Previous = {}

for EachNode in self.NodeList.keys():
Distances[EachNode] = -1
Previous[EachNode] = ""

Distances[Source] = 0
UnseenNodes = self.NodeList.keys()
while len(UnseenNodes) > 0:
ShortestDistance = None
Node = ""
for TempNode in UnseenNodes:
if ShortestDistance == None:
ShortestDistance = Distances[TempNode]
Node = TempNode
elif Distances[TempNode] < ShortestDistance:
ShortestDistance = Distances[TempNode]
Node = TempNode
UnseenNodes.remove(Node)

for Neighbour, NeighbourValue in self.NodeList[Node].Connections.items():
NeighbourID = Neighbour.GetID()
print NeighbourID
if Distances[NeighbourID] < Distances[Node] + NeighbourValue:
Distances[NeighbourID] = Distances[Node] + NeighbourValue
Previous[NeighbourID] = Node

print Previous
Path =
Node = Dest
while not (Node == Source):
if Path.count(Node) == 0:
Path.insert(0,Node)
Node = Previous[Node]
else:
break
Path.insert(0,Source)
print Path






python python-2.7 dijkstra shortest-path






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 3 at 5:29









Cœur

19k9114155




19k9114155










asked Nov 26 '16 at 12:29









Thomas O'keeffeThomas O'keeffe

135




135













  • Btw have you tried to debug it? For example Pycharm has nice python debugger, can put breakpoints to each of your steps and investigate more.

    – pagep
    Nov 26 '16 at 12:37



















  • Btw have you tried to debug it? For example Pycharm has nice python debugger, can put breakpoints to each of your steps and investigate more.

    – pagep
    Nov 26 '16 at 12:37

















Btw have you tried to debug it? For example Pycharm has nice python debugger, can put breakpoints to each of your steps and investigate more.

– pagep
Nov 26 '16 at 12:37





Btw have you tried to debug it? For example Pycharm has nice python debugger, can put breakpoints to each of your steps and investigate more.

– pagep
Nov 26 '16 at 12:37












1 Answer
1






active

oldest

votes


















1














Your problem is in this line:



if Distances[NeighbourID] < Distances[Node] + NeighbourValue:


Change the less-than sign to a greater-than sign, as you only want to replace the Neighbor's distance with a smaller one. Furthermore, you must also make sure that you treat Distance[i] == -1 as a separate case whenever you're trying to find the minimum distance.



The fixed code:



 def ShortestPath(self, Source, Dest):
Distances = {}
Previous = {}

for EachNode in self.NodeList.keys():
Distances[EachNode] = -1
Previous[EachNode] = ""

Distances[Source] = 0
UnseenNodes = self.NodeList.keys()
while len(UnseenNodes) > 0:
ShortestDistance = None
Node = ""
for TempNode in UnseenNodes:
if Distances[TempNode] == -1: continue
if ShortestDistance == None:
ShortestDistance = Distances[TempNode]
Node = TempNode
elif Distances[TempNode] < ShortestDistance:
ShortestDistance = Distances[TempNode]
Node = TempNode

if ShortestDistance is None: break
UnseenNodes.remove(Node)

for Neighbour, NeighbourValue in self.NodeList[Node].Connections.items():
NeighbourID = Neighbour.GetID()
print NeighbourID
if Distances[NeighbourID] == -1 or Distances[NeighbourID] > Distances[Node] + NeighbourValue:
Distances[NeighbourID] = Distances[Node] + NeighbourValue
Previous[NeighbourID] = Node

print Previous
Path =
Node = Dest
while not (Node == Source):
if Path.count(Node) == 0:
Path.insert(0,Node)
Node = Previous[Node]
else:
break
Path.insert(0,Source)
print Path





share|improve this answer
























  • Yeah, that's working like a treat now. I didn't know about the continue statement you have used as well ! Thanks!

    – Thomas O'keeffe
    Nov 28 '16 at 23:03











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f40818331%2fshortest-path-not-returning-correct-solution%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Your problem is in this line:



if Distances[NeighbourID] < Distances[Node] + NeighbourValue:


Change the less-than sign to a greater-than sign, as you only want to replace the Neighbor's distance with a smaller one. Furthermore, you must also make sure that you treat Distance[i] == -1 as a separate case whenever you're trying to find the minimum distance.



The fixed code:



 def ShortestPath(self, Source, Dest):
Distances = {}
Previous = {}

for EachNode in self.NodeList.keys():
Distances[EachNode] = -1
Previous[EachNode] = ""

Distances[Source] = 0
UnseenNodes = self.NodeList.keys()
while len(UnseenNodes) > 0:
ShortestDistance = None
Node = ""
for TempNode in UnseenNodes:
if Distances[TempNode] == -1: continue
if ShortestDistance == None:
ShortestDistance = Distances[TempNode]
Node = TempNode
elif Distances[TempNode] < ShortestDistance:
ShortestDistance = Distances[TempNode]
Node = TempNode

if ShortestDistance is None: break
UnseenNodes.remove(Node)

for Neighbour, NeighbourValue in self.NodeList[Node].Connections.items():
NeighbourID = Neighbour.GetID()
print NeighbourID
if Distances[NeighbourID] == -1 or Distances[NeighbourID] > Distances[Node] + NeighbourValue:
Distances[NeighbourID] = Distances[Node] + NeighbourValue
Previous[NeighbourID] = Node

print Previous
Path =
Node = Dest
while not (Node == Source):
if Path.count(Node) == 0:
Path.insert(0,Node)
Node = Previous[Node]
else:
break
Path.insert(0,Source)
print Path





share|improve this answer
























  • Yeah, that's working like a treat now. I didn't know about the continue statement you have used as well ! Thanks!

    – Thomas O'keeffe
    Nov 28 '16 at 23:03
















1














Your problem is in this line:



if Distances[NeighbourID] < Distances[Node] + NeighbourValue:


Change the less-than sign to a greater-than sign, as you only want to replace the Neighbor's distance with a smaller one. Furthermore, you must also make sure that you treat Distance[i] == -1 as a separate case whenever you're trying to find the minimum distance.



The fixed code:



 def ShortestPath(self, Source, Dest):
Distances = {}
Previous = {}

for EachNode in self.NodeList.keys():
Distances[EachNode] = -1
Previous[EachNode] = ""

Distances[Source] = 0
UnseenNodes = self.NodeList.keys()
while len(UnseenNodes) > 0:
ShortestDistance = None
Node = ""
for TempNode in UnseenNodes:
if Distances[TempNode] == -1: continue
if ShortestDistance == None:
ShortestDistance = Distances[TempNode]
Node = TempNode
elif Distances[TempNode] < ShortestDistance:
ShortestDistance = Distances[TempNode]
Node = TempNode

if ShortestDistance is None: break
UnseenNodes.remove(Node)

for Neighbour, NeighbourValue in self.NodeList[Node].Connections.items():
NeighbourID = Neighbour.GetID()
print NeighbourID
if Distances[NeighbourID] == -1 or Distances[NeighbourID] > Distances[Node] + NeighbourValue:
Distances[NeighbourID] = Distances[Node] + NeighbourValue
Previous[NeighbourID] = Node

print Previous
Path =
Node = Dest
while not (Node == Source):
if Path.count(Node) == 0:
Path.insert(0,Node)
Node = Previous[Node]
else:
break
Path.insert(0,Source)
print Path





share|improve this answer
























  • Yeah, that's working like a treat now. I didn't know about the continue statement you have used as well ! Thanks!

    – Thomas O'keeffe
    Nov 28 '16 at 23:03














1












1








1







Your problem is in this line:



if Distances[NeighbourID] < Distances[Node] + NeighbourValue:


Change the less-than sign to a greater-than sign, as you only want to replace the Neighbor's distance with a smaller one. Furthermore, you must also make sure that you treat Distance[i] == -1 as a separate case whenever you're trying to find the minimum distance.



The fixed code:



 def ShortestPath(self, Source, Dest):
Distances = {}
Previous = {}

for EachNode in self.NodeList.keys():
Distances[EachNode] = -1
Previous[EachNode] = ""

Distances[Source] = 0
UnseenNodes = self.NodeList.keys()
while len(UnseenNodes) > 0:
ShortestDistance = None
Node = ""
for TempNode in UnseenNodes:
if Distances[TempNode] == -1: continue
if ShortestDistance == None:
ShortestDistance = Distances[TempNode]
Node = TempNode
elif Distances[TempNode] < ShortestDistance:
ShortestDistance = Distances[TempNode]
Node = TempNode

if ShortestDistance is None: break
UnseenNodes.remove(Node)

for Neighbour, NeighbourValue in self.NodeList[Node].Connections.items():
NeighbourID = Neighbour.GetID()
print NeighbourID
if Distances[NeighbourID] == -1 or Distances[NeighbourID] > Distances[Node] + NeighbourValue:
Distances[NeighbourID] = Distances[Node] + NeighbourValue
Previous[NeighbourID] = Node

print Previous
Path =
Node = Dest
while not (Node == Source):
if Path.count(Node) == 0:
Path.insert(0,Node)
Node = Previous[Node]
else:
break
Path.insert(0,Source)
print Path





share|improve this answer













Your problem is in this line:



if Distances[NeighbourID] < Distances[Node] + NeighbourValue:


Change the less-than sign to a greater-than sign, as you only want to replace the Neighbor's distance with a smaller one. Furthermore, you must also make sure that you treat Distance[i] == -1 as a separate case whenever you're trying to find the minimum distance.



The fixed code:



 def ShortestPath(self, Source, Dest):
Distances = {}
Previous = {}

for EachNode in self.NodeList.keys():
Distances[EachNode] = -1
Previous[EachNode] = ""

Distances[Source] = 0
UnseenNodes = self.NodeList.keys()
while len(UnseenNodes) > 0:
ShortestDistance = None
Node = ""
for TempNode in UnseenNodes:
if Distances[TempNode] == -1: continue
if ShortestDistance == None:
ShortestDistance = Distances[TempNode]
Node = TempNode
elif Distances[TempNode] < ShortestDistance:
ShortestDistance = Distances[TempNode]
Node = TempNode

if ShortestDistance is None: break
UnseenNodes.remove(Node)

for Neighbour, NeighbourValue in self.NodeList[Node].Connections.items():
NeighbourID = Neighbour.GetID()
print NeighbourID
if Distances[NeighbourID] == -1 or Distances[NeighbourID] > Distances[Node] + NeighbourValue:
Distances[NeighbourID] = Distances[Node] + NeighbourValue
Previous[NeighbourID] = Node

print Previous
Path =
Node = Dest
while not (Node == Source):
if Path.count(Node) == 0:
Path.insert(0,Node)
Node = Previous[Node]
else:
break
Path.insert(0,Source)
print Path






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 26 '16 at 18:50









Eric ZhangEric Zhang

371310




371310













  • Yeah, that's working like a treat now. I didn't know about the continue statement you have used as well ! Thanks!

    – Thomas O'keeffe
    Nov 28 '16 at 23:03



















  • Yeah, that's working like a treat now. I didn't know about the continue statement you have used as well ! Thanks!

    – Thomas O'keeffe
    Nov 28 '16 at 23:03

















Yeah, that's working like a treat now. I didn't know about the continue statement you have used as well ! Thanks!

– Thomas O'keeffe
Nov 28 '16 at 23:03





Yeah, that's working like a treat now. I didn't know about the continue statement you have used as well ! Thanks!

– Thomas O'keeffe
Nov 28 '16 at 23:03




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f40818331%2fshortest-path-not-returning-correct-solution%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas