retagged by
4,319 views
9 votes
9 votes
The time complexity of banker's algorithm to avoid deadlock having $n$ processes and $m$ resources is?
retagged by

1 Answer

Best answer
19 votes
19 votes
Let's keep it Simple and not go too much formal. Explaining the Algorithm just the way we solve any Question in Banker's algo to keep it simple and easy to understand. There might be other more efficient ways using Data Structures like Hash table etc... But I'll use the simplest data structures like Array and matrix(2-D Array) to elaborate my point.

Deadlock Avoidance, assuming that we are in a safe state and we are requested certain resources, simulates the allocation of those resources and determines if the resultant state is safe. If it is safe then request is satisfied, otherwise it is delayed until it becomes safe.

The Banker's Algorithm is used to determine if a request can be satisfied. It uses the following variables :

1. AVAILABLE =  it specifies for each resource how many copies of it are available. Let's take an array data structure to represent it. Hence We'll have Avail$[1...m]$ of integers to represent AVAILABLE array where I assume that Each resource is named $1 \,\,to \,\,m$, Hence, Each Array index corresponds to one specific resource with same name as array index.

2. ALLOCATION =  Array $ALLOC[1..n, 1..m]$ of integer; -- $ALLOC[i,j]$ specifies the number of copies of resource $j$ that are allocated to process $i$. So, Rows represent Processes numbered from $1\,\,to\,\,n$ and columns represent Resources numbered from $1 \,\, to \,\,m$

3. MAX : Array MAX$[1..n, 1..m]$ of integer; -- $MAXIM[i,j]$ specifies the maximum number of copies of resource $j$ that process $i$ will use.

4. NEED; array $NEED[1..n, 1..m]$ of integer; -- $NEED[i,j]$ specifies the number of copies of resource $j$ that process $i$ still requires. It is equal to $MAX[i,j]-ALLOC[i,j]$

Now let's analyze the time comp just like we apply Banker's algo to any problem. (It would help if you keep some example/data with you)

So, First We Try to find that Process whose Need we can satisfy with the Available resources. Hence, We need to scan through All the Rows to NEED matrix to find such process which in worst case might take $O(mn)$ time as we, in worst case, might have to scan all the $n$ rows of NEED matrix with the AVAIL Array and in each row scan we have to compare $m$ elements (of NEED of process $P_i$ and AVAIL array)..Hence $O(mn)$

After this, If we find such Row i.e. Process $P_i$ whose need we can satisfy then we allocate it its needed resources and let it complete. Which when completed, leaves all its resources it was previously holding(ALLOC matrix $P_i$ row) and these resources are added into the AVAIL array. This takes $O(m)$ time as there are $m$ resources. So, So far, For this First Process $P_i$ we have spent $O(mn) + O(m)$ time. And Now it is out of our concern and so out of further consideration.

Then We move on to find next process $P_j$ whose request we can satisfy. And Same Procedure which we applied for Process $P_i$, is applied for it as well. i.e. We scan through each Row of NEED array and try to find such process $P_j$ whose NEED we can satisfy by comparing its NEED with the AVAIL array... Hence, In worst case, We might have to scan through all the (n-1) Rows of NEED array and for each Row, $m$ comparisons with AVAIL array. Hence, Time taken $O(m(n-1))$

And After finding such process, we allocate it its needed resources and let it complete. Which when completed, leaves all its resources it was previously holding(ALLOC matrix $P_j$ row) and these resources are added into the AVAIL array. This takes $O(m)$ time as there are $m$ resources. So, So far, For this Second Process $P_j$ we have spent $O(m(n-1)) + O(m)$ time. And Now it is out of our concern and so out of further consideration.

Repeat this same procedure until All are completed or We can't find a safe sequence in between.

So, Time comp in worst case = $O(mn) + O(m(n-1)) + O(m(n-2)) + ..... + O(m) = O(m.n^2)$
selected by

Related questions

3 votes
3 votes
2 answers
2
asu asked Nov 15, 2015
6,825 views
It the Resource-Allocation Graph contains $'m'$ types of resource and $'n'$ processes, then the time complexity of the algorithm for deciding whether the system is safe o...
2 votes
2 votes
2 answers
3
7 votes
7 votes
6 answers
4
Sunil8860 asked Jul 16, 2017
10,747 views
If a process is in unsafe state, then:(a) It is in deadlock (b) It might successfully complete(c) It will lead to deadlock (d) None of the above