-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNetwork.java
89 lines (77 loc) · 2.4 KB
/
Network.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
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
package a4;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import graph.Graph;
import io.TextIO;
/** An instance represents a network of people, used with a Flu tree.<br>
* The names of all Person's will be distinct --no duplicates. <br>
*
* @author Mshnik, revised by Gries. */
public class Network extends Graph<Person, PersonConnection> {
/** The maximum health a person can have. */
private int maxHealth;
/** Names of all people. */
protected static String[] names;
/** Read in the array of names from text file names. */
static {
try {
names= TextIO.read(new File("data/names.txt"));
} catch (IOException e) {
System.err.println("Error reading names file, should be located at data/names.txt");
throw new RuntimeException(e.getMessage());
}
}
/** Constructor: an instance with no people and no connections. */
public Network() {
super();
}
/** Constructor: a graph of size people of health mh with edges generated <br>
* randomly based on connectionProbability cp. <br>
* Preconditions: 0 <= size, 1 <= mh, 0 <= cp <= 1. */
public Network(int size, int mh, double cp) {
super();
assert 0 <= size && 0 <= cp && cp <= 1 && 1 <= mh;
maxHealth= mh;
for (int i= 0; i < size; i++ ) {
// Add itself to this as part of construction
new Person(names[i], mh, this);
}
for (Person p1 : vertexSet()) {
for (Person p2 : vertexSet()) {
if (p1 != p2 && Math.random() < cp) {
addEdge(p1, p2, new PersonConnection());
}
}
}
}
/** Constructor: an instance generated for the people in dt. <br>
* There is an edge from each parent to each of its children. */
public Network(FluTree dt) {
super();
addVertex(dt.rootPerson());
recCreate(dt);
}
/** Add to this Network the people in children trees of dt, <br>
* adding edges from each root to its children. <br>
* Precondition: dt.getRoot is already in the graph. */
private void recCreate(FluTree dt) {
Person dtRoot= dt.rootPerson();
for (FluTree child : dt.copyOfChildren()) {
addVertex(child.rootPerson());
addEdge(dtRoot, child.rootPerson(), new PersonConnection());
recCreate(child);
}
}
/** Return a list of people in state s in this Network. */
public List<Person> getPeopleOfType(Person.State s) {
ArrayList<Person> lst= new ArrayList<>();
for (Person p : vertexSet()) {
if (p.state() == s) {
lst.add(p);
}
}
return lst;
}
}