Efficient search for generic objects without predecessors

101 Views Asked by At

I have a genObjectList of generic objects and an openList of generic objects which still needs to be used. Some of the generic objects depend on each other. So only generic objects where all of its predecessors were already used could be used. The implementation I use is the following:

var genObjectList = // get the generic objects from somewhere
var openList = new List<GenObject>(genObjectList);
var closedList = new List<GenObject>();
var rand = new Random();

while (openList.Count != 0)
{
                var candidateList =
                    openList.FindAll(
                        t => t.Predecessors.Where(p => genObjectList.Contains(p)).Intersect(closedList).Count() == t.Predecessors.Count(p => genObjectList.Contains(p)));

                var choosenGenObject = candidateList[rand.Next(0, candidateList.Count)];

                openList.Remove(choosenGenObject);
                closedList.Add(choosenGenObject);

    // Do something with the generic object
}

The part to find the candidateList seems to be very inefficient even if the genObjectList contains only 300 entries.

I'm looking for a better implementation to achieve the same behaviour. Any ideas?

Edit: To clarify the misleading use of Task: The question is not about c# tasks and their methods, but on generic objects which have precedence constraints and how to handle them. I edited the post to make this clear.

Edit2: The advise to use topological sorting is the right way. However I'm looking for sth. which could be described as 'non deterministic topological sorting'. With the depth first search implementation from e.g. QuickGraph one always gets the same topological sorting even if it's not unique. I'm looking for an implementation which uses a random number as tie-breaker and thus has the chance to create all topological sorting's.

0

There are 0 best solutions below