地图降低复杂性
假设我有这个输入:列表的列表
(def list-of-list-3 (列表 (列表 1 2 3) (列表 4 5 6) (列表 7 8 9)) )
(map #(reduce * %1) list-of-list3 )
在这种情况下,map-reduce 的复杂度为 O(n^2) ?
map-reduce 是否被翻译为两个嵌套的 for ?
所以当我运行上面的代码时以 clojure REPL 为例,复杂度时间看起来像是 O(n)。
当我复制输入大小( list-of-list-6 (list (list 1 2 3) (list 4 5 6) ( list 7 8 9) (list 8 2 3) (list 9 8 1) (list 7 6 4)) ) 时间以线性方式增加,而不是二次增加。
谁能说出为什么?
提前致谢
Lets suppose that i have this input: a list of list
(def list-of-list-3 (list (list 1 2 3) (list 4 5 6) (list 7 8 9)) )
(map #(reduce * %1) list-of-list3 )
The map-reduce has a O(n^2) complexity in this case?
is map-reduce translated as two nested for ?
So when i run the above example on the clojure REPL, the complexity time seems like O(n).
when i duplicate the input size ( list-of-list-6 (list (list 1 2 3) (list 4 5 6) ( list 7 8 9) (list 8 2 3) (list 9 8 1) (list 7 6 4)) ) the time increase in a linear way, not quadratic.
Can anyone say why ?
thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它不是 O(n^2),大约是 O(n*m),其中 n 是列表的数量,m 是列表的长度。
还有其他因素与各种数字的长度有关。自己动手做,计时看看为什么!
It's not O(n^2), it's roughly O(n*m) where n is the no of lists and m is the length of them.
There will be other factors as well to do with the lengths of various numbers. Do it by hand and time yourself to see why!
不可能说,除非你告诉我们
n
对应于list-of-list-3
表达式中的内容。顺便说一句,
O(n^2)
或O(n*n)
是二次复杂度,而不是指数复杂度。指数复杂度O(e^n)
。由此,我推测
n
应该是外部列表的长度。如果是这样,那么reduce
实际上是O(n)
而不是O(n^2)
。要获得二次增长,您需要将list-of-list-6
定义为:Impossible to say, unless you tell us what
n
corresponds to in thelist-of-list-3
expression.By the way,
O(n^2)
orO(n*n)
is quadratic complexity, not exponential complexity. Exponential complexity itO(e^n)
.From this, I surmise that the
n
is supposed to be the length of the outer list. If so, then thereduce
is actuallyO(n)
notO(n^2)
. To get quadratic growth, you would need to definelist-of-list-6
as: