Skip to content

Instantly share code, notes, and snippets.

@Saneyan
Last active December 29, 2015 01:39
Show Gist options
  • Select an option

  • Save Saneyan/7595186 to your computer and use it in GitHub Desktop.

Select an option

Save Saneyan/7595186 to your computer and use it in GitHub Desktop.
Konigsberg Bridge
function generateEdges(graph) {
var i, node, edges = [];
for (node in graph) {
graph[node].forEach(function (neighbour) {
edges.push([node, neighbour]);
});
}
return edges;
}
function countEdges(edges) {
var counts = {};
edges.forEach(function (edge) {
if (edge[0] !== edge[1]) {
edge.forEach(function (e) {
if (!counts[e])
counts[e] = 1;
else
counts[e]++;
});
}
});
for (key in counts) {
counts[key] /= 2;
if (counts[key] % 2 !== 0)
odd++;
}
counts.possibility = odd === 0 || (odd > 0 && odd === 2);
return counts;
}
var graph_1 = {
'1': ['2', '6'],
'2': ['1', '3', '4', '5'],
'3': ['2', '7'],
'4': ['2', '5', '6', '8'],
'5': ['2', '4', '7', '9'],
'6': ['1', '4', '8', '10'],
'7': ['3', '5', '9', '12'],
'8': ['4', '6', '9', '11'],
'9': ['5', '7', '8', '11'],
'10': ['6', '11'],
'11': ['8', '9', '10', '12'],
'12': ['7', '11']
};
var graph_2 = {
'1': ['2', '4', '8'],
'2': ['1', '3', '4', '5'],
'3': ['2', '5', '6'],
'4': ['1', '2', '6', '7'],
'5': ['2', '3', '7', '8'],
'6': ['3', '4', '7'],
'7': ['4', '5', '6', '8'],
'8': ['1', '5', '7']
};
var edges_1 = generateEdges(graph_1);
var counts_1 = countEdges(edges_1);
//console.log(edges_1);
console.log(counts_1);
var edges_2 = generateEdges(graph_2);
var counts_2 = countEdges(edges_2);
//console.log(edges_2);
console.log(counts_2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment