Ramsey's theorem: Difference between revisions
Content added Content deleted
(→{{header|C}}: add checking) |
(Updated and hopefully fixed the D entry) |
||
Line 89: | Line 89: | ||
=={{header|D}}== |
=={{header|D}}== |
||
⚫ | |||
{{incorrect|D|The task has been changed to also require demonstrating that the graph is a solution.}} |
|||
<lang d>import std.stdio, std.string, std.algorithm, std.range; |
|||
⚫ | |||
<lang d>import std.stdio; |
|||
/// Generate the connectivity matrix. |
|||
⚫ | |||
immutable(char)[][] generateMatrix() { |
|||
enum size_t N = 17; |
|||
immutable r = format("-%b", 53643); |
|||
return r.length.iota.map!(i => r[$-i .. $] ~ r[0 .. $-i]).array; |
|||
} |
|||
/**Check that every clique of four has at least one pair connected |
|||
⚫ | |||
and one pair unconnected. It requires a symmetric matrix.*/ |
|||
⚫ | |||
string ramseyCheck(in char[][] mat) pure @safe |
|||
in { |
|||
⚫ | |||
assert(row.length == mat.length); |
|||
foreach (immutable c, immutable x; row) |
|||
assert(x == mat[c][r]); |
|||
} |
|||
} body { |
|||
immutable N = mat.length; |
|||
char[6] connectivity = '-'; |
|||
foreach (immutable |
foreach (immutable a; 0 .. N) { |
||
foreach (immutable |
foreach (immutable b; 0 .. N) { |
||
if (a == b) continue; |
|||
connectivity[0] = mat[a][b]; |
|||
foreach (immutable c; 0 .. N) { |
|||
if (a == c || b == c) continue; |
|||
connectivity[1] = mat[a][c]; |
|||
connectivity[2] = mat[b][c]; |
|||
foreach (immutable d; 0 .. N) { |
|||
if (a == d || b == d || c == d) continue; |
|||
connectivity[3] = mat[a][d]; |
|||
connectivity[4] = mat[b][d]; |
|||
connectivity[5] = mat[c][d]; |
|||
// We've extracted a meaningful subgraph, |
|||
// check its connectivity. |
|||
if (!connectivity[].canFind('0')) |
|||
return format("Fail, found wholly connected: ", |
|||
a, " ", b," ", c, " ", d); |
|||
else if (!connectivity[].canFind('1')) |
|||
return format("Fail, found wholly " ~ |
|||
"unconnected: ", |
|||
a, " ", b," ", c, " ", d); |
|||
} |
|||
⚫ | |||
} |
} |
||
} |
} |
||
return "Satisfies Ramsey condition."; |
|||
⚫ | |||
} |
|||
⚫ | |||
const mat = generateMatrix; |
|||
⚫ | |||
mat.ramseyCheck.writeln; |
|||
}</lang> |
}</lang> |
||
{{out}} |
{{out}} |
||
Line 126: | Line 164: | ||
0 1 0 0 0 1 1 0 0 0 1 0 1 1 - 1 1 |
0 1 0 0 0 1 1 0 0 0 1 0 1 1 - 1 1 |
||
1 0 1 0 0 0 1 1 0 0 0 1 0 1 1 - 1 |
1 0 1 0 0 0 1 1 0 0 0 1 0 1 1 - 1 |
||
1 1 0 1 0 0 0 1 1 0 0 0 1 0 1 1 - |
1 1 0 1 0 0 0 1 1 0 0 0 1 0 1 1 - |
||
Satisfies Ramsey condition.</pre> |
|||
=={{header|Erlang}}== |
=={{header|Erlang}}== |