Algoritmos_Gulosos

48

description

algoritmos gulosos

Transcript of Algoritmos_Gulosos

GCC111 � Projeto e Análise de Algoritmos

Mayron César O. Moreira

Universidade Federal de Lavras

Departamento de Ciência da Computação

[email protected]�a.br

6 de maio de 2015

Mayron César O. Moreira (UFLA) GCC111 � PAA 6 de maio de 2015 1 / 23

Conteúdo

1 Algoritmos Gulosos

Mayron César O. Moreira (UFLA) GCC111 � PAA 6 de maio de 2015 2 / 23

Algoritmos Gulosos

Contexto

Utilizados com frequência na resolução de problemas de otimização;

Mayron César O. Moreira (UFLA) GCC111 � PAA 6 de maio de 2015 3 / 23

Publicações envolvendo Algoritmos Gulosos

Discrete Optimization

Multiobjective GRASP with Path Relinking

Rafael Martí a,⇑, Vicente Campos a, Mauricio G.C. Resende b, Abraham Duarte c

a Departamento de Estadística e Investigación Operativa, Universidad de Valencia, Spainb Network Evolution Research Department, AT&T Labs Research, 200 S. Laurel Avenue, Room A5-1F34, Middletown, NJ 07748, USAc Departamento de Ciencias de la Computación, Universidad Rey Juan Carlos, Spain

a r t i c l e i n f o

Article history:Received 30 May 2013Accepted 27 June 2014Available online 8 July 2014

Keywords:Multiobjective optimizationBiobjective optimizationGRASPPath Relinking

a b s t r a c t

In this paper we review and propose different adaptations of the GRASP metaheuristic to solve multiob-jective combinatorial optimization problems. In particular, we describe several alternatives to specializethe construction and improvement components of GRASP when two or more objectives are considered.GRASP has been successfully coupled with Path Relinking for single-objective optimization. Moreover, wepropose different hybridizations of GRASP and Path Relinking for multiobjective optimization. We applythe proposed GRASP with Path Relinking variants to two combinatorial optimization problems, thebiobjective orienteering problem and the biobjective path dissimilarity problem. We report on empiricaltests with 70 instances and 30 algorithms, that show that the proposed heuristics are competitive withthe state-of-the-art methods for these problems.

� 2014 Elsevier B.V. All rights reserved.

1. Introduction

The GRASP metaheuristic was developed in the late 1980s (Feo& Resende, 1989, 1995). The acronym was coined in Feo, Resende,and Smith (1994). Since then, GRASP has experienced continueddevelopment and has been applied in a wide range of problemareas (Duarte, Martí, Resende, & Silva, 2014; Sevaux, Rossi, Soto,Duarte, & Martí, 2014). We refer the reader to Resende andRibeiro (2003), Resende and Ribeiro (2010) for recent surveys ofthis metaheuristic. In short, each GRASP iteration consists in con-structing a trial solution with some greedy randomized procedureand then applying local search from the constructed solution. Thistwo-phase process is repeated until some stopping condition issatisfied. The best local optimum found over all local searches isreturned as the solution of the heuristic.

Path Relinking (PR) is an approach that generates new solutionsby exploring trajectories that connect high-quality solutions(Glover & Laguna, 1997). Laguna and Martí (1999) adapted PR inthe context of GRASP as a form of intensification, which consistsin finding a path between a solution found with GRASP and achosen elite solution. See Campos, Martí, Duarte, and Sanchez-Oro (2013), Duarte, Martí, Resende, and Silva (2011), Resende,Martí, Gallego, and Duarte (2010), for successful hybridizations of

GRASP with PR. Resende and Ribeiro (2003) and Ribeiro andResende (2012) present numerous examples of GRASP with PR.

The algorithm in Fig. 1 shows pseudo-code for a generic GRASPwith Path Relinking for minimization. The greedy randomizedconstruction seeks to produce a diverse set of good-quality start-ing solutions from which to start the local search phase. Let x bethe partial solution under construction in a given iteration and letC be the candidate set with all the remaining elements that canbe added to x. The GRASP construction uses a greedy functiongðcÞ to measure the contribution of each candidate elementc 2 C to the partial solution x. A restricted candidate list RCLis the subset of candidate elements from C with good evaluationsaccording to g. In particular, if gmin and gmax are the minimumand maximum evaluations of g in C respectively, thenRCL ¼ fc 2 C jgðcÞ 6 gminþ aðgmax � gminÞ}, where a is a numberin ½0;1�. At each step, the method randomly selects an elementc� from the restricted candidate list and adds this element tothe partial solution. The construction is repeated in the innerwhile loop (steps 4 to 10) until there are no further candidates.If C ¼ ; and x is infeasible, then a repair procedure needs to beapplied to make x feasible (steps 12 to 14). Once a feasible solu-tion x is on hand, a local search improvement is applied. Theresulting solution is a local minimum. If it improves the best solu-tion found so far (step 16), we update it (step 17), and store it inthe elite set ES (step 18). The GRASP algorithm terminates when astopping criterion is met (typically a maximum number of itera-tions, time limit, or a target solution quality). At this stage, weapply the PR method to all pairs of solutions ðx; yÞ stored in ES.

