简体   繁体   English

计算无向图中所有具有约束的顶点对

[英]count all pair of vertex with constraints in an undirected graph

I'm struggling the following an algorithmic puzzle:我正在努力解决以下算法难题:

given a graph with N vertex and N edges I've to count all pairs of vertex (A,B) with the following properties: A>B and exists at least one path made of all vertex labeled with numbers between A and B...给定一个具有 N 个顶点和 N 个边的图,我必须计算具有以下属性的所有顶点对 (A,B):A>B 并且至少存在一条由所有标有 A 和 B 之间数字的顶点组成的路径。 .

As example (1,5) is a valid pair of vertex if the following path exists: (1,2,3,4,5) but would be ok even if the following would exists (1,5,2,3,4) or (1,3,4,3,2,3,5)... I'm not interested in the length of the path or its order it just have to include all vertex which label is >= A and <= B. I've tried with modified bfs and dfs but had no success.例如,如果以下路径存在,则 (1,5) 是一对有效的顶点: (1,2,3,4,5) 但即使存在以下路径也没有问题 (1,5,2,3,4 ) 或 (1,3,4,3,2,3,5)... 我对路径的长度或其顺序不感兴趣,它只需要包含标签为 >= A 和 <= 的所有顶点B. 我尝试过修改 bfs 和 dfs,但没有成功。

can some one help?有人可以帮忙吗?

give some hint?给点提示?

thanks谢谢

Delete all edges which are connecting two nodes with not consecutive numbers.删除连接两个不连续编号的节点的所有边。 (eg a edge connecting node 2 with node 6, but not a edge from node 2 to node 3). (例如,连接节点 2 和节点 6 的边,但不是连接节点 2 到节点 3 的边)。 In the modified graph, each node has either degree 0, 1 or 2.在修改后的图中,每个节点的度数为 0、1 或 2。

Now traverse the graph starting at node 1. Store all visited nodes in a list.现在从节点 1 开始遍历图。将所有访问过的节点存储在一个列表中。 If you reach a node with degree 1, start again on node 2. (and then on node 3 and so on).如果到达度数为 1 的节点,请在节点 2 上重新开始。(然后在节点 3 上以此类推)。

Consider the following example output for a graph with 7 nodes and edges E={{1,2},{2,3},{3,4},{4,5},{6,7}}.考虑具有 7 个节点和边 E={{1,2},{2,3},{3,4},{4,5},{6,7}} 的图的以下示例输出。 Please note that 1..5 denotes a list with the following elements {1},{2},{3},{4},{5}.请注意,1..5 表示包含以下元素 {1}、{2}、{3}、{4}、{5} 的列表。

node |节点 | list |列表 | valid pairs有效对

1 | 1 | 1..5 | 1..5 | 4 {(1,5),(1,4),(1,3),(1,2)} 4 {(1,5),(1,4),(1,3),(1,2)}

2 | 2 | 2..5 | 2..5 | 3 {(2,5),{2,4),(2,3)} 3 {(2,5),{2,4),(2,3)}

3 | 3 | 3..5 | 3..5 | 2 2

4 | 4 | 4..5 | 4..5 | 1 1

5 | 5 | 5..5 | 5..5 | 0 0

6 | 6 | 6..7 | 6..7 | 1 1

7 | 7 | 7..7 | 7..7 | 0 0

This should answer your question.这应该回答你的问题。 Sorry for the bad formatting of the table.抱歉表格格式不正确。 As you can see in the table, you can calculate the valid pairs for node 2,3,4 and 5 once you have all pairs for node 1. So there is some space to improve my algorithm.正如您在表中看到的,一旦您拥有节点 1 的所有对,您就可以计算节点 2、3、4 和 5 的有效对。因此,还有一些空间可以改进我的算法。

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

相关问题 顶点度均匀的随机无向连接图 - random undirected conected graph with even vertex degree 无向图添加/删除顶点; removeEdge方法 - Undirected Graph add/remove Vertex; removeEdge methods 无向图中的最大循环数 - Maximum Count Of Cycles In An Undirected Graph 如何在一对节点之间的无向图中找到所有边不相交的等价路径? - how to find all edge-disjoint equi-cost paths in an undirected graph between a pair of nodes? 使无向图中的所有节点组合对的最佳算法(需要提高时间复杂度) - Best algorthm to get all combination pair of nodes in an undirected graph (need to improve time complexity) 将无向图转换为有向图,使得每个顶点的入度至少为 2 - Convert undirected graph to directed graph such that the indegree of each vertex is at least 2 查找无向图中的所有循环 - Find all cycles in undirected graph 在同一顶点开始和结束的无权无向图中的最长路径 - Longest path in unweighted undirected graph starting and finishing in the same vertex 向加权无向图添加顶点时,保留哪个权重? - When adding a vertex to a weighted undirected graph, which weight stays? 给定一些约束的连接无向图中的多个生成树 - Multiple spanning trees in connected undirected graph given some constraints
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM