-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathHamiltonPath.cs
More file actions
84 lines (65 loc) · 2.41 KB
/
HamiltonPath.cs
File metadata and controls
84 lines (65 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using System;
using System.Collections.Generic;
using System.Linq;
using AlgorithmsAndDataStructures.Algorithms.Graph.Common;
namespace AlgorithmsAndDataStructures.Algorithms.Graph.Backtracking;
public class HamiltonPath
{
#pragma warning disable CA1822 // Mark members as static
public (bool hasPath, bool hasCycle, int[] path) HasHamiltonPath(UndirectedGraph graph)
#pragma warning restore CA1822 // Mark members as static
{
if (graph is null) return (default, default, Array.Empty<int>());
var vertices = graph.Vertices();
var visited = new bool[vertices.Length];
var parent = new int[vertices.Length];
var path = new int[vertices.Length + 1];
for (var i = 0; i < parent.Length; i++) parent[i] = -1;
var hasPath = IsHamiltonPath(0, vertices, visited, parent);
var hasCycle = hasPath && HasCycle(parent, vertices);
if (hasPath)
{
var index = 0;
var pathIndex = 1;
while (parent[index] != -1)
{
path[pathIndex] = parent[index];
pathIndex++;
index = parent[index];
}
path[pathIndex] = hasCycle ? 0 : -1;
}
return (hasPath, hasCycle, path);
}
private static bool HasCycle(IReadOnlyList<int> parent, IReadOnlyList<List<int>> vertices)
{
var lastVertexIndex = -1;
for (var i = 0; i < parent.Count; i++)
if (parent[i] == -1)
{
lastVertexIndex = i;
break;
}
return vertices[lastVertexIndex].Any(arg => arg == 0);
}
private static bool IsHamiltonPath(int currentVertex, IReadOnlyList<List<int>> vertices, IList<bool> visited,
IList<int> parent)
{
visited[currentVertex] = true;
if (IsPathFormed(visited)) return true;
foreach (var adjacentVertex in vertices[currentVertex])
if (!visited[adjacentVertex])
{
parent[currentVertex] = adjacentVertex;
var isPathFormed = IsHamiltonPath(adjacentVertex, vertices, visited, parent);
if (isPathFormed) return true;
parent[currentVertex] = -1;
visited[adjacentVertex] = false;
}
return false;
}
private static bool IsPathFormed(IEnumerable<bool> visited)
{
return visited.All(arg => arg);
}
}