Created
November 24, 2018 04:44
-
-
Save kunigami/540ec61d254e981000bf23bcc59ba6b7 to your computer and use it in GitHub Desktop.
This file contains 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
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