private bool HamiltonianUtil(GraphSamples.Vertex startVertex, GraphSamples.Vertex currentVertex, List <GraphSamples.Vertex> result, HashSet <GraphSamples.Vertex> visited, int totalVertex) { visited.Add(currentVertex); result.Add(currentVertex); foreach (GraphSamples.Vertex child in currentVertex.Adjacents) { if (startVertex.Equals(child) && totalVertex == result.Count) { result.Add(startVertex); return(true); } if (!visited.Contains(child)) { bool isHamil = HamiltonianUtil(startVertex, child, result, visited, totalVertex); if (isHamil) { return(true); } } } result.RemoveAt(result.Count - 1); visited.Remove(currentVertex); return(false); }
public bool GetHamiltonianCycle(GraphSamples.Graph graph, List <GraphSamples.Vertex> result) { GraphSamples.Vertex startVertex = graph.Verticies[0]; HashSet <GraphSamples.Vertex> visited = new HashSet <GraphSamples.Vertex>(); return(HamiltonianUtil(startVertex, startVertex, result, visited, graph.Verticies.Count)); }