# One-pass algorithm

> Mediated Wiki article. Canonical URL: https://mediated.wiki/source/One-pass_algorithm
> Markdown URL: https://mediated.wiki/source/One-pass_algorithm.md
> Source: https://en.wikipedia.org/wiki/One-pass_algorithm
> Source revision: 1298046753
> License: Creative Commons Attribution-ShareAlike 4.0 International (https://creativecommons.org/licenses/by-sa/4.0/)

{{Short description|Type of streaming algorithm}}
{{Refimprove|date=April 2021}}

In computing, a '''one-pass algorithm''' or '''single-pass algorithm''' is a [streaming algorithm](/source/streaming_algorithm) which reads its input exactly once.<ref name="frankfurt"/> It does so by processing items in order, without unbounded [buffering](/source/Buffer_(computer_science)); it reads a block into an [input buffer](/source/input_buffer), processes it, and moves the result into an output buffer for each step in the process.<ref name="sjsu"/> A one-pass algorithm generally requires ''O''(''n'') (see ['big O' notation](/source/Big_O_Notation)) time and less than ''O''(''n'') storage (typically ''O''(1)), where ''n'' is the size of the input.<ref name="eds"/> An example of a one-pass algorithm is the Sondik [partially observable Markov decision process](/source/partially_observable_Markov_decision_process).<ref name="pomdp"/>

==Example problems solvable by one-pass algorithms==
Given any list as an input:
* Count the number of elements.

Given a list of numbers:
* Find the ''k'' largest or smallest elements, ''k'' given in advance.
* Find the [sum](/source/summation), [mean](/source/mean), [variance](/source/variance) and [standard deviation](/source/standard_deviation) of the elements of the list. See also [Algorithms for calculating variance](/source/Algorithms_for_calculating_variance).

Given a list of symbols from an alphabet of ''k'' symbols, given in advance.
* Count the number of times each symbol appears in the input.
* Find the most or least frequent elements.
* Sort the list according to some order on the symbols (possible since the and after number of symbols is limited).
* Find the maximum gap between two appearances of a given symbol.

==Example problems not solvable by one-pass algorithms==
Given any list as an input:
* Find the ''n''th element from the end (or report that the list has fewer than ''n'' elements).
* Find the middle element of the list.  However, this is solvable with two passes: Pass 1 counts the elements and pass 2 picks out the middle one.

Given a list of numbers:
* Find the [median](/source/median).
* Find the [modes](/source/mode_(statistics)) (This is not the same as finding the most frequent symbol from a limited alphabet).
* Sort the list.
* Count the number of items greater than or less than the [mean](/source/mean).  However, this can be done in constant memory with two passes: Pass 1 finds the average and pass 2 does the counting.

The two-pass algorithms above are still [streaming algorithms](/source/streaming_algorithm) but not one-pass algorithms.

== References==
<references>
<ref name="eds">{{Citation|last=Schweikardt|first=Nicole|title=One-Pass Algorithm|date=2009|url=https://doi.org/10.1007/978-0-387-39940-9_253|encyclopedia=Encyclopedia of Database Systems|pages=1948–1949|editor-last=LIU|editor-first=LING|editor-link=Ling Liu (computer scientist)|place=Boston, MA|publisher=Springer US|language=en|doi=10.1007/978-0-387-39940-9_253|isbn=978-0-387-39940-9|access-date=2021-04-13|editor2-last=ÖZSU|editor2-first=M. TAMER|url-access=subscription}}</ref>
<ref name="frankfurt">{{Cite web|last=Schweikardt|first=Nicole|title=One-Pass Algorithm|url=http://www.tks.informatik.uni-frankfurt.de/schweika/downloads/EncycDBS_OnePassAlgos.pdf|access-date=2021-07-01}}</ref>
<ref name="sjsu">{{Cite web|last=Pollett|first=Chris|date=2005-03-14|title=One and Two Pass Algorithms|url=http://www.cs.sjsu.edu/faculty/pollett/157b.12.05s/Lec14032005.pdf|access-date=2021-07-01}}</ref>
<ref name="pomdp">{{Cite web|url=http://www.pomdp.org/tutorial/sondik.html|title=Sondik's One-Pass Algorithm|website=www.pomdp.org}}</ref>
</references>

{{cs-stub}}

{{DEFAULTSORT:One-Pass Algorithm}}
Category:Streaming algorithms

---
Adapted from the Wikipedia article [One-pass algorithm](https://en.wikipedia.org/wiki/One-pass_algorithm) by Wikipedia contributors ([contributor history](https://en.wikipedia.org/wiki/One-pass_algorithm?action=history)). Available under [Creative Commons Attribution-ShareAlike 4.0 International](https://creativecommons.org/licenses/by-sa/4.0/). Changes may have been made.
