Пример #1
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);
        }
Пример #2
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);
        }
Пример #3
0
        public static void CreateReportFromFile()
        {
            int[][] matrix = File.ReadAllLines("a.txt")
                             .Select(l => l.Split(' ').Select(i => int.Parse(i)).ToArray())
                             .ToArray();
            var graph = new EulerGraph(matrix.Length);

            for (int i = 0; i < matrix.Length; i++)
            {
                for (int j = 0; j < i; j++)
                {
                    if (matrix[i][j] != 0)
                    {
                        graph.AddEdge(i, j);
                    }
                }
            }
            var cycle = new int[graph._numberOfVertices];

            for (int i = 0; i < cycle.Length; i++)
            {
                cycle[i] = -1;
            }

            Console.WriteLine($"All Hamilton cycles");
            HamiltonCycle.FindAllHamiltonCycles(graph, cycle, true);

            Console.WriteLine($"Euler cycle: ");
            var eulerCycle = EulerCycle.FindEulerCycle(graph, 0);

            foreach (var item in eulerCycle)
            {
                Console.Write(item + " ");
            }
        }
Пример #4
0
        public static List <int> FindEulerCycle(EulerGraph graph, int start)
        {
            var cycle  = new Stack <int>();
            var stack  = new Stack <int>();
            var vertex = start;

            stack.Push(vertex);

            while (stack.Count > 0)
            {
                var neighbours = graph.GetNeighbours(vertex);
                if (neighbours.Count == 0)
                {
                    cycle.Push(stack.Pop());
                    if (stack.Count == 0)
                    {
                        break;
                    }
                    vertex = stack.Peek();
                    continue;
                }
                var minVertex = neighbours.Min();
                graph.RemoveEdge(vertex, minVertex);
                stack.Push(minVertex);
                vertex = minVertex;
            }

            return(cycle.ToList());
        }
Пример #5
0
 public static void CreateRaport(int start, int step, int numberOfSteps, double saturation, string typeOfRaport)
 {
     using (StreamWriter output = File.CreateText(RESULTS_DIRECTORY + typeOfRaport + (saturation * 100).ToString() + DateTime.Now.ToString() + ".csv"))
         using (CsvWriter csv = new CsvWriter(output, System.Globalization.CultureInfo.CurrentCulture))
         {
             csv.Configuration.NewLine   = NewLine.LF;
             csv.Configuration.Delimiter = ",";
             if (typeOfRaport == FIRST)
             {
                 csv.WriteHeader(typeof(FirstExcerciseResults));
                 for (int i = 0; i < numberOfSteps; i++)
                 {
                     var stopwatch = new Stopwatch();
                     stopwatch.Start();
                     var hamiltonTimes = new int[11];
                     for (int j = 0; j < 11; j++)
                     {
                         var graphHamilton = new EulerGraph(start + (i * step));
                         graphHamilton.MeetSaturationGoal(saturation);
                         hamiltonTimes[j] = graphHamilton.MeasureTime(EulerGraph.HAMILTON_CYCLE);
                     }
                     Array.Sort(hamiltonTimes);
                     Console.WriteLine(string.Join(", ", hamiltonTimes));
                     var graph = new EulerGraph(start + (i * step));
                     graph.MeetSaturationGoal(saturation);
                     var result = new FirstExcerciseResults
                     {
                         NumberOfVertices = start + (i * step),
                         HamiltonCycle    = hamiltonTimes[5], // Median from 11 results
                         EulerCycle       = graph.MeasureTime(EulerGraph.EULER_CYCLE),
                     };
                     csv.NextRecord();
                     csv.WriteRecord(result);
                     stopwatch.Stop();
                     Console.WriteLine(stopwatch.Elapsed);
                 }
             }
             else if (typeOfRaport == SECOND)
             {
                 csv.WriteHeader(typeof(SecondExcerciseResults));
                 for (int i = 0; i < numberOfSteps; i++)
                 {
                     var stopwatch = new Stopwatch();
                     stopwatch.Start();
                     var graph = new EulerGraph(start + (i * step));
                     graph.MeetSaturationGoal(saturation);
                     var result = new SecondExcerciseResults
                     {
                         NumberOfVertices = start + (i * step),
                         Time             = graph.MeasureTime(EulerGraph.ALL_HAMILTON_CYCLES),
                     };
                     csv.NextRecord();
                     csv.WriteRecord(result);
                     stopwatch.Stop();
                     Console.WriteLine(stopwatch.Elapsed);
                 }
             }
         }
 }
Пример #6
0
        public static int FindAllHamiltonCycles(EulerGraph graph, int[] cycle, bool print = false)
        {
            cycle[0] = 0;
            int numberOfCycles = 0;

            HamiltonAllCycles(graph, cycle, 1, ref numberOfCycles, print);
            return(numberOfCycles);
        }
Пример #7
0
 /// <summary>
 /// Values in a cycle should be different than any vertex in a given graph
 /// </summary>
 public static bool FindHamiltonCycle(EulerGraph graph, int[] cycle)
 {
     cycle[0] = 0;
     if (!HamiltonCycleUtil(graph, cycle, 1))
     {
         return(false);
     }
     return(true);
 }
Пример #8
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);
        }