Expression equivalence checking - Programmers Heaven

#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

#### Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

# Expression equivalence checking

Posts: 1Member
Hi All.

I'm trying to solve following problem:

Given two ariphmetic expressions, operations are +, -, *, /, () and operands are just identifiers.
Operations are not associative, but + and * are commutative.
The question is: whether it is possible to rename identifiers and to rearrange operands at commutative operations (+, *) so that to reduce one expression to another.

For example:

1.
Input:
A+B
C+B
Output:
Yes

2.
Input:
(A+A)+B
C+(D+D)
?????:
Yes

3.
Input:
(A+A)+B
(A+B)+A
Output:
No

4.
Input:
A+A+B
A+B+A
Output:
NO

5.
Input:
A+B*C
A1+A2*A2
Output:
No

6.
Input:
(A+B-C)*(C+C)
(D+D)*(B+A-D)
Output:
Yes

I created following algorithm:

1. Build expressions' trees. (They are binaries trees).
2. Check for trees ismorphism with fixed roots.
3. Check identifiers sets.

But is this algorithm correct?
And how to do stage 3 except full recursive?

Thanks.