-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraphContainer.pde
123 lines (108 loc) · 3.15 KB
/
GraphContainer.pde
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
123
/*
Graph container to create graph then
initializae it with nodes.
https://www.quora.com/Is-it-possible-to-generate-
a-random-large-social-network-graph-that-is-statistically
-indistinguishable-from-Facebooks-social-network-graph
- - - - - - - - - - - - - - - - - -
g: original graph object
t: total length of the "infection" spreading
t1: length of time a node is to be infecitous
p: probability of a node to infect its neighbor
s: the number of seeds to generate to be infected on init
*/
/* Initialize necessary components: mess of globals */
int t = 8;
int t1 = 0; // implement this
float p = 0.3;
int seeds = 1;
Graph g = new Graph( 30 );
int[] visited = g.initVisited();
int ran = 0;
int findI = 0;
int i =0;
int current = 9999;
int days = 0;
public class Graph {
int numOfNodes;
/* Array of lists for Adjacency List Representation */
LinkedList<Integer> adjList[];
Map<Integer, String> nodeStage;
Graph(){}
/* Constructor adds # of nodes specified */
Graph( int v ) {
numOfNodes = v;
adjList = new LinkedList[v];
for (int i=0; i<v; ++i)
adjList[i] = new LinkedList();
nodeStage = new HashMap<Integer,String>();
for (Integer i = 0; i< v; i++){
nodeStage.put(Integer.valueOf(i),"S");
}
}
/* function to check whether a node is visited or not */
int isVisited(int[] visited, int i){
if( visited[i] == 0){
return 0;
}
return 1;
}
/* initializes a new visited array */
int[] initVisited(){
int[] visited = new int[numOfNodes];
return visited;
}
/* adds edge to graph by connecting in linked list */
void addEdge(int source, int dest) {
adjList[source].add(dest);
}
/* returns the probability of a given event happening */
boolean getProbability(Float p){
Random rand = new Random();
double q = rand.nextDouble();
if( q <= p){
if(q < 0.3){
stroke(255,255,0);
}else if(q < 0.6 ){
stroke(255,228,181);
} else{
stroke(255,127,80);
}
return true;
}
return false;
}
/* implements Erdos Reyni by way of edge adding */
void buildErdosReyni( Graph g,float p ) {
Random rand = new Random();
for( int i = 0; i < numOfNodes;i++ ){
for( int j = 0; j < numOfNodes; j++ ){
/* probability of the node connecting to another */
if( getProbability( p ) ){
addEdge(i,j);
}
}
}
}
/* prints the graph to ensure functionality */
void printGraph() {
println(" - - - - - - - - - - - - - - - - - - - - - - - -");
for(int v = 0; v < numOfNodes; v++) {
System.out.println("current node: "+ v + " status: " + nodeStage.get(v));
System.out.print("Neighbors: ");
for(Integer node: adjList[v]){
System.out.print(" -> "+ node + "|" + nodeStage.get(node));
}
System.out.println("\n");
}
}
/* generates the seeds to which are infected in the graph */
void generateSeeds( int s ){
ArrayList<Integer> seeds = new ArrayList<Integer>();
Random rand = new Random();
for(int i =0;i<s;i++){
int s1 = rand.nextInt(numOfNodes);
nodeStage.put(Integer.valueOf(s1),"I");
}
}
}