-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmarkov.py
40 lines (34 loc) · 1.19 KB
/
markov.py
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
import stdio
import stdarray
import sys
# Accept integer moves from the command-line, and read a transition
# matrix from standard input. Compute the probabilities that a
# random surfer lands on each page (page ranks) after moves
# matrix-vector multiplies, and write the page ranks to standard
# output.
moves = int(sys.argv[1])
n = stdio.readInt()
stdio.readInt() # Discard the second int of standard input.
# Read the transition matrix from standard input.
# probs[i][j] is the probability that the surfer moves from
# page i to page j.
probs = stdarray.create2D(n, n, 0.0)
for i in range(n):
for j in range(n):
probs[i][j] = stdio.readFloat()
# Use the power method to compute page ranks.
ranks = stdarray.create1D(n, 0.0)
ranks[0] = 1.0
for i in range(moves):
# Compute effect of next move on page ranks.
newRanks = stdarray.create1D(n, 0.0)
for j in range(n):
# New rank of page j is dot product
# of old ranks and column j of probs.
for k in range(n):
newRanks[j] += ranks[k] * probs[k][j]
ranks = newRanks
# Write the page ranks.
for rank in ranks:
stdio.writef("%8.5f", rank)
stdio.writeln()