-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDiffusionInnovationsModel.java
59 lines (43 loc) · 1.79 KB
/
DiffusionInnovationsModel.java
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
import java.util.*;
public class DiffusionInnovationsModel {
private static Random rnd = new Random();
static boolean[] simulateDiffusionOfInnovationsPhenomenon (int s, Map<Integer, List<Integer>> adjacencyMap, int V,
List<Integer> initialAdopters)
{
boolean visited[] = new boolean[V];
boolean nodesFaction[] = new boolean[V];
double thresholdValues[] = new double[V];
LinkedList<Integer> queue = new LinkedList<Integer>();
int iteration = 0;
queue.add(s);
for (int i = 0; i < V; ++i)
thresholdValues[i] = -1.0;
for (Integer node : initialAdopters)
nodesFaction[node] = true;
while (queue.size() > 0)
{
s = queue.poll();
boolean oldFaction = nodesFaction[s];
//Genero il valore di soglia del nodo s
if (thresholdValues[s] < 0) thresholdValues[s] = rnd.nextDouble();
Iterator<Integer> i = adjacencyMap.get(s).listIterator();
//Calcolo la frazione di nodi che hanno adottato A di s
int adopters = 0;
for (Integer adjacent : adjacencyMap.get(s))
if (nodesFaction[adjacent]) adopters++;
adopters = adopters / adjacencyMap.get(s).size();
if (adopters >= thresholdValues[s])
nodesFaction[s] = true;
visited[s] = true;
while (i.hasNext())
{
int n = i.next();
if (!visited[n] || oldFaction != nodesFaction[s])
queue.add(n);
}
iteration++;
}
System.out.println(iteration);
return nodesFaction;
}
}