Пример #1
0
        } // GetNodeToDrop

        static List <int> MakeOneMissing(MaxCliqueGraph graph, List <int> clique)
        {
            int        count;
            List <int> result = new List <int>();

            for (int i = 0; i < graph.NumberNodes; ++i)
            {
                if (graph.NumberNeighbors(i) < clique.Count - 1)
                {
                    continue;
                }
                if (clique.BinarySearch(i) >= 0)
                {
                    continue;
                }

                count = 0;
                for (int j = 0; j < clique.Count; ++j)
                {
                    if (graph.AreAdjacent(i, clique[j]))
                    {
                        ++count;
                    }
                }
                if (count == clique.Count - 1)
                {
                    result.Add(i);
                }
            }
            return(result);
        }
Пример #2
0
 static bool FormsALargerClique(MaxCliqueGraph graph, List <int> clique, int node)
 {
     for (int i = 0; i < clique.Count; ++i)
     {
         if (graph.AreAdjacent(clique[i], node) == false)
         {
             return(false);
         }
     }
     return(true);
 }
Пример #3
0
        static int GetNodeToDrop(MaxCliqueGraph graph, List <int> clique, List <int> oneMissing)
        {
            if (clique.Count == 1)
            {
                return(clique[0]);
            }

            List <int> cliqueCountNotAdjacent = new List <int>(clique.Count);

            int maxCount = 0;

            for (int i = 0; i < clique.Count; ++i)
            {
                int currCliqueNode   = clique[i];
                int countNotAdjacent = 0;
                for (int j = 0; j < oneMissing.Count; ++j)
                {
                    int currOneMissingNode = oneMissing[j];
                    if (graph.AreAdjacent(currCliqueNode,
                                          currOneMissingNode) == false)
                    {
                        ++countNotAdjacent;
                    }
                }
                cliqueCountNotAdjacent.Add(countNotAdjacent);
                if (countNotAdjacent > maxCount)
                {
                    maxCount = countNotAdjacent;
                }
            }

            List <int> candidates = new List <int>();

            for (int i = 0; i < clique.Count; ++i)
            {
                if (cliqueCountNotAdjacent[i] == maxCount)
                {
                    candidates.Add(clique[i]);
                }
            }

            if (candidates.Count == 1)
            {
                return(candidates[0]);
            }

            return(candidates[random.Next(0, candidates.Count)]);
        } // GetNodeToDrop
Пример #4
0
        static int GetNodeToAdd(MaxCliqueGraph graph, List <int> allowedAdd, List <int> possibleAdd)
        {
            if (allowedAdd.Count == 1)
            {
                return(allowedAdd[0]);
            }

            List <int> allowedAddDegree = new List <int>(possibleAdd.Count);

            int maxDegree = 0;

            for (int i = 0; i < allowedAdd.Count; ++i)
            {
                int currNode            = allowedAdd[i];
                int degreeOfCurrentNode = 0;
                for (int j = 0; j < possibleAdd.Count; ++j)
                {
                    int otherNode = possibleAdd[j];
                    if (graph.AreAdjacent(currNode, otherNode) == true)
                    {
                        ++degreeOfCurrentNode;
                    }
                }

                allowedAddDegree.Add(degreeOfCurrentNode);
                if (degreeOfCurrentNode > maxDegree)
                {
                    maxDegree = degreeOfCurrentNode;
                }
            }

            List <int> candidates = new List <int>();

            for (int i = 0; i < allowedAdd.Count; ++i)
            {
                if (allowedAddDegree[i] == maxDegree)
                {
                    candidates.Add(possibleAdd[i]);
                }
            }

            if (candidates.Count == 1)
            {
                return(candidates[0]);
            }

            return(candidates[random.Next(0, candidates.Count)]);
        }
Пример #5
0
        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("\nBegin greedy maximum clique demo\n");
                Console.WriteLine("\nBegin tabu algorithm maximum clique demo\n");

                string graphFile = "..\\..\\DimacsGraph.clq";                   //string graphFile = "DimacsGraph.clq";
                MaxCliqueGraph.ValidateGraphFile(graphFile, FileFormat.DIMACS); //"DIMACS"

                MaxCliqueGraph graph = new MaxCliqueGraph(graphFile, FileFormat.DIMACS);

                graph.ValidateGraph();

                Console.WriteLine("Loading graph into memory");
                Console.WriteLine("Graph loaded and validated\n");

                int maxTime          = 50; // В настоящей задаче о максимальной клике maxTime обычно находится в диапазоне от 1000 до 100 000.
                int targetCliqueSize = graph.NumberNodes;

                List <int> maxClique = FindMaxClique(graph, maxTime, targetCliqueSize);
                Console.WriteLine("\nMaximum time reached");
                Console.WriteLine("\nSize of best clique found = " + maxClique.Count);

                Console.WriteLine("\nBest clique found:");
                Console.WriteLine(ListAsString(maxClique));

                Console.WriteLine("\nEnd greedy maximum clique demo\n");

                Console.WriteLine(graph.ToString());

                Console.WriteLine("\nAre nodes 5 and 8 adjacent? " +
                                  graph.AreAdjacent(5, 8));
                Console.WriteLine("Number neighbors of node 4 = " +
                                  graph.NumberNeighbors(4));

                WriteFile(ListAsString(maxClique), "output.txt");

                string outputGraphFile = "..\\..\\output.txt";
                graph.SaveTxtFormatGraph(outputGraphFile);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Fatal: " + ex.Message);
            }
        } // Main