📜  查找机器的所有兼容边缘和不兼容边缘

📅  最后修改于: 2021-05-28 04:46:29             🧑  作者: Mango

给定一台机器,其形式为N状态和M对输出组合,形式为2D数组arr [] []arr [] []的每一行(例如r )表示从‘A’到’Z’的节点,每一列对(例如(a,b) )表示节点r到节点a的通孔状态变化b 。任务是找到形式语言的兼容和不兼容边缘。
注意: Edge(A,B)被认为是兼容的,因为所有下一状态和输出在与每一列相对应的A,B中相等或未指定。
例子:

方法:

  1. 对于节点的所有可能组合(例如(a,b) ),检查形式语言中是否存在通过任何数量的状态的任何可能路径,例如:
    • 如果通过节点a的状态为,则检查下一对节点。
    • 如果通过节点a的当前遍历状态(例如,节点b )不为空,并且如果从节点a到节点b的输出状态不相同,则递归检查从节点a到节点b的路径。
    • 如果输出状态相同,则它在Node a和Node b之间具有直接边缘。
  2. 如果在任意一对节点之间找到路径,则该对节点是兼容节点的一部分。
  3. 将以上一对兼容节点存储在矩阵Mat [] []中
  4. 遍历所有可能的对的Mat [] [],如果Mat [] []中存在该对,则将其打印为兼容节点,否则它不是兼容节点。

下面是上述方法的实现:

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[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;
}


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


输出:
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)



时间复杂度: O(M * N 3 ),其中N是状态数,M是每个状态的输出。

想要从精选的最佳视频中学习和练习问题,请查看《基础知识到高级C的C基础课程》。