mirror of
https://github.com/mfocko/blog.git
synced 2024-11-14 01:59:42 +01:00
1 line
No EOL
27 KiB
JavaScript
1 line
No EOL
27 KiB
JavaScript
"use strict";(self.webpackChunkfi=self.webpackChunkfi||[]).push([[3707],{69383:(e,s,n)=>{n.r(s),n.d(s,{assets:()=>r,contentTitle:()=>l,default:()=>d,frontMatter:()=>i,metadata:()=>c,toc:()=>m});var a=n(85893),t=n(11151);const i={id:"extend",title:"Time complexity of \u2039extend\u203a",description:"How to make inefficient algorithm unknowingly.\n",tags:["c","python","dynamic array","time complexity","recursion"],last_update:{date:new Date("2021-03-31T00:00:00.000Z")}},l=void 0,c={id:"time-complexity/extend",title:"Time complexity of \u2039extend\u203a",description:"How to make inefficient algorithm unknowingly.\n",source:"@site/algorithms/03-time-complexity/2021-03-31-extend.md",sourceDirName:"03-time-complexity",slug:"/time-complexity/extend",permalink:"/algorithms/time-complexity/extend",draft:!1,unlisted:!1,editUrl:"https://github.com/mfocko/blog/tree/main/algorithms/03-time-complexity/2021-03-31-extend.md",tags:[{label:"c",permalink:"/algorithms/tags/c"},{label:"python",permalink:"/algorithms/tags/python"},{label:"dynamic array",permalink:"/algorithms/tags/dynamic-array"},{label:"time complexity",permalink:"/algorithms/tags/time-complexity"},{label:"recursion",permalink:"/algorithms/tags/recursion"}],version:"current",lastUpdatedAt:1617148800,formattedLastUpdatedAt:"Mar 31, 2021",frontMatter:{id:"extend",title:"Time complexity of \u2039extend\u203a",description:"How to make inefficient algorithm unknowingly.\n",tags:["c","python","dynamic array","time complexity","recursion"],last_update:{date:"2021-03-31T00:00:00.000Z"}},sidebar:"autogeneratedBar",previous:{title:"Asymptotic Notation and Time Complexity",permalink:"/algorithms/category/asymptotic-notation-and-time-complexity"},next:{title:"Recursion",permalink:"/algorithms/category/recursion"}},r={},m=[{value:"Introduction",id:"introduction",level:2},{value:"Technicalities",id:"technicalities",level:2},{value:"Example #1",id:"example-1",level:2},{value:"Example #2",id:"example-2",level:2},{value:"Implementation of <code>extend</code>",id:"implementation-of-extend",level:2}];function h(e){const s={a:"a",annotation:"annotation",code:"code",em:"em",h2:"h2",img:"img",li:"li",math:"math",mi:"mi",mn:"mn",mo:"mo",mrow:"mrow",mtext:"mtext",p:"p",pre:"pre",semantics:"semantics",span:"span",strong:"strong",ul:"ul",...(0,t.a)(),...e.components};return(0,a.jsxs)(a.Fragment,{children:[(0,a.jsx)(s.h2,{id:"introduction",children:"Introduction"}),"\n",(0,a.jsxs)(s.p,{children:["Each year there is a lot of confusion regarding time complexity of the ",(0,a.jsx)(s.code,{children:"extend"})," operation on the lists in Python. I will introduce two specific examples from previous year and also will try to explain it on one of the possible implementations of ",(0,a.jsx)(s.code,{children:"extend"})," operation."]}),"\n",(0,a.jsx)(s.h2,{id:"technicalities",children:"Technicalities"}),"\n",(0,a.jsxs)(s.p,{children:["At the beginning we should clear some of the \u201cmyths\u201d regarding extending of the lists. There is a common misunderstanding regarding differences between ",(0,a.jsx)(s.code,{children:"a += b"}),", ",(0,a.jsx)(s.code,{children:"a.extend(b)"})," and ",(0,a.jsx)(s.code,{children:"a + b"}),"."]}),"\n",(0,a.jsxs)(s.ul,{children:["\n",(0,a.jsxs)(s.li,{children:["\n",(0,a.jsxs)(s.p,{children:[(0,a.jsx)(s.code,{children:"a.extend(b)"})," - adds all elements from ",(0,a.jsx)(s.code,{children:"b"})," to ",(0,a.jsx)(s.code,{children:"a"}),"."]}),"\n",(0,a.jsxs)(s.p,{children:["Time complexity: ",(0,a.jsxs)(s.span,{className:"katex",children:[(0,a.jsx)(s.span,{className:"katex-mathml",children:(0,a.jsx)(s.math,{xmlns:"http://www.w3.org/1998/Math/MathML",children:(0,a.jsxs)(s.semantics,{children:[(0,a.jsxs)(s.mrow,{children:[(0,a.jsx)(s.mi,{mathvariant:"script",children:"O"}),(0,a.jsx)(s.mo,{stretchy:"false",children:"("}),(0,a.jsx)(s.mi,{children:"n"}),(0,a.jsx)(s.mo,{stretchy:"false",children:")"})]}),(0,a.jsx)(s.annotation,{encoding:"application/x-tex",children:"\\mathcal{O}(n)"})]})})}),(0,a.jsx)(s.span,{className:"katex-html","aria-hidden":"true",children:(0,a.jsxs)(s.span,{className:"base",children:[(0,a.jsx)(s.span,{className:"strut",style:{height:"1em",verticalAlign:"-0.25em"}}),(0,a.jsx)(s.span,{className:"mord mathcal",style:{marginRight:"0.02778em"},children:"O"}),(0,a.jsx)(s.span,{className:"mopen",children:"("}),(0,a.jsx)(s.span,{className:"mord mathnormal",children:"n"}),(0,a.jsx)(s.span,{className:"mclose",children:")"})]})})]}),", where ",(0,a.jsxs)(s.span,{className:"katex",children:[(0,a.jsx)(s.span,{className:"katex-mathml",children:(0,a.jsx)(s.math,{xmlns:"http://www.w3.org/1998/Math/MathML",children:(0,a.jsxs)(s.semantics,{children:[(0,a.jsx)(s.mrow,{children:(0,a.jsx)(s.mi,{children:"n"})}),(0,a.jsx)(s.annotation,{encoding:"application/x-tex",children:"n"})]})})}),(0,a.jsx)(s.span,{className:"katex-html","aria-hidden":"true",children:(0,a.jsxs)(s.span,{className:"base",children:[(0,a.jsx)(s.span,{className:"strut",style:{height:"0.4306em"}}),(0,a.jsx)(s.span,{className:"mord mathnormal",children:"n"})]})})]})," denotes the length of ",(0,a.jsx)(s.code,{children:"b"}),"."]}),"\n"]}),"\n",(0,a.jsxs)(s.li,{children:["\n",(0,a.jsxs)(s.p,{children:[(0,a.jsx)(s.code,{children:"a += b"})," - equivalent to ",(0,a.jsx)(s.code,{children:"a.extend(b)"})]}),"\n"]}),"\n",(0,a.jsxs)(s.li,{children:["\n",(0,a.jsxs)(s.p,{children:[(0,a.jsx)(s.code,{children:"a + b"})," - constructs a new list that contains elements from ",(0,a.jsx)(s.code,{children:"a"})," followed by\nelements from ",(0,a.jsx)(s.code,{children:"b"}),"."]}),"\n",(0,a.jsxs)(s.p,{children:["Time complexity: ",(0,a.jsxs)(s.span,{className:"katex",children:[(0,a.jsx)(s.span,{className:"katex-mathml",children:(0,a.jsx)(s.math,{xmlns:"http://www.w3.org/1998/Math/MathML",children:(0,a.jsxs)(s.semantics,{children:[(0,a.jsxs)(s.mrow,{children:[(0,a.jsx)(s.mi,{mathvariant:"script",children:"O"}),(0,a.jsx)(s.mo,{stretchy:"false",children:"("}),(0,a.jsx)(s.mi,{children:"m"}),(0,a.jsx)(s.mo,{children:"+"}),(0,a.jsx)(s.mi,{children:"n"}),(0,a.jsx)(s.mo,{stretchy:"false",children:")"})]}),(0,a.jsx)(s.annotation,{encoding:"application/x-tex",children:"\\mathcal{O}(m + n)"})]})})}),(0,a.jsxs)(s.span,{className:"katex-html","aria-hidden":"true",children:[(0,a.jsxs)(s.span,{className:"base",children:[(0,a.jsx)(s.span,{className:"strut",style:{height:"1em",verticalAlign:"-0.25em"}}),(0,a.jsx)(s.span,{className:"mord mathcal",style:{marginRight:"0.02778em"},children:"O"}),(0,a.jsx)(s.span,{className:"mopen",children:"("}),(0,a.jsx)(s.span,{className:"mord mathnormal",children:"m"}),(0,a.jsx)(s.span,{className:"mspace",style:{marginRight:"0.2222em"}}),(0,a.jsx)(s.span,{className:"mbin",children:"+"}),(0,a.jsx)(s.span,{className:"mspace",style:{marginRight:"0.2222em"}})]}),(0,a.jsxs)(s.span,{className:"base",children:[(0,a.jsx)(s.span,{className:"strut",style:{height:"1em",verticalAlign:"-0.25em"}}),(0,a.jsx)(s.span,{className:"mord mathnormal",children:"n"}),(0,a.jsx)(s.span,{className:"mclose",children:")"})]})]})]}),", where ",(0,a.jsxs)(s.span,{className:"katex",children:[(0,a.jsx)(s.span,{className:"katex-mathml",children:(0,a.jsx)(s.math,{xmlns:"http://www.w3.org/1998/Math/MathML",children:(0,a.jsxs)(s.semantics,{children:[(0,a.jsxs)(s.mrow,{children:[(0,a.jsx)(s.mi,{children:"m"}),(0,a.jsx)(s.mo,{separator:"true",children:","}),(0,a.jsx)(s.mi,{children:"n"})]}),(0,a.jsx)(s.annotation,{encoding:"application/x-tex",children:"m, n"})]})})}),(0,a.jsx)(s.span,{className:"katex-html","aria-hidden":"true",children:(0,a.jsxs)(s.span,{className:"base",children:[(0,a.jsx)(s.span,{className:"strut",style:{height:"0.625em",verticalAlign:"-0.1944em"}}),(0,a.jsx)(s.span,{className:"mord mathnormal",children:"m"}),(0,a.jsx)(s.span,{className:"mpunct",children:","}),(0,a.jsx)(s.span,{className:"mspace",style:{marginRight:"0.1667em"}}),(0,a.jsx)(s.span,{className:"mord mathnormal",children:"n"})]})})]})," denote the length of\n",(0,a.jsx)(s.code,{children:"a"})," and ",(0,a.jsx)(s.code,{children:"b"})," respectively."]}),"\n",(0,a.jsxs)(s.p,{children:["Space complexity: ",(0,a.jsxs)(s.span,{className:"katex",children:[(0,a.jsx)(s.span,{className:"katex-mathml",children:(0,a.jsx)(s.math,{xmlns:"http://www.w3.org/1998/Math/MathML",children:(0,a.jsxs)(s.semantics,{children:[(0,a.jsxs)(s.mrow,{children:[(0,a.jsx)(s.mi,{mathvariant:"script",children:"O"}),(0,a.jsx)(s.mo,{stretchy:"false",children:"("}),(0,a.jsx)(s.mi,{children:"m"}),(0,a.jsx)(s.mo,{children:"+"}),(0,a.jsx)(s.mi,{children:"n"}),(0,a.jsx)(s.mo,{stretchy:"false",children:")"})]}),(0,a.jsx)(s.annotation,{encoding:"application/x-tex",children:"\\mathcal{O}(m + n)"})]})})}),(0,a.jsxs)(s.span,{className:"katex-html","aria-hidden":"true",children:[(0,a.jsxs)(s.span,{className:"base",children:[(0,a.jsx)(s.span,{className:"strut",style:{height:"1em",verticalAlign:"-0.25em"}}),(0,a.jsx)(s.span,{className:"mord mathcal",style:{marginRight:"0.02778em"},children:"O"}),(0,a.jsx)(s.span,{className:"mopen",children:"("}),(0,a.jsx)(s.span,{className:"mord mathnormal",children:"m"}),(0,a.jsx)(s.span,{className:"mspace",style:{marginRight:"0.2222em"}}),(0,a.jsx)(s.span,{className:"mbin",children:"+"}),(0,a.jsx)(s.span,{className:"mspace",style:{marginRight:"0.2222em"}})]}),(0,a.jsxs)(s.span,{className:"base",children:[(0,a.jsx)(s.span,{className:"strut",style:{height:"1em",verticalAlign:"-0.25em"}}),(0,a.jsx)(s.span,{className:"mord mathnormal",children:"n"}),(0,a.jsx)(s.span,{className:"mclose",children:")"})]})]})]}),", where ",(0,a.jsxs)(s.span,{className:"katex",children:[(0,a.jsx)(s.span,{className:"katex-mathml",children:(0,a.jsx)(s.math,{xmlns:"http://www.w3.org/1998/Math/MathML",children:(0,a.jsxs)(s.semantics,{children:[(0,a.jsxs)(s.mrow,{children:[(0,a.jsx)(s.mi,{children:"m"}),(0,a.jsx)(s.mo,{separator:"true",children:","}),(0,a.jsx)(s.mi,{children:"n"})]}),(0,a.jsx)(s.annotation,{encoding:"application/x-tex",children:"m, n"})]})})}),(0,a.jsx)(s.span,{className:"katex-html","aria-hidden":"true",children:(0,a.jsxs)(s.span,{className:"base",children:[(0,a.jsx)(s.span,{className:"strut",style:{height:"0.625em",verticalAlign:"-0.1944em"}}),(0,a.jsx)(s.span,{className:"mord mathnormal",children:"m"}),(0,a.jsx)(s.span,{className:"mpunct",children:","}),(0,a.jsx)(s.span,{className:"mspace",style:{marginRight:"0.1667em"}}),(0,a.jsx)(s.span,{className:"mord mathnormal",children:"n"})]})})]})," denote the length of\n",(0,a.jsx)(s.code,{children:"a"})," and ",(0,a.jsx)(s.code,{children:"b"})," respectively, since we construct new list."]}),"\n"]}),"\n"]}),"\n",(0,a.jsx)(s.h2,{id:"example-1",children:"Example #1"}),"\n",(0,a.jsx)(s.p,{children:"Let us assume function that uses divide & conquer strategy to return indices at which we can find specific element in any list."}),"\n",(0,a.jsx)(s.pre,{children:(0,a.jsx)(s.code,{className:"language-py",metastring:"showLineNumbers",children:"def recursive_find_in_list(\n values: List[Any], key: Any, lower: int, upper: int\n) -> List[int]:\n if lower == upper:\n return [lower] if values[lower] == key else []\n\n indices = []\n mid = (lower + upper) // 2\n\n indices.extend(recursive_find_in_list(values, key, lower, mid))\n indices.extend(recursive_find_in_list(values, key, mid + 1, upper))\n\n return indices\n\n\ndef find_in_list(values: List[Any], key: Any) -> List[int]:\n return recursive_find_in_list(values, key, 0, len(values) - 1)\n"})}),"\n",(0,a.jsxs)(s.p,{children:["This implementation works nicely, ",(0,a.jsx)(s.code,{children:"extend"})," is linear (with the respect to the length of the list that is being appended)."]}),"\n",(0,a.jsxs)(s.p,{children:["Let us try to dissect the way this function works on some specific input (that will be pushed to the extreme, ",(0,a.jsx)(s.em,{children:"just in case"})," ;)"]}),"\n",(0,a.jsxs)(s.p,{children:[(0,a.jsx)(s.code,{children:"find_in_list([1] * 5000, 1)"}),". What shall be the result of this? Since we have ",(0,a.jsx)(s.code,{children:"key = 1"})," and the list contains only ",(0,a.jsx)(s.code,{children:"1"}),"s, we should get list of ",(0,a.jsx)(s.strong,{children:"all"})," indices."]}),"\n",(0,a.jsxs)(s.p,{children:["If we were to draw a tree of call hierarchy of ",(0,a.jsx)(s.code,{children:"recursive_find_in_list"}),", we would notice that in sum it is still linear to the length. ",(0,a.jsxs)(s.strong,{children:["However we use ",(0,a.jsx)(s.code,{children:"extend"}),"!"]})]}),"\n",(0,a.jsxs)(s.p,{children:["In the leaves of the tree we return lists of length 1. In this case it means calling ",(0,a.jsx)(s.code,{children:"extend"})," 5000-times at the second-to-last level of the tree on the 1-element long lists, next level 2500 calls on 2-elements long lists, next one 1250 on 4-elements long lists, etc. At the top-level we get 2 calls on 5000/2-element long lists."]}),"\n",(0,a.jsxs)(s.p,{children:["A lot of ",(0,a.jsx)(s.code,{children:"extend"})," calls, right? And the lengths of the lists are growing (in this example, second call happens on 2500-elements long lists)."]}),"\n",(0,a.jsxs)(s.p,{children:["Because of the ",(0,a.jsx)(s.code,{children:"extend"})," in each level of the tree (call hierarchy) we traverse all of the elements. That means:"]}),"\n",(0,a.jsx)(s.span,{className:"katex-display",children:(0,a.jsxs)(s.span,{className:"katex",children:[(0,a.jsx)(s.span,{className:"katex-mathml",children:(0,a.jsx)(s.math,{xmlns:"http://www.w3.org/1998/Math/MathML",display:"block",children:(0,a.jsxs)(s.semantics,{children:[(0,a.jsxs)(s.mrow,{children:[(0,a.jsx)(s.mi,{mathvariant:"script",children:"O"}),(0,a.jsx)(s.mo,{stretchy:"false",children:"("}),(0,a.jsx)(s.mi,{children:"n"}),(0,a.jsx)(s.mo,{children:"\u22c5"}),(0,a.jsx)(s.mi,{children:"log"}),(0,a.jsx)(s.mo,{children:"\u2061"}),(0,a.jsx)(s.mi,{children:"n"}),(0,a.jsx)(s.mo,{stretchy:"false",children:")"})]}),(0,a.jsx)(s.annotation,{encoding:"application/x-tex",children:"\\mathcal{O}(n \\cdot \\log n)"})]})})}),(0,a.jsxs)(s.span,{className:"katex-html","aria-hidden":"true",children:[(0,a.jsxs)(s.span,{className:"base",children:[(0,a.jsx)(s.span,{className:"strut",style:{height:"1em",verticalAlign:"-0.25em"}}),(0,a.jsx)(s.span,{className:"mord mathcal",style:{marginRight:"0.02778em"},children:"O"}),(0,a.jsx)(s.span,{className:"mopen",children:"("}),(0,a.jsx)(s.span,{className:"mord mathnormal",children:"n"}),(0,a.jsx)(s.span,{className:"mspace",style:{marginRight:"0.2222em"}}),(0,a.jsx)(s.span,{className:"mbin",children:"\u22c5"}),(0,a.jsx)(s.span,{className:"mspace",style:{marginRight:"0.2222em"}})]}),(0,a.jsxs)(s.span,{className:"base",children:[(0,a.jsx)(s.span,{className:"strut",style:{height:"1em",verticalAlign:"-0.25em"}}),(0,a.jsxs)(s.span,{className:"mop",children:["lo",(0,a.jsx)(s.span,{style:{marginRight:"0.01389em"},children:"g"})]}),(0,a.jsx)(s.span,{className:"mspace",style:{marginRight:"0.1667em"}}),(0,a.jsx)(s.span,{className:"mord mathnormal",children:"n"}),(0,a.jsx)(s.span,{className:"mclose",children:")"})]})]})]})}),"\n",(0,a.jsxs)(s.p,{children:["because we have ",(0,a.jsxs)(s.span,{className:"katex",children:[(0,a.jsx)(s.span,{className:"katex-mathml",children:(0,a.jsx)(s.math,{xmlns:"http://www.w3.org/1998/Math/MathML",children:(0,a.jsxs)(s.semantics,{children:[(0,a.jsxs)(s.mrow,{children:[(0,a.jsx)(s.mi,{children:"log"}),(0,a.jsx)(s.mo,{children:"\u2061"}),(0,a.jsx)(s.mi,{children:"n"})]}),(0,a.jsx)(s.annotation,{encoding:"application/x-tex",children:"\\log n"})]})})}),(0,a.jsx)(s.span,{className:"katex-html","aria-hidden":"true",children:(0,a.jsxs)(s.span,{className:"base",children:[(0,a.jsx)(s.span,{className:"strut",style:{height:"0.8889em",verticalAlign:"-0.1944em"}}),(0,a.jsxs)(s.span,{className:"mop",children:["lo",(0,a.jsx)(s.span,{style:{marginRight:"0.01389em"},children:"g"})]}),(0,a.jsx)(s.span,{className:"mspace",style:{marginRight:"0.1667em"}}),(0,a.jsx)(s.span,{className:"mord mathnormal",children:"n"})]})})]})," levels in the tree and ",(0,a.jsxs)(s.span,{className:"katex",children:[(0,a.jsx)(s.span,{className:"katex-mathml",children:(0,a.jsx)(s.math,{xmlns:"http://www.w3.org/1998/Math/MathML",children:(0,a.jsxs)(s.semantics,{children:[(0,a.jsx)(s.mrow,{children:(0,a.jsx)(s.mi,{children:"n"})}),(0,a.jsx)(s.annotation,{encoding:"application/x-tex",children:"n"})]})})}),(0,a.jsx)(s.span,{className:"katex-html","aria-hidden":"true",children:(0,a.jsxs)(s.span,{className:"base",children:[(0,a.jsx)(s.span,{className:"strut",style:{height:"0.4306em"}}),(0,a.jsx)(s.span,{className:"mord mathnormal",children:"n"})]})})]})," elements at each level."]}),"\n",(0,a.jsx)(s.h2,{id:"example-2",children:"Example #2"}),"\n",(0,a.jsxs)(s.p,{children:["As we could observe in the example above, ",(0,a.jsx)(s.code,{children:"extend"})," iterates over all of the elements that it adds. In case of recursive calls, it results in iterating over the same elements multiple times."]}),"\n",(0,a.jsx)(s.p,{children:"Consider constructing of this list:"}),"\n",(0,a.jsxs)(s.p,{children:[(0,a.jsx)(s.img,{alt:"Rendered construction of the list",src:n(41967).Z+"#gh-light-mode-only",width:"851",height:"276"}),"\n",(0,a.jsx)(s.img,{alt:"Rendered construction of the list",src:n(99244).Z+"#gh-dark-mode-only",width:"851",height:"276"})]}),"\n",(0,a.jsx)(s.p,{children:"Let us assume that you extend the result with the list that you get from the recursive call."}),"\n",(0,a.jsxs)(s.ul,{children:["\n",(0,a.jsxs)(s.li,{children:["\n",(0,a.jsxs)(s.p,{children:["B iterates through 1, 2 and 3; returns ",(0,a.jsx)(s.code,{children:"[1, 2, 3]"})]}),"\n"]}),"\n",(0,a.jsxs)(s.li,{children:["\n",(0,a.jsxs)(s.p,{children:["C iterates through 4, 5 and 6; returns ",(0,a.jsx)(s.code,{children:"[4, 5, 6]"})]}),"\n"]}),"\n",(0,a.jsxs)(s.li,{children:["\n",(0,a.jsxs)(s.p,{children:["D iterates through 7, 8 and 9; returns ",(0,a.jsx)(s.code,{children:"[7, 8, 9]"})]}),"\n"]}),"\n",(0,a.jsxs)(s.li,{children:["\n",(0,a.jsxs)(s.p,{children:["now we return those lists to the calls from A), so each of the ",(0,a.jsx)(s.code,{children:"extend"})," calls iterates through:"]}),"\n",(0,a.jsxs)(s.ul,{children:["\n",(0,a.jsx)(s.li,{children:"1, 2, 3 that was returned from B"}),"\n",(0,a.jsx)(s.li,{children:"4, 5, 6 that was returned from C"}),"\n",(0,a.jsx)(s.li,{children:"7, 8, 9 that was returned from D"}),"\n"]}),"\n",(0,a.jsxs)(s.p,{children:["and returns ",(0,a.jsx)(s.code,{children:"[1, 2, 3, 4, 5, 6, 7, 8, 9]"})]}),"\n"]}),"\n"]}),"\n",(0,a.jsx)(s.p,{children:"If the recursion had bigger depth and/or more elements, it would iterate through them more than twice, therefore it does not take constant time to do nor some constant multiple of the input, since it traverses all of the elements in each of the levels."}),"\n",(0,a.jsxs)(s.h2,{id:"implementation-of-extend",children:["Implementation of ",(0,a.jsx)(s.code,{children:"extend"})]}),"\n",(0,a.jsx)(s.p,{children:"There is an example of dynamic array:"}),"\n",(0,a.jsxs)(s.ul,{children:["\n",(0,a.jsx)(s.li,{children:(0,a.jsxs)(s.a,{href:"pathname:///files/algorithms/time-complexity/extend/dynlist.h",children:["interface (",(0,a.jsx)(s.code,{children:"dynlist.h"}),")"]})}),"\n",(0,a.jsx)(s.li,{children:(0,a.jsxs)(s.a,{href:"pathname:///files/algorithms/time-complexity/extend/dynlist.c",children:["implementation (",(0,a.jsx)(s.code,{children:"dynlist.c"}),")"]})}),"\n"]}),"\n",(0,a.jsxs)(s.p,{children:["For the sake of ",(0,a.jsx)(s.em,{children:"Algorithms and Data Structures I"})," we consider ",(0,a.jsx)(s.code,{children:"APPEND"})," operation, i.e. adding the element to the end of the list, to have time complexity ",(0,a.jsxs)(s.span,{className:"katex",children:[(0,a.jsx)(s.span,{className:"katex-mathml",children:(0,a.jsx)(s.math,{xmlns:"http://www.w3.org/1998/Math/MathML",children:(0,a.jsxs)(s.semantics,{children:[(0,a.jsxs)(s.mrow,{children:[(0,a.jsx)(s.mi,{mathvariant:"script",children:"O"}),(0,a.jsx)(s.mo,{stretchy:"false",children:"("}),(0,a.jsx)(s.mn,{children:"1"}),(0,a.jsx)(s.mo,{stretchy:"false",children:")"})]}),(0,a.jsx)(s.annotation,{encoding:"application/x-tex",children:"\\mathcal{O}(1)"})]})})}),(0,a.jsx)(s.span,{className:"katex-html","aria-hidden":"true",children:(0,a.jsxs)(s.span,{className:"base",children:[(0,a.jsx)(s.span,{className:"strut",style:{height:"1em",verticalAlign:"-0.25em"}}),(0,a.jsx)(s.span,{className:"mord mathcal",style:{marginRight:"0.02778em"},children:"O"}),(0,a.jsx)(s.span,{className:"mopen",children:"("}),(0,a.jsx)(s.span,{className:"mord",children:"1"}),(0,a.jsx)(s.span,{className:"mclose",children:")"})]})})]})," (",(0,a.jsx)(s.strong,{children:"amortized"}),"; which is out of the scope of IB002)."]}),"\n",(0,a.jsxs)(s.p,{children:["If we have a look at the ",(0,a.jsx)(s.code,{children:"extend"})," implementation in this dynamic array example:"]}),"\n",(0,a.jsx)(s.pre,{children:(0,a.jsx)(s.code,{className:"language-c",metastring:"showLineNumbers",children:"void dynamic_array_extend(struct dynamic_array_t *arr, struct dynamic_array_t *src)\n{\n if (arr == NULL || src == NULL)\n {\n return;\n }\n\n for (size_t i = 0; i < src->count; i++)\n {\n dynamic_array_push_back(arr, dynamic_array_at(src, i));\n }\n}\n"})}),"\n",(0,a.jsxs)(s.p,{children:["Apart from checking edge cases, we can notice that we run ",(0,a.jsx)(s.code,{children:"for"}),"-loop over the elements from the other array and add them one-by-one to the ",(0,a.jsx)(s.code,{children:"arr"}),". Time complexity of this operation is time dependant on the ",(0,a.jsx)(s.code,{children:"src"})," array."]}),"\n",(0,a.jsxs)(s.p,{children:["In this specific implementation, you could also resize the memory allocated for the array in one go and copy ",(0,a.jsx)(s.em,{children:"whole"})," ",(0,a.jsx)(s.code,{children:"src"})," array in one go. However even if you did so, it would be still dependant on the size of the ",(0,a.jsx)(s.code,{children:"src"})," array. Cause you still need to copy ",(0,a.jsxs)(s.span,{className:"katex",children:[(0,a.jsx)(s.span,{className:"katex-mathml",children:(0,a.jsx)(s.math,{xmlns:"http://www.w3.org/1998/Math/MathML",children:(0,a.jsxs)(s.semantics,{children:[(0,a.jsxs)(s.mrow,{children:[(0,a.jsx)(s.mtext,{mathvariant:"monospace",children:"count"}),(0,a.jsx)(s.mo,{stretchy:"false",children:"("}),(0,a.jsx)(s.mi,{children:"s"}),(0,a.jsx)(s.mi,{children:"r"}),(0,a.jsx)(s.mi,{children:"c"}),(0,a.jsx)(s.mo,{stretchy:"false",children:")"}),(0,a.jsx)(s.mo,{children:"\u22c5"}),(0,a.jsx)(s.mtext,{mathvariant:"monospace",children:"elementSize"}),(0,a.jsx)(s.mo,{stretchy:"false",children:"("}),(0,a.jsx)(s.mi,{children:"s"}),(0,a.jsx)(s.mi,{children:"r"}),(0,a.jsx)(s.mi,{children:"c"}),(0,a.jsx)(s.mo,{stretchy:"false",children:")"})]}),(0,a.jsx)(s.annotation,{encoding:"application/x-tex",children:"\\texttt{count}(src) \\cdot \\texttt{elementSize}(src)"})]})})}),(0,a.jsxs)(s.span,{className:"katex-html","aria-hidden":"true",children:[(0,a.jsxs)(s.span,{className:"base",children:[(0,a.jsx)(s.span,{className:"strut",style:{height:"1em",verticalAlign:"-0.25em"}}),(0,a.jsx)(s.span,{className:"mord text",children:(0,a.jsx)(s.span,{className:"mord texttt",children:"count"})}),(0,a.jsx)(s.span,{className:"mopen",children:"("}),(0,a.jsx)(s.span,{className:"mord mathnormal",children:"src"}),(0,a.jsx)(s.span,{className:"mclose",children:")"}),(0,a.jsx)(s.span,{className:"mspace",style:{marginRight:"0.2222em"}}),(0,a.jsx)(s.span,{className:"mbin",children:"\u22c5"}),(0,a.jsx)(s.span,{className:"mspace",style:{marginRight:"0.2222em"}})]}),(0,a.jsxs)(s.span,{className:"base",children:[(0,a.jsx)(s.span,{className:"strut",style:{height:"1em",verticalAlign:"-0.25em"}}),(0,a.jsx)(s.span,{className:"mord text",children:(0,a.jsx)(s.span,{className:"mord texttt",children:"elementSize"})}),(0,a.jsx)(s.span,{className:"mopen",children:"("}),(0,a.jsx)(s.span,{className:"mord mathnormal",children:"src"}),(0,a.jsx)(s.span,{className:"mclose",children:")"})]})]})]})," bytes. From that we can assume that for specific instance of array the ",(0,a.jsxs)(s.span,{className:"katex",children:[(0,a.jsx)(s.span,{className:"katex-mathml",children:(0,a.jsx)(s.math,{xmlns:"http://www.w3.org/1998/Math/MathML",children:(0,a.jsxs)(s.semantics,{children:[(0,a.jsxs)(s.mrow,{children:[(0,a.jsx)(s.mtext,{mathvariant:"monospace",children:"elementSize"}),(0,a.jsx)(s.mo,{stretchy:"false",children:"("}),(0,a.jsx)(s.mi,{children:"s"}),(0,a.jsx)(s.mi,{children:"r"}),(0,a.jsx)(s.mi,{children:"c"}),(0,a.jsx)(s.mo,{stretchy:"false",children:")"})]}),(0,a.jsx)(s.annotation,{encoding:"application/x-tex",children:"\\texttt{elementSize}(src)"})]})})}),(0,a.jsx)(s.span,{className:"katex-html","aria-hidden":"true",children:(0,a.jsxs)(s.span,{className:"base",children:[(0,a.jsx)(s.span,{className:"strut",style:{height:"1em",verticalAlign:"-0.25em"}}),(0,a.jsx)(s.span,{className:"mord text",children:(0,a.jsx)(s.span,{className:"mord texttt",children:"elementSize"})}),(0,a.jsx)(s.span,{className:"mopen",children:"("}),(0,a.jsx)(s.span,{className:"mord mathnormal",children:"src"}),(0,a.jsx)(s.span,{className:"mclose",children:")"})]})})]})," is fixed, therefore we consider it a constant. That way we are getting ",(0,a.jsxs)(s.span,{className:"katex",children:[(0,a.jsx)(s.span,{className:"katex-mathml",children:(0,a.jsx)(s.math,{xmlns:"http://www.w3.org/1998/Math/MathML",children:(0,a.jsxs)(s.semantics,{children:[(0,a.jsxs)(s.mrow,{children:[(0,a.jsx)(s.mi,{mathvariant:"script",children:"O"}),(0,a.jsx)(s.mo,{stretchy:"false",children:"("}),(0,a.jsx)(s.mtext,{mathvariant:"monospace",children:"count"}),(0,a.jsx)(s.mo,{stretchy:"false",children:"("}),(0,a.jsx)(s.mi,{children:"s"}),(0,a.jsx)(s.mi,{children:"r"}),(0,a.jsx)(s.mi,{children:"c"}),(0,a.jsx)(s.mo,{stretchy:"false",children:")"}),(0,a.jsx)(s.mo,{stretchy:"false",children:")"})]}),(0,a.jsx)(s.annotation,{encoding:"application/x-tex",children:"\\mathcal{O}(\\texttt{count}(src))"})]})})}),(0,a.jsx)(s.span,{className:"katex-html","aria-hidden":"true",children:(0,a.jsxs)(s.span,{className:"base",children:[(0,a.jsx)(s.span,{className:"strut",style:{height:"1em",verticalAlign:"-0.25em"}}),(0,a.jsx)(s.span,{className:"mord mathcal",style:{marginRight:"0.02778em"},children:"O"}),(0,a.jsx)(s.span,{className:"mopen",children:"("}),(0,a.jsx)(s.span,{className:"mord text",children:(0,a.jsx)(s.span,{className:"mord texttt",children:"count"})}),(0,a.jsx)(s.span,{className:"mopen",children:"("}),(0,a.jsx)(s.span,{className:"mord mathnormal",children:"src"}),(0,a.jsx)(s.span,{className:"mclose",children:"))"})]})})]})," as a time complexity of our ",(0,a.jsx)(s.code,{children:"extend"})," operation."]})]})}function d(e={}){const{wrapper:s}={...(0,t.a)(),...e.components};return s?(0,a.jsx)(s,{...e,children:(0,a.jsx)(h,{...e})}):h(e)}},99244:(e,s,n)=>{n.d(s,{Z:()=>a});const a=n.p+"assets/images/construction_dark-fac28e7cafcc43d7e2fb5f0b6c25504e.svg"},41967:(e,s,n)=>{n.d(s,{Z:()=>a});const a=n.p+"assets/images/construction_light-02b0be76041a8b1379107378e8f8b64c.svg"},11151:(e,s,n)=>{n.d(s,{Z:()=>c,a:()=>l});var a=n(67294);const t={},i=a.createContext(t);function l(e){const s=a.useContext(i);return a.useMemo((function(){return"function"==typeof e?e(s):{...s,...e}}),[s,e])}function c(e){let s;return s=e.disableParentContext?"function"==typeof e.components?e.components(t):e.components||t:l(e.components),a.createElement(i.Provider,{value:s},e.children)}}}]); |