Find all compatable and non compatable edges of a machine

Given a machine in the formal language of N states and M pairs of output combinations in the form of 2D array arr[][]. Each row(say r) of arr[][] denotes the nodes from ‘A’ to ‘Z’ and each pair of a column(say (a, b)) denotes the change of state of node r to node a via state b. The task is to find the compatible and non compatible edges of the formal language.

Note: Edge(A, B) is said to be compatible as all the next state and output are either equal or unspecified in A, B corresponding to each column.

Example:

Input: N = 6, M = 4,
arr[][] = { { ‘-‘, ‘-‘, ‘C’, ‘1’, ‘E’, ‘1’, ‘B’, ‘1’ },
{ ‘E’, ‘0’, ‘-‘, ‘-‘, ‘-‘, ‘-‘, ‘-‘, ‘-‘ },
{ ‘F’, ‘0’, ‘F’, ‘1’, ‘-‘, ‘-‘, ‘-‘, ‘-‘ },
{ ‘-‘, ‘-‘, ‘-‘, ‘-‘, ‘B’, ‘1’, ‘-‘, ‘-‘ },
{ ‘-‘, ‘-‘, ‘F’, ‘0’, ‘A’, ‘0’, ‘D’, ‘1’ },
{ ‘C’, ‘0’, ‘-‘, ‘-‘, ‘B’, ‘0’, ‘C’, ‘1’ } }
Output:
Not Compatable Edges
(A, E) (A, F) (B, F) (C, E) (D, E) (D, F)
Compatable Edges
(A, B)(A, C)(A, D)(B, C)(B, D)(B, E)(C, D)(C, F)(E, F)

Input: N = 4, M = 4,
arr[][] = { { ‘-‘, ‘-‘, ‘C’, ‘1’, ‘E’, ‘1’, ‘B’, ‘1’ },
{ ‘-‘, ‘-‘, ‘-‘, ‘-‘, ‘B’, ‘1’, ‘-‘, ‘-‘ },
{ ‘-‘, ‘-‘, ‘F’, ‘0’, ‘A’, ‘0’, ‘D’, ‘1’ },
{ ‘C’, ‘0’, ‘-‘, ‘-‘, ‘B’, ‘0’, ‘C’, ‘1’ } }
Output:
Not Compatable Edges
(A, C) (A, D) (B, C) (B, D)
Compatable Edges
(A, B)(C, D)



Approach:

  1. For all the possible combinations(say (a,b)) of the nodes, check if there is any possible path present in the formal language through any number of states as:
    • If state via Node a is empty, then check for the next pair of nodes.
    • If current traversed state(say Node b) via Node a is not empty and if the output state via Node a to Node b is not same then recursively check for a path from Node a to Node b.
    • If the output state is the same, then it has a direct edge between Node a and Node b.
  2. If the path is found between any pair of nodes,then the pair of nodes are a part of compatible node.
  3. Store the above pair of compatible nodes in a matrix Mat[][].
  4. Traverse the Mat[][] for all the possible pairs, and if that pair is present in Mat[][] then print it as a Compatible Nodes Else it is Not Compatible node.

Below is the implementation of the above approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the above approach
#include <bits/stdc++.h>
using namespace std;
const int M = 8;
  
// Function to find the compatible and
// non-compatible for a given formal language
void findEdges(char arr[][M], int n, int m)
{
  
    // To store the compatible edges
    char mat[1000][1000] = { 'x' };
  
    // Loop over every pair of nodes in the
    // given formal language
    for (int i = 0; i < n; i++) {
        for (int j = i + 1; j < n; j++) {
  
            // Traverse through the output
            // column and compare it between
            // each set of pairs of nodes
            for (int k = 0; k < 2 * m; k += 2) {
  
                // If the the output is not
                // specified then leave the
                // edge unprocessed
                if (arr[i][k + 1] == '-'
                    || arr[j][k + 1] == '-') {
                    continue;
                }
  
                // If the output of states
                // doesn't match then not
                // compatable.
                if (arr[i][k + 1] != arr[j][k + 1]) {
  
                    // Mark the not compatable
                    // edges in the maxtrix with
                    // character 'v'
                    mat[i][j] = 'v';
                    mat[j][i] = 'v';
                    break;
                }
            }
        }
    }
  
    int nn = n;
  
    // Loop over all node to find other non
    // compatable edges
    while (nn--) {
  
        // Loop over every pair of nodes in
        // the given formal language
        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j < n; j++) {
  
                int k;
                for (k = 0; k < m; k += 2) {
  
                    // If the the output is
                    // not specified then
                    // leave edge unprocessed
                    if (arr[i][k + 1] == '-'
                        || arr[j][k + 1] == '-') {
                        continue;
                    }
  
                    // If output is not equal
                    // then break as non-compatable
                    if (arr[i][k + 1] != arr[j][k + 1]) {
                        break;
                    }
                }
  
                if (k < m) {
                    continue;
                }
  
                for (k = 0; k < m; k += 2) {
  
                    // If next states are unspecified
                    // then continue
                    if (arr[i][k] == '-'
                        || arr[j][k] == '-') {
                        continue;
                    }
  
                    // If the states are not equal
                    if (arr[i][k] != arr[j][k]) {
                        int x = arr[i][k] - 'A';
                        int y = arr[j][k] - 'A';
  
                        // If the dependent edge
                        // is not compatable then
                        // this edge is also not
                        // compatable
                        if (mat[x][y] == 'v') {
                            mat[i][j] = 'v';
                            mat[j][i] = 'v';
                            break;
                        }
                    }
                }
            }
        }
    }
  
    // Output all Non-compatable Edges
    printf("Not Compatable Edges \n");
    for (int i = 0; i < n; i++) {
        for (int j = i + 1; j < n; j++) {
            if (mat[i][j] == 'v') {
                printf("(%c, %c) ", i + 65, j + 65);
            }
        }
    }
    printf("\n");
  
    // Output all Compatable Edges
    printf("Compatable Edges \n");
    for (int i = 0; i < n; i++) {
        for (int j = i + 1; j < n; j++) {
            if (mat[i][j] != 'v') {
                printf("(%c, %c)", i + 65, j + 65);
            }
        }
    }
}
  
// Driver Code
int main()
{
    int n = 6, m = 4;
  
    char arr[][8] = { { '-', '-', 'C', '1', 'E', '1', 'B', '1' },
                      { 'E', '0', '-', '-', '-', '-', '-', '-' },
                      { 'F', '0', 'F', '1', '-', '-', '-', '-' },
                      { '-', '-', '-', '-', 'B', '1', '-', '-' },
                      { '-', '-', 'F', '0', 'A', '0', 'D', '1' },
                      { 'C', '0', '-', '-', 'B', '0', 'C', '1' } };
  
    findEdges(arr, n, m);
    return 0;
}

chevron_right


Output:

Not Compatable Edges
(A, E) (A, F) (B, F) (C, E) (D, E) (D, F) 
Compatable Edges
(A, B)(A, C)(A, D)(B, C)(B, D)(B, E)(C, D)(C, F)(E, F)

Time Complexity: O(M*N3), where N is the number of states and M is the Output for every states.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




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 :

Be the First to upvote.


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