예제 #1
0
        public Topological(Graph.Digraph g)
        {
            DirectedCycle dc = new DirectedCycle(g);

            if (!dc.HasCycle())
            {
                order = new List <int>();

                DepthFirstOrder dfo = new DepthFirstOrder(g);

                for (int i = 0; i < dfo.ReversePost.Count; i++)
                {
                    order.Add(dfo.ReversePost.Pop());
                }
            }
        }
예제 #2
0
        /// <summary>
        /// Returns the shortest path between a given node and the first matching node in the
        /// given graph using breadth-first search. If none exists, the returned path is empty.
        /// </summary>
        /// <param name="digraph"></param>
        /// <param name="source"></param>
        /// <param name="match"></param>
        /// <returns></returns>
        public Path FindPath(Digraph <T> digraph, Node <T> source, Predicate <T> match)
        {
            var path     = new Path();
            var parents  = new Dictionary <string, string>();
            var frontier = new Queue <string>();

            parents[source.ToString()] = "";
            frontier.Enqueue(source.ToString());

            while (frontier.Count > 0)
            {
                string current = frontier.Dequeue();

                if (match(digraph[current].Data) && current != source.ToString())
                {
                    path.Extend(current);

                    while (parents[current] != "")
                    {
                        path.Extend(parents[current]);
                        current = parents[current];
                    }

                    return(path);
                }

                digraph[current].Neighbors.ForEach(n =>
                {
                    string neighbor = n.ToString();
                    if (!parents.ContainsKey(neighbor))
                    {
                        frontier.Enqueue(neighbor);
                        parents[neighbor] = current;
                    }
                });
            }

            // Uncomment to allow source-to-source path:
            //if (match(source.Data)) { path.Extend(source.ToString()); }

            return(path);
        }
        private void Bfs(Digraph g, int s)
        {
            Queue <int> queue = new Queue <int>();

            this.marked[s] = true;
            this.distTo[s] = 0;
            queue.Enqueue(s);
            while (queue.Count != 0)
            {
                int v = queue.Dequeue();
                foreach (int i in g.GetAdjacencyList(v))
                {
                    if (!marked[i])
                    {
                        this.edgeTo[i] = v;
                        this.marked[i] = true;
                        this.distTo[i] = this.distTo[v] + 1;
                        queue.Enqueue(i);
                    }
                }
            }
        }
        public static void Main(string[] args)
        {
            string fileName = args[0];

            var vAndE = FileReader.Read(fileName);

            Digraph dg = new Digraph(vAndE.Item1, vAndE.Item2);

            int s = int.Parse(args[1]);

            BreathFirstDirectedPaths bfdp = new BreathFirstDirectedPaths(dg, s);

            for (int v = 0; v < dg.V; v++)
            {
                if (bfdp.HasPathTo(v))
                {
                    Console.Write($"{s} to {v} ({bfdp.DistanceTo(v)}): ");

                    foreach (var x in bfdp.PathTo(v))
                    {
                        if (x == s)
                        {
                            Console.Write($"-> {s}");
                        }
                        else
                        {
                            Console.Write($"-> {x}");
                        }
                    }
                }
                else
                {
                    Console.Write($"{s} to {v}: not connected.");
                }
                Console.WriteLine();
            }
        }
예제 #5
0
        /// <summary>
        /// Returns a path between a source node and the first matching node in the
        /// graph using depth-first search. If none exists, the returned path is empty.
        /// The returned path is not guaranteed to be the shortest one.
        /// </summary>
        /// <param name="digraph"></param>
        /// <param name="source"></param>
        /// <param name="match"></param>
        /// <returns></returns>
        public Path FindPath(Digraph <T> digraph, Node <T> source, Predicate <T> match)
        {
            var parents = new Dictionary <string, string>();

            parents[source.ToString()] = "";
            string result = RecursiveDFS(digraph, source.ToString(), match, parents);
            Path   path   = new Path();

            if (result != "")
            {
                path.Extend(result);

                while (parents[result] != "")
                {
                    path.Extend(parents[result]);
                    result = parents[result];
                }
            }

            // Uncomment to allow source-to-source path:
            //if (match(source.Data)) { path.Extend(source.ToString()); }

            return(path);
        }
예제 #6
0
 public DirectedDFS(Digraph g, int s)
 {
     _marks = new bool[g.Vertice()];
     Search(g, s);
 }
 public DepthFirstDirectedPaths(Digraph g, int s) : base(g, s)
 {
     this.marked = new bool[g.V];
     this.edgeTo = new int[g.V];
     Dfs(g, s);
 }
예제 #8
0
 public DirectedPaths(Digraph g, int s)
 {
     this.s = s;
 }
 public NonRecursiveDirectedDepthFirstSearch(Digraph g, int s)
 {
     this.marked = new bool[g.V];
     Dfs(g, s);
 }