-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecommender.java
More file actions
122 lines (117 loc) · 3.21 KB
/
Recommender.java
File metadata and controls
122 lines (117 loc) · 3.21 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import java.io.File;
import java.util.Scanner;
public class Recommender {
// Read graph from file. The file is a text file where each line contains an
// edge. The end and start of the edge are separated by space(s) or tabs
// (see graph.txt).
public static Graph<Integer> read(String fileName) {
try {
Graph<Integer> g = new MGraph<Integer>();
Scanner input = new Scanner(new File(fileName));
for (int i = 0, j = 0; input.hasNextInt(); i++, j++) {
i = input.nextInt();
j = input.nextInt();
g.addNode(i);
g.addNode(j);
g.addEdge(i, j);
}
input.close();
return g;
} catch (Exception e) {
return null;
}
}
// Return the top k recommended friends for user i using the popular users
// method. If i does not exist, return null. In case of a tie, users with
// the lowest id are selected.
public static <K extends Comparable<K>> PQK<Double, K> recommendDeg(
Graph<K> g, K i, int k) {
boolean res = g.isNode(i);
PQK<Double, K> array = null;
if (!res) {
return array;
} else {
array = new ArrayPQK<Double, K>(k);
Iterator<K> it = g.nodesIt();
while (it.isValid()) {
K k2 = it.next();
double deg = 0;
if (!g.isEdge(i, k2) && k2.compareTo(i) != 0) {
deg = g.deg(k2);
array.enqueue(deg, k2);
}
}
return array;
}
}
// Return the top k recommended friends for user i using common neighbors
// method. If i does not exist, return null. In case of a tie, users with
// the lowest id are selected.
public static <K extends Comparable<K>> PQK<Double, K> recommendCN(
Graph<K> g, K i, int k) {
boolean res = g.isNode(i);
PQK<Double, K> q = null;
if (!res) {
return q;
} else {
q = new ArrayPQK<Double, K>(k);
Iterator<K> it = g.nodesIt();
while (true && it.isValid()) {
K n = it.next();
double num = 0;
if (!g.isEdge(i, n) && n.compareTo(i) != 0) {
Iterator<K> it1 = g.neighb(i).minIt();
Iterator<K> it2;
while (true && it1.isValid()) {
K k1 = it1.next();
it2 = g.neighb(n).minIt();
while (true && it2.isValid()) {
K k2 = it2.next();
if (k1 != null && k2 != null) {
if (k1.compareTo(k2) == 0)
num++;
}
}
}
q.enqueue(num, n);
}
}
return q;
}
}
// Return the top k recommended friends for user i using weighted common
// neighbors method. If i does not exist, return null. In case of a tie,
// users with the lowest id are selected.
public static <K extends Comparable<K>> PQK<Double, K> recommendWCN(
Graph<K> g, K i, int k) {
boolean res = g.isNode(i);
PQK<Double, K> q = null;
if (!res) {
return q;
} else {
q = new ArrayPQK<Double, K>(k);
Iterator<K> it = g.nodesIt();
while (true && it.isValid()) {
K n = it.next();
double num = 0;
if (!g.isEdge(i, n) && n.compareTo(i) != 0) {
Iterator<K> it1 = g.neighb(i).minIt();
Iterator<K> it2;
while (true && it1.isValid()) {
K k1 = it1.next();
it2 = g.neighb(n).minIt();
while (true && it2.isValid()) {
K k2 = it2.next();
if (k1 != null && k2 != null) {
if (k1.compareTo(k2) == 0)
num = num + 1.0 / g.deg(k2);
}
}
}
q.enqueue(num, n);
}
}
return q;
}
}
}