Dictionary<int, List<int>> locationChoices = new Dictionary<int, List<int>>();
locationChoices[23]={4,5,6}
locationChoices[4]= {8,15,14}
locationChoices[14]= {29,30,20}
locationChoices[30]={23,17}
locationChoices[5] ={1, 9, 23}
Please look at the above data stored in Dictionary<int, List<int>> locationChoices. integer value represents work locations. The person at location 23 want to shift to location 4 or 5 or 6. Similarly the person at location 4 , wants to move to location 8 or 15 or 14. i need to identify their all mutual choices of any level .
i wrote a program that consider each key in locationChoices as a rootLocation and find their respective mutual for example , if we consider location 23 as rootLocation then algorithm finds its two possible mutuals 23 -> 4 -> 14 ->30 ->23 and 23->5->23 respectively.So far, it is producing correct result. But it's not well written , and i want you to help me to improve it and find the time complexity of this program ..
Dictionary<int, List<int>> locationChoices = new Dictionary<int, List<int>>();
locationChoices[23]={4,5,6};
locationChoices[4]= {8,15,14};
locationChoices[14]= {29,30,20};
locationChoices[30]={23,17};
locationChoices[5] ={1, 9, 23};
List<List<int>> mutualList = new List<List<int>>();
List<int> tempList = new List<int>();
List<int> alreadyProcessed = new List<int>();
int rootLocation=0;
foreach(var key in locationChoices.Keys)
{
rootLocation=key;
tempList.Clear();
alreadyProcessed.Clear();
GetMutual(key);
}
public void GetMutual(int location)
{
tempList.Add(location);
List<int> choicesList;
if (locationChoices.ContainsKey(location))
{
choicesList = locationChoices[location];
foreach (int choice in choicesList)
{
if (choice != rootLocation && (tempList.Contains(choice) == false) && (alreadyProcessed.Contains(choice) == false))
GetMutual(choice);
if (choice == rootLocation)
{
List<int> cloneList = new List<int>(tempList);
mutualList.Add(cloneList);
}
}
}
alreadyProcessed.Add(location);
tempList.RemoveAt(tempList.Count - 1);
}
