45
45
neighbors_of_column (g:: BipartiteGraph , j:: Integer ) = nz_in_col (g. A_colmajor, j)
46
46
neighbors_of_row (g:: BipartiteGraph , i:: Integer ) = nz_in_row (g. A_rowmajor, i)
47
47
48
- function colored_neighbors_of_column (
49
- g:: BipartiteGraph , j:: Integer , colors:: AbstractVector{<:Integer}
50
- )
51
- return filter (neighbors_of_column (g, j)) do i
52
- ! iszero (colors[i])
53
- end
54
- end
55
-
56
- function colored_neighbors_of_row (
57
- g:: BipartiteGraph , i:: Integer , colors:: AbstractVector{<:Integer}
58
- )
59
- return filter (neighbors_of_row (g, i)) do j
60
- ! iszero (colors[j])
61
- end
62
- end
63
-
64
48
function distance2_column_coloring (g:: BipartiteGraph )
65
49
n = length (columns (g))
66
50
colors = zeros (Int, n)
67
51
forbidden_colors = zeros (Int, n)
68
52
for v in columns (g) # default ordering
69
53
for w in neighbors_of_column (g, v)
70
- for x in colored_neighbors_of_row (g, w, colors)
71
- forbidden_colors[colors[x]] = v
54
+ for x in neighbors_of_row (g, w)
55
+ if ! iszero (colors[x])
56
+ forbidden_colors[colors[x]] = v
57
+ end
58
+ end
59
+ end
60
+ for c in columns (g)
61
+ if forbidden_colors[c] != v
62
+ colors[v] = c
63
+ break
72
64
end
73
65
end
74
- colors[v] = minimum (c for c in columns (g) if forbidden_colors[c] != v)
75
66
end
76
67
return colors
77
68
end
@@ -82,11 +73,18 @@ function distance2_row_coloring(g::BipartiteGraph)
82
73
forbidden_colors = zeros (Int, m)
83
74
for v in 1 : m # default ordering
84
75
for w in neighbors_of_row (g, v)
85
- for x in colored_neighbors_of_column (g, w, colors)
86
- forbidden_colors[colors[x]] = v
76
+ for x in neighbors_of_column (g, w)
77
+ if ! iszero (colors[x])
78
+ forbidden_colors[colors[x]] = v
79
+ end
80
+ end
81
+ end
82
+ for c in rows (g)
83
+ if forbidden_colors[c] != v
84
+ colors[v] = c
85
+ break
87
86
end
88
87
end
89
- colors[v] = minimum (c for c in rows (g) if forbidden_colors[c] != v)
90
88
end
91
89
return colors
92
90
end
@@ -166,21 +164,27 @@ function star_coloring(g::AdjacencyGraph)
166
164
if ! iszero (colors[w]) # w is colored
167
165
forbidden_colors[colors[w]] = v
168
166
end
169
- for x in colored_neighbors (g, w, colors )
170
- if iszero (colors[w]) # w is not colored
167
+ for x in neighbors (g, w)
168
+ if ! iszero (colors[x]) && iszero (colors[w]) # w is not colored
171
169
forbidden_colors[colors[x]] = v
172
170
else
173
- for y in colored_neighbors (g, x, colors)
174
- y != w || continue
175
- if colors[y] == colors[w]
176
- forbidden_colors[colors[x]] = v
177
- break
171
+ for y in neighbors (g, x)
172
+ if ! iszero (colors[y]) && y != w
173
+ if colors[y] == colors[w]
174
+ forbidden_colors[colors[x]] = v
175
+ break
176
+ end
178
177
end
179
178
end
180
179
end
181
180
end
182
181
end
183
- colors[v] = minimum (c for c in columns (g) if forbidden_colors[c] != v)
182
+ for c in columns (g)
183
+ if forbidden_colors[c] != v
184
+ colors[v] = c
185
+ break
186
+ end
187
+ end
184
188
end
185
189
return colors
186
190
end
0 commit comments