I'm trying to implement a greedy algorithm below, So I'm trying to follow the gist of the algorithm details, but I'm getting an error at my IF statement: java.lang.IndexOutOfBoundsException: Index: 4, Size: 4 Does anyone know if the below method is how I should be approaching this?
- Color first vertex with first color.
Do following for remaining V-1 vertices.
a) Consider the currently picked vertex and color it with the lowest numbered color that has not been used on any previously colored vertices adjacent to it. If all previously used colors appear on vertices adjacent to v, assign a new color to it. */
public void greedy(LinkedHashMap<String, Node> nodes) {
ArrayList<Integer> colors = new ArrayList<Integer>();
ArrayList<Integer> colorsUsed = new ArrayList<Integer>();
nodes.entrySet().iterator().next().getValue().currentColor = 1;
colorsUsed.add(1);
for (Map.Entry<String,Node> entry : nodes.entrySet()) {
for (int i = 0; i < nodes.entrySet().size(); i++) {
if(entry.getValue().edges.get(i).currentColor == 0) {
//assign a color
System.out.println(colors.get(i));
}
}
}
}