최근에 Tardos 및 Kleinberg의 알고리즘 디자인 4 장에있는 간격 스케줄링 알고리즘에 대해 읽었습니다. 간격 일정 문제로 제공하는 솔루션이이었다 :욕심 많은 알고리즘을위한 최적 알고리즘 : 간격 스케줄링 - 모든 간격 예약
C++/자바/파이썬에서이 알고리즘은 여기에서 찾을 수 있습니다Sort the n intervals based on their finishing time and store in array F
A = [] #list of intervals
S = array with property that S[i] contains the finishing time of the ith interval
i = 0
For j = 0 to n-1
if S[j] >= F[i] #this interval does not overlap with the ith interval
i = j
A.add(j)
: http://www.geeksforgeeks.org/greedy-algorithms-set-1-activity-selection-problem/
또한 간격 색칠 문제로 알려진 문제의 확장, 다음과 같이 설명 할 수있다.
는 우리는 간격 세트를 제공, 우리는 간격이 같은 색이 교차하지 않는 부여 및 목표 가 사용되는 색상의 수를 최소화하는 것입니다 있도록 모든 간격 색상 할 수 있습니다.
Sort the intervals by their start times in a list I n = len(I) For j = 1 to n: For each interval I[i] that precedes I[j] and overlaps it: Exclude the label of I[i] from consideration for I[j] The jth interval is assigned a nonexcluded label
그러나이 알고리즘의 실행 시간 O (N^2)되지 않습니다 :이 책에서 제안
알고리즘이 있었다? 나는 더 나은 것을 생각했지만 그것이 맞는지 확실하지 않습니다. 기본적으로 Interval Scheduling 알고리즘을 수정합니다.
Sort the n intervals based on their finishing time and store in array F
A = []
x = 1
S = array with property that S[i] contains the finishing time of the ith interval
i = 0
while F is not empty:
new = []
For j = 0 to n-1
if S[j] >= F[i] #this interval does not overlap with the ith interval
i = j
A.add(F[j])
else:
new.add(F[j])
F = new
Recompute S
print x, A
A = []
x++
의사 코드에 어리석은 버그가있을 수 있지만 알고리즘이 무엇을하는지 이해하시기 바랍니다. 기본적으로 나는 표준 간격 스케줄링 알고리즘을 반복적으로 실행하여 가능한 모든 색상을 떼어 낼 것입니다.
내 알고리즘이 간격의 다양한 색상을 모두 인쇄하지 않아야합니까? 이 알고리즘이 올바른 알고리즘이라면 이전 알고리즘보다 효율적입니까? 그렇지 않은 경우, 반대 사례를 제공해 주시겠습니까? 감사합니다. (간격 스케쥴에 대한 자세한 내용은 여기 : https://en.wikipedia.org/wiki/Interval_scheduling)
알고리즘의 시작 시간에 따라 알고리즘에서 해당 간격을 이미 정렬하지 않았습니까? 그래도 고마워! – SinByCos
문제를 공식화 할 수 있습니다. 이미 시작 시간 * 순으로 정렬 된 간격 목록 *을 요청하면 물론 정렬 단계를 생략 할 수 있습니다. 이는 시간 복잡성을 예를 들어, O (n)은 주 루프가 얼마나 효율적인지에 따라 달라집니다. –
하지만 주 루프를 어떻게 효율적으로 만들 수 있습니까? 반복 할 때마다 앞선 이전 간격을 확인하지 않아도됩니까? – SinByCos