# Optimizing state table of a completely specified machine

Optimize state table form a given state table of a completely specified machine using partition based algorithm.

Examples:

```Input :
6
E 0 D 1
F 0 D 0
E 0 B 1
F 0 B 0
C 0 F 1
B 0 C 0

Output :
A    E, 0    D, 1
B    F, 0    D, 0
C    E, 0    B, 1
D    F, 0    B, 0
E    C, 0    F, 1
F    B, 0    C, 0
P1=(ACE) (BDF)
P2=(ACE) (BD) (F)
P3=(AC) (BD) (E) (F)
S1=(AC) S2=(BD) S3=(E) S4=(F)
S1    S3, 0    S2, 1
S2    S4, 0    S2, 0
S3    S1, 0    S4, 1
S4    S2, 0    S1, 0 ```

Approach used:
For Completely-specified state machine Completely-specified state machine:

1. Two states are equivalent if outputs are identical for all input combinations Next states are equivalent for all input combinations.
2. Equivalence of states is an equivalence relation which partitions the states into disjoint equivalence classes.

Machine M1:

PS NS, Z (x=0) NS, Z (x=1)
A E, 0 D, 0
B F, 0 D, 0
C E, 0 B, 1
D F, 0 B, 0
E C, 0 F, 1
F B, 0 C, 0

• Step-1 (State minimization): identify and remove redundant states
• Step-2 (State assignment): assign unique binary code to each state
• Step-3 (Combinational logic optimization): use unassigned state-codes as don’t care.
• P0={(ABCDEF)}:
Initially we add all states in one partition.
• P1={(ACE), (BDF)}:
B, D, F has other input-output responce than other states.
• P2={(ACE), (BD), (F)}:
According to output of next state, F has other input-output responce than other states of previous partition block (BDF).
• P3={(AC), (BD), (E), (F)}:
According to output of next state, E has other input-output responce than other states of previous partition block (ACE).
• P4={(AC), (BD), (E), (F)} :
same as P3, Equivalent partition found.
• Equivalent Partition:
P3, Here, P3 and P4 is same, so, we consider P3 as Equivalent Partition.
• New States:
S1=(AC), S2=(BD), S3=(E), S4=(F)

Here, States A, C can be combined to one state and states B, D can be combined to another state. We consider S1, S2, S3, S4 as states of new optimistic state table.
So, number of states reduced to 4.

