private static void TraceToSource(Bus FromBus, Bus BusThatStartedItAll, HashSet <Bus> busesInThisTrace, HashSet <Bus> busesOnPath) { //start at FromBus, take a step away from FromBus. //If we're at the source, add the path that we took to BusesOnRouteToSource. //If we're at a dead-end, then stop tracing this branch. //If we hit a bus that's already on the route, then terminate and add the branch. var connectedBuses = FromBus.ConnectedTo.OfType <Line>().Select(line => line.ConnectedTo.OfType <Bus>().Except(FromBus.Yield()).Single()) //all the buses neighbouring this one .Except(busesInThisTrace) //exclude any buses we've already touched in this trace ; foreach (var bus in connectedBuses) { if (busesOnPath.Contains(bus)) //we're connected to the target bus. no further processing required on this branch. { busesOnPath.UnionWith(busesInThisTrace.Except(BusThatStartedItAll.Yield())); continue; } else { //keep searching! HashSet <Bus> NextStepTraceList; if (connectedBuses.Count() == 1) //if this is the only possible way forward, then just keep using the same thingy. { NextStepTraceList = busesInThisTrace; } else { NextStepTraceList = new HashSet <Bus>(busesInThisTrace); } NextStepTraceList.Add(bus); TraceToSource(bus, BusThatStartedItAll, NextStepTraceList, busesOnPath); } } }
private static void _traceWithoutCrossingBuses(Bus FromBus, HashSet <Bus> ExcludeList, HashSet <Bus> busesInThisTrace, HashSet <Bus> busesOnPath) { var connectedBuses = FromBus.ConnectedTo.OfType <Line>().Select(line => line.ConnectedTo.OfType <Bus>().Except(FromBus.Yield()).Single()) //all the buses neighbouring this one .Except(busesInThisTrace) //exclude any buses we've already touched in this trace ; if (connectedBuses.Count() == 0) { busesOnPath.UnionWith(busesInThisTrace); return; } foreach (var bus in connectedBuses) { if (ExcludeList.Contains(bus)) //we're connected to the target bus. end the thread. { continue; } else { //keep searching! HashSet <Bus> NextStepTraceList; if (connectedBuses.Count() == 1) //if this is the only possible way forward, then just keep using the same thingy. { NextStepTraceList = busesInThisTrace; } else { NextStepTraceList = new HashSet <Bus>(busesInThisTrace); } NextStepTraceList.Add(bus); _traceWithoutCrossingBuses(bus, ExcludeList, NextStepTraceList, busesOnPath); } } }