@@ -108,12 +108,12 @@ namespace SolveAvdoshin
108108
109109 static BooleanExpression FindMininalExpressionInBasis(int n, BooleanOperation[] ops, BooleanVariable[] vars)
110110 {
111− var queue = new Queue<BooleanExpression>();
111+ var queue = new ImprovisedPriorityQueue<BooleanExpression>(15);
112112 var knownTruthTables = new HashSet<byte>();
113113 var knownExpressions = new HashSet<BooleanExpression>();
114114
115115 foreach(var variable in vars) {
116− queue.Enqueue(new VarExpression(variable));
116+ queue.TryEnqueue(new VarExpression(variable), 1);
117117 }
118118
119119 while(queue.Count != 0) {
@@ -132,20 +132,57 @@ namespace SolveAvdoshin
132132 knownExpressions.Add(curExperession);
133133 knownTruthTables.Add(truthTable);
134134
135− var newExpressions = new Stack<BooleanExpression>();
136−
137135 foreach(var anotherExpression in knownExpressions) {
138136 foreach(var neighbourExpression in curExperession.CombineWith(anotherExpression, ops)) {
139− newExpressions.Push(neighbourExpression);
137+ queue.TryEnqueue(neighbourExpression, neighbourExpression.getSize());
140138 }
141139 }
140+ }
141+
142+ throw new CouldntFindExpressionException();
143+ }
144+ }
145+
146+ class ImprovisedPriorityQueue<T>
147+ {
148+ public int Capacity, Count;
149+ Queue<T>[] Queues;
150+
151+ public ImprovisedPriorityQueue(int capacity)
152+ {
153+ Capacity = capacity;
154+ Count = 0;
155+
156+ Queues = new Queue<T>[capacity + 1];
142157
143− while(newExpressions.Count != 0) {
144− queue.Enqueue(newExpressions.Pop());
158+ for(int i = 1; i <= capacity; i++) {
159+ Queues[i] = new Queue<T>();
160+ }
161+ }
162+
163+ public bool TryEnqueue(T item, int priority)
164+ {
165+ if(priority < 1 || priority > Capacity)
166+ return false;
167+
168+ Count += 1;
169+ Queues[priority].Enqueue(item);
170+ return true;
171+ }
172+
173+ public T Dequeue()
174+ {
175+ if(Count == 0)
176+ throw new Exception("ImprovisedPriorityQueue is empty");
177+
178+ for(int i = 1; i <= Capacity; i++) {
179+ if(Queues[i].Count != 0) {
180+ Count -= 1;
181+ return Queues[i].Dequeue();
145182 }
146183 }
147184
148− throw new CouldntFindExpressionException();
185+ throw new Exception("ImprovisedPriorityQueue is empty");
149186 }
150187 }
151188
@@ -166,6 +203,7 @@ namespace SolveAvdoshin
166203 abstract public byte Eval();
167204 abstract new public string ToString();
168205 abstract public BooleanExpression Clone();
206+ abstract public int getSize();
169207
170208 public override int GetHashCode()
171209 {
@@ -177,34 +215,6 @@ namespace SolveAvdoshin
177215 return ToString() == obj.ToString();
178216 }
179217
180− public static IEnumerable<BooleanExpression> AllTrees(int size)
181− {
182− if(size == 0) {
183− yield return new VarExpression(BooleanVariable.A);
184− }
185− else {
186− for(int i = 0; i < size; i++) {
187− foreach(var l in AllTrees(i)) {
188− foreach(var r in AllTrees(size - 1 - i)) {
189− yield return new OpExpression(BooleanOperation.And, l, r);
190− }
191− }
192− }
193− }
194− }
195−
196− public IEnumerable<BooleanExpression> NeighbourExpressions(BooleanOperation[] ops,
197− BooleanVariable[] vars)
198− {
199− foreach(var op in ops) {
200− foreach(var variable in vars) {
201− yield return new OpExpression(op, Clone(), new VarExpression(variable));
202−
203− yield return new OpExpression(op, new VarExpression(variable), Clone());
204− }
205− }
206− }
207−
208218 public IEnumerable<BooleanExpression> CombineWith(BooleanExpression anotherExpression,
209219 BooleanOperation[] ops)
210220 {
@@ -244,6 +254,11 @@ namespace SolveAvdoshin
244254 return new OpExpression(Op, Left.Clone(), Right.Clone());
245255 }
246256
257+ public override int getSize()
258+ {
259+ return Left.getSize() + Right.getSize() + 1;
260+ }
261+
247262 public override byte Eval()
248263 {
249264 byte aVal = Left.Eval();
@@ -343,6 +358,11 @@ namespace SolveAvdoshin
343358 return new VarExpression(Var);
344359 }
345360
361+ public override int getSize()
362+ {
363+ return 0;
364+ }
365+
346366 public override byte Eval()
347367 {
348368 switch(Var) {