基于 C++ 中的连接元素对元素进行分组
假设我有一对整数,例如: [(1,2), (3,4), (2,3), (3,5), (7, 8), (7,9)] 我想根据它们之间的连接元素将这些对排序到唯一的组中,即如果元素与同一组中的另一对共享公共元素,那么它们应该放入同一组中。因此,在上面的示例中,我们最终将得到两个组:
Group1:(1,2), (2,3), ( 3,4), (3,5)
第 2 组:(7,8), (7, 9 )
关键是应该至少有一个引用出现在前一对中(即这个是连接对的定义),并且前一对可以是之前对中的任何一对,而不是直接在其之前的对。如果没有“连接对”,那么该对应该单独分离到一个新组
我知道这可以通过图形来完成,但是有人能够建议最有效且易于在 C++ 中实现的解决方案吗?
谢谢!
Assume I have pair of integers like: [(1,2), (3,4), (2,3), (3,5), (7, 8), (7,9)] and I would like to sort the pairs into unique groups based on connected elements amongst them i.e. if elements share a common element with one other pair in the same group, then they should be put into the same group. So in the example above, we will end up with two groups:
Group1: (1,2), (2,3), (3,4), (3,5)
Group2: (7,8), (7, 9)
The key is that there should be at least one reference that appears in a previous pair (i.e. this is the definition of connected pairs), and this previous pair can be any one of the previous pairs, and not the one directly preceding it. If there are no "connected pairs", then the pair should be separated to a new group by itself
I understand that this can be done with graphs, but would anyone be able to suggest the most efficient, and easy to implement solution in C++?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设:成对有序。
(a,a),(a,b),(b,a)
是有效输入。是的,这个问题可以使用无向图来解决。
对于每个给定的对
(a, b)
,在a
和b
之间创建一条无向边。现在遍历该图以找到其连接的组件。记住每个节点的组件,例如。通过给它们着色。最后,对于所有输入对,检查它们所属的组件(也称为颜色)并将其添加到该组中。单独对每个组进行排序并输出。时间复杂度
令
n
为输入中的对数。遍历:
O(n)
。我们构建的图中有O(n)
个节点和O(n)
个边。对于下面给定的 C++ 代码 - 我们将最多访问每条边两次(由于无向图)。任何已访问的节点都会从第一行本身返回,这发生在该节点上的每个事件边上。所有节点上此类关联边的计数为 O(n)。排序:
O(n * log n)
,因为组的最大大小可以是O(n)
总计:
O(n * log n)
C++17 中的示例代码:
我将图存储为邻接列表
adj
并使用深度优先遍历dfs.假设输入整数适合
int
,如果它们有进一步的限制,您还可以使用vector
代替unordered_map
。输出:
Assumptions: Pairs are ordered.
(a,a),(a,b),(b,a)
is a valid input.Yes this problem can be solved using an undirected graph.
For each given pair
(a, b)
, create an undirected edge betweena
andb
. Now traverse this graph to find its connected components. Remember the component of each node, eg. by coloring them. Finally for all input pairs check the component they belong to (aka color) and add it to that group. Sort each group individually and output.Time complexity
Let
n
be the number of pairs in input.Traversal:
O(n)
. There areO(n)
nodes andO(n)
edges in the graph we built. For given C++ code below - we will visit each edge at most twice (due to undirected graph). Any already visited node is returned from the first line itself, which happens for each incident edge on that node. Count of such incident edges over all nodes isO(n)
.Sorting:
O(n * log n)
since maximum size of group can beO(n)
Total:
O(n * log n)
Sample code in C++17:
I am storing the graph as an adjacency list
adj
and using depth first traversaldfs
. Assuming the input integers fit into anint
, you can also usevector
s instead ofunordered_map
s if they are further bounded.Output:
O(n^2) 解决方案,这将为您提供组数,可以修改代码以获取组值
O(n^2) solution, this will give you the number of groups, the code can be modified to get the groups values