确定给定图是否是其他图的子图的简单方法?

发布于 2024-08-23 08:58:44 字数 304 浏览 3 评论 0原文

我正在寻找一种算法来检查给定图是否是另一个给定图的子图。

我几乎没有条件使这个 NP 完全问题更加可行。

  • 这些图大约有 <20 个顶点。
  • 这些图是 DAG。
  • 所有顶点都是非唯一标记的,并且主图和子图中对应的顶点应该具有相同的标记。我不知道我是否使用了正确的术语(因为我没有上过图论课程......)。它将类似于:

线图 A--B 是 A--B--A 的子图,但 A--A 不是 A--B--A 的子图。

任何建议都很好。顺便说一句,这不是一个家庭作业问题。 :D

I'm looking for an algorithm to check whether a given graph is subgraph of another given graph.

I have few conditions to make this NP complete problem bit more feasible..

  • The graphs have approx <20 vertices.
  • The graphs are DAG.
  • All vertices are non-uniquely labeled, and the corresponding vertices in the main graph and the subgraph should have same label. I don't know if I'm using the correct terminologies (because I haven't taken a graph theory course...). It will be something like:

The line graph A--B is subgraph of A--B--A but A--A is not a subgraph of A--B--A.

Any suggestions are fine. This is not a homework question btw. :D

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

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

发布评论

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

评论(3

就此别过 2024-08-30 08:58:45

如果标签是唯一的,则对于大小为 N 的图,有 O(N^2) 条边,假设每对之间没有自循环或多个边顶点。我们使用 E 作为边数。

如果对父图中的集合边进行散列,则可以遍历子图的边,检查每条边是否在散列表中(如果需要,则检查数量是否正确)。您需要对每条边执行一次此操作,因此 O(E)

我们将图称为 G(具有 N 顶点)和可能的子图 G_1(具有 M 顶点),并且您想要查找G_1 在G 中

由于标签不是唯一的,因此您可以使用动态编程来构建子问题 - 而不是 O(2^N) 个子问题,每个子图一个,您有 O (M 2^N) 子问题 - 对于每个可能的子图的 G_1 中的每个顶点(具有 M 顶点)。

G_1 位于 G = isSubgraph( 0,empty bitmask)

,状态设置如下:

isSubgraph( index, bitmask ) =
   for all vertex in G
       if G[vertex] is not used (check bitmask)
          and G[vertex]'s label is equal to G_1[index]'s label
          and isSubgraph( index + 1, (add vertex to bitmask) )
               return true
   return false

基本情况为 index = M,您可以检查给定位掩码(和隐式标签映射),边相等。或者,您也可以在 if 语句中进行检查 - 只需检查给定的当前 index,当前子图 G_1[0..index] 是否等于 递归之前的 G[bitmask] (具有相同的隐式标签映射)。

对于N = 20,这应该足够快了。

(添加您的备忘录,或者您可以使用自下而上的 DP 重写此内容)。

If the labels are unique, for a graph of size N, there are O(N^2) edges, assuming there are no self loops or multiple edges between each pair of vertices. Let's use E for the number of edges.

If you hash the set edges in the parent graph, you can go through the subgraph's edges, checking if each one is in the hash table (and in the correct amount, if desired). You're doing this once for each edge, therefore, O(E).

Let's call the graph G (with N vertices) and the possible subgraph G_1 (with M vertices), and you want to find G_1 is in G.

Since the labels are not unique, you can, with Dynamic Programming, build the subproblems as such instead - instead of having O(2^N) subproblems, one for each subgraph, you have O(M 2^N) subproblems - one for each vertex in G_1 (with M vertices) with each of the possible subgraphs.

G_1 is in G = isSubgraph( 0, empty bitmask)

and the states are set up as such:

isSubgraph( index, bitmask ) =
   for all vertex in G
       if G[vertex] is not used (check bitmask)
          and G[vertex]'s label is equal to G_1[index]'s label
          and isSubgraph( index + 1, (add vertex to bitmask) )
               return true
   return false

with the base case being index = M, and you can check for the edges equality, given the bitmask (and an implicit label-mapping). Alternatively, you can also do the checking within the if statement - just check that given current index, the current subgraph G_1[0..index] is equal to G[bitmask] (with the same implicit label mapping) before recursing.

For N = 20, this should be fast enough.

(add your memo, or you can rewrite this using bottoms up DP).

久随 2024-08-30 08:58:45

好吧,我必须问一个显而易见的问题。
1. 你有二十个顶点。首先深入了解每个图,按字母顺序排列
兄弟姐妹来获取字符串。
2. 一个图是另一个图的子图,当且仅当一个字符串在另一个字符串中。

那么,问题规范中还隐藏了哪些内容,导致这种情况不可行呢?

OK, I have to ask the obvious question.
1. You have twenty vertices. Walk each graph in depth first, alphabetical order among
siblings to get strings.
2. One graph is a subgraph of another iff one string is in another.

So, what else is hiding in the problem specification to make this infeasible?

八巷 2024-08-30 08:58:45

您可以将此视为对齐问题。基本上,您想要提出一个单射映射a,将 V_1 中的每个顶点映射到 V_2 中的顶点。对齐图a可以按如下方式评分:

s(a) = \sum_{(v,w) \in E_1} \sigma(v, w, a(v), a(w) ),

其中 \sigma(v, w, a(v), a(w)) = 1,if (a(v), a(w)) \in E_2 否则为 0。

我认为这可以表述为以整数线性规划的形式。

You can view this as an alignment problem. Basically you want to come up with an injective mapping a that maps every vertex in V_1 to a vertex in V_2. An alignment map a can be scored as follows:

s(a) = \sum_{(v,w) \in E_1} \sigma(v, w, a(v), a(w)),

where \sigma(v, w, a(v), a(w)) = 1, if (a(v), a(w)) \in E_2 otherwise it is 0.

I think that this can be formulated in the form of an integer linear program.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文