/
transition.py
40 lines (32 loc) · 1.16 KB
/
transition.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
#-----------------------------------------------------------------------
# transition.py
#-----------------------------------------------------------------------
import stdio
import stdarray
# Read links from standard input and write the corresponding
# transition matrix to standard output. First, process the input
# to count the outlinks from each page. Then apply the 90-10 rule to
# compute the transition matrix. Assume that there are no pages that
# have no outlinks in the input.
n = stdio.readInt()
counts = stdarray.create2D(n, n, 0)
outDegree = stdarray.create1D(n, 0)
while not stdio.isEmpty():
# Accumulate link counts.
i = stdio.readInt()
j = stdio.readInt()
outDegree[i] += 1
counts[i][j] += 1
stdio.writeln(str(n) + ' ' + str(n))
for i in range(n):
# Print probability distribution for row i.
for j in range(n):
# Print probability for column j.
p = .90 * float(counts[i][j])/float(outDegree[i]) + .10/float(n)
stdio.writef('%5.2f', p)
stdio.writeln()
#-----------------------------------------------------------------------
# Example executions:
#
# transition.py < tiny.txt
# transition.py < medium.txt