using
System;
using
System.Collections.Generic;
public
class
Graph {
int
V;
List<
int
>[] adjList;
public
Graph(
int
v)
{
V = v;
adjList =
new
List<
int
>[ V ];
}
public
void
addEdge(
int
u,
int
v,
bool
bidir =
true
)
{
adjList[u].Add(v);
if
(bidir) {
adjList[u].Add(v);
}
}
public
int
dfs(
int
src)
{
Dictionary<
int
,
bool
> visited
=
new
Dictionary<
int
,
bool
>();
List<
int
> dependent =
new
List<
int
>();
int
count = 0;
Stack<
int
> s =
new
Stack<
int
>();
s.Push(src);
visited.Add(src,
true
);
while
(s.Count != 0) {
int
n = s.Pop();
foreach
(
var
i
in
adjList[n])
{
if
(visited.ContainsKey(i) ==
false
) {
dependent.Add(i + 1);
count++;
visited.Add(i,
true
);
s.Push(i);
}
}
}
if
(count == 0) {
Console.WriteLine(
"Vertex "
+ (src + 1)
+
" is not dependent on any vertex."
);
return
count;
}
Console.Write(
"Vertex "
+ (src + 1)
+
" dependency "
);
foreach
(
var
i
in
dependent)
{
Console.Write(
"-> "
+ i);
}
Console.WriteLine();
return
count;
}
}
public
void
operations(
int
[, ] arr,
int
n,
int
m)
{
Graph g =
new
Graph(n);
for
(
int
i = 0; i < m; i++) {
g.addEdge(arr[i, 0], arr[i, 1],
false
);
}
int
ans =
int
.MaxValue;
int
node = 0;
for
(
int
i = 0; i < n; i++) {
int
c = g.dfs(i);
if
(c < ans) {
ans = c;
node = i + 1;
}
}
Console.WriteLine(
"Node "
+ node
+
"has minimum dependency of "
+ ans);
}
public
static
void
Main()
{
int
n, m;
n = 6;
m = 6;
int
[, ] arr
=
new
int
[, ] { { 0, 1 }, { 0, 2 }, { 2, 3 },
{ 4, 5 }, { 3, 4 }, { 1, 5 } };
operations(arr, n, m);
}