AI Glossary / Graphs and symbolic AI / Beginner
LIFO queue
Definition
A LIFO queue is a collection in which the most recently added item is the next one removed. LIFO means last in, first out. It behaves like a stack and is useful when the newest unfinished item should be handled before older ones, as in many forms of backtracking, nested processing, and undo management.
What it means
The defining property is removal order, not the physical storage layout. Items might be stored in an array, linked structure, or a synchronized container. Adding an item is often called pushing, and removing the latest item is called popping.
LIFO contrasts with FIFO, first in, first out, where the oldest waiting item is removed next. It also differs from a priority queue, which selects according to a priority rule. Although some terminology reserves queue for FIFO structures, “LIFO queue” is an established name in several programming interfaces and AI descriptions.
How it works
Maintain one end of the collection as the top. Each insertion places an item at that end, and each removal takes the item currently there. Looking at the top without removing it is commonly called peeking. Empty and capacity-limit behavior depend on the implementation.
In a search algorithm, using a LIFO frontier makes newly generated successors available before older alternatives, producing depth-first behavior under the usual setup. The insertion order of successors affects which branch is explored first. The data structure alone does not handle repeated states, prove termination, or make the search find the shortest solution; those depend on the surrounding algorithm.
Three practical examples
- An editor stores reversible actions so that the most recent action can be undone first.
- A backtracking solver keeps recent decisions available for reversal when a branch fails.
- A depth-first traversal uses a stack of pending nodes to continue along newly discovered branches before older alternatives.
Think of it this way
Think of a stack of trays where new trays are placed on top and the next tray is taken from the top. The analogy is limited because software can impose additional rules for capacity, synchronization, and error handling, and items can represent pending work rather than physical objects.
Worked example
Start with an empty LIFO collection and insert A, then B, then C. Written from bottom to top, its contents are [A, B, C]. Removing one item returns C and leaves [A, B]. Insert D next, producing [A, B, D]. The next removals return D, then B, then A.
The full removal sequence is therefore C, D, B, A. It is not simply the reverse of the original three insertions, because a new insertion occurred between removals. At each step, the rule applies to the items currently present.
Now apply the same idea to a small search. Suppose expanding a start node generates children Left and Right, and the program pushes Left before Right. Right is now on top and is explored first. If the intended traversal should visit Left first, the program can push Right before Left instead, assuming no other frontier operations intervene.
This example illustrates ordering only. It does not specify duplicate detection, cycle handling, or when a goal test occurs. Those decisions can change search behavior and correctness even though the frontier remains LIFO.
Illustrative case study
In a fictional design tool, Aspen Edit implements undo with a list of recorded changes. The first version removes the oldest change, causing the application to undo an early edit while leaving later dependent edits in place.
The team changes the removal policy to LIFO and verifies the required reverse order of reversible actions. It separately defines how redo and non-reversible operations are handled. The invented case illustrates a suitable ordering policy, not a claim that storing actions in a stack automatically makes every operation safely undoable.
Common misconceptions and mistakes
LIFO does not mean that an item is processed immediately when inserted; it only defines which waiting item is selected next. It also does not guarantee fairness. If new items continue arriving, an older item can remain waiting for a long time.
Do not assume that a stack is automatically safe for concurrent access or that checking whether it is empty guarantees a later removal will succeed in a changing multithreaded setting. Those are implementation questions. Likewise, a LIFO frontier does not make depth-first search optimal, and a stack of action descriptions does not supply the logic needed to reverse irreversible actions.
Business use
Choose LIFO when reverse dependency order or recent-first exploration matches the task. It can support undo histories, nested work, and backtracking algorithms. It may be unsuitable for service queues where arrival order or waiting-time fairness matters.
Specify empty behavior, size limits, and concurrency requirements before selecting an implementation. For search, document successor insertion order and repeated-state handling. For undo systems, store enough information to reverse each supported operation and define what happens when history is discarded. The practical value comes from matching the ordering policy to the workflow, not from treating one collection type as universally preferable.
People also ask
Is a LIFO queue the same as a stack?
They share the same last-in, first-out removal behavior, though interfaces may provide different additional features.
Does LIFO process the oldest waiting item first?
No. That is FIFO behavior.
Does a LIFO frontier guarantee the shortest search path?
No. Depth-first exploration can find a longer solution before a shorter one, and other search rules are needed for shortest-path guarantees.
Check your understanding
Why is D removed before B after the second insertion phase in the worked example?
Answer: D was added after B and remains at the top. The LIFO rule always removes the most recently added item still present.
Related terms
- FIFO queue
- Priority queue
- Depth First Search
- Frontier
- stack
- push
- pop
- backtracking
- undo history
Learn next
Trace the same insertion and removal sequence using FIFO and compare the outputs. Then draw a small search tree and test how reversing successor insertion order changes a LIFO traversal's first branch.
More in Graphs and symbolic AI
- Lexical category - A lexical category is a grammatical class associated with a word or lexical item, such as noun, verb, adjective, or adverb.
- Lexicalized PCFG - A lexicalized PCFG is a probabilistic context-free grammar whose syntactic categories or rule probabilities incorporate lexical head information, often...
- Lexicographic breadth-first search - Lexicographic breadth-first search, or LexBFS, is a graph traversal that orders vertices using lexicographic labels based on previously visited neighbors.
- Lifting lemma - The lifting lemma is a result in automated reasoning that connects resolution on ground clauses with resolution on general first-order clauses.
- Lightweight ontology - A lightweight ontology is a relatively simple, explicit model of a domain's concepts and relationships, using limited formal detail compared with a more...
- Limited rationality - Limited rationality is decision-making that accounts for limits on time, information, memory, or computation.
Sources
Sources: Python Software Foundation, official queue documentation, FIFO, LIFO, and priority removal policies: https://docs.python.org/3/library/queue.html
Previous: Lifelong Planning A* Next: Lift