Created
November 1, 2014 11:39
-
-
Save Gerjo/e03903d2218508350fd3 to your computer and use it in GitHub Desktop.
Code to test if a graph contains triangles. Requires N^3 time :(
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
console.clear(); | |
var g = []; | |
// Adjacency list representation of a graph | |
g[0] = [1, 3]; | |
g[1] = [0, 2]; | |
g[2] = [1, 3, 4]; | |
g[3] = [0, 2, 4]; | |
g[4] = [2, 3]; | |
for(var i = 0; i < g.length; ++i) { | |
var v = g[i]; | |
var c = 0; | |
var connected = [] | |
console.log(i + " connects to " + v.join()) | |
for(var j = 0; j < v.length; ++j) { | |
console.log(" " + v[j] + " connects to " + g[v[j]].join()); | |
for(var k = 0; k < g[v[j]].length; ++k) { | |
if(g[v[j]][k] == i) { | |
++c; | |
console.log(" has " + g[v[j]][k]) | |
connected.push(v[j]) | |
} | |
} | |
} | |
console.log("sub search: " + connected.join()) | |
var cc = 0; | |
for(var x = 0; x < connected.length; ++x) { | |
for(var y = x+1; y < connected.length; ++y) { | |
console.log(" test if " + connected[x] + " in " + g[connected[y]].join()) | |
for(var k = 0; k < g[connected[y]].length; ++k) { | |
if(g[y][k] == connected[x]) { | |
cc++; | |
} | |
} | |
} | |
} | |
if(c >= 2 && cc >= 2) { | |
console.log("ja!"); | |
break; | |
} else { | |
console.log("nein"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment