using
System;
using
System.Collections;
using
System.Collections.Generic;
using
System.Linq;
class
Solution
{
static
List<
int
> GraphSets(Dictionary<
int
, List<
int
>> graph)
{
if
(graph.Count == 0)
{
return
new
List<
int
>();
}
if
(graph.Count == 1)
{
return
new
List<
int
>(graph.Keys);
}
int
vCurrent = graph.Keys.First();
Dictionary<
int
, List<
int
>> graph2 =
new
Dictionary<
int
, List<
int
>>(graph);
graph2.Remove(vCurrent);
List<
int
> res1 = GraphSets(graph2);
foreach
(
int
v
in
graph[vCurrent])
{
if
(graph2.ContainsKey(v))
{
graph2.Remove(v);
}
}
List<
int
> res2 =
new
List<
int
>();
res2.Add(vCurrent);
res2.AddRange(GraphSets(graph2));
if
(res1.Count > res2.Count)
{
return
res1;
}
return
res2;
}
static
void
Main(
string
[] args)
{
int
V = 8;
int
[,] E = { { 1, 2 }, { 1, 3 }, { 2, 4 },
{ 5, 6 }, { 6, 7 }, { 4, 8 } };
Dictionary<
int
, List<
int
>> graph =
new
Dictionary<
int
, List<
int
>>();
for
(
int
i = 0; i < E.GetLength(0); i++)
{
int
v1 = E[i, 0];
int
v2 = E[i, 1];
if
(!graph.ContainsKey(v1))
{
graph[v1] =
new
List<
int
>();
}
if
(!graph.ContainsKey(v2))
{
graph[v2] =
new
List<
int
>();
}
graph[v1].Add(v2);
graph[v2].Add(v1);
}
List<
int
> maximalIndependentSet = GraphSets(graph);
foreach
(
int
i
in
maximalIndependentSet)
{
Console.Write(i +
" "
);
}
}
}