def algorithm5(problem, trace = None):
    # if it's empty, we're done 
    if problem.numRow <= 0 or problem.numCol <= 0:
        return None

    # the recursive subproblem will involve half the number of columns
    mid = problem.numCol // 2

    # information about the two subproblems
    (subStartC1, subNumC1) = (0, mid)
    (subStartC2, subNumC2) = (mid + 1, problem.numCol - (mid + 1))

    subproblems = []
    subproblems.append((0, subStartC1, problem.numRow, subNumC1))
    subproblems.append((0, subStartC2, problem.numRow, subNumC2))

    # find a 1D peak of the dividing column
    divider = problem.getSubproblem((0, mid, problem.numRow, 1))
    if not trace is None: trace.setProblemDimensions(divider)
    dividerPeak = algorithm1D.algorithm1(divider, trace)
    dividerPeak = problem.getLocationInSelf(divider, dividerPeak)
    if not trace is None: trace.setProblemDimensions(problem)

    # see if the peak we found on the dividing line has a better neighbor
    # (which cannot be on the dividing line, because we know that this
    # location is a peak on the dividing line)
    neighbor = problem.getBetterNeighbor(dividerPeak, trace)

    # this is a peak, so return it
    if neighbor == dividerPeak:
        if not trace is None: trace.foundPeak(dividerPeak)
        return dividerPeak
   
    # otherwise, figure out which subproblem contains the neighbor, and
    # recurse in that half
    sub = problem.getSubproblemContaining(subproblems, neighbor)
    if not trace is None: trace.setProblemDimensions(sub)
    result = algorithm5(sub, trace)
    return problem.getLocationInSelf(sub, result)
def algorithm5(problem, trace = None):
    # if it's empty, we're done 
    if problem.numRow <= 0 or problem.numCol <= 0:
        return None

    # the recursive subproblem will involve half the number of columns
    mid = problem.numCol // 2

    # information about the two subproblems
    (subStartC1, subNumC1) = (0, mid)
    (subStartC2, subNumC2) = (mid + 1, problem.numCol - (mid + 1))

    subproblems = []
    subproblems.append((0, subStartC1, problem.numRow, subNumC1))
    subproblems.append((0, subStartC2, problem.numRow, subNumC2))

    # find a 1D peak of the dividing column
    divider = problem.getSubproblem((0, mid, problem.numRow, 1))
    if not trace is None: trace.setProblemDimensions(divider)
    dividerPeak = algorithm1D.algorithm1(divider, trace)
    dividerPeak = problem.getLocationInSelf(divider, dividerPeak)
    if not trace is None: trace.setProblemDimensions(problem)

    # see if the peak we found on the dividing line has a better neighbor
    # (which cannot be on the dividing line, because we know that this
    # location is a peak on the dividing line)
    neighbor = problem.getBetterNeighbor(dividerPeak, trace)

    # this is a peak, so return it
    if neighbor == dividerPeak:
        if not trace is None: trace.foundPeak(dividerPeak)
        return dividerPeak
   
    # otherwise, figure out which subproblem contains the neighbor, and
    # recurse in that half
    sub = problem.getSubproblemContaining(subproblems, neighbor)
    if not trace is None: trace.setProblemDimensions(sub)
    result = algorithm5(sub, trace)
    return problem.getLocationInSelf(sub, result)
Exemple #3
0
def algorithm4(problem, trace = None):
    # if it's empty, we're done
    if problem.numRow <= 0 or problem.numCol <= 0:
        return None

    # the recursive subproblem will involve half the number of columns
    mid = problem.numCol // 2

    # information about the two subproblems
    (subStartR, subNumR) = (0, problem.numRow)
    (subStartC1, subNumC1) = (0, mid)
    (subStartC2, subNumC2) = (mid + 1, problem.numCol - (mid + 1))

    subproblems = []
    subproblems.append((subStartR, subStartC1, subNumR, subNumC1))
    subproblems.append((subStartR, subStartC2, subNumR, subNumC2))

    # find A PEAK in the dividing column
    columnSubproblem = problem.getSubproblem((0, mid, problem.numRow, 1))
    bestLocInColSubproblem = algorithm1D.algorithm1(columnSubproblem)
    bestLoc = problem.getLocationInSelf(columnSubproblem, bestLocInColSubproblem)

    # see if the maximum value we found on the dividing line has a better
    # neighbor (which cannot be on the dividing line, because we know that
    # this location is the best on the dividing line)
    neighbor = problem.getBetterNeighbor(bestLoc, trace)

    # this is a peak, so return it
    if neighbor == bestLoc:
        if not trace is None: trace.foundPeak(bestLoc)
        return bestLoc

    # otherwise, figure out which subproblem contains the neighbor, and
    # recurse in that half
    sub = problem.getSubproblemContaining(subproblems, neighbor)
    if not trace is None: trace.setProblemDimensions(sub)
    result = algorithm4(sub, trace)
    return problem.getLocationInSelf(sub, result)