-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWelshPowell.java
More file actions
148 lines (125 loc) · 3.56 KB
/
WelshPowell.java
File metadata and controls
148 lines (125 loc) · 3.56 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
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
public class WelshPowell {
//adjacency matrix
public int[][] graph;
//array of degrees
public int[] degrees;
//number of nodes
public int n;
//all colored nodes
public HashSet<Integer> C;
//all candidate nodes
public HashSet<Integer> V;
//the ordering of vertices by degrees
public int[] order;
//assigned colors
public int[] colours;
/**
* Constructor
* @param graph adj matrix
* @param degrees array of degrees
*/
public WelshPowell(int[][] graph, int[] degrees) {
this.graph = graph;
this.degrees = degrees;
n = graph[0].length;
colours = new int[n];
C = new HashSet<>();
V = new HashSet<>();
}
public int solve() {
order = findOrdering();
for (int i=0; i<n; i++) {
Integer temp = i;
V.add(temp);
}
int c = 0;
int nextVertex;
int startVertex;
while (C.size() != n) {
c++;
startVertex = findFirst();
colour(startVertex, c);
while (!V.isEmpty()) {
nextVertex = findNext();
colour(nextVertex, c);
}
for (int i=0; i<n; i++) {
if (colours[i] == 0) {
V.add(i);
}
}
}
return c;
}
/**
* Colours a node with certain color and updates sets
* @param vertex v being colored
* @param c color
*/
private void colour(int vertex, int c) {
colours[vertex] = c;
C.add(vertex);
V.remove(vertex);
//remove all adjacent vertices from V
for (int i=0; i<n; i++) {
if (graph[vertex][i] == 1) {
V.remove(i);
}
}
}
/**
* Finds first vertex in ordering that hasn't been colored
* and is a candidate
* @return vertex
*/
private int findNext() {
int next = -1;
for (int i=0; i<order.length; i++) {
//if not coloured and still allowed (in V)
if (colours[order[i]] == 0 && V.contains(order[i])) {
next = order[i];
break;
}
}
return next;
}
/**
* Finds first vertex in an iteration
* @return vertex
*/
private int findFirst() {
int next = -1;
for (int i=0; i<order.length; i++) {
//if not coloured
if (colours[order[i]] == 0) {
next = order[i];
break;
}
}
return next;
}
/**
* orders the vertices in an array by degrees
* Does this using a new object and sorting that object
* @return array of vertices ordered by degree
*/
private int[] findOrdering() {
int ordering[] = new int[n];
// Init the element list
List<Vertex> elements = new ArrayList<Vertex>();
for (int i = 0; i < degrees.length; i++) {
elements.add(new Vertex(i, degrees[i]));
}
// Sort by comparator (sorts by degree)
Collections.sort(elements);
//We are interested in the index of the node, not the degree
for (int j=0; j<n; j++) {
ordering[j] = elements.get(j).index;
}
return ordering;
}
}