मेरे कोड है:इस कोड की जटिलता क्या होगी?
vector<int> permutation(N);
vector<int> used(N,0);
void try(int which, int what) {
// try taking the number "what" as the "which"-th element
permutation[which] = what;
used[what] = 1;
if (which == N-1)
outputPermutation();
else
// try all possibilities for the next element
for (int next=0; next<N; next++)
if (!used[next])
try(which+1, next);
used[what] = 0;
}
int main() {
// try all possibilities for the first element
for (int first=0; first<N; first++)
try(0,first);
}
मैं कुछ वेबसाइट है, जहां मैं इस कोड में आए से जटिलता सीख रही थी। मेरी समझ के अनुसार, निम्नलिखित पंक्ति एन बार पुनरावृत्त करती है। तो जटिलता ओ (एन) है।
for (int first=0; first<N; first++)
अगला मैं रिकर्सिव कॉल पर विचार कर रहा हूं।
for (int next=0; next<N; next++)
if (!used[next])
try(which+1, next);
तो, यह पुनरावर्ती कॉल कदम शामिल = टी (एन) की संख्या = Nc + टी (0)। (कुछ निरंतर कदम जहां है) वहाँ हम कह सकते हैं कि इस चरण के लिए, जटिलता है = ओ (एन)।
इस प्रकार कुल जटिलता है - ओ (N.N) = हे (एन^2)
मेरी समझ सही है? धन्यवाद!
बहुत बहुत धन्यवाद! बिल्कुल सही जवाब! –