-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path3col.sql
62 lines (51 loc) · 1.18 KB
/
3col.sql
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
-- Solver for Graph 3-Colorability, implemented in SQL
-- Lorenz Leutgeb <[email protected]>
-- Run as follows:
-- psql -q < sat.sql
-- Implementation --------------------------------------------------------------
create type color as enum ('r', 'g', 'b');
-- Vertices
create temporary table vertex (c color);
insert into vertex values ('r'), ('g'), ('b');
-- Edges
create temporary table edge (u color, v color);
insert into edge select u.c, v.c from vertex u, vertex v where u.c != v.c;
-- Test ------------------------------------------------------------------------
-- 1
-- Example Graph:
-- (1, 2), (2, 3), (3, 1)
select distinct
-- Read off the coloring of all vertices
v1.c as c1,
v2.c as c2,
v3.c as c3
from
-- Declare vertices
vertex as v1,
vertex as v2,
vertex as v3,
-- Declare edges
edge as e1,
edge as e2,
edge as e3
where
-- Define edge 1
e1.u = v1.c and
e1.v = v2.c and
-- Define edge 2
e2.u = v2.c and
e2.v = v3.c and
-- Define edge 3
e3.u = v3.c and
e3.v = v1.c ;
-- Expected Output:
-- c1 | c2 | c3
-- ----+----+----
-- r | g | b
-- r | b | g
-- g | r | b
-- g | b | r
-- b | r | g
-- b | g | r
-- (6 rows)
-- Tested with PostgreSQL 10