Jadu Sen
Jadu Sen

Reputation: 391

counting number of hops

I have below table:

orig dest

100 200

101 201

200 300

In this case, the distance(or hops) from orig 100 to dest 300 is 2. To elaborate, the graph path is 100>200>300 which is 2 hop.

I have created a scipy sparse matrix like below and got my BFS order like below: [100,200,300] when I provide scipy.sparse.csgraph.breadth_first_order with i_start value as 100.

However, I need the hop count array. Is there any option to do that?

Upvotes: 0

Views: 976

Answers (1)

PythonProgrammi
PythonProgrammi

Reputation: 23463

Counting hops

I put the data into a string called hop. And then I counted the hop (100=1) as I think I understood.

hop = """100 200
101 201
200 300"""

hop = hop.split("\n")

hcnt = 0
for h in hop:
    o, d = int(h.split()[0]), int(h. split()[1])
    dist = ((d - o) / 100)
    hcnt += dist

print("hops:", hcnt)

output

hops: 3.0

Upvotes: 1

Related Questions