forked from cmuparlay/parlaylib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph_color.h
68 lines (56 loc) · 2.15 KB
/
graph_color.h
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
#include <unordered_set>
#include <parlay/primitives.h>
#include <parlay/sequence.h>
#include <parlay/delayed.h>
#include <parlay/random.h>
#include "helper/speculative_for.h"
// **************************************************************
// Finds an approximate minimum graph vertex coloring,
// i.e., no neighboring vertices can have the same color.
// Based on the greedy degree heuristic: coloring the vertices in
// order by degree (largest first).
// To respect serial ordering, uses "deterministic reservations".
// Returns the same coloring as the sequential algorithm. See:
// "Internally deterministic parallel algorithms can be fast"
// Blelloch, Fineman, Gibbons, and Shun.
// Will generate same matching as a greedy sequential matching.
// **************************************************************
using vertex = int;
using vertices = parlay::sequence<vertex>;
using graph = parlay::sequence<vertices>;
parlay::sequence<int> graph_coloring(graph const &G) {
long n = G.size(); // number of vertices
// rank vertices by degree, highest first
auto ranks = parlay::rank(parlay::map(G, parlay::size_of()),
std::greater{});
// inverse permutation of the rank
parlay::sequence<vertex> ordering(n);
parlay::parallel_for(0, n, [&] (vertex i) {ordering[ranks[i]] = i;});
// -1 means unknown
parlay::sequence<int> colors(n, -1);
// checks all earlier neighbors by rank ordering have been colored
auto is_ok = [&] (vertex i) {
vertex u = ordering[i];
for (vertex v : G[u])
if (colors[v] == -1 && ranks[v] < i) return try_again;
return try_commit;
};
// if so color this vertex
auto succeeded = [&] (vertex i) {
vertex u = ordering[i];
auto ngh_colors = parlay::map(G[u], [&] (vertex v) {
return colors[v];});
std::sort(ngh_colors.begin(),ngh_colors.end());
// find a color unused by any neighbor
int color = -1;
for (int c : ngh_colors) {
if (c > color + 1) break; // gap in colors, use it
color = c;
}
colors[u] = color + 1;
return true;
};
// loops over the vertices
speculative_for<vertex>(0, n, is_ok, succeeded);
return colors;
}