http://dx.doi.org/10.1016/j.ejor.2014.06.0420377-2217/� 2014 Elsevier B.V. All rights reserved.

⇑ Corresponding author.E-mail addresses: [email protected] (R. Martí), [email protected]

(V. Campos), [email protected] (M.G.C. Resende), [email protected](A. Duarte).

European Journal of Operational Research 240 (2015) 54–71

Contents lists available at ScienceDirect

European Journal of Operational Research

journal homepage: www.elsevier .com/locate /e jor

Mayron César O. Moreira (UFLA) GCC111 � PAA 6 de maio de 2015 4 / 23

Teaching learning based optimization with Pareto tournamentfor the multiobjective software requirements selection

José M. Chaves-González n, Miguel A. Pérez-Toledano, Amparo NavasaComputer Science Department, University of Extremadura, Cáceres, Spain

a r t i c l e i n f o

Article history:Received 2 October 2014Received in revised form1 April 2015Accepted 7 April 2015

Keywords:Software requirements selectionMulti-objective evolutionary algorithmTeaching learning based optimizationSearch Based Software EngineeringNext Release ProblemSwarm intelligence

a b s t r a c t

Software requirements selection is a problem which consists of choosing the set of new requirementswhich will be included in the next release of a software package. This NP-hard problem is an importantissue involving several contradictory objectives which have to be tackled by software companies whendeveloping new releases of software packages. Software projects have to stick to a budget, but they alsohave to satisfy the highest number of customer requirements. Furthermore, when managing realinstances of the problem, the requirements tackled suffer interactions and other restrictions which makethe problem even harder. In this paper, a novel multi-objective teaching learning based optimization(TLBO) algorithm has been successfully applied to several instances of the problem. For doing this, thesoftware requirements selection problem has been formulated as a multiobjective optimization problemwith two objectives: the total software development cost and the overall customer's satisfaction. Inaddition, three interaction constraints have been also managed. In this context, the original TLBOalgorithm has been adapted to solve real instances of the problem generated from data provided byexperts. Numerical experiments with case studies on software requirements selection have been carriedout in order to prove the effectiveness of the multiobjective proposal. In fact, the obtained results showthat the developed algorithm performs better than other relevant algorithms previously published in theliterature.

& 2015 Elsevier Ltd. All rights reserved.

1. Introduction

The complexity and extension of modern software systems havebeen increased in the last decade. In addition, software productshave to be usually developed in limited periods of time and withsevere cost restrictions. Thus, software development companieshave to satisfy in an efficient way large sets of requirements byminimizing the production efforts (in time and cost). In fact, in mostof cases it is not possible to develop all the new features suggestedby the clients when the new release of a software product has to beproduced. Software requirements optimization is an important taskin Software Engineering, and especially relevant within the incre-mental approaches of software development, e.g. agile methodol-ogies. In these kinds of methodologies, the software product isdeveloped by generating releases which have to be produced inshort iterative cycles and a new set of requirements, tailored to fitthe needs of the clients and the development costs, is proposed ineach iteration. In this context, the challenge of Software Engineeringconsists of defining which requirements should be developed by

