using System.Collections.Generic;
public static void Main()
List<Tuple<List<int[]>,int,int>> tests = new List<Tuple<List<int[]>,int,int>>();
tests.Add(new Tuple<List<int[]>,int,int>( new List<int[]>(){
new int[2]{5,7}}, 1, 6));
tests.Add(new Tuple<List<int[]>,int,int>( new List<int[]>(){
new int[2]{7,8}}, 1, 8));
foreach(var test in tests)
int result = GetMinNoStops(test.Item1, test.Item2, test.Item3);
Console.WriteLine("--------> Minimum number of stops from {0} to {1} is: {2} <-------",test.Item2,test.Item3, result);
public static int GetMinNoStops(List<int[]> graph, int start, int end)
Dictionary<int,List<int>> map = new Dictionary<int,List<int>>();
HashSet<int> visited = new HashSet<int>();
foreach(var pair in graph)
if (!map.ContainsKey(pair[0]))
map.Add(pair[0], new List<int>(){pair[1]});
map[pair[0]].Add(pair[1]);
if (!map.ContainsKey(pair[1]))
map.Add(pair[1], new List<int>(){pair[0]});
map[pair[1]].Add(pair[0]);
Console.WriteLine("[{0}|{1}]", pair.Key, string.Join(",", pair.Value));
return minStops(map, start, end, visited, 0);
private static int minStops(Dictionary<int,List<int>> map, int i, int end, HashSet<int> visited, int distance)
int minDistance = Int32.MaxValue;
List<int> edges = map[i];
foreach(var node in edges)
if (!visited.Contains(node))
int currDistance = minStops(map, node, end, visited, distance + 1);
minDistance = Math.Min(minDistance, currDistance);