{"id":95145,"date":"2017-01-09T07:00:00","date_gmt":"2017-01-09T22:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/?p=95145"},"modified":"2019-03-13T01:04:15","modified_gmt":"2019-03-13T08:04:15","slug":"20170109-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20170109-00\/?p=95145","title":{"rendered":"Applying a permutation to a vector, part 4: What is the computational complexity of the apply_permutation function?"},"content":{"rendered":"<p>One question left unanswered was the computational complexity of <a HREF=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/\">the <code>apply_permutation<\/code> function<\/a>. <\/p>\n<p>Here it is again: <\/p>\n<pre>\ntemplate&lt;typename T&gt;\nvoid\napply_permutation(\n    std::vector&lt;T&gt;&amp; v,\n    std::vector&lt;int&gt;&amp; indices)\n{\n using std::swap; \/\/ to permit Koenig lookup\n for (size_t i = 0; i &lt; indices.size(); i++) {\n  auto current = i;\n  while (i != indices[current]) {\n   auto next = indices[current];\n   swap(v[current], v[next]);\n   indices[current] = current;\n   current = next;\n  }\n  indices[current] = current;\n }\n}\n<\/pre>\n<p>The outer <code>for<\/code> loop runs <var>N<\/var> times; that&#8217;s easy to see. The maximum number of iterations of the inner <code>while<\/code> loop is a bit less obvious, but if you understood the discussion, you&#8217;ll see that it runs at most <var>N<\/var> times because that&#8217;s the maximum length of a cycle in the permutation. (Of course, this analysis requires that the <code>indices<\/code> be a permutation of 0 &hellip; <var>N<\/var> &minus; 1.) <\/p>\n<p>Therefore, a na&iuml;ve analysis would conclude that this has worst-case running time of <var>O<\/var>(<var>N<\/var>&sup2;) because the outer <code>for<\/code> loop runs <var>N<\/var> times, and the inner <code>while<\/code> loop also runs <var>N<\/var> times in the worst case. <\/p>\n<p>But it&#8217;s not actually that bad. The complexity is only <var>O<\/var>(<var>N<\/var>), because not all of the worst-case scenarios can exist simultaneously. <\/p>\n<p>One way to notice this is to observe that each item moves only once, namely to its final position. Once an item is in the correct position, it never moves again. In terms of indices, observe that each swap corresponds to an assignment <code>indices[current] = current<\/code>. Therefore, each entry in the index array gets set to its final value. And the <code>while<\/code> loop doesn&#8217;t iterate at all if <code>indices[current] == current<\/code>, so when we revisit an element that has already moved to its final location, we do nothing. <\/p>\n<p>Since each item moves at most only once, and there are <var>N<\/var> items, then the total number of iterations of the inner <code>while<\/code> loop is at most <var>N<\/var>. <\/p>\n<p>Another way of looking at this is that the <code>while<\/code> loop walks through every cycle. But mathematics tell us that permutations decompose into disjoint cycles, so the number of elements involved in the cycles cannot exceed the total number of elements. <\/p>\n<p>Either way, the conclusion is that there are most <var>N<\/var> iterations of the inner <code>while<\/code> loop in total. Combine this with the fixed overhead of <var>N<\/var> iterations of the <code>for<\/code> loop, and you see that the total running time complexity is <var>O<\/var>(<var>N<\/var>). <\/p>\n<p>(We can determine via inspection that the algorithm consumes constant additional space.) <\/p>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;s linear, though it doesn&#8217;t look that way at first glance.<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-95145","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>It&#8217;s linear, though it doesn&#8217;t look that way at first glance.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/95145","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=95145"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/95145\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=95145"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=95145"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=95145"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}