Sibghat Khan
Sibghat Khan

Reputation: 147

K shortest path python

I'm having issues in finding K shortest path from a source called S to a destination called T. My Code looks like this

K = 4
S = 'C'
T = 'A'
B = {}
P = set()
count = {}
for U in graph.keys():
    count[U] = 0 

B[S] = 0 
while(len(B)>=1 and count[T]<K):
    PU = min(B, key = B.get)
    cost = B[PU]
    U = PU[len(PU)-1]
    del B[PU]
    count[U] += 1 
    if U==T:
       P.add(U)
       if count[U]<=K:
          V = graph[U].keys()
          for v in V:
             if v not in PU:
                PV = PU+v
                B[PV] = cost+1

This is equivalent to the actual code equivalent of Pseudo Code which can be found at https://en.wikipedia.org/wiki/K_shortest_path_routing . The actual variables are also the same as in Pseudo Code.Furthermore, my graph looks like this:

{

'A': {'C': 4.0, 'B': 10.0, 'E': 10.0, 'D': 10.0, 'G': 1.0, 'F': 2.0, 'I': 3.0, 'H': 3.0, 'J': 10.0}, 'C': {'A': 4.0, 'B': 5.0, 'E': 9.0, 'D': 6.0, 'G': 9.0, 'F': 10.0, 'I': 5.0, 'H': 10.0, 'J': 5.0}, 'B': {'A': 2.0, 'C': 10.0, 'E': 8.0, 'D': 1.0, 'G': 8.0, 'F': 4.0, 'I': 2.0, 'H': 2.0, 'J': 6.0}, 'E': {'A': 9.0, 'C': 5.0, 'B': 10.0, 'D': 4.0, 'G': 9.0, 'F': 9.0, 'I': 3.0, 'H': 3.0, 'J': 7.0}, 'D': {'A': 4.0, 'C': 6.0, 'B': 5.0, 'E': 7.0, 'G': 1.0, 'F': 1.0, 'I': 2.0, 'H': 9.0, 'J': 3.0}, 
'G': {'A': 2.0, 'C': 10.0, 'B': 3.0, 'E': 1.0, 'D': 10.0, 'F': 5.0, 'I': 5.0, 'H': 6.0, 'J': 1.0}, 'F': {'A': 2.0, 'C': 3.0, 'B': 6.0, 'E': 7.0, 'D': 8.0, 'G': 10.0, 'I': 1.0, 'H': 8.0, 'J': 2.0}, 'I': {'A': 1.0, 'C': 1.0, 'B': 2.0, 'E': 1.0, 'D': 6.0, 'G': 7.0, 'F': 1.0, 'H': 6.0, 'J': 2.0}, 
'H': {'A': 3.0, 'C': 4.0, 'B': 5.0, 'E': 1.0, 'D': 2.0, 'G': 6.0, 'F': 4.0, 'I': 1.0, 'J': 4.0}, 
'J': {'A': 5.0, 'C': 6.0, 'B': 1.0, 'E': 8.0, 'D': 7.0, 'G': 9.0, 'F': 8.0, 'I': 10.0, 'H': 1.0}}

My Output looks like this

{'A'}

Whereas there should be four paths.

Also, Please Note I'm not allowed to Use Networkx or Graph libraries. I have to use the basic libraries in Python only.

Is Someone able to understand the Problem ?

Upvotes: 1

Views: 523

Answers (2)

RPT
RPT

Reputation: 748

I made a few changes in your code. There was an error in your where you check if the node is the target and the code following that.

K = 4
S = 'C'
T = 'F'
B = {}
P = set()
count = {}
for U in graph.keys():
    count[U] = 0

B[S] = 0
while(len(B)>=1 and count[T]<K):
    PU = min(B, key = B.get)
    print('Minimum Distance found in this loop : ' , PU)
    cost = B[PU]
    U = PU[len(PU)-1]
    del B[PU]
    count[U] += 1
    if(U == T):
        P.add(PU)
        print('Closest neighbour of ' , S , ' is : ', U)
        print('Reached target')
        print('Final map : ', P)
        exit(0)
    else:
       if count[U] <= K:
          V = graph[U].keys()
          print('Looking at neighbours of : ', PU)
          for v in V:
             if v not in PU:
                PV = PU + v
                print(PV)
                B[PV] = cost+1
          print('B dictionary is : ', B)

Run this program and you will get something like :

Minimum Distance found in this loop :  C
Looking at neighbours of :  C
CA
CB
CE
CD
CG
CF
CI
CH
CJ
B dictionary is :  {'CA': 1, 'CB': 1, 'CE': 1, 'CD': 1, 'CG': 1, 'CF': 1, 'CI': 1, 'CH': 1, 'CJ': 1}
Minimum Distance found in this loop :  CA
Closest neighbour of  C  is :  A
Reached target
Final map :  {'CA'}

The output should be fairly self-descriptive. Also, you have weights associated with each node (I presume it is that), then I don't understand why you increment the distance count by just 1 to each corresponding neighbour

Upvotes: 1

Shihab Shahriar Khan
Shihab Shahriar Khan

Reputation: 5485

Problem 1: Bring the if count[U]<=K: out under while loop, not if.

Problem 2: change this:

if U==T:
   P.add(U)

to this:

if U==T:
   P.add(PU)

Problem 3: I am a bit confused about this. change PU = min(B, key = B.get) to this PU = min(B,key=lambda x:B[x])

It worked for me, so let me know if you run into additional problems.

Upvotes: 1

Related Questions