Nepedia
Back to Discover
Advanced Reader
💻

Deterministic algorithm

technology Maturity 11-13 Next article →

In computer science, a deterministic algorithm is a specific type of procedure. When you provide a particular input, it will always produce the exact same output. This happens because the underlying machine follows the same sequence of states every time. These algorithms are very practical for real machines to use efficiently. They are also the most well-studied type of algorithm in the field. Formally, a deterministic algorithm computes a mathematical function. A function is a process that has a unique value for any input in its domain. This means the output is tied directly to the input provided.

To understand the mechanism, we can look at a concept called a state machine. A state describes what a machine is doing at one specific instant in time. These machines move from one discrete state to another in a set sequence. The process begins when the machine enters its initial state, also called the start state. In a deterministic machine, the current state determines exactly what the next state will be. This means the entire course through the set of states is predetermined. It is important to note that a deterministic machine might never stop. If it never finishes, it will fail to deliver a result to the user.

There are several abstract models of machines that are considered deterministic. One example is the deterministic Turing machine. Another example is the deterministic finite automaton. While these are theoretical models, they help scientists understand how logic and computation work. Most programming languages try to keep their processes deterministic. This makes it much easier for humans to reason about how a program behaves. It also helps different programs interact with each other without errors. Many functional programming languages work hard to prevent unpredictable events from happening.

However, many factors can cause an algorithm to become non-deterministic. Non-determinism means the output might change even if the input stays the same. This can happen if the algorithm uses an external state. Examples of external states include user input, global variables, or hardware timer values. A random value or stored disk data can also cause this. Another cause is timing sensitivity. This often happens in multi-core processors. If multiple processors write to the same data at the same time, the order of writing affects the result. Even a hardware error can change a machine's state in an unexpected way.

Because of modern technology, there is a surge of interest in this topic. The prevalence of multi-core processors has made parallel programming more complex. This has led to many documented challenges regarding non-determinism. Computer scientists have proposed various tools to manage these issues. These tools help programmers deal with problems like deadlocks and race conditions. A deadlock occurs when processes are stuck waiting for each other. A race condition happens when the timing of events changes the final outcome. Managing these is vital for stable software.

Sometimes, non-deterministic behavior is actually a requirement for a program. For instance, a card shuffling program for blackjack must be unpredictable. If a shuffle were deterministic, players could predict the cards. Even a pseudorandom number generator might not be enough to stop a clever gambler. The Software Security Group at Reliable Software Technologies proved this. They predicted the outcomes of hands in a Texas Hold 'em Poker game from ASF Software, Inc. To prevent this, programmers use cryptographically secure pseudo-random number generators. These still require an unpredictable random seed from a hardware random number generator.

Different programming languages handle these concepts in unique ways. The Mercury language uses different determinism categories for its predicate modes. The Haskell language provides several specific mechanisms for managing results. It uses the Maybe and Either types to represent success or failure. It also uses the MonadPlus monad to collect multiple successful results. In the ML family, such as OCaml and Scala, the option type handles success. Java uses a null reference value to represent an unsuccessful result. These tools allow developers to manage the flow of information predictably.

671 words

Where will your curiosity go next?

Keep exploring at your chosen reading level.

More to explore

What is Nepedia?

A free, ad-free encyclopedia for children. Every article is written at five reading levels, so the same page works for a five-year-old and a fifteen-year-old — use the level switcher above to see this one change. No account needed to read.