Repository navigation
Expand file tree
/
Copy pathGraphNode.java
More file actions
152 lines (131 loc) · 4.15 KB
/
Copy pathGraphNode.java
File metadata and controls
152 lines (131 loc) · 4.15 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import org.apache.commons.collections15.Factory;
import java.awt.Color;
/**
* A node representation for the graph simulation.
*
* @author Katherine (Raven) Russell
*/
class GraphNode extends GraphComp {
/**
* The number of nodes created so far (for gerating unique ids
* from the factory method).
*/
public static int nodeCount = 0;
/**
* The boolean flag to indicate whether a node is active or not.
* Used to support certain graph algorithms.
*/
private boolean active;
/**
* The integer cost associated with each node.
* Used to support certain graph algorithms.
*/
private int cost;
/**
* The byte used as 8 boolean bits used to support graph coloring algorithms.
* Assume no more than 8 possible color and they are defined in ThreeTenColor.COLORS.
* nbrColorFlag[i] == 1: this node has a nbr taking color ThreeTenColor.COLORS[i]
* nbrColorFlag[i] == 0: this node has no nbr taking color ThreeTenColor.COLORS[i]
*/
private byte nbrColors;
/**
* Constructs a node with a given id.
*
* @param id the unique id of the node
*/
public GraphNode(int id) {
this.id = id; this.color = Color.WHITE;
this.active = false;
this.cost = 0;
this.nbrColors = 0;
}
/**
* Set this node to be active.
*/
public void setActive(){ active = true; }
/**
* Set this node to be inactive.
*/
public void unsetActive(){ active = false; }
/**
* Report the active status of this node.
* @return whether this node is active or not
*/
public boolean isActive() {return active; }
/**
* Set the cost of this node to be the incoming value.
*
* @param cost the cost to set for this node
*/
public void setCost(int cost) { this.cost = cost; }
/**
* Report the cost of this node.
* @return the cost of this node
*/
public int getCost() { return this.cost; }
/**
* Clear the information about the colors of neighbor nodes.
* Set all 8 bits to be 0.
*/
public void clearNbrColors(){
nbrColors = 0;
}
/**
* One neighbor of this node has been assigned color ThreeTenColor.COLORS[index].
* Update the record of nbrColors of this node based on this color assignment.
*
* @param index the color index assigned to one neighbor
* @return return false if invalid index; return true otherwise
*/
public boolean setNbrColor(int index){
if (index<0 || index>=ThreeTenColor.COLORS.length)
return false;
nbrColors = (byte) (nbrColors | (1<<index));
return true;
}
/**
* Report whether this node has any neighbor with the color
* defined as ThreeTenColor.COLORS[index].
*
* @param index the color index
* @return return true if there is a neighbor of this node taken the color; false otherwise
*/
public boolean nbrHasColor(int index){
if (index<0 || index>=ThreeTenColor.COLORS.length)
throw new IllegalArgumentException("Invalid color index.");
return (((nbrColors>>index) & 1) != 0);
}
/**
* {@inheritDoc}
*/
@Override
public int compareTo(GraphComp n) {
if (!(n instanceof GraphNode)){
return super.compareTo(n);
}
GraphNode node = (GraphNode) n;
if (this.cost!=node.cost)
return node.cost - this.cost;
else
return this.id-n.id; //use id to break the tie
}
/**
* {@inheritDoc}
*/
@Override
public String toString() {
return "" + id + (active ? ":"+cost : "");
}
/**
* Generates a new node with a (probably) unique id.
*
* @return a new node
*/
public static Factory<GraphNode> getFactory() {
return new Factory<GraphNode> () {
public GraphNode create() {
return new GraphNode(nodeCount++);
}
};
}
}