Good morning,
I'm trying to find a way to calulate time complexity (average and worst) of greedy algorithm. I know that final formula is: O(nlogn + n) which is O(nlogn). I will appreciate any suggestion/hints how I can calculate this.
Regards
Good morning,
I'm trying to find a way to calulate time complexity (average and worst) of greedy algorithm. I know that final formula is: O(nlogn + n) which is O(nlogn). I will appreciate any suggestion/hints how I can calculate this.
Regards
Because the greedy algorithms can be conclude as follows:
Initially let R be the set of all requests,and let A be empty
While R is not yet empty
Choose a request iR that has the smallest finishing time
Add request i to A
Delete all requests from R that are not compatible with request i
EndWhile
Return the set A <is the set of accepted requests
Therefore, the running time of it is consist of:
Sorting the n requests in order, which costs O(nlogn).
Constructing the array containing sorted requests, which costs O(n).
Iterating through the intervals in the array, which costs O(n).
Actually, the second and the third step can often be merged into one step. Thus, the running time of the algorithm is O(nlogn).
(References: Jon,Kleinberg, and Éva,Tardos. Algorithm Design)