Example #1
0
        // Perform a depth first search on a graph with n nodes.
        // This impl returns number of connected nodes starting from firstNode
        public static int DFS(T[] nodes, Common.Edge <T, W>[] dependencies, T firstNode)
        {
            // Example
            // nodes = { a, b, c, d, e, f }
            // dependencies = { {a, d}, {f, b}, {b, d}, {f, a}, {d, c}}

            // DFS(params, a).Count == 5 // connected to 5 other nodes
            // DFS(params, e).Count == 1 // not connected to any other nodes

            var graph = new Common.Graph <T, W>();

            for (int i = 0; i < dependencies.Length; i++)
            {
                graph.AddDirectedEdge(dependencies[i]);
            }

            var numberOfNodes = nodes.Length;

            // initialize visited list/map
            var visited = new Dictionary <T, bool>();

            foreach (var p in nodes)
            {
                visited[p] = false;
            }

            var count = 0;

            // non recursive solution, here we use Stack

            var stack = new pv.Common.Stack <T>();            // lets use homemade stack

            stack.Push(firstNode);
            visited[firstNode] = true;

            while (!stack.IsEmpty())
            {
                var node = stack.Pop();
                // famous doSomething :) e.g. count++
                count++;

                if (graph.ContainsKey(node))
                {
                    foreach (var edge in graph[node])
                    {
                        if (!visited[edge.To])
                        {
                            stack.Push(edge.To);
                            visited[edge.To] = true;
                        }
                    }
                }
            }

            return(count);
        }
Example #2
0
        private static void execDFS(string project, HashSet <string> visited, List <string> visitedNodes, Common.Graph <string, int> graph)
        {
            visited.Add(project);

            var edges = graph.ContainsKey(project) ? graph[project] : null;

            if (edges != null)
            {
                foreach (var edge in edges)
                {
                    if (!visited.Contains(edge.To))
                    {
                        execDFS(edge.To, visited, visitedNodes, graph);
                    }
                }
            }
            visitedNodes.Add(project);
        }