demo and summarize the contribution python prim multi-source shortest path search algorithms numba accelerated method

1, two test algorithms

# Coding: UTF-. 8 
Import Time
 Import numba
 Import numpy AS NP
 '' ' 
using numba acceleration summary, 
(1), the numerical calculation such as int when float double other type of computing 
using numba acceleration, speed may be accelerated, string type data can not be use numba accelerated. 
(2), the numerical calculation: Do not use numba small loop, loop 100 is greater than the above may be used numba acceleration. 
(3), when the small cycles switching time-consuming process of hair, so slow. 
(4), is calculated in the cycle time less calculation within 1 second, the use of other numba acceleration, calculation time greater than one second may be used. 
'' ' 
@ Numba.jit 
DEF test1 (LS): 
    n- = len (LS) 
    dt = np.zeros ((n-, n-))
     for I, Item in the enumerate (LS):
         for J, Val in the enumerate (LS) : 
            dt [I, J] = (Val +item)

    return dt

@numba.njit
def prim(G,start=0):
    N = len(G)
    k = start
    MST = []
    vis = np.zeros(N)
    vis[0] = 1

    while k < N-1:
        minw = np.inf
        u,v=0,0
        for i in range(N):
            for j in range(N):
                if vis[i] ==1 and vis[j] == 0:
                    if G[i,j] < minw:
                        minw = G[i,j]
                        u,v=i,j
        vis[v] = 1
        k = k+1
        MST.append([u,v,minw].copy())

    return MST

if __name__=="__main__":
    ls = list(float(i) for i in range(30000))
    n=500
    G = np.random.randint(100,1000,(n,n))
    st = time.time()
    # re = test1(ls)
    re1 = prim(G)
    print(re1[-10:])
    print(time.time()-st)

 

Guess you like

Origin www.cnblogs.com/wuzaipei/p/11432559.html