Qiang Super
Qiang Super

Reputation: 323

Self-defined Dijkstra's algorithm in Python

I can successfully ran the codes that are not in a function format. However, once I packaged everything into a function. I will receive an error. The data is as following:

# A
graph_a = {}
graph_a['s'] = {}
graph_a['a'] = {}
graph_a['b'] = {}
graph_a['c'] = {}
graph_a['d'] = {}
graph_a['f'] = {}
graph_a['s']['a'] = 2
graph_a['s']['b'] = 5
graph_a['a']['b'] = 8
graph_a['a']['d'] = 7
graph_a['b']['d'] = 2
graph_a['b']['c'] = 4
graph_a['c']['d'] = 6
graph_a['c']['f'] = 3
graph_a['d']['f'] = 1

cost_a = {}
cost_a['a'] = 2
cost_a['b'] = 5
cost_a['f'] = float('inf')
cost_a['c'] = float('inf')
cost_a['d'] = float('inf')

parent_a = {}
parent_a['a'] = 's'
parent_a['b'] = 's'
parent_a['c'] = None
parent_a['d'] = None
parent_a['f'] = None

I also defined the lowest cost node function to find the lowest cost node in the cost in each iteration.

searched = []

def lowestcostnode(x):
    lowestnode = None
    lowestcost = float('inf')
    for node in x:
        if x[node] < lowestcost and node not in searched:
            lowestcost = x[node]
            lowestnode = node
    return lowestnode

I can get the results successfully via:

searched = []
node = lowestcostnode(cost_a)
while node is not None:
    currentcost = cost_a[node]
    neighbors = graph_a[node]
    for n in neighbors:
        if cost_a[n] > currentcost + neighbors[n]:
            cost_a[n] = currentcost + neighbors[n]
            parent_a[n] = node
    searched.append(node)
    node = lowestcostnode(cost_a)

The result is as:

cost_a
{'a': 2, 'b': 5, 'f': 8, 'c': 9, 'd': 7}

But once I tried to packed the codes into a function, I got error message:

  1. The function will generate error message as following if I create the searched list within the function:
def dijkstra(graph, parent, cost):
    searched = []
    node = lowestcostnode(cost)
    while node is not None:
        currentcost = cost[node]
        neighbors = graph[node]
        for n in neighbors:
            if cost[n] > currentcost + neighbors[n]:
                cost[n] = currentcost + neighbors[n]
                parent[n] = node
        searched.append(node)
        node = lowestcostnode(cost)
    print('parent as:', parent)
    print('cost as:', cost)
    return parent, cost

The error will be:

dijkstra(graph_a, cost_a, parent_a)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-57-00f1df255994> in <module>
----> 1 dijkstra(graph, parent, cost)

<ipython-input-56-517377fe9acb> in dijkstra(graph, parent, cost)
     12 def dijkstra(graph, parent, cost):
     13     searched = []
---> 14     node = lowestcostnode(cost)
     15     while node is not None:
     16         currentcost = cost[node]

<ipython-input-56-517377fe9acb> in lowestcostnode(x)
      5     lowestcost = float('inf')
      6     for node in x:
----> 7         if x[node] < lowestcost and node not in searched:
      8             lowestcost = x[node]
      9             lowestnode = node

NameError: name 'searched' is not defined

However, I already defined the searched one line before calling the lowestcostnode function.

Thank you in advance for your help.

Upvotes: 0

Views: 183

Answers (1)

george
george

Reputation: 1829

You are calling: dijkstra(graph_a, cost_a, parent_a) but based on your function definition I would expect: dijkstra(graph_a, parent_a, cost_a)

Does this fix your problem?

Upvotes: 1

Related Questions