-
Notifications
You must be signed in to change notification settings - Fork 299
/
Copy pathMinVertexCover.cs
39 lines (33 loc) · 1.18 KB
/
MinVertexCover.cs
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
using System.Collections.Generic;
using Advanced.Algorithms.DataStructures.Graph;
namespace Advanced.Algorithms.Graph;
/// <summary>
/// A minimum vertex conver algorithm implementation.
/// </summary>
public class MinVertexCover<T>
{
public List<IGraphVertex<T>> GetMinVertexCover(IGraph<T> graph)
{
return GetMinVertexCover(graph.ReferenceVertex, new HashSet<IGraphVertex<T>>(),
new List<IGraphVertex<T>>());
}
/// <summary>
/// An approximation algorithm for NP complete vertex cover problem.
/// Add a random edge vertices until done visiting all edges.
/// </summary>
private List<IGraphVertex<T>> GetMinVertexCover(IGraphVertex<T> vertex,
HashSet<IGraphVertex<T>> visited, List<IGraphVertex<T>> cover)
{
visited.Add(vertex);
foreach (var edge in vertex.Edges)
{
if (!cover.Contains(vertex) && !cover.Contains(edge.TargetVertex))
{
cover.Add(vertex);
cover.Add(edge.TargetVertex);
}
if (!visited.Contains(edge.TargetVertex)) GetMinVertexCover(edge.TargetVertex, visited, cover);
}
return cover;
}
}