public List <List <HapticLocationNode> > BFS(AreaFlag beginNode, int maxDepth) { maxDepth = Math.Max(0, maxDepth); List <List <HapticLocationNode> > stages = new List <List <HapticLocationNode> >(); HapticLocationNode source = _nodes[beginNode]; Dictionary <HapticLocationNode, bool> visited = new Dictionary <HapticLocationNode, bool>(); foreach (var n in _nodes) { visited[n.Value] = false; } Queue <HapticLocationNode> queue = new Queue <HapticLocationNode>(); int currentDepth = 0; int elementsToDepthIncrease = 1; int nextElementsToDepthIncrease = 0; visited[source] = true; queue.Enqueue(source); stages.Add(new List <HapticLocationNode>() { source }); List <HapticLocationNode> potentialNextStage = new List <HapticLocationNode>(); while (queue.Count != 0) { source = queue.Dequeue(); var neighbors = Neighbors(source); foreach (var neighbor in neighbors) { if (!visited[neighbor]) { visited[neighbor] = true; queue.Enqueue(neighbor); potentialNextStage.Add(neighbor); nextElementsToDepthIncrease++; } } if (--elementsToDepthIncrease == 0) { if (potentialNextStage.Count > 0) { stages.Add(new List <HapticLocationNode>(potentialNextStage)); } if (++currentDepth == maxDepth) { return(stages); } elementsToDepthIncrease = nextElementsToDepthIncrease; nextElementsToDepthIncrease = 0; potentialNextStage.Clear(); } } return(stages); }
private void InsertEdge(HapticLocationNode nodeA, HapticLocationNode nodeB, int weight) { if (!_graph[nodeA].Contains(nodeB)) { _graph[nodeA].Add(nodeB); } if (!_graph[nodeB].Contains(nodeA)) { _graph[nodeB].Add(nodeA); } _weights[nodeA][nodeB] = weight; _weights[nodeB][nodeA] = weight; }
private HapticLocationNode minDist(IList <HapticLocationNode> stack, Dictionary <HapticLocationNode, float> dist) { HapticLocationNode best = null; float bestDist = float.PositiveInfinity; foreach (var node in stack) { if (dist[node] < bestDist) { best = node; bestDist = dist[node]; } } return(best); }
private List <HapticLocationNode> Neighbors(HapticLocationNode node) { return(_graph[node]); }
private int Cost(HapticLocationNode a, HapticLocationNode b) { return(_weights[a][b]); }