project1/p1q2.rb
# n is top n actors to consider (0 to n inclusive)
n is top n actors to consider (0 to n inclusive)
n is top n actors to consider ( 0 to n inclusive )
def select_actors_greedy(k, r, n, actor_list)
  result_a = [0]
  result_m = actor_list[0][1].clone
  result_q = result_m.length
  actor_indexes = Array(1..n)
  1.upto(k-1) do |i|
    best_index = actor_indexes[0] 
    best_q = result_q
    best_m = result_m.clone
    for index in actor_indexes do
      break if result_q + actor_list[index][1].length < best_q
      c_movies = best_m.clone | actor_list[index][1]
      if c_movies.length > best_q
        best_index = index
        best_q = c_movies.length
        best_m = c_movies
      end
    end
    result_a << best_index
    result_q = best_q
    result_m = best_m
    actor_indexes.delete(best_index)
  end
  return result_a.map { |i| actor_list[i][0] }
end
def select_actors_greedy ( k , r , n , actor_list ) result_a = [ 0 ] result_m = actor_list [ 0 ] [ 1 ] . clone result_q = result_m . length actor_indexes = Array ( 1 .. n ) 1 . upto ( k - 1 ) do | i | best_index = actor_indexes [ 0 ] best_q = result_q best_m = result_m . clone for index in actor_indexes do break if result_q + actor_list [ index ] [ 1 ] . length < best_q c_movies = best_m . clone | actor_list [ index ] [ 1 ] if c_movies . length > best_q best_index = index best_q = c_movies . length best_m = c_movies end end result_a << best_index result_q = best_q result_m = best_m actor_indexes . delete ( best_index ) end return result_a . map { | i | actor_list [ i ] [ 0 ] } end
n is top n actors to consider (0 to n inclusive)
n is top n actors to consider ( 0 to n inclusive )
# pick the actor with the most movies as first actor # store the indexes of actors that are still being considered # pick the remaining k-1 actors (since 1st has been picked) # first actor still being considered # examine all the actors still being considered # optimisation # don't go on if the remaining actors are too small to improve on # the currently found best quality # get all the movies that the actors are in (without duplicates) # replace if better than previous solution # take the best found solution # convert actor ID to actor names
k
r
n
actor_list