Skip to content

Instantly share code, notes, and snippets.

@MangaD
Created December 23, 2025 23:05
Show Gist options
  • Select an option

  • Save MangaD/a30941db9af6df594a53b9aa8e518592 to your computer and use it in GitHub Desktop.

Select an option

Save MangaD/a30941db9af6df594a53b9aa8e518592 to your computer and use it in GitHub Desktop.
Determinism vs Non-Determinism in Computer Science: Predictability, Choice, and Computation

Determinism vs Non-Determinism in Computer Science: Predictability, Choice, and Computation

CC0

Disclaimer: ChatGPT generated document.

At a high level, determinism is about predictability, while non-determinism is about multiple possible outcomes given the same starting point.

Let’s unpack that carefully, from intuition to formal models, with concrete examples.


1. Determinism (the “same input → same behavior” idea)

Intuitive definition

A deterministic system is one where:

Given the same input and same initial state, the system will always produce the same output and follow the same execution path.

There is no ambiguity about what happens next.

Simple example (deterministic algorithm)

Input: [5, 1, 3]
Algorithm: sort ascending
Output: [1, 3, 5]

Run it 1,000 times — the result is always identical.

Deterministic finite automaton (DFA)

In formal language theory, a DFA has:

  • Exactly one transition for each (state, input symbol) pair.

So:

(state S, input 'a') → next state is uniquely defined

No guessing, no branching.


2. Non-determinism (the “multiple possible paths” idea)

Intuitive definition

A non-deterministic system is one where:

Given the same input and state, multiple valid next steps may exist.

The system can choose among them.

Important: this does not necessarily mean randomness.


3. Non-determinism ≠ randomness (critical distinction)

Concept Meaning
Non-determinism Multiple valid choices exist
Randomness Choice is made via probability

Non-deterministic models say:

“Any of these transitions is allowed.”

They do not say:

“Pick one at random.”


4. Non-deterministic finite automaton (NFA)

An NFA allows:

  • Multiple transitions for the same (state, input)
  • Or even transitions without consuming input (ε-moves)

Example:

(state S, input 'a') → state X OR state Y

The machine accepts the input if any possible path leads to an accepting state.

This is a theoretical abstraction, not a literal machine making magical choices.


5. How non-determinism is “implemented” in reality

Real computers are deterministic (at the hardware level). So how do we deal with non-determinism?

Common techniques

  • Backtracking (try all paths)
  • Parallelism (explore paths simultaneously)
  • State-space search
  • Determinization (e.g., NFA → DFA)

In automata theory:

Every NFA has an equivalent DFA.

The DFA may be exponentially larger, but it behaves deterministically.


6. Determinism vs non-determinism in algorithms

Deterministic algorithm

At each step, exactly one action is defined

Example:

  • Binary search
  • Merge sort
  • Dijkstra’s algorithm (with fixed tie-breaking)

Non-deterministic algorithm (theoretical model)

Example problem:

“Guess a solution and verify it in polynomial time.”

This is the defining idea behind NP:

  • A non-deterministic Turing machine can guess a solution
  • Then check it deterministically

In reality, we simulate this by:

  • Trying all guesses (exponential time)

7. Determinism vs non-determinism in programming

Deterministic program

int f(int x) {
    return x * 2;
}

Same input → same output, always.


Sources of non-deterministic behavior (practical systems)

Even though CPUs are deterministic, systems are not always deterministic:

Source Why it’s non-deterministic
Thread scheduling OS decides interleavings
Race conditions Timing-dependent outcomes
Network I/O Messages arrive in varying order
Signals / interrupts Arrival time varies
Hardware concurrency Reordering & visibility effects

Example:

// Two threads increment the same variable without synchronization
counter++;

Final value depends on execution order — non-deterministic behavior.


8. Determinism in concurrency (important nuance)

A concurrent program can be:

  • Deterministic: outcome independent of scheduling
  • Non-deterministic: outcome depends on interleaving

Well-designed concurrent systems aim for:

  • Deterministic results
  • Even if execution order varies

Techniques:

  • Mutexes
  • Atomics
  • Message passing
  • Functional immutability

9. Determinism in language semantics

Languages differ in how much non-determinism they expose.

Examples:

  • C / C++: undefined behavior introduces semantic non-determinism
  • Functional languages: often deterministic by design
  • Logic languages (e.g., Prolog): explicitly non-deterministic search

10. Summary table

Aspect Determinism Non-determinism
Next step Exactly one One of many
Predictability Full Partial
Theoretical use Models machines & algorithms Models choice & parallel search
Real hardware Yes No (simulated)
Common pitfalls Overconfidence in order Races, heisenbugs

One-sentence intuition

Determinism means the future is fixed by the present; non-determinism means the present allows multiple possible futures.

If you want, I can next:

  • Connect this to NP vs P
  • Show formal definitions with Turing machines
  • Relate it to concurrency memory models (C++20)
  • Or explain why non-determinism is useful despite being “unreal”

Just say the word.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment