Skip to content

Instantly share code, notes, and snippets.

@kunigami
Created November 24, 2018 04:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kunigami/540ec61d254e981000bf23bcc59ba6b7 to your computer and use it in GitHub Desktop.
Save kunigami/540ec61d254e981000bf23bcc59ba6b7 to your computer and use it in GitHub Desktop.
function find_circuit(graph: Graph, initialVertex: number) {
let vertex = initialVertex;
const path = new Path();
path.append(vertex);
while (true) {
const edge = graph.getNextEdgeForVertex(vertex);
if (edge == null) {
throw new Error("This graph is not Eulerian");
}
const nextVertex = edge.getTheOtherVertex(vertex);
graph.deleteEdge(edge);
vertex = nextVertex;
path.append(vertex);
// Circuit found
if (nextVertex === initialVertex) {
break;
}
}
// Search for sub-circuits
for (vertex of path.getContentAsArray()) {
// Since the vertex was added, its edges could have been removed
if (graph.getDegree(vertex) === 0) {
continue;
}
let subPath = find_circuit(graph, vertex);
// Merge sub-path into path
path.insertAtVertex(vertex, subPath);
}
return path;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment