-
Notifications
You must be signed in to change notification settings - Fork 0
/
Set_Covering_Problem_GreedyAlgorithm.cs
37 lines (36 loc) · 1.49 KB
/
Set_Covering_Problem_GreedyAlgorithm.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
using System;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApplication
{
public class Program
{
public static void Main(string[] args)
{
var statesNeeded = new HashSet<string> { "mt", "wa", "or", "id", "nv", "ut", "ca", "az" };
var stations = new Dictionary<string, HashSet<string>>();
stations.Add("kone", new HashSet<string> { "id", "nv", "ut" });
stations.Add("ktwo", new HashSet<string> { "wa", "id", "mt" });
stations.Add("kthree", new HashSet<string> { "or", "nv", "ca" });
stations.Add("kfour", new HashSet<string> { "nv", "ut" });
stations.Add("kfive", new HashSet<string> { "ca", "az" });
var finalStations = new HashSet<string>();
while (statesNeeded.Any())
{
string bestStation = null;
var statesCovered = new HashSet<string>();
foreach (var station in stations)
{
var covered = new HashSet<string>(statesNeeded.Intersect(station.Value));
if (covered.Count > statesCovered.Count){
bestStation = station.Key;
statesCovered = covered;
}
}
statesNeeded.RemoveWhere(s => statesCovered.Contains(s));
finalStations.Add(bestStation);
}
Console.WriteLine(string.Join(", ", finalStations));
}
}
}