首页 \ 问答 \ 改变阵列的顺序(usort changing Array's order)

改变阵列的顺序(usort changing Array's order)

我有一个单行的usort函数:return 0。
我试图在stdClass对象的数组上使用它,它会改变
他们的订单,这怎么可能?


I have a usort function with a single line: return 0.
I tried to use it on an Array of stdClass objects, and it changes
their order, how is that possible?


原文:https://stackoverflow.com/questions/7283877
更新时间:2023-09-15 14:09

最满意答案

嗯,它可以在某种程度上并行化。 它如下:

最初,所有边缘都按升序排序。 有一个main thread实际上从头开始扫描每个边缘,并决定添加当前边缘是否形成周期。 我们将算法并行化的主要目的是使这些检查并行。

这是我们使用worker threads 。 每个线程都有一定数量的边来检查,每个线程在每次迭代后检查其边是否与当前表示形成一个循环(迭代意味着主线程添加新边)。 当主线程继续添加边时,一些线程看到某些边已经与当前表示形成一个循环。

这些边缘被标记为discarded 。 当主线程到达这样的边缘时,它只是移动到下一个边缘而不对其进行任何检查。

因此,我们实际上已经将这些检查并行,这意味着算法快速运行以提高效率。

事实上,有一篇很好的论文使用了与上述相同的想法。

编辑:

如果您非常关心整体算法的运行时间,您甚至可以最初使用并行排序算法,如@ jarod42建议的那样。


Well, it can be parallelized to some extent. It is as follows:

Initially all the edges are sorted in the ascending order. There is a main thread which actually scans each edge from the beginning and decides whether adding the current edge forms the cycle. Our main aim in parallelizing the algorithm is to make these checks parallel.

This is where we use the worker threads. Each thread is given certain number of edges to examine, where in each thread checks if its edges form a cycle with the current representation after every iteration (iteration means the main thread adding a new edge). As the main thread keeps on adding the edges, some threads see that certain edges are already forming a cycle with the current representation.

Such edges are marked as discarded. When the main thread reaches such edges, it simply moves on to the next one without making any check on it.

Thus, we have actually made these checks parallel, which means the algorithm runs quickly increasing the efficiency.

In fact, there is a nice paper that uses the same idea described above.

EDIT:

If you are pretty much concerned about the running time of over-all algorithm, you can even use a parallel sorting algorithm initially as @jarod42 suggested.

相关问答

更多
  • find
  • 是的,由于Michael J. Fischer在1972年有一个匹配的下界(见第3节)。 他的例子使用深度log n的二叉树,其中深度为0的二叉树是单个节点,深度为k的二叉树是深度为k-1的两个二叉树,其中一个树的根指向另一个。 通过反复将二叉树的根联合指向单例并在最深的节点上进行查找,我们执行一个昂贵的(对数步骤)操作,使另一个嵌入式二叉树被利用。 Python演示:这将打印(k+1) * 2**k ,其中k是example的参数,表示O(2**k)键上O(2**k)操作的近似操作计数。 p = {} ...
  • union方法的每次调用都需要迭代id数组,这需要O(n)次。 如果你调用union方法n次,那么所需的时间是n*O(n) = O(n^2) 。 通过将连接方法的时间复杂度提高到O(log n) ,可以将union方法的时间复杂度提高到O(1) O(log n) ,但这只是一次操作。 我相信你的课本详细解释了这一点。 Each invocation of union method requires you iterate over the id array, which takes O(n) time. I ...
  • 从一个列表指针数组开始,它们等于你拥有的位数,由空指针组成。 对于每个元素: 如果没有设置与这些位对应的数组元素,则创建一个新列表并将所有数组元素设置为指向列表。 否则,对于每个初始化的数组元素,合并列表,使指针指向新列表。 还将所有未初始化的数组元素设置为新列表。 将自动递增的值添加到新列表中。 浏览数组,输出所有唯一列表(仅处理唯一列表,您可以在处理完列表后设置一些指标,因此不要处理两次)。 哦,对,我们还需要union-find here - 否则合并列表可能会留下一些指向旧列表的其他元素。 关于un ...
  • 您可以使用双循环,首先找到根,然后将所有内容设置为指向它。 public int find(int p) { int curr = p; while (curr != id[curr]) curr = id[curr]; //find root //at this point, curr is the root while (p != id[p]) { int next = id[p]; id[p] = curr; //remember, cur ...
  • 嗯,它可以在某种程度上并行化。 它如下: 最初,所有边缘都按升序排序。 有一个main thread实际上从头开始扫描每个边缘,并决定添加当前边缘是否形成周期。 我们将算法并行化的主要目的是使这些检查并行。 这是我们使用worker threads 。 每个线程都有一定数量的边来检查,每个线程在每次迭代后检查其边是否与当前表示形成一个循环(迭代意味着主线程添加新边)。 当主线程继续添加边时,一些线程看到某些边已经与当前表示形成一个循环。 这些边缘被标记为discarded 。 当主线程到达这样的边缘时,它只 ...
  • 并且了解建议使用union-find,以便检查添加边创建循环是否只需要O(Log V)时间 这不对。 使用union find是O(alpha(n) * m) ,其中alpha(n)是Ackermann函数的反函数,并且,对于所有意图和目的,可以认为是常数。 比对数快得多: 由于alpha(n)是此函数的反函数,因此对于n所有远程实用值, alpha(n)小于5。 因此,每次操作的摊销运行时间实际上是一个很小的常数。 但Kruskal的大部分复杂性来自于删除E次优先级队列的最小元素 这也是错误的。 Krus ...
  • 我想你应该删除你的最后一行: id[i] = j; 它破坏了你的id数组。 I think you should remove your last line : id[i] = j; it's corrupting your id array.
  • 据我所知,代码中,数组iz []表示给定不相交集中的元素数量。 压缩路径时,不会为每个集修改该编号。 因此,路径压缩不会影响iz []数组。 As far as I understood the code, the array iz[] represents the amount of elements in a given disjoint set. When you compress the path, you don't modify that number for each set. Thus, t ...
  • 在所有三种MST算法中,只有Boruvka的 MST算法易于并行化,而kruskal和prims是顺序贪心算法,因此并行实现它们的范围最小。 注意:实现高效并行boruvka的研究课题可能会找到一些论文 Of all three MST algorithms only Boruvka's MST algorithm is easily parallelizable whereas the kruskal and prims are sequential greedy algorithms hence the ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)