\(b)\) Choose the course \(x\) that starts first, discard all classes that conflict with \(x\), and recurse.
S = [1, 2, 3]
F = [9, 3, 4]
\(d)\) Choose the course \(x\) with shortest duration, discard all classes that conflict with \(x\), and recurse.
S = [1, 3, 4]
F = [4, 5, 9]
Notice the “scare quotes” around “first”.
\(c)\) Choose the course \(x\) that starts last, discard all classes that conflict with \(x\), and recurse.
Supposing GREEDYSCHEDULE returns \(m\), let \(g_1, g_2,\ldots,g_m\) be the classes the algorithm chose (in order of being chosen, so in reverse chronological order). Suppose also that \(g_1,g_2,\ldots, g_{j-1}, c_j, c_{j+1},\ldots, c_{m'}\) is also a compatible list of classes with \(m' \geq m\).
Since \(g_j\) is part of the first solution, it must end before \(g_{j−1}\) and since it was chosen by the algorithm it must start after all the classes excluding \(g_1, g_2,\ldots, g_{j-1}\), or specifically it must start after \(c_{j+1}\). This means \(g_j\) can replace \(c_j\), giving another solution: \(g_1,g_2,\ldots,g_{j−1},g_j,c_{j+1},\ldots,c_{m'}\).
This can continue, replacing all \(c_i\)’s with \(g_i\)’s so that \(m=m'\), which proves that the greedy solution is optimal.
Suppose this greedy algorithm returns \(m\). Let \(g_1, g_2, \ldots, g_m\) be the events chosen by this greedy algorithm.
Suppose that \(c_1,c_2, \ldots, c_{j−1},c_j, g_{j+1}, \ldots, g_{m'}\) is another sequence of compatible events, with \(m'\geq m\).
Since event \(g_j\) is part of the first solution, it must end before \(g_{j+1}\). Given the nature of the greedy algorithm, event \(g_j\) starts at the same time or after all other events ending before \(g_{j+1}\). In particular, it must start after or at the same time as \(c_j\). Therefore, we can replace \(c_j\) with \(g_j\) and obtain another solution \(c_1,c_2, \ldots, c_{j−1},g_j,g_{j+1},\ldots, g_{m'}\). Continuing this way, all of the \(c_i\)’s can be replaced with \(g_j\)’s. Therefore \(m=m'\), and the greedy solution is optimal.
Given tasks \(a_1, a_2, \ldots, a_n\) with running times \(t_1, t_2, \ldots t_n\), describe an algorithm that will determine the ordering of these tasks that minimizes the average completion time.
Table #1 | Table #2 | Table #3 | Table #4 | Table #5 | Table #6 | Table #7 |
---|---|---|---|---|---|---|
John | Jack | Claire | James | Jordan | Drake | Trevor |
Andrew | Logan | Kristen | Talia | Levi | Grace | Blake |
Josiah | Ethan | Nathan | Bri | Kevin | Graham | Isaac |
Let T[1..n]
be an array such that \(T[i]\) is the time it takes to do task \(a_i\). Describe a greedy algorithm that returns a sequence \(g_1, g_2, \ldots g_n\) such that executing the tasks in the order \(a_{g_1}, a_{g_2}, \ldots a_{g_n}\) has the minimum possible average completion time.
Prove your algorithm is correct: