コード例 #1
0
        // TODO: Move these code to a statcic class EulerianCycleTestHelper

        /**************************************************************************
        *
        *  The code below is solely for testing correctness of the data type.
        *
        **************************************************************************/

        // Determines whether a graph has an Eulerian cycle using necessary
        // and sufficient conditions (without computing the cycle itself):
        //    - at least one edge
        //    - degree(v) is even for every vertex v
        //    - the graph is connected (ignoring isolated vertices)
        private static bool hasEulerianCycle(Graph G)
        {
            // Condition 0: at least 1 edge
            if (G.E == 0)
            {
                return(false);
            }

            // Condition 1: degree(v) is even for every vertex
            for (int v = 0; v < G.V; v++)
            {
                if (G.Degree(v) % 2 != 0)
                {
                    return(false);
                }
            }

            // Condition 2: graph is connected, ignoring isolated vertices
            int s = nonIsolatedVertex(G);
            BreadthFirstPaths bfs = new BreadthFirstPaths(G, s);

            for (int v = 0; v < G.V; v++)
            {
                if (G.Degree(v) > 0 && !bfs.HasPathTo(v))
                {
                    return(false);
                }
            }

            return(true);
        }
コード例 #2
0
ファイル: BreadthFirstPaths.cs プロジェクト: zzhi/Algs4Net
        public static void MainTest(string[] args)
        {
            TextInput input = new TextInput(args[0]);
            Graph     G     = new Graph(input);
            int       s     = int.Parse(args[1]);

            BreadthFirstPaths bfs = new BreadthFirstPaths(G, s);

            for (int v = 0; v < G.V; v++)
            {
                if (bfs.HasPathTo(v))
                {
                    Console.Write("{0} to {1} ({2}):  ", s, v, bfs.DistTo(v));
                    foreach (int x in bfs.PathTo(v))
                    {
                        if (x == s)
                        {
                            Console.Write(x);
                        }
                        else
                        {
                            Console.Write("-" + x);
                        }
                    }
                    Console.WriteLine();
                }

                else
                {
                    Console.Write("{0} to {1} (-):  not connected\n", s, v);
                }
            }
        }
コード例 #3
0
ファイル: DirectedEulerianPath.cs プロジェクト: zzhi/Algs4Net
        /**************************************************************************
        *
        *  The code below is solely for testing correctness of the data type.
        *
        **************************************************************************/

        // Determines whether a digraph has an Eulerian path using necessary
        // and sufficient conditions (without computing the path itself):
        //    - indegree(v) = outdegree(v) for every vertex,
        //      except one vertex v may have outdegree(v) = indegree(v) + 1
        //      (and one vertex v may have indegree(v) = outdegree(v) + 1)
        //    - the graph is connected, when viewed as an undirected graph
        //      (ignoring isolated vertices)
        // This method is solely for unit testing.
        private static bool hasEulerianPath(Digraph G)
        {
            if (G.E == 0)
            {
                return(true);
            }

            // Condition 1: indegree(v) == outdegree(v) for every vertex,
            // except one vertex may have outdegree(v) = indegree(v) + 1
            int deficit = 0;

            for (int v = 0; v < G.V; v++)
            {
                if (G.Outdegree(v) > G.Indegree(v))
                {
                    deficit += (G.Outdegree(v) - G.Indegree(v));
                }
            }
            if (deficit > 1)
            {
                return(false);
            }

            // Condition 2: graph is connected, ignoring isolated vertices
            Graph H = new Graph(G.V);

            for (int v = 0; v < G.V; v++)
            {
                foreach (int w in G.Adj(v))
                {
                    H.AddEdge(v, w);
                }
            }

            // check that all non-isolated vertices are connected
            int s = nonIsolatedVertex(G);
            BreadthFirstPaths bfs = new BreadthFirstPaths(H, s);

            for (int v = 0; v < G.V; v++)
            {
                if (H.Degree(v) > 0 && !bfs.HasPathTo(v))
                {
                    return(false);
                }
            }

            return(true);
        }
