Skip to main content
Tweeted twitter.com/StackCodeReview/status/1614004367442558977
Became Hot Network Question
added 157 characters in body
Source Link
g = input().split()
N, M = int(g[0]), int(g[1])
high = 2 ** 32 - 1
map = [[(high) for i in range(N)] for i in range(N)]
for i in range(M):
    g = input().split()
    left, right, value = int(g[0])-1, int(g[1])-1, int(g[2])
    map[left][right] = min(value, map[left][right])
    map[right][left] = min(value, map[right][left])
# END INPUT


queue = [0]
costs = [(high) for i in range(N)]
costs[0] = 0
#camefrom = [-1 for i in range(N)]


while len(queue) > 0: 
    current = queue.pop()
    cost = costs[current]
    for i in range(N): 
        #if i == current: 
        #    continue
        if costs[current] >= costs[i]: 
            continue
        g = cost + map[current][i]
        if g < costs[i]: 
            queue.append(i)
            costs[i] = g


print('\n'.join([("-1" if i == high else str(i)) for i in costs]))

While trying to translate my Java solution to Python (Djikstra's algorithmthat did work) to Python for this problem I've realized that it keeps reaching the time limit. Otherwise, it works perfectly fine, getting maybe half the answers right. I've looked at other answers and I just can't figure out how their code is so much more efficient than mine.

Any ideas for optimization?

g = input().split()
N, M = int(g[0]), int(g[1])
high = 2 ** 32 - 1
map = [[(high) for i in range(N)] for i in range(N)]
for i in range(M):
    g = input().split()
    left, right, value = int(g[0])-1, int(g[1])-1, int(g[2])
    map[left][right] = min(value, map[left][right])
    map[right][left] = min(value, map[right][left])
# END INPUT


queue = [0]
costs = [(high) for i in range(N)]
costs[0] = 0
#camefrom = [-1 for i in range(N)]


while len(queue) > 0: 
    current = queue.pop()
    cost = costs[current]
    for i in range(N): 
        #if i == current: 
        #    continue
        if costs[current] >= costs[i]: 
            continue
        g = cost + map[current][i]
        if g < costs[i]: 
            queue.append(i)
            costs[i] = g


print('\n'.join([("-1" if i == high else str(i)) for i in costs]))

While trying to translate my Java solution to Python (Djikstra's algorithm) for this problem that keeps reaching the time limit. Otherwise, it works perfectly fine.

Any ideas for optimization?

g = input().split()
N, M = int(g[0]), int(g[1])
high = 2 ** 32 - 1
map = [[(high) for i in range(N)] for i in range(N)]
for i in range(M):
    g = input().split()
    left, right, value = int(g[0])-1, int(g[1])-1, int(g[2])
    map[left][right] = min(value, map[left][right])
    map[right][left] = min(value, map[right][left])
# END INPUT


queue = [0]
costs = [(high) for i in range(N)]
costs[0] = 0
#camefrom = [-1 for i in range(N)]


while len(queue) > 0: 
    current = queue.pop()
    cost = costs[current]
    for i in range(N): 
        #if i == current: 
        #    continue
        if costs[current] >= costs[i]: 
            continue
        g = cost + map[current][i]
        if g < costs[i]: 
            queue.append(i)
            costs[i] = g


print('\n'.join([("-1" if i == high else str(i)) for i in costs]))

While trying to translate my Java solution (that did work) to Python for this problem I've realized that it keeps reaching the time limit. Otherwise, it works perfectly fine, getting maybe half the answers right. I've looked at other answers and I just can't figure out how their code is so much more efficient than mine.

Any ideas for optimization?

Source Link

Optimizing Python BFS Code

g = input().split()
N, M = int(g[0]), int(g[1])
high = 2 ** 32 - 1
map = [[(high) for i in range(N)] for i in range(N)]
for i in range(M):
    g = input().split()
    left, right, value = int(g[0])-1, int(g[1])-1, int(g[2])
    map[left][right] = min(value, map[left][right])
    map[right][left] = min(value, map[right][left])
# END INPUT


queue = [0]
costs = [(high) for i in range(N)]
costs[0] = 0
#camefrom = [-1 for i in range(N)]


while len(queue) > 0: 
    current = queue.pop()
    cost = costs[current]
    for i in range(N): 
        #if i == current: 
        #    continue
        if costs[current] >= costs[i]: 
            continue
        g = cost + map[current][i]
        if g < costs[i]: 
            queue.append(i)
            costs[i] = g


print('\n'.join([("-1" if i == high else str(i)) for i in costs]))

While trying to translate my Java solution to Python (Djikstra's algorithm) for this problem that keeps reaching the time limit. Otherwise, it works perfectly fine.

Any ideas for optimization?