示例#1
0
        private static bool HamiltonCycleUtil(EulerGraph graph, int[] cycle, int position)
        {
            if (position == cycle.Length)
            {
                if (graph.IsEdgeExists(cycle[0], cycle[position - 1]))
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            }

            for (int i = 1; i < graph._numberOfVertices; i++)
            {
                if (IsSafe(i, position, cycle, graph))
                {
                    cycle[position] = i;
                    if (HamiltonCycleUtil(graph, cycle, position + 1))
                    {
                        return(true);
                    }
                    cycle[position] = -1;
                }
            }

            return(false);
        }
示例#2
0
        private static int HamiltonAllCycles(EulerGraph graph, int[] cycle, int position, ref int numberOfCycles, bool print = false)
        {
            if (position == cycle.Length)
            {
                if (graph.IsEdgeExists(cycle[0], cycle[position - 1]))
                {
                    return(1);
                }
            }

            for (int i = 1; i < graph._numberOfVertices; i++)
            {
                if (IsSafe(i, position, cycle, graph))
                {
                    cycle[position] = i;
                    if (HamiltonAllCycles(graph, cycle, position + 1, ref numberOfCycles, print) == 1)
                    {
                        numberOfCycles++;
                        if (print)
                        {
                            Console.Write(numberOfCycles + ": ");
                            foreach (var item in cycle)
                            {
                                Console.Write(item + " ");
                            }
                            Console.WriteLine();
                        }
                    }
                    cycle[position] = -1;
                }
            }

            return(0);
        }
示例#3
0
        private static bool IsSafe(int v, int position, int[] cycle, EulerGraph graph)
        {
            if (!graph.IsEdgeExists(v, cycle[position - 1]))
            {
                return(false);
            }
            foreach (var vertex in cycle)
            {
                if (vertex == v)
                {
                    return(false);
                }
            }

            return(true);
        }