コード例 #1
0
ファイル: DFS.cs プロジェクト: Miltt/Console
        private static void SearchInternal(Graph graph, IVertex vertex, GraphSearchResult result)
        {
            result.MarkAsVisited(vertex);

            foreach (var edge in vertex.Edges)
            {
                if (!result.IsVisited(edge.U))
                {
                    SearchInternal(graph, edge.U, result);
                }
            }
        }
コード例 #2
0
ファイル: DFS.cs プロジェクト: Miltt/Console
        public static GraphSearchResult Search(Graph graph, IVertex vertex)
        {
            if (graph is null)
            {
                throw new ArgumentNullException(nameof(graph));
            }
            if (vertex is null)
            {
                throw new ArgumentNullException(nameof(vertex));
            }

            var result = new GraphSearchResult(graph.Count);

            SearchInternal(graph, vertex, result);
            return(result);
        }
コード例 #3
0
ファイル: BFS.cs プロジェクト: Miltt/Console
        public static GraphSearchResult Search(Graph graph, IVertex source, IVertex target)
        {
            if (graph is null)
            {
                throw new ArgumentNullException(nameof(graph));
            }
            if (source is null)
            {
                throw new ArgumentNullException(nameof(source));
            }
            if (target is null)
            {
                throw new ArgumentNullException(nameof(target));
            }

            var result = new GraphSearchResult(graph.Count);
            var queue  = new Queue <IVertex>(graph.Count);

            result.MarkAsVisited(source);
            queue.Enqueue(source);

            while (queue.Count > 0)
            {
                var vertex = queue.Dequeue();
                if (vertex == target)
                {
                    break;
                }

                foreach (var edge in vertex.Edges)
                {
                    if (!result.IsVisited(edge.U))
                    {
                        result.MarkAsVisited(edge.U);
                        queue.Enqueue(edge.U);
                    }
                }
            }

            return(result);
        }