considering several complex factors (different clients' prioritieswith different importance, development efforts, cost restrictions,interactions between different requirements, etc.). There is not asimple solution to this complex problem, which is also called in therelated literature the Next Release Problem, NRP (Bagnall et al.,2001).

The NRP is an NP-hard problem (Garey and Johnson, 1990) whichsimultaneously manages two independent and conflicting objectiveswhich have to be simultaneously optimized: the development effort(cost), and the clients' satisfaction. Thus, the problem cannot bemanaged by traditional exact optimization methods. In this case,multi-objective evolutionary algorithms (MOEAs) are the most appro-priate strategies (Coello et al., 2007; Deb, 2001) because MOEAs tacklesimultaneously several conflicting objectives without the artificialadjustments included in classical single-objective optimization meth-ods. However, most of related works in the bibliography are simplifiedby using an aggregation function and they manage the problem as asingle objective version of the problem. Furthermore, there are othersworks that do not tackle the interactions produced between therequirements in real NRP instances of the problem.

In this paper, a novel technique within the Search-Based Soft-ware Engineering (SBSE) research field (Harman et al., 2012) hasbeen proposed to deal with a real multiobjective version of the NRP(MONRP). Specifically, in this paper we introduce an adapted

Contents lists available at ScienceDirect

journal homepage: www.elsevier.com/locate/engappai

Engineering Applications of Artificial Intelligence

http://dx.doi.org/10.1016/j.engappai.2015.04.0020952-1976/& 2015 Elsevier Ltd. All rights reserved.

n Corresponding author.E-mail addresses: [email protected] (J.M. Chaves-González),

[email protected] (M.A. Pérez-Toledano), [email protected] (A. Navasa).

Engineering Applications of Artificial Intelligence 43 (2015) 89–101

Mayron César O. Moreira (UFLA) GCC111 � PAA 6 de maio de 2015 5 / 23

Intl. Trans. in Op. Res. 00 (2015) 1–26DOI: 10.1111/itor.12167

INTERNATIONALTRANSACTIONS

IN OPERATIONALRESEARCH

Hybridizations of GRASP with path relinking for the far frommost string problem

Daniele Feronea, Paola Festaa and Mauricio G.C. Resendeb

aDepartment of Mathematics and Applications “R. Caccioppoli,” University of Napoli FEDERICO II, Compl. MSA, ViaCintia, 80126 Napoli, Italy

bAlgorithms and Optimization Research Department, AT&T Labs Research, 180 Park Avenue, Room C241, Florham ParkNJ 07932, USA

E-mail: [email protected] [Ferone]; [email protected] [Festa]; [email protected] [Resende]

Received 29 January 2014; received in revised form 23 January 2015; accepted 7 March 2015

Abstract

Among the sequence selection and comparison problems, the far from most string problem (FFMSP) is oneof the computationally hardest with applications in several fields, including molecular biology where oneis interested in creating diagnostic probes for bacterial infections or in discovering potential drug targets.In this paper, we describe several heuristics that hybridize GRASP with different path-relinking strategies,such as forward, backward, mixed, greedy randomized adaptive forward, and evolutionary path relinking.Experiments on a large set of both real-world and randomly generated test instances indicate that these hybridheuristics are both effective and efficient. In particular, the hybrid GRASP with evolutionary path relinkingfinds slightly better quality solutions compared to the other variants when running for the same number ofiterations, while the hybrid with backward path relinking finds better quality solution within a fixed runningtime.

Keywords: string problems; consensus problems; combinatorial optimization; hybrid metaheuristics

1. The far from most string problem

The far from most string problem (FFMSP) is one of the so-called string selection and comparisonproblems that belong to the more general class of problems known as sequences consensus, where afinite set of sequences is given and one is interested in finding their consensus, that is, a new sequencethat agrees as much as possible with all the given sequences. In other words, the objective is todetermine a sequence called consensus, because it represents, in some sense, all the given sequences.For the FFMSP, the objective is to find a sequence that is far from as many sequences as possiblein a given set of sequences all having the same length. The problem has applications in severalfields, including molecular biology where one is interested in creating diagnostic probes for bacterial

C© 2015 The Authors.International Transactions in Operational Research C© 2015 International Federation of Operational Research SocietiesPublished by John Wiley & Sons Ltd, 9600 Garsington Road, Oxford OX4 2DQ, UK and 350 Main St, Malden, MA02148,USA.

Mayron César O. Moreira (UFLA) GCC111 � PAA 6 de maio de 2015 6 / 23

Algoritmos Gulosos

Contexto

Utilizados com frequência na resolução de problemas de otimização;

Toma a melhor decisão local, independente do que vá acontecer nofuturo;

Possui abordagem top-down;

Em problemas com subestrutura ótima, garante a otimalidade dasolução.

Mayron César O. Moreira (UFLA) GCC111 � PAA 6 de maio de 2015 7 / 23

Algoritmos Gulosos

Esquema geral de um Algoritmo Guloso

Conjunto guloso(Conjunto C) {S = ∅;while((C 6= ∅ && not solução(S))) {

x = seleciona(C);C = C − x;if(viável(S + x)) S = S + x;

}if(solução(S)) return S;else return (“Não existe solução”);

}

Mayron César O. Moreira (UFLA) GCC111 � PAA 6 de maio de 2015 8 / 23

Um problema de seleção de atividades

Dados do problema

S = {a1, a2, ..., an}: conjunto de n atividades que desejam utilizar umrecurso;

Cada atividade ai possui um tempo de início si e um tempo detérmino fi , 0 ≤ si < fi <∞;

Se selecionada, a atividade ai ocorre no intervalo de tempo [si , fi ).

Restrição

As atividades não podem ser realizadas em um mesmo intervalo detempo, isto é, o recurso é disponível para apenas uma atividade porvez.

Mayron César O. Moreira (UFLA) GCC111 � PAA 6 de maio de 2015 9 / 23

Um problema de seleção de atividades

Compatibilidade de atividades

As atividades ai e aj são compatíveis se seus intervalos não se sobrepõem,ou seja, si ≥ fj ou sj ≥ fi

i 1 2 3 4 5 6 7 8 9 10 11si 1 0 5 6 2 12 3 5 8 3 8fi 4 6 9 10 14 16 5 7 12 9 11

Atividades compatíveis:

a1, a3;

a2, a6;

a7, a11;

etc.

Mayron César O. Moreira (UFLA) GCC111 � PAA 6 de maio de 2015 10 / 23

Um problema de seleção de atividades

Compatibilidade de atividades

As atividades ai e aj são compatíveis se seus intervalos não se sobrepõem,ou seja, si ≥ fj ou sj ≥ fi

i 1 2 3 4 5 6 7 8 9 10 11si 1 0 5 6 2 12 3 5 8 3 8fi 4 6 9 10 14 16 5 7 12 9 11

Atividades compatíveis:

a1, a3;

a2, a6;

a7, a11;

etc.

Mayron César O. Moreira (UFLA) GCC111 � PAA 6 de maio de 2015 10 / 23

Um problema de seleção de atividades

Compatibilidade de atividades

As atividades ai e aj são compatíveis se seus intervalos não se sobrepõem,ou seja, si ≥ fj ou sj ≥ fi

i 1 2 3 4 5 6 7 8 9 10 11si 1 0 5 6 2 12 3 5 8 3 8fi 4 6 9 10 14 16 5 7 12 9 11

Atividades compatíveis:

a1, a3;

a2, a6;

a7, a11;

etc.

Mayron César O. Moreira (UFLA) GCC111 � PAA 6 de maio de 2015 10 / 23

Um problema de seleção de atividades

Compatibilidade de atividades

As atividades ai e aj são compatíveis se seus intervalos não se sobrepõem,ou seja, si ≥ fj ou sj ≥ fi

i 1 2 3 4 5 6 7 8 9 10 11si 1 0 5 6 2 12 3 5 8 3 8fi 4 6 9 10 14 16 5 7 12 9 11

Atividades compatíveis:

a1, a3;

a2, a6;

a7, a11;

etc.

Mayron César O. Moreira (UFLA) GCC111 � PAA 6 de maio de 2015 10 / 23

Um problema de seleção de atividades

Compatibilidade de atividades

As atividades ai e aj são compatíveis se seus intervalos não se sobrepõem,ou seja, si ≥ fj ou sj ≥ fi

i 1 2 3 4 5 6 7 8 9 10 11si 1 0 5 6 2 12 3 5 8 3 8fi 4 6 9 10 14 16 5 7 12 9 11

Atividades compatíveis:

a1, a3;

a2, a6;

a7, a11;

etc.

Mayron César O. Moreira (UFLA) GCC111 � PAA 6 de maio de 2015 10 / 23

Um problema de seleção de atividades

Objetivo

Selecionar um subconjunto de tamanho máximo de atividades mutuamentecompatíveis.

Ideia

Ordenar as atividades em ordem crescente de tempo de término:f1 ≤ f2 ≤ ... ≤ fn−1 ≤ fn. Renomeando as atividades mostradasanteriormente, dado este critério de ordenação, temos:

Mayron César O. Moreira (UFLA) GCC111 � PAA 6 de maio de 2015 11 / 23

Um problema de seleção de atividades

Objetivo

Selecionar um subconjunto de tamanho máximo de atividades mutuamentecompatíveis.

Ideia

Ordenar as atividades em ordem crescente de tempo de término:f1 ≤ f2 ≤ ... ≤ fn−1 ≤ fn. Renomeando as atividades mostradasanteriormente, dado este critério de ordenação, temos:

i 1 2 3 4 5 6 7 8 9 10 11si 1 3 0 5 3 5 6 8 8 2 12fi 4 5 6 7 9 9 10 11 12 14 16

Mayron César O. Moreira (UFLA) GCC111 � PAA 6 de maio de 2015 11 / 23

Um problema de seleção de atividades

Objetivo

Selecionar um subconjunto de tamanho máximo de atividades mutuamentecompatíveis.

Ideia

Ordenar as atividades em ordem crescente de tempo de término:f1 ≤ f2 ≤ ... ≤ fn−1 ≤ fn. Renomeando as atividades mostradasanteriormente, dado este critério de ordenação, temos:

i 1 2 3 4 5 6 7 8 9 10 11si 1 3 0 5 3 5 6 8 8 2 12fi 4 5 6 7 9 9 10 11 12 14 16

Mayron César O. Moreira (UFLA) GCC111 � PAA 6 de maio de 2015 11 / 23

Um problema de seleção de atividades

Objetivo

Selecionar um subconjunto de tamanho máximo de atividades mutuamentecompatíveis.

Ideia

Ordenar as atividades em ordem crescente de tempo de término:f1 ≤ f2 ≤ ... ≤ fn−1 ≤ fn. Renomeando as atividades mostradasanteriormente, dado este critério de ordenação, temos:

i 1 2 3 4 5 6 7 8 9 10 11si 1 3 0 5 3 5 6 8 8 2 12fi 4 5 6 7 9 9 10 11 12 14 16

Mayron César O. Moreira (UFLA) GCC111 � PAA 6 de maio de 2015 11 / 23

Um problema de seleção de atividades

Subestrutura ótima

Seja Sij o conjunto de atividades que começam após o término da atividadeai e terminam antes do início da atividade aj .Tomemos Sij = ∅, i ≥ j (Por que? S10,2 faria sentido?).

Considere Aij o subconjunto máximo de atividades mutualmentecompatíveis, que inclui alguma atividade ak . Isso nos permite dizerque existem Sik e Skj ;

Sejam Aik = Aij ∩ Sik e Akj = Aij ∩ Skj ;

Então Aij = Aik ∪ {ak} ∪ Akj e portanto, |Aij | = |Aik |+ |Akj |+ 1;

Pelo argumento usual de �recortar e colar�, suponha que exista A′kjonde A′kj > Akj . Poderíamos utilizar A′kj ao invés de Akj ;

Assim, |Aik |+ |A′kj |+ 1 > |Aik |+ |Akj |+ 1 = |Aij |, o que contrdiz ahipótese que Aij é a ótima;

Mayron César O. Moreira (UFLA) GCC111 � PAA 6 de maio de 2015 12 / 23

Um problema de seleção de atividades

Subestrutura ótima

Seja Sij o conjunto de atividades que começam após o término da atividadeai e terminam antes do início da atividade aj .Tomemos Sij = ∅, i ≥ j (Por que? S10,2 faria sentido?).

Considere Aij o subconjunto máximo de atividades mutualmentecompatíveis, que inclui alguma atividade ak . Isso nos permite dizerque existem Sik e Skj ;

Sejam Aik = Aij ∩ Sik e Akj = Aij ∩ Skj ;

Então Aij = Aik ∪ {ak} ∪ Akj e portanto, |Aij | = |Aik |+ |Akj |+ 1;

Pelo argumento usual de �recortar e colar�, suponha que exista A′kjonde A′kj > Akj . Poderíamos utilizar A′kj ao invés de Akj ;

Assim, |Aik |+ |A′kj |+ 1 > |Aik |+ |Akj |+ 1 = |Aij |, o que contrdiz ahipótese que Aij é a ótima;

Mayron César O. Moreira (UFLA) GCC111 � PAA 6 de maio de 2015 12 / 23

Um problema de seleção de atividades

Subestrutura ótima

Seja Sij o conjunto de atividades que começam após o término da atividadeai e terminam antes do início da atividade aj .Tomemos Sij = ∅, i ≥ j (Por que? S10,2 faria sentido?).

Considere Aij o subconjunto máximo de atividades mutualmentecompatíveis, que inclui alguma atividade ak . Isso nos permite dizerque existem Sik e Skj ;

Sejam Aik = Aij ∩ Sik e Akj = Aij ∩ Skj ;

Então Aij = Aik ∪ {ak} ∪ Akj e portanto, |Aij | = |Aik |+ |Akj |+ 1;

Pelo argumento usual de �recortar e colar�, suponha que exista A′kjonde A′kj > Akj . Poderíamos utilizar A′kj ao invés de Akj ;

Assim, |Aik |+ |A′kj |+ 1 > |Aik |+ |Akj |+ 1 = |Aij |, o que contrdiz ahipótese que Aij é a ótima;

Mayron César O. Moreira (UFLA) GCC111 � PAA 6 de maio de 2015 12 / 23

Um problema de seleção de atividades

Subestrutura ótima

Seja Sij o conjunto de atividades que começam após o término da atividadeai e terminam antes do início da atividade aj .Tomemos Sij = ∅, i ≥ j (Por que? S10,2 faria sentido?).

Considere Aij o subconjunto máximo de atividades mutualmentecompatíveis, que inclui alguma atividade ak . Isso nos permite dizerque existem Sik e Skj ;

Sejam Aik = Aij ∩ Sik e Akj = Aij ∩ Skj ;

Então Aij = Aik ∪ {ak} ∪ Akj e portanto, |Aij | = |Aik |+ |Akj |+ 1;

Pelo argumento usual de �recortar e colar�, suponha que exista A′kjonde A′kj > Akj . Poderíamos utilizar A′kj ao invés de Akj ;

Assim, |Aik |+ |A′kj |+ 1 > |Aik |+ |Akj |+ 1 = |Aij |, o que contrdiz ahipótese que Aij é a ótima;

Mayron César O. Moreira (UFLA) GCC111 � PAA 6 de maio de 2015 12 / 23

Um problema de seleção de atividades

Subestrutura ótima

Seja Sij o conjunto de atividades que começam após o término da atividadeai e terminam antes do início da atividade aj .Tomemos Sij = ∅, i ≥ j (Por que? S10,2 faria sentido?).

Considere Aij o subconjunto máximo de atividades mutualmentecompatíveis, que inclui alguma atividade ak . Isso nos permite dizerque existem Sik e Skj ;

Sejam Aik = Aij ∩ Sik e Akj = Aij ∩ Skj ;

Então Aij = Aik ∪ {ak} ∪ Akj e portanto, |Aij | = |Aik |+ |Akj |+ 1;

Pelo argumento usual de �recortar e colar�, suponha que exista A′kjonde A′kj > Akj . Poderíamos utilizar A′kj ao invés de Akj ;

Assim, |Aik |+ |A′kj |+ 1 > |Aik |+ |Akj |+ 1 = |Aij |, o que contrdiz ahipótese que Aij é a ótima;

Mayron César O. Moreira (UFLA) GCC111 � PAA 6 de maio de 2015 12 / 23

Um problema de seleção de atividades

Subestrutura ótima

Seja Sij o conjunto de atividades que começam após o término da atividadeai e terminam antes do início da atividade aj .Tomemos Sij = ∅, i ≥ j (Por que? S10,2 faria sentido?).

Considere Aij o subconjunto máximo de atividades mutualmentecompatíveis, que inclui alguma atividade ak . Isso nos permite dizerque existem Sik e Skj ;

Sejam Aik = Aij ∩ Sik e Akj = Aij ∩ Skj ;

Então Aij = Aik ∪ {ak} ∪ Akj e portanto, |Aij | = |Aik |+ |Akj |+ 1;

Pelo argumento usual de �recortar e colar�, suponha que exista A′kjonde A′kj > Akj . Poderíamos utilizar A′kj ao invés de Akj ;

Assim, |Aik |+ |A′kj |+ 1 > |Aik |+ |Akj |+ 1 = |Aij |, o que contrdiz ahipótese que Aij é a ótima;

Mayron César O. Moreira (UFLA) GCC111 � PAA 6 de maio de 2015 12 / 23

Um problema de seleção de atividades

Uma abordagem: via Programação Dinâmica

c[i , j ] =

{0, Sij = ∅,maxak∈Sij

{c[i , k] + c[k, j ] + 1}, Sij 6= ∅.

Mayron César O. Moreira (UFLA) GCC111 � PAA 6 de maio de 2015 13 / 23

Um problema de seleção de atividades

Dúvida

A escolha gulosa é sempre parte de alguma solução ótima?

Teorema (Garantia de otimalidade nas escolhas gulosas)

Considere Sk 6= ∅ e seja am uma atividade em Sk com o tempo de términomais cedo. Então, am estará incluída em algum subconjunto de tamanhomáximo de atividades mutualmente compatíveis de Sk .

Mayron César O. Moreira (UFLA) GCC111 � PAA 6 de maio de 2015 14 / 23

Um problema de seleção de atividades

Dúvida

A escolha gulosa é sempre parte de alguma solução ótima?

Teorema (Garantia de otimalidade nas escolhas gulosas)

Considere Sk 6= ∅ e seja am uma atividade em Sk com o tempo de términomais cedo. Então, am estará incluída em algum subconjunto de tamanhomáximo de atividades mutualmente compatíveis de Sk .

Mayron César O. Moreira (UFLA) GCC111 � PAA 6 de maio de 2015 14 / 23

Um problema de seleção de atividades

Algoritmo Guloso recursivo

REC-SELECAO-ATIV(s, f , k , n)m = k + 1while(m ≤ n e s[m] < f [k])

m = m + 1if m ≤ nreturn {am} ∪ Rec-Selecao-Ativ(s, f ,m, n)

elsereturn ∅

Mayron César O. Moreira (UFLA) GCC111 � PAA 6 de maio de 2015 15 / 23

Um problema de seleção de atividades

Algoritmo Guloso recursivo

REC-SELECAO-ATIV(s, f , k , n)m = k + 1while(m ≤ n e s[m] < f [k])

m = m + 1if m ≤ nreturn {am} ∪ Rec-Selecao-Ativ(s, f ,m, n)

elsereturn ∅

Primeira chamada recursiva: REC-SELECAO-ATIV(s, f , 0, 11)

i 1 2 3 4 5 6 7 8 9 10 11si 1 3 0 5 3 5 6 8 8 2 12fi 4 5 6 7 9 9 10 11 12 14 16

Mayron César O. Moreira (UFLA) GCC111 � PAA 6 de maio de 2015 15 / 23

Um problema de seleção de atividades

Algoritmo Guloso iterativo

SELECAO-ATIV(s, f )A = {a1}k = 1for m = 2 to nif s[m] ≥ f [k]

A = A ∪ {am}k = m

return A

Mayron César O. Moreira (UFLA) GCC111 � PAA 6 de maio de 2015 16 / 23

Um problema de seleção de atividades

Algoritmo Guloso iterativo

SELECAO-ATIV(s, f )A = {a1}k = 1for m = 2 to nif s[m] ≥ f [k]

A = A ∪ {am}k = m

return A

i 1 2 3 4 5 6 7 8 9 10 11si 1 3 0 5 3 5 6 8 8 2 12fi 4 5 6 7 9 9 10 11 12 14 16

Mayron César O. Moreira (UFLA) GCC111 � PAA 6 de maio de 2015 16 / 23

Um problema de seleção de atividades

Perguntas

Em vez de priorizar a primeira atividade a terminar, e se escolhermos aúltima a atividade a começar. O que mudaria?

Outras formar de selecionar uma tarefa, tais como priorizar a quepossui menor duração ou a que se sobrepõe ao menor número deoutras atividades, possuem o mesmo efeito quanto à otimalidade?

Mayron César O. Moreira (UFLA) GCC111 � PAA 6 de maio de 2015 17 / 23

Elementos da estratégia gulosa

Propriedade da escolha gulosa

É nesse ponto que os algoritmos gulosos são diferentes da PD;

Na PD, fazemos uma escolha em cada etapa, mas normalmente, estadepende das soluções dos subproblemas;

PD utiliza a estratégia botton-up, ao passo que os algoritmos gulosos,aplicam a estratégia top-down.

Mayron César O. Moreira (UFLA) GCC111 � PAA 6 de maio de 2015 18 / 23

Elementos da estratégia gulosa

Propriedade da escolha gulosa

É nesse ponto que os algoritmos gulosos são diferentes da PD;

Na PD, fazemos uma escolha em cada etapa, mas normalmente, estadepende das soluções dos subproblemas;

PD utiliza a estratégia botton-up, ao passo que os algoritmos gulosos,aplicam a estratégia top-down.

Mayron César O. Moreira (UFLA) GCC111 � PAA 6 de maio de 2015 18 / 23

Elementos da estratégia gulosa

Propriedade da escolha gulosa

É nesse ponto que os algoritmos gulosos são diferentes da PD;

Na PD, fazemos uma escolha em cada etapa, mas normalmente, estadepende das soluções dos subproblemas;

PD utiliza a estratégia botton-up, ao passo que os algoritmos gulosos,aplicam a estratégia top-down.

Mayron César O. Moreira (UFLA) GCC111 � PAA 6 de maio de 2015 18 / 23

Elementos da estratégia gulosa

Subestrutura ótima

Devemos mostrar que uma solução ótima para o subproblema,combinada com a escolha gulosa já feita, produz uma solução ótimapara o problema original;

Uso implícito do princípio da indução: fazer a escolha gulosa em cadaetapa produz uma solução ótima.

Mayron César O. Moreira (UFLA) GCC111 � PAA 6 de maio de 2015 19 / 23

Estratégia Gulosa vs. PD

Problema da mochila 0-1

Um ladrão que assalta uma loja encontra n itens. O i-ésimo item vale vireais e pesa wi quilos, em que vi ,wi ∈ Z∗+. Ele deseja levar consigo a cargamais valiosa possível, mas só pode carregar W aquilos. Quais os itens eledeve levar?

Problema da mochila fracionária

A mesma ideia da anterior, mas o ladrão pode levar frações dos itens.

Mayron César O. Moreira (UFLA) GCC111 � PAA 6 de maio de 2015 20 / 23

Estratégia Gulosa vs. PD

Problema da mochila 0-1

Um ladrão que assalta uma loja encontra n itens. O i-ésimo item vale vireais e pesa wi quilos, em que vi ,wi ∈ Z∗+. Ele deseja levar consigo a cargamais valiosa possível, mas só pode carregar W aquilos. Quais os itens eledeve levar?

Problema da mochila fracionária

A mesma ideia da anterior, mas o ladrão pode levar frações dos itens.

Mayron César O. Moreira (UFLA) GCC111 � PAA 6 de maio de 2015 20 / 23

Estratégia Gulosa vs. PD

Exemplo

Consideremos 3 itens no problema da mochila tais quev1 = $60, v2 = $100, v3 = $120, com w1 = 10kg ,w2 = 20kg e w3 = 30kg .Vamos tomar W = 50kg como a capacidade da mochila.

Como resolver a mochila fracionária com uma estratégia gulosa?

ordenar de maneira decescente viwi;

levar o máximo possível do item com maior valor viwi;

se houver espaço, levar o máximo do próximo item;

repete este algoritmo até que a mochila �que completamente cheia;

Solução: 10Kg do item 1, 20Kg do item 2 e 2030

do item 3, comf =

∑i viwi = $240.

Complexidade: O(nlgn).

Mayron César O. Moreira (UFLA) GCC111 � PAA 6 de maio de 2015 21 / 23

Estratégia Gulosa vs. PD

Exemplo

Consideremos 3 itens no problema da mochila tais quev1 = $60, v2 = $100, v3 = $120, com w1 = 10kg ,w2 = 20kg e w3 = 30kg .Vamos tomar W = 50kg como a capacidade da mochila.

Como resolver a mochila fracionária com uma estratégia gulosa?

ordenar de maneira decescente viwi;

levar o máximo possível do item com maior valor viwi;

se houver espaço, levar o máximo do próximo item;

repete este algoritmo até que a mochila �que completamente cheia;

Solução: 10Kg do item 1, 20Kg do item 2 e 2030

do item 3, comf =

∑i viwi = $240.

Complexidade: O(nlgn).

Mayron César O. Moreira (UFLA) GCC111 � PAA 6 de maio de 2015 21 / 23

Estratégia Gulosa vs. PD

Exemplo

Consideremos 3 itens no problema da mochila tais quev1 = $60, v2 = $100, v3 = $120, com w1 = 10kg ,w2 = 20kg e w3 = 30kg .Vamos tomar W = 50kg como a capacidade da mochila.

Como resolver a mochila fracionária com uma estratégia gulosa?

ordenar de maneira decescente viwi;

levar o máximo possível do item com maior valor viwi;

se houver espaço, levar o máximo do próximo item;

repete este algoritmo até que a mochila �que completamente cheia;

Solução: 10Kg do item 1, 20Kg do item 2 e 2030

do item 3, comf =

∑i viwi = $240.

Complexidade: O(nlgn).

Mayron César O. Moreira (UFLA) GCC111 � PAA 6 de maio de 2015 21 / 23

Estratégia Gulosa vs. PD

Exemplo

Consideremos 3 itens no problema da mochila tais quev1 = $60, v2 = $100, v3 = $120, com w1 = 10kg ,w2 = 20kg e w3 = 30kg .Vamos tomar W = 50kg como a capacidade da mochila.

Como resolver a mochila fracionária com uma estratégia gulosa?

ordenar de maneira decescente viwi;

levar o máximo possível do item com maior valor viwi;

se houver espaço, levar o máximo do próximo item;

repete este algoritmo até que a mochila �que completamente cheia;

Solução: 10Kg do item 1, 20Kg do item 2 e 2030

do item 3, comf =

∑i viwi = $240.

Complexidade: O(nlgn).

Mayron César O. Moreira (UFLA) GCC111 � PAA 6 de maio de 2015 21 / 23

Estratégia Gulosa vs. PD

Exemplo

Consideremos 3 itens no problema da mochila tais quev1 = $60, v2 = $100, v3 = $120, com w1 = 10kg ,w2 = 20kg e w3 = 30kg .Vamos tomar W = 50kg como a capacidade da mochila.

Como resolver a mochila fracionária com uma estratégia gulosa?

ordenar de maneira decescente viwi;

levar o máximo possível do item com maior valor viwi;

se houver espaço, levar o máximo do próximo item;

repete este algoritmo até que a mochila �que completamente cheia;

Solução: 10Kg do item 1, 20Kg do item 2 e 2030

do item 3, comf =

∑i viwi = $240.

Complexidade: O(nlgn).

Mayron César O. Moreira (UFLA) GCC111 � PAA 6 de maio de 2015 21 / 23

Estratégia Gulosa vs. PD

Exemplo

Consideremos 3 itens no problema da mochila tais quev1 = $60, v2 = $100, v3 = $120, com w1 = 10kg ,w2 = 20kg e w3 = 30kg .Vamos tomar W = 50kg como a capacidade da mochila.

Como resolver a mochila fracionária com uma estratégia gulosa?

ordenar de maneira decescente viwi;

levar o máximo possível do item com maior valor viwi;

se houver espaço, levar o máximo do próximo item;

repete este algoritmo até que a mochila �que completamente cheia;

Solução: 10Kg do item 1, 20Kg do item 2 e 2030

do item 3, comf =

∑i viwi = $240.

Complexidade: O(nlgn).

Mayron César O. Moreira (UFLA) GCC111 � PAA 6 de maio de 2015 21 / 23

Estratégia Gulosa vs. PD

Exemplo

Consideremos 3 itens no problema da mochila tais quev1 = $60, v2 = $100, v3 = $120, com w1 = 10kg ,w2 = 20kg e w3 = 30kg .Vamos tomar W = 50kg como a capacidade da mochila.

Estratégia gulosa não resolve a mochila 0-1

Solução 1: 10Kg do item 1 e 20Kg do item 2 ⇒ $160;

Solução 2: 10Kg do item 1 e 30Kg do item 3 ⇒ $180;

Solução 3 (Ótima): 20Kg do item 2 e 30Kg do item 3 ⇒ $220.

Mayron César O. Moreira (UFLA) GCC111 � PAA 6 de maio de 2015 22 / 23

Estratégia Gulosa vs. PD

Exemplo

Consideremos 3 itens no problema da mochila tais quev1 = $60, v2 = $100, v3 = $120, com w1 = 10kg ,w2 = 20kg e w3 = 30kg .Vamos tomar W = 50kg como a capacidade da mochila.

Estratégia gulosa não resolve a mochila 0-1

Solução 1: 10Kg do item 1 e 20Kg do item 2 ⇒ $160;

Solução 2: 10Kg do item 1 e 30Kg do item 3 ⇒ $180;

Solução 3 (Ótima): 20Kg do item 2 e 30Kg do item 3 ⇒ $220.

Mayron César O. Moreira (UFLA) GCC111 � PAA 6 de maio de 2015 22 / 23

Estratégia Gulosa vs. PD

Exemplo

Consideremos 3 itens no problema da mochila tais quev1 = $60, v2 = $100, v3 = $120, com w1 = 10kg ,w2 = 20kg e w3 = 30kg .Vamos tomar W = 50kg como a capacidade da mochila.

Estratégia gulosa não resolve a mochila 0-1

Solução 1: 10Kg do item 1 e 20Kg do item 2 ⇒ $160;

Solução 2: 10Kg do item 1 e 30Kg do item 3 ⇒ $180;

Solução 3 (Ótima): 20Kg do item 2 e 30Kg do item 3 ⇒ $220.

Mayron César O. Moreira (UFLA) GCC111 � PAA 6 de maio de 2015 22 / 23

Outros exemplos

Código de Hu�man: compressão de dados, baseado na frequência comque aparecem;

Algoritmos de caminho mínimo em grafos;

Mayron César O. Moreira (UFLA) GCC111 � PAA 6 de maio de 2015 23 / 23