修正後、時間複雑度は実際にはO(2^n)であると結論付けました。big-oを使用した時間複雑度解析
質問は時間の複雑さとは何ですか?それはO(2^n)か?
私はこれがforループのためだと思う理由は、n回runnedとみなされます。ネストされたwhileループは2^n回実行されます。 2番目のwhileループは2^n回実行されます。
Algorithm subsetsGenerator(T)
Input: A set T of n elements
Output: All the subsets of T stored in a queue Q {
create an empty queue Q;
create an empty stack S;
let a1, a2, …, an be all the elements in T;
S.push({}); // Push the empty subset into the stack
S.push({a1})
for (each element ai in T-{a1})
{
while (S is not empty)
{
x=S.pop;
Q.enqueue(x);
x=x ∪ { ai };
Q.enqueue(x);
}
if (ai is not the last element)
while (Q is not empty)
{
x=Q.dequeue();
S.push(x);
}
}
}
編集:解析を分解したい場合は、以下のようにコメントしてください。
出力の大きさは分かりますか?一度に1つずつ要素を出力に追加すると、それを出力するのにどれくらいの時間がかかりますか?あなたとあなたの友人の両方が分析を間違ってしまった。 – user2357112
はい、まだ考えていますが、4つの要素のセットを使って分析しています。もう一度やり直すと、各whileループは2^nを返し、forループはちょうどnを返します。 – Mikeez
ここに質問がありますか? – Charlie