📜  完全指定机器的优化状态表

📅  最后修改于: 2022-05-13 02:24:08.187000             🧑  作者: Mango

完全指定机器的优化状态表

使用基于分区的算法从完全指定的机器的给定状态表中优化状态表。

例子:

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 

使用的方法:

对于完全指定的状态机完全指定的状态机:

  1. 如果所有输入组合的输出都相同,则两个状态是等价的 下一个状态对于所有输入组合都是等价的。
  2. 状态等价是一种等价关系,它将状态划分为不相交的等价类。

机器 M1:

PSNS, Z (x=0)NS, Z (x=1)
AE, 0D, 0
BF, 0D, 0
CE, 0B, 1
DF, 0B, 0
EC, 0F, 1
FB, 0C, 0

  • Step-1(状态最小化):识别并移除冗余状态
  • Step-2(状态分配):为每个状态分配唯一的二进制代码
  • 第 3 步(组合逻辑优化):使用未分配的状态代码作为无关。
    • P0={(ABCDEF)}:
      最初,我们将所有状态添加到一个分区中。
    • P1={(ACE), (BDF)}:
      B、D、F 具有与其他状态不同的输入输出响应。
    • P2={(ACE), (BD), (F)}:
      根据下一个状态的输出,F 具有不同于前一个分区块(BDF)的其他状态的其他输入-输出响应。
    • P3={(AC), (BD), (E), (F)}:
      根据下一个状态的输出,E 具有不同于前一个分区块 (ACE) 的其他状态的其他输入-输出响应。
    • P4={(AC), (BD), (E), (F)}:
      与 P3 相同,找到等效分区。
    • 等效分区:
      P3,这里,P3和P4是相同的,所以,我们认为P3是等效分区。
    • 新州:
      S1=(AC), S2=(BD), S3=(E), S4=(F)

这里,状态 A、C 可以组合成一个状态,而状态 B、D 可以组合成另一个状态。我们将 S1、S2、S3、S4 视为新乐观状态表的状态。

因此,状态数减少到 4 个。

以下是该方法的实施 -

C
#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 output(Z) 
    table arr[2]; 
    for (int i = 0; i < ps; i++) { 
        char present_state = 'A' + i; 
  
        // next-state(NS) and output(Z) for x=0 
        scanf("%*c%c%d", &temp, &arr[i][0].output); 
  
        // generate present state(PS) 
        arr[i][0].state = (int)temp - 65; 
  
        // next-state(NS) and output(Z) for x=1 
        scanf("%*c%c%d", &temp, &arr[i][1].output); 
  
        // generate present state(PS) 
        arr[i][1].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][0].state + 65), arr[j][0].output, 
                                                        (char)(arr[j][1].state + 65), arr[j][1].output); 
    } 
    int prev_result; 
    int new_result; 
    for (int i = 0; i < ps; i++) { 
        // divide P0(first partition P0=ABCDEF) according to their output in x=0, 1 
        // for x=0, x=1 whose output 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][0].output + arr[i][1].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][0].state] * 2 + prev_result[arr[j][1].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]][0].state]), 
                                                                        arr[s[j]][0].output, 
                                                            (prev_result[arr[s[j]][1].state]), 
                                                                        arr[s[j]][1].output); 
    } 
  
    // *********************** 
    return 0; 
}


输入:

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 

输出:

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