public static bool CheckQSat(List <Quantor> pQuantors, Formula pFormular) { bool qSatCheckResult = false; if (pQuantors != null && pQuantors.Count != 0) { Quantor currentQuantor = pQuantors[0]; List <Quantor> remainingQunators = pQuantors.ToList(); remainingQunators.RemoveAt(0); Formula one = ReplaceVarialbe(pFormular, currentQuantor.Variable, true); Formula zero = ReplaceVarialbe(pFormular, currentQuantor.Variable, false); if (currentQuantor.Type == Quantor.QuantorTypes.AllQuantor) { qSatCheckResult = CheckQSat(remainingQunators, one) && CheckQSat(remainingQunators, zero); } else { qSatCheckResult = CheckQSat(remainingQunators, one) || CheckQSat(remainingQunators, zero); } } else { SatResolver satResolver = new SatResolver(); SatisfiabilityInfo satisfiableInfo = satResolver.IsSatisfiable(pFormular); qSatCheckResult = satisfiableInfo.IsSatisfiable; } return(qSatCheckResult); }
static void Main(string[] args) { var undirectedGraph = new AdjacentMap { { "1", new List <string>() { "2", "7", "11" } }, { "2", new List <string>() { "1", "3", "11" } }, { "3", new List <string>() { "2", "4", "10" } }, { "4", new List <string>() { "3", "5", "9" } }, { "5", new List <string>() { "4", "6", "9" } }, { "6", new List <string>() { "5", "7", "8" } }, { "7", new List <string>() { "1", "6", "8" } }, { "8", new List <string>() { "6", "7", "12" } }, { "9", new List <string>() { "4", "5", "12" } }, { "10", new List <string>() { "3", "12", "11" } }, { "11", new List <string>() { "1", "2", "10" } }, { "12", new List <string>() { "8", "9", "10" } } }; int vertexCount = 7; //Cover(1, 5, 3) //var undirectedGraph = new AdjacentMap //{ // {"1", new List<string>() {"2", "4"}}, // {"2", new List<string>() { "1", "3", "5"}}, // {"3", new List<string>() {"2", "5", "6", "7"}}, // {"4", new List<string>() {"1"}}, // {"5", new List<string>() {"2", "3", "6"}}, // {"6", new List<string>() {"3", "5"}}, // {"7", new List<string>() {"3"}} //}; //int vertexCount = 3; // Cover (5,3) //var undirectedGraph = new AdjacentMap //{ // {"2", new List<string>() { "3", "5"}}, // {"3", new List<string>() {"2", "5", "6", "7"}}, // {"5", new List<string>() {"2", "3", "6"}}, // {"6", new List<string>() {"3", "5"}}, // {"7", new List<string>() {"3"}} //}; //int vertexCount = 2; var vertexCoverReducter = new VertexCoverReducer(); Formula formula = vertexCoverReducter.ReduceVertexCoverToSat(undirectedGraph, vertexCount); Console.WriteLine(formula.ToString()); Console.WriteLine(); Console.WriteLine(); SatResolver satResolver = new SatResolver(); Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); var satisfiableInfo = satResolver.IsSatisfiable(formula); stopwatch.Stop(); Console.WriteLine($"{Console.Out.NewLine}Time elapsed: {stopwatch.Elapsed}"); bool[] maxSatisfiebleAssignment = SatUtil.GetMinSatisfiebleAssignment(satisfiableInfo.SatisfiableAssignments); if (satisfiableInfo.IsSatisfiable && maxSatisfiebleAssignment.Count(a => a) > 0) { Console.WriteLine("\nThe vertex cover problem is satisfiable!"); for (int i = 0; i < satisfiableInfo.VariableList.Count; i++) { Console.WriteLine("{0}:{1}", satisfiableInfo.VariableList[i].Name, maxSatisfiebleAssignment[i]); } } else { Console.WriteLine("\nThe vertex cover problem is not satisfiable!"); } Console.ReadLine(); }
static void Main(string[] args) { Variable arya = new Variable("Arya"); Variable brandon = new Variable("Brandon"); Variable cersei = new Variable("Cersei"); Variable dany = new Variable("Dany"); Variable eddard = new Variable("Eddard"); Variable gilly = new Variable("Gilly"); Variable jon = new Variable("Jon"); Variable melisandre = new Variable("Melisandre"); Variable rob = new Variable("Rob"); Variable sansa = new Variable("Sansa"); Operator and = new Operator(Operator.Types.And); Operator or = new Operator(Operator.Types.Or); Operator not = new Operator(Operator.Types.Not); var dinnerPartyProblem = new Formula() { new Bracket() { not, arya, or, not, brandon }, and, new Bracket() { not, arya, or, not, cersei }, and, new Bracket() { not, arya, or, not, gilly }, and, new Bracket() { not, brandon, or, not, arya }, and, new Bracket() { not, brandon, or, not, jon }, and, new Bracket() { not, brandon, or, not, melisandre }, and, new Bracket() { not, cersei, or, not, arya }, and, new Bracket() { not, cersei, or, not, dany }, and, new Bracket() { not, cersei, or, not, rob }, and, new Bracket() { not, dany, or, not, cersei }, and, new Bracket() { not, dany, or, not, eddard }, and, new Bracket() { not, dany, or, not, melisandre }, and, new Bracket() { not, eddard, or, not, dany }, and, new Bracket() { not, eddard, or, not, gilly }, and, new Bracket() { not, eddard, or, not, jon }, and, new Bracket() { not, gilly, or, not, arya }, and, new Bracket() { not, gilly, or, not, eddard }, and, new Bracket() { not, gilly, or, not, sansa }, and, new Bracket() { not, jon, or, not, brandon }, and, new Bracket() { not, jon, or, not, eddard }, and, new Bracket() { not, jon, or, not, rob }, and, new Bracket() { not, melisandre, or, not, brandon }, and, new Bracket() { not, melisandre, or, not, dany }, and, new Bracket() { not, melisandre, or, not, sansa }, and, new Bracket() { not, rob, or, not, cersei }, and, new Bracket() { not, rob, or, not, jon }, and, new Bracket() { not, rob, or, not, sansa }, and, new Bracket() { not, sansa, or, not, gilly }, and, new Bracket() { not, sansa, or, not, melisandre }, and, new Bracket() { not, sansa, or, not, rob } }; var satResolver = new SatResolver(); Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); var satisfiableInfo = satResolver.IsSatisfiable(dinnerPartyProblem); stopwatch.Stop(); Console.WriteLine($"{Console.Out.NewLine}Time elapsed: {stopwatch.Elapsed}"); bool[] maxSatisfiebleAssignment = SatUtil.GetMaxSatisfiebleAssignment(satisfiableInfo.SatisfiableAssignments); if (satisfiableInfo.IsSatisfiable && maxSatisfiebleAssignment.Count(a => a) > 0) { Console.WriteLine("\nThe dinner party problem is satisfiable!"); for (int i = 0; i < satisfiableInfo.VariableList.Count; i++) { Console.WriteLine("{0}:{1}", satisfiableInfo.VariableList[i].Name, maxSatisfiebleAssignment[i]); } } else { Console.WriteLine("\nThe dinner party problem is not satisfiable!"); } Console.ReadLine(); }
private static void Main(string[] args) { var graph = new AdjacentMap() { { "A", new List <string>() { "B", "F", "C", "E" } }, { "B", new List <string>() { "A", "C", "D", "F" } }, { "C", new List <string>() { "B", "D", "A", "E" } }, { "D", new List <string>() { "C", "E", "B", "F" } }, { "E", new List <string>() { "D", "F", "C", "A" } }, { "F", new List <string>() { "E", "A", "B", "D" } } }; Formula formula = ThreeColoringReducer.ReduceTo3Sat(graph); Console.WriteLine("Formula:"); Console.WriteLine(formula); var satResolver = new SatResolver(); Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); var satisfiableInfo = satResolver.IsSatisfiable(formula); stopwatch.Stop(); Console.WriteLine($"{Console.Out.NewLine}Time elapsed: {stopwatch.Elapsed}"); bool[] maxSatisfiebleAssignment = SatUtil.GetMaxSatisfiebleAssignment(satisfiableInfo.SatisfiableAssignments); if (satisfiableInfo.IsSatisfiable && maxSatisfiebleAssignment.Count(a => a) > 0) { Console.WriteLine("\nThe graph is 3-colorable!"); for (int i = 0; i < satisfiableInfo.VariableList.Count; i++) { var nodeColorInfo = satisfiableInfo.VariableList[i].Name .Split("|", StringSplitOptions.RemoveEmptyEntries); Console.WriteLine("Node {0}|{1}:{2}", nodeColorInfo[0], nodeColorInfo[1], maxSatisfiebleAssignment[i]); } } else { Console.WriteLine("\nThe graph is not 3-colorable!"); } Console.Out.Flush(); Console.ReadLine(); }