Below is implementation of the approach –

 `#include ` `#include ` `#include ` `typedef` `struct` `table { ` `    ``int` `state; ` `    ``int` `output; ` `} table; ` `int` `main() ` `{ ` `    ``char` `present_state, temp; ` `    ``int` `ps, temp2; ` ` `  `    ``// hash array ` `    ``int` `hash; ` ` `  `    ``// Input number of states ` `    ``scanf``(``"%d"``, &ps); ` ` `  `    ``// make a table for input of next-state(NS) and optput(Z) ` `    ``table arr; ` `    ``for` `(``int` `i = 0; i < ps; i++) { ` `        ``char` `present_state = ``'A'` `+ i; ` ` `  `        ``// next-state(NS) and optput(Z) for x=0 ` `        ``scanf``(``"%*c%c%d"``, &temp, &arr[i].output); ` ` `  `        ``// generate present state(PS) ` `        ``arr[i].state = (``int``)temp - 65; ` ` `  `        ``// next-state(NS) and optput(Z) for x=1 ` `        ``scanf``(``"%*c%c%d"``, &temp, &arr[i].output); ` ` `  `        ``// generate present state(PS) ` `        ``arr[i].state = (``int``)temp - 65; ` `    ``} ` `    ``for` `(``int` `j = 0; j < ps; j++) { ` `        ``char` `present_state = ``'A'` `+ j; ` `        ``// print the given state table ` `        ``printf``(``"%c    %c, %d    %c, %d\n"``, present_state, (``char``)(arr[j].state + 65), arr[j].output,  ` `                                                          ``(``char``)(arr[j].state + 65), arr[j].output); ` `    ``} ` `    ``int` `prev_result; ` `    ``int` `new_result; ` `    ``for` `(``int` `i = 0; i < ps; i++) { ` `        ``// devide P0(first partition P0=ABCDEF) according to their output in x=0, 1 ` `        ``// for x=0, x=1 whose optput are same, are contained in same partition block ` `        ``// for the example machine M1, P1 will be generated  ` `        ``// like { (ACE), (BDF) } and stored in prev_result ` `        ``prev_result[i] = (2 * arr[i].output + arr[i].output); ` `    ``} ` `    ``// -------------------------------------------------------- ` ` `  `    ``// run this function for generate n-tn Partition(Pn) ` `    ``for` `(``int` `j = 0; j < 100; j++) { ` `        ``for` `(``int` `i = 0; i < ps; i++) ` ` `  `            ``// Load previous result in new result ` `            ``new_result[i] = prev_result[i]; ` `        ``for` `(``int` `i = 0; i < ps; i++) ` `            ``hash[i] = 0; ` ` `  `        ``// ************ Update New Result **************** ` ` `  `        ``int` `op = 100; ` ` `  `        ``// print n-th partition (Pn) ` `        ``printf``(``"P%d="``, j + 1); ` `        ``for` `(``int` `i = 0; i < ps; i++) { ` ` `  `            ``// if i-th PS is not visited ` `            ``if` `(hash[i] == 0) { ` ` `  `                ``// make i-th PS visited ` `                ``hash[i] = 1; ` `                ``printf``(``"("``); ` `                ``for` `(``int` `j = i; j < ps; j++) { ` `                    ``if` `(prev_result[i] == prev_result[j]) { ` ` `  `                        ``// print n-th partition (Pn) ` `                        ``printf``(``"%c"``, j + 65); ` ` `  `                        ``// increase i-th new-result ` `                        ``new_result[j] = new_result[j] * op; ` ` `  `                        ``// update new result according to next state ` `                        ``new_result[j] += (prev_result[arr[j].state] * 2 + prev_result[arr[j].state]); ` `                        ``hash[j] = 1; ``// make j-th PS visited ` `                    ``} ` `                ``} ` `                ``printf``(``") "``); ` `                ``op += 100; ` `            ``} ` `        ``} ` `        ``printf``(``"\n"``); ` ` `  `        ``// Check New and Previous Result according to difference of e, If same ` ` `  `        ``int` `flag = -1; ` `        ``for` `(``int` `x = 0; x < ps - 1; x++) { ` `            ``for` `(``int` `y = x + 1; y < ps; y++) { ` `                ``if` `(prev_result[x] == prev_result[y]) { ` ` `  `                    ``// compare difference of x-th and y-th result(new and prev) ` `                    ``if` `(new_result[x] != new_result[y]) { ` ` `  `                        ``// if difference is not same and break ` `                        ``flag = 0; ` `                        ``break``; ` `                    ``} ` `                ``} ` `            ``} ` `            ``if` `(flag == 0) ` `                ``break``; ` `        ``} ` ` `  `        ``// ******************** Update Previous Result with new result ********************** ` ` `  `        ``int` `ij = 1; ` `        ``for` `(``int` `i = 0; i < ps; i++) { ` `            ``hash[i] = 0; ` `        ``} ` `        ``for` `(``int` `i = 0; i < ps; i++) { ` ` `  `            ``// if i-th PS is not visited ` `            ``if` `(hash[i] == 0) { ` ` `  `                ``// make i-th PS visited ` `                ``hash[i] = 1; ` `                ``for` `(``int` `j = i; j < ps; j++) { ` `                    ``if` `(new_result[i] == new_result[j]) { ` ` `  `                        ``// change values of prev result with minimal value(ij) ` `                        ``// (if Prev_result=[5, 8, 5, 5, 9], if change to [1, 2, 1, 1, 3]) ` `                        ``prev_result[j] = ij; ` `                        ``hash[j] = 1; ``// make j-th PS visited ` `                    ``} ` `                ``} ` `                ``ij++; ` `            ``} ` `        ``} ` `        ``// ********************************************************** ` ` `  `        ``// Equivalent Partition Found(new result and prev result is same) ` `        ``if` `(flag != 0) ` `            ``break``; ` `    ``} ` ` `  `    ``// ----------- generate optimistic state table ------------------ ` ` `  `    ``int` `ij = 1, s; ` `    ``for` `(``int` `i = 0; i < ps; i++) { ` `        ``hash[i] = 0; ` `    ``} ` `    ``for` `(``int` `i = 0; i < ps; i++) { ` ` `  `        ``// make i-th PS is not visited ` `        ``if` `(hash[i] == 0) { ` ` `  `            ``// print S-th partition block of equivalent partition ` `            ``printf``(``"S%d=("``, ij); ` ` `  `            ``// make i-th PS visited ` `            ``hash[i] = 1; ` `            ``for` `(``int` `j = i; j < ps; j++) { ` `                ``if` `(prev_result[i] == prev_result[j]) { ` ` `  `                    ``// make j-th PS visited ` `                    ``hash[j] = 1; ` ` `  `                    ``// print S-th partition block of equivalent partition ` `                    ``printf``(``"%c"``, j + 65); ` `                ``} ` `            ``} ` `            ``printf``(``") "``); ` ` `  `            ``// assign value of i-th PS for ij-th state  ` `            ``// of new optimistic state table ` `            ``s[ij] = i; ` `            ``ij++; ` `        ``} ` `    ``} ` `    ``printf``(``"\n"``); ` ` `  `    ``for` `(``int` `j = 1; j < ij; j++) { ` `        ``int` `present_state = j; ` ` `  `        ``// print optimistic state table ` `        ``printf``(``"S%d    S%d, %d    S%d, %d\n"``, present_state, (prev_result[arr[s[j]].state]),  ` `                                                                          ``arr[s[j]].output,  ` `                                                             ``(prev_result[arr[s[j]].state]),  ` `                                                                          ``arr[s[j]].output); ` `    ``} ` ` `  `    ``// *********************** ` `    ``return` `0; ` `} `

Input:

```7
E 0 C 0
C 0 A 0
B 0 G 0
G 0 A 0
F 1 B 0
E 0 D 0
D 0 G 0 ```
Output:

```A    E, 0    C, 0
B    C, 0    A, 0
C    B, 0    G, 0
D    G, 0    A, 0
E    F, 1    B, 0
F    E, 0    D, 0
G    D, 0    G, 0
P1=(ABCDFG) (E)
P2=(AF) (BCDG) (E)
P3=(AF) (BD) (CG) (E)
P4=(A) (BD) (CG) (E) (F)
S1=(A) S2=(BD) S3=(CG) S4=(E) S5=(F)
S1    S4, 0    S3, 0
S2    S3, 0    S1, 0
S3    S2, 0    S3, 0
S4    S5, 1    S2, 0
S5    S4, 0    S2, 0 ```

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.