TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Ask HN: O(n^n) procedure?

2 点作者 cao825超过 14 年前
I was discussing with some friends at work some of the worst algorithms (in terms of Big O), like Bogo Sort, Stooge Sort, etc... We were trying to think of how one could write a procedure with O(n^n) and all of our attempts ended up being infinite or much less than n^n.<p>I would imagine we just didn't think in the correct terms. Unfortunately, neither Google nor Wolframalpha were any help finding one from my searches.<p>Thanks!

1 comment

tgflynn超过 14 年前
If you think of an nxn matrix the set of all paths containing one element from each column has cardinality n^n. This is also the set of all functions from a set of cardinality n to itself or the set of all sequences of length n where each element is an element of a set of cardinality n.<p>Below is a procedure that generates it. The number of operations is actually sum from k =0 to k =n ( n^(n-k) ) but asymptotically that's still O( n^n ).<p><pre><code> def allFunctions( s, i = 0, count = 0 ): F = [] if i == len( s ) - 1: return ( [ [ x ] for x in s ], len( s ) ) (G,count) = allFunctions( s, i + 1, count ) for g in G: for i in range( len( s ) ): F.append( g + [ s[i] ] ) count += 1 return ( F, count )</code></pre>
评论 #2234782 未加载