简体   繁体   English

有向图问题的算法

[英]Algorithm for a directed graph problem

Please help me out with an algorithm for the following problem - 请帮我解决以下问题的算法-

Given a collection of facts, we would like to get rid of as much redundancy as possible. 鉴于事实,我们希望消除尽可能多的冗余。 The facts involved in this problem are members of a transitive relation between uppercase letters. 此问题涉及的事实是大写字母之间的传递关系的成员。 So each fact is a pair of uppercase letters such as AB meaning that A is related to B. A letter may or may not be related to itself, but transitivity holds: if A is related to B and B is related to C then we can infer that A is related to C. Create a class FactCount that contains a method minFacts that is given a String[] known and that returns the size of the smallest set of facts that will allow us to infer everything (and only those things) that can be inferred from the facts contained in known. 因此,每个事实都是一对大写字母,例如AB,表示A与B相关。一个字母可能与自身无关,但传递性成立:如果A与B相关并且B与C相关,则我们可以推断A与C有关。创建一个FactCount类,其中包含一个minFacts方法,该方法被赋予String []已知,并返回最小事实集的大小,这将使我们能够推断所有(且仅那些)事实可以从已知的事实推断出来。

Each element of known will contain 1 or more facts separated by a single space. 每个已知元素将包含一个或多个事实,并用一个空格隔开。 The smallest set of facts may contain facts that can be inferred from known but that are not contained in it. 最小的事实集可能包含可以从已知推断的事实,但不包含在其中。

For example: 例如:

{"AB AC CA AA BC", "AD"} {“ AB AC CA AA BC”,“ AD”}

Returns: 4 返回:4

AB, CA, BC, and AD allow us to infer both AA (AB, BC, CA gives AA by transitivity) and AC (AB, BC gives AC by transitivity), and there is no smaller subset that allows us to infer all the known facts. AB,CA,BC和AD允许我们推断AA(AB,BC,CA通过传递性得出AA)和AC(AB,BC通过传递性给出AC),并且没有较小的子集可以让我们推断所有已知事实。

PS - Its NOT homework. PS-不是作业。 Just a problem I found online and have been unable to solve for hours... 只是我在网上发现的一个问题,几个小时来一直无法解决...

It looks to me you are looking for a spanning tree of your graph. 在我看来,您正在寻找图形的生成树

A spanning tree of a connected graph G can also be defined as a maximal set of edges of G that contains no cycle, or as a minimal set of edges that connect all vertices. 连通图G的生成树也可以定义为G的不包含循环的最大边集,或定义为连接所有顶点的最小边集。

From the spanning tree, you have a minimal representation of the graph. 在生成树中,您具有图形的最小表示。 Any other addition of edges will create a cycle, and therefore redundant information for the relationships among nodes. 边的任何其他添加都将创建一个循环,因此会为节点之间的关系提供冗余信息。

Also please note that if, at the end of the algorithm, you have non-connected nodes remaining (meaning, there are nodes in your graph that your MST does not touch), it means that the spanning tree you obtained is an independent entity, and there's no relationship (edge) connecting the nodes of your spanning tree and the nodes not in it. 还请注意,如果在算法结束时还剩下未连接的节点(这意味着图形中的某些节点您的MST不会碰到),则意味着您获得的生成树是一个独立的实体,并且没有关系(边缘)连接生成树的节点和不在其中的节点。

In more mathematical terms, the nodes belonging to your spanning tree and the nodes not belonging to it are disjoint sets, and none of them is the empty set. 用更多的数学术语来说,属于生成树的节点和不属于生成树的节点是不相交的集,而它们都不是空集。

You can of course perform the MST search again on the remaining subset to generate a spanning forest, until you exhaust your set of nodes. 当然,您可以对其余的子集再次执行MST搜索,以生成生成林,直到耗尽节点集为止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM