public static IEnumerable<IEnumerable<Vertex>> BFS(Graph graph, HashSet<Vertex> visited, Vertex end, Vertex expand) { if (graph.Terminals.All(visited.Contains) || visited.Count == graph.NumberOfVertices - 1) yield break; List<Vertex> nodes = graph.GetNeighboursForVertex(expand); //for (int i = 0; i < nodes.Count; i++ ) //{ // foreach (var combination in new Combinations<Vertex>(nodes, i + 1)) // { // if (combination.Any(x => visited.Contains(x))) // continue; // if (combination.Contains(end)) // { // foreach (var n in combination) // visited.Add(n); // foreach (var n in combination) // foreach (var bfs in BFS(graph, visited, end, n)) // yield return bfs; // foreach (var n in combination) // visited.Remove(n); // break; // } // } //} foreach (var node in nodes) { if (visited.Contains(node)) continue; if (node == end) { visited.Add(node); yield return visited; visited.Remove(node); break; } } //for (int i = 0; i < nodes.Count; i++) //{ // if (visited.Count + i == graph.NumberOfVertices - 1) // break; // Adding i+1 results in all nodes, which is useless. // foreach (var combination in new Combinations<Vertex>(nodes, i + 1)) // { // if (combination.Any(visited.Contains)) // || combination.Contains(end)) // continue; // foreach (var n in combination) // visited.Add(n); // foreach (var n in combination) // foreach (var bfs in BFS(graph, visited, end, n)) // yield return bfs; // foreach (var n in combination) // visited.Remove(n); // } //} foreach (var node in nodes) { if (visited.Contains(node) || node == end) continue; visited.Add(node); foreach (var bfs in BFS(graph, visited, end, node)) yield return bfs; visited.Remove(node); } }
public static IEnumerable<IEnumerable<Vertex>> BFS2(Graph graph, List<Vertex> visited) { List<Vertex> nodes = graph.GetNeighboursForVertex(visited.Last()); var visitedHash = new HashSet<Vertex>(visited); foreach (var node in nodes) { if (visitedHash.Contains(node)) continue; //if (node == end) { visited.Add(node); yield return visited; visited.RemoveAt(visited.Count - 1); //break; } } foreach (var node in nodes) { if (visitedHash.Contains(node)) continue; visited.Add(node); foreach (var bfs in BFS2(graph, visited)) yield return bfs; visited.RemoveAt(visited.Count - 1); } }
public static IEnumerable<IEnumerable<Vertex>> GetAllConnectedCombinations(Graph graph, Vertex start) { var visited = new HashSet<Vertex>(); var terminals = new HashSet<Vertex>(graph.Terminals); var stack = new Stack<Vertex>(); Dictionary<Vertex, Vertex> childParentDictionary = new Dictionary<Vertex, Vertex>(); stack.Push(start); childParentDictionary.Add(start, null); while (stack.Count != 0) { var current = stack.Pop(); if (!visited.Add(current)) continue; var neighbours = graph.GetNeighboursForVertex(current) .Where(n => !visited.Contains(n)) .ToList(); // Travel left to right neighbours.Reverse(); foreach (var neighbour in neighbours) { stack.Push(neighbour); childParentDictionary.Add(neighbour, current); } if (!neighbours.Any() || neighbours.All(visited.Contains)) { List<Vertex> vertices = new List<Vertex>(); Vertex parentNode = current; do { vertices.Add(parentNode); } while ((parentNode = childParentDictionary[parentNode]) != null); yield return vertices; } } }