Bird
0
0
DSA Cprogramming~10 mins

Collision Handling Using Open Addressing Linear Probing in DSA C - Execution Trace

Choose your learning style9 modes available
Concept Flow - Collision Handling Using Open Addressing Linear Probing
Start: Insert key
Compute hash index
Check slot at index
Place key
Repeat check until empty slot found
Done
When inserting a key, compute its hash index. If the slot is empty, place the key. If occupied, move to the next slot and repeat until an empty slot is found.
Execution Sample
DSA C
int hash(int key, int size) {
    return key % size;
}

void insert(int table[], int size, int key) {
    int index = hash(key, size);
    while (table[index] != -1) {
        index = (index + 1) % size;
    }
    table[index] = key;
}
This code inserts keys into a hash table using linear probing to handle collisions.
Execution Table
StepOperationKeyComputed IndexSlot CheckedSlot StatusActionTable State
1Insert key1010 % 11 = 1010EmptyPlace key at index 10[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 10]
2Insert key2222 % 11 = 00EmptyPlace key at index 0[22, -1, -1, -1, -1, -1, -1, -1, -1, -1, 10]
3Insert key3131 % 11 = 99EmptyPlace key at index 9[22, -1, -1, -1, -1, -1, -1, -1, -1, 31, 10]
4Insert key44 % 11 = 44EmptyPlace key at index 4[22, -1, -1, -1, 4, -1, -1, -1, -1, 31, 10]
5Insert key1515 % 11 = 44Occupied (4)Move to index 5[22, -1, -1, -1, 4, -1, -1, -1, -1, 31, 10]
6Check slot1555EmptyPlace key at index 5[22, -1, -1, -1, 4, 15, -1, -1, -1, 31, 10]
7Insert key2828 % 11 = 66EmptyPlace key at index 6[22, -1, -1, -1, 4, 15, 28, -1, -1, 31, 10]
8Insert key1717 % 11 = 66Occupied (28)Move to index 7[22, -1, -1, -1, 4, 15, 28, -1, -1, 31, 10]
9Check slot1777EmptyPlace key at index 7[22, -1, -1, -1, 4, 15, 28, 17, -1, 31, 10]
10Insert key8888 % 11 = 00Occupied (22)Move to index 1[22, -1, -1, -1, 4, 15, 28, 17, -1, 31, 10]
11Check slot8811EmptyPlace key at index 1[22, 88, -1, -1, 4, 15, 28, 17, -1, 31, 10]
12Insert key5959 % 11 = 44Occupied (4)Move to index 5[22, 88, -1, -1, 4, 15, 28, 17, -1, 31, 10]
13Check slot5955Occupied (15)Move to index 6[22, 88, -1, -1, 4, 15, 28, 17, -1, 31, 10]
14Check slot5966Occupied (28)Move to index 7[22, 88, -1, -1, 4, 15, 28, 17, -1, 31, 10]
15Check slot5977Occupied (17)Move to index 8[22, 88, -1, -1, 4, 15, 28, 17, -1, 31, 10]
16Check slot5988EmptyPlace key at index 8[22, 88, -1, -1, 4, 15, 28, 17, 59, 31, 10]
17Insert key55 % 11 = 55Occupied (15)Move to index 6[22, 88, -1, -1, 4, 15, 28, 17, 59, 31, 10]
18Check slot566Occupied (28)Move to index 7[22, 88, -1, -1, 4, 15, 28, 17, 59, 31, 10]
19Check slot577Occupied (17)Move to index 8[22, 88, -1, -1, 4, 15, 28, 17, 59, 31, 10]
20Check slot588Occupied (59)Move to index 9[22, 88, -1, -1, 4, 15, 28, 17, 59, 31, 10]
21Check slot599Occupied (31)Move to index 10[22, 88, -1, -1, 4, 15, 28, 17, 59, 31, 10]
22Check slot51010Occupied (10)Move to index 0[22, 88, -1, -1, 4, 15, 28, 17, 59, 31, 10]
23Check slot500Occupied (22)Move to index 1[22, 88, -1, -1, 4, 15, 28, 17, 59, 31, 10]
24Check slot511Occupied (88)Move to index 2[22, 88, -1, -1, 4, 15, 28, 17, 59, 31, 10]
25Check slot522EmptyPlace key at index 2[22, 88, 5, -1, 4, 15, 28, 17, 59, 31, 10]
26End-----Insertion complete
27Final Table State-----[22, 88, 5, -1, 4, 15, 28, 17, 59, 31, 10]
💡 Insertion stops when an empty slot is found or table is full.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 6After Step 9After Step 11After Step 16After Step 25Final
index-1009457182-
table[-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1][-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,10][22,-1,-1,-1,-1,-1,-1,-1,-1,-1,10][22,-1,-1,-1,-1,-1,-1,-1,-1,31,10][22,-1,-1,-1,4,-1,-1,-1,-1,31,10][22,-1,-1,-1,4,15,-1,-1,-1,31,10][22,-1,-1,-1,4,15,-1,17,-1,31,10][22,88,-1,-1,4,15,-1,17,-1,31,10][22,88,-1,-1,4,15,-1,17,59,31,10][22,88,5,-1,4,15,-1,17,59,31,10][22,88,5,-1,4,15,-1,17,59,31,10]
Key Moments - 3 Insights
Why do we move to the next index when the slot is occupied?
Because in linear probing, if the computed slot is taken, we check the next slot to find an empty place. This is shown in execution_table rows 5, 8, 10, etc., where the slot is occupied and the index moves forward.
What happens if the index reaches the end of the table?
The index wraps around to the start using modulo operation. For example, in step 22, index moves from 10 to 0, ensuring we check all slots circularly.
Why do we stop inserting when we find an empty slot?
Because the key can be safely placed there without collision. This is shown in steps 1, 2, 3, 4, 6, 9, 11, 16, and 25 where insertion happens at empty slots.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 5. What is the slot status at index 4?
AEmpty
BOut of bounds
COccupied
DDeleted
💡 Hint
Check the 'Slot Status' column for step 5 in execution_table.
At which step does the key 59 get inserted into the table?
AStep 16
BStep 14
CStep 13
DStep 15
💡 Hint
Look for the row where 'Place key at index 8' happens for key 59.
If the key 5 was inserted before key 59, at which index would it be placed according to the variable_tracker?
AIndex 5
BIndex 2
CIndex 8
DIndex 0
💡 Hint
Check variable_tracker for index values after step 25 where key 5 is placed.
Concept Snapshot
Collision Handling Using Open Addressing Linear Probing:
- Compute hash index: index = key % table_size
- If slot empty, insert key
- If occupied, move to next slot (index+1)%size
- Repeat until empty slot found
- Wrap around table if needed
- Stops when key inserted or table full
Full Transcript
This visualization shows how keys are inserted into a hash table using linear probing to handle collisions. Each key's hash index is computed using modulo operation. If the slot at that index is empty, the key is placed there. If occupied, the algorithm moves to the next slot, wrapping around the table if needed, until an empty slot is found. The execution table tracks each insertion step, showing the key, computed index, slot checked, slot status, action taken, and the table state after the operation. The variable tracker shows how the index and table change over time. Key moments clarify why the algorithm moves to the next slot on collision, how wrapping works, and when insertion stops. The visual quiz tests understanding of slot status, insertion steps, and final placement. This method ensures all keys are stored without overwriting existing keys by probing linearly for free slots.