Jan-22-2019, 12:09 AM
(This post was last modified: Jan-22-2019, 12:09 AM by grandpapa10.)
Hey there,
I m trying to run a code for a basic Dijkstra s Algo Implementation...
Unfortunately it s to no avail…
Whenever I try to run my code on the jupyter notebook, I experience some trouble with the kernel.
It simply won't run and cell gets stuck in [*].
I d appreciate if somebody could take a quick look at my code:
I just tried to run it again after updating the 'Class Graph' statement…
I also tried running the "def shortpath" in another cell, but NameError pops up saying that the "shortpath" is not defined...
I m trying to run a code for a basic Dijkstra s Algo Implementation...
Unfortunately it s to no avail…
Whenever I try to run my code on the jupyter notebook, I experience some trouble with the kernel.
It simply won't run and cell gets stuck in [*].
I d appreciate if somebody could take a quick look at my code:
graph = { 'A': {'B': 10, 'D': 4, 'F': 10}, 'B': {'E': 5, 'J': 10, 'I': 17}, 'C': {'A': 4, 'D': 10, 'E': 16}, 'D': {'F': 12, 'G': 21}, 'E': {'G': 4}, 'F': {'H': 3}, 'G': {'J': 3}, 'H': {'G': 3, 'J':5}, 'I': {}, 'J': {'I': 8}, } Class Graph: def shortpath(graph, start, end): D = {} # final distances dict P = {} # Predecessor dict #Fill the dicts with default values for node in graph.keys(): D[node] = - 1 # Vertices are unreachable P[node] = "" # Vertices have no predecessors D[start] = 0 # The start vertex needs no move unseen_nodes = graph.keys() # All nodes are unseen while len(unseen_nodes) > 0: # Select the node with the lowest value in D (final distance) shortest = None node = '' for temp_node in unseen_nodes: if shortest == None: shortest = D[temp_node] node = temp_node elif D[temp_node] < shortest: shortest = D[temp_node] node = temp_node #remove the selected node from unseen_nodes unseen_nodes.remove(node) #for each child (connected vertex) of the current node for child_node, child_value in graph[node].items(): if D[child_node] < D[node] + child_value: D[child_node] = D[node] + child_value #To go to child_node, you have to go through node P[child_node] = node #Set a clean path path = [] # we begin from the end node = end #while we are not arrived at the beginning while not (node == start): if path.count(node) == 0: path.insert(0, node) #Insert the predecessor of the current node node = P[node] # The current node becomes its predecessor else: break path.insert(0, start)#Finally, insert the start vertex return path shortpath(graph, 'C', 'I')sorry, I ve got a little problem with the indentation…
I just tried to run it again after updating the 'Class Graph' statement…
I also tried running the "def shortpath" in another cell, but NameError pops up saying that the "shortpath" is not defined...