# Find all compatable and non compatable edges of a machine

• Last Updated : 25 Sep, 2020

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)

We provide nothing but the best curated videos and practice problems for our students. Check out the C Foundation Course and master the C language from basic to advanced level. Wait no more, start learning today!

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 the 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 the 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 is a part of a 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:

## C++

 `// C++ implementation of the above approach``#include ``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 = { ``'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[] = { { ``'-'``, ``'-'``, ``'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;``}`

## Java

 `// Java implementation of the above approach``import` `java.util.*;`` ` `class` `GFG{``    ` `static` `int` `M = ``8``;`` ` `// Function to find the compatible and``// non-compatible for a given formal language``static` `void` `findEdges(``char` `arr[][], ``int` `n, ``int` `m)``{`` ` `    ``// To store the compatible edges``    ``char` `[][]mat = ``new` `char``[``1000``][``1000``];`` ` `    ``// 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-- > ``0``)``    ``{`` ` `        ``// 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``    ``System.out.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'``)``            ``{``                ``System.out.printf(``"(%c, %c) "``,``                                  ``i + ``65``, j + ``65``);``            ``}``        ``}``    ``}``    ``System.out.printf(``"\n"``);`` ` `    ``// Output all Compatable Edges``    ``System.out.printf(``"Compatable Edges \n"``);``    ``for``(``int` `i = ``0``; i < n; i++)``    ``{``        ``for``(``int` `j = i + ``1``; j < n; j++)``        ``{``            ``if` `(mat[i][j] != ``'v'``)``            ``{``                ``System.out.printf(``"(%c, %c)"``,``                                  ``i + ``65``, j + ``65``);``            ``}``        ``}``    ``}``}`` ` `// Driver Code``public` `static` `void` `main(String[] args)``{``    ``int` `n = ``6``, m = ``4``;`` ` `    ``char` `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'` `} };`` ` `    ``findEdges(arr, n, m);``}``}`` ` `// This code is contributed by Amit Katiyar`

## C#

 `// C# implementation of``// the above approach``using` `System;``class` `GFG{``    ` `static` `int` `M = 8;`` ` `// Function to find the``//compatible and non-compatible``// for a given formal language ``static` `void` `findEdges(``char` `[,]arr,``                      ``int` `n, ``int` `m)``{``  ``// To store the compatible edges``  ``char` `[,]mat = ``new` `char``[1000, 1000];` `  ``// 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-- > 0)``  ``{` `    ``// 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``  ``Console.Write(``"Not Compatable Edges \n"``);``  ``for``(``int` `i = 0; i < n; i++)``  ``{``    ``for``(``int` `j = i + 1; j < n; j++)``    ``{``      ``if` `(mat[i, j] == ``'v'``)``      ``{``        ``Console.Write(``"({0}, {1}) "``,``                      ``(``char``)(i + 65),``                      ``(``char``)(j + 65));``      ``}``    ``}``  ``}``  ``Console.Write(``"\n"``);` `  ``// Output all Compatable Edges``  ``Console.Write(``"Compatable Edges \n"``);``  ``for``(``int` `i = 0; i < n; i++)``  ``{``    ``for``(``int` `j = i + 1; j < n; j++)``    ``{``      ``if` `(mat[i, j] != ``'v'``)``      ``{``        ``Console.Write(``"({0}, {1})"``,``                      ``(``char``)(i + 65),``                      ``(``char``)(j + 65));``      ``}``    ``}``  ``}``}`` ` `// Driver Code``public` `static` `void` `Main(String[] args)``{``  ``int` `n = 6, m = 4;``  ``char` `[,]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'``}};``  ``findEdges(arr, n, m);``}``}`` ` `// This code is contributed by 29AjayKumar`
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 state.

My Personal Notes arrow_drop_up