计算跳数

时间:2017-11-18 18:16:41

标签: python numpy scipy graph-algorithm

我有下表:

orig dest

100 200

101 201

200 300

在这种情况下,从orig 100到dest 300的距离(或跳跃)是2.详细说来,图形路径是100> 200> 300,这是2跳。

我已经创建了一个scipy稀疏矩阵,如下所示,得到了我的BFS命令,如下所示: 当我提供scipy.sparse.csgraph.breadth_first_order且i_start值为100时,[100,200,300]

但是,我需要跳数计数器。有没有选择呢?

1 个答案:

答案 0 :(得分:1)

计算跳数

我将数据放入名为hop的字符串中。然后我按照我想的那样计算了跳跃(100 = 1)。

RedisTemplate<String,Set<String>> redisTemplate = new RedisTemplate<String, Set<String>>();
redisTemplate.setHashValueSerializer(NEED_A_HASH_VALUE_SERIALIZER_HERE);
redisTemplate.setValueSerializer(NEED_A_VALUE_SERIALIZER_HERE);

输出

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)
相关问题