greedy_method

greedy_method - Greedy Method Objective: General approach:...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
Abdelghani Bellaachia, Algorithms - 1 - Greedy Method ˜ Objective: ˜ General approach: Given a set of n inputs. Find a subset, called feasible solution, of the n inputs subject to some constraints, and satisfying a given objective function. If the objective function is maximized or minimized, the feasible solution is optimal. It is a locally optimal method. ˜ Algorithm: F Step 1: Choose an input from the input set, based on some criterion. If no more input exit. F Step 2: Check whether the chosen input yields to a feasible solution. If no, discard the input and goto step 1. F Step 3: Include the input into the solution vector and update the objective function. Goto step 1.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Abdelghani Bellaachia, Algorithms - 2 - Optimal merge patterns ˜ Introduction: Merge two files each has n m elements, respectively: takes O (n+m). Given n files What's the minimum time needed to merge all n files? Example: (F 1 , F 2 , F 3 , F 4 , F 5 )= (20, 30, 10, 5, 30). M 1 = F 1 F 2 20+30 = 50 M 2 = M 1 F 3 50+10 = 60 M 3 = M 2 F 4 60+5 = 65 M 4 = M 3 F 5 65+30 = 95 270 Optimal merge pattern : Greedy method. Sort the list of files: (5,10, 20, 30, 30)= (F 4 , F 3 , F 1 , F 2 , F 5 )
Background image of page 2
Abdelghani Bellaachia, Algorithms - 3 - Merge the first two files: (5, 10, 20, 30, 30) L (15, 20, 30, 30) Merge the next two files: (15, 20, 30, 30) L (30, 30, 35) Merge the next two files: (30, 30, 35) L ( 35, 60) Merge the last two files: (35,60) L ( 95) Total time: 15 + 35 + 60 + 95 = 205 This is called a 2-way merge pattern. Problem: ü Given n sorted files ü Merge n files in a minimum amount of time. Algorithm : ü We associate with each file a node Left Weight Right
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Abdelghani Bellaachia, Algorithms - 4 - ˜ Example : Initial ü 1 5 1 1 10 1 1 20 1 1 30 1 1 30 1 ü 1 15 1 1 20 1 1 30 1 1 30 1 5 10 ü 1 35 1 1 30 1 1 30 1 15 20 5 10 ü 1 35 1 1 60 1 15 20 30 30 5 10
Background image of page 4
Abdelghani Bellaachia, Algorithms - 5 - ü 1 95 1 35 60 15 20 30 30 5 10 Algorithm: - Least (L): find a tree in L whose root has the smallest weight. - Function : Tree (L,n). Integer i; Begin For i=1 to n -1 do Get node (T) / create a node pointed by T / Left child (T)= Least (L) / first smallest / Right child (T)= Least (L) / second smallest / Weight (T) = weight (left child (T)) + weight (right child (T)) Insert (L,T); / insert new tree with root T in L / End for Return (Least (L)) / tree left in L / End.
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

This document was uploaded on 09/03/2010.

Page1 / 21

greedy_method - Greedy Method Objective: General approach:...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online