ebook img

Algoritmos y Estructuras de Datos PDF

306 Pages·2014·1.46 MB·Spanish
by  
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Algoritmos y Estructuras de Datos

Algoritmos y Estructuras de Datos [email protected] Bottazzi, Cristian. , [email protected] Costarelli, Santiago. , [email protected] D’Elía, Jorge. , [email protected] Dalcin, Lisandro. , [email protected] Galizzi, Diego. , [email protected] Olivera, José. , [email protected] Paz, Rodrigo. , [email protected] Prigioni, Juan. , [email protected] Pucheta, Martín. , [email protected] Rojas Fredini, Pablo Sebastián. , [email protected] Storti, Mario. , http://www.cimec.org.ar/aed www: Facultad de Ingeniería y Ciencias Hídricas http://fich.unl.edu.ar Universidad Nacional del Litoral Centro de Investigación de Métodos Computacionales http://www.cimec.org.ar (Document version: aed-2.0.6-13-ga4e5fe8) (Date: Mon Nov 10 19:54:58 2014 -0300) Indice 1. Diseñoyanálisisdealgoritmos 9 1.1. Conceptosbásicosdealgoritmos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 1.1.1. Ejemplo:Sincronizacióndeaccesoaobjetosencálculodistribuido . . . . . . . . . . . 10 1.1.2. Introducciónbásicaagrafos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 1.1.3. Planteodelproblemamediantegrafos . . . . . . . . . . . . . . . . . . . . . . . . . . 11 1.1.4. Algoritmodebúsquedaexhaustiva . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 1.1.5. Generacióndelascoloraciones . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 1.1.6. Crecimientodeltiempodeejecución . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 1.1.7. Búsquedaexhaustivamejorada . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 1.1.8. Algoritmoheurísticoávido . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 1.1.9. Descripcióndelalgoritmoheurísticoenseudo-código . . . . . . . . . . . . . . . . . . 21 1.1.10.Crecimientodeltiempodeejecuciónparaelalgoritmoávido . . . . . . . . . . . . . . . 26 1.1.11.Conclusióndelejemplo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 1.2. Tiposabstractosdedatos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 1.2.1. OperacionesabstractasycaracterísticasdelTADCONJUNTO . . . . . . . . . . . . . 28 1.2.2. InterfazdelTADCONJUNTO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 1.2.3. ImplementacióndelTADCONJUNTO . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 1.3. Tiempodeejecucióndeunprograma . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 1.3.1. Notaciónasintótica . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 1.3.2. Invarianciaanteconstantesmultiplicativas . . . . . . . . . . . . . . . . . . . . . . . . 33 1.3.3. Invarianciadelatasadecrecimientoantevaloresenunconjuntofinitodepuntos . . . 33 1.3.4. Transitividad . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 1.3.5. Regladelasuma. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 1.3.6. Regladelproducto . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 1.3.7. Funcionestípicasutilizadasenlanotaciónasintótica . . . . . . . . . . . . . . . . . . . 34 1.3.8. Equivalencia . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 1.3.9. Lafunciónfactorial . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 1.3.10.Determinaciónexperimentaldelatasadecrecimiento . . . . . . . . . . . . . . . . . . 37 1.3.11.Otrosrecursoscomputacionales. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 1.3.12.Tiemposdeejecuciónno-polinomiales . . . . . . . . . . . . . . . . . . . . . . . . . . 39 1.3.13.ProblemasPyNP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 1.3.14.Variosparámetrosenelproblema . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 1.4. Conteodeoperacionesparaelcálculodeltiempodeejecución . . . . . . . . . . . . . . . . . 40 1 INDICE INDICE 1.4.1. Bloquesif . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 1.4.2. Lazos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 1.4.3. Sumadepotencias . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 1.4.4. Llamadasarutinas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 1.4.5. Llamadasrecursivas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 2. Tiposdedatosabstractosfundamentales 48 2.1. ElTADLista . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 2.1.1. Descripciónmatemáticadelaslistas . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 2.1.2. Operacionesabstractassobrelistas . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 2.1.3. Unainterfazsimpleparalistas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 2.1.4. Funcionesqueretornanreferencias . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 2.1.5. Ejemplosdeusodelainterfazbásica . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 2.1.6. Implementacióndelistasporarreglos . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 2.1.6.1. Eficienciadelaimplementaciónporarreglos . . . . . . . . . . . . . . . . . . 63 2.1.7. Implementaciónmedianteceldasenlazadasporpunteros . . . . . . . . . . . . . . . . 64 2.1.7.1. Eltipoposición . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 2.1.7.2. Celdadeencabezamiento . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 begin() end() 2.1.7.3. Lasposiciones y . . . . . . . . . . . . . . . . . . . . . . . . 68 2.1.7.4. Detallesdeimplementación . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 2.1.8. Implementaciónmedianteceldasenlazadasporcursores . . . . . . . . . . . . . . . . 70 2.1.8.1. Cómoconvivenvariasceldasenunmismoespacio . . . . . . . . . . . . . . 72 2.1.8.2. Gestióndeceldas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 2.1.8.3. Analogíaentrepunterosycursores . . . . . . . . . . . . . . . . . . . . . . . 73 2.1.9. Tiemposdeejecucióndelosmétodosenlasdiferentesimplementaciones. . . . . . . . 76 2.1.10.InterfazSTL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 2.1.10.1.VentajasdelainterfazSTL. . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 2.1.10.2.Ejemplodeuso . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 2.1.10.2.1.Usodetemplatesyclasesanidadas . . . . . . . . . . . . . . . . . . 78 2.1.10.2.2.Operadoresdeincrementoprefijoypostfijo: . . . . . . . . . . . . . . 79 2.1.10.3.Detallesdeimplementación . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 2.1.10.4.Listasdoblementeenlazadas . . . . . . . . . . . . . . . . . . . . . . . . . . 82 2.2. ElTADpila . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 2.2.1. UnacalculadoraRPNconunapila . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 2.2.2. Operacionesabstractassobrepilas . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 2.2.3. Interfazparapila . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 2.2.4. ImplementacióndeunacalculadoraRPN . . . . . . . . . . . . . . . . . . . . . . . . . 85 2.2.5. Implementacióndepilasmediantelistas . . . . . . . . . . . . . . . . . . . . . . . . . 88 2.2.6. Lapilacomounadaptador . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 2.2.7. InterfazSTL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 2.3. ElTADcola . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 2.3.1. Intercalacióndevectoresordenados . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 2.3.1.1. Ordenamientoporinserción . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 2.3.1.2. Tiempodeejecución . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 ((versionaed-2.0.6-13-ga4e5fe8)(dateMonNov1019:54:582014-0300)(proc-dateMonNov1020:03:112014-0300)) 2 INDICE INDICE 2.3.1.3. Particularidadesalestarlassecuenciaspareseimparesordenadas . . . . . 93 2.3.1.4. Algoritmodeintercalaciónconunacolaauxiliar. . . . . . . . . . . . . . . . . 94 2.3.2. Operacionesabstractassobrecolas . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 2.3.3. Interfazparacola . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 2.3.4. Implementacióndelalgoritmodeintercalacióndevectores . . . . . . . . . . . . . . . . 96 2.3.4.1. Tiempodeejecución . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 2.4. ElTADcorrespondencia . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 2.4.1. Interfazsimpleparacorrespondencias . . . . . . . . . . . . . . . . . . . . . . . . . . 100 2.4.2. Implementacióndecorrespondenciasmediantecontenedoreslineales . . . . . . . . . 102 2.4.3. Implementaciónmediantecontenedoreslinealesordenados . . . . . . . . . . . . . . . 103 2.4.3.1. Implementaciónmediantelistasordenadas . . . . . . . . . . . . . . . . . . . 105 2.4.3.2. InterfazcompatibleconSTL . . . . . . . . . . . . . . . . . . . . . . . . . . . 106 2.4.3.3. Tiemposdeejecuciónparalistasordenadas . . . . . . . . . . . . . . . . . . 109 2.4.3.4. Implementaciónmediantevectoresordenados . . . . . . . . . . . . . . . . . 110 2.4.3.5. Tiemposdeejecuciónparavectoresordenados . . . . . . . . . . . . . . . . 112 2.4.4. Definicióndeunarelacióndeorden . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 3. Arboles 114 3.1. Nomenclaturabásicadeárboles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114 3.1.0.0.1. Alturadeunnodo. . . . . . . . . . . . . . . . . . . . . . . . . . . . 116 3.1.0.0.2. Profundidaddeunnodo.Nivel. . . . . . . . . . . . . . . . . . . . . . 116 3.1.0.0.3. Nodoshermanos . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116 3.2. Ordendelosnodos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116 3.2.1. Particionamientodelconjuntodenodos . . . . . . . . . . . . . . . . . . . . . . . . . . 117 3.2.2. Listadodelosnodosdeunárbol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 3.2.2.1. Ordenprevio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 3.2.2.2. Ordenposterior. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 3.2.2.3. Ordenposteriorylanotaciónpolacainvertida . . . . . . . . . . . . . . . . . 120 3.2.3. NotaciónLispparaárboles. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 3.2.4. Reconstruccióndelárbolapartirdesusórdenes . . . . . . . . . . . . . . . . . . . . . 122 3.3. Operacionesconárboles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124 3.3.1. Algoritmosparalistarnodos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124 3.3.2. Inserciónenárboles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 3.3.2.1. Algoritmoparacopiarárboles . . . . . . . . . . . . . . . . . . . . . . . . . . 126 3.3.3. Supresiónenárboles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128 3.3.4. Operacionesbásicassobreeltipoárbol . . . . . . . . . . . . . . . . . . . . . . . . . . 129 3.4. Interfazbásicaparaárboles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129 3.4.1. ListadosenordenprevioyposteriorynotaciónLisp . . . . . . . . . . . . . . . . . . . 132 3.4.2. Funcionesauxiliarespararecursiónysobrecargadefunciones . . . . . . . . . . . . . 133 3.4.3. Algoritmosdecopia . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133 3.4.4. Algoritmodepoda . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133 3.5. Implementacióndelainterfazbásicaporpunteros . . . . . . . . . . . . . . . . . . . . . . . . 134 3.5.1. Eltipoiterator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134 3.5.2. Lasclasescelleiterator_t . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136 ((versionaed-2.0.6-13-ga4e5fe8)(dateMonNov1019:54:582014-0300)(proc-dateMonNov1020:03:112014-0300)) 3 INDICE INDICE 3.5.3. Laclasetree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139 3.6. Interfazavanzada . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141 3.6.1. Ejemplodeusodelainterfazavanzada . . . . . . . . . . . . . . . . . . . . . . . . . . 145 3.7. Tiemposdeejecución . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148 3.8. Arbolesbinarios . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148 3.8.1. Listadosenordensimétrico . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149 3.8.2. NotaciónLisp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149 3.8.3. Árbolbinariolleno . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150 3.8.4. Operacionesbásicassobreárbolesbinarios . . . . . . . . . . . . . . . . . . . . . . . 150 3.8.5. Interfaceseimplementaciones. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151 3.8.5.1. Interfazbásica . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151 3.8.5.2. Ejemplodeuso.Predicadosdeigualdadyespejo . . . . . . . . . . . . . . . 151 3.8.5.3. Ejemplodeuso.Hacerespejo“inplace” . . . . . . . . . . . . . . . . . . . . 153 3.8.5.4. Implementaciónconceldasenlazadasporpunteros . . . . . . . . . . . . . . 154 3.8.5.5. Interfazavanzada . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159 3.8.5.6. Ejemplodeuso.Elalgoritmoapplyyprincipiosdeprogramaciónfuncional. . 160 3.8.5.7. Implementacióndelainterfazavanzada . . . . . . . . . . . . . . . . . . . . . 161 3.8.6. ArbolesdeHuffman . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165 3.8.6.1. Condicióndeprefijos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166 3.8.6.2. RepresentacióndecódigoscomoárbolesdeHuffman . . . . . . . . . . . . . 166 3.8.6.3. Códigosredundantes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167 3.8.6.4. Tabladecódigosóptima.Algoritmodebúsquedaexhaustiva . . . . . . . . . . 168 3.8.6.4.1. Generacióndelosárboles . . . . . . . . . . . . . . . . . . . . . . . 169 3.8.6.4.2. Agregandouncondimentodeprogramaciónfuncional . . . . . . . . 171 3.8.6.4.3. Elalgoritmodecombinación . . . . . . . . . . . . . . . . . . . . . . 173 3.8.6.4.4. Funciónauxiliarquecalculalalongitudmedia . . . . . . . . . . . . 174 3.8.6.4.5. Usodecombycodelen. . . . . . . . . . . . . . . . . . . . . . . . . 175 3.8.6.5. ElalgoritmodeHuffman . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176 3.8.6.6. Implementacióndelalgoritmo . . . . . . . . . . . . . . . . . . . . . . . . . . 178 3.8.6.7. Unprogramadecompresióndearchivos . . . . . . . . . . . . . . . . . . . . 181 4. Conjuntos 191 4.1. Introducciónalosconjuntos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191 4.1.1. Notacióndeconjuntos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191 4.1.2. Interfazbásicaparaconjuntos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192 4.1.3. Análisisdeflujodedatos. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193 4.2. Implementaciónporvectoresdebits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198 4.2.1. Conjuntosuniversalesquenosonrangoscontiguosdeenteros . . . . . . . . . . . . . 199 4.2.2. Descripcióndelcódigo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199 4.3. Implementaciónconlistas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201 4.3.0.1. SimilaridadentrelosTADconjuntoycorrespondencia . . . . . . . . . . . . . 201 4.3.0.2. Algoritmolinealparalasoperacionesbinarias . . . . . . . . . . . . . . . . . 202 4.3.0.3. Descripcióndelaimplementación . . . . . . . . . . . . . . . . . . . . . . . . 204 4.3.0.4. Tiemposdeejecución . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207 ((versionaed-2.0.6-13-ga4e5fe8)(dateMonNov1019:54:582014-0300)(proc-dateMonNov1020:03:112014-0300)) 4 INDICE INDICE 4.4. Interfazavanzadaparaconjuntos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207 4.5. Eldiccionario . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210 4.5.1. Laestructuratabladedispersión . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210 4.5.2. Tablasdedispersiónabiertas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211 4.5.2.1. Detallesdeimplementación . . . . . . . . . . . . . . . . . . . . . . . . . . . 211 4.5.2.2. Tiemposdeejecución . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214 4.5.3. Funcionesdedispersión . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215 4.5.4. Tablasdedispersióncerradas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216 4.5.4.1. Costodelainserciónexitosa . . . . . . . . . . . . . . . . . . . . . . . . . . 217 4.5.4.2. Costodelainserciónnoexitosa . . . . . . . . . . . . . . . . . . . . . . . . . 219 4.5.4.3. Costodelabúsqueda . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220 4.5.4.4. Supresióndeelementos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220 4.5.4.5. Costodelasfuncionescuandohaysupresión . . . . . . . . . . . . . . . . . 221 4.5.4.6. Reinsercióndelatabla . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221 4.5.4.7. Costodelasoperacionesconsupresión . . . . . . . . . . . . . . . . . . . . 222 4.5.4.8. Estrategiasderedispersión . . . . . . . . . . . . . . . . . . . . . . . . . . . 223 4.5.4.9. Detallesdeimplementación . . . . . . . . . . . . . . . . . . . . . . . . . . . 224 4.6. Conjuntosconárbolesbinariosdebúsqueda . . . . . . . . . . . . . . . . . . . . . . . . . . . 227 4.6.1. Representacióncomolistaordenadadelosvalores . . . . . . . . . . . . . . . . . . . 227 4.6.2. VerificarlacondicióndeABB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228 4.6.3. Mínimoymáximo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229 4.6.4. Buscarunelemento . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229 4.6.5. Costodemínimoymáximo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229 4.6.6. Operacióndeinserción . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232 4.6.7. Operacióndeborrado . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232 4.6.8. Recorridoenelárbol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234 4.6.9. Operacionesbinarias . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234 4.6.10.Detallesdeimplementación . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235 4.6.11.Tiemposdeejecución . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240 4.6.12.Balanceodelárbol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240 5. Ordenamiento 241 5.1. Introducción . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241 5.1.1. Relacionesdeordendébiles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241 5.1.2. Signaturadelasrelacionesdeorden.Predicadosbinarios. . . . . . . . . . . . . . . . 242 5.1.3. Relacionesdeordeninducidasporcomposición . . . . . . . . . . . . . . . . . . . . . 245 5.1.4. Estabilidad . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246 5.1.5. Primerasestimacionesdeeficiencia . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246 5.1.6. AlgoritmosdeordenamientoenlasSTL . . . . . . . . . . . . . . . . . . . . . . . . . . 246 5.2. Métodosdeordenamientolentos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247 5.2.1. Elmétododelaburbuja . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247 5.2.2. Elmétododeinserción . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248 5.2.3. Elmétododeselección . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249 5.2.4. Comparacióndelosmétodoslentos . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250 ((versionaed-2.0.6-13-ga4e5fe8)(dateMonNov1019:54:582014-0300)(proc-dateMonNov1020:03:112014-0300)) 5 INDICE INDICE 5.2.5. Estabilidad . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250 5.3. Ordenamientoindirecto . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251 5.3.1. Minimizarlallamadaafunciones . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253 5.4. Elmétododeordenamientorápido,quick-sort . . . . . . . . . . . . . . . . . . . . . . . . . . 253 5.4.1. Tiempodeejecución.Casosextremos . . . . . . . . . . . . . . . . . . . . . . . . . . 255 5.4.2. Eleccióndelpivote . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256 5.4.3. Tiempodeejecución.Casopromedio.. . . . . . . . . . . . . . . . . . . . . . . . . . . 258 5.4.4. Dispersióndelostiemposdeejecución . . . . . . . . . . . . . . . . . . . . . . . . . . 260 5.4.5. Elecciónaleatoriadelpivote . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261 5.4.6. Elalgoritmodepartición . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261 5.4.7. Tiempodeejecucióndelalgoritmodeparticionamiento . . . . . . . . . . . . . . . . . 262 5.4.8. Búsquedadelpivoteporlamediana . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263 5.4.9. Implementacióndequick-sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264 5.4.10.Estabilidad . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265 5.4.11.Elalgoritmodeintercambio(swap) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266 5.4.12.Tiempodeejecucióndelquick-sortestable . . . . . . . . . . . . . . . . . . . . . . . . 269 5.5. Ordenamientopormontículos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270 5.5.1. Elmontículo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271 5.5.2. Propiedades . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272 5.5.3. Inserción . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273 5.5.4. Costodelainserción . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274 5.5.5. Eliminarelmínimo.Re-heap. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274 5.5.6. Costodere-heap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276 5.5.7. Implementaciónin-place . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276 5.5.8. Elprocedimientomake-heap. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277 5.5.9. Implementación . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279 5.5.10.Propiedadesdelordenamientopormontículo . . . . . . . . . . . . . . . . . . . . . . . 280 5.6. Ordenamientoporfusión . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281 5.6.1. Implementación . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282 5.6.2. Estabilidad . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283 5.6.3. Versiónestabledesplit. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284 5.6.4. Merge-sortparavectores . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284 5.6.5. Ordenamientoexterno . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286 5.7. Comparacióndealgunasimplementacionesdealgoritmosdeordenamiento . . . . . . . . . . 287 6. GNUFreeDocumentationLicense 289 ((versionaed-2.0.6-13-ga4e5fe8)(dateMonNov1019:54:582014-0300)(proc-dateMonNov1020:03:112014-0300)) 6 Sobre este libro: EstelibrocorrespondealcursoAlgoritmosyEstructuradeDatosquesedictaenlacurrículadeIngeniería http://www. InformáticayAnalistaenInformáticaAplicadadelaFacultaddeIngenieríayCienciasHídricas( fich.unl.edu.ar http://www.unl.edu.ar )delaUniversidadNacionaldelLitoral ( ). http://www.cimec.org.ar/aed Página web del curso: La página web del curso es . En esa página fun- ciona un wiki, listas de correo y un repositorio de archivos donde se puede bajar la mayor parte del código quefiguraenellibro.EstelibrosepuedebajarenformatoPDFdeesapáginatambién. Licencia de uso: This book is Copyright (c) 2004-2014, Bottazzi, Cristian; Costarelli, Santiago; D’Elía, Jorge; Dalcin, Lisandro; Galizzi, Diego; Olivera, José; Paz, Rodrigo; Prigioni, Juan; Pucheta, Martín; Rojas Fredini,PabloSebastián;Storti,Mario;Permissionisgrantedto copy,distributeand/ormodifythisdocument under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; with no Invariant Sections, with no Front-Cover Texts, and with no Back-Cover Texts.Acopyofthelicenseisincludedbelowinthesectionentitled“GNUFreeDocumentationLicense”. Utilitarios usados: Todo este libro ha sido escrito con utilitarios de software libre, de acuerdo a los linea- http://www.gnu.org mientos de la Free Software Foundation/GNU Project ( ). La mayoría de los utilitarios Fedora release 20 (Heisenbug) Kernel 3.16.6-200.fc20.x86_64 correspondenaunsistema . pdflatex El libro ha sido escrito en LATEX y convertido a PDF con . El libro está completamente inter- hyperref referenciadousandolasutilidadespropiasdeLATEXyelpaquete . Muchos de los ejemplos con un matiz matemáticos han sido parcialmente implementados en Octave http://www.octave.org ( ).Tambiénmuchosdelosgráficos. C++ GCC 4.8.3 20140911 Los ejemplos en han sidos desarrollados y probados con el compilador (Red Hat 4.8.3-7) (GCC)) http://gcc.gnu.org GNU Make 3.82http://www. ( )yconlaayudade gnu.org/software/make/make.html . http://inkscape. Las figuras han sido generadas con Inkscape 0.48.5 r10040 (Jul 22 2014) ( sourceforge.net/http://inkscape.sourceforge.net/ http://www.xfig. ) y Xfig 3.2.4-21.1 ( org/ ). http://git-scm.com/ EllibrohasidoescritoenformacolaborativaporlosautoresusandoGit1.9.3( ). 7 INDICE INDICE Errores Alfinaldellibrohayuncapítulodedicadoareportarloserroresquesevandetectandoycorrigiendo. Este capítulo se irá publicando en la página web por separado, de manera que si Ud. posee una versión anteriordellibropuedebajarlaserratasycorregirsuversión. Si encuentra algún error en el libro le agradecemos reportarlo a cualquiera de los autores, indicando la versióndellibro,talcomoapareceenlaportada. ((versionaed-2.0.6-13-ga4e5fe8)(dateMonNov1019:54:582014-0300)(proc-dateMonNov1020:03:112014-0300)) 8 Capítulo 1 Diseño y análisis de algoritmos 1.1. Conceptos básicos de algoritmos Noexisteunareglaprecisaparaescribirunprogramaqueresuelvaundadoproblemapráctico.Almenos porahoraescribirprogramasesengranmedidaunarte.Sinembargoconeltiemposehandesarrolladoun variedad de conceptos que ayudan a desarrollar estrategias para resolver problemas y comparar a priori la eficienciadelasmismas. Por ejemplo supongamos que queremos resolver el “Problema del Agente Viajero” (TSP, por “Traveling Salesman Problem”) el cual consiste en encontrar el orden en que se debe recorrer un cierto número de ciudades (esto es, una serie de puntos en el plano) en forma de tener un recorrido mínimo. Este problema surge en una variedad de aplicaciones prácticas, por ejemplo encontrar caminos mínimos para recorridos de distribución de productos o resolver el problema de “la vuelta del caballo en el tablero de ajedrez”, es decir, encontrar un camino para el caballo que recorra toda las casillas del tablero pasando una sola vez porcadacasilla.Existeunaestrategia(trivial)queconsisteenevaluartodosloscaminosposibles.Peroesta estrategiade“búsquedaexhaustiva” tieneungrandefecto,elcostocomputacionalcrecedetalmaneraconel númerodeciudadesquedejadeseraplicableapartirdeunacantidadrelativamentepequeña.Otraestrategia “heurística” sebasaenbuscaruncaminoque,sibiennoeselóptimo(eldemenorrecorridosobretodoslos posibles)puedeserrelativamentebuenoenlamayoríadeloscasosprácticos.Porejemplo,empezarenuna ciudadeiralamáscercanaquenohayasidoaúnvisitadahastarecorrerlastodas. Unaformaabstractadeplantearunaestrategiaesenlaformadeun“algoritmo”,esdecirunasecuencia deinstruccionescadaunadelascualesrepresentaunatareabiendefinidaypuedeserllevadaacaboenuna cantidadfinitadetiempoyconunnúmerofinitoderecursoscomputacionales.Unrequerimientofundamental es que el algoritmo debe terminar en un número finito de pasos, de esta manera él mismo puede ser usado comounainstrucciónenotroalgoritmomáscomplejo. Entonces, comparando diferentes algoritmos para el TSP entre sí, podemos plantear las siguientes pre- guntas ¿Daelalgoritmolasoluciónóptima? Sielalgoritmoesiterativo,¿converge? ¿Comocreceelesfuerzocomputacionalamedidaqueelnúmerodeciudadescrece? 9

Description:
Este libro corresponde al curso Algoritmos y Estructura de Datos que se dicta en .. mos bajar el tiempo de cálculo al orden de los millones de años.
See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.