d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Algorithm Discussion > Splitting Logic Formulas Into Groups.
Add Reply New Topic New Poll
Member
Posts: 21,861
Joined: Dec 13 2010
Gold: 0.91
Jan 28 2017 12:39pm
Hey guys.

I need help with developing an algorithm, and I post here because I know it is not that easy (at least I can not come up with a good solution)

Say you got a Formula F = A & B & (C | D) .. where "&" is AND, and "|" is OR.

What I wanna do, Is creating multiple Formulas which will be connected by disjunction, that means for the formula above:

F1 = A & B & C
F2 = A & B & D

--> F1 | F2

Which is also called "Conjunctive Normal Form".

In my case, the parts of this CNF-Formula represent Initial States/Values of a Procedure

Functions I already created/used during my project and that could come in handy i guess:

- Conversion of a Formula into an array, splitted on operators e.g. "x = 3" --> ["x","=","3"]
- Conversion of a Formula in Infix notation to Postfix notation, this fucntion also regards oparator precedence. e.g. "x = 3" --> x 3 =

I don't want full solutions at all, I'm happy with ideas, probably someone has done something like that before

I will post ideas if I come up with something^^

Kind Regards,
Bree
Member
Posts: 36,123
Joined: Jul 18 2008
Gold: 2,407.00
Jan 30 2017 05:58am
So you want to make an algorithm that turns a formula into CNF?
Member
Posts: 21,861
Joined: Dec 13 2010
Gold: 0.91
Jan 30 2017 06:03am
Oh i misspelled it, I mean the Disjunctive normal form, so DNF.
because the CNF would be OR Terms conncected with AND. and of the form (A|B) & (A|C)

And the DNF is of the form (A&B) | (A&C) ...

I got a solution by now, I can post later how it works if i don't forget it.
Its pretty ugly tho :lol:
Member
Posts: 36,123
Joined: Jul 18 2008
Gold: 2,407.00
Jan 30 2017 06:52am
Is this for school? Algorithms was the hardest class in my program. Only class I got a C in.
Member
Posts: 21,861
Joined: Dec 13 2010
Gold: 0.91
Jan 30 2017 06:52am
for a project on university.
Member
Posts: 36,123
Joined: Jul 18 2008
Gold: 2,407.00
Jan 30 2017 06:55am
Well good luck! Wish I knew more to help you lol
Member
Posts: 21,861
Joined: Dec 13 2010
Gold: 0.91
Jan 30 2017 06:58am
Quote (Mastersam93 @ 30 Jan 2017 13:55)
Well good luck! Wish I knew more to help you lol


well as I said, i got a solution with stacks.
short said you convert your expression into postfix notation and translate it back, while you do that you evaluate and simplify terms and build up the "groups" of the DNF.
more detailed explanation follows :D
Member
Posts: 14,631
Joined: Sep 14 2006
Gold: 575.56
Feb 5 2017 12:00am
haha the good old reverse polish notation problem
i remember this from data structures it was a fun one

Code
line = scanner.nextLine();
tokenize(line);

System.out.println();
Stack<Token> tokenStack = new Stack<>();
Queue<Token> outputArray = new Queue<>();
Token t = inputQueue.get();
System.out.print("Original: ["+t);

while(t!=null){
if (t.getType().equals(Token.Type.LEFT)) {
tokenStack.push(t);
} else if (t.getType().equals(Token.Type.RIGHT)) {
while (!tokenStack.peek().getType().equals(Token.Type.LEFT)) {
outputArray.enqueue(tokenStack.pop());
}
tokenStack.pop();
} else if (t.getType().equals(Token.Type.OPERAND)) {
outputArray.enqueue(t);
} else if (!tokenStack.isEmpty()) {
if (tokenStack.peek().compareTo(t) < 0) {
tokenStack.push(t);
} else if (tokenStack.peek().compareTo(t) >= 0) {
outputArray.enqueue(tokenStack.pop());
tokenStack.push(t);
}
} else {
tokenStack.push(t);
}
t = inputQueue.get();
if(t!=null)System.out.print(" " + t);
}
while (!tokenStack.isEmpty()) {
if (!tokenStack.peek().getType().equals(Token.Type.LEFT)) {
outputArray.enqueue(tokenStack.pop());
} else {
tokenStack.pop();
}
}
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll