1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| n = 5 d = 2 start = 0 connected = [ [0, 1, 1, 1, 0], [1, 0, 0, 0, 1], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [0, 1, 0, 0, 0] ]
deg = [3, 2, 1, 1, 1]
def solve(v, days): if days == d: return 1.0 if v==t else 0.0
if cache[v][days] != None: return cache[v][days]
cache[v][days] = 0.0 for i in range(n): if connected[v][i]: cache[v][days] += solve(i, days+1)/deg[v] return cache[v][days]
if __name__ == "__main__": prob = [] for t in range(n): cache = [[None for _ in range(30)] for _ in range(10)] prob.append(solve(0, 0)) print(prob)
|