예제 #1
0
파일: Graph.cs 프로젝트: AlexL70/Algorithms
        public void DepthFirstSearch(TKey key, ProcessVertex preProcess = null, ProcessVertex postProcess = null)
        {
            var v = GetVertex(key);

            DataStructures.Stack <Vertex> vStack = new DataStructures.Stack <Vertex>();
            v.Viewed = true;
            vStack.Push(v);
            preProcess?.Invoke(v);
            while (!vStack.IsEmpty)
            {
                bool neighboursFound = false;
                foreach (var vert in Nearest(vStack.Peek().Key).Where(vrt => !vrt.Viewed))
                {
                    neighboursFound = true;
                    preProcess?.Invoke(vert);
                    vert.Viewed = true;
                    vStack.Push(vert);
                    break;
                }
                if (!neighboursFound)
                {
                    var vert = vStack.Pop();
                    postProcess?.Invoke(vert);
                    vert.Viewed = true;
                }
            }
        }
예제 #2
0
파일: Graph.cs 프로젝트: AlexL70/Algorithms
        public void BreadthFirstSearch(TKey key, ProcessVertex preProcess = null, ProcessVertex postProcess = null)
        {
            var v = GetVertex(key);

            DataStructures.Queue <Vertex> vQueue = new DataStructures.Queue <Vertex>();
            v.Viewed = true;
            vQueue.Enqueue(v);
            preProcess?.Invoke(v);
            while (!vQueue.IsEmpty)
            {
                foreach (var vert in Nearest(vQueue.Peek().Key).Where(vrt => !vrt.Viewed))
                {
                    preProcess?.Invoke(vert);
                    vert.Viewed = true;
                    vQueue.Enqueue(vert);
                }
                v = vQueue.Dequeue();
                postProcess?.Invoke(v);
            }
        }