Find read-write conflicts among given database transactions

Given a list of database transactions, find all read-write conflicts among them. Assume that there is no strict two-phase locking (Strict 2PL) protocol to prevent read-write conflicts.

Each database transaction is given in the form of a tuple. The tuple ('T', 'A', 't', 'R') indicates that a transaction T accessed a database record A at a time t , and a read operation is performed on the record.

Assume that a data conflict happens when two transactions access the same record in the database within an interval of 5 units. At least one write operation is performed on the record.

Input:

(T1, A, 0, R)
(T2, A, 2, W)
(T3, B, 4, W)
(T4, C, 5, W)
(T5, B, 7, R)
(T6, C, 8, W)
(T7, A, 9, R)

Output:

Transaction T1 and T2 are involved in RW conflict
Transaction T3 and T5 are involved in WR conflict
Transaction T4 and T6 are involved in WW conflict

Brief Overview on Read–Write conflicts

In databases, data conflict may arise during a read or write operation by the different transactions on the same data during the transaction’s life, leading to an inconsistent final database state. There are three types of data conflicts involved in the database transaction.

Note that there is no Read–Read (RR) conflict in the database since no information is updated in the database during the read operation.

Back to our Problem…

The idea is to sort all transactions in increasing order of database records and record access time. For each database record, consider all transaction pairs that have accessed the current record within an interval of 5 units. Finally, print all the conflicting transaction pairs for which at least one write operation is performed on the current record.

This can be implemented as follows in C++, Java, and Python: