Sub topic 6
Testing conflict serializability with a precedence graph
This chapter assumes you know what a schedule is and what a conflicting pair of operations is. It also assumes you can find a cycle in a directed graph and compute a topological order. Here you will turn those pieces into a mechanical test. The test answers GATE's most common schedule question: "Is this schedule conflict serializable, and if so, which serial schedules is it equivalent to?"
The idea in one paragraph
A schedule is conflict serializable if you can reorder it into a serial schedule by swapping only adjacent non-conflicting operations. Swapping non-conflicting operations never changes what any transaction reads or writes. Conflicting operations must keep their relative order. So each conflict forces one transaction to come before another in any equivalent serial schedule. The precedence graph records these forced orderings, and the schedule is conflict serializable exactly when the graph has no cycle.
Which operation pairs create an edge
Two operations from different transactions conflict when they touch the same data item and at least one is a write.
| First operation (earlier) | Second operation (later) | Conflict? | Edge |
|---|---|---|---|
| Read(X) by Ti | Read(X) by Tj | No | none |
| Read(X) by Ti | Write(X) by Tj | Yes (RW) | Ti → Tj |
| Write(X) by Ti | Read(X) by Tj | Yes (WR) | Ti → Tj |
| Write(X) by Ti | Write(X) by Tj | Yes (WW) | Ti → Tj |
The edge always points from the transaction whose operation appears earlier in the schedule to the one whose operation appears later. Operations on different items never conflict. Two operations of the same transaction never produce an edge.
The procedure
- List the transactions that appear in the schedule. Each one is a node.
- For each data item, scan the operations on that item from left to right.
- For every pair of operations on the same item where the first is by Ti, the second is by Tj, Ti ≠ Tj, and at least one is a write, add the edge Ti → Tj.
- Look for a directed cycle.
- If there is no cycle, the schedule is conflict serializable. Any topological order of the graph is an equivalent serial order.
- If there is a cycle, the schedule is not conflict serializable.
Duplicate edges do not matter. You need only one edge per ordered pair.
Worked example 1: acyclic graph
Consider this schedule, written in time order:
R1(A) W2(A) R3(B) W1(B) W3(C)Go through each item.
- Item A: R1(A) then W2(A) is a read-write conflict, so T1 → T2.
- Item B: R3(B) then W1(B) is a read-write conflict, so T3 → T1.
- Item C: only T3 touches it, so there is no edge.
The graph is a chain with no cycle. The only topological order is T3, T1, T2, so the schedule is equivalent to the serial schedule T3 T1 T2. T3 and T2 never conflict directly. Their order is still forced, because T3 must precede T1 and T1 must precede T2.
Worked example 2: a cycle
Now consider:
R1(A) W1(A) R2(A) W2(A) R2(B) W2(B) R1(B) W1(B)- Item A: T1's operations come before T2's, and the pairs include a write. This gives T1 → T2.
- Item B: T2's operations come before T1's, and the pairs include a write. This gives T2 → T1.
T1 must come before T2 because of A, and T2 must come before T1 because of B. No serial order satisfies both, so the schedule is not conflict serializable. It is the classic "transfer interleaved with another transfer" failure. Each transaction sees the other's half-finished work on one item.
Running the test as code
The function below builds the edges from every ordered pair of operations and then runs a topological sort (Kahn's algorithm). If the sort cannot place every transaction, a cycle exists. The sorted calls only make the output order repeatable.
def conflict_serializable(schedule):
# schedule: list of (txn, op, item), op is 'R' or 'W'
edges = set()
for i, (ti, oi, xi) in enumerate(schedule):
for tj, oj, xj in schedule[i + 1:]:
if ti != tj and xi == xj and 'W' in (oi, oj):
edges.add((ti, tj))
nodes = sorted({t for t, _, _ in schedule})
indegree = {n: 0 for n in nodes}
for _, b in edges:
indegree[b] += 1
ready = [n for n in nodes if indegree[n] == 0]
order = []
while ready:
n = ready.pop(0)
order.append(n)
for a, b in sorted(edges):
if a == n:
indegree[b] -= 1
if indegree[b] == 0:
ready.append(b)
return len(order) == len(nodes), order
acyclic = [('T1', 'R', 'A'), ('T2', 'W', 'A'), ('T3', 'R', 'B'),
('T1', 'W', 'B'), ('T3', 'W', 'C')]
cyclic = [('T1', 'R', 'A'), ('T1', 'W', 'A'), ('T2', 'R', 'A'),
('T2', 'W', 'A'), ('T2', 'R', 'B'), ('T2', 'W', 'B'),
('T1', 'R', 'B'), ('T1', 'W', 'B')]
lost_update = [('T1', 'R', 'A'), ('T2', 'W', 'A'), ('T1', 'W', 'A')]
print(conflict_serializable(acyclic)) # (True, ['T3', 'T1', 'T2'])
print(conflict_serializable(cyclic)) # (False, [])
print(conflict_serializable(lost_update)) # (False, [])When the result is False, the returned order is only the part of the graph that could be sorted before the cycle blocked progress. In these examples that part is empty. Do not read it as a serial order.
The pairwise scan is O(n²) in the number of operations. That is fine for exam-size schedules. A real scheduler would instead track, per item, the transactions that last read and wrote it.
Finding every equivalent serial order
An acyclic precedence graph usually has more than one topological order. Each topological order is an equivalent serial schedule. A GATE question may ask for the number of such orders, or ask which listed serial schedule is equivalent.
For example, suppose the graph has nodes T1, T2, T3 and the only edge is T1 → T2. T3 is unconstrained, so it can appear in any of three positions relative to the T1, T2 pair. The valid orders are T3 T1 T2, T1 T3 T2 and T1 T2 T3.
To pick the equivalent serial schedule from options, check each option against every edge. The correct option puts the source of every edge before its target.
Common errors
- Adding an edge for read-read pairs. Two reads never conflict.
- Reversing the direction. The edge goes from the earlier operation's transaction to the later one's.
- Comparing operations on different items. Conflicts exist only per item. Group by item first.
- Counting conflicts inside one transaction. Ti → Ti is never added.
- Missing a conflict by checking only adjacent operations. You must compare each operation with all later ones on the same item, not only the next one. For example, in R1(A) R3(A) W2(A), T1 conflicts with T2 even though R3(A) sits between them.
- Including aborted transactions. The test applies to the committed projection. If a transaction aborts, its operations are removed from the schedule before testing.
- Treating a cycle as proof of incorrectness. A cycle shows the schedule is not conflict serializable. It does not show that the final state is wrong for that particular data. A blind-write schedule can be view serializable without being conflict serializable. The next chapter covers that.
Why the test is sound
Suppose the graph is acyclic and you take a topological order. Move operations so that all of the first transaction's operations come first, then the next, and so on. Each swap you perform exchanges two adjacent operations from different transactions. If those two operations conflicted, an edge would force their current relative order, and the topological order would already agree with it. So every swap involves non-conflicting operations, and the schedule remains conflict equivalent at each step.
If the graph has a cycle, say T1 → T2 → T1, then conflicting operations force T1 before T2 and T2 before T1. No serial schedule can honor both. This is why the test is both necessary and sufficient for conflict serializability.
A quick checklist for exam questions
- Write down the transactions.
- For each item, list its operations in order.
- Add edges for each conflicting pair, earlier to later.
- Check for cycles, including longer ones such as T1 → T2 → T3 → T1.
- If acyclic, give a topological order.
Exercise
Is the schedule R1(X) R2(Y) W2(X) W1(Y) R3(X) W3(Y) conflict serializable? If it is, give one equivalent serial order.
Show answer
Work through each item. On X the operations in order are R1(X), W2(X), R3(X). R1(X) then W2(X) gives T1 → T2. R1(X) then R3(X) is read-read, so no edge. W2(X) then R3(X) gives T2 → T3. On Y the operations in order are R2(Y), W1(Y), W3(Y). R2(Y) then W1(Y) gives T2 → T1. R2(Y) then W3(Y) gives T2 → T3. W1(Y) then W3(Y) gives T1 → T3. The edges are T1 → T2 and T2 → T1, so there is a cycle between T1 and T2. The schedule is not conflict serializable, and no equivalent serial order exists.
Exercise
A precedence graph has nodes T1, T2, T3, T4 and edges T1 → T2, T1 → T3, T2 → T4, T3 → T4. How many equivalent serial schedules does it have, and what are they?
Show answer
The graph is acyclic, so the schedule is conflict serializable. T1 must come first and T4 must come last. T2 and T3 can appear in either order between them. There are two equivalent serial schedules: T1 T2 T3 T4 and T1 T3 T2 T4.