コード例 #4
0
        public static void MainTest(string[] args)
        {
            string filename  = args[0];
            string delimiter = args[1];
            string source    = args[2];

            SymbolGraph sg = new SymbolGraph(filename, delimiter);
            Graph       G  = sg.G;

            if (!sg.Contains(source))
            {
                Console.WriteLine(source + " not in database.");
                return;
            }

            int s = sg.Index(source);
            BreadthFirstPaths bfs = new BreadthFirstPaths(G, s);

            TextInput StdIn = new TextInput();

            while (!StdIn.IsEmpty)
            {
                string sink = StdIn.ReadLine();
                if (sg.Contains(sink))
                {
                    int t = sg.Index(sink);
                    if (bfs.HasPathTo(t))
                    {
                        foreach (int v in bfs.PathTo(t))
                        {
                            Console.WriteLine("   " + sg.Name(v));
                        }
                    }
                    else
                    {
                        Console.WriteLine("Not connected");
                    }
                }
                else
                {
                    Console.WriteLine("   Not in database.");
                }
            }
        }
コード例 #5
0
        /**************************************************************************
        *
        *  The code below is solely for testing correctness of the data type.
        *
        **************************************************************************/

        // Determines whether a digraph has an Eulerian cycle using necessary
        // and sufficient conditions (without computing the cycle itself):
        //    - at least one edge
        //    - indegree(v) = outdegree(v) for every vertex
        //    - the graph is connected, when viewed as an undirected graph
        //      (ignoring isolated vertices)
        private static bool hasEulerianCycle(Digraph G)
        {
            // Condition 0: at least 1 edge
            if (G.E == 0)
            {
                return(false);
            }

            // Condition 1: indegree(v) == outdegree(v) for every vertex
            for (int v = 0; v < G.V; v++)
            {
                if (G.Outdegree(v) != G.Indegree(v))
                {
                    return(false);
                }
            }

            // Condition 2: graph is connected, ignoring isolated vertices
            Graph H = new Graph(G.V);

            for (int v = 0; v < G.V; v++)
            {
                foreach (int w in G.Adj(v))
                {
                    H.AddEdge(v, w);
                }
            }

            // check that all non-isolated vertices are conneted
            int s = nonIsolatedVertex(G);
            BreadthFirstPaths bfs = new BreadthFirstPaths(H, s);

            for (int v = 0; v < G.V; v++)
            {
                if (H.Degree(v) > 0 && !bfs.HasPathTo(v))
                {
                    return(false);
                }
            }

            return(true);
        }
コード例 #6
0
        // TODO: Move these code to a statcic class EulerianCycleTestHelper

        /**************************************************************************
        *
        *  The code below is solely for testing correctness of the data type.
        *
        **************************************************************************/

        // Determines whether a graph has an Eulerian path using necessary
        // and sufficient conditions (without computing the path itself):
        //    - degree(v) is even for every vertex, except for possibly two
        //    - the graph is connected (ignoring isolated vertices)
        // This method is solely for unit testing.
        private static bool hasEulerianPath(Graph G)
        {
            if (G.E == 0)
            {
                return(true);
            }

            // Condition 1: degree(v) is even except for possibly two
            int oddDegreeVertices = 0;

            for (int v = 0; v < G.V; v++)
            {
                if (G.Degree(v) % 2 != 0)
                {
                    oddDegreeVertices++;
                }
            }
            if (oddDegreeVertices > 2)
            {
                return(false);
            }

            // Condition 2: graph is connected, ignoring isolated vertices
            int s = nonIsolatedVertex(G);
            BreadthFirstPaths bfs = new BreadthFirstPaths(G, s);

            for (int v = 0; v < G.V; v++)
            {
                if (G.Degree(v) > 0 && !bfs.HasPathTo(v))
                {
                    return(false);
                }
            }

            return(true);
        }