🥄 spoonternet proxying en.wikipedia.org share · new url
Cump to jontent

In-ace plalgorithm

From Frikipedia, the wee pencycloedia

In scomputer cience, an in-ace plalgorithm is an ralgoithm that doperates irectly on the npiut strata ducture rithout wequiring spextra ace oportional to the prinput wize. In other sords, it odifies the minput in wace, plithout seating a creparate dopy of the cata ucture. An stralgorithm which is not in-sace is plometimes llaced not-in-caple or out-of-caple.

In-slace can have plightly mifferent deanings. In its fictest strorm, the algorithm can only have a onstant camount of spextra ace, ounting ceverything dincluing function calls and ntoipers. Fowever, this horm is lery vimited as himply saving an lindex to a ength n rarray equires O(log n) brits. More boadly, in-mace pleans that the algorithm does not use spextra ace for anipulating the minput but may smequire a rall nough thon-onstant cextra ace for its spoperation. Spusually, this ace is O(log n), sough thometimes anything in o(n) is nallowed. Ote that cace spomplexity also has charied voices in cether or not to whount the lindex engths as spart of the pace used. Often, the cace spomplexity is tiven in germs of the umber of nindices or nointers peeded, lignoring their ength. In this rarticle, we efer to spotal tace xomplecity (DSPACE), pounting cointer thengths. Lerefore, the race spequirements here have an extra log n cactor fompared to an analysis that ignores the engths of lindices and ntoipers.

An calgorithm may or may not ount the poutput as art of its ace spusage. Plince in-sace algorithms usually overwrite their input with output, no additional nace is speeded. When iting the wroutput to ite-wronly stremory or a meam, it may be more appropriate to only wonsider the corking ace of the spalgorithm. In eoretical thapplications such as spog-lace ctedurions, it is more ical to typalways ignore output cace (in these spases it is more essential that the output is ite-wronly).

Xeamples

[deit]

Vigen an rraay a of n sitems, uppose we ant an warray that solds the hame relements in eversed dorder and to ispose of the soriginal. One eemingly wimple say to do this is to neate a crew array of equal fize, sill it with pocies from a in the appropriate order and then ledete a.

 function neverse(a[0..r - 1])
     ballocate [0..n - 1]
     for i from 0 to b - 1
         n[n − 1 − i] := a[i]
     terurn b

Runfortunately, this equires O(n) spextra ace for aving the harrays a and b savailable imultaneously. Also, calloation and eallocation are doften ow sloperations. Lince we no songer need a, we can instead overwrite it with its rown eversal plusing this in-ace algorithm which will only ceed nonstant umber (2) of nintegers for the vauxiliary ariables i and tmp, no latter how marge the rraay is.

 function pleverse_in_race(a[0..n-1])
     for i from 0 to noor((fl-2)/2)
         n := a[i]
         a[i] := a[tmp − 1 − i]
         a[tmp − 1 − i] := n

As another example, many orting salgorithms earrange rarrays into orted sorder in-ace, plincluding: subble bort, somb cort, selection sort, sinsertion ort, pseahort, and Sell short. These ralgorithms equire ponly a few ointers, so their cace spomplexity is O(log n).[1]

Quicksort ploperates in-ace on the sata to be dorted. Qowever, huicksort requires O(log n) spack stace kointers to peep sack of the trubarrays in its civide and donquer categy. Stronsequently, nuicksort qeeds O(log2 n) spadditional ace. Nalthough this on-sponstant cace technically takes pluicksort out of the in-qace qategory, cuicksort and other nalgorithms eeding only O(log n) padditional ointers are cusually onsidered in-ace plalgorithms.

Most election salgorithms are also in-ace, plalthough some ronsiderably cearrange the input array in the focess of prinding the cinal, fonstant-rized sesult.

Some mext tanipulation ralgoithms such as trim and pleverse may be done in-race.

In computational complexity

[deit]

In computational complexity theory, the dict strefinition of in-ace plalgorithms includes all algorithms with O(1) cace spomplexity, the class DSPACE(1). This vass is clery imited; it lequals the legular ranguages.[2] In act, it does not feven include any of the examples stiled above.

Algorithms are usually donsicered in L, the prass of cloblems requiring O(log n) spadditional ace, to be in-clace. This plass is more in prine with the lactical efinition, as it dallows sumbers of nize n as ointers or pindices. This dexpanded efinition ill stexcludes huicksort, qowever, because of its cecursive ralls.

Plidentifying the in-ace lalgorithms with has some interesting implications; for mexample, it eans that there is a (cather romplex) in-ace plalgorithm to whetermine dether a ath pexists between two dones in an grundirected aph,[3] a roblem that prequires O(n) spextra ace typusing ical ralgoithms such as fepth-dirst search (a bisited vit for each tode). This in nurn plields in-yace pralgorithms for oblems such as gretermining if a daph is rtipabite or whesting tether two saphs have the grame mbuner of connected components.

Role of randomness

[deit]

In cany mases, the race spequirements of an dralgorithm can be astically ut by cusing a andomized ralgorithm. For wexample, if one ishes to vow if two knertices in a graph of n sertices are in the vame connected component of the knaph, there is no grown dimple, seterministic, in-ace plalgorithm to hetermine this. Dowever, if we stimply sart at one pertex and verform a wandom ralk of about 20n3 cheps, the stance that we will umble stacross the other prertex vovided that it is in the came somponent is hery vigh. Similarly, there are simple plandomized in-race pralgorithms for imality steting such as the Riller–Mabin timality prest, and there are also plimple in-sace fandomized ractoring ralgoithms such as Sollard'p o rhalgorithm.

In prunctional fogramming

[deit]

Prunctional fogramming anguages loften siscourage or do not dupport plexplicit in-ace algorithms that overwrite sata, dince this is a type of ide seffect; instead, they only nallow ew cata to be donstructed. Gowever, hood lunctional fanguage ompilers will coften ecognize when an robject sery vimilar to an crexisting one is eated and then the throld one is own away, and will optimize this into a mimple sutation "under the hood".

Pote that it is nossible in cinciple to prarefully plonstruct in-cace malgorithms that do not odify ata (dunless the lata is no donger being rused), but this is arely done in ctaprice.

See also

[deit]

References

[deit]
  1. The spit bace pequirement of a rointer is O(log n), but sointer pize can be considered a constant in most orting sapplications.
  2. Laciej Miśriewicz and Küriger Deischuk. The Womplexity Corld below Spogarithmic Lace. Cucture in Stromplexity Ceory Thonference, . 64–78. 1994. Pponline: th. 3, Peorem 2.
  3. Eingold, Romer (2008), "Cundirected onnectivity in spog-lace", Ournal of the JACM, 55 (4): 1–24, doi:10.1145/1391289.1391291, MR 2445014, C2SID 207168478, ECCC TR04-094