检查关系传递性的算法?

发布于 2024-07-15 00:53:48 字数 157 浏览 11 评论 0原文

我需要检查关系是否具有传递性?

您能建议一些算法来检查关系的传递性吗?

我将关系存储为布尔矩阵,如果元素与图表中的0相关,则有1

谢谢。

I need to check if relation is transitive or not?

Would you please suggest some algorithm to check the transitivity of relations?

I am storing relation as a boolean matrix there is 1 if elements are related other wise 0 like in graphs.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

胡大本事 2024-07-22 00:53:52

拓扑排序或许是正确的方向。 如果有向图表示中没有循环,则该关系是传递的。 如果您关心速度,图算法可能是最佳选择。

Topological sorting may be the right direction. The relationship is transitive if there are no loops in its directed graph representation. If you care about speed, graph algorithms are probably the way to go.

此生挚爱伱 2024-07-22 00:53:51

尽管这听起来完全像家庭作业...

您需要存储您的关系,以便您可以通过先行词快速查找它们。 然后你就可以发现A->B->C类型的传递关系,将它们添加到同一个存储中,并继续查找A->B->C->D,等等。 。

Despite this totally sounds like homework...

You'd need to store your relations so that you can look them up by the antecedent very quickly. Then you can discover transitive relations of the type A->B->C, add them to the same storage, and keep going to look up A->B->C->D, etc etc...

情绪失控 2024-07-22 00:53:49

与我的地图/集合版本(已删除)相比,算法更简单,现在带有布尔矩阵。 也许这更容易理解,即使你不懂Java?

public class Trans
{

  final static int SIZE = 4;

  static boolean isTransitive(boolean[][] function)
  {
    for (int i = 0; i < SIZE; i++)
    {
      for (int j = 0; j < SIZE; j++)
      {
        if (function[i][j])
        {
          for (int k = 0; k < SIZE; k++)
          {
            if (function[j][k] && !function[i][k])
            {
              return false;
            }
          }
        }
      }
    }
    return true;
  }

  public static void main(String[] args)
  {
    boolean[][] function = new boolean[SIZE][SIZE];
    for (int i = 0; i < SIZE; i++)
    {
      function[i] = new boolean[SIZE];
    }
    function[0][1] = true;
    function[1][2] = true;
    function[0][2] = true;
    function[0][3] = true;
    function[1][3] = true;   
    System.out.println(isTransitive(function));
  }
}

Much simpler algorithm as my Map/Set version (deleted), now with boolean matrix. Maybe this is easier to understand, even if you don't know Java?

public class Trans
{

  final static int SIZE = 4;

  static boolean isTransitive(boolean[][] function)
  {
    for (int i = 0; i < SIZE; i++)
    {
      for (int j = 0; j < SIZE; j++)
      {
        if (function[i][j])
        {
          for (int k = 0; k < SIZE; k++)
          {
            if (function[j][k] && !function[i][k])
            {
              return false;
            }
          }
        }
      }
    }
    return true;
  }

  public static void main(String[] args)
  {
    boolean[][] function = new boolean[SIZE][SIZE];
    for (int i = 0; i < SIZE; i++)
    {
      function[i] = new boolean[SIZE];
    }
    function[0][1] = true;
    function[1][2] = true;
    function[0][2] = true;
    function[0][3] = true;
    function[1][3] = true;   
    System.out.println(isTransitive(function));
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文