예제 #1
0
파일: Graph.cs 프로젝트: albertk78/Study
        private void Visit(GraphNode current, Dictionary<object, IList<object>> serializer)
        {
            var currentDependencies = new List<object>();

            //since dependent node could depend on other node, do depth first
            if (current.Edges.Count > 0)
            {
                foreach (var node in current.Edges)
                {
                    //only if we have not seriazlied this node already
                    if (!serializer.ContainsKey(node.Value))
                    {
                        //visit edges
                        Visit(node, serializer);
                    }

                    //add this node as a dependency
                    currentDependencies.Add(node.Value);

                    //see if this depends on others
                    var dependencyDepencies = serializer[node.Value];
                    if (dependencyDepencies.Count > 0)
                    {
                        currentDependencies.AddRange(dependencyDepencies);
                    }
                }
            }

            //serialize current node
            serializer.Add(current.Value, currentDependencies.Distinct().ToList());
        }
예제 #2
0
파일: Graph.cs 프로젝트: albertk78/Study
        private bool Check(GraphNode current, Dictionary<object, bool> tracker)
        {
            //check if current node is already being checked, which means we have a circular dependency
            if (tracker.ContainsKey(current.Value) && tracker[current.Value])
            {
                return true;
            }

            //mark as currently checking
            tracker[current.Value] = true;

            //check childfren
            foreach (var node in current.Edges)
            {
                if (Check(node, tracker))
                {
                    return true;
                }
            }

            //mark as done visiting
            tracker[current.Value] = false;

            return false;
        }