-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTransition.java
More file actions
66 lines (55 loc) · 1.96 KB
/
Copy pathTransition.java
File metadata and controls
66 lines (55 loc) · 1.96 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*************************************************************************
* Compilation: javac Transition.java
* Execution: java Transition < input.txt
* Data files: http://introcs.cs.princeton.edu/16pagerank/tiny.txt
* http://introcs.cs.princeton.edu/16pagerank/medium.txt
*
* This program is a filter that reads links from standard input and
* produces the corresponding transition matrix on standard output.
* First, it processes the input to count the outlinks from each page.
* Then it applies the 90-10 rule to compute the transition matrix.
* It assumes that there are no pages that have no outlinks in the
* input (see Exercise 1.6.3).
*
* % more tiny.txt
* 5
* 0 1
* 1 2 1 2
* 1 3 1 3 1 4
* 2 3
* 3 0
* 4 0 4 2
*
* % java Transition < tiny.txt
* 5 5
* 0.02 0.92 0.02 0.02 0.02
* 0.02 0.02 0.38 0.38 0.20
* 0.02 0.02 0.02 0.92 0.02
* 0.92 0.02 0.02 0.02 0.02
* 0.47 0.02 0.47 0.02 0.02
*
*************************************************************************/
public class Transition {
public static void main(String[] args) {
int N = StdIn.readInt(); // number of pages
int[][] counts = new int[N][N]; // counts[i][j] = # links from page i to page j
int[] outDegree = new int[N]; // outDegree[j] = # links from page i to anywhere
// Accumulate link counts.
while (!StdIn.isEmpty()) {
int i = StdIn.readInt();
int j = StdIn.readInt();
outDegree[i]++;
counts[i][j]++;
}
StdOut.println(N + " " + N);
// Print probability distribution for row i.
for (int i = 0; i < N; i++) {
// Print probability for column j.
for (int j = 0; j < N; j++) {
double p = .90*counts[i][j]/outDegree[i] + .10/N;
StdOut.printf("%7.5f ", p);
}
StdOut.println();
}
}
}