Вы находитесь на странице: 1из 259

13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.

Cuandosehacereferenciaaprogramaslinealesduales,

Pgina1

880 Captulo29Programacinlineal

Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandose
anilloparaprogramaslinealesduales,quellamamoselprogramalinealoriginaldelaprimitiva.
Dadounprogramalinealprimarioenformaestndar,comoen(29.16)(29.18),definimos
Elprogramalinealdualcomo
xmetro
minimizar biyi (29,83)
ID1
sujetoa
xmetro
aijSI cj ParajD$$2:::N (29,84)
ID1
yi 0paraiD$$2:::M (29,85)
Paraformareldual,cambiamoslamaximizacinaunaminimizacin,cambiamosla
Funcionesdeloscoeficientesenlosladosderechosylafuncinobjetivo,y
Cadaunomenosoigualporunmayorqueoiguala.Cadaunadelasmrestricciones
enelprimaltieneunavariableyasociadoieneldoble,ycadaunadelaslimitacionesden
eneldobletieneunasociadovariableXjenelprimario.Porejemplo,considerela
Linealdadoen(29.53)(29.57).Eldobledeesteprogramalineales
minimizar30y1 C24y2 C36y3 (29,86)
sujetoa
y1
do 2y2 do 4y3 3 (29,87)
y1
do 2y2 do y3 1 (29,88)
3y1 do 5y2 do 2y3 2 (29,89)
y1y2y3 0: (29.90)
Vamosamostrarenelteorema29.10queelvalorptimodeladuallinealpro
Gramoessiempreigualalvalorptimodelprogramalinealprimario.Promover
Ms,elalgoritmosimplexrealmenteimplcitamenteresuelvetantoelprimalcomoeldual
Linealessimultneamente,proporcionandoasunapruebadeoptimalidad.
Comenzamosdemostrandoladualidaddbil,loqueindicaquecualquiersolucinviable
Cinalprogramalinealprimariotieneunvalornomayorqueeldecualquier
Solucinalprogramalineardual.

Lema29.8(Dualidaddeprogramacinlinealdbil)
SeaNxunasolucinfactiblealprogramalinealprimarioen(29.16)(29.18)y
SeaNycualquiersolucinfactiblealprogramalinealdualen(29.83)(29.85).Entonces,
tenemos
xnorte xmetro
cjjaNx biNyi:
JD1 ID1

Pgina2

29.4Dualidad 881

Tenemoslaprueba
!
xnorte x nortexmetro
cNxjj UN aijiNy nxj (Pordesigualdades(29.84))
JD1 JD1 ID1 !
xmetroxnorte
re aijjNx iny

https://translate.googleusercontent.com/translate_f 1/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
xID1
metroJD1
UN biiNy (Pordesigualdades(29.17)).
ID1

Corolario29,9
SeaNxunasolucinfactibleaunprogramalinealprimario.AsegundoC/,yseaNyun
Solucinviablealcorrespondienteprogramalinealdual.Si
xnorte xmetro
cjjDNx biNyi
JD1 ID1
EntoncesNxyNysonsolucionesptimasparalosprogramaslinealesprimarioydual,respectivamente
Tamente.

Porellemapruebade29,8,elvalorobjetivodeunasolucinviablealaprimitiva
Nopuedeexcedereldeunasolucinfactiblealdoble.Elprogramalinealprimales
Unproblemademaximizacinyeldualesunproblemademinimizacin.As,siesfactible
LassolucionesNxyNytienenelmismovalorobjetivo,nisepuedenmejorar.

Antesdeprobarquesiemprehayunasolucindualcuyovaloresiguala
Deunasolucinprimalptima,describimoscmoencontrartalsolucin.Cuando
Corrimoselalgoritmosimplexenelprogramalinealen(29.53)(29.57),elfinal
iteracindiolaformadeholgura(29.72)(29.75)conelobjetivode"D28x3=6
x5=62x6=3,f1BD24g,yf3ND56g.Comoveremosacontinuacin,la
Solucinasociadaconlaformadeholgurafinalesdehechounasolucinptima
ProgramalinealUnasolucinptimaalprogramalineal(29.53)(29.57)esporlotanto
.NX1Nx2Nx3/D0,840/,convalorobjetivo.38/C.14/C.20/D28.Como
Tambinmostramosacontinuacin,podemosleerunasolucindualptima:losnegativosdela
Coeficientesdelafuncinobjetivoprimariosonlosvaloresdelasvariablesduales.
Msprecisamente,supongamosquelaltimaformaholgadadelprimales
x
D 0C cj0xj
J2n
x
xi Db0 yo un
ij0xj Parai_{2}B:
J2n

Pgina3

882 Captulo29Programacinlineal

Entonces,paraproducirunasolucindualptima,
(
cNCi
0 Si.nC$$/2N
NyiD (29,91)
0 Delocontrario:

Porlotanto,unasolucinptimaparaelprogramalinealdualdefinidoen(29.86)(29.90)
esNy1D0(puestonCD142B),Ny2Dc0 5D1=6,yNy3Dc0 6D2=3.
Alevaluarlafuncinobjetivodual(29.86),seobtieneunvalorobjetivode
.300/C.24.1=6//C.36.2=3//D28,loqueconfirmaqueelvalorobjetivo
Delprimarioesciertamenteigualalvalorobjetivodeldual.Combinandoestos
ClculosconelLema29.8daunapruebadequeelvalorobjetivoptimodela
Primallinealprogramaes28.Ahorademostrarqueesteenfoqueseaplicaengeneral:
Podemosencontrarunasolucinptimaaldualydemostrarsimultneamentequeunasolucin
Alptimoesptimo.

Teorema29.10(Dualidaddeprogramacinlineal)
SupongamosqueSIMPLEXdevuelvevaloresNxD.NX1Nx2:::Nxn/linparaelprimal
Odoprograma.Asegundodo/.SeaNyBlasvariablesnobsicasybsicaspara
laformadeholgurafinal,seaC0denotanloscoeficientesenformaholgurafinal,ydejar
NyD.Ny1Ny2:::Nym/serdefinidaporlaecuacin(29.91).EntoncesNxesunptimo,
Cinalprogramalinealprimario,Nyesunasolucinptimaala
Programa,y
xnorte xmetro
cjjDNx biNyi: (29,92)
JD1 ID1

PruebaPorCorolario29.9,sipodemosencontrarsolucionesfactiblesNxyNyquesatisfagan
(29.92),entoncesNxyNydebensersolucionesprimitivasydualesptimas.Nosotros
https://translate.googleusercontent.com/translate_f 2/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
DemostrarahoraquelassolucionesNxyNydescritasenelenunciadodelteorema
Satisfacerlaecuacin(29.92).
SupongamosquecorremosSIMPLEXenunprogramalinealprimario,comoseindicaenlneas
(29,16)(29,18).Elalgoritmoprocedeatravsdeunaseriedeformasflojashastaque
Terminaconunaformadeholgurafinalconfuncinobjetivo
x
'D0C c0 (29,93)
jxj:
J2n
DesdeSIMPLEXterminaconunasolucin,porlacondicinenlalnea3,sabemosque
cj0a0paratodoj2N: (29,94)

Pgina4

29.4Dualidad 883

Sidefinimos
c0 (29,95)
jD0paratodoj2B
Podemosreescribirlaecuacin(29.93)como
x
'D0C cj0xj
J2n
x x
D0C c0 c0
jjxC jxj(porquec0 jD0sij2B)
J2n J2b
x
Ncm
D0C cj0xj (DebidoaqueN[BDf12:::Cmg).(29,96)
JD1
ParalasolucinbsicaNxasociadaconestaformadeholgurafinal,NxjD0paratodoj2N,
y'D0.Dadoquetodaslasformasholgadassonequivalentes,sievaluamoselobje
EnNx,debemosobtenerelmismovalorobjetivo:
xnorte x
Ncm
cNxjj re 0C cjj
0 (29,97)
Nx
JD1 JD1
x x
re 0C cjj
0 cjj
0
CNx Nx
J2n
x J2b
x
re 0C .cJ0 0/C 0.0Nxj/ (29,98)
J2n J2b
re 0:

VamosademostrarahoraqueNy,definidoporlaecuacin(29.91),esfactibleparaeldual
Pm Pn
Programalinealyquesuvalorobjetivo
ID1biNyiesigual jd1cNxjj. Equa
(29.97)dicequelaprimeraylaltimaformadeholgura,evaluadasenNx,soniguales.Ms
engeneral,laequivalenciadetodaslasformasdeholguraimplicaqueparacualquierconjuntodevalores
xD.x1x2:::xn/,tenemos
xnorte x
Ncm
cjjxD0C c0
jxj:
JD1 JD1
Porlotanto,paracualquierconjuntoparticulardevaloresNxD.NX1Nx2:::Nxn/,tenemos

Pgina5

884 Captulo29Programacinlineal

https://translate.googleusercontent.com/translate_f 3/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
xnorte
cNxjj
JD1
x
Ncm
re 0C c0
jjNx
JD1
x norte x
Ncm
re 0C cjj
0 cjj
0
CNx Nx
JD1 JDnC1
x norte xmetro
re 0C cjj
0 cnCi
0
CNx NxnCi
JD1 ID1
x norte xmetro
re 0C c0 .Nyi/NxnCi (Porlasecuaciones(29.91)y(29.95))
jjCNx
JD1 ID1 !
x norte xmetro x norte
re 0C cjj
0 .Nyi/ib aijjNx (Porlaecuacin(29.32))
CNx
JD1 ID1 JD1
x norte xmetro xmetro
xnorte
re 0C cjj
0 biiNyC .aijNxj/Nyi
Nx
JD1 ID1 ID1JD1
x norte xmetro x norte
xmetro
re 0C cjj
0 biiNyC .aijNyi/jNx
Nx
JD1 ! ID1 JD1 ID1 !
xmetro xnorte xmetro
re 0 biiNydo cj0C aijiNy Nxj
ID1 JD1 ID1
asqueeso
! !
xnorte xmetro xnorte xmetro
cjjDNx 0 biiNydo c0 aijiNy Nxj: (29,99)
jC
JD1 ID1 JD1 ID1
AplicandoelLema29.3alaecuacin(29.99),obtenemos
xmetro
0 biiNyD$$ (29.100)
ID1
xmetro
cj0C aijiNy Dcj ParajD$$2:::N (29.101)
ID1 Pm
Porlaecuacin(29.100),tenemosque
UN
Pm ID1biNyiD0,yporlotantoelvalorobjetivo
Deldoble esigualaladelaprimitiva
ID1biiNy (0).Quedapormostrar

Pgina6

29.4Dualidad 885

QuelasolucinNyesfactibleparaelproblemadual.Delasdesigualdades(29.94)y
lasecuaciones(29.95),quetienenquec0
ja0paratodojD12:::NMm. Porlotanto,paracualquier
JD$$2:::N,lasecuaciones(29.101)implicanque
xmetro
cj Dc0 jC aijiNy
ID1
xmetro
UN aijNyi
ID1
Quesatisfacelasrestricciones(29.84)deldual.Porltimo,yaquecj0 a0paracada
j2N[B,cuandofijamosNyacuerdoconlaecuacin(29.91),tenemosquecadaNy 0, i
Yporlotantolasrestriccionesdenonegatividadtambinsesatisfacen.

Hemosdemostradoque,dadounprogramalinealfactible,siNITIALIZESIMPLEX
devuelveunasolucinfactible,ysiSIMPLEXterminasindevolver"in
Limitada",entonceslasolucindevueltaesdehechounasolucinptima.Tambintenemos
Semuestracmoconstruirunasolucinptimaparaelprogramalinealdual.

Ceremonias

29.41

https://translate.googleusercontent.com/translate_f 4/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
Formuleeldualdelprogramalinealdadoenelejercicio29.35.
29,42
Supongamosquetenemosunprogramalinealquenoestenformaestndar.Podramos
Producireldualconvirtindoloprimeroalaformaestndar,yluegotomareldual.
Seramsconveniente,sinembargo,sercapazdeproducireldualdirectamente.
Expliquecmopodemostomardirectamenteeldobledeunprogramalinealarbitrario.

29,43
Anoteeldualdelprogramalinealdeflujomximo,talcomosedaenlneas
(29.47)(29.50)enlapgina860.Expliquecmointerpretarestaformulacincomoun
Problemadecortemnimo.

29,44
Anoteeldualdelprogramalinealdeflujomnimodecoste,talcomosedaenlneas
(29.51)(29.52)enlapgina862.Expliquecmointerpretaresteproblemaentrminosde
Grficosyflujos.

29,45
Demuestrequeeldualdeldualdeunprogramalinealeselprogramalinealprimal.

Pgina7

886 Captulo29Programacinlineal

29,46
QuresultadodelCaptulo26puedeinterpretarsecomodualidaddbilparaelmximo
Problemadeflujo

29.5Lasolucinbsicainicialfactible

Enestaseccin,describiremosprimerocmoprobarsiunprogramalinealesfactible,
Ysiesas,cmoproducirunaformaflojaparalacuallasolucinbsicaesfactible.
Concluimosprobandoelteoremafundamentaldelaprogramacinlineal,que
dicequeelprocedimientoSIMPLEXsiempreproduceelresultadocorrecto.

Encontrarunasolucininicial
EnlaSeccin29.3,hemosasumidoquetuvimosunprocedimientoqueNITIALIZESIMPLEXque
Determinasiunprogramalinealtienealgunasolucinfactible,ysilohace,
Unaformaflojaparalacuallasolucinbsicaesfactible.Describimosesteprocedimiento
aqu.
Unprogramalinealpuedeserfactible,perolasolucinbsicainicialpodranoser
factible.Consideremos,porejemplo,elsiguienteprogramalineal:
maximizar2x1 x2 (29.102)
sujetoa
2x1 x2 UN 2 (29.103)
x1 5x2 4 (29.104)
x1x2 0: (29.105)

Sifuramosaconvertiresteprogramalinealenformaholgada,lasolucinbsicasera
conjuntox1D0yDx20.Estasolucinviolalarestriccin(29.104),yporlotantonoesuna
solucinfactible.Porlotanto,yoNITIALIZESIMPLEXnopuedesimplementedevolverlaholguraobvia
formar.Conelfindedeterminarsiunprogramalinealtienealgunasolucinfactible,
vamosaformularunprogramalinealauxiliar.Paraesteprogramalinealauxiliar,
Podemosencontrar(conunpocodetrabajo)unaformaholgadaparalacuallasolucinbsicaesfactible.
Adems,lasolucindeesteprogramalinealauxiliardeterminasila
Programalinealinicialesfactibleysiesas,proporcionaunasolucinfactibleconlaque
podemosinicializarSIMPLEX.

Lema29.11
SeaLunprogramalinealenformaestndar,dadocomoen(29.16)(29.18).Seax0sea
unanuevavariable,yseaLauxserelsiguienteprogramalinealconnC1Variables:

https://translate.googleusercontent.com/translate_f 5/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,

Pgina8

29.5Lasolucinbsicainicialfactible 887

maximizar x0 (29.106)
sujetoa
xnorte
aijxj x0 ABi ParaiD12:::M (29.107)
JD1
xj 0paraj$_{D0}$1:::N (29.108)

EntoncesLesfactiblesiyslosielvalorobjetivoptimodeLauxes0.

PruebaSupongamosqueLtieneunasolucinfactibleNxD.NX1Nx2:::Nxn/.Entoncesel
solucinNx00DcombinadoconNXesunasolucinfactibleaLauxconelobjetivo
valor0.Comox0 0esunalimitacindelaLauxylafuncinobjetivoes
maximizarx0,estasolucindebeserptimaparaLaux.
Porelcontrario,supongamosqueelvalorobjetivoptimodeLauxes0.EntoncesNx0D0,
YlosvaloresdesolucinrestantesdeNxsatisfacenlasrestriccionesdeL.

Ahoradescribimosnuestraestrategiaparaencontrarunasolucinfactiblebsicainicialparauna
ProgramaLenformaestndar:

MeNITIALIZESIMPLEX.Asegundodo/
1Seakelndicedelamnimabi
2sibk 0 //Esfactiblelasolucinbsicainicial?
3 volver.f12:::NgfnC$$NC$$C$_$mgUNsegundodo0
4formaLauxmediantelaadicindex0enelladoizquierdodecadarestriccin
yelestablecimientodelafuncinobjetivoa0x
5dejeque.NSEGUNDOUNsegundodo/SerlaformadecrecimientoresultantedeLaux
6lDnCk
7//LauxtienenC1mvariablesnobsicasyvariablesbsicas.
8NSEGUNDOUNsegundodo/DPIvot.NSEGUNDOUNsegundodoL0
9//LasolucinbsicaesahorafactibleparaLaux.
10iterarelbuclewhiledelneas312deSIMPLEXhastaunasolucinptima
aLseencuentraaux
11silasolucinptimaparaLauxestableceNx00
12 SiNx0esbsico
13 Realizarunpivote(degenerado)parahacerlonobsico
14 desdelaformadeholgurafinaldeLaux,retirex0ydelaslimitaciones
RestaurarlafuncinobjetivooriginaldeL,peroreemplazarcadafuncinbsica
Variableenestafuncinobjetivoporelladoderechodesu
Restriccinasociada
15 devolverelformularioholguradefinitivamodificada
16oregrese"inviable"

Pgina9

888 Captulo29Programacinlineal

INITIALIZESIMPLEXfuncionacomosigue.Enlaslneas13,probamosimplcitamenteel
SolucinbsicaalaformadeholgurainicialparaLdadaporNDf12:::Ng,BD
F_{n}C_{1}nC2:::nCmg,NxiDBiparatodoi2B,yD0Nxjparatodoj2N.
(Crearlaformaholgadanorequiereningnesfuerzoexplcito,yaquelosvaloresdeA,bycson
Lamismaenambasformasholgadayestndar.)Silalnea2encuentraestasolucinbsicaparaser
viable,esdecir,iNx 0paratodosi2N[Bentonceslalnea3devuelvelaformafloja.
Delocontrario,enlalnea4,formamoselprogramaauxiliarlinealLAUXcomoenelLema29.11.
DadoquelasolucinbsicainicialdeLnoesfactible,lasolucinbsicainicialdel
formulariodeholguraparaLauxnopuedeserfactible,yasea.Paraencontrarunasolucinfactiblebsica,
Realizarunaoperacindepivotenico.Lalnea6seleccionalDnCkcomoelndicedela
Variablebsicaqueserlavariabledesalidaenlaprximaoperacindepivote.
DadoquelasvariablesbsicassonnC1xxNC2:::xNCM,dejandolavariablexlser
Elquetieneelvalormsnegativo.Lalnea8querealizalallamadadePIvot,con
x0xlentradaysalida.Veremosenseguidaquelasolucin
delapresenteconvocatoriadePIvotserfactible.Ahoraquetenemosunaformaflojaparalacual
https://translate.googleusercontent.com/translate_f 6/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
lasolucinbsicaesfactible,podemos,enlalnea10,enrepetidasocasionesllamaraPIvottotalmente
Resolverelprogramalinealauxiliar.Comodemuestralapruebadelalnea11,siencontramos
unasolucinptimaaLauxconelvalorobjetivo0,acontinuacin,enlaslneas1214,creamos
UnaformaholgadaparaLparalacuallasolucinbsicaesfactible.Paraello,primero,
enlaslneas1213,manejarelcasodegeneradoenlaquex0puedeseguirsiendobsicocon
Nxvalor0D0.Enestecaso,realizamosunpasodepivoteparaeliminarx0delabase,
utilizandocualquiere2Ndetalmaneraqueun0e0comolavariabledeentrada.Lanuevabase
SolucinsiguesiendofactibleElpivotedegeneradonocambiaelvalordeninguna
variable.Acontinuacinborramostodox0trminosdelaslimitacionesyrestaurareloriginal
FuncinobjetivoparaL.Lafuncinobjetivooriginalpuedecontenertanto
Yvariablesnobsicas.Porlotanto,enlafuncinobjetivoreemplazamoscada
Variableporelladoderechodesurestriccinasociada.Lalnea15luegoregresa
Estaformaflojamodificada.Si,porelcontrario,lalnea11descubrequeel
LinealLesinviable,entonceslalnea16devuelveestainformacin.
AhorademuestranelfuncionamientodelaINITIALIZESIMPLEXenelprolineal
Gramo(29.102)(29.105).Esteprogramalinealesfactiblesipodemosencontrarno
Ativevaloresparax1yx2quesatisfacenlasdesigualdades(29.103)y(29.104).Utilizando
Lema29.11,seformulaelprogramalinealauxiliar
maximizar x0 (29.109)
sujetoa
2x1 x2 x0 UN 2 (29.110)
x1 5x2 x0 4 (29.111)
x1x2x0 0:
PorelLema29.11,sielvalorobjetivoptimodeesteprogramalinealauxiliar
Es0,entonceselprogramalinealoriginaltieneunasolucinfactible.Sielobjetivoptimo

Pgina10

29.5Lasolucinbsicainicialfactible 889

Valordeesteprogramalinealauxiliaresnegativo,entonceselprogramalinealoriginal
Notieneunasolucinfactible.
Escribimosesteprogramalinealenformaholgada,obteniendo
D x0
x3 re 2 2x1 do x2 Cx0
x4 D4 x1 C5x2 Cx0:
Noestamosfueradelbosquetodava,porquelasolucinbsica,queestablecera
4x4D,noesfactibleparaesteprogramalinealauxiliar.Sinembargo,podemos

unallamadaaPIvot,convertirestaformaflojaenunaenlaquelasolucinbsicaes
factible.Comoindicalalnea8,elegimosx0aserlavariabledeentrada.Enlalnea6,
elegircomovariabledesalidax4,queeslavariablebsicacuyovalorenel
Lasolucinbsicaesmsnegativa.Despusdepivotar,tenemoslaformafloja
D4 x1 C5x2 x4
x0 re 4Cx1 5x2 Cx4
x3 re 6 x1 4x2 Cx4:
Lasolucinbsicaasociadaes.NX0Nx1Nx2Nx3Nx4/D0,40060/,queesfactible
BleAhorallamamosrepetidamentePIvothastaobtenerunasolucinptimaaLaux.En
estecaso,unallamadaaPIvotconx2x0entradaysalidarendimientos
D x0
4 x0 x1 x4
x2 re do do
5 5 5 5
14 4x0 9x1 x4
x3 re do do :
5 5 5 5
Estaformaholgadaeslasolucinfinalalproblemaauxiliar.Dadoqueestasolucin
xtiene0D0,sabemosquenuestroproblemainicialerafactible.Adems,dadoque
x0D0,podemossimplementeeliminarlodelaseriederestricciones.Luegorestauramosel
Funcinobjetivooriginal,conlassustitucionesapropiadashechasparaincluirsolamente
Variablesnobsicas.Ennuestroejemplo,obtenemoslafuncinobjetivo
UN UN
4 x0 x1 x4
2x1 x2D12x do do :
5 5 5 5
Marcox00Dysimplificando,obtenemoslafuncinobjetivo
https://translate.googleusercontent.com/translate_f 7/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,

4 9x1 x4
do
5 5 5
Ylaformaholgada

Pgina11

890 Captulo29Programacinlineal

4 9x1 x4
D do
5 5 5
4 x1 x4
x2 re do do
5 5 5
14 9x1 x4
x3 re do :
5 5 5
Estaformaholgadatieneunasolucinbsicafactible,ypodemosdevolverlaalprocedimiento
SIMPLEX.
VamosademostrarformalmentelaexactituddeloNITIALIZESIMPLEX.

Lema29.12
SiunprogramalinealLnotienesolucinfactible,entoncesmeNITIALIZEvuelveSIMPLEX
"Inviable".Delocontrario,devuelveunaformadeholguravlidaparalacuallasolucinbsica
Esfactible.

EnprimerlugarlapruebasuponerqueelprogramalinealLnotienesolucinfactible.Entoncespor
Lema29.11,elvalorptimoobjetivodeLaux,definidaen(29.106)(29.108),
esdistintodecero,yporlarestriccindenonegatividadenx0,elvalorobjetivoptimo
Debesernegativo.Adems,estevalorobjetivodebeserfinito,yaque
xiD0,paraiD12:::nyx0DJminm ID1fbiGJesfactible,yestasolucin
tienevalorobjetivoJminm ID1fbiGJ. Porlotanto,lalnea10delaINITIALIZESIMPLEX
Encuentraunasolucinconunvalorobjetivonopositivo.SeaNxlasolucinbsica
Asociadoconlaformadeholgurafinal.NopodemostenerNx0D0,porqueentoncesLaux
Tendraunvalorobjetivo0,loquecontradicequeelvalorobjetivoesnegativo.
Portanto,lapruebadelalnea11dacomoresultadoquelalnea16vuelvaaser"inviable".
SupongamosahoraqueelprogramalinealLtieneunasolucinfactible.De
Ejercicio29,34,sabemosquesibi 0paraiD$$2:::M,entonceslasolucinbsica
Asociadoconlaformaholgadainicialesfactible.Enestecaso,laslneas23devuelvenel
Formaholgadaasociadaconlaentrada.(Conversindelaformaestndaralaformafloja
Esfcil,yaqueA,b,ycsonlosmismosenambos).
Enelrestodelaprueba,manejamoselcasoenelqueelprogramalineales
Peronoregresamosenlalnea3.Argumentamosqueenestecaso,laslneas410encuentranuna
solucinfactibleaLauxconelvalorobjetivo0.Enprimerlugar,porlaslneas12,debemostener
bk<0
y
bKABI Paracadai2B: (29.112)
Enlalnea8,llevamosacabounaoperacindepivoteenelquelavariabledesalidaxl(recurdese
quelDnCk,demodoquebl<0)eselladoizquierdodelaecuacinconmini
bmami,ylavariabledeentradaesx0,lavariableextraaadido.Ahoramostramos

Pgina12

29.5Lasolucinbsicainicialfactible 891

Quedespusdeestepivote,todaslasentradasdebsonnonegativas,yporlotantolasolucinbsica
aLauxesfactible.DejandoNxsealasolucinbsicadespusdelallamadaaPIvot,y
Dejando Y
byYBservaloresdevueltosporP Ivot,Lema29.1implicaque
(
Yuna
bi esdecir,b Sii2yBfeg
correo
NxiD (29.113)
b=lale Siyodi

LallamadaaPIvotenlalnea8tieneeD0.Sivolvemosaescribirlasdesigualdades(29.107),a

https://translate.googleusercontent.com/translate_f 8/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
incluircoeficientesai0,
xnorte
aijxjaib ParaiD12:::M (29.114)
JD0
entonces
uni0DaDesdecir,1decada2iB: (29.115)
(Tengaencuentaqueunai0eselcoeficientedex0talcomoapareceenlasdesigualdades(29.114),no
lanegacindelcoeficiente,porqueauxLesestndarenlugardeenformafloja.)
Desdel2B,tambintenemosqueleD1.Porlotanto,bl=ale> 0,yasNxe> 0.Por
Lasvariablesbsicasrestantes,tenemos
inx DBI Yuna
b
esdecir, correo (Porlaecuacin(29.113))
DBI un.bl=unfichero/(porlalnea3dePIvot)
esdecir,

DBI bl (porlaecuacin(29.115)yunficheroD1)
0 (Pordesigualdad(29.112)),
Loqueimplicaquecadavariablebsicaesahoranonegativa.Porlotanto,lasolucinbsica
cindespusdelallamadaaPIvotenlalnea8esfactible.Seguidamenteejecutamoslalnea10,que
resuelveLaux.DadoquehemosasumidoqueLtieneunasolucinfactible,elLema29.11
implicaqueLauxtieneunasolucinptimaconelvalorobjetivo0.Dadoquetodalaholgura
formassonequivalentes,lasolucinbsicafinalaLauxdebetenerNx0D0,ydespusde
laeliminacindex0delprogramalineal,seobtieneunaformadeholguraqueesfactibleparaL.
Lalnea15devuelveesteformulariodeholgura.

Teoremafundamentaldelaprogramacinlineal
Concluimosestecaptulo,demostrandoqueelprocedimientoSIMPLEXfunciona.Enpar
Cualquierprogramalinealnoesfactible,notienelmitesotieneunvalorptimo
Solucinconunvalorobjetivofinito.Encadacaso,SIMPLEXactaapropiadamente.

Pgina13

892 Captulo29Programacinlineal

Teorema29.13(Teoremafundamentaldelaprogramacinlineal)
CualquierprogramalinealL,dadoenformaestndar,
1.tieneunasolucinptimaconunvalorobjetivofinito,
2.esimposible,o
3.notienelmites.
SiLnoesfactible,SIMPLEXdevuelve"inviable".SiLesilimitada,SIMPLEX
devuelve"sinlmites".Delocontrario,devuelveSIMPLEXunasolucinptimaconunfinita
Objetivo.

PruebaPorelLema29.12,sielprogramalinealLnoesfactible,entoncesSvuelveIMPLEX
"Imposible".SupongamosahoraqueelprogramalinealLesfactible.PorelLema29.12,
INITIALIZESIMPLEXdevuelveunaformaholguraparaquelasolucinbsicaesfactible.
PorelLema29.7,porlotanto,SIMPLEXobienvuelve"sinlmites"otermine
Conunasolucinfactible.Siterminaconunasolucinfinita,entonceselteorema29.10
Nosdicequeestasolucinesptima.Porotrolado,siSIMPLEXdevuelve"UN
Limitada",elLema29.2nosdicequeelprogramalinealLesrealmenteilimitado.Yaque
SIMPLEXsiempreterminaenunadeestasformas,lapruebaestcompleta.

Ceremonias

29.51
Darpseudocdigodetalladoparaaplicarlaslneas5y14delaINITIALIZESIMPLEX.

29,52
DemostrarquecuandoelbucleprincipaldeSIMPLEXestdirigidoporloNITIALIZESIMPLEX,loquepuede
Nuncavuelva"ilimitado".

29,53

https://translate.googleusercontent.com/translate_f 9/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
SupongamosquesenosdaunprogramalinealLenformaestndar,ysupongamosque
TantoparaLcomoparaeldualdeL,lassolucionesbsicasasociadasconlaholgurainicial
Formassonfactibles.DemuestrequeelvalorobjetivoptimodeLes0.

29,54
Supongamosquepermitimosdesigualdadesestrictasenunprogramalineal.Mostrarqueeneste
,Elteoremafundamentaldelaprogramacinlinealnosecumple.

Pgina14

29.5Lasolucinbsicainicialfactible 893

29,55
ResolverelsiguienteprogramalinealutilizandoSIMPLEX:
maximizar x1 C3x2
sujetoa
x1 x2 UN 8
x1 x2 3
x1 C4x2 UN 2
x1x2 0:

29.56
ResolverelsiguienteprogramalinealutilizandoSIMPLEX:
maximizar x1 2x2
sujetoa
x1 C2x2 UN 4
2x1 6x2 12
x2 UN 1
x1x2 0:

29,57
ResolverelsiguienteprogramalinealutilizandoSIMPLEX:
maximizar x1 C3x2
sujetoa
x1 do x2 1
x1 x2 3
x1 C4x2 UN 2
x1x2 0:

29,58
Resuelvaelprogramalinealdadoen(29.6)(29.10).

29,59
Considereelsiguienteprogramalinealde1variable,quellamamosP:
maximizar Tx
sujetoa
Rxes
x 0
Donder,sytsonnmerosrealesarbitrarios.SeaDeldobledeP.

Pgina15

894 Captulo29Programacinlineal

Estadoparaculesvaloresder,s,ytpuedeafirmarque

https://translate.googleusercontent.com/translate_f 10/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
1.TantoPcomoDtienensolucionesptimasconvaloresobjetivosfinitos.
2.Pesfactible,peroDesinviable.
3.Desfactible,peroPesinviable.
4.NiPniDsonfactibles.

Problemas

291Viabilidaddedesigualdadlineal
Dadounconjuntodemdesigualdadeslinealesennvariablesx1,x2:::xn,lalinealidad
problemadeladesigualdaddeviabilidadpreguntasihayunajustedelasvariablesque
Satisfacesimultneamentecadaunadelasdesigualdades.
a.Demuestrequesitenemosunalgoritmoparalaprogramacinlineal,podemosusarlopara
Resolverunproblemadeviabilidaddedesigualdadlineal.Elnmerodevariablesycon
Straintsqueseutilizaenelproblemadeprogramacinlinealdebeserpolinomial
Ennym.

segundo.Demuestrequesitenemosunalgoritmoparaelproblemadeviabilidaddeladesigualdadlineal,
Podemosusarlopararesolverunproblemadeprogramacinlineal.Elnmerodevariables
Ydesigualdadeslinealesqueseutilizanenelproblemadeviabilidaddeladesigualdadlineal
Debeserpolinomialennym,elnmerodevariablesyrestriccionesen
Elprogramalineal.

292Escasezcomplementaria
Holguracomplementariadescribeunarelacinentrelosvaloresdeprimal
Variablesyrestriccionesdoblesyentrelosvaloresdelasvariablesdualesypri
Limitaciones.SeaNxunasolucinfactiblealprogramalinealprimariodado
En(29.16)(29.18),yseaNyunasolucinfactiblealprogramalinealdualdado
En(29,83)(29,85).Laholguracomplementariaestablecequelassiguientescondiciones
SonnecesariosysuficientesparaqueNxyNyseanptimos:
xmetro
un0DijNyiDjcoNxjparajD12:::norte
ID1
y
xnorte
NxaijjDbioNyiD0paraiD12:::M
JD1

Pgina16

ProblemasparaelCaptulo29 895

a.Verificarquelaholguracomplementariasemantieneparaelprogramalinealenlneas
(29,53)(29,57).

segundo.Demuestrequelaholguracomplementariasemantieneparacualquierprogramalinealprimarioy
Sucorrespondientedual.

do.ProbarqueunasolucinfactibleNxaunprogramalinealprimariodadoenlneas
(29.16)(29.18)esptimasiyslosiexistenvaloresNyD.Ny1Ny2:::Nym/
Talque
1.Nyesunasolucinfactiblealprogramalineardualdadoen(29.83)(29.85),
Pm
2. ID1aijNyiDcjparatodojtalqueNx
Pn j>0,y
3.NyiD0paratodoitalque jd1aijjNx<bi.

293Programacinlinealentero
Unproblemalinealenteradeprogramacinesunproblemadeprogramacinlinealcon
Larestriccinadicionalquelasvariablesxdebentomarsobrelosvaloresintegrales.Exer
Cise34.53muestraqueslodeterminarsiunprogramalinealenterotieneun
LasolucinfactibleesNPduro,loquesignificaquenohaypolinomiotiempoconocido
Algoritmoparaesteproblema.

a.Demuestrequeladualidaddbil(Lema29.8)esvlidaparaunprogramalinealentero.

https://translate.googleusercontent.com/translate_f 11/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
segundo.Demuestrequeladualidad(Teorema29.10)nosiempreesvlidaparaunenterolineal
programa.

do.Dadounprogramalinealprimarioenformaestndar,definamosPcomolaopti
Malobjetivoparaelprogramalinealprimario,Dcomoelobjetivoptimo
valorporsudoble,lapropiedadintelectualeselvalorobjetivoptimoparalaversinnmeroenterode
Elprimal(esdecir,elprimalconlarestriccinaadidaquetomanlasvariables
envaloresenteros),ydeidentificacinparaserelvalorobjetivoptimoparalaversinnmeroentero
Deladualidad.Suponiendoquetantoelprogramaenteroprimalcomoeldual
Sonfactiblesylimitados,muestranque

IPPDDaID:

294EllemadeFarkas
SeaAunamatrizmnycunvectorn.EntoncesellemadeFarkasafirmaque
Exactamenteunodelossistemas

Pgina17

896 Captulo29Programacinlineal

Ax0
cTx>0
y
ATCyD
Y 0
Essoluble,dondexesunnvectoreyesunmvector.ProbarellemadeFarkas.

295Circulacindecostomnimo
Enesteproblema,consideramosunavariantedelproblemadeflujode
Seccin29.2enlacualnosenosdaunademanda,unafuenteounsumidero.Enlugar,
Senosda,comoantes,unareddeflujoycostosdebordeau/.Unflujoesfactible
sisatisfacelarestriccindelacapacidaddeconservacinencadabordeyelflujoencada
vrtice.Elobjetivoesencontrar,entretodoslosflujosposibles,eldecostomnimo.Nosotros
llamaraesteproblemaelproblemadecostomnimocirculacin.
a.Formuleelproblemadecirculacindecostosmnimoscomounprogramalineal.

segundo.Supongamosqueparatodoslosbordes.u/2E,tenemosau/>0.Caracterizarun
Solucinptimaalproblemadelacirculacindecostesmnimos.

do.Formuleelproblemadeflujomximocomounproblemadecirculacindecostosmnimos
Programalineal.SedaunainstanciadeproblemadeflujomximoGD.VE/
Confuentess,sumideroycapacidadesdebordec,creanunacirculacindecostemnimo
problemadandoun(posiblementediferente)alaredG0D0.VE0/conelborde
capacidadesc0yelbordecuestaun0talquesepuedediscernirunasolucinal
Problemadeflujomximodesdeunasolucinalosproblemasdecirculacinde
Lem

re.Formuleelproblemadelatrayectoriamscortadeunasolafuentecomouncostomnimodecircu
Programalinealdeproblemas.

Notasdelcaptulo

Estecaptuloslocomienzaaestudiarelampliocampodelaprogramacinlineal.Unnmero
Millonesdelibrossededicanexclusivamentealaprogramacinlineal,incluidos
Chvtal[69],Gass[130],Karloff[197],Schrijver[303]yVanderbei[344].
Muchosotroslibrosdanunabuenacoberturadelaprogramacinlineal,
PorPapadimitriouySteiglitz[271]yAhuja,MagnantiyOrlin[7].los
LacoberturadeestecaptulosebasaenelenfoqueadoptadoporChvtal.

https://translate.googleusercontent.com/translate_f 12/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,

Pgina18

NotasparaelCaptulo29 897

ElalgoritmosimplexparalaprogramacinlinealfueinventadoporG.Dantzig
En1947.Pocodespus,losinvestigadoresdescubrieroncmoformularunaseriedepro
Problemasenunavariedaddecamposcomoprogramaslinealesyresolverlosconelsimplex
algoritmo.Comoresultado,florecieronlasaplicacionesdelaprogramacinlineal,
Variosalgoritmos.Lasvariantesdelalgoritmosimplexsiguensiendolasmspopulares
Mtodospararesolverproblemasdeprogramacinlineal.Estahistoriaapareceenun
Nmerodelugares,incluidaslasnotasde[69]y[197].
Elalgoritmodelelipsoidefueelprimeralgoritmopolinomialtiempoparaelpro
GramticaysedebeaLGKhachianen1979Sebasentrabajosanterioresde
NZShor,DBJudinyASNemirovskii.Grtschel,LovszySchrijver
[154]describencmousarelalgoritmoelipsoidepararesolverunavariedaddeproblemasen
Optimizacincombinatoria.Hastalafecha,elalgoritmoelipsoidenopareceser
Competitivoconelalgoritmosimplexenlaprctica.
EltrabajodeKarmarkar[198]incluyeunadescripcindelprimer
Rithm.Muchosinvestigadoresposterioresdisearonalgoritmosdepuntosinteriores.Buenasuperficie
VeysaparecenenelartculodeGoldfarbyTodd[141]yellibrodeYe[361].
Elanlisisdelalgoritmosimplexsiguesiendounreaactivadeinvestigacin.V.Klee
YGJMintyconstruyunejemploenelqueelalgoritmosimplexejecuta
atravsde2n 1iteraciones.Elalgoritmosimplexnormalmentefuncionamuybienen
Prcticaymuchosinvestigadoreshantratadodedarunajustificacintericapara
Observacinemprica.UnalneadeinvestigacininiciadaporKHBorgwardty
Pormuchosotros,muestraquebajociertassuposicionesprobabilsticassobre
Put,elalgoritmosimplexconvergeeneltiempopolinomialesperado.Spielmany
Teng[322]avanzenestarea,introduciendoel"anlisissuavizadodealgo
Rithms"yaplicarloalalgoritmosimplex.
Sesabequeelalgoritmosimplexfuncionaeficientementeenciertoscasosespeciales.Par
Elalgoritmoredsimplex,queeselsimplexal
Gorithm,especializadaenproblemasdeflujodered.Paraciertosproblemasdered,
Incluyendoloscaminosmscortos,flujomximoyproblemasdeflujodecostomnimo,
Variantesdelalgoritmoredsimplexejecutadoentiempopolinomial.Vase,porejemplo,
ElartculodeOrlin[268]ysuscitas.

Pgina19

30 PolinomiosylaFFT

Elmtododirectodeaadirdospolinomiosdegradontoma,.n/
tiempo,peroelmtododirectodemultiplicarloslleva,.n2/hora.Enesto
Captulo,mostraremoscmolatransformadarpidadeFourier,oFFT,puedereducireltiempo
Paramultiplicarpolinomiosa,.nlgn/.
ElusomscomnparalastransformadasdeFourier,yporlotantolaFFT,estenlaseal

https://translate.googleusercontent.com/translate_f 13/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
tratamiento.Unasealsedaeneldominiodeltiempo:comountiempoparacorrelacindefunciones
amplitud.Fourieranalysisallowsustoexpressthesignalasaweightedsumof
phaseshiftedsinusoidsofvaryingfrequencies.Theweightsandphasesassociated
withthefrequenciescharacterizethesignalinthefrequencydomain.Entreel
manyeverydayapplicationsofFFT'sarecompressiontechniquesusedtoencode
digitalvideoandaudioinformation,includingMP3files.Severalfinebooksdelve
intotherichareaofsignalprocessingthechapternotesreferenceafewofthem.

Polynomials

ApolynomialinthevariablexoveranalgebraicfieldFrepresentsafunctionAx/
asaformalsum:
xN1
Ax/D ajxj:
jD0
Wecallthevaluesa0a1:::an1thecoefficientsofthepolynomial.Theco
efficientsaredrawnfromafieldF,typicallythesetCofcomplexnumbers.UN
polynomialAx/hasdegreekifitshighestnonzerocoefficientisakwewrite
thatdegree.A/Dk.Anyintegerstrictlygreaterthanthedegreeofapolynomial
isadegreeboundofthatpolynomial.Therefore,thedegreeofapolynomialof
degreeboundnmaybeanyintegerbetween0andn1,inclusive.
Wecandefineavarietyofoperationsonpolynomials.Forpolynomialaddi
tion,ifAx/andBx/arepolynomialsofdegreeboundn,theirsumisapolyno

Pgina20

Chapter30PolynomialsandtheFFT 899

mialCx/,alsoofdegreeboundn,suchthatCx/DAx/CBx/forallxinthe
underlyingfield.Thatis,if
xN1
Ax/D ajxj
jD0
y
xN1
Bx/D bjxj
jD0
entonces
xN1
Cx/D cjxj
jD0
wherecjDajCbjforjD01:::norte 1.Forexample,ifwehavethe
polynomialsAx/D6x3C7x2 10xC9andBx/D2x3C4x 5,then
Cx/D4x3C7x2 6xC4.
Forpolynomialmultiplication,ifAx/andBx/arepolynomialsofdegree
boundn,theirproductCx/isapolynomialofdegreebound2n1suchthat
Cx/DAx/Bx/forallxintheunderlyingfield.Youprobablyhavemulti
pliedpolynomialsbefore,bymultiplyingeachterminAx/byeachterminBx/
andthencombiningtermswithequalpowers.Forexample,wecanmultiply
Ax/D6x3C7x2 10xC9andBx/D2x3C4x5asfollows:
6x3C7x2 10xC9
2x3 C4x 5
30x3 35x2C50x 45
24x4C28x3 40x2C36x
12x6 14x5C20x4 18x3
12x6 14x5C44x4 20x3 75x2C86x 45

AnotherwaytoexpresstheproductCx/is
x
2n2
Cx/D cjxj (30.1)
jD0
dnde

https://translate.googleusercontent.com/translate_f 14/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
xJ
cjD akbjk: (30.2)
kD0

Pgina21

900 Chapter30PolynomialsandtheFFT

Notethatdegree.C/Ddegree.A/Cdegree.B/,implyingthatifAisapolyno
mialofdegreeboundnaandBisapolynomialofdegreeboundnb,thenCisa
polynomialofdegreeboundnaCnb 1.Sinceapolynomialofdegreeboundk
isalsoapolynomialofdegreeboundkC1,wewillnormallysaythattheproduct
polynomialCisapolynomialofdegreeboundnaCnb.

Chapteroutline
Section30.1presentstwowaystorepresentpolynomials:thecoefficientrepresen
tationandthepointvaluerepresentation.Thestraightforwardmethodsformulti
plyingpolynomialsequations(30.1)and(30.2)take.n2/timewhenwerep
resentpolynomialsincoefficientform,butonly.n/timewhenwerepresentthem
inpointvalueform.Wecan,however,multiplypolynomialsusingthecoefficient
representationinonly.nlgn/timebyconvertingbetweenthetworepresenta
Ciones.Toseewhythisapproachworks,wemustfirststudycomplexrootsofunity,
whichwedoinSection30.2.Then,weusetheFFTanditsinverse,alsodescribed
inSection30.2,toperformtheconversions.Section30.3showshowtoimplement
theFFTquicklyinbothserialandparallelmodels.
Thischapterusescomplexnumbersextensively,andwithinthischapterweuse
pag
thesymboliexclusivelytodenote 1.

30.1Representingpolynomials

Thecoefficientandpointvaluerepresentationsofpolynomialsareinasenseequiv
alentthatis,apolynomialinpointvalueformhasauniquecounterpartinco
efficientform.Inthissection,weintroducethetworepresentationsandshow
howtocombinethemsothatwecanmultiplytwodegreeboundnpolynomials
in.nlgn/time.

Coefficientrepresentation
Pn1
AcoefficientrepresentationofapolynomialAx/D
jD0ajxjofdegree
boundnisavectorofcoefficientsaD.a0a1:::an1/.Inmatrixequations
inthischapter,weshallgenerallytreatvectorsascolumnvectors.
Thecoefficientrepresentationisconvenientforcertainoperationsonpolyno
mials.Forexample,theoperationofevaluatingthepolynomialAx/atagiven
pointx0consistsofcomputingthevalueofAx0/.Wecanevaluateapolynomial
in.n/timeusingHorner'srule:
Ax0/Da0Cx0.a1Cx0.a2CCx0.an2Cx0.an1////:

Pgina22

30.1Representingpolynomials 901

Similarly,addingtwopolynomialsrepresentedbythecoefficientvectorsaD
.a0a1:::an1/andbD.b0b1:::bn1/takes.n/time:wejustproduce
thecoefficientvectorcD.c0c1:::cn1/,wherecj DajCbjforjD
01:::n1.
Now,considermultiplyingtwodegreeboundnpolynomialsAx/andBx/rep
resentedincoefficientform.Ifweusethemethoddescribedbyequations(30.1)
and(30.2),multiplyingpolynomialstakestime.n2/,sincewemustmultiply
eachcoefficientinthevectorabyeachcoefficientinthevectorb.Laoperacion
ofmultiplyingpolynomialsincoefficientformseemstobeconsiderablymorediffi
cultthanthatofevaluatingapolynomialoraddingtwopolynomials.Laresultante
https://translate.googleusercontent.com/translate_f 15/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
coefficientvectorc,givenbyequation(30.2),isalsocalledtheconvolutionofthe
inputvectorsaandb,denotedcDab.Sincemultiplyingpolynomialsand
computingconvolutionsarefundamentalcomputationalproblemsofconsiderable
practicalimportance,thischapterconcentratesonefficientalgorithmsforthem.

Pointvaluerepresentation

ApointvaluerepresentationofapolynomialAx/ofdegreeboundnisasetof
npointvaluepairs
fx0y0/.x1y1/:::.xn1yn1/g
suchthatallofthexkaredistinctand
ykDAxk/ (30.3)
forkD01:::n1.Apolynomialhasmanydifferentpointvaluerepresenta
tions,sincewecanuseanysetofndistinctpointsx0x1:::xn1asabasisfor
therepresentation.
Computingapointvaluerepresentationforapolynomialgivenincoefficient
formisinprinciplestraightforward,sinceallwehavetodoisselectndistinct
pointsx0x1:::xn1andthenevaluateAxk/forkD01:::norte 1.With
Horner'smethod,evaluatingapolynomialatnpointstakestime.n2/.Weshall
seelaterthatifwechoosethepointsxkcleverly,wecanacceleratethiscomputation
torunintime.nlgn/.
Theinverseofevaluationdeterminingthecoefficientformofapolynomial
fromapointvaluerepresentationisinterpolation.Thefollowingtheoremshows
thatinterpolationiswelldefinedwhenthedesiredinterpolatingpolynomialmust
haveadegreeboundequaltothegivennumberofpointvaluepairs.

Theorem30.1(Uniquenessofaninterpolatingpolynomial)
Foranysetfx0y0/.x1y1/:::.xn1yn1/gofnpointvaluepairssuchthat
allthexkvaluesaredistinct,thereisauniquepolynomialAx/ofdegreeboundn
suchthatykDAxk/forkD01:::n1.

Pgina23

902 Chapter30PolynomialsandtheFFT

ProofTheproofreliesontheexistenceoftheinverseofacertainmatrix.Equa
tion(30.3)isequivalenttothematrixequation

1x0 x2 xn1 a0 y0
0 0
1x1 x2 xn1 un1 y1
1 1 re : (30.4)
Unesdoc.unesco.orgunesdoc.unesco.org
Unesdoc.unesco.orgunesdoc.unesco.org
Unesdoc.unesco.orgunesdoc.unesco.org
Unesdoc.unesco.orgunesdoc.unesco.org
Unesdoc.unesco.orgunesdoc.unesco.org
Unesdoc.unesco.orgunesdoc.unesco.org
Unesdoc.unesco.orgunesdoc.unesco.org
1xn1 x2 xn1 an1 yn1
N1 N1
ThematrixontheleftisdenotedV.x0x1:::xn1/andisknownasaVander
mondematrix.ByProblemD1,thismatrixhasdeterminant
Y
.xk xj/
0j<kn1
andtherefore,byTheoremD.5,itisinvertible(thatis,nonsingular)ifthexkare
distinto.Thus,wecansolveforthecoefficientsajuniquelygiventhepointvalue
representacin:
aDV.x0x1:::xn1/1y:
TheproofofTheorem30.1describesanalgorithmforinterpolationbasedon
solvingtheset(30.4)oflinearequations.UsingtheLUdecompositionalgorithms
ofChapter28,wecansolvetheseequationsintimeOn3/.
AfasteralgorithmfornpointinterpolationisbasedonLagrange'sformula:
Y
.xxj/
xN1
Ax/D yk Yjk : (30,5)
kD0 .xk xj/
jk
Youmaywishtoverifythattherighthandsideofequation(30.5)isapolynomial
ofdegreeboundnthatsatisfiesAxk/Dykforallk.Exercise30.15asksyou
howtocomputethecoefficientsofAusingLagrange'sformulaintime.n2/.
Thus,npointevaluationandinterpolationarewelldefinedinverseoperations
thattransformbetweenthecoefficientrepresentationofapolynomialandapoint
https://translate.googleusercontent.com/translate_f 16/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
valuerepresentation.1 Thealgorithmsdescribedabovefortheseproblemstake
time.n2/.
Thepointvaluerepresentationisquiteconvenientformanyoperationsonpoly
nomials.Foraddition,ifCx/DAx/CBx/,thenCxk/DAxk/CBxk/for
anypointxk.Moreprecisely,ifwehaveapointvaluerepresentationforA,

1Interpolationisanotoriouslytrickyproblemfromthepointofviewofnumericalstability.Apesardeque
theapproachesdescribedherearemathematicallycorrect,smalldifferencesintheinputsorroundoff
errorsduringcomputationcancauselargedifferencesintheresult.

Pgina24

30.1Representingpolynomials 903

fx0y0/.x1y1/:::.xn1yn1/g
andforB,
fx0y0
0/.x1y1
0/:::.xn1y0n1/g
(notethatAandBareevaluatedatthesamenpoints),thenapointvaluerepre
sentationforCis
fx0y0Cy0
0/.x1y1Cy1
0/:::.xn1yn1Cy0 n1/g:
Thus,thetimetoaddtwopolynomialsofdegreeboundninpointvalueform
is.n/.
Similarly,thepointvaluerepresentationisconvenientformultiplyingpolyno
mials.IfCx/DAx/Bx/,thenCxk/DAxk/Bxk/foranypointxk,and
wecanpointwisemultiplyapointvaluerepresentationforAbyapointvaluerep
resentationforBtoobtainapointvaluerepresentationforC.Wemustfacethe
problem,however,thatdegree.C/Ddegree.A/Cdegree.B/ifAandBareof
degreeboundn,thenCisofdegreebound2n.Astandardpointvaluerepresen
tationforAandBconsistsofnpointvaluepairsforeachpolynomial.Cuandonosotros
multiplythesetogether,wegetnpointvaluepairs,butweneed2npairstointerpo
lateauniquepolynomialCofdegreebound2n.(SeeExercise30.14.)Wemust
thereforebeginwithextendedpointvaluerepresentationsforAandforBcon
sistingof2npointvaluepairseach.Givenanextendedpointvaluerepresentation
forA,
fx0y0/.x1y1/:::.x2n1y2n1/g
andacorrespondingextendedpointvaluerepresentationforB,
fx0y0
0/.x1y1
0/:::.x2n1y02n1/g
thenapointvaluerepresentationforCis
fx0y0y0
0/.x1y11
y0/:::.x2n1y2n1y02n1/g:
Giventwoinputpolynomialsinextendedpointvalueform,weseethatthetimeto
multiplythemtoobtainthepointvalueformoftheresultis.n/,muchlessthan
thetimerequiredtomultiplypolynomialsincoefficientform.
Finally,weconsiderhowtoevaluateapolynomialgiveninpointvalueformata
newpoint.Forthisproblem,weknowofnosimplerapproachthanconvertingthe
polynomialtocoefficientformfirst,andthenevaluatingitatthenewpoint.

Fastmultiplicationofpolynomialsincoefficientform
Canweusethelineartimemultiplicationmethodforpolynomialsinpointvalue
formtoexpeditepolynomialmultiplicationincoefficientform?Theanswerhinges

Pgina25

904 Chapter30PolynomialsandtheFFT

a a :::a
0 1 n1
Ordinarymultiplication Coeficiente
c c :::c
0 1 2n2
representations
b b :::b
0 1 n1
Time.n / 2

https://translate.googleusercontent.com/translate_f 17/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
Evaluacin Interpolation
Time.nlgn/ Time.nlgn/

A.!2n/B.!02n/
0 C.! 2n/
0

A.!2n/B.!1
1
2n/ Pointwisemultiplication C.!2n/
1 Pointvalue
Unesdoc.unesco.orgunesdoc.unesco.org Unesdoc.unesco.orgunesdoc.unesco.org
representations
Time.n/
A.!2n /B.!2n /
2n1 2n1 C.!2n /
2n1

Figure30.1Agraphicaloutlineofanefficientpolynomialmultiplicationprocess.Representaciones
onthetopareincoefficientform,whilethoseonthebottomareinpointvalueform.Thearrows
fromlefttorightcorrespondtothemultiplicationoperation.The!2ntermsarecomplex.2n/throots
ofunity.

onwhetherwecanconvertapolynomialquicklyfromcoefficientformtopoint
valueform(evaluate)andviceversa(interpolate).
Wecanuseanypointswewantasevaluationpoints,butbychoosingtheeval
uationpointscarefully,wecanconvertbetweenrepresentationsinonly.nlgn/
hora.AsweshallseeinSection30.2,ifwechoosecomplexrootsofunityas
theevaluationpoints,wecanproduceapointvaluerepresentationbytakingthe
discreteFouriertransform(orDFT)ofacoefficientvector.Wecanperformthe
inverseoperation,interpolation,bytakingtheinverseDFTofpointvaluepairs,
yieldingacoefficientvector.Section30.2willshowhowtheFFTaccomplishes
theDFTandinverseDFToperationsin.nlgn/time.
Figure30.1showsthisstrategygraphically.Oneminordetailconcernsdegree
bounds.Theproductoftwopolynomialsofdegreeboundnisapolynomialof
degreebound2n.BeforeevaluatingtheinputpolynomialsAandB,therefore,
wefirstdoubletheirdegreeboundsto2nbyaddingnhighordercoefficientsof0.
Becausethevectorshave2nelements,weusecomplex.2n/throotsofunity,
whicharedenotedbythe!2ntermsinFigure30.1.
GiventheFFT,wehavethefollowing.nlgn/timeprocedureformultiplying
twopolynomialsAx/andBx/ofdegreeboundn,wheretheinputandoutput
representationsareincoefficientform.Weassumethatnisapowerof2wecan
alwaysmeetthisrequirementbyaddinghighorderzerocoefficients.
1.Doubledegreebound:CreatecoefficientrepresentationsofAx/andBx/as
degreebound2npolynomialsbyaddingnhighorderzerocoefficientstoeach.

Pgina26

30.1Representingpolynomials 905

2.Evaluate:ComputepointvaluerepresentationsofAx/andBx/oflength2n
byapplyingtheFFToforder2noneachpolynomial.Theserepresentations
containthevaluesofthetwopolynomialsatthe.2n/throotsofunity.
3.Pointwisemultiply:Computeapointvaluerepresentationforthepolynomial
Cx/DAx/Bx/bymultiplyingthesevaluestogetherpointwise.Thisrepre
sentationcontainsthevalueofCx/ateach.2n/throotofunity.
4.Interpolate:CreatethecoefficientrepresentationofthepolynomialCx/by
applyingtheFFTon2npointvaluepairstocomputetheinverseDFT.
Steps(1)and(3)taketime.n/,andsteps(2)and(4)taketime.nlgn/.As,
onceweshowhowtousetheFFT,wewillhaveproventhefollowing.

Theorem30.2
Wecanmultiplytwopolynomialsofdegreeboundnintime.nlgn/,withboth
theinputandoutputrepresentationsincoefficientform.

Exercises

30.11
MultiplythepolynomialsAx/D7x3 x2Cx10andBx/D8x3 6xC3
usingequations(30.1)and(30.2).

30.12
AnotherwaytoevaluateapolynomialAx/ofdegreeboundnatagivenpointx0

https://translate.googleusercontent.com/translate_f 18/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
istodivideAx/bythepolynomial.xx0/,obtainingaquotientpolynomialqx/
ofdegreeboundn1andaremainderr,suchthat
Ax/Dqx/.xx0/Cr:
Clearly,Ax0/Dr.Showhowtocomputetheremainderrandthecoefficients
ofqx/intime.n/fromx0andthecoefficientsofA.

30.13 Pn1
DeriveapointvaluerepresentationforA
Pn1 rev.x/D
jD0an1jxjfromapoint
valuerepresentationforAx/D jD0ajxj,assumingthatnoneofthepointsis0.
30.14
Provethatndistinctpointvaluepairsarenecessarytouniquelyspecifyapolyno
mialofdegreeboundn,thatis,iffewerthanndistinctpointvaluepairsaregiven,
theyfailtospecifyauniquepolynomialofdegreeboundn.(Hint:UsingTheo
rem30.1,whatcanyousayaboutasetofn1pointvaluepairstowhichyouadd
onemorearbitrarilychosenpointvaluepair?)

Pgina27

906 Chapter30PolynomialsandtheFFT

30.15
Showhowtouseequation(30.5)tointerpolateintime.n
Q 2/.(Hint:Firstcompute

thecoefficientrepresentationofthepolynomial xj/andthendivideby
j.x
.xxk/asnecessaryforthenumeratorofeachtermseeExercise30.12.Ustedpuede
computeeachofthendenominatorsintimeOn/.)

30.16
Explainwhatiswrongwiththeobviousapproachtopolynomialdivisionusing
apointvaluerepresentation,ie,dividingthecorrespondingyvalues.Discutir
separatelythecaseinwhichthedivisioncomesoutexactlyandthecaseinwhich
itdoesn't.

30.17
ConsidertwosetsAandB,eachhavingnintegersintherangefrom0to10n.Nosotros
wishtocomputetheCartesiansumofAandB,definedby
CDfxCyWx2Aandy2Bg:
NotethattheintegersinCareintherangefrom0to20n.Wewanttofindthe
elementsofCandthenumberoftimeseachelementofCisrealizedasasumof
elementsinAandB.ShowhowtosolvetheprobleminO.nlgn/time.(Hint:
RepresentAandBaspolynomialsofdegreeatmost10n.)

30.2TheDFTandFFT

InSection30.1,weclaimedthatifweusecomplexrootsofunity,wecanevaluate
andinterpolatepolynomialsin.nlgn/time.Inthissection,wedefinecomplex
rootsofunityandstudytheirproperties,definetheDFT,andthenshowhowthe
FFTcomputestheDFTanditsinversein.nlgn/time.

Complexrootsofunity
Acomplexnthrootofunityisacomplexnumber!Talque
!nD1:
Thereareexactlyncomplexnthrootsofunity:e2ik=nforkD01:::n1.
Tointerpretthisformula,weusethedefinitionoftheexponentialofacomplex
nmero:
eiuDcos.u/Cisin.u/:
Figure30.2showsthatthencomplexrootsofunityareequallyspacedaroundthe
circleofunitradiuscenteredattheoriginofthecomplexplane.Elvalor

Pgina28

https://translate.googleusercontent.com/translate_f 19/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,

30.2TheDFTandFFT 907

yo !8 2

!8
3 !8 1

!8
4 !8D!88
0

1 1

!8
5 !8 7

yo !8 6

Figure30.2Thevaluesof!
8!01
8:::!7 8inthecomplexplane,where!8De2i=8istheprin
cipal8throotofunity.

!nDe2i=n (30.6)
istheprincipalnthrootofunity2allothercomplexnthrootsofunityarepowers
of!n.
Thencomplexnthrootsofunity,
!0
n!1n:::!N1norte
formagroupundermultiplication(seeSection31.3).Thisgrouphasthesame
structureastheadditivegroup.ZnC/modulon,since!norte
nD!0nD1impliesthat
!J D!.jCk/modn .Similarly,!1 D!N1 .Thefollowinglemmas
n!K
nD!jCk norte norte norte norte
furnishsomeessentialpropertiesofthecomplexnthrootsofunity.

Lemma30.3(Cancellationlemma)
Foranyintegersn 0,k 0,andd>0,
!Dk
dnD!Kn: (30.7)

ProofThelemmafollowsdirectlyfromequation(30.6),since
!Dk re e2i=dndk
Dn
re e2i=nk
D!K
n:

2Manyotherauthorsdefine!ndifferently:!nDe2i=n.Thisalternativedefinitiontendstobe
usedforsignalprocessingapplications.Theunderlyingmathematicsissubstantiallythesamewith
eitherdefinitionof!n.

Pgina29

908 Chapter30PolynomialsandtheFFT

Corollary30.4
Foranyevenintegern>0,
!n=2 D!2D1:
norte

ProofTheproofisleftasExercise30.21.

Lemma30.5(Halvinglemma)
Ifn>0iseven,thenthesquaresofthencomplexnthrootsofunityarethen=2
complex.n=2/throotsofunity.

ProofBythecancellationlemma,wehave.!K n/2D!Kn=2,foranynonnegative
integerk.Notethatifwesquareallofthecomplexnthrootsofunity,thenwe
obtaineach.n=2/throotofunityexactlytwice,since

.!kCn=2
norte /2 D!2kCnnorte
D!2k
n!norte
norte
https://translate.googleusercontent.com/translate_f 20/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
D!2k norte
D.!K
n/2:
Thus,!Knand!kCn=2
norte havethesamesquare.WecouldalsohaveusedCorol
lary30.4toprovethisproperty,since!n=2 norteD1implies!kCn=2 norte D!K n,and
thus.!kCn=2 /2D.!K
norte n/2.
Asweshallsee,thehalvinglemmaisessentialtoourdivideandconquerap
proachforconvertingbetweencoefficientandpointvaluerepresentationsofpoly
nomials,sinceitguaranteesthattherecursivesubproblemsareonlyhalfaslarge.

Lemma30.6(Summationlemma)
Foranyintegern 1andnonzerointegerknotdivisiblebyn,
xN1
J D0:
!Knorte
jD0

ProofEquation(A.5)appliestocomplexvaluesaswellastoreals,andsowe
tener

Pgina30

30.2TheDFTandFFT 909

xN1
.!K 1
!K J re n/n
norte !K 1
jD0 norte
.!norte 1
re n/k
!Knorte1
.1/k 1
re
!K 1
norte
D0:
Becausewerequirethatkisnotdivisiblebyn,andbecause!K
nD1onlywhenk
isdivisiblebyn,weensurethatthedenominatorisnot0.

TheDFT
Recallthatwewishtoevaluateapolynomial
xN1
Ax/D ajxj
jD0
ofdegreeboundnat!0 n!1n!2n:::!N1norte(thatis,atthencomplexnthrootsof
unity).3WeassumethatAisgivenincoefficientform:aD.a0a1:::an1/.Dejar
usdefinetheresultsyk,forkD01:::n1,by
yk DA.!K n/
xN1
re aj!n
Kj
:
(30.8)
jD0
ThevectoryD.y0y1:::yn1/isthediscreteFouriertransform(DFT)ofthe
coefficientvectoraD.a0a1:::an1/.WealsowriteyDDFTn.a/.

TheFFT
ByusingamethodknownasthefastFouriertransform(FFT),whichtakesad
vantageofthespecialpropertiesofthecomplexrootsofunity,wecancompute
DFTn.a/intime.nlgn/,asopposedtothe.n2/timeofthestraightforward
Mtodo.Weassumethroughoutthatnisanexactpowerof2.Althoughstrategies

3Thelengthnisactuallywhatwereferredtoas2ninSection30.1,sincewedoublethedegreebound

https://translate.googleusercontent.com/translate_f 21/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
weareactuallyworkingwithcomplex.2n/throotsofunity.
ofthegivenpolynomialspriortoevaluation.Inthecontextofpolynomialmultiplication,therefore,

Pgina31

910 Chapter30PolynomialsandtheFFT

fordealingwithnonpowerof2sizesareknown,theyarebeyondthescopeofthis
libro.
TheFFTmethodemploysadivideandconquerstrategy,usingtheevenindexed
andoddindexedcoefficientsofAx/separatelytodefinethetwonewpolynomials
A0.x/andA1.x/ofdegreeboundn=2:
A0.x/Da0Ca2xCa4x2CCan2xn=21
A1.x/Da1Ca3xCa5x2CCan1xn=21:

NotethatA0containsalltheevenindexedcoefficientsofA(thebinaryrepresen
tationoftheindexendsin0)andA1containsalltheoddindexedcoefficients(the
binaryrepresentationoftheindexendsin1).Resultaque
Ax/DA0.x2/CxA1.x2/ (30,9)

sothattheproblemofevaluatingAx/at!0 n!1n:::!N1nortereducesto
1.evaluatingthedegreeboundn=2polynomialsA0.x/andA1.x/atthepoints

.!0 /2 (30.10)
n/2.!n1/2:::.!N1
norte
yentonces
2.combiningtheresultsaccordingtoequation(30.9).
Bythehalvinglemma,thelistofvalues(30.10)consistsnotofndistinctval
uesbutonlyofthen=2complex.n=2/throotsofunity,witheachrootoccurring
exactlytwice.Therefore,werecursivelyevaluatethepolynomialsA0andA1
ofdegreeboundn=2atthen=2complex.n=2/throotsofunity.Thesesubprob
lemshaveexactlythesameformastheoriginalproblem,butarehalfthesize.
WehavenowsuccessfullydividedannelementDFTncomputationintotwon=2
elementDFTn=2computations.Thisdecompositionisthebasisforthefollow
ingrecursiveFFTalgorithm,whichcomputestheDFTofannelementvector
aD.a0a1:::an1/,wherenisapowerof2.

Pgina32

30.2TheDFTandFFT 911

RECURSIVEFFT.a/
1nDa:length //nisapowerof2
2ifn==1
3 returna
4!nDe2i=n
5!D1
6a0D.a0a2:::an2/
7a1D.a1a3:::an1/
8y0DRECURSIVEFFT.a0/
9y1DRECURSIVEFFT.a1/
10forkD0ton=21

https://translate.googleusercontent.com/translate_f 22/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
11 ykDy K 0C!Y K
1
12 ykC.n=2/Dy 0 !Y 1
K K
13 !D!!norte
14returny //yisassumedtobeacolumnvector

TheRECURSIVEFFTprocedureworksasfollows.Lines23representthebasis
oftherecursiontheDFTofoneelementistheelementitself,sinceinthiscase
y0 Da0!0
1
Da01
Da0:
Lines67definethecoefficientvectorsforthepolynomialsA0andA1.Lneas
4,5,and13guaranteethat!isupdatedproperlysothatwheneverlines1112
areexecuted,wehave!D!K
n.(Keepingarunningvalueof!fromiteration
toiterationsavestimeovercomputing!K nfromscratcheachtimethroughthefor
loop.)Lines89performtherecursiveDFTn=2computations,setting,forkD
01:::n=21,

Y0 DA0.!K
K n=2/
Y1 DA1.!K
K n=2/
or,since!K bythecancellationlemma,
norte
n=2D!2k
Y0 DA0.!2k
K n/
Y1 DA1.!2k
K n/:

Pgina33

912 Chapter30PolynomialsandtheFFT

Lines1112combinetheresultsoftherecursiveDFTn=2calculations.Fory0y1
:::yn=21,line11yields

yk Dy 0C!K 1
K nyK
DA0.!2k
n/C!KnA1n
.!2k
/
DA.!K n/ (byequation(30.9)).
Foryn=2yn=2C1:::yn1,lettingkD01:::n=21,line12yields

ykC.n=2/ Dy 0 !K 1
K nyK
Dy 0C!kC.n=2/
norte YK
1
norte D!K n)
(since!kC.n=2/
K
DA0.!2k A1.!2k
n/C!kC.n=2/
norte n/
DA0.!2kCn /C!kC.n=2/ A1.!2kCn /(since!2kCn D!2k
norte norte norte norte n)
DA.!kC.n=2/ / (byequation(30.9)).
norte
Thus,thevectoryreturnedbyRECURSIVEFFTisindeedtheDFToftheinput
vectora.
Lines11and12multiplyeachvaluey 1 K by!Kn,forkD01:::n=2
1.
Line11addsthisproducttoy 0
k,andline12subtractsit.Becauseweuseeach
factor!Kninbothitspositiveandnegativeforms,wecallthefactors!K ntwiddle
factors.
TodeterminetherunningtimeofprocedureRECURSIVEFFT,wenotethat
exclusiveoftherecursivecalls,eachinvocationtakestime.n/,wherenisthe
lengthoftheinputvector.Therecurrencefortherunningtimeistherefore
T.n/D2T.n=2/C.n/
D.nlgn/:

Thus,wecanevaluateapolynomialofdegreeboundnatthecomplexnthrootsof
unityintime.nlgn/usingthefastFouriertransform.

Interpolationatthecomplexrootsofunity

https://translate.googleusercontent.com/translate_f 23/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
Wenowcompletethepolynomialmultiplicationschemebyshowinghowtoin
terpolatethecomplexrootsofunitybyapolynomial,whichenablesustoconvert
frompointvalueformbacktocoefficientform.WeinterpolatebywritingtheDFT
asamatrixequationandthenlookingattheformofthematrixinverse.
Fromequation(30.4),wecanwritetheDFTasthematrixproductyDVna,
whereVnisaVandermondematrixcontainingtheappropriatepowersof!n:

Pgina34

30.2TheDFTandFFT 913

y0 1 1 1 1 1 a0
y1 1!norte !2norte !3norte !N1
norte un1
y2 1!2 !4 !6 !2.n1/ un2
re norte norte norte norte :
y3 1!3 !6 !9 !3.n1/ a3
norte norte norte norte
Unesdoc.unesco.orgunesdoc.unesco.org
Unesdoc.unesco.orgunesdoc.unesco.org
Unesdoc.unesco.orgunesdoc.unesco.org
Unesdoc.unesco.orgunesdoc.unesco.org
Unesdoc.unesco.orgunesdoc.unesco.org
Unesdoc.unesco.orgunesdoc.unesco.org
Unesdoc.unesco.orgunesdoc.unesco.org
Unesdoc.unesco.orgunesdoc.unesco.org
yn1 1!N1 !2.n1/ !3.n1/ !.n1/.n1/ an1
norte norte norte norte
The.kj/entryofVnis!Kj n,forjkD01:::norte 1.Theexponentsofthe
entriesofVnformamultiplicationtable.
Fortheinverseoperation,whichwewriteasaDDFT1
n.y/,weproceedby
multiplyingybythematrixV1 ,theinverseofVn.
norte
Theorem30.7
ForjkD01:::n1,the.jk/entryofV1 =n.
is!Kjnorte
norte
ProofWeshowthatV1 VnDIn,thennidentitymatrix.Considerthe.jj0/
norte
entryofV1
nVn:
xN1
V1 re .!Kj =n/.!kj0
nVnjj 0 norte n/
kD0
xN1
re !kj j/ =n:
norte
0

kD0
Thissummationequals1ifj0Dj,anditis0otherwisebythesummationlemma
(Lemma30.6).Notethatwerelyon.n1/j0 jn1,sothatj0 jis
notdivisiblebyn,inorderforthesummationlemmatoapply.

GiventheinversematrixVnorte
1 ,wehavethatDFT1
n.y/isgivenby
xN1
1
ajD yk!norte
Kj (30.11)
norte
kD0
forjD01:::n1.Bycomparingequations(30.8)and(30.11),weseethat
bymodifyingtheFFTalgorithmtoswitchtherolesofaandy,replace!nby!1 n,
anddivideeachelementoftheresultbyn,wecomputetheinverseDFT(seeEx
ercise30.24).Thus,wecancomputeDFT1norte in.nlgn/timeaswell.
Weseethat,byusingtheFFTandtheinverseFFT,wecantransformapoly
nomialofdegreeboundnbackandforthbetweenitscoefficientrepresentation
andapointvaluerepresentationintime.nlgn/.Inthecontextofpolynomial
multiplication,wehaveshownthefollowing.

Pgina35

914 Chapter30PolynomialsandtheFFT

Theorem30.8(Convolutiontheorem)
Foranytwovectorsaandboflengthn,wherenisapowerof2,
abDDFT1
2n.DFT2n.a/DFT2n.b//
wherethevectorsaandbarepaddedwith0stolength2nanddenotesthecom
https://translate.googleusercontent.com/translate_f 24/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
ponentwiseproductoftwo2nelementvectors.

Exercises

30.21
ProveCorollary30.4.

30.22
ComputetheDFTofthevector.0123/.

30.23
DoExercise30.11byusingthe.nlgn/timescheme.

30.24
WritepseudocodetocomputeDFT1 in.nlgn/time.
norte
30.25
DescribethegeneralizationoftheFFTproceduretothecaseinwhichnisapower
of3.Givearecurrencefortherunningtime,andsolvetherecurrence.

30.26?
SupposethatinsteadofperformingannelementFFToverthefieldofcomplex
numbers(whereniseven),weusetheringZmofintegersmodulom,where
mD2tn=2C1andtisanarbitrarypositiveinteger.Use!D2tinsteadof!norte
asaprincipalnthrootofunity,modulom.ProvethattheDFTandtheinverseDFT
arewelldefinedinthissystem.

30.27
Givenalistofvalues01:::n1(possiblywithrepetitions),showhowtofind
thecoefficientsofapolynomialPx/ofdegreeboundnC1thathaszerosonly
at01:::n1(possiblywithrepetitions).Yourprocedureshouldrunintime
O.nlg2n/.(Hint:ThepolynomialPx/hasazeroatjifandonlyifPx/isa
multipleof.xj/.)

30.28?
ThechirptransformofavectoraD.a
Pn1 0a1:::an1/isthevectoryD
.y0y1:::yn1/,whereykD
jD0ajkjandisanycomplexnumber. los

Pgina36

30.3EfficientFFTimplementations 915

DFTisthereforeaspecialcaseofthechirptransform,obtainedbytakingD!n.
ShowhowtoevaluatethechirptransformintimeO.nlgn/foranycomplexnum
ber.(Hint:Usetheequation
xN1 UN UN
ykDk =2
2 ajj =2 .kj/ =2
2 2

jD0
toviewthechirptransformasaconvolution.)

30.3EfficientFFTimplementations

SincethepracticalapplicationsoftheDFT,suchassignalprocessing,demandthe
utmostspeed,thissectionexaminestwoefficientFFTimplementations.First,we
shallexamineaniterativeversionoftheFFTalgorithmthatrunsin.nlgn/time
butcanhavealowerconstanthiddeninthenotationthantherecursiveversion
inSection30.2.(Dependingontheexactimplementation,therecursiveversion
mayusethehardwarecachemoreefficiently.)Then,weshallusetheinsightsthat
ledustotheiterativeimplementationtodesignanefficientparallelFFTcircuit.

AniterativeFFTimplementation
Wefirstnotethattheforloopoflines1013ofRECURSIVEFFTinvolvescom
1dosveces.Incompilerterminology,wecallsuchavaluea
putingthevalue!K ny K
commonsubexpression.Wecanchangethelooptocomputeitonlyonce,storing
itinatemporaryvariablet.
https://translate.googleusercontent.com/translate_f 25/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,

forkD0ton=21
tD!Y 1
K
ykDy 0Ct
K
ykC.n=2/Dy 0 T
K
!D!!norte

Theoperationinthisloop,multiplyingthetwiddlefactor!D!K 1
nbyy k,storing
theproductintot,andaddingandsubtractingtfromy 0
k,isknownasabutterfly
operationandisshownschematicallyinFigure30.3.
WenowshowhowtomaketheFFTalgorithmiterativeratherthanrecursive
instructure.InFigure30.4,wehavearrangedtheinputvectorstotherecursive
callsinaninvocationofRECURSIVEFFTinatreestructure,wheretheinitial
callisfornD8.Thetreehasonenodeforeachcalloftheprocedure,labeled

Pgina37

916 Chapter30PolynomialsandtheFFT

yK
0 + yK C!nyK1
0 K yK0 yK C!nyK
0 K
1
!norte
K !norte
K

yK
1 yK0 !nyK
K
1 yK1 yK0 !nyK
K
1
(un) (segundo)

Figure30.3Abutterflyoperation.(a)Thetwoinputvaluesenterfromtheleft,thetwiddlefac
tor!n 1
K
ismultipliedbyy K ,andthesumanddifferenceareoutputontheright.(b)Asimplified
drawingofabutterflyoperation.WewillusethisrepresentationinaparallelFFTcircuit.

(a0 ,a1 ,a2 ,a3 ,a4 ,a5 ,a6 ,a7 )

(a0 ,a2 ,a4 ,a6 ) (a1 ,a3 ,a5 ,a7 )

(a0 ,a4 ) (a2 ,a6 ) (a1 ,a5 ) (a3 ,a7 )

(a0 ) (a4 ) (a2 ) (a6 ) (a1 ) (a5 ) (a3 ) (a7 )

Figure30.4ThetreeofinputvectorstotherecursivecallsoftheRECURSIVEFFTprocedure.El
initialinvocationisfornD8.

bythecorrespondinginputvector.EachRECURSIVEFFTinvocationmakestwo
recursivecalls,unlessithasreceiveda1elementvector.Thefirstcallappearsin
theleftchild,andthesecondcallappearsintherightchild.
Lookingatthetree,weobservethatifwecouldarrangetheelementsofthe
initialvectoraintotheorderinwhichtheyappearintheleaves,wecouldtrace
theexecutionoftheRECURSIVEFFTprocedure,butbottomupinsteadoftop
abajo.First,wetaketheelementsinpairs,computetheDFTofeachpairusing
onebutterflyoperation,andreplacethepairwithitsDFT.Thevectorthenholds
n=22elementDFTs.Next,wetakethesen=2DFTsinpairsandcomputethe
DFTofthefourvectorelementstheycomefrombyexecutingtwobutterflyoper
ations,replacingtwo2elementDFTswithone4elementDFT.Thevectorthen
holdsn=44elementDFTs.Wecontinueinthismanneruntilthevectorholdstwo
.n=2/elementDFTs,whichwecombineusingn=2butterflyoperationsintothe
finalnelementDFT.
Toturnthisbottomupapproachintocode,weuseanarrayA0::n 1that
initiallyholdstheelementsoftheinputvectoraintheorderinwhichtheyappear

Pgina38

https://translate.googleusercontent.com/translate_f 26/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
30.3EfficientFFTimplementations 917

intheleavesofthetreeofFigure30.4.(Weshallshowlaterhowtodeterminethis
order,whichisknownasabitreversalpermutation.)Becausewehavetocombine
DFTsoneachlevelofthetree,weintroduceavariablestocountthelevels,ranging
from1(atthebottom,whenwearecombiningpairstoform2elementDFTs)
tolgn(atthetop,whenwearecombiningtwo.n=2/elementDFTstoproducethe
finalresult).Thealgorithmthereforehasthefollowingstructure:
1forsD1tolgn
2 forkD0ton1by2s
3 combinethetwo2s1elementDFTsin
Ak::kC2s1 1andAkC2s1::kC2s 1
intoone2selementDFTinAk::kC2s 1
Wecanexpressthebodyoftheloop(line3)asmoreprecisepseudocode.Nosotros
copytheforloopfromtheRECURSIVEFFTprocedure,identifyingy0with
Ak::kC2s1 1andy1withAkC2s1::kC2s 1.Thetwiddlefac
torusedineachbutterflyoperationdependsonthevalueofsitisapowerof!m,
wheremD2s.(Weintroducethevariablemsolelyforthesakeofreadability.)
Weintroduceanothertemporaryvariableuthatallowsustoperformthebutterfly
operationinplace.Whenwereplaceline3oftheoverallstructurebytheloop
body,wegetthefollowingpseudocode,whichformsthebasisoftheparallelim
plementationweshallpresentlater.Thecodefirstcallstheauxiliaryprocedure
BITREVERSECOPY.aA/tocopyvectoraintoarrayAintheinitialorderin
whichweneedthevalues.
ITERATIVEFFT.a/
1BITREVERSECOPY.aUN/
2nDa:length //nisapowerof2
3forsD1tolgn
4 mD2s
5 !mDe2i=m
6 forkD0ton1bym
7 !D1
8 forjD0tom=21
9 tD!AkCjCm=2
10 uDAkCj
11 AkCjDuCt
12 AkCjCm=2Dut
13 !D!!metro
14returnA
HowdoesBITREVERSECOPYgettheelementsoftheinputvectoraintothe
desiredorderinthearrayA?TheorderinwhichtheleavesappearinFigure30.4

Pgina39

918 Chapter30PolynomialsandtheFFT

isabitreversalpermutation.Thatis,ifweletrev.k/bethelgnbitinteger
formedbyreversingthebitsofthebinaryrepresentationofk,thenwewantto
placevectorelementakinarraypositionArev.k/.InFigure30.4,forexam
ple,theleavesappearintheorder04261537thissequenceinbinaryis
000100010110001101011111,andwhenwereversethebitsofeachvalue
wegetthesequence000001010011100101110111.Toseethatwewanta
bitreversalpermutationingeneral,wenotethatatthetoplevelofthetree,indices
whoseloworderbitis0gointotheleftsubtreeandindiceswhoseloworderbit
is1gointotherightsubtree.Strippingofftheloworderbitateachlevel,wecon
tinuethisprocessdownthetree,untilwegettheordergivenbythebitreversal
permutationattheleaves.
Sincewecaneasilycomputethefunctionrev.k/,theBITREVERSECOPYpro
cedureissimple:

BITREVERSECOPY.aUN/
1nDa:length
2forkD0ton1
3 Arev.k/Dak

https://translate.googleusercontent.com/translate_f 27/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
TheiterativeFFTimplementationrunsintime.nlgn/.ThecalltoB
REVERSE IT
COPY.aA/certainlyrunsinO.nlgn/time,sinceweiteratentimes
andcanreverseanintegerbetween0andn 1,withlgnbits,inO.lgn/time.
(Inpractice,becauseweusuallyknowtheinitialvalueofninadvance,wewould
probablycodeatablemappingktorev.k/,makingBITREVERSECOPYrunin
.n/timewithalowhiddenconstant.Alternatively,wecouldusethecleveramor
tizedreversebinarycounterschemedescribedinProblem171.)Tocompletethe
proofthatITERATIVEFFTrunsintime.nlgn/,weshowthatLn/,thenumber
oftimesthebodyoftheinnermostloop(lines813)executes,is.nlgn/.los
forloopoflines613iteratesn=mDn=2stimesforeachvalueofs,andthe
innermostloopoflines813iteratesm=2D2s1times.Thus,
xlgn
norte
Ln/D 2s1
2s
Sd1
xlgn norte
re
2
Sd1
D.nlgn/:

Pgina40

30.3EfficientFFTimplementations 919

a 0 y 0

!2
0

un 1 y 1

!4
0

un 2 y 2

!2
0 !4
1

a 3 y 3

!8
0

a 4 y 4

!2
0 !8
1
5
y 5

!4
0 !8
2

un 6 y 6

!2
0 !4
1 !8
3

a 7 y 7

stagesD1 stagesD2 stagesD3

Figure30.5AcircuitthatcomputestheFFTinparallel,hereshownonnD8inputs.Cada
butterflyoperationtakesasinputthevaluesontwowires,alongwithatwiddlefactor,anditproduces
asoutputsthevaluesontwowires.Thestagesofbutterfliesarelabeledtocorrespondtoiterations
oftheoutermostloopoftheITERATIVEFFTprocedure.Onlythetopandbottomwirespassing
throughabutterflyinteractwithitwiresthatpassthroughthemiddleofabutterflydonotaffect
thatbutterfly,noraretheirvalueschangedbythatbutterfly.Forexample,thetopbutterflyinstage2
hasnothingtodowithwire1(thewirewhoseoutputislabeledy1)itsinputsandoutputsareonly
onwires0and2(labeledy0andy2,respectively).Thiscircuithasdepth.lgn/andperforms
.nlgn/butterflyoperationsaltogether.

AparallelFFTcircuit
Wecanexploitmanyofthepropertiesthatallowedustoimplementanefficient
iterativeFFTalgorithmtoproduceanefficientparallelalgorithmfortheFFT.Nosotros
willexpresstheparallelFFTalgorithmasacircuit.Figure30.5showsaparallel
FFTcircuit,whichcomputestheFFTonninputs,fornD8.Thecircuitbegins
withabitreversepermutationoftheinputs,followedbylgnstages,eachstage
consistingofn=2butterfliesexecutedinparallel.Thedepthofthecircuitthe
maximumnumberofcomputationalelementsbetweenanyoutputandanyinput
thatcanreachitistherefore.lgn/.
TheleftmostpartoftheparallelFFTcircuitperformsthebitreversepermuta
tion,andtheremaindermimicstheiterativeITERATIVEFFTprocedure.Porque
eachiterationoftheoutermostforloopperformsn=2independentbutterflyopera
tions,thecircuitperformstheminparallel.Thevalueofsineachiterationwithin

https://translate.googleusercontent.com/translate_f 28/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,

Pgina41

920 Chapter30PolynomialsandtheFFT

ITERATIVEFFTcorrespondstoastageofbutterfliesshowninFigure30.5.por
sD12:::lgn,stagesconsistsofn=2sgroupsofbutterflies(correspondingto
eachvalueofkinITERATIVEFFT),with2s1butterfliespergroup(corresponding
toeachvalueofjinITERATIVEFFT).ThebutterfliesshowninFigure30.5corre
spondtothebutterflyoperationsoftheinnermostloop(lines912ofITERATIVE
FFT).Notealsothatthetwiddlefactorsusedinthebutterfliescorrespondtothose
usedinITERATIVEFFT:instages,weuse!0 ,wheremD2s.
m!1
m:::!m=21
metro

Exercises

30.31
ShowhowITERATIVEFFTcomputestheDFToftheinputvector.02314
579/.

30.32
ShowhowtoimplementanFFTalgorithmwiththebitreversalpermutationoccur
ringattheend,ratherthanatthebeginning,ofthecomputation.(Hint:Consider
theinverseDFT.)

30.33
HowmanytimesdoesITERATIVEFFTcomputetwiddlefactorsineachstage?
RewriteITERATIVEFFTtocomputetwiddlefactorsonly2s1timesinstages.

30.34?
SupposethattheadderswithinthebutterflyoperationsoftheFFTcircuitsome
timesfailinsuchamannerthattheyalwaysproduceazerooutput,independent
oftheirinputs.Supposethatexactlyoneadderhasfailed,butthatyoudon'tknow
whichone.Describehowyoucanidentifythefailedadderbysupplyinginputsto
theoverallFFTcircuitandobservingtheoutputs.Howefficientisyourmethod?

Problemas

301Divideandconquermultiplication
a.ShowhowtomultiplytwolinearpolynomialsaxCbandcxCdusingonly
threemultiplications.(Hint:Oneofthemultiplicationsis.aCb/.cCd/.)

segundo.Givetwodivideandconqueralgorithmsformultiplyingtwopolynomialsof
degreeboundnin.nlg3/time.Thefirstalgorithmshoulddividetheinput
polynomialcoefficientsintoahighhalfandalowhalf,andthesecondalgorithm
shoulddividethemaccordingtowhethertheirindexisoddoreven.

Pgina42

ProblemsforChapter30 921

do.ShowhowtomultiplytwonbitintegersinOnlg3/steps,whereeachstep
operatesonatmostaconstantnumberof1bitvalues.

302Toeplitzmatrices
AToeplitzmatrixisann nmatrixAD.aij/suchthataijDai1j1for
iD23:::nandjD23:::norte.

a.IsthesumoftwoToeplitzmatricesnecessarilyToeplitz?Whatabouttheprod
uct?

segundo.DescribehowtorepresentaToeplitzmatrixsothatyoucanaddtwon norte
ToeplitzmatricesinOn/time.

https://translate.googleusercontent.com/translate_f 29/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
do.GiveanO.nlgn/timealgorithmformultiplyingannnToeplitzmatrixbya
vectoroflengthn.Useyourrepresentationfrompart(b).

re.GiveanefficientalgorithmformultiplyingtwonnToeplitzmatrices.Analizar
itsrunningtime.

303MultidimensionalfastFouriertransform
Wecangeneralizethe1dimensionaldiscreteFouriertransformdefinedbyequa
tion(30.8)toddimensions.TheinputisaddimensionalarrayAD.aj j :::j / 1 2 d

whosedimensionsaren1n2:::nd,wheren1n2 ndDn.Wedefinethe
ddimensionaldiscreteFouriertransformbytheequation
x 1 n
n x 1
1 2 x 1
n d

yk k :::k re aj j :::j !j k !j k !j k
n n n
1 2 d 1 2 d 1 1 2 2 d d
1 2 d

j D0j D0 j D0
1 2 d

for0k1<n1,0k2<n2,...,0kd<nd.

a.ShowthatwecancomputeaddimensionalDFTbycomputing1dimensional
DFTsoneachdimensioninturn.Thatis,wefirstcomputen=n1separate
1dimensionalDFTsalongdimension1.Then,usingtheresultoftheDFTs
alongdimension1astheinput,wecomputen=n2separate1dimensionalDFTs
alongdimension2.Usingthisresultastheinput,wecomputen=n3separate
1dimensionalDFTsalongdimension3,andsoon,throughdimensiond.

segundo.Showthattheorderingofdimensionsdoesnotmatter,sothatwecancompute
addimensionalDFTbycomputingthe1dimensionalDFTsinanyorderof
theddimensions.

Pgina43

922 Chapter30PolynomialsandtheFFT

do.Showthatifwecomputeeach1dimensionalDFTbycomputingthefastFour
iertransform,thetotaltimetocomputeaddimensionalDFTisO.nlgn/,
independentofd.

304Evaluatingallderivativesofapolynomialatapoint
"
GivenapolynomialAx/ofdegreeboundn,wedefineitstthderivativeby

Ax/ iftD0
A.t/.x/D re A.t1/.x/if1tn1
dx
0 ift n:

Fromthecoefficientrepresentation.a0a1:::an1/ofAx/andagivenpointx0,
wewishtodetermineA.t/.x0/fortD01:::n1.

a.Givencoefficientsb0b1:::bn1suchthat
xN1
Ax/D bj.xx0/j
jD0

showhowtocomputeA.t/.x0/,fortD01:::n1,inOn/time.

segundo.Explainhowtofindb0b1:::bn1inO.nlgn/time,givenAx0C!K
n/for
kD01:::n1.

do.Provethat
!
xN1 xN1
!Krnorte
Ax0C!K f.j/grj/
n/D r
rD0 jD0

wheref.j/Daj jand
(
xl
gl/D 0=.l/if.n1/l0
0 if1ln1:
https://translate.googleusercontent.com/translate_f 30/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,

re.ExplainhowtoevaluateAx0C!K 1inO.nlgn/
n/forkD01:::norte
hora.ConcludethatwecanevaluateallnontrivialderivativesofAx/atx0in
O.nlgn/time.

Pgina44

ProblemsforChapter30 923

305Polynomialevaluationatmultiplepoints
Wehaveseenhowtoevaluateapolynomialofdegreeboundnatasinglepointin
On/timeusingHorner'srule.Wehavealsodiscoveredhowtoevaluatesucha
polynomialatallncomplexrootsofunityinO.nlgn/timeusingtheFFT.Nosotros
shallnowshowhowtoevaluateapolynomialofdegreeboundnatnarbitrary
pointsinO.nlg2n/time.
Todoso,weshallassumethatwecancomputethepolynomialremainderwhen
onesuchpolynomialisdividedbyanotherinO.nlgn/time,aresultthatwestate
withoutproof.Forexample,theremainderof3x3Cx2 3xC1whendividedby
x2CxC2is
.3x3Cx2 3xC1/mod.x2CxC2/D7xC5:
Pn1
GiventhecoefficientrepresentationofapolynomialAx/D
kD0akxkand
npointsx0x1:::xn1,wewishtocomputethenvaluesAx0/Ax1/:::
Qj
Axn1/.For0ijn1,definethepolynomialsPij.x/D
kDi.xxk/
andQij.x/DAx/modPij.x/.NotethatQij.x/hasdegreeatmostj yo.

a.ProvethatAx/mod.x/DA./foranypoint.

segundo.ProvethatQkk.x/DAxk/andthatQ0n1.x/DAx/.

do.Provethatforikj,wehaveQik.x/DQij.x/modPik.x/and
Qkj.x/DQij.x/modPkj.x/.

re.GiveanO.nlg2n/timealgorithmtoevaluateAx0/Ax1/:::Axn1/.

306FFTusingmodulararithmetic
Asdefined,thediscreteFouriertransformrequiresustocomputewithcomplex
numbers,whichcanresultinalossofprecisionduetoroundofferrors.Paraalgunos
problems,theanswerisknowntocontainonlyintegers,andbyusingavariantof
theFFTbasedonmodulararithmetic,wecanguaranteethattheansweriscalcu
latedexactly.Anexampleofsuchaproblemisthatofmultiplyingtwopolynomials
withintegercoefficients.Exercise30.26givesoneapproach,usingamodulusof
length.n/bitstohandleaDFTonnpoints.Thisproblemgivesanotherap
proach,whichusesamodulusofthemorereasonablelengthO.lgn/itrequires
thatyouunderstandthematerialofChapter31.Letnbeapowerof2.

a.SupposethatwesearchforthesmallestksuchthatpDknC1isprime.Dar
asimpleheuristicargumentwhywemightexpectktobeapproximatelylnn.
(Thevalueofkmightbemuchlargerorsmaller,butwecanreasonablyexpect
toexamineO.lgn/candidatevaluesofkonaverage.)Howdoestheexpected
lengthofpcomparetothelengthofn?

Pgina45

924 Chapter30PolynomialsandtheFFT

LetgbeageneratorofZp,andletwDgkmodp.

segundo.ArguethattheDFTandtheinverseDFTarewelldefinedinverseoperations
modulop,wherewisusedasaprincipalnthrootofunity.

do.ShowhowtomaketheFFTanditsinverseworkmodulopintimeO.nlgn/,
whereoperationsonwordsofO.lgn/bitstakeunittime.Assumethatthe
https://translate.googleusercontent.com/translate_f 31/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
algorithmisgivenpandw.

re.ComputetheDFTmodulopD17ofthevector.05377216/.Tengaencuentaque
gD3isageneratorofZ17.

Chapternotes

VanLoan'sbook[343]providesanoutstandingtreatmentofthefastFouriertrans
formar.Press,Teukolsky,Vetterling,andFlannery[283,284]haveagooddescrip
tionofthefastFouriertransformanditsapplications.Foranexcellentintroduction
tosignalprocessing,apopularFFTapplicationarea,seethetextsbyOppenheim
andSchafer[266]andOppenheimandWillsky[267].TheOppenheimandSchafer
bookalsoshowshowtohandlecasesinwhichnisnotanintegerpowerof2.
Fourieranalysisisnotlimitedto1dimensionaldata.Itiswidelyusedinimage
processingtoanalyzedatain2ormoredimensions.ThebooksbyGonzalezand
Woods[146]andPratt[281]discussmultidimensionalFouriertransformsandtheir
useinimageprocessing,andbooksbyTolimieri,An,andLu[338]andVanLoan
[343]discussthemathematicsofmultidimensionalfastFouriertransforms.
CooleyandTukey[76]arewidelycreditedwithdevisingtheFFTinthe1960s.
TheFFThadinfactbeendiscoveredmanytimespreviously,butitsimportancewas
notfullyrealizedbeforetheadventofmoderndigitalcomputers.AlthoughPress,
Teukolsky,Vetterling,andFlanneryattributetheoriginsofthemethodtoRunge
andKnigin1924,anarticlebyHeideman,Johnson,andBurrus[163]tracesthe
historyoftheFFTasfarbackasCFGaussin1805.
FrigoandJohnson[117]developedafastandflexibleimplementationofthe
FFT,calledFFTW(fastestFouriertransformintheWest).FFTWisdesignedfor
situationsrequiringmultipleDFTcomputationsonthesameproblemsize.antesde
actuallycomputingtheDFTs,FFTWexecutesaplanner,which,byaseriesof
trialruns,determineshowbesttodecomposetheFFTcomputationforthegiven
problemsizeonthehostmachine.FFTWadaptstousethehardwarecacheef
ficiently,andoncesubproblemsaresmallenough,FFTWsolvesthemwithopti
mized,straightlinecode.Furthermore,FFTWhastheunusualadvantageoftaking
.nlgn/timeforanyproblemsizen,evenwhennisalargeprime.

Pgina46

NotesforChapter30 925

AlthoughthestandardFouriertransformassumesthattheinputrepresentspoints
thatareuniformlyspacedinthetimedomain,othertechniquescanapproximatethe
FFTonnonequispaceddata.ThearticlebyWare[348]providesanoverview.

https://translate.googleusercontent.com/translate_f 32/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,

Pgina47

31 NumberTheoreticAlgorithms

Numbertheorywasonceviewedasabeautifulbutlargelyuselesssubjectinpure
matemticas.Todaynumbertheoreticalgorithmsareusedwidely,dueinlargepart
totheinventionofcryptographicschemesbasedonlargeprimenumbers.Estas
schemesarefeasiblebecausewecanfindlargeprimeseasily,andtheyaresecure
becausewedonotknowhowtofactortheproductoflargeprimes(orsolverelated
problems,suchascomputingdiscretelogarithms)efficiently.Thischapterpresents
someofthenumbertheoryandrelatedalgorithmsthatunderliesuchapplications.
Section31.1introducesbasicconceptsofnumbertheory,suchasdivisibility,
modularequivalence,anduniquefactorization.Section31.2studiesoneofthe
world'soldestalgorithms:Euclid'salgorithmforcomputingthegreatestcommon
divisoroftwointegers.Section31.3reviewsconceptsofmodulararithmetic.Sec
tion31.4thenstudiesthesetofmultiplesofagivennumbera,modulon,andshows
howtofindallsolutionstotheequationaxb.modn/byusingEuclid'salgo
rithm.TheChineseremaindertheoremispresentedinSection31.5.Section31.6
considerspowersofagivennumbera,modulon,andpresentsarepeatedsquaring
algorithmforefficientlycomputingabmodn,givena,b,andn.Thisoperationis
attheheartofefficientprimalitytestingandofmuchmoderncryptography.Sec
tion31.7thendescribestheRSApublickeycryptosystem.Section31.8examines
arandomizedprimalitytest.Wecanusethistesttofindlargeprimesefficiently,
whichweneedtodoinordertocreatekeysfortheRSAcryptosystem.Finalmente,
Section31.9reviewsasimplebuteffectiveheuristicforfactoringsmallintegers.Eso
isacuriousfactthatfactoringisoneproblempeoplemaywishtobeintractable,
sincethesecurityofRSAdependsonthedifficultyoffactoringlargeintegers.

Sizeofinputsandcostofarithmeticcomputations
Becauseweshallbeworkingwithlargeintegers,weneedtoadjusthowwethink
aboutthesizeofaninputandaboutthecostofelementaryarithmeticoperations.
Inthischapter,alargeinputtypicallymeansaninputcontaininglargein
tegersratherthananinputcontainingmanyintegers(asforsorting).As,

Pgina48

31.1Elementarynumbertheoreticnotions 927

https://translate.googleusercontent.com/translate_f 33/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,

weshallmeasurethesizeofaninputintermsofthenumberofbitsrequiredto
representthatinput,notjustthenumberofintegersintheinput.Analgorithm
withintegerinputsa1a2:::akisapolynomialtimealgorithmifitrunsintime
polynomialinlga1lga2:::lgak,thatis,polynomialinthelengthsofitsbinary
encodedinputs.
Inmostofthisbook,wehavefounditconvenienttothinkoftheelemen
taryarithmeticoperations(multiplications,divisions,orcomputingremainders)
asprimitiveoperationsthattakeoneunitoftime.Bycountingthenumberofsuch
arithmeticoperationsthatanalgorithmperforms,wehaveabasisformakinga
reasonableestimateofthealgorithm'sactualrunningtimeonacomputer.Elemen
taryoperationscanbetimeconsuming,however,whentheirinputsarelarge.Eso
thusbecomesconvenienttomeasurehowmanybitoperationsanumbertheoretic
algorithmrequires.Inthismodel,multiplyingtwobitintegersbytheordinary
methoduses.2/bitoperations.Similarly,wecandivideabitintegerbya
shorterintegerortaketheremainderofabitintegerwhendividedbyashorterin
tegerintime.2/bysimplealgorithms.(SeeExercise31.112.)Fastermethods
areknown.Forexample,asimpledivideandconquermethodformultiplyingtwo
bitintegershasarunningtimeof.lg3/,andthefastestknownmethodhas
arunningtimeof.lglglg/.Forpracticalpurposes,however,the.2/
algorithmisoftenbest,andweshallusethisboundasabasisforouranalyses.
Weshallgenerallyanalyzealgorithmsinthischapterintermsofboththenumber
ofarithmeticoperationsandthenumberofbitoperationstheyrequire.

31.1Elementarynumbertheoreticnotions

Thissectionprovidesabriefreviewofnotionsfromelementarynumbertheory
concerningthesetZDf:::21012:::gofintegersandthesetND
f012:::gofnaturalnumbers.

Divisibilityanddivisors
Thenotionofoneintegerbeingdivisiblebyanotheriskeytothetheoryofnumbers.
Thenotationdja(readddividesa)meansthataDkdforsomeintegerk.
Everyintegerdivides0.Ifa>0anddja,thenjdjjaj.Ifdja,thenwealso
saythataisamultipleofd.Ifddoesnotdividea,wewriteda.
Ifdjaandd 0,wesaythatdisadivisorofa.Notethatdjaifandonly
ifdja,sothatnogeneralityislostbydefiningthedivisorstobenonnegative,
withtheunderstandingthatthenegativeofanydivisorofaalsodividesa.UN

Pgina49

928 Chapter31NumberTheoreticAlgorithms

divisorofanonzerointegeraisatleast1butnotgreaterthanjaj.Porejemplo,el
divisorsof24are1,2,3,4,6,8,12,and24.
Everypositiveintegeraisdivisiblebythetrivialdivisors1anda.Thenontrivial
divisorsofaarethefactorsofa.Forexample,thefactorsof20are2,4,5,and10.

Primeandcompositenumbers
Anintegera>1whoseonlydivisorsarethetrivialdivisors1andaisaprime
numberor,moresimply,aprime.Primeshavemanyspecialpropertiesandplaya
criticalroleinnumbertheory.Thefirst20primes,inorder,are
235711131719232931374143475359616771:
Exercise31.12asksyoutoprovethatthereareinfinitelymanyprimes.Aninteger
a>1thatisnotprimeisacompositenumberor,moresimply,acomposite.por
example,39iscompositebecause3j39.Wecalltheinteger1aunit,anditis
neitherprimenorcomposite.Similarly,theinteger0andallnegativeintegersare
neitherprimenorcomposite.

Thedivisiontheorem,remainders,andmodularequivalence
Givenanintegern,wecanpartitiontheintegersintothosethataremultiplesofn

https://translate.googleusercontent.com/translate_f 34/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
andthosethatarenotmultiplesofn.Muchnumbertheoryisbaseduponrefining
thispartitionbyclassifyingthenonmultiplesofnaccordingtotheirremainders
whendividedbyn.Thefollowingtheoremprovidesthebasisforthisrefinement.
Weomittheproof(butsee,forexample,NivenandZuckerman[265]).

Theorem31.1(Divisiontheorem)
Foranyintegeraandanypositiveintegern,thereexistuniqueintegersqandr
suchthat0r<nandaDqnCr.

ThevalueqDba=ncisthequotientofthedivision.ThevaluerDamodn
istheremainder(orresidue)ofthedivision.Wehavethatnjaifandonlyif
amodnD0.
Wecanpartitiontheintegersintonequivalenceclassesaccordingtotheirre
maindersmodulon.Theequivalenceclassmoduloncontaininganintegerais
anDfaCknWk2Zg:

Forexample,37Df:::11431017:::gwecanalsodenotethissetby
47and107.Usingthenotationdefinedonpage54,wecansaythatwriting
a2bnisthesameaswritingab.modn/.Thesetofallsuchequivalence
classesis

Pgina50

31.1Elementarynumbertheoreticnotions 929

ZnDfanW0an1g: (31.1)
Whenyouseethedefinition
ZnDf01:::n1g (31.2)
youshouldreaditasequivalenttoequation(31.1)withtheunderstandingthat0
represents0n,1represents1n,andsooneachclassisrepresentedbyitssmallest
nonnegativeelement.Youshouldkeeptheunderlyingequivalenceclassesinmind,
sinembargo.Forexample,ifwereferto1asamemberofZn,wearereallyreferring
ton1n,since1n1.modn/.

Commondivisorsandgreatestcommondivisors
Ifdisadivisorofaanddisalsoadivisorofb,thendisacommondivisorofa
andb.Forexample,thedivisorsof30are1,2,3,5,6,10,15,and30,andsothe
commondivisorsof24and30are1,2,3,and6.Notethat1isacommondivisor
ofanytwointegers.
Animportantpropertyofcommondivisorsisthat
djaanddjbimpliesdj.aCb/anddj.ab/: (31.3)
Moregenerally,wehavethat
djaanddjbimpliesdj.axCby/ (31.4)
foranyintegersxandy.Also,ifajb,theneitherjajjbjorbD0,which
impliesthat
ajbandbjaimpliesaDb: (31.5)
Thegreatestcommondivisoroftwointegersaandb,notbothzero,isthe
largestofthecommondivisorsofaandbwedenoteitbygcd.ab/.Porejemplo,
gcd.2430/D6,gcd.57/D1,andgcd.09/D9.Ifaandbarebothnonzero,
thengcd.ab/isanintegerbetween1andmin.jajjbj/.Wedefinegcd.00/to
be0thisdefinitionisnecessarytomakestandardpropertiesofthegcdfunction
(suchasequation(31.9)below)universallyvalid.
Thefollowingareelementarypropertiesofthegcdfunction:
gcd.ab/ Dgcd.ba/ (31.6)
gcd.ab/ Dgcd.ab/ (31.7)
gcd.ab/ Dgcd.jajjbj/ (31.8)
gcd.a0/ Djaj (31.9)
gcd.aka/Djaj foranyk2Z: (31.10)
Thefollowingtheoremprovidesanalternativeandusefulcharacterizationof
gcd.ab/.

https://translate.googleusercontent.com/translate_f 35/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,

Pgina51

930 Chapter31NumberTheoreticAlgorithms

Theorem31.2
Ifaandbareanyintegers,notbothzero,thengcd.ab/isthesmallestpositive
elementofthesetfaxCbyWxy2Zgoflinearcombinationsofaandb.

ProofLetsbethesmallestpositivesuchlinearcombinationofaandb,andlet
sDaxCbyforsomexy2Z.LetqDba=sc.Equation(3.8)thenimplies
amodsDaqs
Daq.axCby/
Da.1qx/Cb.qy/

andsoamodsisalinearcombinationofaandbaswell.But,since0
amods<s,wehavethatamodsD0,becausesisthesmallestpositivesuchlin
earcombination.Therefore,wehavethatsjaand,byanalogousreasoning,sjb.
Thus,sisacommondivisorofaandb,andsogcd.ab/ SEquation(31.4)
impliesthatgcd.ab/js,sincegcd.ab/dividesbothaandbandsisalinear
combinationofaandb.Butgcd.ab/jsands>0implythatgcd.ab/s.
Combininggcd.ab/ sandgcd.ab/syieldsgcd.ab/Ds.Weconclude
thatsisthegreatestcommondivisorofaandb.

Corollary31.3
Foranyintegersaandb,ifdjaanddjb,thendjgcd.ab/.

ProofThiscorollaryfollowsfromequation(31.4),becausegcd.ab/isalinear
combinationofaandbbyTheorem31.2.

Corollary31.4
Forallintegersaandbandanynonnegativeintegern,
gcd.anbn/Dngcd.ab/:

ProofIfnD0,thecorollaryistrivial.Ifn>0,thengcd.anbn/isthesmallest
positiveelementofthesetfanxCbnyWxy2Zg,whichisntimesthesmallest
positiveelementofthesetfaxCbyWxy2Zg.

Corollary31.5
Forallpositiveintegersn,a,andb,ifnjabandgcd.an/D1,thennjb.

ProofWeleavetheproofasExercise31.15.

Pgina52

31.1Elementarynumbertheoreticnotions 931

Relativelyprimeintegers
Twointegersaandbarerelativelyprimeiftheironlycommondivisoris1,that
is,ifgcd.ab/D1.Forexample,8and15arerelativelyprime,sincethedivisors
of8are1,2,4,and8,andthedivisorsof15are1,3,5,and15.Thefollowing
theoremstatesthatiftwointegersareeachrelativelyprimetoanintegerp,then
theirproductisrelativelyprimetop.

Theorem31.6
Foranyintegersa,b,andp,ifbothgcd.ap/D1andgcd.bp/D1,then
gcd.abp/D1.

ProofItfollowsfromTheorem31.2thatthereexistintegersx,y,x0,andy0such
ese

https://translate.googleusercontent.com/translate_f 36/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
axCpy
bx0Cpy0 D1
D1:
Multiplyingtheseequationsandrearranging,wehave
ab.xx0/Cp.ybx0Cy0axCpyy0/D1:
Since1isthusapositivelinearcombinationofabandp,anappealtoTheo
rem31.2completestheproof.

Integersn1,n2,...,nkarepairwiserelativelyprimeif,wheneverij,we
havegcd.ninj/D1.

Uniquefactorization
Anelementarybutimportantfactaboutdivisibilitybyprimesisthefollowing.

Theorem31.7
Forallprimespandallintegersaandb,ifpjab,thenpjaorpjb(orboth).

ProofAssumeforthepurposeofcontradictionthatpjab,butthatpaand
pb.Thus,gcd.ap/D1andgcd.bp/D1,sincetheonlydivisorsofpare1
andp,andweassumethatpdividesneitheranorb.Theorem31.6thenimplies
thatgcd.abp/D1,contradictingourassumptionthatpjab,sincepjab
impliesgcd.abp/Dp.Thiscontradictioncompletestheproof.

AconsequenceofTheorem31.7isthatwecanuniquelyfactoranycomposite
integerintoaproductofprimes.

Pgina53

932 Chapter31NumberTheoreticAlgorithms

Theorem31.8(Uniquefactorization)
Thereisexactlyonewaytowriteanycompositeintegeraasaproductoftheform
aDp e e
1 2
pe
1p 2 R
r

wherethepiareprime,p1<p2< <pr,andtheeiarepositiveintegers.

ProofWeleavetheproofasExercise31.111.

Asanexample,thenumber6000isuniquelyfactoredintoprimesas24 353.

Exercises

31.11
Provethatifa>b>0andcDaCb,thencmodaDb.

31.12
Provethatthereareinfinitelymanyprimes.(Hint:Showthatnoneoftheprimes
p1p2:::pkdivide.p1p2 pk/C1.)

31.13
Provethatifajbandbjc,thenajc.

31.14
Provethatifpisprimeand0<k<p,thengcd.kp/D1.

31.15
ProveCorollary31.5.

31.16
Provethatifpisprimeand0<k<p,thenpj pag
.Concludethatforallintegers
K
aandbandallprimesp,
.aCb/papCbp .modp/:

31.17
Provethatifaandbareanypositiveintegerssuchthatajb,then
.xmodb/modaDxmoda
https://translate.googleusercontent.com/translate_f 37/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,

foranyx.Prove,underthesameassumptions,that
xy.modb/impliesxy.moda/
foranyintegersxandy.

Pgina54

31.2Greatestcommondivisor 933

31.18
Foranyintegerk>0,anintegernisakthpowerifthereexistsanintegerasuch
thatakDn.Furthermore,n>1isanontrivialpowerifitisakthpowerfor
someintegerk>1.Showhowtodeterminewhetheragivenbitintegernisa
nontrivialpowerintimepolynomialin.

31.19
Proveequations(31.6)(31.10).

31.110
Showthatthegcdoperatorisassociative.Thatis,provethatforallintegersa,b,
andc,
gcd.agcd.bc//Dgcd.gcd.ab/c/:

31.111?
ProveTheorem31.8.

31.112
Giveefficientalgorithmsfortheoperationsofdividingabitintegerbyashorter
integerandoftakingtheremainderofabitintegerwhendividedbyashorter
integer.Youralgorithmsshouldrunintime.2/.

31.113
Giveanefficientalgorithmtoconvertagivenbit(binary)integertoadecimal
representacin.Arguethatifmultiplicationordivisionofintegerswhoselength
isatmosttakestimeM./,thenwecanconvertbinarytodecimalintime
.M./lg/.(Hint:Useadivideandconquerapproach,obtainingthetopand
bottomhalvesoftheresultwithseparaterecursions.)

31.2Greatestcommondivisor

Inthissection,wedescribeEuclid'salgorithmforefficientlycomputingthegreat
estcommondivisoroftwointegers.Whenweanalyzetherunningtime,weshall
seeasurprisingconnectionwiththeFibonaccinumbers,whichyieldaworstcase
inputforEuclid'salgorithm.
Werestrictourselvesinthissectiontononnegativeintegers.Thisrestrictionis
justifiedbyequation(31.8),whichstatesthatgcd.ab/Dgcd.jajjbj/.

Pgina55

934 Chapter31NumberTheoreticAlgorithms

Inprinciple,wecancomputegcd.ab/forpositiveintegersaandbfromthe
primefactorizationsofaandb.Indeed,if
aDp e e
1 2
pR
e (31.11)
1p 2 r

bDp f f
1 2
pf (31.12)
1p 2 R
r

withzeroexponentsbeingusedtomakethesetofprimesp1p2:::prthesame
forbothaandb,then,asExercise31.21asksyoutoshow,

https://translate.googleusercontent.com/translate_f 38/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
gcd.ab/Dpmin.e
1 f / p2min.e f /
1 1 2 2 pR
min.e f / :
r r (31.13)
AsweshallshowinSection31.9,however,thebestalgorithmstodateforfactoring
donotruninpolynomialtime.Thus,thisapproachtocomputinggreatestcommon
divisorsseemsunlikelytoyieldanefficientalgorithm.
Euclid'salgorithmforcomputinggreatestcommondivisorsreliesonthefollow
ingtheorem.

Theorem31.9(GCDrecursiontheorem)
Foranynonnegativeintegeraandanypositiveintegerb,

gcd.ab/Dgcd.bamodb/:

ProofWeshallshowthatgcd.ab/andgcd.bamodb/divideeachother,so
thatbyequation(31.5)theymustbeequal(sincetheyarebothnonnegative).
Wefirstshowthatgcd.ab/jgcd.bamodb/.IfweletdDgcd.ab/,then
djaanddjb.Byequation(3.8),amodbDa qb,whereqDba=bc.
Sinceamodbisthusalinearcombinationofaandb,equation(31.4)impliesthat
dj.amodb/.Therefore,sincedjbanddj.amodb/,Corollary31.3implies
thatdjgcd.bamodb/or,equivalently,that
gcd.ab/jgcd.bamodb/: (31.14)

Showingthatgcd.bamodb/jgcd.ab/isalmostthesame.Ifwenowlet
dDgcd.bamodb/,thendjbanddj.amodb/.SinceaDqbC.amodb/,
whereqDba=bc,wehavethataisalinearcombinationofband.amodb/.Por
equation(31.4),weconcludethatdja.Sincedjbanddja,wehavethat
djgcd.ab/byCorollary31.3or,equivalently,that
gcd.bamodb/jgcd.ab/: (31.15)
Usingequation(31.5)tocombineequations(31.14)and(31.15)completesthe
prueba.

Pgina56

31.2Greatestcommondivisor 935

Euclid'salgorithm
TheElementsofEuclid(circa300B.C.)describesthefollowinggcdalgorithm,
althoughitmaybeofevenearlierorigin.WeexpressEuclid'salgorithmasa
recursiveprogrambaseddirectlyonTheorem31.9.Theinputsaandbarearbitrary
nonnegativeintegers.

EUCLID.ab/
1ifb==0
2 returna
3elsereturnEUCLID.bamodb/

AsanexampleoftherunningofEUCLID,considerthecomputationofgcd.3021/:
EUCLID.3021/DEUCLID.219/
DEUCLID.93
DEUCLID.30/
D3:
ThiscomputationcallsEUCLIDrecursivelythreetimes.
ThecorrectnessofEUCLIDfollowsfromTheorem31.9andthepropertythatif
thealgorithmreturnsainline2,thenbD0,sothatequation(31.9)impliesthat
gcd.ab/Dgcd.a0/Da.Thealgorithmcannotrecurseindefinitely,sincethe
secondargumentstrictlydecreasesineachrecursivecallandisalwaysnonnegative.
Therefore,EUCLIDalwaysterminateswiththecorrectanswer.

TherunningtimeofEuclid'salgorithm
WeanalyzetheworstcaserunningtimeofEUCLIDasafunctionofthesizeof
aandb.Weassumewithnolossofgeneralitythata>b 0.Tojustifythis

https://translate.googleusercontent.com/translate_f 39/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
assumption,observethatifb>a 0,thenEUCLID.ab/immediatelymakesthe
recursivecallEUCLID.ba/.Thatis,ifthefirstargumentislessthanthesecond
argument,EUCLIDspendsonerecursivecallswappingitsargumentsandthenpro
CeedsSimilarly,ifbDa>0,theprocedureterminatesafteronerecursivecall,
sinceamodbD0.
TheoverallrunningtimeofEUCLIDisproportionaltothenumberofrecursive
callsitmakes.OuranalysismakesuseoftheFibonaccinumbersFk,definedby
therecurrence(3.22).

Lemma31.10
Ifa>b 1andthecallEUCLID.ab/performsk 1recursivecalls,then
un FkC2andb FkC1.

Pgina57

936 Chapter31NumberTheoreticAlgorithms

ProofTheproofproceedsbyinductiononk.Forthebasisoftheinduction,let
kD1.Then,b 1DF2,andsincea>b,wemusthavea 2DF3.Desde
b>.amodb/,ineachrecursivecallthefirstargumentisstrictlylargerthanthe
secondtheassumptionthata>bthereforeholdsforeachrecursivecall.
Assumeinductivelythatthelemmaholdsifk1recursivecallsaremadenosotros
shallthenprovethatthelemmaholdsforkrecursivecalls.Sincek>0,wehave
b>0,andEUCLID.ab/callsEUCLID.bamodb/recursively,whichinturn
makesk1recursivecalls.Theinductivehypothesisthenimpliesthatb FkC1
(thusprovingpartofthelemma),andamodb Fk.Tenemos

bC.amodb/DbC.abba=bc/
a
sincea>b>0impliesba=bc1.Thus,
un bC.amodb/
FkC1CFk
DFkC2:

Thefollowingtheoremisanimmediatecorollaryofthislemma.

Theorem31.11(Lam'stheorem)
Foranyintegerk 1,ifa>b 1andb<FkC1,thenthecallEUCLID.ab/
makesfewerthankrecursivecalls.

WecanshowthattheupperboundofTheorem31.11isthebestpossibleby
showingthatthecallEUCLID.FkC1Fk/makesexactlyk 1recursivecalls
whenk 2.Weuseinductiononk.Forthebasecase,kD2,andthecall
EUCLID.F3F2/makesexactlyonerecursivecall,toEUCLID.10/.(Wehaveto
startatkD2,becausewhenkD1wedonothaveF2>F1.)Fortheinduc
tivestep,assumethatEUCLID.FkFk1/makesexactlyk2recursivecalls.por
k>2,wehaveFk>Fk1>0andFkC1DFkCFk1,andsobyExercise31.11,
wehaveFkC1modFkDFk1.Thus,wehave
gcd.FkC1Fk/Dgcd.FkFkC1modFk/
Dgcd.FkFk1/:
Therefore,thecallEUCLID.FkC1Fk/recursesonetimemorethanthecall
EUCLID.FkFk1/,orexactlyk 1times,meetingtheupperboundofTheo
rem31.11. pag pag
SinceFkisapproximatelyk= 5,whereisthegoldenratio.1C 5/=2de
finedbyequation(3.24),thenumberofrecursivecallsinEUCLIDisO.lgb/.(See

Pgina58

31.2Greatestcommondivisor 937

un segundo
ba=bc re x Y
https://translate.googleusercontent.com/translate_f 40/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
99 78 1 3 11 14
78 21 3 3 3 11
21 15 1 3 2 3
15 6 2 3 1 2
6 3 2 3 0 1
3 0 3 1 0

Figure31.1HowEXTENDEDEUCLIDcomputesgcd.9978/.Eachlineshowsonelevelofthe
recursion:thevaluesoftheinputsaandb,thecomputedvalueba=bc,andthevaluesd,x,andy
returned.Thetriple.dxy/returnedbecomesthetriple.d0x0y0/usedatthenexthigherlevel
ofrecursion.ThecallEXTENDEDEUCLID.9978/returns.31114/,sothatgcd.9978/D3D
99.11/C7814.

Exercise31.25foratighterbound.)Therefore,ifwecallEUCLIDontwobit
numbers,thenitperformsO./arithmeticoperationsandO.3/bitoperations
(assumingthatmultiplicationanddivisionofbitnumberstakeO.2/bitoper
ations).Problem312asksyoutoshowanO.2/boundonthenumberofbit
Operaciones.

TheextendedformofEuclid'salgorithm
WenowrewriteEuclid'salgorithmtocomputeadditionalusefulinformation.
Specifically,weextendthealgorithmtocomputetheintegercoefficientsxandy
suchthat
dDgcd.ab/DaxCby: (31.16)
Notethatxandymaybezeroornegative.Weshallfindthesecoefficientsuseful
laterforcomputingmodularmultiplicativeinverses.TheprocedureEXTENDED
EUCLIDtakesasinputapairofnonnegativeintegersandreturnsatripleofthe
form.dxy/thatsatisfiesequation(31.16).

EXTENDEDEUCLID.ab/
1ifb==0
2 return.a10/
3else.d0x0y0/DEXTENDEDEUCLID.bamodb/
4 .dxy/D.d0y0x0 ba=bcy0/
5 return.dxy/

Figure31.1illustrateshowEXTENDEDEUCLIDcomputesgcd.9978/.
TheEXTENDEDEUCLIDprocedureisavariationoftheEUCLIDprocedure.
Line1isequivalenttothetestb==0inline1ofEUCLID.IfbD0,then

Pgina59

938 Chapter31NumberTheoreticAlgorithms

EXTENDEDEUCLIDreturnsnotonlydDainline2,butalsothecoefficients
xD1andyD0,sothataDaxCby.Ifb0,EXTENDEDEUCLIDfirst
computes.d0x0y0/suchthatd0Dgcd.bamodb/and
d0Dbx0C.amodb/y0: (31.17)
AsforEUCLID,wehaveinthiscasedDgcd.ab/Dd0Dgcd.bamodb/.
ToobtainxandysuchthatdDaxCby,westartbyrewritingequation(31.17)
usingtheequationdDd0andequation(3.8):
dDbx0C.abba=bc/y0
Day0Cbx0 ba=bcy0/:
Thus,choosingxDy0andyDx0 ba=bcy0satisfiestheequationdDaxCby,
provingthecorrectnessofEXTENDEDEUCLID.
SincethenumberofrecursivecallsmadeinEUCLIDisequaltothenumber
ofrecursivecallsmadeinEXTENDEDEUCLID,therunningtimesofEUCLID
andEXTENDEDEUCLIDarethesame,towithinaconstantfactor.Thatis,for
a>b>0,thenumberofrecursivecallsisO.lgb/.

Exercises

31.21
Provethatequations(31.11)and(31.12)implyequation(31.13).

https://translate.googleusercontent.com/translate_f 41/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,

31.22
Computethevalues.dxy/thatthecallEXTENDEDEUCLID.899493/returns.

31.23
Provethatforallintegersa,k,andn,
gcd.an/Dgcd.aCknn/:

31.24
RewriteEUCLIDinaniterativeformthatusesonlyaconstantamountofmemory
(thatis,storesonlyaconstantnumberofintegervalues).

31.25
Ifa>b 0,showthatthecallEUCLID.ab/makesatmost1Clogbrecursive
Llamadas.Improvethisboundto1Clog.b=gcd.ab//.

31.26
WhatdoesEXTENDEDEUCLID.FkC1Fk/return?Proveyouranswercorrect.

Pgina60

31.3Modulararithmetic 939

31.27
Definethegcdfunctionformorethantwoargumentsbytherecursiveequation
gcd.a0a1:::an/Dgcd.a0gcd.a1a2:::an//.Showthatthegcdfunction
returnsthesameanswerindependentoftheorderinwhichitsargumentsarespeci
FiedAlsoshowhowtofindintegersx0x1:::xnsuchthatgcd.a0a1:::an/D
a0x0Ca1x1CCanxn.Showthatthenumberofdivisionsperformedbyyour
algorithmisOnClg.maxfa0a1:::ang//.

31.28
Definelcm.a1a2:::an/tobetheleastcommonmultipleofthenintegers
a1a2:::an,thatis,thesmallestnonnegativeintegerthatisamultipleofeachai.
Showhowtocomputelcm.a1a2:::an/efficientlyusingthe(twoargument)gcd
operationasasubroutine.

31.29
Provethatn1,n2,n3,andn4arepairwiserelativelyprimeifandonlyif
gcd.n1n2n3n4/Dgcd.n1n3n2n4/D1:
Moregenerally,showthatn1n2:::nkarepairwiserelativelyprimeifandonly
ifasetofdlgkepairsofnumbersderivedfromtheniarerelativelyprime.

31.3Modulararithmetic

Informally,wecanthinkofmodulararithmeticasarithmeticasusualoverthe
integers,exceptthatifweareworkingmodulon,theneveryresultxisreplaced
bytheelementoff01:::n1gthatisequivalenttox,modulon(thatis,xis
replacedbyxmodn).Thisinformalmodelsufficesifwesticktotheoperations
ofaddition,subtraction,andmultiplication.Amoreformalmodelformodular
arithmetic,whichwenowgive,isbestdescribedwithintheframeworkofgroup
teora.

Finitegroups
Agroup.S/isasetStogetherwithabinaryoperationdefinedonSfor
whichthefollowingpropertieshold:
1.Closure:Foralla,b2S,wehaveab2S.
2.Identity:Thereexistsanelemente2S,calledtheidentityofthegroup,such
thateaDaeDaforalla2S.
3.Associativity:Foralla,b,c2S,wehave.ab/cDa.bc/.

https://translate.googleusercontent.com/translate_f 42/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,

Pgina61

940 Chapter31NumberTheoreticAlgorithms

4.Inverses:Foreacha2S,thereexistsauniqueelementb2S,calledthe
inverseofa,suchthatabDbaDe.
Asanexample,considerthefamiliargroup.ZC/oftheintegersZunderthe
operationofaddition:0istheidentity,andtheinverseofaisa.Ifagroup.S/
satisfiesthecommutativelawabDbaforallab2S,thenitisanabelian
group.Ifagroup.S/satisfiesjSj<1,thenitisafinitegroup.

Thegroupsdefinedbymodularadditionandmultiplication
Wecanformtwofiniteabeliangroupsbyusingadditionandmultiplicationmod
ulon,wherenisapositiveinteger.Thesegroupsarebasedontheequivalence
classesoftheintegersmodulon,definedinSection31.1.
TodefineagrouponZn,weneedtohavesuitablebinaryoperations,which
weobtainbyredefiningtheordinaryoperationsofadditionandmultiplication.
WecaneasilydefineadditionandmultiplicationoperationsforZn,becausethe
equivalenceclassoftwointegersuniquelydeterminestheequivalenceclassoftheir
sumorproduct.Thatis,ifaa0.modn/andbb0.modn/,then
aCba0Cb0.modn/
Ab a0b0 .modn/:

Thus,wedefineadditionandmultiplicationmodulon,denotedCnandn,by
anCnbn DaCbn (31.18)
annbn Dabn:

(WecandefinesubtractionsimilarlyonZnbyan nbnDabn,butdivi
sionismorecomplicated,asweshallsee.)Thesefactsjustifythecommonand
convenientpracticeofusingthesmallestnonnegativeelementofeachequivalence
classasitsrepresentativewhenperformingcomputationsinZn.Weadd,subtract,
andmultiplyasusualontherepresentatives,butwereplaceeachresultxbythe
representativeofitsclass,thatis,byxmodn.
Usingthisdefinitionofadditionmodulon,wedefinetheadditivegroup
modulonas.ZnCn/.ThesizeoftheadditivegroupmodulonisjZnjDn.
Figure31.2(a)givestheoperationtableforthegroup.Z6C6/.

Theorem31.12
Thesystem.ZnCn/isafiniteabeliangroup.

ProofEquation(31.18)showsthat.ZnCn/isclosed.Associativityandcom
mutativityofCnfollowfromtheassociativityandcommutativityofC:

Pgina62

31.3Modulararithmetic 941

+6 012345 15 1 2 4 7 8111314
0 012345 1 1 2 4 7 8111314
1 12345 0 2 2 4 8141 71113
2 2345 01 4 4 8 113214711
3 345 012 7 714134112 1 8
4 45 0123 8 8 1 211413147
5 5 01234 11 117142131 8 4
13 13117 1148 4 2
14 1413118 7 4 2 1
(un) (segundo)

Figure31.2Twofinitegroups.Equivalenceclassesaredenotedbytheirrepresentativeelements.
(a)Thegroup.Z6C6/.(b)Thegroup.Z1515/.

https://translate.googleusercontent.com/translate_f 43/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
.anCnbn/Cncn DaCbnCncn
D.aCb/Ccn
DaC.bCc/n
DanCnbCcn
DanCn.bnCncn/

anCnbn DaCbn
DbCan
DbnCnan:
Theidentityelementof.ZnCn/is0(thatis,0n).The(additive)inverseof
anelementa(thatis,ofan)istheelementa(thatis,anornan),since
anCnanDaanD0n.

Usingthedefinitionofmultiplicationmodulon,wedefinethemultiplicative
groupmodulonas.Znn/.TheelementsofthisgrouparethesetZnofelements
inZnthatarerelativelyprimeton,sothateachonehasauniqueinverse,modulon:
ZnDfan2ZnWgcd.an/D1g:

ToseethatZniswelldefined,notethatfor0a<n,wehavea.aCkn/
.modn/forallintegersk.ByExercise31.23,therefore,gcd.an/D1implies
gcd.aCknn/D1forallintegersk.SinceanDfaCknWk2Zg,thesetZn
iswelldefined.Anexampleofsuchagroupis
Z15Df12478111314g

Pgina63

942 Chapter31NumberTheoreticAlgorithms

wherethegroupoperationismultiplicationmodulo15.(Herewedenoteanel
ementa15asaforexample,wedenote715as7.)Figure31.2(b)showsthe
group.Z1515/.Forexample,81113.mod15/,workinginZ15.Theiden
tityforthisgroupis1.

Theorem31.13
Thesystem.Znn/isafiniteabeliangroup.

ProofTheorem31.6impliesthat.Znn/isclosed.Associativityandcommu
tativitycanbeprovedfornastheywereforCnintheproofofTheorem31.12.
Theidentityelementis1n.Toshowtheexistenceofinverses,letabeanelement
ofZnandlet.dxy/bereturnedbyEXTENDEDEUCLID.an/.Then,dD1,
sincea2Zn,and
axCnyD1 (31.19)
or,equivalently,
ax1.modn/:
Thus,xnisamultiplicativeinverseofan,modulon.Furthermore,weclaim
thatxn2Zn.Toseewhy,equation(31.19)demonstratesthatthesmallestpos
itivelinearcombinationofxandnmustbe1.Therefore,Theorem31.2implies
thatgcd.xn/D1.Wedefertheproofthatinversesareuniquelydefineduntil
Corollary31.26.

Asanexampleofcomputingmultiplicativeinverses,supposethataD5and
nD11.ThenEXTENDEDEUCLID.an/returns.dxy/D.121/,sothat
1D5.2/C111.Thus,211(ie,911)isthemultiplicativeinverseof511.
Whenworkingwiththegroups.ZnCn/and.Znn/intheremainderofthis
chapter,wefollowtheconvenientpracticeofdenotingequivalenceclassesbytheir
representativeelementsanddenotingtheoperationsCnandnbytheusualarith
meticnotationsCand(orjuxtaposition,sothatabDab)respectively.Also,
equivalencesmodulonmayalsobeinterpretedasequationsinZn.Porejemplo,
thefollowingtwostatementsareequivalent:
axb.modn/
annxn Dbn:
Asafurtherconvenience,wesometimesrefertoagroup.S/merelyasS
whentheoperationisunderstoodfromcontext.Wemaythusrefertothegroups
https://translate.googleusercontent.com/translate_f 44/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
.ZnCn/and.Znn/asZnandZn,respectively.
Wedenotethe(multiplicative)inverseofanelementaby.a1modn/.Divisin
inZnisdefinedbytheequationa=bab1.modn/.Forexample,inZ15

Pgina64

31.3Modulararithmetic 943

wehavethat7113.mod15/,since713D911.mod15/,sothat
4=74137.mod15/.
ThesizeofZnisdenoted.n/.Thisfunction,knownasEuler'sphifunction,
satisfiestheequation
UN UN
Y
1
.n/Dn 1 (31.20)
pag
pWpisprimeandpjn
sothatprunsoveralltheprimesdividingn(includingnitself,ifnisprime).
Weshallnotprovethisformulahere.Intuitively,webeginwithalistofthen
remaindersf01:::n1gandthen,foreachprimepthatdividesn,crossout
everymultipleofpinthelist.Forexample,sincetheprimedivisorsof45are3
and5, UN UN
1 1
.45/D45 1 1
UN3 UN 5
2 4
D45
3 5
D24:
Ifpisprime,thenZpDf12:::p1g,and
UN UN
1
.p/Dp 1
pag
Dp1: (31.21)
Ifniscomposite,then.n/<n1,althoughitcanbeshownthat
norte
.n/> (31.22)
elnlnnC3
lnlnn
forn 3,whereD0:5772156649:::isEuler'sconstant.Asomewhatsimpler
(butlooser)lowerboundforn>5is
norte
.n/> : (31.23)
6lnlnn
Thelowerbound(31.22)isessentiallythebestpossible,since
.n/
liminf De: (31.24)
n!1 n=lnlnn

Subgroups

If.S/isagroup,S0S,and.S0/isalsoagroup,then.S0/isasubgroup
of.S/.Forexample,theevenintegersformasubgroupoftheintegersunderthe
operationofaddition.Thefollowingtheoremprovidesausefultoolforrecognizing
subgroups.

Pgina65

944 Chapter31NumberTheoreticAlgorithms

Theorem31.14(Anonemptyclosedsubsetofafinitegroupisasubgroup)
If.S/isafinitegroupandS0isanynonemptysubsetofSsuchthatab2S0
forallab2S0,then.S0/isasubgroupof.S/.

ProofWeleavetheproofasExercise31.33.

Forexample,thesetf0246gformsasubgroupofZ8,sinceitisnonempty
andclosedundertheoperationC(thatis,itisclosedunderC8).
Thefollowingtheoremprovidesanextremelyusefulconstraintonthesizeofa
subgroupweomittheproof.
https://translate.googleusercontent.com/translate_f 45/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,

Theorem31.15(Lagrange'stheorem)
If.S/isafinitegroupand.S0/isasubgroupof.S/,thenjS0jisadivisor
ofjSj.

AsubgroupS0ofagroupSisapropersubgroupifS0S.Weshallusethe
followingcorollaryinouranalysisinSection31.8oftheMillerRabinprimality
testprocedure.

Corollary31.16
IfS0isapropersubgroupofafinitegroupS,thenjS0jjSj=2.

Subgroupsgeneratedbyanelement
Theorem31.14givesusaneasywaytoproduceasubgroupofafinitegroup.S/:
chooseanelementaandtakeallelementsthatcanbegeneratedfromausingthe
groupoperation.Specifically,definea.k/fork 1by
METRO
K
a.k/D aDaaa :
ID1
K
Forexample,ifwetakeaD2inthegroupZ6,thesequencea.1/a.2/a.3/:::is
240240240::::
InthegroupZn,wehavea.k/Dkamodn,andinthegroupZn,wehavea.k/D
akmodn.Wedefinethesubgroupgeneratedbya,denotedhaior.hai/,by
haiDfa.k/Wk 1g:
Wesaythatageneratesthesubgrouphaiorthataisageneratorofhai.SinceSis
finite,haiisafinitesubsetofS,possiblyincludingallofS.Sincetheassociativity
ofimplies

Pgina66

31.3Modulararithmetic 945

a.i/a.j/Da.iCj/
haiisclosedandtherefore,byTheorem31.14,haiisasubgroupofS.Forexample,
inZ6,wehave
h0iDf0g
h1iDf012345g
h2iDf024g:
Similarly,inZ7,wehave
h1iDf1g
h2iDf124g
h3iDf123456g:
Theorderofa(inthegroupS),denotedord.a/,isdefinedasthesmallestposi
tiveintegertsuchthata.t/De.

Theorem31.17
Foranyfinitegroup.S/andanya2S,theorderofaisequaltothesizeofthe
subgroupitgenerates,orord.a/Djhaij.

ProofLettDord.a/.Sincea.t/Deanda.tCk/Da.t/a.k/Da.k/for
K 1,ifi>t,thena.i/Da.j/forsomej<i.Thus,aswegenerateele
mentsbya,weseenonewelementsaftera.t/.Thus,haiDfa.1/a.2/:::a.t/g,
andsojhaijt.Toshowthatjhaijt,weshowthateachelementofthese
quencea.1/a.2/:::a.t/isdistinct.Supposeforthepurposeofcontradictionthat
a.i/Da.j/forsomeiandjsatisfying1i<jt.Then,a.iCk/Da.jCk/
fork 0.Butthisequalityimpliesthata.iC.tj//Da.jC.tj//De,acontradic
tion,sinceiCtj/<tbuttistheleastpositivevaluesuchthata.t/De.Ah
fore,eachelementofthesequencea.1/a.2/:::a.t/isdistinct,andjhaijt.Nosotros
concludethatord.a/Djhaij.

Corollary31.18
https://translate.googleusercontent.com/translate_f 46/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
Thesequencea.1/a.2/:::isperiodicwithperiodtDord.a/thatis,a.i/Da.j/
ifandonlyifij.modt/.

Consistentwiththeabovecorollary,wedefinea.0/aseanda.i/asa.imodt/,
wheretDord.a/,forallintegersi.

Corollary31.19
If.S/isafinitegroupwithidentitye,thenforalla2S,
a.jSj/De:

Pgina67

946 Chapter31NumberTheoreticAlgorithms

ProofLagrange'stheorem(Theorem31.15)impliesthatord.a/jjSj,andso
jSj0.modt/,wheretDord.a/.Therefore,a.jSj/Da.0/De.

Exercises

31.31
Drawthegroupoperationtablesforthegroups.Z4C4/and.Z55/.Muestraesa
thesegroupsareisomorphicbyexhibitingaonetoonecorrespondencebetween
theirelementssuchthataCbc.mod4/ifandonlyif.a/.b/.c/
.mod5/.

31.32
ListallsubgroupsofZ9andofZ13.

31.33
ProveTheorem31.14.

31.34
Showthatifpisprimeandeisapositiveinteger,then
.pe/Dpe1.p1/:

31.35
Showthatforanyintegern>1andforanya2Zn,thefunctionfaWZn!Zn
definedbyfa.x/DaxmodnisapermutationofZn.

31.4Solvingmodularlinearequations

Wenowconsidertheproblemoffindingsolutionstotheequation
axb.modn/ (31.25)

wherea>0andn>0.Thisproblemhasseveralapplicationsforexample,
weshalluseitaspartoftheprocedureforfindingkeysintheRSApublickey
cryptosysteminSection31.7.Weassumethata,b,andnaregiven,andwewish
tofindallvaluesofx,modulon,thatsatisfyequation(31.25).Theequationmay
havezero,one,ormorethanonesuchsolution.
LethaidenotethesubgroupofZngeneratedbya.SincehaiDfa.x/Wx>0gD
faxmodnWx>0g,equation(31.25)hasasolutionifandonlyifb2hai.La
grange'stheorem(Theorem31.15)tellsusthatjhaijmustbeadivisorofn.los
followingtheoremgivesusaprecisecharacterizationofhai.

Pgina68

31.4Solvingmodularlinearequations 947

Theorem31.20
Foranypositiveintegersaandn,ifdDgcd.an/,then

https://translate.googleusercontent.com/translate_f 47/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
haiDhdiDf0d2d:::..n=d/1/dg (31.26)
inZn,andthus
jhaijDn=d:

ProofWebeginbyshowingthatd2hai.RecallthatEXTENDEDEUCLID.an/
producesintegersx0andy0suchthatax0Cny0Dd.Thus,ax0d.modn/,so
thatd2hai.Inotherwords,disamultipleofainZn.
Sinced2hai,itfollowsthateverymultipleofdbelongstohai,becauseany
multipleofamultipleofaisitselfamultipleofa.Thus,haicontainseveryelement
inf0d2d:::..n=d/1/dg.Thatis,hdihai.
Wenowshowthathaihdi.Ifm2hai,thenmDaxmodnforsome
integerx,andsomDaxCnyforsomeintegery.However,djaanddjn,and
sodjmbyequation(31.4).Therefore,m2hdi.
Combiningtheseresults,wehavethathaiDhdi.ToseethatjhaijDn=d,
observethatthereareexactlyn=dmultiplesofdbetween0andn1,inclusive.

Corollary31.21
Theequationaxb.modn/issolvablefortheunknownxifandonlyifdjb,
wheredDgcd.an/.

ProofTheequationaxb.modn/issolvableifandonlyifb2hai,which
isthesameassaying
.bmodn/2f0d2d:::..n=d/1/dg
byTheorem31.20.If0b<n,thenb2haiifandonlyifdjb,sincethe
membersofhaiarepreciselythemultiplesofd.Ifb<0orb n,thecorollary
thenfollowsfromtheobservationthatdjbifandonlyifdj.bmodn/,sinceb
andbmodndifferbyamultipleofn,whichisitselfamultipleofd.

Corollary31.22
Theequationaxb.modn/eitherhasddistinctsolutionsmodulon,where
dDgcd.an/,orithasnosolutions.

ProofIfaxb.modn/hasasolution,thenb2hai.ByTheorem31.17,
ord.a/Djhaij,andsoCorollary31.18andTheorem31.20implythatthesequence
aimodn,foriD01:::,isperiodicwithperiodjhaijDn=d.Ifb2hai,thenb
appearsexactlydtimesinthesequenceaimodn,foriD01:::n1,since

Pgina69

948 Chapter31NumberTheoreticAlgorithms

thelength.n=d/blockofvalueshairepeatsexactlydtimesasiincreasesfrom0
ton1.TheindicesxofthedpositionsforwhichaxmodnDbarethesolutions
oftheequationaxb.modn/.

Theorem31.23
LetdDgcd.an/,andsupposethatdDax0Cny0forsomeintegersx0andy0
(forexample,ascomputedbyEXTENDEDEUCLID).Ifdjb,thentheequation
axb.modn/hasasoneofitssolutionsthevaluex0,where
x0Dx0.b=d/modn:

ProofWehave
ax0 ax0.b=d/.modn/
db=d/.modn/ (becauseax0d.modn/)
b .modn/
andthusx0isasolutiontoaxb.modn/.

Theorem31.24
Supposethattheequationaxb.modn/issolvable(thatis,djb,where
dDgcd.an/)andthatx0isanysolutiontothisequation.Then,thisequa
tionhasexactlyddistinctsolutions,modulon,givenbyxiDx0Cin=d/for
iD01:::re 1.

https://translate.googleusercontent.com/translate_f 48/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
ProofBecausen=d>0and0in=d/<nforiD01:::re 1,the
valuesx0x1:::xd1arealldistinct,modulon.Sincex0isasolutionofaxb
.modn/,wehaveax0modnb.modn/.Thus,foriD01:::re 1,we
tener
aximodnDax0Cin=d/modn
D.ax0Cain=d/modn
Dax0modn(becausedjaimpliesthatain=disamultipleofn)
b.modn/
andhenceaxib.modn/,makingxiasolution,too.ByCorollary31.22,the
equationaxb.modn/hasexactlydsolutions,sothatx0x1:::xd1must
beallofthem.

Wehavenowdevelopedthemathematicsneededtosolvetheequationaxb
.modn/thefollowingalgorithmprintsallsolutionstothisequation.Theinputs
aandnarearbitrarypositiveintegers,andbisanarbitraryinteger.

Pgina70

31.4Solvingmodularlinearequations 949

MODULARLINEAREQUATIONSOLVER.abn/
1.dx0y0/DEXTENDEDEUCLID.an/
2ifdjb
3 x0Dx0.b=d/modn
4 foriD0tod 1
5 print.x0Cin=d//modn
6elseprintnosolutions

Asanexampleoftheoperationofthisprocedure,considertheequation14x
30.mod100/(here,aD14,bD30,andnD100).CallingEXTENDED
EUCLIDinline1,weobtain.dx0y0/D.271/.Since2j30,lines35
ejecutar.Line3computesx0D.7/.15/mod100D95.Thelooponlines45
printsthetwosolutions95and45.
TheprocedureMODULARLINEAREQUATIONSOLVERworksasfollows.
Line1computesdDgcd.an/,alongwithtwovaluesx0andy0suchthatdD
ax0Cny0,demonstratingthatx0isasolutiontotheequationax0d.modn/.
Ifddoesnotdivideb,thentheequationaxb.modn/hasnosolution,by
Corollary31.21.Line2checkstoseewhetherdjbifnot,line6reportsthatthere
arenosolutions.Otherwise,line3computesasolutionx0toaxb.modn/,
inaccordancewithTheorem31.23.Givenonesolution,Theorem31.24statesthat
addingmultiplesof.n=d/,modulon,yieldstheotherd 1solutions.Thefor
loopoflines45printsoutalldsolutions,beginningwithx0andspacedn=d
apart,modulon.
MODULARLINEAREQUATIONSOLVERperformsO.lgnCgcd.an//arith
meticoperations,sinceEXTENDEDEUCLIDperformsO.lgn/arithmeticopera
tions,andeachiterationoftheforloopoflines45performsaconstantnumberof
arithmeticoperations.
ThefollowingcorollariesofTheorem31.24givespecializationsofparticular
interesar.

Corollary31.25
Foranyn>1,ifgcd.an/D1,thentheequationaxb.modn/hasaunique
solution,modulon.

IfbD1,acommoncaseofconsiderableinterest,thexwearelookingforisa
multiplicativeinverseofa,modulon.

Corollary31.26
Foranyn>1,ifgcd.an/D1,thentheequationax1.modn/hasaunique
solution,modulon.Otherwise,ithasnosolution.

Pgina71
https://translate.googleusercontent.com/translate_f 49/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,

950 Chapter31NumberTheoreticAlgorithms

ThankstoCorollary31.26,wecanusethenotationa1modntorefertothe
multiplicativeinverseofa,modulon,whenaandnarerelativelyprime.Si
gcd.an/D1,thentheuniquesolutiontotheequationax1.modn/isthe
integerxreturnedbyEXTENDEDEUCLID,sincetheequation
gcd.an/D1DaxCny
impliesax1.modn/.Thus,wecancomputea1modnefficientlyusing
EXTENDEDEUCLID.

Exercises

31.41
Findallsolutionstotheequation35x10.mod50/.

31.42
Provethattheequationaxay.modn/impliesxy.modn/whenever
gcd.an/D1.Showthattheconditiongcd.an/D1isnecessarybysupplyinga
counterexamplewithgcd.an/>1.

31.43
Considerthefollowingchangetoline3oftheprocedureMODULARLINEAR
EQUATIONSOLVER:

3 x0Dx0.b=d/mod.n=d/

Willthiswork?Explainwhyorwhynot.

31.44?
Letpbeprimeandf.x/f0Cf1xCCftxt.modp/beapolyno
mialofdegreet,withcoefficientsfidrawnfromZp.Wesaythata2Zp
isazerooffiff.a/0.modp/.Provethatifaisazerooff,then
f.x/.xa/gx/.modp/forsomepolynomialgx/ofdegreet 1.Prove
byinductionontthatifpisprime,thenapolynomialf.x/ofdegreetcanhave
atmosttdistinctzerosmodulop.

31.5TheChineseremaindertheorem

AroundA.D.100,theChinesemathematicianSunTsusolvedtheproblemoffind
ingthoseintegersxthatleaveremainders2,3,and2whendividedby3,5,and7
respectivamente.OnesuchsolutionisxD23allsolutionsareoftheform23C105k

Pgina72

31.5TheChineseremaindertheorem 951

forarbitraryintegersk.TheChineseremaindertheoremprovidesacorrespon
dencebetweenasystemofequationsmoduloasetofpairwiserelativelyprime
moduli(forexample,3,5,and7)andanequationmodulotheirproduct(forexam
ple,105).
TheChineseremaindertheoremhastwomajorapplications.Lettheinte
gernbefactoredasnDn1n2 nk,wherethefactorsniarepairwiserelatively
prime.First,theChineseremaindertheoremisadescriptivestructuretheorem
thatdescribesthestructureofZnasidenticaltothatoftheCartesianproduct
Zn Zn
1 2 Zn withcomponentwiseadditionandmultiplicationmoduloni
k

intheithcomponent.Second,thisdescriptionhelpsustodesignefficientalgo
rithms,sinceworkingineachofthesystemsZn canbemoreefficient(intermsof
i

bitoperations)thanworkingmodulon.

Theorem31.27(Chineseremaindertheorem)
LetnDn1n2 nk,wheretheniarepairwiserelativelyprime.Considerael
correspondence
a$.a1a2:::ak/ (31.27)

https://translate.googleusercontent.com/translate_f 50/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
wherea2Zn,ai2Zn ,Y
i

aiDamodni
foriD12:::KThen,mapping(31.27)isaonetoonecorrespondence(bijec
tion)betweenZnandtheCartesianproductZn Zn 1
Zn .Operationsper
2 k

formedontheelementsofZncanbeequivalentlyperformedonthecorresponding
ktuplesbyperformingtheoperationsindependentlyineachcoordinatepositionin
theappropriatesystem.Thatis,if
a$.a1a2:::ak/
b$.b1b2:::bk/
entonces
.aCb/modn$..a1Cb1/modn1:::.akCbk/modnk/ (31.28)
.ab/modn$..a1 b1/modn1:::.ak bk/modnk/ (31.29)
.ab/modn $.a1b1modn1:::akbkmodnk/: (31.30)

ProofTransformingbetweenthetworepresentationsisfairlystraightforward.
Goingfromato.a1a2:::ak/isquiteeasyandrequiresonlykmodopera
Ciones.
Computingafrominputs.a1a2:::ak/isabitmorecomplicated.Webegin
bydefiningmiDn=niforiD12:::kthusmiistheproductofallofthenj's
otherthanni:miDn1n2 ni1niC1 nk.Wenextdefine

Pgina73

952 Chapter31NumberTheoreticAlgorithms

ciDmi.m1 modni/ (31.31)


yo
foriD12:::KEquation(31.31)isalwayswelldefined:sincemiandniare
relativelyprime(byTheorem31.6),Corollary31.26guaranteesthatm1 modni
yo
exists.Finally,wecancomputeaasafunctionofa1,a2,...,akasfollows:
a.a1c1Ca2c2CCakck/.modn/: (31.32)
Wenowshowthatequation(31.32)ensuresthataai.modni/foriD
12:::KNotethatifji,thenmj0.modni/,whichimpliesthatcj
mj0.modni/.Notealsothatci1.modni/,fromequation(31.31).Nosotros
thushavetheappealingandusefulcorrespondence
ci$.00:::010:::0/
avectorthathas0severywhereexceptintheithcoordinate,whereithasa1theci
thusformabasisfortherepresentation,inacertainsense.Foreachi,therefore,
wehave
aaici .modni/
aimi.m1 modni/.modni/
yo
ai .modni/
whichiswhatwewishedtoshow:ourmethodofcomputingafromtheai'spro
ducesaresultathatsatisfiestheconstraintsaai.modni/foriD12:::K
Thecorrespondenceisonetoone,sincewecantransforminbothdirections.
Finally,equations(31.28)(31.30)followdirectlyfromExercise31.17,since
xmodniD.xmodn/modniforanyxandiD12:::K

Weshallusethefollowingcorollarieslaterinthischapter.

Corollary31.28
Ifn1n2:::nkarepairwiserelativelyprimeandnDn1n2 nk,thenforany
integersa1a2:::ak,thesetofsimultaneousequations
xai .modni/
foriD12:::k,hasauniquesolutionmodulonfortheunknownx.

Corollary31.29
Ifn1n2:::nkarepairwiserelativelyprimeandnDn1n2 nk,thenforall
integersxanda,
xa.modni/
foriD12:::kifandonlyif
xa.modn/:
https://translate.googleusercontent.com/translate_f 51/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
xa.modn/:

Pgina74

31.5TheChineseremaindertheorem 953

0 1 2 3 4 5 6 7 8 9 10 11 12
0 0 40 15 55 30 5 45 20 60 35 10 50 25
1 26 1 41 diecisis
56 31 6 46 21 61 36 11 51
2 52 27 2 42 17 57 32 7 47 22 62 37 12
3 13 53 28 3 43 18 58 33 8 48 23 63 38
4 39 14 54 29 4 44 19 59 34 9 49 24 64

Figure31.3AnillustrationoftheChineseremaindertheoremforn1D5andn2D13.Forthis
example,c1D26andc2D40.Inrowi,columnjisshownthevalueofa,modulo65,such
thatamod5Diandamod13Dj.Notethatrow0,column0containsa0.Similarly,row4,
column12containsa64(equivalentto1).Sincec1D26,movingdownarowincreasesaby26.
Similarly,c2D40meansthatmovingrightbyacolumnincreasesaby40.Increasingaby1
correspondstomovingdiagonallydownwardandtotheright,wrappingaroundfromthebottomto
thetopandfromtherighttotheleft.

AsanexampleoftheapplicationoftheChineseremaindertheorem,supposewe
aregiventhetwoequations
a2.mod5/
a3.mod13/
sothata1D2,n1Dm2D5,a2D3,andn2Dm1D13,andwewish
tocomputeamod65,sincenDn1n2D65.Because1312.mod5/and
518.mod13/,wehave
c1 D13.2mod5/D26
c2 D5.8mod13/D40
y
a226C340.mod65/
52C120 .mod65/
42 .mod65/:

SeeFigure31.3foranillustrationoftheChineseremaindertheorem,modulo65.
Thus,wecanworkmodulonbyworkingmodulondirectlyorbyworkinginthe
transformedrepresentationusingseparatemodulonicomputations,asconvenient.
Thecomputationsareentirelyequivalent.

Exercises

31.51
Findallsolutionstotheequationsx4.mod5/andx5.mod11/.

Pgina75

954 Chapter31NumberTheoreticAlgorithms

31.52
Findallintegersxthatleaveremainders1,2,3whendividedby9,8,7respectively.

31.53
Arguethat,underthedefinitionsofTheorem31.27,ifgcd.an/D1,then
.a1modn/$..a1 modn1/.a1 modn2/:::.a1 modnk//:
1 2 K

31.54
UnderthedefinitionsofTheorem31.27,provethatforanypolynomialf,thenum
berofrootsoftheequationf.x/0.modn/equalstheproductofthenumber
ofrootsofeachoftheequationsf.x/0.modn1/,f.x/0.modn2/,...,
f.x/0.modn /.
https://translate.googleusercontent.com/translate_f 52/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
f.x/0.modnk/.

31.6Powersofanelement

Justasweoftenconsiderthemultiplesofagivenelementa,modulon,weconsider
thesequenceofpowersofa,modulon,wherea2Zn:
a0a1a2a3::: (31.33)
modulon.Indexingfrom0,the0thvalueinthissequenceisa0modnD1,and
theithvalueisaimodn.Forexample,thepowersof3modulo7are
yo 01234567891011
3imod7 13264513264 5
whereasthepowersof2modulo7are
yo 01234567891011
2imod7 12412412412 4
Inthissection,lethaidenotethesubgroupofZngeneratedbyabyrepeated
multiplication,andletordn.a/(theorderofa,modulon)denotetheorderofa
inZn.Forexample,h2iDf124ginZ7,andord7.2/D3.Usingthedefinitionof
theEulerphifunction.n/asthesizeofZn(seeSection31.3),wenowtranslate
Corollary31.19intothenotationofZntoobtainEuler'stheoremandspecializeit
toZp,wherepisprime,toobtainFermat'stheorem.

Theorem31.30(Euler'stheorem)
Foranyintegern>1,
a.n/1.modn/foralla2Zn:

Pgina76

31.6Powersofanelement 955

Theorem31.31(Fermat'stheorem)
Ifpisprime,then
ap11.modp/foralla2Zp:

ProofByequation(31.21),.p/Dp1ifpisprime.

Fermat'stheoremappliestoeveryelementinZpexcept0,since062Zp.Paratodos
a2Zp,however,wehaveapa.modp/ifpisprime.
Ifordn.g/DjZnj,theneveryelementinZnisapowerofg,modulon,and
gisaprimitiverootorageneratorofZn.Forexample,3isaprimitiveroot,
modulo7,but2isnotaprimitiveroot,modulo7.IfZnpossessesaprimitive
root,thegroupZniscyclic.Weomittheproofofthefollowingtheorem,whichis
provenbyNivenandZuckerman[265].

Theorem31.32
Thevaluesofn>1forwhichZniscyclicare2,4,pe,and2pe,forallprimes
p>2andallpositiveintegerse.

IfgisaprimitiverootofZnandaisanyelementofZn,thenthereexistsasuch
thatga.modn/.Thisisadiscretelogarithmoranindexofa,modulon,
tothebasegwedenotethisvalueasindng.a/.

Theorem31.33(Discretelogarithmtheorem)
IfgisaprimitiverootofZn,thentheequationgxgy.modn/holdsifand
onlyiftheequationxy.mod.n//holds.

ProofSupposefirstthatxy.mod.n//.Then,xDyCk.n/forsome
integerk.Porlotanto,
gx gyCk.n/ .modn/
gy .g.n//k .modn/
gy 1k .modn/ (byEuler'stheorem)

g
https://translate.googleusercontent.com/translate_f 53/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
gy .modn/:
Conversely,supposethatgxgy.modn/.Becausethesequenceofpowersofg
generateseveryelementofhgiandjhgijD.n/,Corollary31.18impliesthat
thesequenceofpowersofgisperiodicwithperiod.n/.Therefore,ifgxgy
.modn/,thenwemusthavexy.mod.n//.

Wenowturnourattentiontothesquarerootsof1,moduloaprimepower.los
followingtheoremwillbeusefulinourdevelopmentofaprimalitytestingalgo
rithminSection31.8.

Pgina77

956 Chapter31NumberTheoreticAlgorithms

Theorem31.34
Ifpisanoddprimeande 1,thentheequation
x21.modpe/ (31.34)
hasonlytwosolutions,namelyxD1andxD1.

ProofEquation(31.34)isequivalentto
pej.x1/.xC1/:
Sincep>2,wecanhavepj.x 1/orpj.xC1/,butnotboth.(Otherwise,
byproperty(31.3),pwouldalsodividetheirdifference.xC1/.x1/D2.)
Ifp.x1/,thengcd.pex 1/D1,andbyCorollary31.5,wewouldhave
pej.xC1/.Thatis,x1.modpe/.Symmetrically,ifp.xC1/,
thengcd.pexC1/D1,andCorollary31.5impliesthatpej.x 1/,sothat
x1.modpe/.Therefore,eitherx1.modpe/orx1.modpe/.

Anumberxisanontrivialsquarerootof1,modulon,ifitsatisfiestheequation
x21.modn/butxisequivalenttoneitherofthetwotrivialsquareroots:
1or1,modulon.Forexample,6isanontrivialsquarerootof1,modulo35.
WeshallusethefollowingcorollarytoTheorem31.34inthecorrectnessproofin
Section31.8fortheMillerRabinprimalitytestingprocedure.

Corollary31.35
Ifthereexistsanontrivialsquarerootof1,modulon,thenniscomposite.

ProofBythecontrapositiveofTheorem31.34,ifthereexistsanontrivialsquare
rootof1,modulon,thenncannotbeanoddprimeorapowerofanoddprime.
Ifx21.mod2/,thenx1.mod2/,andsoallsquarerootsof1,modulo2,
aretrivial.Thus,ncannotbeprime.Finally,wemusthaven>1foranontrivial
squarerootof1toexist.Therefore,nmustbecomposite.

Raisingtopowerswithrepeatedsquaring
Afrequentlyoccurringoperationinnumbertheoreticcomputationsisraisingone
numbertoapowermoduloanothernumber,alsoknownasmodularexponentia
tion.Moreprecisely,wewouldlikeanefficientwaytocomputeabmodn,where
aandbarenonnegativeintegersandnisapositiveinteger.Modularexponenti
ationisanessentialoperationinmanyprimalitytestingroutinesandintheRSA
publickeycryptosystem.Themethodofrepeatedsquaringsolvesthisproblem
efficientlyusingthebinaryrepresentationofb.
Lethbkbk1:::b1b0ibethebinaryrepresentationofb.(Thatis,thebinary
representationiskC1bitslong,bkisthemostsignificantbit,andb0istheleast

Pgina78

31.6Powersofanelement 957

yo 9 8 7 6 5 4 3 2 1 0
bi 1 0 0 0 1 1 0 0 0 0
do 1 2 4 8 17 35 70 140 280 560
re 7 49 157 526 160 241 298 166 67 1

https://translate.googleusercontent.com/translate_f 54/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,

Figure31.4TheresultsofMODULAREXPONENTIATIONwhencomputingab.modn/,where
aD7,bD560Dh1000110000i,andnD561.Thevaluesareshownaftereachexecutionofthe
forloop.Thefinalresultis1.

significantbit.)Thefollowingprocedurecomputesacmodnascisincreasedby
doublingsandincrementationsfrom0tob.

MODULAREXPONENTIATION.abn/
1cD0
2dD1
3lethbkbk1:::b0ibethebinaryrepresentationofb
4foriDkdownto0
5 cD2c
6 dD.dd/modn
7 ifbi==1
8 cDcC1
9 dD.da/modn
10returnd

Theessentialuseofsquaringinline6ofeachiterationexplainsthenamerepeated
squaring.Asanexample,foraD7,bD560,andnD561,thealgorithm
computesthesequenceofvaluesmodulo561showninFigure31.4thesequence
ofexponentsusedappearsintherowofthetablelabeledbyc.
Thevariablecisnotreallyneededbythealgorithmbutisincludedforthefol
lowingtwopartloopinvariant:

Justpriortoeachiterationoftheforloopoflines49,
1.Thevalueofcisthesameastheprefixhbkbk1:::biC1iofthebinary
representationofb,and
2.dDacmodn.

Weusethisloopinvariantasfollows:
Initialization:Initially,iDk,sothattheprefixhbkbk1:::biC1iisempty,
whichcorrespondstocD0.Moreover,dD1Da0modn.

Pgina79

958 Chapter31NumberTheoreticAlgorithms

Maintenance:Letc0andd0denotethevaluesofcanddattheendofaniteration
oftheforloop,andthusthevaluespriortothenextiteration.Eachiteration
updatesc0D2c(ifbiD0)orc0D2cC1(ifbiD1),sothatcwillbecorrect
priortothenextiteration.IfbiD0,thend0Dd2modnD.ac/2modnD
a2cmodnDac modn.IfbiD1,thend0Dd2amodnD.ac/2amodnD
0

a2cC1modnDac modn.Ineithercase,dDacmodnpriortothenext
0

iteration.
Termination:Attermination,iD1.Thus,cDb,sincechasthevalueofthe
prefixhbkbk1:::b0iofb'sbinaryrepresentation.HencedDacmodnD
abmodn.
Iftheinputsa,b,andnarebitnumbers,thenthetotalnumberofarith
meticoperationsrequiredisO./andthetotalnumberofbitoperationsrequired
isO.3/.

Exercises

31.61
DrawatableshowingtheorderofeveryelementinZ11.Pickthesmallestprimitive
rootgandcomputeatablegivingind11g.x/forallx2Z11.

31.62
Giveamodularexponentiationalgorithmthatexaminesthebitsofbfromrightto
leftinsteadoflefttoright.

31.63
Assumingthatyouknow.n/,explainhowtocomputea1modnforanya2Zn
https://translate.googleusercontent.com/translate_f 55/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,

usingtheprocedureMODULAREXPONENTIATION.

31.7TheRSApublickeycryptosystem

Withapublickeycryptosystem,wecanencryptmessagessentbetweentwocom
municatingpartiessothataneavesdropperwhooverhearstheencryptedmessages
willnotbeabletodecodethem.Apublickeycryptosystemalsoenablesaparty
toappendanunforgeabledigitalsignaturetotheendofanelectronicmessage.
Suchasignatureistheelectronicversionofahandwrittensignatureonapaperdoc
Cin.Itcanbeeasilycheckedbyanyone,forgedbynoone,yetlosesitsvalidity
ifanybitofthemessageisaltered.Itthereforeprovidesauthenticationofboththe
identityofthesignerandthecontentsofthesignedmessage.Itistheperfecttool

Pgina80

31.7TheRSApublickeycryptosystem 959

forelectronicallysignedbusinesscontracts,electronicchecks,electronicpurchase
orders,andotherelectroniccommunicationsthatpartieswishtoauthenticate.
TheRSApublickeycryptosystemreliesonthedramaticdifferencebetweenthe
easeoffindinglargeprimenumbersandthedifficultyoffactoringtheproductof
twolargeprimenumbers.Section31.8describesanefficientprocedureforfinding
largeprimenumbers,andSection31.9discussestheproblemoffactoringlarge
integers.

Publickeycryptosystems

Inapublickeycryptosystem,eachparticipanthasbothapublickeyandasecret
key.Eachkeyisapieceofinformation.Forexample,intheRSAcryptosystem,
eachkeyconsistsofapairofintegers.TheparticipantsAliceandBobare
traditionallyusedincryptographyexampleswedenotetheirpublicandsecret
keysasPA,SAforAliceandPB,SBforBob.
Eachparticipantcreateshisorherownpublicandsecretkeys.Secretkeysare
keptsecret,butpublickeyscanberevealedtoanyoneorevenpublished.Dehecho,
itisoftenconvenienttoassumethateveryone'spublickeyisavailableinapub
licdirectory,sothatanyparticipantcaneasilyobtainthepublickeyofanyother
participant.
Thepublicandsecretkeysspecifyfunctionsthatcanbeappliedtoanymessage.
LetDdenotethesetofpermissiblemessages.Forexample,Dmightbethesetof
allfinitelengthbitsequences.Inthesimplest,andoriginal,formulationofpublic
keycryptography,werequirethatthepublicandsecretkeysspecifyonetoone
functionsfromDtoitself.WedenotethefunctioncorrespondingtoAlice'spublic
keyPAbyPA./andthefunctioncorrespondingtohersecretkeySAbySA./.El
functionsPA./andSA./arethuspermutationsofD.Weassumethatthefunctions
PA./andSA./areefficientlycomputablegiventhecorrespondingkeyPAorSA.
Thepublicandsecretkeysforanyparticipantareamatchedpairinthatthey
specifyfunctionsthatareinversesofeachother.Thatis,
MDSA.PA.M// (31.35)
MDPA.SA.M// (31.36)
foranymessageM2D.TransformingMwiththetwokeysPAandSAsucces
sively,ineitherorder,yieldsthemessageMback.
Inapublickeycryptosystem,werequirethatnoonebutAlicebeabletocom
putethefunctionSA./inanypracticalamountoftime.Thisassumptioniscrucial
tokeepingencryptedmailsenttoAliceprivateandtoknowingthatAlice'sdigi
talsignaturesareauthentic.AlicemustkeepSAsecretifshedoesnot,sheloses
heruniquenessandthecryptosystemcannotprovideherwithuniquecapabilities.
TheassumptionthatonlyAlicecancomputeSA./mustholdeventhougheveryone

Pgina81

https://translate.googleusercontent.com/translate_f 56/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
960 Chapter31NumberTheoreticAlgorithms

Bob Alicia
communicationchannel
Cifrar decrypt
CDP .M/
A

METRO P A SA METRO

eavesdropper
do

Figure31.5Encryptioninapublickeysystem.BobencryptsthemessageMusingAlice'spublic
keyPAandtransmitstheresultingciphertextCDPA.M/overacommunicationchanneltoAl
ice.AneavesdropperwhocapturesthetransmittedciphertextgainsnoinformationaboutM.Alice
receivesCanddecryptsitusinghersecretkeytoobtaintheoriginalmessageMDSA.C/.

knowsPAandcancomputePA./,theinversefunctiontoSA./,efficiently.Enorden
todesignaworkablepublickeycryptosystem,wemustfigureouthowtocreate
asysteminwhichwecanrevealatransformationPA./withouttherebyrevealing
howtocomputethecorrespondinginversetransformationSA./.Thistaskappears
formidable,butweshallseehowtoaccomplishit.
Inapublickeycryptosystem,encryptionworksasshowninFigure31.5.Sup
poseBobwishestosendAliceamessageMencryptedsothatitwilllooklike
unintelligiblegibberishtoaneavesdropper.Thescenarioforsendingthemessage
goesasfollows.
BobobtainsAlice'spublickeyPA(fromapublicdirectoryordirectlyfrom
Alice).
BobcomputestheciphertextCDPA.M/correspondingtothemessageM
andsendsCtoAlice.
WhenAlicereceivestheciphertextC,sheapplieshersecretkeySAtoretrieve
theoriginalmessage:SA.C/DSA.PA.M//DM.
BecauseSA./andPA./areinversefunctions,AlicecancomputeMfromC.Be
causeonlyAliceisabletocomputeSA./,AliceistheonlyonewhocancomputeM
fromC.BecauseBobencryptsMusingPA./,onlyAlicecanunderstandthetrans
mittedmessage.
Wecanjustaseasilyimplementdigitalsignatureswithinourformulationofa
publickeycryptosystem.(Thereareotherwaysofapproachingtheproblemof
constructingdigitalsignatures,butweshallnotgointothemhere.)Supposenow
thatAlicewishestosendBobadigitallysignedresponseM0.Figure31.6shows
howthedigitalsignaturescenarioproceeds.
Alicecomputesherdigitalsignature forthemessageM0usinghersecret
keySAandtheequationDSA.M0/.

Pgina82

31.7TheRSApublickeycryptosystem 961

Alicia Bob
firmar verificar
SA
DS .M /
A 0
P A

=? aceptar
.M / M
M
0 0
0

communicationchannel

Figure31.6Digitalsignaturesinapublickeysystem.AlicesignsthemessageM0byappending
herdigitalsignatureDSA.M0/toit.Shetransmitsthemessage/signaturepair.M0/toBob,
whoverifiesitbycheckingtheequationM0DPA./.Iftheequationholds,heaccepts.M0/as
amessagethatAlicehassigned.

Alicesendsthemessage/signaturepair.M0/toBob.
WhenBobreceives.M0/,hecanverifythatitoriginatedfromAlicebyus
ingAlice'spublickeytoverifytheequationM0DPA./.(Presumably,M0
containsAlice'sname,soBobknowswhosepublickeytouse.)Iftheequation
holds,thenBobconcludesthatthemessageM0wasactuallysignedbyAlice.
https://translate.googleusercontent.com/translate_f 57/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
Iftheequationfailstohold,BobconcludeseitherthatthemessageM0orthe
digitalsignaturewascorruptedbytransmissionerrorsorthatthepair.M0/
isanattemptedforgery.
Becauseadigitalsignatureprovidesbothauthenticationofthesigner'sidentityand
authenticationofthecontentsofthesignedmessage,itisanalogoustoahandwrit
tensignatureattheendofawrittendocument.
Adigitalsignaturemustbeverifiablebyanyonewhohasaccesstothesigners
publickey.Asignedmessagecanbeverifiedbyonepartyandthenpassedonto
otherpartieswhocanalsoverifythesignature.Forexample,themessagemight
beanelectroniccheckfromAlicetoBob.AfterBobverifiesAlice'ssignatureon
thecheck,hecangivethechecktohisbank,whocanthenalsoverifythesignature
andeffecttheappropriatefundstransfer.
Asignedmessageisnotnecessarilyencryptedthemessagecanbeintheclear
andnotprotectedfromdisclosure.Bycomposingtheaboveprotocolsforencryp
tionandforsignatures,wecancreatemessagesthatarebothsignedandencrypted.
Thesignerfirstappendshisorherdigitalsignaturetothemessageandthenen
cryptstheresultingmessage/signaturepairwiththepublickeyoftheintendedre
cipient.Therecipientdecryptsthereceivedmessagewithhisorhersecretkeyto
obtainboththeoriginalmessageanditsdigitalsignature.Therecipientcanthen
verifythesignatureusingthepublickeyofthesigner.Thecorrespondingcom
binedprocessusingpaperbasedsystemswouldbetosignthepaperdocumentand

Pgina83

962 Chapter31NumberTheoreticAlgorithms

thensealthedocumentinsideapaperenvelopethatisopenedonlybytheintended
recipiente.

TheRSAcryptosystem
IntheRSApublickeycryptosystem,aparticipantcreateshisorherpublicand
secretkeyswiththefollowingprocedure:
1.Selectatrandomtwolargeprimenumberspandqsuchthatpq.Theprimes
pandqmightbe,say,1024bitseach.
2.ComputenDpq.
3.Selectasmalloddintegerethatisrelativelyprimeto.n/,which,byequa
tion(31.20),equals.p1/.q1/.
4.Computedasthemultiplicativeinverseofe,modulo.n/.(Corollary31.26
guaranteesthatdexistsandisuniquelydefined.Wecanusethetechniqueof
Section31.4tocomputed,giveneand.n/.)
5.PublishthepairPD.en/astheparticipant'sRSApublickey.
6.KeepsecretthepairSD.dn/astheparticipant'sRSAsecretkey.

Forthisscheme,thedomainDisthesetZn.TotransformamessageMasso
ciatedwithapublickeyPD.en/,compute
P.M/DMemodn: (31.37)
TotransformaciphertextCassociatedwithasecretkeySD.dn/,compute
SC/DCdmodn: (31.38)
Theseequationsapplytobothencryptionandsignatures.Tocreateasignature,the
signerapplieshisorhersecretkeytothemessagetobesigned,ratherthantoa
ciphertext.Toverifyasignature,thepublickeyofthesignerisappliedtoit,rather
thantoamessagetobeencrypted.
Wecanimplementthepublickeyandsecretkeyoperationsusingtheprocedure
MODULAREXPONENTIATIONdescribedinSection31.6.Toanalyzetherunning
timeoftheseoperations,assumethatthepublickey.en/andsecretkey.dn/
satisfylgeDO.1/,lgd,andlgn.Then,applyingapublickeyrequires
O.1/modularmultiplicationsandusesO.2/bitoperations.Applyingasecret
keyrequiresO./modularmultiplications,usingO.3/bitoperations.

Theorem31.36(CorrectnessofRSA)
TheRSAequations(31.37)and(31.38)defineinversetransformationsofZnsatis
fyingequations(31.35)and(31.36).
https://translate.googleusercontent.com/translate_f 58/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,

Pgina84

31.7TheRSApublickeycryptosystem 963

ProofFromequations(31.37)and(31.38),wehavethatforanyM2Zn,
PSM//DSPM//DMed .modn/:
Sinceeanddaremultiplicativeinversesmodulo.n/D.p1/.q1/,
edD1Ckp1/.q1/
forsomeintegerk.Butthen,ifM60.modp/,wehave
Med MMp1/k.q1/ .modp/
M..Mmodp/p1/k.q1/.modp/
M.1/k.q1/ .modp/ (byTheorem31.31)
M .modp/:
Also,MedM.modp/ifM0.modp/.Thus,
MedM.modp/
forallM.Similarly,
MedM.modq/
forallM.Thus,byCorollary31.29totheChineseremaindertheorem,
MedM.modn/
forallM.

ThesecurityoftheRSAcryptosystemrestsinlargepartonthedifficultyoffac
toringlargeintegers.Ifanadversarycanfactorthemodulusninapublickey,then
theadversarycanderivethesecretkeyfromthepublickey,usingtheknowledge
ofthefactorspandqinthesamewaythatthecreatorofthepublickeyusedthem.
Therefore,iffactoringlargeintegersiseasy,thenbreakingtheRSAcryptosystem
iseasy.Theconversestatement,thatiffactoringlargeintegersishard,thenbreak
ingRSAishard,isunproven.Aftertwodecadesofresearch,however,noeasier
methodhasbeenfoundtobreaktheRSApublickeycryptosystemthantofactor
themodulusn.AndasweshallseeinSection31.9,factoringlargeintegersissur
prisinglydifficult.Byrandomlyselectingandmultiplyingtogethertwo1024bit
primes,wecancreateapublickeythatcannotbebrokeninanyfeasibleamount
oftimewithcurrenttechnology.Intheabsenceofafundamentalbreakthroughin
thedesignofnumbertheoreticalgorithms,andwhenimplementedwithcarefol
lowingrecommendedstandards,theRSAcryptosystemiscapableofprovidinga
highdegreeofsecurityinapplications.
InordertoachievesecuritywiththeRSAcryptosystem,however,weshould
useintegersthatarequitelonghundredsorevenmorethanonethousandbits

Pgina85

964 Chapter31NumberTheoreticAlgorithms

longtoresistpossibleadvancesintheartoffactoring.Atthetimeofthis
writing(2009),RSAmoduliwerecommonlyintherangeof768to2048bits.
Tocreatemoduliofsuchsizes,wemustbeabletofindlargeprimesefficiently.
Section31.8addressesthisproblem.
Forefficiency,RSAisoftenusedinahybridorkeymanagementmode
withfastnonpublickeycryptosystems.Withsuchasystem,theencryptionand
decryptionkeysareidentical.IfAlicewishestosendalongmessageMtoBob
privately,sheselectsarandomkeyKforthefastnonpublickeycryptosystemand
encryptsMusingK,obtainingciphertextC.Here,CisaslongasM,butK
isquiteshort.Then,sheencryptsKusingBob'spublicRSAkey.SinceKis
short,computingPB.K/isfast(muchfasterthancomputingPB.M/).Ellaentonces
transmits.CPB.K//toBob,whodecryptsPB.K/toobtainKandthenusesK
todecryptC,obtainingM.
https://translate.googleusercontent.com/translate_f 59/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
Wecanuseasimilarhybridapproachtomakedigitalsignaturesefficiently.
ThisapproachcombinesRSAwithapubliccollisionresistanthashfunctionha
functionthatiseasytocomputebutforwhichitiscomputationallyinfeasibleto
findtwomessagesMandM0suchthathM/DhM0/.ThevaluehM/is
ashort(say,256bit)fingerprintofthemessageM.IfAlicewishestosigna
messageM,shefirstapplieshtoMtoobtainthefingerprinthM/,whichshe
thenencryptswithhersecretkey.Shesends.MSA.hM///toBobashersigned
versionofM.BobcanverifythesignaturebycomputinghM/andverifying
thatPAappliedtoSA.hM//asreceivedequalshM/.Becausenoonecancreate
twomessageswiththesamefingerprint,itiscomputationallyinfeasibletoaltera
signedmessageandpreservethevalidityofthesignature.
Finally,wenotethattheuseofcertificatesmakesdistributingpublickeysmuch
msfcil.Forexample,assumethereisatrustedauthorityTwhosepublickey
isknownbyeveryone.AlicecanobtainfromTasignedmessage(hercertificate)
statingthatAlice'spublickeyisPA.Thiscertificateisselfauthenticatingsince
everyoneknowsPT.Alicecanincludehercertificatewithhersignedmessages,
sothattherecipienthasAlice'spublickeyimmediatelyavailableinordertoverify
hersignature.BecauseherkeywassignedbyT,therecipientknowsthatAlices
keyisreallyAlice's.

Exercises

31.71
ConsideranRSAkeysetwithpD11,qD29,nD319,andeD3.What
valueofdshouldbeusedinthesecretkey?Whatistheencryptionofthemessage
MD100?

Pgina86

31.8Primalitytesting 965

31.72
ProvethatifAlice'spublicexponenteis3andanadversaryobtainsAlice'ssecret
exponentd,where0<d<.n/,thentheadversarycanfactorAlice'smodulusn
intimepolynomialinthenumberofbitsinn.(Althoughyouarenotaskedtoprove
it,youmaybeinterestedtoknowthatthisresultremainstrueevenifthecondition
eD3isremoved.SeeMiller[255].)

31.73?
ProvethatRSAismultiplicativeinthesensethat
PA.M1/PA.M2/PA.M1M2/.modn/:

Usethisfacttoprovethatifanadversaryhadaprocedurethatcouldefficiently
decrypt1percentofmessagesfromZnencryptedwithPA,thenhecouldemploy
aprobabilisticalgorithmtodecrypteverymessageencryptedwithPAwithhigh
probabilidad.

?31.8Primalitytesting

Inthissection,weconsidertheproblemoffindinglargeprimes.Webeginwitha
discussionofthedensityofprimes,proceedtoexamineaplausible,butincomplete,
approachtoprimalitytesting,andthenpresentaneffectiverandomizedprimality
testduetoMillerandRabin.

Thedensityofprimenumbers

Formanyapplications,suchascryptography,weneedtofindlargerandom
primes.Fortunately,largeprimesarenottoorare,sothatitisfeasibletotest
randomintegersoftheappropriatesizeuntilwefindaprime.Theprimedistribu
tionfunction.n/specifiesthenumberofprimesthatarelessthanorequalton.
Forexample,.10/D4,sincethereare4primenumberslessthanorequalto10,
namely,2,3,5,and7.Theprimenumbertheoremgivesausefulapproximation
to.n/.

https://translate.googleusercontent.com/translate_f 60/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,

Theorem31.37(Primenumbertheorem)
.n/
Lim D1:
n!1 n=lnn

Theapproximationn=lnngivesreasonablyaccurateestimatesof.n/even
forsmalln.Forexample,itisoffbylessthan6%atnD109,where.n/D

Pgina87

966 Chapter31NumberTheoreticAlgorithms

50,847,534andn=lnn 48,254,942.(Toanumbertheorist,109isasmallnum
ber.)
Wecanviewtheprocessofrandomlyselectinganintegernanddetermining
whetheritisprimeasaBernoullitrial(seeSectionC.4).Bytheprimenumber
theorem,theprobabilityofasuccessthatis,theprobabilitythatnisprimeis
approximately1=lnn.Thegeometricdistributiontellsushowmanytrialsweneed
toobtainasuccess,andbyequation(C.32),theexpectednumberoftrialsisap
proximatelylnn.Thus,wewouldexpecttoexamineapproximatelylnnintegers
chosenrandomlynearninordertofindaprimethatisofthesamelengthasn.
Forexample,weexpectthatfindinga1024bitprimewouldrequiretestingap
proximatelyln21024 710randomlychosen1024bitnumbersforprimality.(Of
course,wecancutthisfigureinhalfbychoosingonlyoddintegers.)
Intheremainderofthissection,weconsidertheproblemofdeterminingwhether
ornotalargeoddintegernisprime.Fornotationalconvenience,weassumethatn
hastheprimefactorization
nDpe pre (31.39)
1pe
2
1 r
2

wherer 1,p1p2:::praretheprimefactorsofn,ande1e2:::erareposi
tiveintegers.TheintegernisprimeifandonlyifrD1ande1D1.
Onesimpleapproachtotheproblemoftestingforprimalityistrialdivision.Nosotros
pag
trydividingnbyeachinteger23:::b nc.(Again,wemayskipevenintegers
greaterthan2.)Itiseasytoseethatnisprimeifandonlyifnoneofthetrialdivi
sorsdividesn.Assumingthateachtrialdivisiontakesconstanttime,theworstcase
pag
runningtimeis. n/,whichisexponentialinthelengthofn.(Recallthatifn
pag
isencodedinbinaryusingbits,thenDdlg.nC1/e,andso nD.2=2/.)
Thus,trialdivisionworkswellonlyifnisverysmallorhappenstohaveasmall
primefactor.Whenitworks,trialdivisionhastheadvantagethatitnotonlyde
termineswhethernisprimeorcomposite,butalsodeterminesoneofn'sprime
factorsifniscomposite.
Inthissection,weareinterestedonlyinfindingoutwhetheragivennumbern
isprimeifniscomposite,wearenotconcernedwithfindingitsprimefactor
Cin.AsweshallseeinSection31.9,computingtheprimefactorizationofa
numberiscomputationallyexpensive.Itisperhapssurprisingthatitismucheasier
totellwhetherornotagivennumberisprimethanitistodeterminetheprime
factorizationofthenumberifitisnotprime.

Pseudoprimalitytesting
Wenowconsideramethodforprimalitytestingthatalmostworksandinfact
isgoodenoughformanypracticalapplications.Lateron,weshallpresentare

Pgina88

31.8Primalitytesting 967

finementofthismethodthatremovesthesmalldefect.LetZC ndenotethenonzero
elementsofZn:
ZC
nDf12:::n1g:
Ifnisprime,thenZC
nDZn.
Wesaythatnisabaseapseudoprimeifniscompositeand
https://translate.googleusercontent.com/translate_f 61/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,

an11.modn/: (31.40)
Fermat'stheorem(Theorem31.31)impliesthatifnisprime,thennsatisfiesequa
tion(31.40)foreveryainZC n.Thus,ifwecanfindanya2ZC nsuchthatndoes
notsatisfyequation(31.40),thenniscertainlycomposite.Surprisingly,thecon
versealmostholds,sothatthiscriterionformsanalmostperfecttestforprimality.
Wetesttoseewhethernsatisfiesequation(31.40)foraD2.Ifnot,wedeclaren
tobecompositebyreturningCOMPOSITE.Otherwise,wereturnPRIME,guessing
thatnisprime(when,infact,allweknowisthatniseitherprimeorabase2
pseudoprime).
Thefollowingprocedurepretendsinthismannertobecheckingtheprimality
ofn.ItusestheprocedureMODULAREXPONENTIATIONfromSection31.6.Nosotros
assumethattheinputnisanoddintegergreaterthan2.

PSEUDOPRIME.n/
1ifMODULAREXPONENTIATION.2n1n/61.modn/
2 returnCOMPOSITE //definitely
3elsereturnPRIME //wehope!

Thisprocedurecanmakeerrors,butonlyofonetype.Thatis,ifitsaysthatn
iscomposite,thenitisalwayscorrect.Ifitsaysthatnisprime,however,thenit
makesanerroronlyifnisabase2pseudoprime.
Howoftendoesthisprocedureerr?Surprisinglyrarely.Thereareonly22values
ofnlessthan10,000forwhichiterrsthefirstfoursuchvaluesare341,561,
645,and1105.Wewon'tproveit,buttheprobabilitythatthisprogrammakesan
erroronarandomlychosenbitnumbergoestozeroas!1.Usingmore
preciseestimatesduetoPomerance[279]ofthenumberofbase2pseudoprimesof
agivensize,wemayestimatethatarandomlychosen512bitnumberthatiscalled
primebytheaboveprocedurehaslessthanonechancein1020ofbeingabase2
pseudoprime,andarandomlychosen1024bitnumberthatiscalledprimehasless
thanonechancein1041ofbeingabase2pseudoprime.Soifyouaremerely
tryingtofindalargeprimeforsomeapplication,forallpracticalpurposesyou
almostnevergowrongbychoosinglargenumbersatrandomuntiloneofthem
causesPSEUDOPRIMEtoreturnPRIME.Butwhenthenumbersbeingtestedfor
primalityarenotrandomlychosen,weneedabetterapproachfortestingprimality.

Pgina89

968 Chapter31NumberTheoreticAlgorithms

Asweshallsee,alittlemorecleverness,andsomerandomization,willyielda
primalitytestingroutinethatworkswellonallinputs.
Unfortunately,wecannotentirelyeliminatealltheerrorsbysimplychecking
equation(31.40)forasecondbasenumber,sayaD3,becausethereexistcom
positeintegersn,knownasCarmichaelnumbers,thatsatisfyequation(31.40)for
alla2Zn.(Wenotethatequation(31.40)doesfailwhengcd.an/>1that
is,whena62Znbuthopingtodemonstratethatniscompositebyfindingsuch
anacanbedifficultifnhasonlylargeprimefactors.)ThefirstthreeCarmichael
numbersare561,1105,and1729.Carmichaelnumbersareextremelyrareah
are,forexample,only255ofthemlessthan100,000,000.Exercise31.82helps
explainwhytheyaresorare.
Wenextshowhowtoimproveourprimalitytestsothatitwon'tbefooledby
Carmichaelnumbers.

TheMillerRabinrandomizedprimalitytest
TheMillerRabinprimalitytestovercomestheproblemsofthesimpletestPSEU
DOPRIMEwithtwomodifications:
Ittriesseveralrandomlychosenbasevaluesainsteadofjustonebasevalue.
Whilecomputingeachmodularexponentiation,itlooksforanontrivialsquare
rootof1,modulon,duringthefinalsetofsquarings.Ifitfindsone,itstops
andreturnsCOMPOSITE.Corollary31.35fromSection31.6justifiesdetecting
compositesinthismanner.

ThepseudocodefortheMillerRabinprimalitytestfollows.Theinputn>2is
theoddnumbertobetestedforprimality,andsisthenumberofrandomlycho
senbasevaluesfromZCntobetried.Thecodeusestherandomnumbergenerator

https://translate.googleusercontent.com/translate_f 62/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
RANDOMdescribedonpage117:RANDOM.1n1/returnsarandomlychosen
integerasatisfying1an1.ThecodeusesanauxiliaryprocedureWITNESS
suchthatWITNESS.an/isTRUEifandonlyifaisawitnesstothecomposite
nessofnthatis,ifitispossibleusingatoprove(inamannerthatweshallsee)
thatniscomposite.ThetestWITNESS.an/isanextensionof,butmoreeffective
than,thetest
an161.modn/

thatformedthebasis(usingaD2)forPSEUDOPRIME.Wefirstpresentand
justifytheconstructionofWITNESS,andthenweshallshowhowweuseitinthe
MillerRabinprimalitytest.Letn 1D2tuwheret 1anduisoddesdecir,
thebinaryrepresentationofn1isthebinaryrepresentationoftheoddintegeru
followedbyexactlytzeros.Therefore,an1.au/2 t .modn/,sothatwecan

Pgina90

31.8Primalitytesting 969

computean1modnbyfirstcomputingaumodnandthensquaringtheresultt
timessuccessively.

WITNESS.an/
1lettandubesuchthatt 1,uisodd,andn1D2tu
2x0DMODULAREXPONENTIATION.aun/
3foriD1tot
4 xiDx2
i1modn
5 ifxi==1andxi11andxi1n1
6 returnTRUE
7ifxt1
8 returnTRUE
9returnFALSE

ThispseudocodeforWITNESScomputesan1modnbyfirstcomputingthe
valuex0Daumodninline2andthensquaringtheresultttimesinarowinthe
forloopoflines36.Byinductiononi,thesequencex0,x1,...,xtofvalues
computedsatisfiestheequationxia2 u.modn/foriD01:::t,sothatin
i

particularxtan1.modn/.Afterline4performsasquaringstep,however,
theloopmayterminateearlyiflines56detectthatanontrivialsquarerootof1
hasjustbeendiscovered.(Weshallexplainthesetestsshortly.)Ifso,thealgo
rithmstopsandreturnsTRUE.Lines78returnTRUEifthevaluecomputedfor
xtan1.modn/isnotequalto1,justasthePSEUDOPRIMEprocedurereturns
COMPOSITEinthiscase.Line9returnsFALSEifwehaven'treturnedTRUEin
lines6or8.
WenowarguethatifWITNESS.an/returnsTRUE,thenwecanconstructa
proofthatniscompositeusingaasawitness.
IfWITNESSreturnsTRUEfromline8,thenithasdiscoveredthatxt re
an1modn1.Ifnisprime,however,wehavebyFermat'stheorem(Theo
rem31.31)thatan11.modn/foralla2ZC n.Therefore,ncannotbeprime,
andtheequationan1modn1provesthisfact.
IfWITNESSreturnsTRUEfromline6,thenithasdiscoveredthatxi1isanon
trivialsquarerootof1,modulon,sincewehavethatxi161.modn/yet
xix2
i11.modn/.Corollary31.35statesthatonlyifniscompositecan
thereexistanontrivialsquarerootof1modulon,sothatdemonstratingthatxi1
isanontrivialsquarerootof1modulonprovesthatniscomposite.
ThiscompletesourproofofthecorrectnessofWITNESS.Ifwefindthatthecall
WITNESS.an/returnsTRUE,thennissurelycomposite,andthewitnessa,along
withthereasonthattheprocedurereturnsTRUE(diditreturnfromline6orfrom
line8?),providesaproofthatniscomposite.

Pgina91

970 Chapter31NumberTheoreticAlgorithms

https://translate.googleusercontent.com/translate_f 63/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,

Atthispoint,webrieflypresentanalternativedescriptionofthebehaviorof
WITNESSasafunctionofthesequenceXDhx0x1:::xti,whichweshallfind
usefullateron,whenweanalyzetheefficiencyoftheMillerRabinprimalitytest.
NotethatifxiD1forsome0i<t,WITNESSmightnotcomputetherest
ofthesequence.Ifitweretodoso,however,eachvaluexiC1xiC2:::xtwould
be1,andweconsiderthesepositionsinthesequenceXasbeingall1s.Tenemos
fourcases:
1.XDh:::di,whered1:thesequenceXdoesnotendin1.ReturnTRUE
inline8aisawitnesstothecompositenessofn(byFermat'sTheorem).
2.XDh11:::1i:thesequenceXisall1s.ReturnFALSEaisnotawitness
tothecompositenessofn.
3.XDh:::11:::1i:thesequenceXendsin1,andthelastnon1isequal
to1.ReturnFALSEaisnotawitnesstothecompositenessofn.
4.XDh:::d1:::1i,whered1:thesequenceXendsin1,butthelast
non1isnot1.ReturnTRUEinline6aisawitnesstothecompositeness
ofn,sincedisanontrivialsquarerootof1.
WenowexaminetheMillerRabinprimalitytestbasedontheuseofWITNESS.
Again,weassumethatnisanoddintegergreaterthan2.

MILLERRABIN.ns/
1forjD1tos
2 aDRANDOM.1n1/
3 ifWITNESS.an/
4 returnCOMPOSITE //definitely
5returnPRIME //almostsurely
TheprocedureMILLERRABINisaprobabilisticsearchforaproofthatnis
compuesto.Themainloop(beginningonline1)picksuptosrandomvaluesofa
fromZC
n(line2).Ifoneofthea'spickedisawitnesstothecompositenessofn,
thenMILLERRABINreturnsCOMPOSITEonline4.Sucharesultisalwayscor
rect,bythecorrectnessofWITNESS.IfMILLERRABINfindsnowitnessins
trials,thentheprocedureassumesthatthisisbecausenowitnessesexist,andthere
foreitassumesthatnisprime.Weshallseethatthisresultislikelytobecorrect
ifsislargeenough,butthatthereisstillatinychancethattheproceduremaybe
unluckyinitschoiceofa'sandthatwitnessesdoexisteventhoughnonehasbeen
encontr.
ToillustratetheoperationofMILLERRABIN,letnbetheCarmichaelnum
ber561,sothatn 1D560D24 35,tD4,anduD35.Ifthepro
cedurechoosesaD7asabase,Figure31.4inSection31.6showsthatWIT
NESScomputesx0a35241.mod561/andthuscomputesthesequence

Page92

31.8Primalitytesting 971

XDh241298166671i.Thus,WITNESSdiscoversanontrivialsquareroot
of1inthelastsquaringstep,sincea28067.modn/anda5601.modn/.
Therefore,aD7isawitnesstothecompositenessofn,WITNESS.7n/returns
TRUE,andMILLERRABINreturnsCOMPOSITE.
Ifnisabitnumber,MILLERRABINrequiresO.s/arithmeticoperations
andO.s3/bitoperations,sinceitrequiresasymptoticallynomoreworkthans
modularexponentiations.

ErrorrateoftheMillerRabinprimalitytest

IfMILLERRABINreturnsPRIME,thenthereisaveryslimchancethatithasmade
unerror.UnlikePSEUDOPRIME,however,thechanceoferrordoesnotdepend
onntherearenobadinputsforthisprocedure.Rather,itdependsonthesizeofs
andtheluckofthedrawinchoosingbasevaluesa.Moreover,sinceeachtestis
morestringentthanasimplecheckofequation(31.40),wecanexpectongeneral
principlesthattheerrorrateshouldbesmallforrandomlychosenintegersn.los
followingtheorempresentsamorepreciseargument.

Theorem31.38
Ifnisanoddcompositenumber,thenthenumberofwitnessestothecomposite
nessofnisatleast.n1/=2.

https://translate.googleusercontent.com/translate_f 64/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,

ProofTheproofshowsthatthenumberofnonwitnessesisatmost.n1/=2,
whichimpliesthetheorem.
WestartbyclaimingthatanynonwitnessmustbeamemberofZn.Porqu?
Consideranynonwitnessa.Itmustsatisfyan11.modn/or,equivalently,
aan21.modn/.Thus,theequationax1.modn/hasasolution,
namelyan2.ByCorollary31.21,gcd.an/j1,whichinturnimpliesthat
gcd.an/D1.Therefore,aisamemberofZnallnonwitnessesbelongtoZn.
Tocompletetheproof,weshowthatnotonlyareallnonwitnessescontained
inZn,theyareallcontainedinapropersubgroupBofZn(recallthatwesayB
isapropersubgroupofZnwhenBissubgroupofZnbutBisnotequaltoZn).
ByCorollary31.16,wethenhavejBjjZnj=2.SincejZnjn1,weobtain
jBj.n1/=2.Therefore,thenumberofnonwitnessesisatmost.n1/=2,so
thatthenumberofwitnessesmustbeatleast.n1/=2.
WenowshowhowtofindapropersubgroupBofZncontainingallofthe
nonwitnesses.Webreaktheproofintotwocases.
Case1:Thereexistsanx2Znsuchthat
xn161.modn/:

Pgina93

972 Chapter31NumberTheoreticAlgorithms

Inotherwords,nisnotaCarmichaelnumber.Because,aswenotedearlier,
Carmichaelnumbersareextremelyrare,case1isthemaincasethatarisesin
practice(eg,whennhasbeenchosenrandomlyandisbeingtestedforprimal
ity).
LetBDfb2ZnWbn11.modn/g.Clearly,Bisnonempty,since12B.
SinceBisclosedundermultiplicationmodulon,wehavethatBisasubgroup
ofZnbyTheorem31.14.NotethateverynonwitnessbelongstoB,sinceanon
witnessasatisfiesan11.modn/.Sincex2Zn B,wehavethatBisa
propersubgroupofZn.
Case2:Forallx2Zn,
xn11.modn/: (31.41)
Inotherwords,nisaCarmichaelnumber.Thiscaseisextremelyrareinprac
TiceHowever,theMillerRabintest(unlikeapseudoprimalitytest)canefficiently
determinethatCarmichaelnumbersarecomposite,aswenowshow.
Inthiscase,ncannotbeaprimepower.Toseewhy,letussupposetothe
contrarythatnDpe,wherepisaprimeande>1.Wederiveacontradiction
comosigue.Sinceweassumethatnisodd,pmustalsobeodd.Theorem31.32
impliesthatZnisacyclicgroup:itcontainsageneratorgsuchthatordn.g/D
jZnjD.n/Dpe.11=p/D.p1/pe1.(Theformulafor.n/comesfrom
equation(31.20).)Byequation(31.41),wehavegn11.modn/.Thenthe
discretelogarithmtheorem(Theorem31.33,takingyD0)impliesthatn10
.mod.n//,or
.p1/pe1jpe 1

Thisisacontradictionfore>1,since.p 1/pe1isdivisiblebytheprimep
butpe 1isnot.Thus,nisnotaprimepower.
Sincetheoddcompositenumbernisnotaprimepower,wedecomposeitinto
aproductn1n2,wheren1andn2areoddnumbersgreaterthan1thatarerelatively
primetoeachother.(Theremaybeseveralwaystodecomposen,anditdoesnot
matterwhichonewechoose.Forexample,ifnDp e e pe
1 2
1p 2
r
r,thenwecan
choosen1Dp e 1 e e pe
2 3
1andn2Dp 2p 3 r.)
r

Recallthatwedefinetandusothatn1D2tu,wheret 1anduisodd,and
thatforaninputa,theprocedureWITNESScomputesthesequence

XDhaua2ua2 u:::a2 ui
2 t

(allcomputationsareperformedmodulon).
Letuscallapair.j/ofintegersacceptableif2Zn,j2f01:::tg,and
2 u1.modn/:
j

https://translate.googleusercontent.com/translate_f 65/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,

Pgina94

31.8Primalitytesting 973

Acceptablepairscertainlyexistsinceuisoddwecanchoose Dn 1y
jD0,sothat.n10/isanacceptablepair.Nowpickthelargestpossiblejsuch
thatthereexistsanacceptablepair.j/,andfixsothat.j/isanacceptable
par.Dejar
BDfx2ZnWx2 u1.modn/g:
j

SinceBisclosedundermultiplicationmodulon,itisasubgroupofZn.ByTheo
rem31.15,therefore,jBjdividesjZnj.EverynonwitnessmustbeamemberofB,
sincethesequenceXproducedbyanonwitnessmusteitherbeall1sorelsecontain
a1nolaterthanthejthposition,bythemaximalityofj.(If.aj0/isacceptable,
whereaisanonwitness,wemusthavej0jbyhowwechosej.)
Wenowusetheexistenceoftodemonstratethatthereexistsaw2Zn SEGUNDO,
andhencethatBisapropersubgroupofZn.Since2 u1.modn/,wehave
j

2 u1.modn1/byCorollary31.29totheChineseremaindertheorem. Por
j

Corollary31.28,thereexistsawsimultaneouslysatisfyingtheequations
w .modn1/
w1.modn2/:
Porlotanto,
w2 u 1.modn1/
j

w2 u UN
j 1.modn2/:
ByCorollary31.29,w2 u61.modn1/impliesw2 u61.modn/,and
j j

w2 u61.modn2/impliesw2 u61.modn/.Hence,weconcludethat
j j

w2 u61.modn/,andsow62B.
j

Itremainstoshowthatw2Zn,whichwedobyfirstworkingseparatelymod
ulon1andmodulon2.Workingmodulon1,weobservethatsince 2Zn,we
havethatgcd.n/D1,andsoalsogcd.n1/D1ifdoesnothaveanycom
mondivisorswithn,thenitcertainlydoesnothaveanycommondivisorswithn1.
Sincew .modn1/,weseethatgcd.wn1/D1.Workingmodulon2,we
observethatw1.modn2/impliesgcd.wn2/D1.Tocombinetheseresults,
weuseTheorem31.6,whichimpliesthatgcd.wn1n2/Dgcd.wn/D1.Thatis,
w2Zn.
Thereforew2Zn B,andwefinishcase2withtheconclusionthatBisa
propersubgroupofZn.
Ineithercase,weseethatthenumberofwitnessestothecompositenessofnis
atleast.n1/=2.

Theorem31.39
Foranyoddintegern>2andpositiveintegers,theprobabilitythatMILLER
RABIN.ns/errsisatmost2s.

Pgina95

974 Chapter31NumberTheoreticAlgorithms

ProofUsingTheorem31.38,weseethatifniscomposite,theneachexecutionof
theforloopoflines14hasaprobabilityofatleast1=2ofdiscoveringawitnessx
tothecompositenessofn.MILLERRABINmakesanerroronlyifitissounlucky
astomissdiscoveringawitnesstothecompositenessofnoneachofthesiterations
ofthemainloop.Theprobabilityofsuchasequenceofmissesisatmost2s.

Ifnisprime,MILLERRABINalwaysreportsPRIME,andifniscomposite,the
chancethatMILLERRABINreportsPRIMEisatmost2s.
WhenapplyingMILLERRABINtoalargerandomlychosenintegern,however,
weneedtoconsideraswellthepriorprobabilitythatnisprime,inordertocor
rectlyinterpretMILLERRABINsresult.Supposethatwefixabitlengthand
chooseatrandomanintegernoflengthbitstobetestedforprimality.LetA
denotetheeventthatnisprime.Bytheprimenumbertheorem(Theorem31.37),
theprobabilitythatnisprimeisapproximately

https://translate.googleusercontent.com/translate_f 66/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
PrfAg1=lnn
1:443=:
NowletBdenotetheeventthatM
ILLERRABINreturnsPRIME.Wehavethat

Pr BjA D0(orequivalently,thatPrfBjAgD1)andPr
BjA 2s(or
equivalently,thatPr BjA >12s).
ButwhatisPrfAjBg,theprobabilitythatnisprime,giventhatMILLER
RABINhasreturnedPRIME?BythealternateformofBayes'stheorem(equa
tion(C.18))wehave
PrfAgPrfBjAg
PrfAjBgD
PrfAgPrfBjAgCPr UNPr BjA
1
:
1C2s.lnn1/
Thisprobabilitydoesnotexceed1=2untilsexceedslg.lnn1/.Intuitively,that
manyinitialtrialsareneededjustfortheconfidencederivedfromfailingtofinda
witnesstothecompositenessofntoovercomethepriorbiasinfavorofnbeing
compuesto.ForanumberwithD1024bits,thisinitialtestingrequiresabout
lg.lnn1/ lg.=1:443/
9
trials.Inanycase,choosingsD50shouldsufficeforalmostanyimaginable
solicitud.
Infact,thesituationismuchbetter.Ifwearetryingtofindlargeprimesby
applyingMILLERRABINtolargerandomlychosenoddintegers,thenchoosing
asmallvalueofs(say3)isveryunlikelytoleadtoerroneousresults,though

Pgina96

31.9Integerfactorization 975

wewon'tproveithere.Thereasonisthatforarandomlychosenoddcomposite
integern,theexpectednumberofnonwitnessestothecompositenessofnislikely
tobeverymuchsmallerthan.n1/=2.
Iftheintegernisnotchosenrandomly,however,thebestthatcanbeprovenis
thatthenumberofnonwitnessesisatmost.n1/=4,usinganimprovedversion
ofTheorem31.38.Furthermore,theredoexistintegersnforwhichthenumberof
nonwitnessesis.n1/=4.

Exercises

31.81
Provethatifanoddintegern>1isnotaprimeoraprimepower,thenthereexists
anontrivialsquarerootof1modulon.

31.82?
ItispossibletostrengthenEuler'stheoremslightlytotheform
a.n/1.modn/foralla2Zn

wherenDp 1e 1
pe and.n/isdefinedby
R
r

.n/Dlcm..p e 1
(31.42)
1/:::.pe r//:
r

Provethat.n/j .n/.AcompositenumbernisaCarmichaelnumberif
.n/jn1.ThesmallestCarmichaelnumberis561D31117aqu,
.n/Dlcm.21016/D80,whichdivides560.ProvethatCarmichaelnum
bersmustbebothsquarefree(notdivisiblebythesquareofanyprime)andthe
productofatleastthreeprimes.(Forthisreason,theyarenotverycommon.)

31.83
Provethatifxisanontrivialsquarerootof1,modulon,thengcd.x 1n/and
gcd.xC1n/arebothnontrivialdivisorsofn.

?31.9Integerfactorization

Supposewehaveanintegernthatwewishtofactor,thatis,todecomposeintoa
https://translate.googleusercontent.com/translate_f 67/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
productofprimes.Theprimalitytestoftheprecedingsectionmaytellusthatnis
composite,butitdoesnottellustheprimefactorsofn.Factoringalargeintegern
seemstobemuchmoredifficultthansimplydeterminingwhethernisprimeor
compuesto.Evenwithtoday'ssupercomputersandthebestalgorithmstodate,we
cannotfeasiblyfactoranarbitrary1024bitnumber.

Page97

976 Chapter31NumberTheoreticAlgorithms

Pollard'srhoheuristic
TrialdivisionbyallintegersuptoRisguaranteedtofactorcompletelyanynumber
uptoR2.Forthesameamountofwork,thefollowingprocedure,POLLARDRHO,
factorsanynumberuptoR4(unlessweareunlucky).Sincetheprocedureisonly
aheuristic,neitheritsrunningtimenoritssuccessisguaranteed,althoughthe
procedureishighlyeffectiveinpractice.AnotheradvantageofthePOLLARD
RHOprocedureisthatitusesonlyaconstantnumberofmemorylocations.(Ifyou
wantedto,youcouldeasilyimplementPOLLARDRHOonaprogrammablepocket
calculatortofindfactorsofsmallnumbers.)

POLLARDRHO.n/
1iD1
2x1DRANDOM.0n1/
3yDx1
4kD2
5whileTRUE
6 Idic1
7 xiD.x2 1/modn
i1
8 dDgcd.yxin/
9 ifd1anddn
10 printd
11 ifi==k
12 yDxi
13 kD2k

Theprocedureworksasfollows.Lines12initializeito1andx1toarandomly
chosenvalueinZn.Thewhileloopbeginningonline5iteratesforever,searching
forfactorsofn.Duringeachiterationofthewhileloop,line7usestherecurrence
xiD.x2 1/modn (31.43)
i1
toproducethenextvalueofxiintheinfinitesequence
x1x2x3x4::: (31.44)
withline6correspondinglyincrementingi.Thepseudocodeiswrittenusingsub
scriptedvariablesxiforclarity,buttheprogramworksthesameifallofthesub
scriptsaredropped,sinceonlythemostrecentvalueofxineedstobemaintained.
Withthismodification,theprocedureusesonlyaconstantnumberofmemorylo
Cationes.
Everysooften,theprogramsavesthemostrecentlygeneratedxivalueinthe
variabley.Specifically,thevaluesthataresavedaretheoneswhosesubscriptsare
powersof2:

Pgina98

31.9Integerfactorization 977

x1x2x4x8x16::::

Line3savesthevaluex1,andline12savesxkwheneveriisequaltok.El
variablekisinitializedto2inline4,andline13doublesitwheneverline12
updatesy.Therefore,kfollowsthesequence1248:::andalwaysgivesthe
subscriptofthenextvaluexktobesavediny.
Lines810trytofindafactorofn,usingthesavedvalueofyandthecur
rentvalueofxi.Specifically,line8computesthegreatestcommondivisor
https://translate.googleusercontent.com/translate_f 68/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
dDgcd.yxin/.Ifline9findsdtobeanontrivialdivisorofn,thenline10
printsd.
Thisprocedureforfindingafactormayseemsomewhatmysteriousatfirst.
Note,however,thatPOLLARDRHOneverprintsanincorrectansweranynum
beritprintsisanontrivialdivisorofn.POLLARDRHOmightnotprintanything
atall,thoughitcomeswithnoguaranteethatitwillprintanydivisors.Weshall
see,however,thatwehavegoodreasontoexpectPOLLARDRHOtoprintafac
pag
torpofnafter. p/iterationsofthewhileloop.Thus,ifniscomposite,we
canexpectthisproceduretodiscoverenoughdivisorstofactorncompletelyafter
approximatelyn1=4updates,sinceeveryprimefactorpofnexceptpossiblythe
pag
largestoneislessthan norte.
Webeginouranalysisofhowthisprocedurebehavesbystudyinghowlong
ittakesarandomsequencemodulontorepeatavalue.SinceZnisfinite,and
sinceeachvalueinthesequence(31.44)dependsonlyonthepreviousvalue,the
sequence(31.44)eventuallyrepeatsitself.OncewereachanxisuchthatxiDxj
forsomej<i,weareinacycle,sincexiC1DxjC1,xiC2DxjC2,andsoon.
Thereasonforthenamerhoheuristicisthat,asFigure31.7shows,wecandraw
thesequencex1x2:::xj1asthetailoftherhoandthecyclexjxjC1:::xi
asthebodyoftherho.
Letusconsiderthequestionofhowlongittakesforthesequenceofxitorepeat.
Thisinformationisnotexactlywhatweneed,butweshallseelaterhowtomodify
theargument.Forthepurposeofthisestimation,letusassumethatthefunction
fn.x/D.x2 1/modn

behaveslikearandomfunction.Ofcourse,itisnotreallyrandom,butthisas
sumptionyieldsresultsconsistentwiththeobservedbehaviorofPOLLARDRHO.
WecanthenconsidereachxitohavebeenindependentlydrawnfromZnaccording
toauniformdistributiononZ
pag n.BythebirthdayparadoxanalysisofSection5.4.1,

weexpect. n/stepstobetakenbeforethesequencecycles.
Nowfortherequiredmodification.Letpbeanontrivialfactorofnsuchthat
gcd.pn=p/D1.Forexample,ifnhasthefactorizationnDp e e pe 1 2
1p 2 r,then
r

wemaytakeptobep 1. e 1
(Ife1D1,thenpisjustthesmallestprimefactorofn,
agoodexampletokeepinmind.)

Pgina99

978 Chapter31NumberTheoreticAlgorithms

996 310
814 396

x7
00
x 177 7 84
31

x 1186
6 120 x6 18
00
11

x 1194 339
5 529 x5 26
00
47
595 1053
x 63 x x4 63
6 4
0
4 00

x7
0

x 3 8 x3 0 8 x6
0 x3 00 8

diecisis
x 2 3 x2 0 3 x5
0
x2 00 3

x 1 2 x1 0 2 x1
00 2
mod1387 mod19 mod73
(un) (segundo) (do)

Figure31.7Pollard'srhoheuristic.(a)ThevaluesproducedbytherecurrencexiC1re
.xyo
2 1/mod1387,startingwithx1D2.Theprimefactorizationof1387is1973.Theheavy
arrowsindicatetheiterationstepsthatareexecutedbeforethefactor19isdiscovered.Thelight
arrowspointtounreachedvaluesintheiteration,toillustratetherhoshape.Theshadedvaluesare
theyvaluesstoredbyPOLLARDRHO.Thefactor19isdiscovereduponreachingx7D177,when
gcd.63 1771387/D19iscomputed.Thefirstxvaluethatwouldberepeatedis1186,butthe
factor19isdiscoveredbeforethisvalueisrepeated.(b)Thevaluesproducedbythesamerecurrence,
modulo19.Everyvaluexigiveninpart(a)isequivalent,modulo19,tothevaluex0
https://translate.googleusercontent.com/translate_f 69/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
Forexample,bothx4D63andx7D177areequivalentto6,modulo19.(c)Thevaluesproducedishownhere.
bythesamerecurrence,modulo73.Everyvaluexigiveninpart(a)isequivalent,modulo73,tothe
valuex
ishownhere.BytheChineseremaindertheorem,eachnodeinpart(a)correspondstoapair
00

ofnodes,onefrompart(b)andonefrompart(c).

Thesequencehxiiinducesacorrespondingsequencehx0
iimodulop,where
x0
iDximodp
foralli.
Furthermore,becausefnisdefinedusingonlyarithmeticoperations(squaring
andsubtraction)modulon,wecancomputex0iC1fromx0 ithemodulopviewof

Pgina100

31.9Integerfactorization 979

thesequenceisasmallerversionofwhatishappeningmodulon:
x0 DxiC1modp
iC1
Dfn.xi/modp
D..x2 1/modn/modp
yo
D.x2 1/modp (byExercise31.17)
yo
D..ximodp/2 1/modp
D..x0 1/modp
i/2
Dfp.x0 i/:
Thus,althoughwearenotexplicitlycomputingthesequencehx0ii,thissequenceis
welldefinedandobeysthesamerecurrenceasthesequencehxii.
Reasoningasbefore,wefindthattheexpectednumberofstepsbeforethese
pag
quencehx0 p/.Ifpissmallcomparedton,thesequencehx0
iirepeatsis. iimight
repeatmuchmorequicklythanthesequencehxii.Indeed,asparts(b)and(c)of
Figure31.7show,thehx0
iisequencerepeatsassoonastwoelementsofthese
quencehxiiaremerelyequivalentmodulop,ratherthanequivalentmodulon.
Lettdenotetheindexofthefirstrepeatedvalueinthehx0 iisequence,andlet
u>0denotethelengthofthecyclethathasbeentherebyproduced.Thatis,t
andu>0arethesmallestvaluessuchthatx0 tCiDx0tCuCiforalli 0.Bythe
pag
abovearguments,theexpectedvaluesoftanduareboth. p/.Notethatif
xtCi
0
Dx0tCuCi,thenpj.xtCuCi
xtCi/.Thus,gcd.xtCuCi xtCin/>1.
Therefore,oncePOLLARDRHOhassavedasyanyvaluexksuchthatk T,
thenymodpisalwaysonthecyclemodulop.(Ifanewvalueissavedasy,
thatvalueisalsoonthecyclemodulop.)Eventually,kissettoavaluethat
isgreaterthanu,andtheprocedurethenmakesanentirelooparoundthecycle
modulopwithoutchangingthevalueofy.Theprocedurethendiscoversafactor
ofnwhenxirunsintothepreviouslystoredvalueofy,modulop,thatis,when
xiy.modp/.
Presumably,thefactorfoundisthefactorp,althoughitmayoccasionallyhap
penthatamultipleofpisdiscovered.Sincetheexpectedvaluesofbothtanduare
pag pag
. p/,theexpectednumberofstepsrequiredtoproducethefactorpis. p/.
Thisalgorithmmightnotperformquiteasexpected,fortworeasons.Primeroel
heuristicanalysisoftherunningtimeisnotrigorous,anditispossiblethatthecycle
pag
ofvalues,modulop,couldbemuchlargerthan pag.Inthiscase,thealgorithm
performscorrectlybutmuchmoreslowlythandesired.Inpractice,thisissueseems
tobemoot.Second,thedivisorsofnproducedbythisalgorithmmightalwaysbe
oneofthetrivialfactors1orn.Forexample,supposethatnDpq,wherep
andqareprime.Itcanhappenthatthevaluesoftanduforpareidenticalwith
thevaluesoftanduforq,andthusthefactorpisalwaysrevealedinthesame
gcdoperationthatrevealsthefactorq.Sincebothfactorsarerevealedatthesame

Pgina101

980 Chapter31NumberTheoreticAlgorithms

time,thetrivialfactorpqDnisrevealed,whichisuseless.Again,thisproblem
https://translate.googleusercontent.com/translate_f 70/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
seemstobeinsignificantinpractice.Ifnecessary,wecanrestarttheheuristicwith
adifferentrecurrenceoftheformxiC1D.x2 c/modn.(Weshouldavoidthe
yo
valuescD0andcD2forreasonswewillnotgointohere,butothervaluesare
fine.)
Ofcourse,thisanalysisisheuristicandnotrigorous,sincetherecurrenceis
notreallyrandom.Nonetheless,theprocedureperformswellinpractice,and
itseemstobeasefficientasthisheuristicanalysisindicates.Itisthemethodof
choiceforfindingsmallprimefactorsofalargenumber.Tofactorabitcompos
itenumberncompletely,weonlyneedtofindallprimefactorslessthanbn1=2c,
andsoweexpectPOLLARDRHOtorequireatmostn1=4D2=4arithmeticopera
tionsandatmostn1=42D2=42bitoperations.POLLARDRHOsabilitytofind
pag
asmallfactorpofnwithanexpectednumber. p/ofarithmeticoperationsis
oftenitsmostappealingfeature.

Exercises

31.91
ReferringtotheexecutionhistoryshowninFigure31.7(a),whendoesPOLLARD
RHOprintthefactor73of1387?

31.92
SupposethatwearegivenafunctionfWZn!Znandaninitialvaluex02Zn.
DefinexiDf.xi1/foriD12:::.Lettandu>0bethesmallestvaluessuch
thatxtCiDxtCuCiforiD01:::.IntheterminologyofPollard'srhoalgorithm,
tisthelengthofthetailanduisthelengthofthecycleoftherho.Giveanefficient
algorithmtodeterminetanduexactly,andanalyzeitsrunningtime.

31.93
HowmanystepswouldyouexpectPOLLARDRHOtorequiretodiscoverafactor
oftheformpe,wherepisprimeande>1?

31.94?
OnedisadvantageofPOLLARDRHOaswrittenisthatitrequiresonegcdcompu
tationforeachstepoftherecurrence.Instead,wecouldbatchthegcdcomputa
tionsbyaccumulatingtheproductofseveralxivaluesinarowandthenusingthis
productinsteadofxiinthegcdcomputation.Describecarefullyhowyouwould
implementthisidea,whyitworks,andwhatbatchsizeyouwouldpickasthemost
effectivewhenworkingonabitnumbern.

Pgina102

ProblemsforChapter31 981

Problemas

311Binarygcdalgorithm
Mostcomputerscanperformtheoperationsofsubtraction,testingtheparity(odd
oreven)ofabinaryinteger,andhalvingmorequicklythancomputingremainders.
Thisprobleminvestigatesthebinarygcdalgorithm,whichavoidstheremainder
computationsusedinEuclid'salgorithm.

a.Provethatifaandbarebotheven,thengcd.ab/D2gcd.a=2b=2/.

segundo.Provethatifaisoddandbiseven,thengcd.ab/Dgcd.ab=2/.

do.Provethatifaandbarebothodd,thengcd.ab/Dgcd..ab/=2b/.

re.Designanefficientbinarygcdalgorithmforinputintegersaandb,where
un b,thatrunsinO.lga/time.Assumethateachsubtraction,paritytest,
andhalvingtakesunittime.

312AnalysisofbitoperationsinEuclid'salgorithm
a.Considertheordinarypaperandpencilalgorithmforlongdivision:dividing
abyb,whichyieldsaquotientqandremainderr.Showthatthismethod
requiresO..1Clgq/lgb/bitoperations.
https://translate.googleusercontent.com/translate_f 71/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,

segundo.Define.ab/D.1Clga/.1Clgb/.Showthatthenumberofbitoperations
performedbyEUCLIDinreducingtheproblemofcomputinggcd.ab/tothat
ofcomputinggcd.bamodb/isatmostc..ab/ .bamodb//forsome
sufficientlylargeconstantc>0.

do.ShowthatEUCLID.ab/requiresO..ab//bitoperationsingeneraland
O.2/bitoperationswhenappliedtotwobitinputs.

313ThreealgorithmsforFibonaccinumbers
ThisproblemcomparestheefficiencyofthreemethodsforcomputingthenthFi
bonaccinumberFn,givenn.Assumethatthecostofadding,subtracting,ormul
tiplyingtwonumbersisO.1/,independentofthesizeofthenumbers.

a.Showthattherunningtimeofthestraightforwardrecursivemethodforcom
putingFnbasedonrecurrence(3.22)isexponentialinn.(See,forexample,the
FIBprocedureonpage775.)

segundo.ShowhowtocomputeFninOn/timeusingmemoization.

Pgina103

982 Chapter31NumberTheoreticAlgorithms

do.ShowhowtocomputeFninO.lgn/timeusingonlyintegeradditionandmul
tiplication.(Hint:Considerthematrix
UN UN
01
11

anditspowers.)

re.Assumenowthataddingtwobitnumberstakes./timeandthatmulti
plyingtwobitnumberstakes.2/time.Whatistherunningtimeofthese
threemethodsunderthismorereasonablecostmeasurefortheelementaryarith
meticoperations?

314Quadraticresidues
Letpbeanoddprime.Anumbera2Zpisaquadraticresidueiftheequation
x2Da.modp/hasasolutionfortheunknownx.

a.Showthatthereareexactly.p1/=2quadraticresidues,modulop.

segundo.Ifpisprime,wedefinetheLegendresymbol.un
p/,fora2Zp,tobe1ifaisa
quadraticresiduemodulopand1otherwise.Provethatifa2Zp,then
UN
un
a.p1/=2.modp/:
pag

Giveanefficientalgorithmthatdetermineswhetheragivennumberaisaqua
draticresiduemodulop.Analyzetheefficiencyofyouralgorithm.

do.Provethatifpisaprimeoftheform4kC3andaisaquadraticresidueinZp,
thenakC1modpisasquarerootofa,modulop.Howmuchtimeisrequired
tofindthesquarerootofaquadraticresidueamodulop?

re.Describeanefficientrandomizedalgorithmforfindinganonquadraticresidue,
moduloanarbitraryprimep,thatis,amemberofZpthatisnotaquadratic
residuo.Howmanyarithmeticoperationsdoesyouralgorithmrequireonaver
age?

Chapternotes

NivenandZuckerman[265]provideanexcellentintroductiontoelementarynum
bertheory.Knuth[210]containsagooddiscussionofalgorithmsforfindingthe

https://translate.googleusercontent.com/translate_f 72/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,

Pgina104

NotesforChapter31 983

greatestcommondivisor,aswellasotherbasicnumbertheoreticalgorithms.Llevarunavidadesoltero
[30]andRiesel[295]providemorerecentsurveysofcomputationalnumberthe
OryDixon[91]givesanoverviewoffactorizationandprimalitytesting.los
conferenceproceedingseditedbyPomerance[280]containsseveralexcellentsur
veyarticles.Morerecently,BachandShallit[31]haveprovidedanexceptional
overviewofthebasicsofcomputationalnumbertheory.
Knuth[210]discussestheoriginofEuclid'salgorithm.ItappearsinBook7,
Propositions1and2,oftheGreekmathematicianEuclid'sElements,whichwas
writtenaround300B.C.Euclid'sdescriptionmayhavebeenderivedfromanal
gorithmduetoEudoxusaround375B.C.Euclid'salgorithmmayholdthehonor
ofbeingtheoldestnontrivialalgorithmitisrivaledonlybyanalgorithmformul
tiplicationknowntotheancientEgyptians.Shallit[312]chroniclesthehistoryof
theanalysisofEuclid'salgorithm.
KnuthattributesaspecialcaseoftheChineseremaindertheorem(Theo
rem31.27)totheChinesemathematicianSunTsu,wholivedsometimebetween
200B.C.andA.D.200thedateisquiteuncertain.Thesamespecialcasewas
givenbytheGreekmathematicianNichomachusaroundA.D.100.Itwasgener
alizedbyChhinChiuShaoin1247.TheChineseremaindertheoremwasfinally
statedandprovedinitsfullgeneralitybyL.Eulerin1734.
TherandomizedprimalitytestingalgorithmpresentedhereisduetoMiller[255]
andRabin[289]itisthefastestrandomizedprimalitytestingalgorithmknown,
towithinconstantfactors.TheproofofTheorem31.39isaslightadaptationof
onesuggestedbyBach[29].AproofofastrongerresultforMILLERRABIN
wasgivenbyMonier[258,259].Formanyyearsprimalitytestingwastheclassic
exampleofaproblemwhererandomizationappearedtobenecessarytoobtain
anefficient(polynomialtime)algorithm.In2002,however,Agrawal,Kayal,and
Saxema[4]surprisedeveryonewiththeirdeterministicpolynomialtimeprimality
testingalgorithm.Untilthen,thefastestdeterministicprimalitytestingalgorithm
known,duetoCohenandLenstra[73],ranintime.lgn/O.lglglgn/oninputn,which
isjustslightlysuperpolynomial.Nonetheless,forpracticalpurposesrandomized
primalitytestingalgorithmsremainmoreefficientandarepreferred.
Theproblemoffindinglargerandomprimesisnicelydiscussedinanarticle
byBeauchemin,Brassard,Crpeau,Goutier,andPomerance[36].
TheconceptofapublickeycryptosystemisduetoDiffieandHellman[87].
TheRSAcryptosystemwasproposedin1977byRivest,Shamir,andAdleman
[296].Sincethen,thefieldofcryptographyhasblossomed.Ourunderstanding
oftheRSAcryptosystemhasdeepened,andmodernimplementationsusesignif
icantrefinementsofthebasictechniquespresentedhere.Inaddition,manynew
techniqueshavebeendevelopedforprovingcryptosystemstobesecure.Forex
ample,GoldwasserandMicali[142]showthatrandomizationcanbeaneffective
toolinthedesignofsecurepublickeyencryptionschemes.Forsignatureschemes,

Pgina105

984 Chapter31NumberTheoreticAlgorithms

Goldwasser,Micali,andRivest[143]presentadigitalsignatureschemeforwhich
everyconceivabletypeofforgeryisprovablyasdifficultasfactoring.Menezes,
vanOorschot,andVanstone[254]provideanoverviewofappliedcryptography.
TherhoheuristicforintegerfactorizationwasinventedbyPollard[277].los
versionpresentedhereisavariantproposedbyBrent[56].
Thebestalgorithmsforfactoringlargenumbershavearunningtimethatgrows
roughlyexponentiallywiththecuberootofthelengthofthenumberntobefac
tored.Thegeneralnumberfieldsievefactoringalgorithm(asdevelopedbyBuh
ler,Lenstra,andPomerance[57]asanextensionoftheideasinthenumberfield
sievefactoringalgorithmbyPollard[278]andLenstraetal.[232]andrefinedby
Coppersmith[77]andothers)isperhapsthemostefficientsuchalgorithmingen
eralforlargeinputs.Althoughitisdifficulttogivearigorousanalysisofthis
algorithm,underreasonableassumptionswecanderivearunningtimeestimateof
L.1=3n/1:902Co.1/,whereL.n/De.lnn/ .lnlnn/
1 .
TheellipticcurvemethodduetoLenstra[233]maybemoreeffectiveforsome
inputsthanthenumberfieldsievemethod,since,likePollard'srhomethod,itcan
https://translate.googleusercontent.com/translate_f 73/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
findasmallprimefactorpquitequickly.Withthismethod,thetimetofindpis
pag
estimatedtobeL.1=2p/ 2Co.1/.

Pgina106

32 StringMatching

Texteditingprogramsfrequentlyneedtofindalloccurrencesofapatterninthe
text.Typically,thetextisadocumentbeingedited,andthepatternsearchedforisa
particularwordsuppliedbytheuser.Efficientalgorithmsforthisproblemcalled
stringmatchingcangreatlyaidtheresponsivenessofthetexteditingprogram.
Amongtheirmanyotherapplications,stringmatchingalgorithmssearchforpar
ticularpatternsinDNAsequences.Internetsearchenginesalsousethemtofind
Webpagesrelevanttoqueries.
Weformalizethestringmatchingproblemasfollows.Weassumethatthe
textisanarrayT1::noflengthnandthatthepatternisanarrayP1::m
oflengthmn.WefurtherassumethattheelementsofPandTarechar
actersdrawnfromafinitealphabet.Forexample,wemayhaveDf0,1g
orDfab:::zg.ThecharacterarraysPandTareoftencalledstringsof
caracteres.
ReferringtoFigure32.1,wesaythatpatternPoccurswithshiftsintextT
(or,equivalently,thatpatternPoccursbeginningatpositionsC1intextT)if
0snmandTsC1::sCmDP1::m(thatis,ifTsCjDPj,for
1jm).IfPoccurswithshiftsinT,thenwecallsavalidshiftotherwise,
wecallsaninvalidshift.Thestringmatchingproblemistheproblemoffinding
allvalidshiftswithwhichagivenpatternPoccursinagiventextT.

textT abcabaabcabac

s=3
patternP abaa

https://translate.googleusercontent.com/translate_f 74/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
Figure32.1Anexampleofthestringmatchingproblem,wherewewanttofindalloccurrencesof
thepatternPDabaainthetextTDabcabaabcabac.Thepatternoccursonlyonceinthetext,
atshiftsD3,whichwecallavalidshift.Averticallineconnectseachcharacterofthepatterntoits
matchingcharacterinthetext,andallmatchedcharactersareshaded.

Pgina107

986 Chapter32StringMatching

Algoritmo Preprocessingtime Matchingtime


Ingenuo 0 O..nmC1/m/
RabinKarp .m/ O..nmC1/m/
Finiteautomaton O.mjj/ .n/
KnuthMorrisPratt .m/ .n/

Figure32.2Thestringmatchingalgorithmsinthischapterandtheirpreprocessingandmatching
veces.

Exceptforthenaivebruteforcealgorithm,whichwereviewinSection32.1,
eachstringmatchingalgorithminthischapterperformssomepreprocessingbased
onthepatternandthenfindsallvalidshiftswecallthislatterphasematching.
Figure32.2showsthepreprocessingandmatchingtimesforeachofthealgorithms
inthischapter.Thetotalrunningtimeofeachalgorithmisthesumoftheprepro
cessingandmatchingtimes.Section32.2presentsaninterestingstringmatching
algorithm,duetoRabinandKarp.Althoughthe..n mC1/m/worstcase
runningtimeofthisalgorithmisnobetterthanthatofthenaivemethod,itworks
muchbetteronaverageandinpractice.Italsogeneralizesnicelytootherpattern
matchingproblems.Section32.3thendescribesastringmatchingalgorithmthat
beginsbyconstructingafiniteautomatonspecificallydesignedtosearchforoccur
rencesofthegivenpatternPinatext.ThisalgorithmtakesO.mjj/preprocess
ingtime,butonly.n/matchingtime.Section32.4presentsthesimilar,butmuch
cleverer,KnuthMorrisPratt(orKMP)algorithmithasthesame.n/matching
time,anditreducesthepreprocessingtimetoonly.m/.

Notationandterminology
Wedenoteby(readsigmastar)thesetofallfinitelengthstringsformed
usingcharactersfromthealphabet.Inthischapter,weconsideronlyfinite
lengthstrings.Thezerolengthemptystring,denoted",alsobelongsto.The
lengthofastringxisdenotedjxj.Theconcatenationoftwostringsxandy,
denotedxy,haslengthjxjCjyjandconsistsofthecharactersfromxfollowedby
thecharactersfromy.
Wesaythatastringwisaprefixofastringx,denotedwx,ifxDwyfor
somestringy2.Notethatifwx,thenjwjjxj.Similarly,wesaythata
stringwisasuffixofastringx,denotedwZx,ifxDywforsomey2.Como
withaprefix,wZximpliesjwjjxj.Forexample,wehaveababccaand
ccaZabcca.Theemptystring"isbothasuffixandaprefixofeverystring.For
anystringsxandyandanycharactera,wehavexZyifandonlyifxaZya.

Page108

Chapter32StringMatching 987

x x x

Z Z Z

Y Y Y

x x x
https://translate.googleusercontent.com/translate_f 75/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,

Y Y Y
(un) (segundo) (do)

Figure32.3AgraphicalproofofLemma32.1.Wesupposethatx=andy=.Thethreeparts
ofthefigureillustratethethreecasesofthelemma.Verticallinesconnectmatchingregions(shown
shaded)ofthestrings.(a)Ifjxjjyj,thenx=y.(b)Ifjxjjyj,theny=x.(c)IfjxjDjyj,
thenxDy.

AlsonotethatandZaretransitiverelations.Thefollowinglemmawillbeuseful
luego.

Lemma32.1(Overlappingsuffixlemma)
Supposethatx,y,andarestringssuchthatxZandyZ.Ifjxjjyj,
thenxZy.Ifjxjjyj,thenyZx.IfjxjDjyj,thenxDy.

ProofSeeFigure32.3foragraphicalproof.

Forbrevityofnotation,wedenotethekcharacterprefixP1::kofthepattern
P1::mbyPk.Thus,P0D"andPmDPDP1::m.Similarly,wedenote
thekcharacterprefixofthetextTbyTk.Usingthisnotation,wecanstatethe
stringmatchingproblemasthatoffindingallshiftssintherange0snm
suchthatPZTsCm.
Inourpseudocode,weallowtwoequallengthstringstobecomparedforequal
ityasaprimitiveoperation.Ifthestringsarecomparedfromlefttorightandthe
comparisonstopswhenamismatchisdiscovered,weassumethatthetimetaken
bysuchatestisalinearfunctionofthenumberofmatchingcharactersdiscovered.
Tobeprecise,thetestx==yisassumedtotaketime.tC1/,wheretisthe
lengthofthelongeststringsuchthatxandy.(Wewrite.tC1/
ratherthan.t/tohandlethecaseinwhichtD0thefirstcharacterscompared
donotmatch,butittakesapositiveamountoftimetoperformthiscomparison.)

Page109

988 Chapter32StringMatching

32.1Thenaivestringmatchingalgorithm

Thenaivealgorithmfindsallvalidshiftsusingaloopthatchecksthecondition
P1::mDTsC1::sCmforeachofthenmC1possiblevaluesofs.

NAIVESTRINGMATCHER.TP/
1nDT:length
2mDP:length
3forsD0tonm
4 ifP1::m==TsC1::sCm
5 printPatternoccurswithshifts

Figure32.4portraysthenaivestringmatchingprocedureasslidingatemplate
containingthepatternoverthetext,notingforwhichshiftsallofthecharacters
onthetemplateequalthecorrespondingcharactersinthetext.Theforloopof
lines35considerseachpossibleshiftexplicitly.Thetestinline4determines
whetherthecurrentshiftisvalidthistestimplicitlyloopstocheckcorresponding
characterpositionsuntilallpositionsmatchsuccessfullyoramismatchisfound.
Line5printsouteachvalidshifts.
ProcedureNAIVESTRINGMATCHERtakestimeO..nmC1/m/,andthis
boundistightintheworstcase.Forexample,considerthetextstringan(astring
ofna's)andthepatternam.ForeachofthenmC1possiblevaluesoftheshifts,
theimplicitlooponline4tocomparecorrespondingcharactersmustexecutem
timestovalidatetheshift.Theworstcaserunningtimeisthus..nmC1/m/,
whichis.n2/ifmDbn=2c.Becauseitrequiresnopreprocessing,NAIVE
STRINGMATCHERsrunningtimeequalsitsmatchingtime.

acaabc acaabc acaabc acaabc

https://translate.googleusercontent.com/translate_f 76/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,

s=0 s=1 s=2 s=3


aab aab aab aab

(un) (segundo) (do) (re)

Figure32.4TheoperationofthenaivestringmatcherforthepatternPDaabandthetext
TDacaabc.WecanimaginethepatternPasatemplatethatweslidenexttothetext.(a)(d)The
foursuccessivealignmentstriedbythenaivestringmatcher.Ineachpart,verticallinesconnectcor
respondingregionsfoundtomatch(shownshaded),andajaggedlineconnectsthefirstmismatched
characterfound,ifany.Thealgorithmfindsoneoccurrenceofthepattern,atshiftsD2,shownin
part(c).

Pgina110

32.1Thenaivestringmatchingalgorithm 989

Asweshallsee,NAIVESTRINGMATCHERisnotanoptimalprocedureforthis
problema.Indeed,inthischapterweshallseethattheKnuthMorrisPrattalgorithm
ismuchbetterintheworstcase.Thenaivestringmatcherisinefficientbecause
itentirelyignoresinformationgainedaboutthetextforonevalueofswhenit
considersothervaluesofs.Suchinformationcanbequitevaluable,however.por
example,ifPDaaabandwefindthatsD0isvalid,thennoneoftheshifts1,2,
or3arevalid,sinceT4Db.Inthefollowingsections,weexamineseveralways
tomakeeffectiveuseofthissortofinformation.

Exercises

32.11
ShowthecomparisonsthenaivestringmatchermakesforthepatternPD0001
inthetextTD000010001010001.

32.12
SupposethatallcharactersinthepatternParedifferent.Showhowtoaccelerate
NAIVESTRINGMATCHERtorunintimeOn/onanncharactertextT.

32.13
SupposethatpatternPandtextTarerandomlychosenstringsoflengthmandn,
respectively,fromthedaryalphabetdDf01:::re 1g,whered 2.Show
thattheexpectednumberofcharactertocharactercomparisonsmadebytheim
plicitloopinline4ofthenaivealgorithmis
1dm
.nmC1/ 2.nmC1/
1d1
overallexecutionsofthisloop.(Assumethatthenaivealgorithmstopscomparing
charactersforagivenshiftonceitfindsamismatchormatchestheentirepattern.)
Thus,forrandomlychosenstrings,thenaivealgorithmisquiteefficient.

32.14
SupposeweallowthepatternPtocontainoccurrencesofagapcharacter}that
canmatchanarbitrarystringofcharacters(evenoneofzerolength).Porejemplo,
thepatternab}ba}coccursinthetextcabccbacbacabas
' ' ' " '
cab Cc licenciadoenLetras
cba do Ab
Ab } licenciadoenLetras
} do
ycomo
' ' ' '
cab ccbac licenciadoenLetras
do ab:
Ab } licenciadoenLetras
} do

Pgina111

990 Chapter32StringMatching

Notethatthegapcharactermayoccuranarbitrarynumberoftimesinthepattern
butnotatallinthetext.Giveapolynomialtimealgorithmtodeterminewhether

https://translate.googleusercontent.com/translate_f 77/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
suchapatternPoccursinagiventextT,andanalyzetherunningtimeofyour
algoritmo.

32.2TheRabinKarpalgorithm

RabinandKarpproposedastringmatchingalgorithmthatperformswellinprac
ticeandthatalsogeneralizestootheralgorithmsforrelatedproblems,suchas
twodimensionalpatternmatching.TheRabinKarpalgorithmuses.m/prepro
cessingtime,anditsworstcaserunningtimeis..nmC1/m/.Basedoncertain
assumptions,however,itsaveragecaserunningtimeisbetter.
Thisalgorithmmakesuseofelementarynumbertheoreticnotionssuchasthe
equivalenceoftwonumbersmoduloathirdnumber.Youmightwanttoreferto
Section31.1fortherelevantdefinitions.
Forexpositorypurposes,letusassumethatDf012:::9g,sothateach
characterisadecimaldigit.(Inthegeneralcase,wecanassumethateachcharac
terisadigitinradixdnotation,wheredDjj.)Wecanthenviewastringofk
consecutivecharactersasrepresentingalengthkdecimalnumber.Thecharacter
string31415thuscorrespondstothedecimalnumber31,415.Becauseweinter
prettheinputcharactersasbothgraphicalsymbolsanddigits,wefinditconvenient
inthissectiontodenotethemaswewoulddigits,inourstandardtextfont.
GivenapatternP1::m,letpdenoteitscorrespondingdecimalvalue.Inasim
ilarmanner,givenatextT1::n,lettsdenotethedecimalvalueofthelengthm
substringTsC1::sCm,forsD01:::nm.Certainly,tsDpifandonly
ifTsC1::sCmDP1::mthus,sisavalidshiftifandonlyiftsDp.Sinosotros
couldcomputepintime.m/andallthetsvaluesinatotalof.nmC1/time,1
thenwecoulddetermineallvalidshiftssintime.m/C.nmC1/D.n/
bycomparingpwitheachofthetsvalues.(Forthemoment,let'snotworryabout
thepossibilitythatpandthetsvaluesmightbeverylargenumbers.)
Wecancomputepintime.m/usingHorner'srule(seeSection30.1):
pDPmC10.Pm1C10.Pm2CC10.P2C10P1///:
Similarly,wecancomputet0fromT1::mintime.m/.

1Wewrite.nmC1/insteadof.nm/becausestakesonnmC1differentvalues.los
C1issignificantinanasymptoticsensebecausewhenmDn,computingthelonetsvaluetakes
.1/time,not.0/time.

Pgina112

32.2TheRabinKarpalgorithm 991

Tocomputetheremainingvaluest1t2:::tnmintime.nm/,weobserve
thatwecancomputetsC1fromtsinconstanttime,since
tsC1D10.ts 10m1TsC1/CTsCmC1: (32.1)

Subtracting10m1TsC1removesthehighorderdigitfromts,multiplyingthe
resultby10shiftsthenumberleftbyonedigitposition,andaddingTsCmC1
bringsintheappropriateloworderdigit.Forexample,ifmD5andtsD31415,
thenwewishtoremovethehighorderdigitTsC1D3andbringinthenew
loworderdigit(supposeitisTsC5C1D2)toobtain
tsC1 D10.31415100003/C2
D14152:

Ifweprecomputetheconstant10m1(whichwecandointimeO.lgm/usingthe
techniquesofSection31.6,althoughforthisapplicationastraightforwardOm/
timemethodsuffices),theneachexecutionofequation(32.1)takesaconstantnum
berofarithmeticoperations.Thus,wecancomputepintime.m/,andwecan
computealloft0t1:::tnmintime.nmC1/.Therefore,wecanfindall
occurrencesofthepatternP1::minthetextT1::nwith.m/preprocessing
timeand.nmC1/matchingtime.
Untilnow,wehaveintentionallyoverlookedoneproblem:pandtsmaybe
toolargetoworkwithconveniently.IfPcontainsmcharacters,thenwecannot
reasonablyassumethateacharithmeticoperationonp(whichismdigitslong)
takesconstanttime.Fortunately,wecansolvethisproblemeasily,asFigure32.5

https://translate.googleusercontent.com/translate_f 78/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
shows:computepandthetsvaluesmoduloasuitablemodulusq.Wecancompute
pmoduloqin.m/timeandallthet svaluesmoduloqin.nmC1/time.

Ifwechoosethemodulusqasaprimesuchthat10qjustfitswithinonecomputer
word,thenwecanperformallthenecessarycomputationswithsingleprecision
aritmtica.Ingeneral,withadaryalphabetf01:::re 1g,wechooseqso
thatdqfitswithinacomputerwordandadjusttherecurrenceequation(32.1)to
workmoduloq,sothatitbecomes
tsC1D.dts TsC1h/CTsCmC1/modq (32.2)
wherehdm1.modq/isthevalueofthedigit1inthehighorderposition
ofanmdigittextwindow.
Thesolutionofworkingmoduloqisnotperfect,however:tsp.modq/
doesnotimplythattsDp.Ontheotherhand,ifts6p.modq/,thenwe
definitelyhavethattsp,sothatshiftsisinvalid.Wecanthususethetest
tsp.modq/asafastheuristictesttoruleoutinvalidshiftss.Anyshiftsfor
whichtsp.modq/mustbetestedfurthertoseewhethersisreallyvalidor
wejusthaveaspurioushit.Thisadditionaltestexplicitlychecksthecondition

Page113

992 Chapter32StringMatching

2359023141526739921

mod13
7
(un)

1 2 3 4 5 6 7 8 910111213141516171819
2359023141526739921
... ... ...
mod13
8931101 7 8 4 5 10 11 7 911
valid spurious
partido golpear
(segundo)

antiguo nuevo antiguo nuevo


highorder loworder highorder loworder
dgito dgito dgito cambio dgito

314152 14152 (31415310000)10+2(mod13)


(733)10+2(mod13)
8(mod13)
78
(do)

Figure32.5TheRabinKarpalgorithm.Eachcharacterisadecimaldigit,andwecomputevalues
modulo13.(a)Atextstring.Awindowoflength5isshownshaded.Thenumericalvalueofthe
shadednumber,computedmodulo13,yieldsthevalue7.(b)Thesametextstringwithvaluescom
putedmodulo13foreachpossiblepositionofalength5window.AssumingthepatternPD31415,
welookforwindowswhosevaluemodulo13is7,since314157.mod13/.Thealgorithmfinds
twosuchwindows,shownshadedinthefigure.Thefirst,beginningattextposition7,isindeedan
occurrenceofthepattern,whilethesecond,beginningattextposition13,isaspurioushit.(c)How
tocomputethevalueforawindowinconstanttime,giventhevalueforthepreviouswindow.los
firstwindowhasvalue31415.Droppingthehighorderdigit3,shiftingleft(multiplyingby10),and
thenaddingintheloworderdigit2givesusthenewvalue14152.Becauseallcomputationsare
performedmodulo13,thevalueforthefirstwindowis7,andthevalueforthenewwindowis8.

https://translate.googleusercontent.com/translate_f 79/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
Pgina114
32.2TheRabinKarpalgorithm 993

P1::mDTsC1::sCm.Ifqislargeenough,thenwehopethatspurious
hitsoccurinfrequentlyenoughthatthecostoftheextracheckingislow.
Thefollowingproceduremakestheseideasprecise.Theinputstotheprocedure
arethetextT,thepatternP,theradixdtouse(whichistypicallytakentobejj),
andtheprimeqtouse.

RABINKARPMATCHER.TPdq/
1nDT:length
2mDP:length
3hDdm1modq
4pD0
5t0D0
6foriD1tom //preprocessing
7 pD.dpCPi/modq
8 t0D.dt0CTi/modq
9forsD0tonm //matching
10 ifp==ts
11 ifP1::m==TsC1::sCm
12 printPatternoccurswithshifts
13 ifs<nm
14 tsC1D.dts TsC1h/CTsCmC1/modq

TheprocedureRABINKARPMATCHERworksasfollows.Allcharactersare
interpretedasradixddigits.Thesubscriptsontareprovidedonlyforclarityel
programworkscorrectlyifallthesubscriptsaredropped.Line3initializeshtothe
valueofthehighorderdigitpositionofanmdigitwindow.Lines48computep
asthevalueofP1::mmodqandt0asthevalueofT1::mmodq.Thefor
loopoflines914iteratesthroughallpossibleshiftss,maintainingthefollowing
invariant:
Wheneverline10isexecuted,tsDTsC1::sCmmodq.
IfpDtsinline10(ahit),thenline11checkstoseewhetherP1::mD
TsC1::sCminordertoruleoutthepossibilityofaspurioushit.Line12prints
outanyvalidshiftsthatarefound.Ifs<nm(checkedinline13),thenthefor
loopwillexecuteatleastonemoretime,andsoline14firstexecutestoensurethat
theloopinvariantholdswhenwegetbacktoline10.Line14computesthevalue
oftsC1modqfromthevalueoftsmodqinconstanttimeusingequation(32.2)
directamente.
RABINKARPMATCHERtakes.m/preprocessingtime,anditsmatchingtime
is..nmC1/m/intheworstcase,since(likethenaivestringmatchingalgo
rithm)theRabinKarpalgorithmexplicitlyverifieseveryvalidshift.IfPDam

Pgina115

994 Chapter32StringMatching

andTDan,thenverifyingtakestime..nmC1/m/,sinceeachofthenmC1
possibleshiftsisvalid.
Inmanyapplications,weexpectfewvalidshiftsperhapssomeconstantcof
ellos.Insuchapplications,theexpectedmatchingtimeofthealgorithmisonly
O..nmC1/Ccm/DOnCm/,plusthetimerequiredtoprocessspurious
hits.Wecanbaseaheuristicanalysisontheassumptionthatreducingvaluesmod
uloqactslikearandommappingfromtoZq.(Seethediscussionontheuseof
divisionforhashinginSection11.3.1.Itisdifficulttoformalizeandprovesuchan
assumption,althoughoneviableapproachistoassumethatqischosenrandomly
fromintegersoftheappropriatesize.Weshallnotpursuethisformalizationhere.)
WecanthenexpectthatthenumberofspurioushitsisOn=q/,sincewecanes
timatethechancethatanarbitrarytswillbeequivalenttop,moduloq,as1=q.
SincethereareOn/positionsatwhichthetestofline10failsandwespendOm/
timeforeachhit,theexpectedmatchingtimetakenbytheRabinKarpalgorithm
es
On/COmCn=q//

https://translate.googleusercontent.com/translate_f 80/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
whereisthenumberofvalidshifts.ThisrunningtimeisOn/ifDO.1/and
wechooseq metro.Thatis,iftheexpectednumberofvalidshiftsissmall(O.1/)
andwechoosetheprimeqtobelargerthanthelengthofthepattern,thenwe
canexpecttheRabinKarpproceduretouseonlyOnCm/matchingtime.Yaque
mn,thisexpectedmatchingtimeisOn/.

Exercises

32.21
WorkingmoduloqD11,howmanyspurioushitsdoestheRabinKarpmatcheren
counterinthetextTD3141592653589793whenlookingforthepatternPD26?

32.22
HowwouldyouextendtheRabinKarpmethodtotheproblemofsearchingatext
stringforanoccurrenceofanyoneofagivensetofkpatterns?Startbyassuming
thatallkpatternshavethesamelength.Thengeneralizeyoursolutiontoallowthe
patternstohavedifferentlengths.

32.23
ShowhowtoextendtheRabinKarpmethodtohandletheproblemoflookingfor
agivenmmpatterninannnarrayofcharacters.(Thepatternmaybeshifted
verticallyandhorizontally,butitmaynotberotated.)

Pgina116

32.3Stringmatchingwithfiniteautomata 995

32.24
AlicehasacopyofalongnbitfileADhan1an2:::a0i,andBobsimilarly
hasannbitfileBDhbn1bn2:::b0i.AliceandBobwishtoknowiftheir
filesareidentical.ToavoidtransmittingallofAorB,theyusethefollowingfast
probabilisticcheck.Together,theyselectaprimeq>1000nandrandomlyselect
anintegerxfromf01:::q1g.Then,Aliceevaluates
!
xN1
Ax/D aixi modq
ID0
andBobsimilarlyevaluatesBx/.ProvethatifAB,thereisatmostone
chancein1000thatAx/DBx/,whereasifthetwofilesarethesame,Ax/is
necessarilythesameasBx/.(Hint:SeeExercise31.44.)

32.3Stringmatchingwithfiniteautomata

Manystringmatchingalgorithmsbuildafiniteautomatonasimplemachinefor
processinginformationthatscansthetextstringTforalloccurrencesofthepat
ternP.Thissectionpresentsamethodforbuildingsuchanautomaton.Estas
stringmatchingautomataareveryefficient:theyexamineeachtextcharacterex
actlyonce,takingconstanttimepertextcharacter.Thematchingtimeusedafter
preprocessingthepatterntobuildtheautomatonistherefore.n/.Thetimeto
buildtheautomaton,however,canbelargeifislarge.Section32.4describesa
cleverwayaroundthisproblem.
Webeginthissectionwiththedefinitionofafiniteautomaton.Wethenexamine
aspecialstringmatchingautomatonandshowhowtouseittofindoccurrences
ofapatterninatext.Finally,weshallshowhowtoconstructthestringmatching
automatonforagiveninputpattern.

Finiteautomata
AfiniteautomatonM,illustratedinFigure32.6,isa5tuple.Qq0A/,
dnde
Qisafinitesetofstates,
q02Qisthestartstate,
AQisadistinguishedsetofacceptingstates,

https://translate.googleusercontent.com/translate_f 81/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
isafiniteinputalphabet,
isafunctionfromQintoQ,calledthetransitionfunctionofM.

Pgina117

996 Chapter32StringMatching

un
input segundo
estado Ab 0 1
0 10 un
1 00 segundo
(un) (segundo)

Figure32.6AsimpletwostatefiniteautomatonwithstatesetQDf01g,startstateq0D0,
andinputalphabetDfabg.(a)Atabularrepresentationofthetransitionfunction.(b)An
equivalentstatetransitiondiagram.State1,shownblackend,istheonlyacceptingstate.Directed
edgesrepresenttransitions.Forexample,theedgefromstate1tostate0labeledbindicatesthat
.1b/D0.Thisautomatonacceptsthosestringsthatendinanoddnumberofa's.Moreprecisely,
itacceptsastringxifandonlyifxDy,whereyD"oryendswithab,andDak,wherekis
odd.Forexample,oninputabaaa,includingthestartstate,thisautomatonentersthesequenceof
statesh010101i,andsoitacceptsthisinput.Forinputabbaa,itentersthesequenceofstates
h010010i,andsoitrejectsthisinput.

Thefiniteautomatonbeginsinstateq0andreadsthecharactersofitsinputstring
oneatatime.Iftheautomatonisinstateqandreadsinputcharactera,itmoves
(makesatransition)fromstateqtostate.qa/.Wheneveritscurrentstateqis
amemberofA,themachineMhasacceptedthestringreadsofar.Aninputthat
isnotacceptedisrejected.
AfiniteautomatonMinducesafunction,calledthefinalstatefunction,
fromtoQsuchthat.w/isthestateMendsupinafterscanningthestringw.
Thus,Macceptsastringwifandonlyif.w/2A.Wedefinethefunction
recursively,usingthetransitionfunction:
."/Dq0
.wa/D..w/a/forw2a2.

Stringmatchingautomata
ForagivenpatternP,weconstructastringmatchingautomatoninapreprocess
ingstepbeforeusingittosearchthetextstring.Figure32.7illustrateshowwe
constructtheautomatonforthepatternPDababaca.Fromnowon,weshall
assumethatPisagivenfixedpatternstringforbrevity,weshallnotindicatethe
dependenceuponPinournotation.
Inordertospecifythestringmatchingautomatoncorrespondingtoagivenpat
ternP1::m,wefirstdefineanauxiliaryfunction,calledthesuffixfunction
correspondingtoP.Thefunctionmapstof01:::mgsuchthat.x/isthe
lengthofthelongestprefixofPthatisalsoasuffixofx:
.x/DmaxfkWPkZxg: (32.3)

Pgina118

32.3Stringmatchingwithfiniteautomata 997

un un
un un
0 un 1 segundo
2 un 3 segundo
4 un 5 do 6 un 7
segundo
segundo
(un)
input
estadoaBC PAG
0 100 un

https://translate.googleusercontent.com/translate_f 82/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
1 120 segundo
2 300 un
3 140 segundo
4 500 un
5 146 do yo 1234567891011
6 700 un Ti abababacaba
7 120 state.T /
i 012345456723

(segundo) (do)

Figure32.7(a)Astatetransitiondiagramforthestringmatchingautomatonthatacceptsall
stringsendinginthestringababaca.State0isthestartstate,andstate7(shownblackened)is
theonlyacceptingstate.Adirectededgefromstateitostatejlabeledarepresents.ia/Dj.los
rightgoingedgesformingthespineoftheautomaton,shownheavyinthefigure,correspondto
successfulmatchesbetweenpatternandinputcharacters.Theleftgoingedgescorrespondtofailing
partidos.Someedgescorrespondingtofailingmatchesareomittedbyconvention,ifastateihas
nooutgoingedgelabeledaforsomea2,then.ia/D0.(b)Thecorrespondingtransition
function,andthepatternstringPDababaca.Theentriescorrespondingtosuccessfulmatches
betweenpatternandinputcharactersareshownshaded.(c)Theoperationoftheautomatononthe
textTDabababacaba.UndereachtextcharacterTiappearsthestate.Ti/thattheautoma
tonisinafterprocessingtheprefixTi.Theautomatonfindsoneoccurrenceofthepattern,endingin
position9.

Thesuffixfunction iswelldefinedsincetheemptystringP0D"isasuf
fixofeverystring.Asexamples,forthepatternPDab,wehave."/D0,
.ccaca/D1,and.ccab/D2.ForapatternPoflengthm,wehave
.x/DmifandonlyifPZx.Fromthedefinitionofthesuffixfunction,
xZyimplies.x/.y/.
Wedefinethestringmatchingautomatonthatcorrespondstoagivenpattern
P1::masfollows:

Pgina119

998 Chapter32StringMatching

ThestatesetQisf01:::Mg.Thestartstateq0isstate0,andstatemisthe
onlyacceptingstate.
Thetransitionfunctionisdefinedbythefollowingequation,foranystateq
andcharactera:

.qa/D.Pqa/: (32.4)

Wedefine.qa/D.Pqa/becausewewanttokeeptrackofthelongestpre
fixofthepatternPthathasmatchedthetextstringTsofar.Weconsiderthe
mostrecentlyreadcharactersofT.InorderforasubstringofTlet'ssaythe
substringendingatTitomatchsomeprefixPjofP,thisprefixPjmustbea
suffixofTi.SupposethatqD.Ti/,sothatafterreadingTi,theautomatonisin
stateq.Wedesignthetransitionfunctionsothatthisstatenumber,q,tellsusthe
lengthofthelongestprefixofPthatmatchesasuffixofTi.Thatis,instateq,
PqZTiandqD.Ti/.(WheneverqDm,allmcharactersofPmatchasuffix
ofTi,andsowehavefoundamatch.)Thus,since.Ti/and.Ti/bothequalq,
weshallsee(inTheorem32.4,below)thattheautomatonmaintainsthefollowing
invariant:
.Ti/D.Ti/: (32.5)
IftheautomatonisinstateqandreadsthenextcharacterTiC1Da,thenwe
wantthetransitiontoleadtothestatecorrespondingtothelongestprefixofPthat
isasuffixofTia,andthatstateis.Tia/.BecausePqisthelongestprefixofP
thatisasuffixofTi,thelongestprefixofPthatisasuffixofTiaisnotonly.Tia/,
butalso.Pqa/.(Lemma32.3,onpage1000,provesthat.Tia/D.Pqa/.)
Thus,whentheautomatonisinstateq,wewantthetransitionfunctiononcharac
teratotaketheautomatontostate.Pqa/.
Therearetwocasestoconsider.Inthefirstcase,aDPqC1,sothatthe
characteracontinuestomatchthepatterninthiscase,because.qa/DqC1,the
transitioncontinuestogoalongthespineoftheautomaton(theheavyedgesin
Figure32.7).Inthesecondcase,aPqC1,sothatadoesnotcontinuetomatch
thepattern.Here,wemustfindasmallerprefixofPthatisalsoasuffixofTi.

https://translate.googleusercontent.com/translate_f 83/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
Becausethepreprocessingstepmatchesthepatternagainstitselfwhencreatingthe
stringmatchingautomaton,thetransitionfunctionquicklyidentifiesthelongest
suchsmallerprefixofP.
Let'slookatanexample.ThestringmatchingautomatonofFigure32.7has
.5c/D6,illustratingthefirstcase,inwhichthematchcontinues.Toillus
tratethesecondcase,observethattheautomatonofFigure32.7has.5b/D4.
WemakethistransitionbecauseiftheautomatonreadsabinstateqD5,then
PqbDababab,andthelongestprefixofPthatisalsoasuffixofabababis
P4Dabab.

Page120

32.3Stringmatchingwithfiniteautomata 999

Pr1 un

Pr

Figure32.8AnillustrationfortheproofofLemma32.2.Thefigureshowsthatr.x/C1,
whererD.xa/.

Toclarifytheoperationofastringmatchingautomaton,wenowgiveasimple,
efficientprogramforsimulatingthebehaviorofsuchanautomaton(represented
byitstransitionfunction)infindingoccurrencesofapatternPoflengthminan
inputtextT1::n.Asforanystringmatchingautomatonforapatternoflengthm,
thestatesetQisf01:::mg,thestartstateis0,andtheonlyacceptingstateis
statem.

FINITEAUTOMATONMATCHER.Tmetro/
1nDT:length
2qD0
3foriD1ton
4 qD.qTi/
5 ifq==m
6 printPatternoccurswithshiftim

FromthesimpleloopstructureofFINITEAUTOMATONMATCHER,wecaneasily
seethatitsmatchingtimeonatextstringoflengthnis.n/.Thismatching
time,however,doesnotincludethepreprocessingtimerequiredtocomputethe
transitionfunction.Weaddressthisproblemlater,afterfirstprovingthatthe
procedureFINITEAUTOMATONMATCHERoperatescorrectly.
ConsiderhowtheautomatonoperatesonaninputtextT1::n.Weshallprove
thattheautomatonisinstate.Ti/afterscanningcharacterTi.Since.Ti/Dm
ifandonlyifPZTi,themachineisintheacceptingstatemifandonlyifithas
justscannedthepatternP.Toprovethisresult,wemakeuseofthefollowingtwo
lemmasaboutthesuffixfunction.

Lemma32.2(Suffixfunctioninequality)
Foranystringxandcharactera,wehave.xa/.x/C1.

ProofReferringtoFigure32.8,letrD.xa/.IfrD0,thentheconclusion
.xa/Dr.x/C1istriviallysatisfied,bythenonnegativityof.x/.Ahora
assumethatr>0.Then,PrZxa,bythedefinitionof.Thus,Pr1Zx,by

Pgina121

1000 Chapter32StringMatching

un

https://translate.googleusercontent.com/translate_f 84/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
Pq un

Pr

Figure32.9AnillustrationfortheproofofLemma32.3.ThefigureshowsthatrD.Pqa/,
whereqD.x/andrD.xa/.

droppingtheafromtheendofPrandfromtheendofxa.Therefore,r1.x/,
since.x/isthelargestksuchthatPkZx,andthus.xa/Dr.x/C1.

Lemma32.3(Suffixfunctionrecursionlemma)
Foranystringxandcharactera,ifqD.x/,then.xa/D.Pqa/.

ProofFromthedefinitionof,wehavePqZx.AsFigure32.9shows,we
alsohavePqaZxa.IfweletrD.xa/,thenPrZxaand,byLemma32.2,
rqC1.Thus,wehavejPrjDrqC1DjPqaj.SincePqaZxa,PrZxa,
andjPrjjPqaj,Lemma32.1impliesthatPrZPqa.Therefore,r.Pqa/,
thatis,.xa/.Pqa/.Butwealsohave.Pqa/.xa/,sincePqaZxa.
Thus,.xa/D.Pqa/.

Wearenowreadytoproveourmaintheoremcharacterizingthebehaviorofa
stringmatchingautomatononagiveninputtext.Asnotedabove,thistheorem
showsthattheautomatonismerelykeepingtrack,ateachstep,ofthelongest
prefixofthepatternthatisasuffixofwhathasbeenreadsofar.Inotherwords,
theautomatonmaintainstheinvariant(32.5).

Theorem32.4
IfisthefinalstatefunctionofastringmatchingautomatonforagivenpatternP
andT1::nisaninputtextfortheautomaton,then
.Ti/D.Ti/
foriD01:::norte.

ProofTheproofisbyinductiononi.ForiD0,thetheoremistriviallytrue,
sinceT0D".Thus,.T0/D0D.T0/.

Page122

32.3Stringmatchingwithfiniteautomata 1001

Now,weassumethat.Ti/D.Ti/andprovethat.TiC1/D.TiC1/.Letq
denote.Ti/,andletadenoteTiC1.Entonces,
.TiC1/D .Tia/ (bythedefinitionsofTiC1anda)
D..Ti/a/(bythedefinitionof)
D.qa/ (bythedefinitionofq)
re .Pqa/ (bythedefinition(32.4)of)
re .Tia/ (byLemma32.3andinduction)
re .TiC1/ (bythedefinitionofTiC1).

ByTheorem32.4,ifthemachineentersstateqonline4,thenqisthelargest
valuesuchthatPqZTi.Thus,wehaveqDmonline5ifandonlyifthema
chinehasjustscannedanoccurrenceofthepatternP.WeconcludethatFINITE
AUTOMATONMATCHERoperatescorrectly.

Computingthetransitionfunction
Thefollowingprocedurecomputesthetransitionfunctionfromagivenpattern
P1::m.

COMPUTETRANSITIONFUNCTION.P/
1mDP:length
2forqD0tom
3 foreachcharactera2

https://translate.googleusercontent.com/translate_f 85/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,

54 kDmin.mC1qC2/
repetir
6 kDk1
7 untilPkZPqa
8 .qa/Dk
9return

Thisprocedurecomputes.qa/inastraightforwardmanneraccordingtoitsdef
initioninequation(32.4).Thenestedloopsbeginningonlines2and3consider
allstatesqandallcharactersa,andlines48set.qa/tobethelargestksuch
thatPkZPqa.Thecodestartswiththelargestconceivablevalueofk,whichis
min.mqC1/.ItthendecreaseskuntilPkZPqa,whichmusteventuallyoccur,
sinceP0D"isasuffixofeverystring.
TherunningtimeofCOMPUTETRANSITIONFUNCTIONisOm3jj/,be
causetheouterloopscontributeafactorofmjj,theinnerrepeatloopcanrun
atmostmC1times,andthetestPkZPqaonline7canrequirecomparingup

Pgina123

1002 Chapter32StringMatching

tomcharacters.Muchfasterproceduresexistbyutilizingsomecleverlycom
putedinformationaboutthepatternP(seeExercise32.48),wecanimprovethe
timerequiredtocomputefromPtoO.mjj/.Withthisimprovedprocedurefor
computing,wecanfindalloccurrencesofalengthmpatterninalengthntext
overanalphabetwithO.mjj/preprocessingtimeand.n/matchingtime.

Exercises

32.31
ConstructthestringmatchingautomatonforthepatternPDaababandillustrate
itsoperationonthetextstringTDaaababaabaababaab.

32.32
Drawastatetransitiondiagramforastringmatchingautomatonforthepattern
ababbabbababbababbabboverthealphabetDfabg.

32.33
WecallapatternPnonoverlappableifPkZPqimplieskD0orkDq.De
scribethestatetransitiondiagramofthestringmatchingautomatonforanonover
lappablepattern.

32.34?
GiventwopatternsPandP0,describehowtoconstructafiniteautomatonthat
determinesalloccurrencesofeitherpattern.Trytominimizethenumberofstates
inyourautomaton.

32.35
GivenapatternPcontaininggapcharacters(seeExercise32.14),showhowto
buildafiniteautomatonthatcanfindanoccurrenceofPinatextTinOn/
matchingtime,wherenDjTj.

?32.4TheKnuthMorrisPrattalgorithm

WenowpresentalineartimestringmatchingalgorithmduetoKnuth,Morris,and
Pratt.Thisalgorithmavoidscomputingthetransitionfunctionaltogether,andits
matchingtimeis.n/usingjustanauxiliaryfunction,whichweprecompute
fromthepatternintime.m/andstoreinanarray1::m.Thearrayallows
ustocomputethetransitionfunctionefficiently(inanamortizedsense)onthe
flyasneeded.Looselyspeaking,foranystateqD01:::mandanycharacter

Pgina124

https://translate.googleusercontent.com/translate_f 86/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
32.4TheKnuthMorrisPrattalgorithm 1003

a2,thevalueqcontainstheinformationweneedtocompute.qa/but
thatdoesnotdependona.Sincethearray hasonlymentries,whereashas
.mjj/entries,wesaveafactorofjjinthepreprocessingtimebycomputing
ratherthan.

Theprefixfunctionforapattern

Theprefixfunction forapatternencapsulatesknowledgeabouthowthepat
ternmatchesagainstshiftsofitself.Wecantakeadvantageofthisinformationto
avoidtestinguselessshiftsinthenaivepatternmatchingalgorithmandtoavoid
precomputingthefulltransitionfunctionforastringmatchingautomaton.
Considertheoperationofthenaivestringmatcher.Figure32.10(a)showsa
particularshiftsofatemplatecontainingthepatternPDababacaagainsta
textT.Forthisexample,qD5ofthecharactershavematchedsuccessfully,but
the6thpatterncharacterfailstomatchthecorrespondingtextcharacter.Lasinformaciones
mationthatqcharactershavematchedsuccessfullydeterminesthecorresponding
textcharacters.Knowingtheseqtextcharactersallowsustodetermineimmedi
atelythatcertainshiftsareinvalid.Intheexampleofthefigure,theshiftsC1is
necessarilyinvalid,sincethefirstpatterncharacter(a)wouldbealignedwithatext
characterthatweknowdoesnotmatchthefirstpatterncharacter,butdoesmatch
thesecondpatterncharacter(b).Theshifts0DsC2showninpart(b)ofthefig
ure,however,alignsthefirstthreepatterncharacterswiththreetextcharactersthat
mustnecessarilymatch.Ingeneral,itisusefultoknowtheanswertothefollowing
question:
GiventhatpatterncharactersP1::qmatchtextcharactersTsC1::sCq,
whatistheleastshifts0>ssuchthatforsomek<q,

P1::kDTs0C1::s0Ck (32.6)

wheres0CkDsCq?
Inotherwords,knowingthatPqZTsCq,wewantthelongestproperprefixPk
ofPqthatisalsoasuffixofTsCq.(Sinces0CkDsCq,ifwearegivens
andq,thenfindingthesmallestshifts0istantamounttofindingthelongestprefix
lengthk.)WeaddthedifferenceqkinthelengthsoftheseprefixesofPtothe
shiftstoarriveatournewshifts0,sothats0DsCqk/.Inthebestcase,kD0,
sothats0DsCq,andweimmediatelyruleoutshiftssC1sC2:::sCq1.
Inanycase,atthenewshifts0wedon'tneedtocomparethefirstkcharactersofP
withthecorrespondingcharactersofT,sinceequation(32.6)guaranteesthatthey
match.
Wecanprecomputethenecessaryinformationbycomparingthepatternagainst
itself,asFigure32.10(c)demonstrates.SinceTs0C1::s0Ckispartofthe

Pgina125

1004 Chapter32StringMatching

bacbab abaabcbab T

S aba baca PAG


Q
(un)

bacbab abaabcbab T

s=s+2 aba baca PAG


K
(segundo)

Ab aba Pq

aba Pk
(do)

https://translate.googleusercontent.com/translate_f 87/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,

Figure32.10Theprefixfunction.(a)ThepatternPDababacaalignswithatextTsothat
thefirstqD5charactersmatch.Matchingcharacters,shownshaded,areconnectedbyverticallines.
(b)Usingonlyourknowledgeofthe5matchedcharacters,wecandeducethatashiftofsC1is
invalid,butthatashiftofs0DsC2isconsistentwitheverythingweknowaboutthetextandtherefore
ispotentiallyvalid.(c)Wecanprecomputeusefulinformationforsuchdeductionsbycomparingthe
patternwithitself.Here,weseethatthelongestprefixofPthatisalsoapropersuffixofP5isP3.
Werepresentthisprecomputedinformationinthearray,sothat5D3.Giventhatqcharacters
havematchedsuccessfullyatshifts,thenextpotentiallyvalidshiftisats0DsC.qq/asshown
inpart(b).

knownportionofthetext,itisasuffixofthestringPq.Therefore,wecaninterpret
equation(32.6)asaskingforthegreatestk<qsuchthatPkZPq.Then,thenew
shifts0DsC.qk/isthenextpotentiallyvalidshift.Wewillfinditconvenientto
store,foreachvalueofq,thenumberkofmatchingcharactersatthenewshifts0,
ratherthanstoring,say,s0 S
Weformalizetheinformationthatweprecomputeasfollows.Givenapattern
P1::m,theprefixfunctionforthepatternPisthefunctionWf12:::mg!
f01:::m1gsuchthat

qDmaxfkWk<qandPkZPqg:
Thatis,qisthelengthofthelongestprefixofPthatisapropersuffixofPq.
Figure32.11(a)givesthecompleteprefixfunctionforthepatternababaca.

Pgina126

32.4TheKnuthMorrisPrattalgorithm 1005

P 5 ababaca

P 3 ababaca 5D3

P 1 ababaca 3D1
yo 1234567
Pi ababaca
P 0 " ababaca 1D0
i 0012301
(un) (segundo)

Figure32.11AnillustrationofLemma32.5forthepatternPDababacaandqD5.(a)The
functionforthegivenpattern.Since5D3,3D1,and1D0,byiterating weobtain
5Df310g.(b)WeslidethetemplatecontainingthepatternPtotherightandnotewhensome
prefixPkofPmatchesupwithsomepropersuffixofP5wegetmatcheswhenkD3,1,and0.In
thefigure,thefirstrowgivesP,andthedottedverticallineisdrawnjustafterP5.Successiverows
showalltheshiftsofPthatcausesomeprefixPkofPtomatchsomesuffixofP5.Successfully
matchedcharactersareshownshaded.Verticallinesconnectalignedmatchingcharacters.As,
fkWk<5andPk=P5gDf310g.Lemma32.5claimsthat qDfkWk<qandPk=Pqg
forallq.

ThepseudocodebelowgivestheKnuthMorrisPrattmatchingalgorithmas
theprocedureKMPMATCHER.Forthemostpart,theprocedurefollowsfrom
FINITEAUTOMATONMATCHER,asweshallsee.KMPMATCHERcallstheaux
iliaryprocedureCOMPUTEPREFIXFUNCTIONtocompute.

KMPMATCHER.TP/
1nDT:length
2mDP:length
3 DCOMPUTEPREFIXFUNCTION.P/
4qD0 //numberofcharactersmatched
5foriD1ton //scanthetextfromlefttoright
6 whileq>0andPqC1Ti
7 qDq //nextcharacterdoesnotmatch
8 ifPqC1==Ti
9 qDqC1 //nextcharactermatches
10 ifq==m //isallofPmatched?
11 printPatternoccurswithshiftim
12 qDq //lookforthenextmatch

https://translate.googleusercontent.com/translate_f 88/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,

Page127

1006 Chapter32StringMatching

COMPUTEPREFIXFUNCTION.P/
1mDP:length
2let1::mbeanewarray
3 1D0
4kD0
5forqD2tom
6 whilek>0andPkC1Pq
7 kDk
8 ifPkC1==Pq
9 kDkC1
10 qDk
11return

Thesetwoprocedureshavemuchincommon,becausebothmatchastringagainst
thepatternP:KMPMATCHERmatchesthetextTagainstP,andCOMPUTE
PREFIXFUNCTIONmatchesPagainstitself.
Webeginwithananalysisoftherunningtimesoftheseprocedures.Proving
theseprocedurescorrectwillbemorecomplicated.

Runningtimeanalysis

TherunningtimeofCOMPUTEPREFIXFUNCTIONis.m/,whichweshowby
usingtheaggregatemethodofamortizedanalysis(seeSection17.1).Lonico
trickypartisshowingthatthewhileloopoflines67executesOm/timesalto
JuntosWeshallshowthatitmakesatmostm1iterations.Westartbymaking
someobservationsaboutk.First,line4startskat0,andtheonlywaythatk
increasesisbytheincrementoperationinline9,whichexecutesatmostonceper
iterationoftheforloopoflines510.Thus,thetotalincreaseinkisatmostm1.
Second,sincek<quponenteringtheforloopandeachiterationoftheloopin
crementsq,wealwayshavek<q.Therefore,theassignmentsinlines3and10
ensurethatq<qforallqD12:::m,whichmeansthateachiterationof
thewhileloopdecreasesk.Third,kneverbecomesnegative.Puttingthesefacts
together,weseethatthetotaldecreaseinkfromthewhileloopisboundedfrom
abovebythetotalincreaseinkoveralliterationsoftheforloop,whichism1.
Thus,thewhileloopiteratesatmostm1timesinall,andCOMPUTEPREFIX
FUNCTIONrunsintime.m/.
Exercise32.44asksyoutoshow,byasimilaraggregateanalysis,thatthematch
ingtimeofKMPMATCHERis.n/.
ComparedwithFINITEAUTOMATONMATCHER,byusingratherthan,we
havereducedthetimeforpreprocessingthepatternfromO.mjj/to.m/,while
keepingtheactualmatchingtimeboundedby.n/.

Pgina128

32.4TheKnuthMorrisPrattalgorithm 1007

Correctnessoftheprefixfunctioncomputation
Weshallseealittlelaterthattheprefixfunctionhelpsussimulatethetransition
functioninastringmatchingautomaton.Butfirst,weneedtoprovethatthe
procedureCOMPUTEPREFIXFUNCTIONdoesindeedcomputetheprefixfunc
tioncorrectly.Inordertodoso,wewillneedtofindallprefixesPkthatareproper
suffixesofagivenprefixPq.Thevalueofqgivesusthelongestsuchprefix,but
thefollowinglemma,illustratedinFigure32.11,showsthatbyiteratingtheprefix
function,wecanindeedenumeratealltheprefixesPkthatarepropersuffixes
ofPq.Dejar
qDfq.2/q.3/q:::.t/qg
where.i/qisdefinedintermsoffunctionaliteration,sothat.0/qDqand
https://translate.googleusercontent.com/translate_f 89/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
.i/qD.i1/qfori 1,andwherethesequencein qstopsupon
reaching.t/qD0.

Lemma32.5(Prefixfunctioniterationlemma)
LetPbeapatternoflengthmwithprefixfunction.Then,forqD12:::m,
wehave qDfkWk<qandPkZPqg.

ProofWefirstprovethat qfkWk<qandPkZPqgor,equivalently,
i2 qimpliesPiZPq: (32.7)
Ifi2 q,theniD.u/qforsomeu>0.Weproveequation(32.7)by
inductiononu.ForuD1,wehaveiDq,andtheclaimfollowssincei<q
andPqZPqbythedefinitionof.Usingtherelationsi<iandPiZPi
andthetransitivityof<andZestablishestheclaimforalliin q.Porlotanto,
qfkWk<qandPkZPqg.
WenowprovethatfkWk<qandPkZPqg qbycontradiction.Sup
posetothecontrarythatthesetfkWk<qandPkZPqg qisnonempty,
andletjbethelargestnumberintheset.Becauseqisthelargestvaluein
fkWk<qandPkZPqgandq2 q,wemusthavej<q,andsowe
letj0denotethesmallestintegerin qthatisgreaterthanj.(Wecanchoose
j0Dqifnoothernumberin qisgreaterthanj.)WehavePjZPqbecause
j2fkWk<qandPkZPqg,andfromj02 qandequation(32.7),wehave
Pj ZPq.Thus,PjZPj byLemma32.1,andjisthelargestvaluelessthanj0
0 0

withthisproperty.Therefore,wemusthavej0Djand,sincej02 q,we
musthavej2 qaswell.Thiscontradictionprovesthelemma.

ThealgorithmCOMPUTEPREFIXFUNCTIONcomputesq,inorder,forqD
12:::metro.Setting1to0inline3ofCOMPUTEPREFIXFUNCTIONiscer
tainlycorrect,sinceq<qforallq.Weshallusethefollowinglemmaand

Pgina129

1008 Chapter32StringMatching

itscorollarytoprovethatCOMPUTEPREFIXFUNCTIONcomputesqcorrectly
forq>1.

Lemma32.6
LetPbeapatternoflengthm,andlet betheprefixfunctionforP.ForqD
12:::m,ifq>0,thenq12 q1.

ProofLetrDq>0,sothatr<qandPrZPqthus,r 1<q1and
Pr1ZPq1(bydroppingthelastcharacterfromPrandPq,whichwecando
becauser>0).ByLemma32.5,therefore,r 12 q 1.Thus,wehave
q1Dr12 q1.

ForqD23:::m,definethesubsetEq1 q1by
Eq1Dfk2 q1WPkC1DPqg
DfkWk<q1andPkZPq1andPkC1DPqg(byLemma32.5)
DfkWk<q1andPkC1ZPqg:
ThesetEq1consistsofthevaluesk<q1forwhichPkZPq1andforwhich,
becausePkC1DPq,wehavePkC1ZPq.Thus,Eq1consistsofthose
valuesk2 q1suchthatwecanextendPktoPkC1andgetapropersuffix
ofPq.

Corollary32.7
LetPbeapatternoflengthm,andlet betheprefixfunctionforP.ForqD
23:::m,
(
0 ifEq1D
qD
1Cmaxfk2Eq1gifEq1:

ProofIfEq1isempty,thereisnok2 q1(includingkD0)forwhich
wecanextendPktoPkC1andgetapropersuffixofPq.ThereforeqD0.
IfEq1isnonempty,thenforeachk2Eq1wehavekC1<qandPkC1ZPq.
Therefore,fromthedefinitionofq,wehave
q 1Cmaxfk2Eq1g: (32.8)

https://translate.googleusercontent.com/translate_f 90/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
Notethatq>0.LetrD q 1,sothatrC1D qandthere
forePrC1ZPq.SincerC1>0,wehavePrC1DPq.Adems,
byLemma32.6,wehaver2 q 1.Therefore,r2Eq1,andsor
maxfk2Eq1gor,equivalently,
q1Cmaxfk2Eq1g: (32,9)
Combiningequations(32.8)and(32.9)completestheproof.

Pgina130

32.4TheKnuthMorrisPrattalgorithm 1009

WenowfinishtheproofthatCOMPUTEPREFIXFUNCTIONcomputes cor
Directamente.IntheprocedureCOMPUTEPREFIXFUNCTION,atthestartofeachiter
ationoftheforloopoflines510,wehavethatkDq 1.Thiscondition
isenforcedbylines3and4whentheloopisfirstentered,anditremainstruein
eachsuccessiveiterationbecauseofline10.Lines69adjustksothatitbecomes
thecorrectvalueofq.Thewhileloopoflines67searchesthroughallvalues
k2 q1untilitfindsavalueofkforwhichPkC1DPqatthatpoint,
kisthelargestvalueinthesetEq1,sothat,byCorollary32.7,wecansetq
tokC1.Ifthewhileloopcannotfindak2 q1suchthatPkC1DPq,
thenkequals0atline8.IfP1DPq,thenweshouldsetbothkandqto1
otherwiseweshouldleavekaloneandsetqto0.Lines810setkandq
correctlyineithercase.ThiscompletesourproofofthecorrectnessofCOMPUTE
PREFIXFUNCTION.

CorrectnessoftheKnuthMorrisPrattalgorithm
WecanthinkoftheprocedureKMPMATCHERasareimplementedversionof
theprocedureFINITEAUTOMATONMATCHER,butusingtheprefixfunction
tocomputestatetransitions.Specifically,weshallprovethatintheithiterationof
theforloopsofbothKMPMATCHERandFINITEAUTOMATONMATCHER,the
stateqhasthesamevaluewhenwetestforequalitywithm(atline10inKMP
MATCHERandatline5inFINITEAUTOMATONMATCHER).Oncewehave
arguedthatKMPMATCHERsimulatesthebehaviorofFINITEAUTOMATON
MATCHER,thecorrectnessofKMPMATCHERfollowsfromthecorrectnessof
FINITEAUTOMATONMATCHER(thoughweshallseealittlelaterwhyline12in
KMPMATCHERisnecessary).
BeforeweformallyprovethatKMPMATCHERcorrectlysimulatesFINITE
AUTOMATONMATCHER,let'stakeamomenttounderstandhowtheprefixfunc
Cin replacesthetransitionfunction.Recallthatwhenastringmatching
automatonisinstateqanditscansacharacteraDTi,itmovestoanew
state.qa/.IfaDPqC1,sothatacontinuestomatchthepattern,then
.qa/DqC1.Otherwise,aPqC1,sothatadoesnotcontinuetomatch
thepattern,and0.qa/q.Inthefirstcase,whenacontinuestomatch,
KMPMATCHERmovestostateqC1withoutreferringtothe function:the
whilelooptestinline6comesupfalsethefirsttime,thetestinline8comesup
true,andline9incrementsq.
Thefunctioncomesintoplaywhenthecharacteradoesnotcontinuetomatch
thepattern,sothatthenewstate.qa/iseitherqortotheleftofqalongthespine
oftheautomaton.Thewhileloopoflines67inKMPMATCHERiteratesthrough
thestatesin q,stoppingeitherwhenitarrivesinastate,sayq0,suchthata
matchesPq0C1orq0hasgoneallthewaydownto0.IfamatchesPq0C1,

Pgina131

1010 Chapter32StringMatching

thenline9setsthenewstatetoq0C1,whichshouldequal.qa/forthesimulation
toworkcorrectly.Inotherwords,thenewstate.qa/shouldbeeitherstate0or
onegreaterthansomestatein q.
Let'slookattheexampleinFigures32.7and32.11,whichareforthepattern
PDababaca.SupposethattheautomatonisinstateqD5thestatesin
5are,indescendingorder,3,1,and0.Ifthenextcharacterscannedisc,then
https://translate.googleusercontent.com/translate_f 91/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
wecaneasilyseethattheautomatonmovestostate.5c/D6inbothFINITE
AUTOMATONMATCHERandKMPMATCHER.Nowsupposethatthenextchar
acterscannedisinsteadb,sothattheautomatonshouldmovetostate.5b/D4.
ThewhileloopinKMPMATCHERexitshavingexecutedline7once,anditar
rivesinstateq0D5D3.SincePq0C1DP4Db,thetestinline8
comesuptrue,andKMPMATCHERmovestothenewstateq0C1D4D.5b/.
Finally,supposethatthenextcharacterscannedisinsteada,sothattheautoma
tonshouldmovetostate.5a/D1.Thefirstthreetimesthatthetestinline6
executes,thetestcomesuptrue.Thefirsttime,wefindthatP6Dca,and
KMPMATCHERmovestostate5D3(thefirststatein 5).Elsegundo
time,wefindthatP4Dbaandmovetostate3D1(thesecondstate
en 5).Thethirdtime,wefindthatP2Dbaandmovetostate1D0
(thelaststatein 5).Thewhileloopexitsonceitarrivesinstateq0D0.Now,
line8findsthatPq0C1DP1Da,andline9movestheautomatontothenew
stateq0C1D1D.5a/.
Thus,ourintuitionisthatKMPMATCHERiteratesthroughthestatesin qin
decreasingorder,stoppingatsomestateq0andthenpossiblymovingtostateq0C1.
Althoughthatmightseemlikealotofworkjusttosimulatecomputing.qa/,
bearinmindthatasymptotically,KMPMATCHERisnoslowerthanFINITE
AUTOMATONMATCHER.
WearenowreadytoformallyprovethecorrectnessoftheKnuthMorrisPratt
algoritmo.ByTheorem32.4,wehavethatqD.Ti/aftereachtimeweexecute
line4ofFINITEAUTOMATONMATCHER.Therefore,itsufficestoshowthatthe
samepropertyholdswithregardtotheforloopinKMPMATCHER.Theproof
proceedsbyinductiononthenumberofloopiterations.Initially,bothprocedures
setqto0astheyentertheirrespectiveforloopsforthefirsttime.Consideritera
tionioftheforloopinKMPMATCHER,andletq0bestateatthestartofthisloop
iteration.Bytheinductivehypothesis,wehaveq0D.Ti1/.Weneedtoshow
thatqD.Ti/atline10.(Again,weshallhandleline12separately.)
WhenweconsiderthecharacterTi,thelongestprefixofPthatisasuffixofTi
iseitherPq C1(ifPq0C1DTi)orsomeprefix(notnecessarilyproper,and
0

possiblyempty)ofPq .Weconsiderseparatelythethreecasesinwhich.Ti/D0,
0

.Ti/Dq0C1,and0<.Ti/q0.

Page132

32.4TheKnuthMorrisPrattalgorithm 1011

If.Ti/D0,thenP0D"istheonlyprefixofPthatisasuffixofTi.Thewhile
loopoflines67iteratesthroughthevaluesin q0,butalthoughPqZTifor
everyq2 q0,theloopneverfindsaqsuchthatPqC1DTi.Theloop
terminateswhenqreaches0,andofcourseline9doesnotexecute.Porlotanto,
qD0atline10,sothatqD.Ti/.
If.Ti/Dq0C1,thenPq0C1DTi,andthewhilelooptestinline6
failsthefirsttimethrough.Line9executes,incrementingqsothatafterward
wehaveqDq0C1D.Ti/.
If0<.Ti/q0,thenthewhileloopoflines67iteratesatleastonce,
checkingindecreasingordereachvalueq2 q0untilitstopsatsomeq<q0.
Thus,PqisthelongestprefixofPq forwhichPqC1DTi,sothatwhenthe
0

whileloopterminates,qC1D.Pq Ti/.Sinceq0D.Ti1/,Lemma32.3
0

impliesthat.Ti1Ti/D.Pq Ti/.Thus,wehave
0

qC1D .Pq Ti/


0

re .Ti1Ti/
re .Ti/

whenthewhileloopterminates.Afterline9incrementsq,wehaveqD.Ti/.
Line12isnecessaryinKMPMATCHER,becauseotherwise,wemightrefer
encePmC1online6afterfindinganoccurrenceofP.(Theargumentthat
qD.Ti1/uponthenextexecutionofline6remainsvalidbythehintgivenin
Exercise32.48:.ma/D.ma/or,equivalently,.Pa/D.Pma/for
anya2.)TheremainingargumentforthecorrectnessoftheKnuthMorris
PrattalgorithmfollowsfromthecorrectnessofFINITEAUTOMATONMATCHER,
sincewehaveshownthatKMPMATCHERsimulatesthebehaviorofFINITE

https://translate.googleusercontent.com/translate_f 92/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
AUTOMATONMATCHER.
Exercises

32.41
Computetheprefixfunctionforthepatternababbabbabbababbabb.

32.42
Giveanupperboundonthesizeof qasafunctionofq.Giveanexampleto
showthatyourboundistight.

32.43
ExplainhowtodeterminetheoccurrencesofpatternPinthetextTbyexamining
thefunctionforthestringPT(thestringoflengthmCnthatistheconcatenation
ofPandT).

Pgina133

1012 Chapter32StringMatching

32.44
UseanaggregateanalysistoshowthattherunningtimeofKMPMATCHER
is.n/.

32.45
UseapotentialfunctiontoshowthattherunningtimeofKMPMATCHERis.n/.

32.46
ShowhowtoimproveKMPMATCHERbyreplacingtheoccurrenceofinline7
(butnotline12)by0,where0isdefinedrecursivelyforqD12:::m1by
Un
theequation

0 ifqD0
0qD 0qifq0andPqC1DPqC1
q ifq0andPqC1PqC1:
Explainwhythemodifiedalgorithmiscorrect,andexplaininwhatsensethis
changeconstitutesanimprovement.

32.47
GivealineartimealgorithmtodeterminewhetheratextTisacyclicrotationof
anotherstringT0.Forexample,arcandcararecyclicrotationsofeachother.

32.48?
GiveanO.mjj/timealgorithmforcomputingthetransitionfunctionforthe
stringmatchingautomatoncorrespondingtoagivenpatternP.(Hint:Provethat
.qa/D.qa/ifqDmorPqC1a.)

Problemas

321Stringmatchingbasedonrepetitionfactors
Letyidenotetheconcatenationofstringywithitselfitimes.Porejemplo,
.ab/3Dababab.Wesaythatastringx2hasrepetitionfactorrifxDyr
forsomestringy2andsomer>0.Let.x/denotethelargestrsuchthatx
hasrepetitionfactorr.

a.GiveanefficientalgorithmthattakesasinputapatternP1::mandcomputes
thevalue.Pi/foriD12:::metro.Whatistherunningtimeofyouralgo
rithm?

Pgina134

NotesforChapter32 1013

https://translate.googleusercontent.com/translate_f 93/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,

.Pi/.Provethatif
segundo.ForanypatternP1::m,let.P/bedefinedasmax1im
thepatternPischosenrandomlyfromthesetofallbinarystringsoflengthm,
thentheexpectedvalueof.P/isO.1/.

do.Arguethatthefollowingstringmatchingalgorithmcorrectlyfindsalloccur
rencesofpatternPinatextT1::nintimeO..P/nCm/:

REPETITIONMATCHER.PT/
1mDP:length
2nDT:length
3kD1C.P/
4qD0
5sD0
6whilesnm
7 ifTsCqC1==PqC1
8 qDqC1
9 ifq==m
10 printPatternoccurswithshifts
11 ifq==morTsCqC1PqC1
12 sDsCmax.1dq=ke/
13 qD0

ThisalgorithmisduetoGalilandSeiferas.Byextendingtheseideasgreatly,
theyobtainedalineartimestringmatchingalgorithmthatusesonlyO.1/stor
agebeyondwhatisrequiredforPandT.

Chapternotes

Therelationofstringmatchingtothetheoryoffiniteautomataisdiscussedby
Aho,Hopcroft,andUllman[5].TheKnuthMorrisPrattalgorithm[214]was
inventedindependentlybyKnuthandPrattandbyMorristheypublishedtheir
workjointly.Reingold,Urban,andGries[294]giveanalternativetreatmentofthe
KnuthMorrisPrattalgorithm.TheRabinKarpalgorithmwasproposedbyKarp
andRabin[201].GalilandSeiferas[126]giveaninterestingdeterministiclinear
timestringmatchingalgorithmthatusesonlyO.1/spacebeyondthatrequiredto
storethepatternandtext.

Page135

33 ComputationalGeometry

Computationalgeometryisthebranchofcomputersciencethatstudiesalgorithms
forsolvinggeometricproblems.Inmodernengineeringandmathematics,com
putationalgeometryhasapplicationsinsuchdiversefieldsascomputergraphics,
robotics,VLSIdesign,computeraideddesign,molecularmodeling,metallurgy,
manufacturing,textilelayout,forestry,andstatistics.Theinputtoacomputational
geometryproblemistypicallyadescriptionofasetofgeometricobjects,suchas
asetofpoints,asetoflinesegments,ortheverticesofapolygonincounterclock
wiseorder.Theoutputisoftenaresponsetoaqueryabouttheobjects,suchas
whetheranyofthelinesintersect,orperhapsanewgeometricobject,suchasthe

https://translate.googleusercontent.com/translate_f 94/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
convexhull(smallestenclosingconvexpolygon)ofthesetofpoints.
Inthischapter,welookatafewcomputationalgeometryalgorithmsintwo
dimensions,thatis,intheplane.Werepresenteachinputobjectbyasetof
pointsfp1p2p3:::g,whereeachpiD.xiyi/andxiyi2R.Forexam
ple,werepresentannvertexpolygonPbyasequencehp0p1p2:::pn1i
ofitsverticesinorderoftheirappearanceontheboundaryofP.Computational
geometrycanalsoapplytothreedimensions,andevenhigherdimensionalspaces,
butsuchproblemsandtheirsolutionscanbeverydifficulttovisualize.Evenin
twodimensions,however,wecanseeagoodsampleofcomputationalgeometry
Tcnicas.
Section33.1showshowtoanswerbasicquestionsaboutlinesegmentseffi
cientlyandaccurately:whetheronesegmentisclockwiseorcounterclockwise
fromanotherthatsharesanendpoint,whichwayweturnwhentraversingtwo
adjoininglinesegments,andwhethertwolinesegmentsintersect.Section33.2
presentsatechniquecalledsweepingthatweusetodevelopanO.nlgn/time
algorithmfordeterminingwhetherasetofnlinesegmentscontainsanyinter
Secciones.Section33.3givestworotationalsweepalgorithmsthatcomputethe
convexhull(smallestenclosingconvexpolygon)ofasetofnpoints:Grahams
scan,whichrunsintimeO.nlgn/,andJarvis'smarch,whichtakesO.nh/time,
wherehisthenumberofverticesoftheconvexhull.Finally,Section33.4gives

Page136

33.1Linesegmentproperties 1015

anO.nlgn/timedivideandconqueralgorithmforfindingtheclosestpairof
pointsinasetofnpointsintheplane.

33.1Linesegmentproperties

Severalofthecomputationalgeometryalgorithmsinthischapterrequireanswers
toquestionsaboutthepropertiesoflinesegments.Aconvexcombinationoftwo
distinctpointsp1D.x1y1/andp2D.x2y2/isanypointp3D.x3y3/such
thatforsomeintherange01,wehavex3Dx1C.1 /x2and
y3Dy1C.1/y2.Wealsowritethatp3Dp1C.1/p2.Intuitively,p3
isanypointthatisonthelinepassingthroughp1andp2andisonorbetweenp1
andp2ontheline.Giventwodistinctpointsp1andp2,thelinesegmentp1p2
isthesetofconvexcombinationsofp1andp2.Wecallp1andp2theendpoints
ofsegmentp1p2.Sometimestheorderingofp1andp2matters,andwespeakof
thedirectedsegment p1p ! 2.Ifp1istheorigin.00/,thenwecantreatthedirected
segmentop1p! 2asthevectorp2.
Inthissection,weshallexplorethefollowingquestions:
! 1and p0p
1.Giventwodirectedsegments p0p ! 2,is p0p
! 1clockwisefrom p0p
! 2
withrespecttotheircommonendpointp0?
2.Giventwolinesegmentsp0p1andp1p2,ifwetraversep0p1andthenp1p2,
dowemakealeftturnatpointp1?
3.Dolinesegmentsp1p2andp3p4intersect?
Therearenorestrictionsonthegivenpoints.
WecananswereachquestioninO.1/time,whichshouldcomeasnosurprise
sincetheinputsizeofeachquestionisO.1/.Moreover,ourmethodsuseonlyad
ditions,subtractions,multiplications,andcomparisons.Weneedneitherdivision
nortrigonometricfunctions,bothofwhichcanbecomputationallyexpensiveand
pronetoproblemswithroundofferror.Forexample,thestraightforwardmethod
ofdeterminingwhethertwosegmentsintersectcomputethelineequationofthe
formyDmxCbforeachsegment(mistheslopeandbistheyintercept),
findthepointofintersectionofthelines,andcheckwhetherthispointisonboth
segmentsusesdivisiontofindthepointofintersection.Whenthesegmentsare
nearlyparallel,thismethodisverysensitivetotheprecisionofthedivisionopera
tiononrealcomputers.Themethodinthissection,whichavoidsdivision,ismuch
moreaccurate.

https://translate.googleusercontent.com/translate_f 95/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,

Page137

1016 Chapter33ComputationalGeometry

Y Y
p1+p2
pag
p2
(0,0)
x
p1
(0,0) x
(un) (segundo)

Figure33.1(a)Thecrossproductofvectorsp1andp2isthesignedareaoftheparallelogram.
(b)Thelightlyshadedregioncontainsvectorsthatareclockwisefromp.Thedarklyshadedregion
containsvectorsthatarecounterclockwisefromp.

Crossproducts
Computingcrossproductsliesattheheartofourlinesegmentmethods.Considerar
vectorsp1andp2,showninFigure33.1(a).Wecaninterpretthecrossproduct
p1 p2asthesignedareaoftheparallelogramformedbythepoints.00/,p1,p2,
andp1Cp2D.x1Cx2y1Cy2/.Anequivalent,butmoreuseful,definitiongives
thecrossproductasthedeterminantofamatrix:1
UN UN
x1 x2
p1 p2 Ddet
y1 y2
Dx1y2 x2y1
Dp2 p1:

Ifp1 p2ispositive,thenp1isclockwisefromp2withrespecttotheorigin.00/
ifthiscrossproductisnegative,thenp1iscounterclockwisefromp2.(SeeExer
cise33.11.)Figure33.1(b)showstheclockwiseandcounterclockwiseregions
relativetoavectorp.Aboundaryconditionarisesifthecrossproductis0enesto
case,thevectorsarecolinear,pointingineitherthesameoroppositedirections.
Todeterminewhetheradirectedsegment ! 1isclosertoadirectedseg
p0p
!
Ment p0p2inaclockwisedirectionorinacounterclockwisedirectionwithrespect
totheircommonendpointp0,wesimplytranslatetousep0astheorigin.Ese
is,weletp1 p0denotethevectorp0 x0and
1D.x0 1y0
1/,wherex01Dx1
y0 y0,andwedefinep2 p0similarly.Wethencomputethecrossproduct
1Dy1

1Actually,thecrossproductisathreedimensionalconcept.Itisavectorthatisperpendicularto
bothp1andp2accordingtotherighthandruleandwhosemagnitudeisjx x2y11j.Enesto
y2
chapter,however,wefinditconvenienttotreatthecrossproductsimplyasthevaluex x2y1. 1y2

Page138

33.1Linesegmentproperties 1017

p2 p2
p1 p1
counterclockwise clockwise

p0 p0
(un) (segundo)

Figure33.2Usingthecrossproducttodeterminehowconsecutivelinesegmentsp0p1andp1p2
turnatpointp1.Wecheckwhetherthedirectedsegment! 2isclockwiseorcounterclockwise
p0p
! 1.(a)Ifcounterclockwise,thepointsmakealeftturn.(b)If
p0p
relativetothedirectedsegment
clockwise,theymakearightturn.

.p1 p0/.p2 p0/D.x1 x0/.y2 y0/.x2 x0/.y1 y0/:

https://translate.googleusercontent.com/translate_f 96/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
Ifthiscrossproductispositive,then p0p
! 1isclockwisefrom p0p
! 2ifnegative,it
iscounterclockwise.

Determiningwhetherconsecutivesegmentsturnleftorright
Ournextquestioniswhethertwoconsecutivelinesegmentsp0p1andp1p2turn
leftorrightatpointp1.Equivalently,wewantamethodtodeterminewhichwaya
givenanglep0p1p2turns.Crossproductsallowustoanswerthisquestionwith
outcomputingtheangle.AsFigure33.2shows,wesimplycheckwhetherdirected
segmentop0p! 2isclockwiseorcounterclockwiserelativetodirectedsegment p0p
! 1.
Todoso,wecomputethecrossproduct.p2 p0/.p1 p0/.Ifthesignof
! 2iscounterclockwisewithrespectto p0p
thiscrossproductisnegative,then p0p ! 1,
andthuswemakealeftturnatp1.Apositivecrossproductindicatesaclockwise
orientationandarightturn.Acrossproductof0meansthatpointsp0,p1,andp2
arecolinear.

Determiningwhethertwolinesegmentsintersect
Todeterminewhethertwolinesegmentsintersect,wecheckwhethereachsegment
straddlesthelinecontainingtheother.Asegmentp1p2straddlesalineifpointp1
liesononesideofthelineandpointp2liesontheotherside.Aboundarycase
arisesifp1orp2liesdirectlyontheline.Twolinesegmentsintersectifandonly
ifeither(orboth)ofthefollowingconditionsholds:
1.Eachsegmentstraddlesthelinecontainingtheother.
2.Anendpointofonesegmentliesontheothersegment.(Thisconditioncomes
fromtheboundarycase.)

Page139

1018 Chapter33ComputationalGeometry

Thefollowingproceduresimplementthisidea.SEGMENTSINTERSECTreturns
TRUEifsegmentsp1p2andp3p4intersectandFALSEiftheydonot.Itcalls
thesubroutinesDIRECTION,whichcomputesrelativeorientationsusingthecross
productmethodabove,andONSEGMENT,whichdetermineswhetherapoint
knowntobecolinearwithasegmentliesonthatsegment.

SEGMENTSINTERSECT.p1p2p3p4/
1d1DDIRECTION.p3p4p1/
2d2DDIRECTION.p3p4p2/
3d3DDIRECTION.p1p2p3/
4d4DDIRECTION.p1p2p4/
5if..d1>0andd2<0/or.d1<0andd2>0//and
..d3>0andd4<0/or.d3<0andd4>0//
6 returnTRUE
7elseifd1==0andONSEGMENT.p3p4p1/
8 returnTRUE
9elseifd2==0andONSEGMENT.p3p4p2/
10 returnTRUE
11elseifd3==0andONSEGMENT.p1p2p3/
12 returnTRUE
13elseifd4==0andONSEGMENT.p1p2p4/
14 returnTRUE
15elsereturnFALSE

DIRECTION.pipjpk/
1return.pk pi/.pj pi/

ONSEGMENT.pipjpk/
1ifmin.xixj/xkmax.xixj/andmin.yiyj/ykmax.yiyj/
2 returnTRUE
3elsereturnFALSE

SEGMENTSINTERSECTworksasfollows.Lines14computetherelativeori
entationdiofeachendpointpiwithrespecttotheothersegment.Ifalltherelative
orientationsarenonzero,thenwecaneasilydeterminewhethersegmentsp1p2

https://translate.googleusercontent.com/translate_f 97/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
andp3p
mentp 4intersect,asfollows.Segmentp
! 1p!
2straddlesthelinecontainingseg
3p4ifdirectedsegments p3p1and p3p2haveoppositeorientationsrelative
! 4.Inthiscase,thesignsofd1andd2differ.Similarly,p3p4straddles
a p3p
thelinecontainingp1p2ifthesignsofd3andd4differ.Ifthetestofline5is
true,thenthesegmentsstraddleeachother,andSEGMENTSINTERSECTreturns
TRUE.Figure33.3(a)showsthiscase.Otherwise,thesegmentsdonotstraddle

Pgina140

33.1Linesegmentproperties 1019

(p1 p3 )(p4 p3 )<0p4 (p1 p3 )(p4 p3 )<0p4


p1 (p4 p1 )(p2 p1 )<0 p1 (p4 p1 )(p2 p1 )<0
p2
(p2 p3 )(p4 p3 )<0
p2
(p3 p1 )(p2 p1 )>0 (p3 p1 )(p2 p1 )>0
p3 (p2 p3 )(p4 p3 )>0 p3
(un) (segundo)

p4 p4
p1 p1

p3
p2 p2
p3
(do) (re)

Figure33.3CasesintheprocedureSEGMENTSINTERSECT.(a)Thesegmentsp1p2andp3p4
straddleeachother'slines.Becausep3p4straddlesthelinecontainingp1p2,thesignsofthecross
products.p3 p1/.p2 p1/and.p4 p1/.p2 p1/differ.Becausep1p2straddlestheline
containingp3p4,thesignsofthecrossproducts.p
p3/.p
1 4 p3/and.p2 p3/.p4 p3/
diferirde.(b)Segmentp3p4straddlesthelinecontainingp1p2,butp1p2doesnotstraddletheline
containingp3p4.Thesignsofthecrossproducts.p
p3/.p1 4 p3/and.p2 p3/.p4 p3/
sonlomismo.(c)Pointp3iscolinearwithp1p2andisbetweenp1andp2.(d)Pointp3iscolinear
withp1p2,butitisnotbetweenp1andp2.Thesegmentsdonotintersect.

eachother'slines,althoughaboundarycasemayapply.Ifalltherelativeorienta
tionsarenonzero,noboundarycaseapplies.Allthetestsagainst0inlines713
thenfail,andSEGMENTSINTERSECTreturnsFALSEinline15.Figure33.3(b)
showsthiscase.
Aboundarycaseoccursifanyrelativeorientationdkis0.Here,weknowthatpk
iscolinearwiththeothersegment.Itisdirectlyontheothersegmentifandonly
ifitisbetweentheendpointsoftheothersegment.TheprocedureONSEGMENT
returnswhetherpkisbetweentheendpointsofsegmentpipj,whichwillbethe
othersegmentwhencalledinlines713theprocedureassumesthatpkiscolinear
withsegmentpipj.Figures33.3(c)and(d)showcaseswithcolinearpoints.En
Figure33.3(c),p3isonp1p2,andsoSEGMENTSINTERSECTreturnsTRUEin
line12.NoendpointsareonothersegmentsinFigure33.3(d),andsoSEGMENTS
INTERSECTreturnsFALSEinline15.

Pgina141

1020 Chapter33ComputationalGeometry

Otherapplicationsofcrossproducts
Latersectionsofthischapterintroduceadditionalusesforcrossproducts.InSec
tion33.3,weshallneedtosortasetofpointsaccordingtotheirpolarangleswith
respecttoagivenorigin.AsExercise33.13asksyoutoshow,wecanusecross
productstoperformthecomparisonsinthesortingprocedure.InSection33.2,we
shalluseredblacktreestomaintaintheverticalorderingofasetoflinesegments.
Ratherthankeepingexplicitkeyvalueswhichwecomparetoeachotherinthe
https://translate.googleusercontent.com/translate_f 98/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
redblacktreecode,weshallcomputeacrossproducttodeterminewhichoftwo
segmentsthatintersectagivenverticallineisabovetheother.

Exercises

33.11
Provethatifp1 p2ispositive,thenvectorp1isclockwisefromvectorp2with
respecttotheorigin.00/andthatifthiscrossproductisnegative,thenp1is
counterclockwisefromp2.

33.12
ProfessorvanPeltproposesthatonlythexdimensionneedstobetestedinline1
ofONSEGMENT.Showwhytheprofessoriswrong.

33.13
Thepolarangleofapointp1withrespecttoanoriginpointp0istheangleofthe
vectorp1 p0intheusualpolarcoordinatesystem.Forexample,thepolarangle
of.35/withrespectto.24/istheangleofthevector.11/,whichis45degrees
or=4radians.Thepolarangleof.33/withrespectto.24/istheangleofthe
vector.11/,whichis315degreesor7=4radians.Writepseudocodetosorta
sequencehp1p2:::pniofnpointsaccordingtotheirpolarangleswithrespect
toagivenoriginpointp0.YourprocedureshouldtakeO.nlgn/timeandusecross
productstocompareangles.

33.14
ShowhowtodetermineinOn2lgn/timewhetheranythreepointsinasetofn
pointsarecolinear.

33.15
Apolygonisapiecewiselinear,closedcurveintheplane.Thatis,itisacurve
endingonitselfthatisformedbyasequenceofstraightlinesegments,calledthe
sidesofthepolygon.Apointjoiningtwoconsecutivesidesisavertexofthepoly
gon.Ifthepolygonissimple,asweshallgenerallyassume,itdoesnotcrossitself.
Thesetofpointsintheplaneenclosedbyasimplepolygonformstheinteriorof

Page142

33.2Determiningwhetheranypairofsegmentsintersects 1021

thepolygon,thesetofpointsonthepolygonitselfformsitsboundary,andtheset
ofpointssurroundingthepolygonformsitsexterior.Asimplepolygonisconvex
if,givenanytwopointsonitsboundaryorinitsinterior,allpointsontheline
segmentdrawnbetweenthemarecontainedinthepolygon'sboundaryorinterior.
Avertexofaconvexpolygoncannotbeexpressedasaconvexcombinationofany
twodistinctpointsontheboundaryorintheinteriorofthepolygon.
ProfessorAmundsenproposesthefollowingmethodtodeterminewhetherase
quencehp0p1:::pn1iofnpointsformstheconsecutiveverticesofaconvex
polygon.OutputyesifthesetfpipiC1piC2WiD01:::n1g,wheresub
scriptadditionisperformedmodulon,doesnotcontainbothleftturnsandright
turnsotherwise,outputno.Showthatalthoughthismethodrunsinlineartime,
itdoesnotalwaysproducethecorrectanswer.Modifytheprofessor'smethodso
thatitalwaysproducesthecorrectanswerinlineartime.

33.16
Givenapointp0D.x0y0/,therighthorizontalrayfromp0isthesetofpoints
fpiD.xiyi/Wxi x0andyiDy0g,thatis,itisthesetofpointsduerightofp0
alongwithp0itself.Showhowtodeterminewhetheragivenrighthorizontalray
fromp0intersectsalinesegmentp1p2inO.1/timebyreducingtheproblemto
thatofdeterminingwhethertwolinesegmentsintersect.

33.17
Onewaytodeterminewhetherapointp0isintheinteriorofasimple,butnot
necessarilyconvex,polygonPistolookatanyrayfromp0andcheckthattheray
intersectstheboundaryofPanoddnumberoftimesbutthatp0itselfisnoton
theboundaryofP.Showhowtocomputein.n/timewhetherapointp0isin
theinteriorofannvertexpolygonP.(Hint:UseExercise33.16.Makesureyour
algorithmiscorrectwhentherayintersectsthepolygonboundaryatavertexand
whentherayoverlapsasideofthepolygon.)

https://translate.googleusercontent.com/translate_f 99/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
33.18
Showhowtocomputetheareaofannvertexsimple,butnotnecessarilyconvex,
polygonin.n/time.(SeeExercise33.15fordefinitionspertainingtopolygons.)

33.2Determiningwhetheranypairofsegmentsintersects

Thissectionpresentsanalgorithmfordeterminingwhetheranytwolinesegments
inasetofsegmentsintersect.Thealgorithmusesatechniqueknownassweep
ing,whichiscommontomanycomputationalgeometryalgorithms.Moreover,as

Pgina143

1022 Chapter33ComputationalGeometry

theexercisesattheendofthissectionshow,thisalgorithm,orsimplevariationsof
it,canhelpsolveothercomputationalgeometryproblems.
ThealgorithmrunsinO.nlgn/time,wherenisthenumberofsegmentsweare
dado.Itdeterminesonlywhetherornotanyintersectionexistsitdoesnotprint
alltheintersections.(ByExercise33.21,ittakes.n2/timeintheworstcaseto
findalltheintersectionsinasetofnlinesegments.)
Insweeping,animaginaryverticalsweeplinepassesthroughthegivensetof
geometricobjects,usuallyfromlefttoright.Wetreatthespatialdimensionthat
thesweeplinemovesacross,inthiscasethexdimension,asadimensionof
hora.Sweepingprovidesamethodfororderinggeometricobjects,usuallybyplac
ingthemintoadynamicdatastructure,andfortakingadvantageofrelationships
amongthem.Thelinesegmentintersectionalgorithminthissectionconsidersall
thelinesegmentendpointsinlefttorightorderandchecksforanintersectioneach
timeitencountersanendpoint.
Todescribeandprovecorrectouralgorithmfordeterminingwhetheranytwo
ofnlinesegmentsintersect,weshallmaketwosimplifyingassumptions.First,we
assumethatnoinputsegmentisvertical.Second,weassumethatnothreeinput
segmentsintersectatasinglepoint.Exercises33.28and33.29askyoutoshow
thatthealgorithmisrobustenoughthatitneedsonlyaslightmodificationtowork
evenwhentheseassumptionsdonothold.Indeed,removingsuchsimplifying
assumptionsanddealingwithboundaryconditionsoftenpresentthemostdifficult
challengeswhenprogrammingcomputationalgeometryalgorithmsandproving
theircorrectness.

Orderingsegments

Becauseweassumethattherearenoverticalsegments,weknowthatanyinput
segmentintersectingagivenverticalsweeplineintersectsitatasinglepoint.As,
wecanorderthesegmentsthatintersectaverticalsweeplineaccordingtothey
coordinatesofthepointsofintersection.
Tobemoreprecise,considertwosegmentss1ands2.Wesaythatthesesegments
arecomparableatxiftheverticalsweeplinewithxcoordinatexintersectsbothof
ellos.Wesaythats1isaboves2atx,writtens1<xs2,ifs1ands2arecomparable
atxandtheintersectionofs1withthesweeplineatxishigherthantheintersection
ofs2withthesamesweepline,orifs1ands2intersectatthesweepline.En
Figure33.4(a),forexample,wehavetherelationshipsa<rc,a<tb,b<tc,
a<tc,andb<uc.Segmentdisnotcomparablewithanyothersegment.
Foranygivenx,therelation<xisatotalpreorder(seeSectionB.2)forall
segmentsthatintersectthesweeplineatx.Thatis,therelationistransitive,and
ifsegmentss1ands2eachintersectthesweeplineatx,theneithers1<xs2
ors2<xs1,orboth(ifs1ands2intersectatthesweepline).(Therelation<xis

Page144

33.2Determiningwhetheranypairofsegmentsintersects 1023

mi
https://translate.googleusercontent.com/translate_f 100/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
re
un segundo gramo yo
do marido
F
R T Usted V Z W
(un) (segundo)

Figure33.4Theorderingamonglinesegmentsatvariousverticalsweeplines.(a)Wehavea<rc,
a<tb,b<tc,a<tc,andb<uc.Segmentdiscomparablewithnoothersegmentshown.
(b)Whensegmentseandfintersect,theyreversetheirorders:wehavee<fbutf<we.Alguna
sweepline(suchas)thatpassesthroughtheshadedregionhaseandfconsecutiveintheordering
givenbytherelation<.

alsoreflexive,butneithersymmetricnorantisymmetric.)Thetotalpreordermay
differfordifferingvaluesofx,however,assegmentsenterandleavetheordering.
Asegmententerstheorderingwhenitsleftendpointisencounteredbythesweep,
anditleavestheorderingwhenitsrightendpointisencountered.
Whathappenswhenthesweeplinepassesthroughtheintersectionoftwoseg
ments?AsFigure33.4(b)shows,thesegmentsreversetheirpositionsinthetotal
preorder.Sweeplinesandwaretotheleftandright,respectively,ofthepoint
ofintersectionofsegmentseandf,andwehavee<fandf<we.Nota
thatbecauseweassumethatnothreesegmentsintersectatthesamepoint,there
mustbesomeverticalsweeplinexforwhichintersectingsegmentseandfare
consecutiveinthetotalpreorder<x.Anysweeplinethatpassesthroughtheshaded
regionofFigure33.4(b),suchas,haseandfconsecutiveinitstotalpreorder.

Movingthesweepline

Sweepingalgorithmstypicallymanagetwosetsofdata:
1.Thesweeplinestatusgivestherelationshipsamongtheobjectsthatthesweep
lineintersects.
2.Theeventpointscheduleisasequenceofpoints,calledeventpoints,which
weorderfromlefttorightaccordingtotheirxcoordinates.Asthesweep
progressesfromlefttoright,wheneverthesweeplinereachesthexcoordinate
ofaneventpoint,thesweephalts,processestheeventpoint,andthenresumes.
Changestothesweeplinestatusoccuronlyateventpoints.
Forsomealgorithms(thealgorithmaskedforinExercise33.27,forexample),
theeventpointscheduledevelopsdynamicallyasthealgorithmprogresses.Theal
gorithmathand,however,determinesalltheeventpointsbeforethesweep,based

Pgina145

1024 Chapter33ComputationalGeometry

solelyonsimplepropertiesoftheinputdata.Inparticular,eachsegmentendpoint
isaneventpoint.Wesortthesegmentendpointsbyincreasingxcoordinateand
proceedfromlefttoright.(Iftwoormoreendpointsarecovertical,ie,theyhave
thesamexcoordinate,webreakthetiebyputtingallthecoverticalleftendpoints
beforethecoverticalrightendpoints.Withinasetofcoverticalleftendpoints,we
putthosewithlowerycoordinatesfirst,andwedothesamewithinasetofcover
ticalrightendpoints.)Whenweencounterasegment'sleftendpoint,weinsertthe
segmentintothesweeplinestatus,andwedeletethesegmentfromthesweepline
statusuponencounteringitsrightendpoint.Whenevertwosegmentsfirstbecome
consecutiveinthetotalpreorder,wecheckwhethertheyintersect.
ThesweeplinestatusisatotalpreorderT,forwhichwerequirethefollowing
Operaciones:

INSERT.Ts/:insertsegmentsintoT.
DELETE.Ts/:deletesegmentsfromT.
ABOVE.Ts/:returnthesegmentimmediatelyabovesegmentsinT.
BELOW.Ts/:returnthesegmentimmediatelybelowsegmentsinT.

Itispossibleforsegmentss1ands2tobemutuallyaboveeachotherinthetotal
preorderTthissituationcanoccurifs1ands2intersectatthesweeplinewhose
totalpreorderisgivenbyT.Inthiscase,thetwosegmentsmayappearineither
orderinT.
https://translate.googleusercontent.com/translate_f 101/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
Iftheinputcontainsnsegments,wecanperformeachoftheoperationsINSERT,
DELETE,ABOVE,andBELOWinO.lgn/timeusingredblacktrees.Recallthat
theredblacktreeoperationsinChapter13involvecomparingkeys.Wecanre
placethekeycomparisonsbycomparisonsthatusecrossproductstodeterminethe
relativeorderingoftwosegments(seeExercise33.22).

Segmentintersectionpseudocode

ThefollowingalgorithmtakesasinputasetSofnlinesegments,returningthe
booleanvalueTRUEifanypairofsegmentsinSintersects,andFALSEotherwise.
AredblacktreemaintainsthetotalpreorderT.

Page146

33.2Determiningwhetheranypairofsegmentsintersects 1025

ANYSEGMENTSINTERSECT.S/
1TD
2sorttheendpointsofthesegmentsinSfromlefttoright,
breakingtiesbyputtingleftendpointsbeforerightendpoints
andbreakingfurthertiesbyputtingpointswithlower
ycoordinatesfirst
3foreachpointpinthesortedlistofendpoints
4 ifpistheleftendpointofasegments
5 INSERT.Ts/
6 if(ABOVE.Ts/existsandintersectss)
or(BELOW.Ts/existsandintersectss)
7 returnTRUE
8 ifpistherightendpointofasegments
9 ifbothABOVE.Ts/andBELOW.Ts/exist
andABOVE.Ts/intersectsBELOW.Ts/
10 returnTRUE
11 DELETE.Ts/
12returnFALSE

Figure33.5illustrateshowthealgorithmworks.Line1initializesthetotalpreorder
tobeempty.Line2determinestheeventpointschedulebysortingthe2nsegment
endpointsfromlefttoright,breakingtiesasdescribedabove.Onewaytoperform
line2isbylexicographicallysortingtheendpointson.xey/,wherexandyare
theusualcoordinates,eD0foraleftendpoint,andeD1forarightendpoint.
Eachiterationoftheforloopoflines311processesoneeventpointp.Ifpis
theleftendpointofasegments,line5addsstothetotalpreorder,andlines67
returnTRUEifsintersectseitherofthesegmentsitisconsecutivewithinthetotal
preorderdefinedbythesweeplinepassingthroughp.(Aboundarycondition
occursifpliesonanothersegments0.Inthiscase,werequireonlythatsands0
beplacedconsecutivelyintoT.)Ifpistherightendpointofasegments,then
weneedtodeletesfromthetotalpreorder.Butfirst,lines910returnTRUEif
thereisanintersectionbetweenthesegmentssurroundingsinthetotalpreorder
definedbythesweeplinepassingthroughp.Ifthesesegmentsdonotintersect,
line11deletessegmentsfromthetotalpreorder.Ifthesegmentssurrounding
segmentsintersect,theywouldhavebecomeconsecutiveafterdeletingshadthe
returnstatementinline10notpreventedline11fromexecuting.Thecorrectness
argument,whichfollows,willmakeitclearwhyitsufficestocheckthesegments
surroundings.Finally,ifweneverfindanyintersectionsafterhavingprocessed
all2neventpoints,line12returnsFALSE.

https://translate.googleusercontent.com/translate_f 102/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,

Pgina147

1026 Chapter33ComputationalGeometry

mi
un re

do
F
segundo

un un un re re mi
segundo
do un do re
segundo
do segundo
do
segundo segundo
hora

Figure33.5TheexecutionofANYSEGMENTSINTERSECT.Eachdashedlineisthesweeplineat
aneventpoint.Exceptfortherightmostsweepline,theorderingofsegmentnamesbeloweachsweep
linecorrespondstothetotalpreorderTattheendoftheforloopprocessingthecorrespondingevent
punto.Therightmostsweeplineoccurswhenprocessingtherightendpointofsegmentcbecause
segmentsdandbsurroundcandintersecteachother,theprocedurereturnsTRUE.

Correctness

ToshowthatANYSEGMENTSINTERSECTiscorrect,wewillprovethatthecall
ANYSEGMENTSINTERSECT.S/returnsTRUEifandonlyifthereisanintersec
tionamongthesegmentsinS.
ItiseasytoseethatANYSEGMENTSINTERSECTreturnsTRUE(onlines7
and10)onlyifitfindsanintersectionbetweentwooftheinputsegments.Porlotanto,
ifitreturnsTRUE,thereisanintersection.
Wealsoneedtoshowtheconverse:thatifthereisanintersection,thenANY
SEGMENTSINTERSECTreturnsTRUE.Letussupposethatthereisatleastone
interseccin.Letpbetheleftmostintersectionpoint,breakingtiesbychoosingthe
pointwiththelowestycoordinate,andletaandbbethesegmentsthatintersect
atp.Sincenointersectionsoccurtotheleftofp,theordergivenbyTiscorrectat
allpointstotheleftofp.Becausenothreesegmentsintersectatthesamepoint,a
andbbecomeconsecutiveinthetotalpreorderatsomesweepline.2Moreover,
istotheleftofporgoesthroughp.Somesegmentendpointqonsweepline

2Ifweallowthreesegmentstointersectatthesamepoint,theremaybeaninterveningsegmentcthat
intersectsbothaandbatpointp.Thatis,wemayhavea<wcandc<wbforallsweeplineswto
theleftofpforwhicha<wb.Exercise33.28asksyoutoshowthatANYSEGMENTSINTERSECT
iscorrectevenifthreesegmentsdointersectatthesamepoint.

Pgina148

33.2Determiningwhetheranypairofsegmentsintersects 1027

istheeventpointatwhichaandbbecomeconsecutiveinthetotalpreorder.Ifp
isonsweepline,thenqDp.Ifpisnotonsweepline,thenqistotheleft
ofp.Ineithercase,theordergivenbyTiscorrectjustbeforeencounteringq.
(Hereiswhereweusethelexicographicorderinwhichthealgorithmprocesses
eventpoints.Becausepisthelowestoftheleftmostintersectionpoints,evenifp
isonsweeplineandsomeotherintersectionpointp0ison,eventpointqDp
isprocessedbeforetheotherintersectionp0caninterferewiththetotalpreorderT.
Moreover,evenifpistheleftendpointofonesegment,saya,andtherightend
pointoftheothersegment,sayb,becauseleftendpointeventsoccurbeforeright
endpointevents,segmentbisinTuponfirstencounteringsegmenta.)Eitherevent
pointqisprocessedbyANYSEGMENTSINTERSECToritisnotprocessed.
IfqisprocessedbyANYSEGMENTSINTERSECT,onlytwopossibleactions
mayoccur:
1.EitheraorbisinsertedintoT,andtheothersegmentisaboveorbelowitin
thetotalpreorder.Lines47detectthiscase.
https://translate.googleusercontent.com/translate_f 103/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,

2.SegmentsaandbarealreadyinT,andasegmentbetweentheminthetotal
preorderisdeleted,makingaandbbecomeconsecutive.Lines811detectthis
case.

Ineithercase,wefindtheintersectionpandANYSEGMENTSINTERSECTreturns
TRUE.
IfeventpointqisnotprocessedbyANYSEGMENTSINTERSECT,theproce
duremusthavereturnedbeforeprocessingalleventpoints.Thissituationcould
haveoccurredonlyifANYSEGMENTSINTERSECThadalreadyfoundaninter
sectionandreturnedTRUE.
Thus,ifthereisanintersection,ANYSEGMENTSINTERSECTreturnsTRUE.
Aswehavealreadyseen,ifANYSEGMENTSINTERSECTreturnsTRUE,thereis
anintersection.Therefore,ANYSEGMENTSINTERSECTalwaysreturnsacorrect
responder.

Runningtime
IfsetScontainsnsegments,thenANYSEGMENTSINTERSECTrunsintime
O.nlgn/.Line1takesO.1/time.Line2takesO.nlgn/time,usingmerge
sortorheapsort.Theforloopoflines311iteratesatmostoncepereventpoint,
andsowith2neventpoints,theloopiteratesatmost2ntimes.Eachiterationtakes
O.lgn/time,sinceeachredblacktreeoperationtakesO.lgn/timeand,usingthe
methodofSection33.1,eachintersectiontesttakesO.1/time.Thetotaltimeis
thusO.nlgn/.

Pgina149

1028 Chapter33ComputationalGeometry

Exercises

33.21
Showthatasetofnlinesegmentsmaycontain.n2/intersections.

33.22
Giventwosegmentsaandbthatarecomparableatx,showhowtodetermine
inO.1/timewhichofa<xborb<xaholds.Assumethatneithersegment
isvertical.(Hint:Ifaandbdonotintersect,youcanjustusecrossproducts.
Ifaandbintersectwhichyoucanofcoursedetermineusingonlycrossprod
uctsyoucanstilluseonlyaddition,subtraction,andmultiplication,avoiding
divisin.Ofcourse,intheapplicationofthe<xrelationusedhere,ifaandb
intersect,wecanjuststopanddeclarethatwehavefoundanintersection.)

33.23
ProfessorMasonsuggeststhatwemodifyANYSEGMENTSINTERSECTsothat
insteadofreturninguponfindinganintersection,itprintsthesegmentsthatinter
sectandcontinuesontothenextiterationoftheforloop.Theprofessorcallsthe
resultingprocedurePRINTINTERSECTINGSEGMENTSandclaimsthatitprints
allintersections,fromlefttoright,astheyoccurinthesetoflinesegments.Pro
fessorDixondisagrees,claimingthatProfessorMason'sideaisincorrect.Cual
professorisright?WillPRINTINTERSECTINGSEGMENTSalwaysfindtheleft
mostintersectionfirst?Willitalwaysfindalltheintersections?

33.24
GiveanO.nlgn/timealgorithmtodeterminewhetherannvertexpolygonis
sencillo.

33.25
GiveanO.nlgn/timealgorithmtodeterminewhethertwosimplepolygonswith
atotalofnverticesintersect.

33.26
Adiskconsistsofacircleplusitsinteriorandisrepresentedbyitscenterpointand
radio.Twodisksintersectiftheyhaveanypointincommon.GiveanO.nlgn/
timealgorithmtodeterminewhetheranytwodisksinasetofnintersect.

33.27
Givenasetofnlinesegmentscontainingatotalofkintersections,showhowto
https://translate.googleusercontent.com/translate_f 104/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
outputallkintersectionsinO..nCk/lgn/time.

Pgina150

33.3Findingtheconvexhull 1029

33.28
ArguethatANYSEGMENTSINTERSECTworkscorrectlyevenifthreeormore
segmentsintersectatthesamepoint.

33.29
ShowthatANYSEGMENTSINTERSECTworkscorrectlyinthepresenceofverti
calsegmentsifwetreatthebottomendpointofaverticalsegmentasifitwerea
leftendpointandthetopendpointasifitwerearightendpoint.Howdoesyour
answertoExercise33.22changeifweallowverticalsegments?

33.3Findingtheconvexhull

TheconvexhullofasetQofpoints,denotedbyCH.Q/,isthesmallestconvex
polygonPforwhicheachpointinQiseitherontheboundaryofPorinits
interior.(SeeExercise33.15foraprecisedefinitionofaconvexpolygon.)We
implicitlyassumethatallpointsinthesetQareuniqueandthatQcontainsat
leastthreepointswhicharenotcolinear.Intuitively,wecanthinkofeachpoint
inQasbeinganailstickingoutfromaboard.Theconvexhullisthentheshape
formedbyatightrubberbandthatsurroundsallthenails.Figure33.6showsaset
ofpointsanditsconvexhull.
Inthissection,weshallpresenttwoalgorithmsthatcomputetheconvexhull
ofasetofnpoints.Bothalgorithmsoutputtheverticesoftheconvexhullin
counterclockwiseorder.Thefirst,knownasGraham'sscan,runsinO.nlgn/time.
Thesecond,calledJarvis'smarch,runsinO.nh/time,wherehisthenumberof
verticesoftheconvexhull.AsFigure33.6illustrates,everyvertexofCH.Q/isa

p10

p11 p9 p7 p6 p5
p8 p3
p12 p4
p2
p1
p0

Figure33.6AsetofpointsQDfp0p1:::p12gwithitsconvexhullCH.Q/ingray.

Pgina151

1030 Chapter33ComputationalGeometry

pointinQ.Bothalgorithmsexploitthisproperty,decidingwhichverticesinQto
keepasverticesoftheconvexhullandwhichverticesinQtoreject.
WecancomputeconvexhullsinO.nlgn/timebyanyoneofseveralmethods.
BothGraham'sscanandJarvis'smarchuseatechniquecalledrotationalsweep,
processingverticesintheorderofthepolaranglestheyformwithareference
vertex.Othermethodsincludethefollowing:
Intheincrementalmethod,wefirstsortthepointsfromlefttoright,yieldinga
sequencehp1p2:::pni.Attheithstage,weupdatetheconvexhullofthe

https://translate.googleusercontent.com/translate_f 105/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
yo 1leftmostpoints,CH.fp11p
theleft,thusformingCH.fp p22:::p
:::pig/.Exercise33.36asksyouhowto
i1g/,accordingtotheithpointfrom

implementthismethodtotakeatotalofO.nlgn/time.
Inthedivideandconquermethod,wedividethesetofnpointsin.n/time
intotwosubsets,onecontainingtheleftmostdn=2epointsandonecontaining
therightmostbn=2cpoints,recursivelycomputetheconvexhullsofthesubsets,
andthen,bymeansofaclevermethod,combinethehullsinOn/time.los
runningtimeisdescribedbythefamiliarrecurrenceT.n/D2T.n=2/COn/,
andsothedivideandconquermethodrunsinO.nlgn/time.
Thepruneandsearchmethodissimilartotheworstcaselineartimemedian
algorithmofSection9.3.Withthismethod,wefindtheupperportion(orupper
chain)oftheconvexhullbyrepeatedlythrowingoutaconstantfractionofthe
remainingpointsuntilonlytheupperchainoftheconvexhullremains.Nosotrosentonces
dothesameforthelowerchain.Thismethodisasymptoticallythefastest:if
theconvexhullcontainshvertices,itrunsinonlyO.nlgh/time.
Computingtheconvexhullofasetofpointsisaninterestingprobleminitsown
right.Moreover,algorithmsforsomeothercomputationalgeometryproblemsstart
bycomputingaconvexhull.Consider,forexample,thetwodimensionalfarthest
pairproblem:wearegivenasetofnpointsintheplaneandwishtofindthe
twopointswhosedistancefromeachotherismaximum.AsExercise33.33asks
youtoprove,thesetwopointsmustbeverticesoftheconvexhull.Althoughwe
won'tproveithere,wecanfindthefarthestpairofverticesofannvertexconvex
polygoninOn/time.Thus,bycomputingtheconvexhulloftheninputpoints
inO.nlgn/timeandthenfindingthefarthestpairoftheresultingconvexpolygon
vertices,wecanfindthefarthestpairofpointsinanysetofnpointsinO.nlgn/
hora.

Graham'sscan
Graham'sscansolvestheconvexhullproblembymaintainingastackSofcan
didatepoints.ItpusheseachpointoftheinputsetQontothestackonetime,

Pgina152

33.3Findingtheconvexhull 1031

anditeventuallypopsfromthestackeachpointthatisnotavertexofCH.Q/.
Whenthealgorithmterminates,stackScontainsexactlytheverticesofCH.Q/,in
counterclockwiseorderoftheirappearanceontheboundary.
TheprocedureGRAHAMSCANtakesasinputasetQofpoints,wherejQj3.
ItcallsthefunctionsTOP.S/,whichreturnsthepointontopofstackSwithout
changingS,andNEXTTOTOP.S/,whichreturnsthepointoneentrybelowthe
topofstackSwithoutchangingS.Asweshallproveinamoment,thestackS
returnedbyGRAHAMSCANcontains,frombottomtotop,exactlythevertices
ofCH.Q/incounterclockwiseorder.

GRAHAMSCAN.Q/
1letp0bethepointinQwiththeminimumycoordinate,
ortheleftmostsuchpointincaseofatie
2lethp1p2:::pmibetheremainingpointsinQ,
sortedbypolarangleincounterclockwiseorderaroundp0
(ifmorethanonepointhasthesameangle,removeallbut
theonethatisfarthestfromp0)
3letSbeanemptystack
4PUSH.p0S/
5PUSH.p1S/
6PUSH.p2S/
7foriD3tom
8 whiletheangleformedbypointsNEXTTOTOP.S/,TOP.S/,
andpimakesanonleftturn
9 POP.S/
10 PUSH.piS/
11returnS

Figure33.7illustratestheprogressofGRAHAMSCAN.Line1choosespointp0
asthepointwiththelowestycoordinate,pickingtheleftmostsuchpointincase

https://translate.googleusercontent.com/translate_f 106/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
ofatie.SincethereisnopointinQthatisbelowp
thesameycoordinatearetoitsright,p 0andanyotherpointswith
0mustbeavertexofCH.Q/.Line2

sortstheremainingpointsofQbypolaranglerelativetop0,usingthesame
methodcomparingcrossproductsasinExercise33.13.Iftwoormorepoints
havethesamepolaranglerelativetop0,allbutthefarthestsuchpointareconvex
combinationsofp0andthefarthestpoint,andsoweremovethementirelyfrom
consideracin.Weletmdenotethenumberofpointsotherthanp0thatremain.
Thepolarangle,measuredinradians,ofeachpointinQrelativetop0isinthe
halfopeninterval0/.Sincethepointsaresortedaccordingtopolarangles,
theyaresortedincounterclockwiseorderrelativetop0.Wedesignatethissorted
sequenceofpointsbyhp1p2:::pmi.Notethatpointsp1andpmarevertices

Pgina153

1032 Chapter33ComputationalGeometry

p10 p10

p6 p6
p11 p9 p7 p11 p9 p7
p8 p5 p8 p5
p4 p3 p4 p3
p12 p12
p2 p2
p1 p1
p0 (un) p0 (segundo)

p10 p10

p6 p6
p11 p9 p7 p11 p9 p7 p5
p8 p5 p8
p3 p3
p12 p4 p12 p4
p2 p2
p1 p1
p0 (do) p0 (re)

p10 p10

p6 p6
p11 p9 p7 p11 p9 p7
p8 p5 p8 p5
p4 p3 p4 p3
p12 p12
p2 p2
p1 p1
p0 (mi) p0 (f)

Figure33.7TheexecutionofGRAHAMSCANonthesetQofFigure33.6.Thecurrentconvex
hullcontainedinstackSisshowningrayateachstep.(a)Thesequencehp1p2:::p12iofpoints
numberedinorderofincreasingpolaranglerelativetop0,andtheinitialstackScontainingp0,p1,
andp2.(b)(k)StackSaftereachiterationoftheforloopoflines710.Dashedlinesshownonleft
turns,whichcausepointstobepoppedfromthestack.Inpart(h),forexample,therightturnat
anglep7p8p9causesp8tobepopped,andthentherightturnatanglep6p7p9causesp7tobe
popped.

Pgina154

33.3Findingtheconvexhull 1033

p10 p10

https://translate.googleusercontent.com/translate_f 107/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,

p11 p9 p7 p6 p11 p9 p6
p5 p7 p5
p8 p4 p3 p8 p4 p3
p12 p12
p2 p2
p1 p1
p0 (gramo) p0 (marido)

p10 p10

p11 p9 p7 p6
p9 p7 p6 p5 p11 p8 p5
p8 p4 p3 p4 p3
p12 p12
p2 p2
p1 p1
p0 (yo) p0 (J)

p10 p10

p9 p7 p6 p9 p7 p6
p11 p8 p5 p11 p8 p5
p4 p3 p4 p3
p12 p12
p2 p2
p1 p1
p0 (K) p0 (L)

Figure33.7,continued(l)Theconvexhullreturnedbytheprocedure,whichmatchesthatof
Figure33.6.

Pgina155

1034 Chapter33ComputationalGeometry

ofCH.Q/(seeExercise33.31).Figure33.7(a)showsthepointsofFigure33.6
sequentiallynumberedinorderofincreasingpolaranglerelativetop0.
TheremainderoftheprocedureusesthestackS.Lines36initializethestack
tocontain,frombottomtotop,thefirstthreepointsp0,p1,andp2.Figure33.7(a)
showstheinitialstackS.Theforloopoflines710iteratesonceforeachpoint
inthesubsequencehp3p4:::pmi.Weshallseethatafterprocessingpointpi,
stackScontains,frombottomtotop,theverticesofCH.fp0p1:::pig/incoun
terclockwiseorder.Thewhileloopoflines89removespointsfromthestackif
wefindthemnottobeverticesoftheconvexhull.Whenwetraversetheconvex
hullcounterclockwise,weshouldmakealeftturnateachvertex.Thus,eachtime
thewhileloopfindsavertexatwhichwemakeanonleftturn,wepopthevertex
fromthestack.(Bycheckingforanonleftturn,ratherthanjustarightturn,this
testprecludesthepossibilityofastraightangleatavertexoftheresultingconvex
hull.Wewantnostraightangles,sincenovertexofaconvexpolygonmaybea
convexcombinationofotherverticesofthepolygon.)Afterwepopallvertices
thathavenonleftturnswhenheadingtowardpointpi,wepushpiontothestack.
Figures33.7(b)(k)showthestateofthestackSaftereachiterationofthefor
lazo.Finally,GRAHAMSCANreturnsthestackSinline11.Figure33.7(l)shows
thecorrespondingconvexhull.
ThefollowingtheoremformallyprovesthecorrectnessofGRAHAMSCAN.

Theorem33.1(CorrectnessofGraham'sscan)
IfGRAHAMSCANexecutesonasetQofpoints,wherejQj3,thenattermina
tion,thestackSconsistsof,frombottomtotop,exactlytheverticesofCH.Q/in
https://translate.googleusercontent.com/translate_f 108/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
counterclockwiseorder.

ProofAfterline2,wehavethesequenceofpointshp1p2:::pmi.Nosdeja
define,foriD23:::m,thesubsetofpointsQiDfp0p1:::pig.El
pointsinQQmarethosethatwereremovedbecausetheyhadthesamepolar
anglerelativetop0assomepointinQmthesepointsarenotinCH.Q/,and
soCH.Qm/DCH.Q/.Thus,itsufficestoshowthatwhenGRAHAMSCAN
terminates,thestackSconsistsoftheverticesofCH.Qm/incounterclockwise
order,whenlistedfrombottomtotop.Notethatjustasp0,p1,andpmarevertices
ofCH.Q/,thepointsp0,p1,andpiareallverticesofCH.Qi/.
Theproofusesthefollowingloopinvariant:
Atthestartofeachiterationoftheforloopoflines710,stackSconsistsof,
frombottomtotop,exactlytheverticesofCH.Qi1/incounterclockwise
orden.

Initialization:Theinvariantholdsthefirsttimeweexecuteline7,sinceatthat
time,stackSconsistsofexactlytheverticesofQ2DQi1,andthissetofthree

Pgina156

33.3Findingtheconvexhull 1035

pj pk pj

pi pi pr
pt
Qj
p2

p1 p1
p0 p0
(un) (segundo)

Figure33.8TheproofofcorrectnessofGRAHAMSCAN.(a)Becausepi'spolaranglerelative
top0isgreaterthanpj'spolarangle,andbecausetheanglepkpjpimakesaleftturn,addingpi
toCH.Qj/givesexactlytheverticesofCH.Qj[fpig/.(b)Iftheangleprptpimakesanonleft
turn,thenptiseitherintheinteriorofthetriangleformedbyp0,pr,andpioronasideofthe
triangle,whichmeansthatitcannotbeavertexofCH.Qi/.

verticesformsitsownconvexhull.Moreover,theyappearincounterclockwise
orderfrombottomtotop.
Maintenance:Enteringaniterationoftheforloop,thetoppointonstackS
ispi1,whichwaspushedattheendofthepreviousiteration(orbeforethe
firstiteration,wheniD3).LetpjbethetoppointonSafterexecutingthe
whileloopoflines89butbeforeline10pushespi,andletpkbethepoint
justbelowpjonS.AtthemomentthatpjisthetoppointonSandwehave
notyetpushedpi,stackScontainsexactlythesamepointsitcontainedafter
iterationjoftheforloop.Bytheloopinvariant,therefore,Scontainsexactly
theverticesofCH.Qj/atthatmoment,andtheyappearincounterclockwise
orderfrombottomtotop.
Letuscontinuetofocusonthismomentjustbeforepushingpi.Sabemos
thatpi'spolaranglerelativetop0isgreaterthanpj'spolarangleandthat
theanglepkpjpimakesaleftturn(otherwisewewouldhavepoppedpj).
Therefore,becauseScontainsexactlytheverticesofCH.Qj/,weseefrom
Figure33.8(a)thatoncewepushpi,stackSwillcontainexactlythevertices
ofCH.Qj[fpig/,stillincounterclockwiseorderfrombottomtotop.
WenowshowthatCH.Qj[fpig/isthesamesetofpointsasCH.Qi/.Consider
anypointptthatwaspoppedduringiterationioftheforloop,andletprbe
thepointjustbelowptonstackSatthetimeptwaspopped(prmightbepj).
Theangleprptpimakesanonleftturn,andthepolarangleofptrelative
top0isgreaterthanthepolarangleofpr.AsFigure33.8(b)shows,ptmust

https://translate.googleusercontent.com/translate_f 109/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
Pgina157

1036 Chapter33ComputationalGeometry

beeitherintheinteriorofthetriangleformedbyp0,pr,andpioronasideof
thistriangle(butitisnotavertexofthetriangle).Clearly,sinceptiswithina
triangleformedbythreeotherpointsofQi,itcannotbeavertexofCH.Qi/.
SinceptisnotavertexofCH.Qi/,wehavethat

CH.Qi fptg/DCH.Qi/: (33.1)

LetPibethesetofpointsthatwerepoppedduringiterationioftheforloop.
Sincetheequality(33.1)appliesforallpointsinPi,wecanapplyitrepeatedly
toshowthatCH.Qi Pi/DCH.Qi/.ButQi PiDQj[fpig,andsowe
concludethatCH.Qj[fpig/DCH.Qi Pi/DCH.Qi/.
Wehaveshownthatoncewepushpi,stackScontainsexactlythevertices
ofCH.Qi/incounterclockwiseorderfrombottomtotop.Incrementingiwill
thencausetheloopinvarianttoholdforthenextiteration.
Termination:Whentheloopterminates,wehaveiDmC1,andsotheloop
invariantimpliesthatstackSconsistsofexactlytheverticesofCH.Qm/,which
isCH.Q/,incounterclockwiseorderfrombottomtotop.Thiscompletesthe
prueba.

WenowshowthattherunningtimeofGRAHAMSCANisO.nlgn/,where
nDjQj.Line1takes.n/time.Line2takesO.nlgn/time,usingmergesort
orheapsorttosortthepolaranglesandthecrossproductmethodofSection33.1
tocompareangles.(Wecanremoveallbutthefarthestpointwiththesamepolar
angleintotalofOn/timeoverallnpoints.)Lines36takeO.1/time.Porque
mn1,theforloopoflines710executesatmostn3times.SincePUSH
takesO.1/time,eachiterationtakesO.1/timeexclusiveofthetimespentinthe
whileloopoflines89,andthusoveralltheforlooptakesOn/timeexclusiveof
thenestedwhileloop.
WeuseaggregateanalysistoshowthatthewhilelooptakesOn/timeoverall.
ForiD01:::m,wepusheachpointpiontostackSexactlyonce.Asinthe
analysisoftheMULTIPOPprocedureofSection17.1,weobservethatwecanpopat
mostthenumberofitemsthatwepush.Atleastthreepointsp0,p1,andpmare
neverpoppedfromthestack,sothatinfactatmostm 2POPoperationsare
performedintotal.EachiterationofthewhileloopperformsonePOP,andso
thereareatmostm2iterationsofthewhileloopaltogether.Sincethetestin
line8takesO.1/time,eachcallofPOPtakesO.1/time,andmn1,thetotal
timetakenbythewhileloopisOn/.Thus,therunningtimeofGRAHAMSCAN
isO.nlgn/.

Pgina158

33.3Findingtheconvexhull 1037

leftchain rightchain

p3

p4 p2

p1
p0
leftchain rightchain
https://translate.googleusercontent.com/translate_f 110/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,

Figure33.9TheoperationofJarvis'smarch.Wechoosethefirstvertexasthelowestpointp0.
Thenextvertex,p1,hasthesmallestpolarangleofanypointwithrespecttop0.Then,p2hasthe
smallestpolaranglewithrespecttop1.Therightchaingoesashighasthehighestpointp3.Entonces,
weconstructtheleftchainbyfindingsmallestpolarangleswithrespecttothenegativexaxis.

Jarvis'smarch

Jarvis'smarchcomputestheconvexhullofasetQofpointsbyatechniqueknown
aspackagewrapping(orgiftwrapping).ThealgorithmrunsintimeO.nh/,
wherehisthenumberofverticesofCH.Q/.Whenhiso.lgn/,Jarvis'smarchis
asymptoticallyfasterthanGraham'sscan.
Intuitively,Jarvis'smarchsimulateswrappingatautpieceofpaperaroundthe
setQ.Westartbytapingtheendofthepapertothelowestpointintheset,thatis,
tothesamepointp0withwhichwestartGraham'sscan.Weknowthatthispoint
mustbeavertexoftheconvexhull.Wepullthepapertotherighttomakeittaut,
andthenwepullithigheruntilittouchesapoint.Thispointmustalsobeavertex
oftheconvexhull.Keepingthepapertaut,wecontinueinthiswayaroundtheset
ofverticesuntilwecomebacktoouroriginalpointp0.
Moreformally,Jarvis'smarchbuildsasequenceHDhp0p1:::ph1iofthe
verticesofCH.Q/.Westartwithp0.AsFigure33.9shows,thenextvertexp1
intheconvexhullhasthesmallestpolaranglewithrespecttop0.(Incaseofties,
wechoosethepointfarthestfromp0.)Similarly,p2hasthesmallestpolarangle

Pgina159

1038 Chapter33ComputationalGeometry

withrespecttop1,andsoon.Whenwereachthehighestvertex,saypk(breaking
tiesbychoosingthefarthestsuchvertex),wehaveconstructed,asFigure33.9
shows,therightchainofCH.Q/.Toconstructtheleftchain,westartatpkand
choosepkC1asthepointwiththesmallestpolaranglewithrespecttopk,butfrom
thenegativexaxis.Wecontinueon,formingtheleftchainbytakingpolarangles
fromthenegativexaxis,untilwecomebacktoouroriginalvertexp0.
WecouldimplementJarvis'smarchinoneconceptualsweeparoundtheconvex
hull,thatis,withoutseparatelyconstructingtherightandleftchains.Suchimple
mentationstypicallykeeptrackoftheangleofthelastconvexhullsidechosenand
requirethesequenceofanglesofhullsidestobestrictlyincreasing(intherange
of0to2radians).Theadvantageofconstructingseparatechainsisthatweneed
notexplicitlycomputeanglesthetechniquesofSection33.1sufficetocompare
anglos.
Ifimplementedproperly,Jarvis'smarchhasarunningtimeofO.nh/.Paracada
ofthehverticesofCH.Q/,wefindthevertexwiththeminimumpolarangle.Cada
comparisonbetweenpolaranglestakesO.1/time,usingthetechniquesofSec
tion33.1.AsSection9.1shows,wecancomputetheminimumofnvaluesinOn/
timeifeachcomparisontakesO.1/time.Thus,Jarvis'smarchtakesO.nh/time.

Exercises

33.31
ProvethatintheprocedureGRAHAMSCAN,pointsp1andpmmustbevertices
ofCH.Q/.

33.32
Consideramodelofcomputationthatsupportsaddition,comparison,andmultipli
cationandforwhichthereisalowerboundof.nlgn/tosortnnumbers.Prove
that.nlgn/isalowerboundforcomputing,inorder,theverticesoftheconvex
hullofasetofnpointsinsuchamodel.

33.33
GivenasetofpointsQ,provethatthepairofpointsfarthestfromeachothermust
beverticesofCH.Q/.

33.34
ForagivenpolygonPandapointqonitsboundary,theshadowofqistheset
ofpointsrsuchthatthesegmentqrisentirelyontheboundaryorintheinterior
ofP.AsFigure33.10illustrates,apolygonPisstarshapedifthereexistsa
pointpintheinteriorofPthatisintheshadowofeverypointontheboundary
https://translate.googleusercontent.com/translate_f 111/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,

ofP.ThesetofallsuchpointspiscalledthekernelofP.Givenannvertex,

Pgina160

33.4Findingtheclosestpairofpoints 1039

pag
Q

(un) (segundo)

Figure33.10Thedefinitionofastarshapedpolygon,foruseinExercise33.34.(a)Astarshaped
polygon.Thesegmentfrompointptoanypointqontheboundaryintersectstheboundaryonlyatq.
(b)Anonstarshapedpolygon.Theshadedregionontheleftistheshadowofq,andtheshaded
regionontherightistheshadowofq0.Sincetheseregionsaredisjoint,thekernelisempty.

starshapedpolygonPspecifiedbyitsverticesincounterclockwiseorder,show
howtocomputeCH.P/inOn/time.

33.35
Intheonlineconvexhullproblem,wearegiventhesetQofnpointsonepointat
atime.Afterreceivingeachpoint,wecomputetheconvexhullofthepointsseen
sofar.Obviously,wecouldrunGraham'sscanonceforeachpoint,withatotal
runningtimeofOn2lgn/.Showhowtosolvetheonlineconvexhullproblemin
atotalofOn2/time.

33.36?
Showhowtoimplementtheincrementalmethodforcomputingtheconvexhull
ofnpointssothatitrunsinO.nlgn/time.

33.4Findingtheclosestpairofpoints

WenowconsidertheproblemoffindingtheclosestpairofpointsinasetQof
norte2points.Closestreferstotheusualeuclideandistance:thedistancebetween
pag
pointsp1D.x1y1/andp2D.x2y2/is .x1 x2/2C.y1 y2/2.Twopoints
insetQmaybecoincident,inwhichcasethedistancebetweenthemiszero.Esta
problemhasapplicationsin,forexample,trafficcontrolsystems.Asystemfor
controllingairorseatrafficmightneedtoidentifythetwoclosestvehiclesinorder
todetectpotentialcollisions.
Abruteforceclosestpairalgorithmsimplylooksatallthe norte
D.n2/pairs
2
ofpoints.Inthissection,weshalldescribeadivideandconqueralgorithmfor

Pgina161

1040 Chapter33ComputationalGeometry

thisproblem,whoserunningtimeisdescribedbythefamiliarrecurrenceT.n/D
2T.n=2/COn/.Thus,thisalgorithmusesonlyO.nlgn/time.

Thedivideandconqueralgorithm

EachrecursiveinvocationofthealgorithmtakesasinputasubsetPQand
arraysXandY,eachofwhichcontainsallthepointsoftheinputsubsetP.
ThepointsinarrayXaresortedsothattheirxcoordinatesaremonotonically
creciente.Similarly,arrayYissortedbymonotonicallyincreasingycoordinate.
NotethatinordertoattaintheO.nlgn/timebound,wecannotaffordtosort
ineachrecursivecallifwedid,therecurrencefortherunningtimewouldbe
https://translate.googleusercontent.com/translate_f 112/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
T.n/D2T.n=2/CO.nlgn/,whosesolutionisT.n/DO.nlg2n/.(Usethe
versionofthemastermethodgiveninExercise4.62.)Weshallseealittlelater
howtousepresortingtomaintainthissortedpropertywithoutactuallysortingin
eachrecursivecall.
AgivenrecursiveinvocationwithinputsP,X,andYfirstcheckswhether
jPj3.Ifso,theinvocationsimplyperformsthebruteforcemethoddescribed
above:tryall jPj pairsofpointsandreturntheclosestpair.IfjPj>3,the
2
recursiveinvocationcarriesoutthedivideandconquerparadigmasfollows.

Divide:FindaverticallinelthatbisectsthepointsetPintotwosetsPLandPR
suchthatjPLjDdjPj=2e,jPRjDbjPj=2c,allpointsinPLareonortothe
leftoflinel,andallpointsinPRareonortotherightofl.DividethearrayX
intoarraysXLandXR,whichcontainthepointsofPLandPRrespectively,
sortedbymonotonicallyincreasingxcoordinate.Similarly,dividethearrayY
intoarraysYLandYR,whichcontainthepointsofPLandPRrespectively,
sortedbymonotonicallyincreasingycoordinate.
Conquer:HavingdividedPintoPLandPR,maketworecursivecalls,onetofind
theclosestpairofpointsinPLandtheothertofindtheclosestpairofpoints
inPR.TheinputstothefirstcallarethesubsetPLandarraysXLandYLEl
secondcallreceivestheinputsPR,XR,andYR.Lettheclosestpairdistances
returnedforPLandPRbeLandR,respectively,andletDmin.LR/.
Combine:Theclosestpairiseitherthepairwithdistancefoundbyoneofthe
recursivecalls,oritisapairofpointswithonepointinPLandtheotherinPR.
ThealgorithmdetermineswhetherthereisapairwithonepointinPLandthe
otherpointinPRandwhosedistanceislessthan.Observethatifapairof
pointshasdistancelessthan,bothpointsofthepairmustbewithinunits
oflinel.Thus,asFigure33.11(a)shows,theybothmustresideinthe2wide
verticalstripcenteredatlinel.Tofindsuchapair,ifoneexists,wedothe
siguiendo:

Pgina162

33.4Findingtheclosestpairofpoints 1041

1.CreateanarrayY0,whichisthearrayYwithallpointsnotinthe2wide
verticalstripremoved.ThearrayY0issortedbyycoordinate,justasYis.
2.ForeachpointpinthearrayY0,trytofindpointsinY0thatarewithin
unitsofp.Asweshallseeshortly,onlythe7pointsinY0thatfollowpneed
beconsidered.Computethedistancefromptoeachofthese7points,and
keeptrackoftheclosestpairdistance0foundoverallpairsofpointsinY0.
3.If0<,thentheverticalstripdoesindeedcontainacloserpairthanthe
recursivecallsfound.Returnthispairanditsdistance0.Otherwise,return
theclosestpairanditsdistancefoundbytherecursivecalls.
Theabovedescriptionomitssomeimplementationdetailsthatarenecessaryto
achievetheO.nlgn/runningtime.Afterprovingthecorrectnessofthealgorithm,
weshallshowhowtoimplementthealgorithmtoachievethedesiredtimebound.

Correctness

Thecorrectnessofthisclosestpairalgorithmisobvious,exceptfortwoaspects.
First,bybottomingouttherecursionwhenjPj3,weensurethatwenevertryto
solveasubproblemconsistingofonlyonepoint.Thesecondaspectisthatweneed
onlycheckthe7pointsfollowingeachpointpinarrayY0weshallnowprovethis
property.
Supposethatatsomeleveloftherecursion,theclosestpairofpointsispL2PL
andpR2PR.Thus,thedistance0betweenpLandpRisstrictlylessthan.
PointpLmustbeonortotheleftoflinelandlessthanunitsaway.Similarly,pR
isonortotherightoflandlessthanunitsaway.Moreover,pLandpRare
withinunitsofeachothervertically.Thus,asFigure33.11(a)shows,pLandpR
arewithina2rectanglecenteredatlinel.(Theremaybeotherpointswithin
thisrectangleaswell.)
Wenextshowthatatmost8pointsofPcanresidewithinthis2rectangle.
Considerthesquareformingthelefthalfofthisrectangle.Sinceallpoints
withinPLareatleastunitsapart,atmost4pointscanresidewithinthissquare
Figure33.11(b)showshow.Similarly,atmost4pointsinPRcanresidewithin

https://translate.googleusercontent.com/translate_f 113/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
thesquareformingtherighthalfoftherectangle.Thus,atmost8pointsofP
canresidewithinthe2rectangle.(Notethatsincepointsonlinelmaybein
eitherPLorPR,theremaybeupto4pointsonl.Thislimitisachievedifthereare
twopairsofcoincidentpointssuchthateachpairconsistsofonepointfromPLand
onepointfromPR,onepairisattheintersectionoflandthetopoftherectangle,
andtheotherpairiswherelintersectsthebottomoftherectangle.)
Havingshownthatatmost8pointsofPcanresidewithintherectangle,we
caneasilyseewhyweneedtocheckonlythe7pointsfollowingeachpointinthe
arrayY0.StillassumingthattheclosestpairispLandpR,letusassumewithout

Pgina163

1042 Chapter33ComputationalGeometry

PR
PL

PR
2 PL
pR
coincidentpoints,
pL
oneinPL,
oneinPR
coincidentpoints,
oneinPL,
L L oneinPR
(un) (segundo)

Figure33.11Keyconceptsintheproofthattheclosestpairalgorithmneedstocheckonly7points
followingeachpointinthearrayY0.(a)IfpL2PLandpR2PRarelessthanunitsapart,they
mustresidewithina2rectanglecenteredatlinel.(b)How4pointsthatarepairwiseatleast
unitsapartcanallresidewithinasquare.Ontheleftare4pointsinPL,andontherightare4
pointsinPR.The 2rectanglecancontain8pointsifthepointsshownonlinelareactually
pairsofcoincidentpointswithonepointinPLandoneinPR.

lossofgeneralitythatpLprecedespRinarrayY0.Then,evenifpLoccursasearly
aspossibleinY0andpRoccursaslateaspossible,pRisinoneofthe7positions
followingpL.Thus,wehaveshownthecorrectnessoftheclosestpairalgorithm.

Implementationandrunningtime
Aswehavenoted,ourgoalistohavetherecurrencefortherunningtimebeT.n/D
2T.n=2/COn/,whereT.n/istherunningtimeforasetofnpoints.Elprincipal
difficultycomesfromensuringthatthearraysXL,XR,YL,andYR,whichare
passedtorecursivecalls,aresortedbythepropercoordinateandalsothatthe
arrayY0issortedbyycoordinate.(NotethatifthearrayXthatisreceivedbya
recursivecallisalreadysorted,thenwecaneasilydividesetPintoPLandPRin
lineartime.)
Thekeyobservationisthatineachcall,wewishtoformasortedsubsetofa
sortedarray.Forexample,aparticularinvocationreceivesthesubsetPandthe
arrayY,sortedbyycoordinate.HavingpartitionedPintoPLandPR,itneedsto
formthearraysYLandYR,whicharesortedbyycoordinate,inlineartime.Nosotros
canviewthemethodastheoppositeoftheMERGEprocedurefrommergesortin

Pgina164

33.4Findingtheclosestpairofpoints 1043

Section2.3.1:wearesplittingasortedarrayintotwosortedarrays.Thefollowing
pseudocodegivestheidea.

1letYL1::Y:lengthandYR1::Y:lengthbenewarrays
https://translate.googleusercontent.com/translate_f 114/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
2YL:lengthDYR:lengthD0
3foriD1toY:length
4 ifYi2PL
5 YL:lengthDYL:lengthC1
6 YLYL:lengthDYi
7 elseYR:lengthDYR:lengthC1
8 YRYR:lengthDYi

WesimplyexaminethepointsinarrayYinorder.IfapointYiisinPL,we
appendittotheendofarrayYLotherwise,weappendittotheendofarrayYR.
SimilarpseudocodeworksforformingarraysXL,XR,andY0.
Theonlyremainingquestionishowtogetthepointssortedinthefirstplace.Nosotros
presortthemthatis,wesortthemonceandforallbeforethefirstrecursivecall.
Wepassthesesortedarraysintothefirstrecursivecall,andfromtherewewhittle
themdownthroughtherecursivecallsasnecessary.Presortingaddsanadditional
O.nlgn/termtotherunningtime,butnoweachstepoftherecursiontakeslinear
timeexclusiveoftherecursivecalls.Thus,ifweletT.n/betherunningtimeof
eachrecursivestepandT0.n/betherunningtimeoftheentirealgorithm,weget
T0.n/DT.n/CO.nlgn/and
(
2T.n=2/COn/ifn>3
T.n/D
O.1/ ifn3:

Thus,T.n/DO.nlgn/andT0.n/DO.nlgn/.

Exercises

33.41
ProfessorWilliamscomesupwithaschemethatallowstheclosestpairalgorithm
tocheckonly5pointsfollowingeachpointinarrayY0.Theideaisalwaystoplace
pointsonlinelintosetPL.Then,therecannotbepairsofcoincidentpointson
linelwithonepointinPLandoneinPR.Thus,atmost6pointscanresidein
the2rectangle.Whatistheflawintheprofessor'sscheme?

33.42
Showthatitactuallysufficestocheckonlythepointsinthe5arraypositionsfol
lowingeachpointinthearrayY0.

Pgina165

1044 Chapter33ComputationalGeometry

33.43
Wecandefinethedistancebetweentwopointsinwaysotherthaneuclidean.En
theplane,theLmdistancebetweenpointsp1andp2isgivenbytheexpres
sion.jx1 x2jmetro / 1=m.Euclideandistance,therefore,isL2distance.
Cjy1 y2jmetro
ModifytheclosestpairalgorithmtousetheL1distance,whichisalsoknownas
theManhattandistance.

33.44
Giventwopointsp1andp2intheplane,theL1distancebetweenthemis
givenbymax.jx1 x2jjy1 y2j/.Modifytheclosestpairalgorithmtousethe
L1distance.

33.45
Supposethat.n/ofthepointsgiventotheclosestpairalgorithmarecovertical.
ShowhowtodeterminethesetsPLandPRandhowtodeterminewhethereach
pointofYisinPLorPRsothattherunningtimefortheclosestpairalgorithm
remainsO.nlgn/.

33.46
SuggestachangetotheclosestpairalgorithmthatavoidspresortingtheYarray
butleavestherunningtimeasO.nlgn/.(Hint:MergesortedarraysYLandYRto
formthesortedarrayY.)

Problemas

https://translate.googleusercontent.com/translate_f 115/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,

331Convexlayers
GivenasetQofpointsintheplane,wedefinetheconvexlayersofQinductively.
ThefirstconvexlayerofQconsistsofthosepointsinQthatareverticesofCH.Q/.
Fori>1,defineQitoconsistofthepointsofQwithallpointsinconvexlayers
12:::i1removed.Then,theithconvexlayerofQisCH.Qi/ifQiY
isundefinedotherwise.

a.GiveanOn2/timealgorithmtofindtheconvexlayersofasetofnpoints.

segundo.Provethat.nlgn/timeisrequiredtocomputetheconvexlayersofasetofn
pointswithanymodelofcomputationthatrequires.nlgn/timetosortnreal
nmeros.

Page166

ProblemsforChapter33 1045

332Maximallayers
LetQbeasetofnpointsintheplane.Wesaythatpoint.xy/dominates
point.x0y0/ifx x0andy y0.ApointinQthatisdominatedbynoother
pointsinQissaidtobemaximal.NotethatQmaycontainmanymaximalpoints,
whichcanbeorganizedintomaximallayersasfollows.ThefirstmaximallayerL1
isthesetofmaximalpointsofQ.Fori>1,theithmaximallayerL
Si1 iisthesetof

maximalpointsinQ jD1Lj.
SupposethatQhasknonemptymaximallayers,andletyibetheycoordinate
oftheleftmostpointinLiforiD12:::KFornow,assumethatnotwopoints
inQhavethesamexorycoordinate.
a.Showthaty1>y2> >yk.
Considerapoint.xy/thatistotheleftofanypointinQandforwhichyis
distinctfromtheycoordinateofanypointinQ.LetQ0DQ[fxy/g.
segundo.Letjbetheminimumindexsuchthatyj<y,unlessy<yk,inwhichcase
weletjDkC1.ShowthatthemaximallayersofQ0areasfollows:
Ifjk,thenthemaximallayersofQ0arethesameasthemaximallayers
ofQ,exceptthatLjalsoincludes.xy/asitsnewleftmostpoint.
IfjDkC1,thenthefirstkmaximallayersofQ0arethesameasforQ,but
inaddition,Q0hasanonempty.kC1/stmaximallayer:LkC1Dfxy/g.

do.DescribeanO.nlgn/timealgorithmtocomputethemaximallayersofasetQ
ofnpoints.(Hint:Moveasweeplinefromrighttoleft.)

re.Doanydifficultiesariseifwenowallowinputpointstohavethesamexor
ycoordinate?Suggestawaytoresolvesuchproblems.

333Ghostbustersandghosts
AgroupofnGhostbustersisbattlingnghosts.EachGhostbustercarriesaproton
pack,whichshootsastreamataghost,eradicatingit.Astreamgoesinastraight
lineandterminateswhenithitstheghost.TheGhostbustersdecideuponthefol
lowingstrategy.Theywillpairoffwiththeghosts,formingnGhostbusterghost
pairs,andthensimultaneouslyeachGhostbusterwillshootastreamathischo
senghost.Asweallknow,itisverydangeroustoletstreamscross,andsothe
Ghostbustersmustchoosepairingsforwhichnostreamswillcross.
AssumethatthepositionofeachGhostbusterandeachghostisafixedpointin
theplaneandthatnothreepositionsarecolinear.
a.ArguethatthereexistsalinepassingthroughoneGhostbusterandoneghost
suchthatthenumberofGhostbustersononesideofthelineequalsthenumber
ofghostsonthesameside.DescribehowtofindsuchalineinO.nlgn/time.

Page167

https://translate.googleusercontent.com/translate_f 116/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,

1046 Chapter33ComputationalGeometry

segundo.GiveanOn2lgn/timealgorithmtopairGhostbusterswithghostsinsucha
waythatnostreamscross.

334Pickingupsticks
ProfessorCharonhasasetofnsticks,whicharepiledupinsomeconfiguration.
Eachstickisspecifiedbyitsendpoints,andeachendpointisanorderedtriple
givingits.xy/coordinates.Nostickisvertical.Hewishestopickupallthe
sticks,oneatatime,subjecttotheconditionthathemaypickupastickonlyif
thereisnootherstickontopofit.

a.Giveaprocedurethattakestwosticksaandbandreportswhetheraisabove,
below,orunrelatedtob.

segundo.Describeanefficientalgorithmthatdetermineswhetheritispossibletopickup
allthesticks,andifso,providesalegalorderinwhichtopickthemup.

335Sparsehulleddistributions
Considertheproblemofcomputingtheconvexhullofasetofpointsintheplane
thathavebeendrawnaccordingtosomeknownrandomdistribution.Aveces,
thenumberofpoints,orsize,oftheconvexhullofnpointsdrawnfromsucha
distributionhasexpectationOn1/forsomeconstant >0.Wecallsucha
distributionsparsehulled.Sparsehulleddistributionsincludethefollowing:
Pointsdrawnuniformlyfromaunitradiusdisk.Theconvexhullhasexpected
size.n1=3/.
Pointsdrawnuniformlyfromtheinteriorofaconvexpolygonwithksides,for
anyconstantk.Theconvexhullhasexpectedsize.lgn/.
Pointsdrawnaccordingtoatwodimensionalnormaldistribution.Theconvex
pag
hullhasexpectedsize. lgn/.

a.Giventwoconvexpolygonswithn1andn2verticesrespectively,showhowto
computetheconvexhullofalln1Cn2pointsinOn1Cn2/time.(Thepolygons
mayoverlap.)

segundo.Showhowtocomputetheconvexhullofasetofnpointsdrawnindependently
accordingtoasparsehulleddistributioninOn/averagecasetime.(Hint:
Recursivelyfindtheconvexhullsofthefirstn=2pointsandthesecondn=2
points,andthencombinetheresults.)

Pgina168

NotesforChapter33 1047

Chapternotes

Thischapterbarelyscratchesthesurfaceofcomputationalgeometryalgorithms
andtechniques.BooksoncomputationalgeometryincludethosebyPreparataand
Shamos[282],Edelsbrunner[99],andO'Rourke[269].
Althoughgeometryhasbeenstudiedsinceantiquity,thedevelopmentofalgo
rithmsforgeometricproblemsisrelativelynew.PreparataandShamosnotethat
theearliestnotionofthecomplexityofaproblemwasgivenbyE.Lemoinein1902.
Hewasstudyingeuclideanconstructionsthoseusingacompassandarulerand
devisedasetoffiveprimitives:placingonelegofthecompassonagivenpoint,
placingonelegofthecompassonagivenline,drawingacircle,passingtherulers
edgethroughagivenpoint,anddrawingaline.Lemoinewasinterestedinthe
numberofprimitivesneededtoeffectagivenconstructionhecalledthisamount
thesimplicityoftheconstruction.
ThealgorithmofSection33.2,whichdetermineswhetheranysegmentsinter
sect,isduetoShamosandHoey[313].
TheoriginalversionofGraham'sscanisgivenbyGraham[150].Thepackage

https://translate.googleusercontent.com/translate_f 117/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
wrappingalgorithmisduetoJarvis[189].Usingadecisiontreemodelofcom
putation,Yao[359]provedaworstcaselowerboundof.nlgn/fortherunning
timeofanyconvexhullalgorithm.Whenthenumberofverticeshofthecon
vexhullistakenintoaccount,thepruneandsearchalgorithmofKirkpatrickand
Seidel[206],whichtakesO.nlgh/time,isasymptoticallyoptimal.
TheO.nlgn/timedivideandconqueralgorithmforfindingtheclosestpairof
pointsisbyShamosandappearsinPreparataandShamos[282].Preparataand
Shamosalsoshowthatthealgorithmisasymptoticallyoptimalinadecisiontree
modelo.

Pgina169

34 NPCompleteness

Almostallthealgorithmswehavestudiedthusfarhavebeenpolynomialtimeal
gorithms:oninputsofsizen,theirworstcaserunningtimeisOnk/forsomecon
stantk.Youmightwonderwhetherallproblemscanbesolvedinpolynomialtime.
Theanswerisno.Forexample,thereareproblems,suchasTuring'sfamousHalt
ingProblem,thatcannotbesolvedbyanycomputer,nomatterhowmuchtimewe
allow.Therearealsoproblemsthatcanbesolved,butnotintimeOnk/forany
constantk.Generally,wethinkofproblemsthataresolvablebypolynomialtime
algorithmsasbeingtractable,oreasy,andproblemsthatrequiresuperpolynomial
timeasbeingintractable,orhard.
Thesubjectofthischapter,however,isaninterestingclassofproblems,called
theNPcompleteproblems,whosestatusisunknown.Nopolynomialtimeal
gorithmhasyetbeendiscoveredforanNPcompleteproblem,norhasanyoneyet
beenabletoprovethatnopolynomialtimealgorithmcanexistforanyoneofthem.
ThissocalledPNPquestionhasbeenoneofthedeepest,mostperplexingopen
researchproblemsintheoreticalcomputersciencesinceitwasfirstposedin1971.
SeveralNPcompleteproblemsareparticularlytantalizingbecausetheyseem
onthesurfacetobesimilartoproblemsthatweknowhowtosolveinpolynomial
hora.Ineachofthefollowingpairsofproblems,oneissolvableinpolynomial
timeandtheotherisNPcomplete,butthedifferencebetweenproblemsappearsto
beslight:
Shortestvs.longestsimplepaths:InChapter24,wesawthatevenwithnegative
edgeweights,wecanfindshortestpathsfromasinglesourceinadirected
graphGD.VE/inO.VE/time.Findingalongestsimplepathbetweentwo
verticesisdifficult,however.Merelydeterminingwhetheragraphcontainsa
simplepathwithatleastagivennumberofedgesisNPcomplete.
Eulertourvs.hamiltoniancycle:AnEulertourofaconnected,directedgraph
GD.VE/isacyclethattraverseseachedgeofGexactlyonce,although
itisallowedtovisiteachvertexmorethanonce.ByProblem223,wecan

https://translate.googleusercontent.com/translate_f 118/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
determinewhetheragraphhasanEulertourinonlyOE/timeand,infact,

Pgina170

Chapter34NPCompleteness 1049

wecanfindtheedgesoftheEulertourinOE/time.Ahamiltoniancycleof
adirectedgraphGD.VE/isasimplecyclethatcontainseachvertexinV.
DeterminingwhetheradirectedgraphhasahamiltoniancycleisNPcomplete.
(Laterinthischapter,weshallprovethatdeterminingwhetheranundirected
graphhasahamiltoniancycleisNPcomplete.)
2CNFsatisfiabilityvs.3CNFsatisfiability:Abooleanformulacontainsvari
ableswhosevaluesare0or1booleanconnectivessuchas^(AND),_(OR),
and:(NOT)andparentheses.Abooleanformulaissatisfiableifthereexists
someassignmentofthevalues0and1toitsvariablesthatcausesittoevaluate
to1.Weshalldefinetermsmoreformallylaterinthischapter,butinformally,a
booleanformulaisinkconjunctivenormalform,orkCNF,ifitistheAND
ofclausesofORsofexactlykvariablesortheirnegations.Porejemplo,el
booleanformula.x1_:x2/^.:x1_x3/^.:x2_:x3/isin2CNF.(Ithas
thesatisfyingassignmentx1D1x2D0x3D1.)Althoughwecandeter
mineinpolynomialtimewhethera2CNFformulaissatisfiable,weshallsee
laterinthischapterthatdeterminingwhethera3CNFformulaissatisfiableis
NPcomplete.

NPcompletenessandtheclassesPandNP
Throughoutthischapter,weshallrefertothreeclassesofproblems:P,NP,and
NPC,thelatterclassbeingtheNPcompleteproblems.Wedescribetheminfor
mallyhere,andweshalldefinethemmoreformallylateron.
TheclassPconsistsofthoseproblemsthataresolvableinpolynomialtime.
Morespecifically,theyareproblemsthatcanbesolvedintimeOnk/forsome
constantk,wherenisthesizeoftheinputtotheproblem.Mostoftheproblems
examinedinpreviouschaptersareinP.
TheclassNPconsistsofthoseproblemsthatareverifiableinpolynomialtime.
Whatdowemeanbyaproblembeingverifiable?Ifweweresomehowgivena
certificateofasolution,thenwecouldverifythatthecertificateiscorrectintime
polynomialinthesizeoftheinputtotheproblem.Forexample,inthehamiltonian
cycleproblem,givenadirectedgraphGD.VE/,acertificatewouldbease
quenceh123:::jVjiofjVjvertices.Wecouldeasilycheckinpolynomial
timethat.iiC1/2EforiD123:::jVj1andthat.jVj1/2Easwell.
Asanotherexample,for3CNFsatisfiability,acertificatewouldbeanassignment
ofvaluestovariables.Wecouldcheckinpolynomialtimethatthisassignment
satisfiesthebooleanformula.
AnyprobleminPisalsoinNP,sinceifaproblemisinPthenwecansolveit
inpolynomialtimewithoutevenbeingsuppliedacertificate.Weshallformalize
thisnotionlaterinthischapter,butfornowwecanbelievethatPNP.Laapertura
questioniswhetherornotPisapropersubsetofNP.

Pgina171

1050 Chapter34NPCompleteness

Informally,aproblemisintheclassNPCandwerefertoitasbeingNP
completeifitisinNPandisashardasanyprobleminNP.Weshallformally
definewhatitmeanstobeashardasanyprobleminNPlaterinthischapter.
Inthemeantime,wewillstatewithoutproofthatifanyNPcompleteproblem
canbesolvedinpolynomialtime,theneveryprobleminNPhasapolynomial
timealgorithm.MosttheoreticalcomputerscientistsbelievethattheNPcomplete
problemsareintractable,sincegiventhewiderangeofNPcompleteproblems
thathavebeenstudiedtodatewithoutanyonehavingdiscoveredapolynomial
timesolutiontoanyofthemitwouldbetrulyastoundingifallofthemcould
besolvedinpolynomialtime.Yet,giventheeffortdevotedthusfartoproving
thatNPcompleteproblemsareintractablewithoutaconclusiveoutcomewe
cannotruleoutthepossibilitythattheNPcompleteproblemsareinfactsolvable

https://translate.googleusercontent.com/translate_f 119/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
inpolynomialtime.
Tobecomeagoodalgorithmdesigner,youmustunderstandtherudimentsofthe
theoryofNPcompleteness.IfyoucanestablishaproblemasNPcomplete,you
providegoodevidenceforitsintractability.Asanengineer,youwouldthendo
bettertospendyourtimedevelopinganapproximationalgorithm(seeChapter35)
orsolvingatractablespecialcase,ratherthansearchingforafastalgorithmthat
solvestheproblemexactly.Moreover,manynaturalandinterestingproblemsthat
onthesurfaceseemnoharderthansorting,graphsearching,ornetworkfloware
infactNPcomplete.Therefore,youshouldbecomefamiliarwiththisremarkable
classofproblems.

OverviewofshowingproblemstobeNPcomplete
ThetechniquesweusetoshowthataparticularproblemisNPcompletediffer
fundamentallyfromthetechniquesusedthroughoutmostofthisbooktodesign
andanalyzealgorithms.WhenwedemonstratethataproblemisNPcomplete,
wearemakingastatementabouthowharditis(oratleasthowhardwethinkit
is),ratherthanabouthoweasyitis.Wearenottryingtoprovetheexistenceof
anefficientalgorithm,butinsteadthatnoefficientalgorithmislikelytoexist.En
thisway,NPcompletenessproofsbearsomesimilaritytotheproofinSection8.1
ofan.nlgn/timelowerboundforanycomparisonsortalgorithmthespecific
techniquesusedforshowingNPcompletenessdifferfromthedecisiontreemethod
usedinSection8.1,however.
WerelyonthreekeyconceptsinshowingaproblemtobeNPcomplete:

Decisionproblemsvs.optimizationproblems
Manyproblemsofinterestareoptimizationproblems,inwhicheachfeasible(ie,
legal)solutionhasanassociatedvalue,andwewishtofindafeasiblesolution
withthebestvalue.Forexample,inaproblemthatwecallSHORTESTPATH,

Pgina172

Chapter34NPCompleteness 1051

wearegivenanundirectedgraphGandverticesuand,andwewishtofinda
pathfromutothatusesthefewestedges.Inotherwords,SHORTESTPATH
isthesinglepairshortestpathprobleminanunweighted,undirectedgraph.NP
completenessappliesdirectlynottooptimizationproblems,however,buttodeci
sionproblems,inwhichtheanswerissimplyyesorno(or,moreformally,1
or0).
AlthoughNPcompleteproblemsareconfinedtotherealmofdecisionproblems,
wecantakeadvantageofaconvenientrelationshipbetweenoptimizationproblems
anddecisionproblems.Weusuallycancastagivenoptimizationproblemasare
lateddecisionproblembyimposingaboundonthevaluetobeoptimized.por
example,adecisionproblemrelatedtoSHORTESTPATHisPATH:givenadi
rectedgraphG,verticesuand,andanintegerk,doesapathexistfromuto
consistingofatmostkedges?
Therelationshipbetweenanoptimizationproblemanditsrelateddecisionprob
lemworksinourfavorwhenwetrytoshowthattheoptimizationproblemis
hard.Thatisbecausethedecisionproblemisinasenseeasier,oratleastno
harder.Asaspecificexample,wecansolvePATHbysolvingSHORTESTPATH
andthencomparingthenumberofedgesintheshortestpathfoundtothevalue
ofthedecisionproblemparameterk.Inotherwords,ifanoptimizationprob
lemiseasy,itsrelateddecisionproblemiseasyaswell.Statedinawaythathas
morerelevancetoNPcompleteness,ifwecanprovideevidencethatadecision
problemishard,wealsoprovideevidencethatitsrelatedoptimizationproblemis
hard.Thus,eventhoughitrestrictsattentiontodecisionproblems,thetheoryof
NPcompletenessoftenhasimplicationsforoptimizationproblemsaswell.

Reductions
Theabovenotionofshowingthatoneproblemisnoharderornoeasierthanan
otherappliesevenwhenbothproblemsaredecisionproblems.Wetakeadvantage
ofthisideainalmosteveryNPcompletenessproof,asfollows.Letusconsidera
decisionproblemA,whichwewouldliketosolveinpolynomialtime.Wecallthe
inputtoaparticularproblemaninstanceofthatproblemforexample,inPATH,
aninstancewouldbeaparticulargraphG,particularverticesuandofG,anda
particularintegerk.Nowsupposethatwealreadyknowhowtosolveadifferent

https://translate.googleusercontent.com/translate_f 120/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
decisionproblemBinpolynomialtime.Finally,supposethatwehaveaprocedure
thattransformsanyinstanceofAintosomeinstanceofBwiththefollowing
Caractersticas:
Thetransformationtakespolynomialtime.
Theanswersarethesame.Thatis,theanswerforisyesifandonlyifthe
answerforisalsoyes.

Pgina173

1052 Chapter34NPCompleteness

instance polynomialtime instance polynomialtime s s


ofA reductionalgorithm ofB algorithmtodecideBno no
polynomialtimealgorithmtodecideA

Figure34.1HowtouseapolynomialtimereductionalgorithmtosolveadecisionproblemAin
polynomialtime,givenapolynomialtimedecisionalgorithmforanotherproblemB.Inpolynomial
time,wetransformaninstanceofAintoaninstanceofB,wesolveBinpolynomialtime,and
weusetheanswerforastheanswerfor.

Wecallsuchaprocedureapolynomialtimereductionalgorithmand,asFig
ure34.1shows,itprovidesusawaytosolveproblemAinpolynomialtime:

1.GivenaninstanceofproblemA,useapolynomialtimereductionalgorithm
totransformittoaninstanceofproblemB.
2.RunthepolynomialtimedecisionalgorithmforBontheinstance.
3.Usetheanswerforastheanswerfor.
Aslongaseachofthesestepstakespolynomialtime,allthreetogetherdoalso,and
sowehaveawaytodecideoninpolynomialtime.Inotherwords,byreducing
solvingproblemAtosolvingproblemB,weusetheeasinessofBtoprovethe
easinessofA.
RecallingthatNPcompletenessisaboutshowinghowhardaproblemisrather
thanhoweasyitis,weusepolynomialtimereductionsintheoppositewaytoshow
thataproblemisNPcomplete.Letustaketheideaastepfurther,andshowhowwe
couldusepolynomialtimereductionstoshowthatnopolynomialtimealgorithm
canexistforaparticularproblemB.SupposewehaveadecisionproblemAfor
whichwealreadyknowthatnopolynomialtimealgorithmcanexist.(Letusnot
concernourselvesfornowwithhowtofindsuchaproblemA.)Supposefurther
thatwehaveapolynomialtimereductiontransforminginstancesofAtoinstances
ofB.Nowwecanuseasimpleproofbycontradictiontoshowthatnopolynomial
timealgorithmcanexistforB.Supposeotherwiseie,supposethatBhasa
polynomialtimealgorithm.Then,usingthemethodshowninFigure34.1,we
wouldhaveawaytosolveproblemAinpolynomialtime,whichcontradictsour
assumptionthatthereisnopolynomialtimealgorithmforA.
ForNPcompleteness,wecannotassumethatthereisabsolutelynopolynomial
timealgorithmforproblemA.Theproofmethodologyissimilar,however,inthat
weprovethatproblemBisNPcompleteontheassumptionthatproblemAisalso
NPcomplete.

Pgina174

34.1Polynomialtime 1053

AfirstNPcompleteproblem
Becausethetechniqueofreductionreliesonhavingaproblemalreadyknownto
beNPcompleteinordertoproveadifferentproblemNPcomplete,weneeda
firstNPcompleteproblem.Theproblemweshalluseisthecircuitsatisfiability
problem,inwhichwearegivenabooleancombinationalcircuitcomposedofAND,

https://translate.googleusercontent.com/translate_f 121/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
OR,andNOTgates,andwewishtoknowwhetherthereexistssomesetofboolean
inputstothiscircuitthatcausesitsoutputtobe1.Weshallprovethatthisfirst
problemisNPcompleteinSection34.3.

Chapteroutline
ThischapterstudiestheaspectsofNPcompletenessthatbearmostdirectlyonthe
analysisofalgorithms.InSection34.1,weformalizeournotionofproblemand
definethecomplexityclassPofpolynomialtimesolvabledecisionproblems.Nosotros
alsoseehowthesenotionsfitintotheframeworkofformallanguagetheory.Sec
tion34.2definestheclassNPofdecisionproblemswhosesolutionsareverifiable
inpolynomialtime.ItalsoformallyposesthePNPquestion.
Section34.3showswecanrelateproblemsviapolynomialtimereductions.
ItdefinesNPcompletenessandsketchesaproofthatoneproblem,calledcircuit
satisfiability,isNPcomplete.HavingfoundoneNPcompleteproblem,weshow
inSection34.4howtoproveotherproblemstobeNPcompletemuchmoresimply
bythemethodologyofreductions.Weillustratethismethodologybyshowingthat
twoformulasatisfiabilityproblemsareNPcomplete.Withadditionalreductions,
weshowinSection34.5avarietyofotherproblemstobeNPcomplete.

34.1Polynomialtime

WebeginourstudyofNPcompletenessbyformalizingournotionofpolynomial
timesolvableproblems.Wegenerallyregardtheseproblemsastractable,butfor
philosophical,notmathematical,reasons.Wecanofferthreesupportingargu
Mentos.
First,althoughwemayreasonablyregardaproblemthatrequirestime.n100/
tobeintractable,veryfewpracticalproblemsrequiretimeontheorderofsucha
highdegreepolynomial.Thepolynomialtimecomputableproblemsencountered
inpracticetypicallyrequiremuchlesstime.Experiencehasshownthatoncethe
firstpolynomialtimealgorithmforaproblemhasbeendiscovered,moreefficient
algorithmsoftenfollow.Evenifthecurrentbestalgorithmforaproblemhasa
runningtimeof.n100/,analgorithmwithamuchbetterrunningtimewilllikely
soonbediscovered.

Pgina175

1054 Chapter34NPCompleteness

Second,formanyreasonablemodelsofcomputation,aproblemthatcanbe
solvedinpolynomialtimeinonemodelcanbesolvedinpolynomialtimeinan
other.Forexample,theclassofproblemssolvableinpolynomialtimebytheserial
randomaccessmachineusedthroughoutmostofthisbookisthesameastheclass
ofproblemssolvableinpolynomialtimeonabstractTuringmachines.1Itisalso
thesameastheclassofproblemssolvableinpolynomialtimeonaparallelcom
puterwhenthenumberofprocessorsgrowspolynomiallywiththeinputsize.
Third,theclassofpolynomialtimesolvableproblemshasniceclosureproper
ties,sincepolynomialsareclosedunderaddition,multiplication,andcomposition.
Forexample,iftheoutputofonepolynomialtimealgorithmisfedintotheinputof
another,thecompositealgorithmispolynomial.Exercise34.15asksyoutoshow
thatifanalgorithmmakesaconstantnumberofcallstopolynomialtimesubrou
tinesandperformsanadditionalamountofworkthatalsotakespolynomialtime,
thentherunningtimeofthecompositealgorithmispolynomial.

Abstractproblems

Tounderstandtheclassofpolynomialtimesolvableproblems,wemustfirsthave
aformalnotionofwhataproblemis.WedefineanabstractproblemQtobea
binaryrelationonasetIofprobleminstancesandasetSofproblemsolutions.
Forexample,aninstanceforSHORTESTPATHisatripleconsistingofagraph
andtwovertices.Asolutionisasequenceofverticesinthegraph,withperhaps
theemptysequencedenotingthatnopathexists.TheproblemSHORTESTPATH
itselfistherelationthatassociateseachinstanceofagraphandtwoverticeswith
ashortestpathinthegraphthatconnectsthetwovertices.Sinceshortestpathsare
notnecessarilyunique,agivenprobleminstancemayhavemorethanonesolution.
Thisformulationofanabstractproblemismoregeneralthanweneedforour

https://translate.googleusercontent.com/translate_f 122/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
Lospropsitos.Aswesawabove,thetheoryofNPcompletenessrestrictsattentionto
decisionproblems:thosehavingayes/nosolution.Inthiscase,wecanviewan
abstractdecisionproblemasafunctionthatmapstheinstancesetItothesolution
setf01g.Forexample,adecisionproblemrelatedtoSHORTESTPATHisthe
problemPATHthatwesawearlier.IfiDhGukiisaninstanceofthedecision
problemPATH,thenPATH.i/D1(yes)ifashortestpathfromuto hasat
mostkedges,andPATH.i/D0(no)otherwise.Manyabstractproblemsarenot
decisionproblems,butratheroptimizationproblems,whichrequiresomevalueto
beminimizedormaximized.Aswesawabove,however,wecanusuallyrecastan
optimizationproblemasadecisionproblemthatisnoharder.

1SeeHopcroftandUllman[180]orLewisandPapadimitriou[236]forathoroughtreatmentofthe
Turingmachinemodel.

Pgina176

34.1Polynomialtime 1055

Encodings
Inorderforacomputerprogramtosolveanabstractproblem,wemustrepresent
probleminstancesinawaythattheprogramunderstands.AnencodingofasetS
ofabstractobjectsisamappingefromStothesetofbinarystrings.2Forexample,
weareallfamiliarwithencodingthenaturalnumbersNDf01234:::gas
thestringsf011011100:::g.Usingthisencoding,e.17/D10001.Ifyou
havelookedatcomputerrepresentationsofkeyboardcharacters,youprobablyhave
seentheASCIIcode,where,forexample,theencodingofAis1000001.Wecan
encodeacompoundobjectasabinarystringbycombiningtherepresentationsof
itsconstituentparts.Polygons,graphs,functions,orderedpairs,programsallcan
beencodedasbinarystrings.
Thus,acomputeralgorithmthatsolvessomeabstractdecisionproblemactu
allytakesanencodingofaprobleminstanceasinput.Wecallaproblemwhose
instancesetisthesetofbinarystringsaconcreteproblem.Wesaythatanalgo
rithmsolvesaconcreteproblemintimeOT.n//if,whenitisprovidedaproblem
instanceioflengthnDjij,thealgorithmcanproducethesolutioninOT.n//
hora.3Aconcreteproblemispolynomialtimesolvable,therefore,ifthereexists
analgorithmtosolveitintimeOnk/forsomeconstantk.
WecannowformallydefinethecomplexityclassPasthesetofconcretedeci
sionproblemsthatarepolynomialtimesolvable.
Wecanuseencodingstomapabstractproblemstoconcreteproblems.Dado
anabstractdecisionproblemQmappinganinstancesetItof01g,anencoding
eWI!f01gcaninducearelatedconcretedecisionproblem,whichwedenote
byeQ/.4Ifthesolutiontoanabstractprobleminstancei2IisQi/2f01g,
thenthesolutiontotheconcreteprobleminstanceei/2f01gisalsoQi/.Comoun
technicality,somebinarystringsmightrepresentnomeaningfulabstractproblem
ejemplo.Forconvenience,weshallassumethatanysuchstringmapsarbitrarily
to0.Thus,theconcreteproblemproducesthesamesolutionsastheabstractprob
lemonbinarystringinstancesthatrepresenttheencodingsofabstractproblem
instancias.
Wewouldliketoextendthedefinitionofpolynomialtimesolvabilityfromcon
creteproblemstoabstractproblemsbyusingencodingsasthebridge,butwewould

2Thecodomainofeneednotbebinarystringsanysetofstringsoverafinitealphabethavingat
least2symbolswilldo.
3Weassumethatthealgorithm'soutputisseparatefromitsinput.Becauseittakesatleastonetime
steptoproduceeachbitoftheoutputandthealgorithmtakesOT.n//timesteps,thesizeofthe
outputisOT.n//.
4Wedenotebyf01gthesetofallstringscomposedofsymbolsfromthesetf01g.

Pgina177

1056 Chapter34NPCompleteness

https://translate.googleusercontent.com/translate_f 123/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,

likethedefinitiontobeindependentofanyparticularencoding.Thatis,theef
ficiencyofsolvingaproblemshouldnotdependonhowtheproblemisencoded.
Unfortunately,itdependsquiteheavilyontheencoding.Forexample,supposethat
anintegerkistobeprovidedasthesoleinputtoanalgorithm,andsupposethat
therunningtimeofthealgorithmis.k/.Iftheintegerkisprovidedinunarya
stringofk1sthentherunningtimeofthealgorithmisOn/onlengthninputs,
whichispolynomialtime.Ifweusethemorenaturalbinaryrepresentationofthe
integerk,however,thentheinputlengthisnDblgkcC1.Inthiscase,therun
ningtimeofthealgorithmis.k/D.2n/,whichisexponentialinthesizeofthe
entrada.Thus,dependingontheencoding,thealgorithmrunsineitherpolynomial
orsuperpolynomialtime.
Howweencodeanabstractproblemmattersquiteabittohowweunderstand
polynomialtime.Wecannotreallytalkaboutsolvinganabstractproblemwithout
firstspecifyinganencoding.Nevertheless,inpractice,ifweruleoutexpensive
encodingssuchasunaryones,theactualencodingofaproblemmakeslittledif
ferencetowhethertheproblemcanbesolvedinpolynomialtime.Porejemplo,
representingintegersinbase3insteadofbinaryhasnoeffectonwhetheraprob
lemissolvableinpolynomialtime,sincewecanconvertanintegerrepresentedin
base3toanintegerrepresentedinbase2inpolynomialtime.
WesaythatafunctionfWf01g!f01gispolynomialtimecomputable
ifthereexistsapolynomialtimealgorithmAthat,givenanyinputx2f01g,
producesasoutputf.x/.ForsomesetIofprobleminstances,wesaythattwoen
codingse1ande2arepolynomiallyrelatedifthereexisttwopolynomialtimecom
putablefunctionsf12andf21suchthatforanyi2I,wehavef12.e1.i//De2.i/
andf21.e2.i//De1.i/.5Thatis,apolynomialtimealgorithmcancomputetheen
codinge2.i/fromtheencodinge1.i/,andviceversa.Iftwoencodingse1ande2of
anabstractproblemarepolynomiallyrelated,whethertheproblemispolynomial
timesolvableornotisindependentofwhichencodingweuse,asthefollowing
lemmashows.

Lemma34.1
LetQbeanabstractdecisionproblemonaninstancesetI,andlete1ande2be
polynomiallyrelatedencodingsonI.Then,e1.Q/2Pifandonlyife2.Q/2P.

5Technically,wealsorequirethefunctionsf12andf21tomapnoninstancestononinstances.
Anoninstanceofanencodingeisastringx2f01gsuchthatthereisnoinstanceiforwhich
ei/Dx.Werequirethatf12.x/Dyforeverynoninstancexofencodinge1,whereyissomenon
instanceofe2,andthatf21.x0/Dy0foreverynoninstancex0ofe2,wherey0issomenoninstance
ofe1.

Pgina178

34.1Polynomialtime 1057

ProofWeneedonlyprovetheforwarddirection,sincethebackwarddirectionis
symmetric.Suppose,therefore,thate1.Q/canbesolvedintimeOnk/forsome
constantk.Further,supposethatforanyprobleminstancei,theencodinge1.i/
canbecomputedfromtheencodinge2.i/intimeOnc/forsomeconstantc,where
nDje2.i/j.Tosolveprobleme2.Q/,oninpute2.i/,wefirstcomputee1.i/and
thenrunthealgorithmfore1.Q/one1.i/.Howlongdoesthistake?Mudado
encodingstakestimeOnc/,andthereforeje1.i/jDOnc/,sincetheoutputof
aserialcomputercannotbelongerthanitsrunningtime.Resolviendoelproblema
one1.i/takestimeO.je1.i/jk/DOnck/,whichispolynomialsincebothcandk
areconstants.

Thus,whetheranabstractproblemhasitsinstancesencodedinbinaryorbase3
doesnotaffectitscomplexity,thatis,whetheritispolynomialtimesolvableor
notbutifinstancesareencodedinunary,itscomplexitymaychange.Afindeque
beabletoconverseinanencodingindependentfashion,weshallgenerallyassume
thatprobleminstancesareencodedinanyreasonable,concisefashion,unlesswe
specificallysayotherwise.Tobeprecise,weshallassumethattheencodingofan
integerispolynomiallyrelatedtoitsbinaryrepresentation,andthattheencodingof
afinitesetispolynomiallyrelatedtoitsencodingasalistofitselements,enclosed
inbracesandseparatedbycommas.(ASCIIisonesuchencodingscheme.)With

https://translate.googleusercontent.com/translate_f 124/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
suchastandardencodinginhand,wecanderivereasonableencodingsofother
mathematicalobjects,suchastuples,graphs,andformulas.Todenotethestandard
encodingofanobject,weshallenclosetheobjectinanglebraces.Thus,hGi
denotesthestandardencodingofagraphG.
Aslongasweimplicitlyuseanencodingthatispolynomiallyrelatedtothis
standardencoding,wecantalkdirectlyaboutabstractproblemswithoutreference
toanyparticularencoding,knowingthatthechoiceofencodinghasnoeffecton
whethertheabstractproblemispolynomialtimesolvable.Henceforth,weshall
generallyassumethatallprobleminstancesarebinarystringsencodedusingthe
standardencoding,unlessweexplicitlyspecifythecontrary.Weshallalsotypically
neglectthedistinctionbetweenabstractandconcreteproblems.Youshouldwatch
outforproblemsthatariseinpractice,however,inwhichastandardencodingis
notobviousandtheencodingdoesmakeadifference.

Aformallanguageframework
Byfocusingondecisionproblems,wecantakeadvantageofthemachineryof
formallanguagetheory.Let'sreviewsomedefinitionsfromthattheory.Un
alphabetisafinitesetofsymbols.AlanguageLoverisanysetof
stringsmadeupofsymbolsfrom.Forexample,ifDf01g,theset
LDf10111011111011110110001:::gisthelanguageofbinaryrepresen

Pgina179

1058 Chapter34NPCompleteness

tationsofprimenumbers.Wedenotetheemptystringby",theemptylanguage
by,andthelanguageofallstringsoverby.Forexample,ifDf01g,
thenDf"0100011011000:::gisthesetofallbinarystrings.Every
languageLoverisasubsetof.
Wecanperformavarietyofoperationsonlanguages.Settheoreticoperations,
suchasunionandintersection,followdirectlyfromthesettheoreticdefinitions.
WedefinethecomplementofLbyLD L.TheconcatenationL1L2oftwo
languagesL1andL2isthelanguage
LDfx1x2Wx12L1andx22L2g:
TheclosureorKleenestarofalanguageListhelanguage
LDf"g[L[L2[L3[
whereLkisthelanguageobtainedbyconcatenatingLtoitselfktimes.
Fromthepointofviewoflanguagetheory,thesetofinstancesforanydecision
problemQissimplytheset,whereDf01g.SinceQisentirelycharacter
izedbythoseprobleminstancesthatproducea1(yes)answer,wecanviewQas
alanguageLoverDf01g,where
LDfx2WQx/D1g:
Forexample,thedecisionproblemPATHhasthecorrespondinglanguage
PATHDfhGukiWGD.VE/isanundirectedgraph,
u2V
K 0isaninteger,and
thereexistsapathfromutoinG
consistingofatmostkedgesg:
(Whereconvenient,weshallsometimesusethesamenamePATHinthiscase
torefertobothadecisionproblemanditscorrespondinglanguage.)
Theformallanguageframeworkallowsustoexpressconciselytherelationbe
tweendecisionproblemsandalgorithmsthatsolvethem.Wesaythatanal
gorithmAacceptsastringx2f01gif,giveninputx,thealgorithm'sout
putAx/is1.ThelanguageacceptedbyanalgorithmAisthesetofstrings
LDfx2f01gWAx/D1g,thatis,thesetofstringsthatthealgorithmaccepts.
AnalgorithmArejectsastringxifAx/D0.
EveniflanguageLisacceptedbyanalgorithmA,thealgorithmwillnotneces
sarilyrejectastringx62Lprovidedasinputtoit.Forexample,thealgorithmmay
loopforever.AlanguageLisdecidedbyanalgorithmAifeverybinarystring
inLisacceptedbyAandeverybinarystringnotinLisrejectedbyA.Alan
guageLisacceptedinpolynomialtimebyanalgorithmAifitisacceptedbyA
andifinadditionthereexistsaconstantksuchthatforanylengthnstringx2L,

https://translate.googleusercontent.com/translate_f 125/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,

Pgina180

34.1Polynomialtime 1059

algorithmAacceptsxintimeOnk/.AlanguageLisdecidedinpolynomial
timebyanalgorithmAifthereexistsaconstantksuchthatforanylengthnstring
x2f01g,thealgorithmcorrectlydecideswhetherx2LintimeOnk/.Thus,
toacceptalanguage,analgorithmneedonlyproduceananswerwhenprovideda
stringinL,buttodecidealanguage,itmustcorrectlyacceptorrejecteverystring
inf01g.
Asanexample,thelanguagePATHcanbeacceptedinpolynomialtime.Uno
polynomialtimeacceptingalgorithmverifiesthatGencodesanundirectedgraph,
verifiesthatuandareverticesinG,usesbreadthfirstsearchtocomputeashort
estpathfromutoinG,andthencomparesthenumberofedgesontheshortest
pathobtainedwithk.IfGencodesanundirectedgraphandthepathfoundfromu
tohasatmostkedges,thealgorithmoutputs1andhalts.Otherwise,thealgo
rithmrunsforever.ThisalgorithmdoesnotdecidePATH,however,sinceitdoes
notexplicitlyoutput0forinstancesinwhichashortestpathhasmorethankedges.
AdecisionalgorithmforPATHmustexplicitlyrejectbinarystringsthatdonotbe
longtoPATH.ForadecisionproblemsuchasPATH,suchadecisionalgorithmis
easytodesign:insteadofrunningforeverwhenthereisnotapathfromutowith
atmostkedges,itoutputs0andhalts.(Itmustalsooutput0andhaltiftheinput
encodingisfaulty.)Forotherproblems,suchasTuring'sHaltingProblem,there
existsanacceptingalgorithm,butnodecisionalgorithmexists.
Wecaninformallydefineacomplexityclassasasetoflanguages,membership
inwhichisdeterminedbyacomplexitymeasure,suchasrunningtime,ofan
algorithmthatdetermineswhetheragivenstringxbelongstolanguageL.The
actualdefinitionofacomplexityclassissomewhatmoretechnical.6
Usingthislanguagetheoreticframework,wecanprovideanalternativedefini
tionofthecomplexityclassP:
PDfLf01gWthereexistsanalgorithmAthatdecidesL
inpolynomialtimeg:
Infact,Pisalsotheclassoflanguagesthatcanbeacceptedinpolynomialtime.

Theorem34.2
PDfLWLisacceptedbyapolynomialtimealgorithmg:

ProofBecausetheclassoflanguagesdecidedbypolynomialtimealgorithmsis
asubsetoftheclassoflanguagesacceptedbypolynomialtimealgorithms,we
needonlyshowthatifLisacceptedbyapolynomialtimealgorithm,itisde
cidedbyapolynomialtimealgorithm.LetLbethelanguageacceptedbysome

6Formoreoncomplexityclasses,seetheseminalpaperbyHartmanisandStearns[162].

Pgina181

1060 Chapter34NPCompleteness

polynomialtimealgorithmA.Weshalluseaclassicsimulationargumentto
constructanotherpolynomialtimealgorithmA0thatdecidesL.BecauseAac
ceptsLintimeOnk/forsomeconstantk,therealsoexistsaconstantcsuch
thatAacceptsLinatmostcnksteps.Foranyinputstringx,thealgorithmA0
simulatescnkstepsofA.Aftersimulatingcnksteps,algorithmA0inspectsthebe
haviorofA.IfAhasacceptedx,thenA0acceptsxbyoutputtinga1.IfAhasnot
acceptedx,thenA0rejectsxbyoutputtinga0.TheoverheadofA0simulatingA
doesnotincreasetherunningtimebymorethanapolynomialfactor,andthusA0
isapolynomialtimealgorithmthatdecidesL.

NotethattheproofofTheorem34.2isnonconstructive.Foragivenlanguage
L2P,wemaynotactuallyknowaboundontherunningtimeforthealgorithmA
thatacceptsL.Nevertheless,weknowthatsuchaboundexists,andtherefore,that
analgorithmA0existsthatcancheckthebound,eventhoughwemaynotbeable
https://translate.googleusercontent.com/translate_f 126/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
tofindthealgorithmA0easily.

Exercises

34.11
DefinetheoptimizationproblemLONGESTPATHLENGTHastherelationthat
associateseachinstanceofanundirectedgraphandtwoverticeswiththenum
berofedgesinalongestsimplepathbetweenthetwovertices.Definethede
cisionproblemLONGESTPATHDfhGukiWGD.VE/isanundi
rectedgraph,u2V,k 0isaninteger,andthereexistsasimplepath
fromutoinGconsistingofatleastkedgesg.Showthattheoptimizationprob
lemLONGESTPATHLENGTHcanbesolvedinpolynomialtimeifandonlyif
LONGESTPATH2P.

34.12
Giveaformaldefinitionfortheproblemoffindingthelongestsimplecycleinan
undirectedgraph.Givearelateddecisionproblem.Givethelanguagecorrespond
ingtothedecisionproblem.

34.13
Giveaformalencodingofdirectedgraphsasbinarystringsusinganadjacency
matrixrepresentation.Dothesameusinganadjacencylistrepresentation.Argue
thatthetworepresentationsarepolynomiallyrelated.

34.14
Isthedynamicprogrammingalgorithmforthe01knapsackproblemthatisasked
forinExercise16.22apolynomialtimealgorithm?Explicaturespuesta.

Pgina182

34.2Polynomialtimeverification 1061

34.15
Showthatifanalgorithmmakesatmostaconstantnumberofcallstopolynomial
timesubroutinesandperformsanadditionalamountofworkthatalsotakespolyno
mialtime,thenitrunsinpolynomialtime.Alsoshowthatapolynomialnumberof
callstopolynomialtimesubroutinesmayresultinanexponentialtimealgorithm.

34.16
ShowthattheclassP,viewedasasetoflanguages,isclosedunderunion,inter
section,concatenation,complement,andKleenestar.Thatis,ifL1L22P,then
L1[L22P,L1\L22P,L1L22P,L12P,andL12P.

34.2Polynomialtimeverification

Wenowlookatalgorithmsthatverifymembershipinlanguages.Porejemplo,
supposethatforagiveninstancehGukiofthedecisionproblemPATH,we
arealsogivenapathpfromuto.WecaneasilycheckwhetherpisapathinG
andwhetherthelengthofpisatmostk,andifso,wecanviewpasacertificate
thattheinstanceindeedbelongstoPATH.ForthedecisionproblemPATH,this
certificatedoesn'tseemtobuyusmuch.Afterall,PATHbelongstoPinfact,
wecansolvePATHinlineartimeandsoverifyingmembershipfromagiven
certificatetakesaslongassolvingtheproblemfromscratch.Weshallnowexamine
aproblemforwhichweknowofnopolynomialtimedecisionalgorithmandyet,
givenacertificate,verificationiseasy.

Hamiltoniancycles
Theproblemoffindingahamiltoniancycleinanundirectedgraphhasbeenstud
iedforoverahundredyears.Formally,ahamiltoniancycleofanundirectedgraph
GD.VE/isasimplecyclethatcontainseachvertexinV.Agraphthatcon
tainsahamiltoniancycleissaidtobehamiltonianotherwise,itisnonhamilto
nian.ThenamehonorsWRHamilton,whodescribedamathematicalgameon
thedodecahedron(Figure34.2(a))inwhichoneplayersticksfivepinsinanyfive
consecutiveverticesandtheotherplayermustcompletethepathtoformacycle

https://translate.googleusercontent.com/translate_f 127/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,

Pgina183

1062 Chapter34NPCompleteness

(un) (segundo)

Figure34.2(a)Agraphrepresentingthevertices,edges,andfacesofadodecahedron,witha
hamiltoniancycleshownbyshadededges.(b)Abipartitegraphwithanoddnumberofvertices.
Anysuchgraphisnonhamiltonian.

containingallthevertices.7Thedodecahedronishamiltonian,andFigure34.2(a)
showsonehamiltoniancycle.Notallgraphsarehamiltonian,however.Forex
ample,Figure34.2(b)showsabipartitegraphwithanoddnumberofvertices.
Exercise34.22asksyoutoshowthatallsuchgraphsarenonhamiltonian.
Wecandefinethehamiltoniancycleproblem,DoesagraphGhaveahamil
toniancycle?asaformallanguage:
HAMCYCLEDfhGiWGisahamiltoniangraphg:

HowmightanalgorithmdecidethelanguageHAMCYCLE?Givenaproblem
instancehGi,onepossibledecisionalgorithmlistsallpermutationsofthevertices
ofGandthencheckseachpermutationtoseeifitisahamiltonianpath.Whatis
therunningtimeofthisalgorithm?Ifweusethereasonableencodingofagraph
pag
asitsadjacencymatrix,thenumbermofverticesinthegraphis. n/,where
nDjhGijisthelengthoftheencodingofG.Therearempossiblepermutations

7Inaletterdated17October1856tohisfriendJohnT.Graves,Hamilton[157,p.624]wrote,I
havefoundthatsomeyoungpersonshavebeenmuchamusedbytryinganewmathematicalgame
whichtheIcosionfurnishes,onepersonstickingfivepinsinanyfiveconsecutivepoints...andthe
otherplayerthenaimingtoinsert,whichbythetheoryinthislettercanalwaysbedone,fifteenother
pins,incyclicalsuccession,soastocoveralltheotherpoints,andtoendinimmediateproximityto
thepinwherewithhisantagonisthadbegun.

Page184

34.2Polynomialtimeverification 1063

pag pag
ofthevertices,andthereforetherunningtimeis.m/D. n/D.2 n/,
whichisnotOnk/foranyconstantk.Thus,thisnaivealgorithmdoesnotrun
inpolynomialtime.Infact,thehamiltoniancycleproblemisNPcomplete,aswe
shallproveinSection34.5.

Verificationalgorithms

https://translate.googleusercontent.com/translate_f 128/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
Consideraslightlyeasierproblem.Supposethatafriendtellsyouthatagiven
graphGishamiltonian,andthenofferstoproveitbygivingyoutheverticesin
orderalongthehamiltoniancycle.Itwouldcertainlybeeasyenoughtoverifythe
proof:simplyverifythattheprovidedcycleishamiltonianbycheckingwhether
itisapermutationoftheverticesofVandwhethereachoftheconsecutiveedges
alongthecycleactuallyexistsinthegraph.Youcouldcertainlyimplementthis
verificationalgorithmtoruninOn2/time,wherenisthelengthoftheencoding
ofG.Thus,aproofthatahamiltoniancycleexistsinagraphcanbeverifiedin
polynomialtime.
WedefineaverificationalgorithmasbeingatwoargumentalgorithmA,where
oneargumentisanordinaryinputstringxandtheotherisabinarystringycalled
acertificate.AtwoargumentalgorithmAverifiesaninputstringxifthereexists
acertificateysuchthatAxy/D1.Thelanguageverifiedbyaverification
algorithmAis
LDfx2f01gWthereexistsy2f01gsuchthatAxy/D1g:

Intuitively,analgorithmAverifiesalanguageLifforanystringx2L,there
existsacertificateythatAcanusetoprovethatx2L.Moreover,foranystring
x62L,theremustbenocertificateprovingthatx2L.Forexample,inthe
hamiltoniancycleproblem,thecertificateisthelistofverticesinsomehamilto
niancycle.Ifagraphishamiltonian,thehamiltoniancycleitselfoffersenough
informationtoverifythisfact.Conversely,ifagraphisnothamiltonian,there
canbenolistofverticesthatfoolstheverificationalgorithmintobelievingthatthe
graphishamiltonian,sincetheverificationalgorithmcarefullycheckstheproposed
cycletobesure.

Pgina185

1064 Chapter34NPCompleteness

ThecomplexityclassNP
ThecomplexityclassNPistheclassoflanguagesthatcanbeverifiedbyapoly
nomialtimealgorithm.8Moreprecisely,alanguageLbelongstoNPifandonlyif
thereexistatwoinputpolynomialtimealgorithmAandaconstantcsuchthat
LDfx2f01gWthereexistsacertificateywithjyjDO.jxjc/
suchthatAxy/D1g:
WesaythatalgorithmAverifieslanguageLinpolynomialtime.
Fromourearlierdiscussiononthehamiltoniancycleproblem,wenowseethat
HAMCYCLE2NP.(Itisalwaysnicetoknowthatanimportantsetisnonempty.)
Moreover,ifL2P,thenL2NP,sinceifthereisapolynomialtimealgorithm
todecideL,thealgorithmcanbeeasilyconvertedtoatwoargumentverification
algorithmthatsimplyignoresanycertificateandacceptsexactlythoseinputstrings
itdeterminestobeinL.Thus,PNP.
ItisunknownwhetherPDNP,butmostresearchersbelievethatPandNPare
notthesameclass.Intuitively,theclassPconsistsofproblemsthatcanbesolved
conrapidez.TheclassNPconsistsofproblemsforwhichasolutioncanbeverified
conrapidez.Youmayhavelearnedfromexperiencethatitisoftenmoredifficultto
solveaproblemfromscratchthantoverifyaclearlypresentedsolution,especially
whenworkingundertimeconstraints.Theoreticalcomputerscientistsgenerally
believethatthisanalogyextendstotheclassesPandNP,andthusthatNPincludes
languagesthatarenotinP.
Thereismorecompelling,thoughnotconclusive,evidencethatPNPthe
existenceoflanguagesthatareNPcomplete.WeshallstudythisclassinSec
tion34.3.
ManyotherfundamentalquestionsbeyondthePNPquestionremainunre
ResueltoFigure34.3showssomepossiblescenarios.Despitemuchworkbymany
researchers,nooneevenknowswhethertheclassNPisclosedundercomple
https://translate.googleusercontent.com/translate_f 129/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
Cin.Thatis,doesL2NPimplyL2NP?Wecandefinethecomplexityclass
coNPasthesetoflanguagesLsuchthatL2NP.Wecanrestatethequestion
ofwhetherNPisclosedundercomplementaswhetherNPDcoNP.SincePis
closedundercomplement(Exercise34.16),itfollowsfromExercise34.29that
PNP\coNP.Onceagain,however,nooneknowswhetherPDNP\coNP
orwhetherthereissomelanguageinNP\coNPP.

8ThenameNPstandsfornondeterministicpolynomialtime.TheclassNPwasoriginallystudied
inthecontextofnondeterminism,butthisbookusesthesomewhatsimpleryetequivalentnotionof
verificacin.HopcroftandUllman[180]giveagoodpresentationofNPcompletenessintermsof
nondeterministicmodelsofcomputation.

Page186

34.2Polynomialtimeverification 1065

NP=coNP
P=NP=coNP
PAG

(un) (segundo)

coNP notariopblicocoNP NPcoNP notariopblico


P=NPcoNP
PAG

(do) (re)

Figure34.3Fourpossibilitiesforrelationshipsamongcomplexityclasses.Ineachdiagram,one
regionenclosinganotherindicatesapropersubsetrelation.(a)PDNPDcoNP.Mostresearchers
regardthispossibilityasthemostunlikely.(b)IfNPisclosedundercomplement,thenNPDcoNP,
butitneednotbethecasethatPDNP.(c)PDNP\coNP,butNPisnotclosedundercomplement.
(d)NPcoNPandPNP\coNP.Mostresearchersregardthispossibilityasthemostlikely.

Thus,ourunderstandingofthepreciserelationshipbetweenPandNPiswoe
fullyincomplete.Nevertheless,eventhoughwemightnotbeabletoprovethata
particularproblemisintractable,ifwecanprovethatitisNPcomplete,thenwe
havegainedvaluableinformationaboutit.

Exercises

34.21
ConsiderthelanguageGRAPHISOMORPHISMDfhG1G2iWG1andG2are
isomorphicgraphsg.ProvethatGRAPHISOMORPHISM2NPbydescribinga
polynomialtimealgorithmtoverifythelanguage.

34.22
ProvethatifGisanundirectedbipartitegraphwithanoddnumberofvertices,
thenGisnonhamiltonian.

34.23
ShowthatifHAMCYCLE2P,thentheproblemoflistingtheverticesofa
hamiltoniancycle,inorder,ispolynomialtimesolvable.

Page187

1066 Chapter34NPCompleteness

https://translate.googleusercontent.com/translate_f 130/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
34.24
ProvethattheclassNPoflanguagesisclosedunderunion,intersection,concate
nation,andKleenestar.DiscusstheclosureofNPundercomplement.

34.25
ShowthatanylanguageinNPcanbedecidedbyanalgorithmrunningin
time2O.n /forsomeconstantk.
k

34.26
Ahamiltonianpathinagraphisasimplepaththatvisitseveryvertexexactly
unavez.ShowthatthelanguageHAMPATHDfhGuiWthereisahamiltonian
pathfromutoingraphGgbelongstoNP.

34.27
ShowthatthehamiltonianpathproblemfromExercise34.26canbesolvedin
polynomialtimeondirectedacyclicgraphs.Giveanefficientalgorithmforthe
problema.

34.28
Dejar beabooleanformulaconstructedfromthebooleaninputvariablesx1x2
:::xk,negations(:),ANDs(^),ORs(_),andparentheses.Theformulaisa
tautologyifitevaluatesto1foreveryassignmentof1and0totheinputvariables.
DefineTAUTOLOGYasthelanguageofbooleanformulasthataretautologies.
ShowthatTAUTOLOGY2coNP.

34.29
ProvethatPcoNP.

34.210
ProvethatifNPcoNP,thenPNP.

34.211
LetGbeaconnected,undirectedgraphwithatleast3vertices,andletG3bethe
graphobtainedbyconnectingallpairsofverticesthatareconnectedbyapathinG
oflengthatmost3.ProvethatG3ishamiltonian.(Hint:Constructaspanningtree
forG,anduseaninductiveargument.)

Pgina188

34.3NPcompletenessandreducibility 1067

34.3NPcompletenessandreducibility

Perhapsthemostcompellingreasonwhytheoreticalcomputerscientistsbelieve
thatPNPcomesfromtheexistenceoftheclassofNPcompleteproblems.
ThisclasshastheintriguingpropertythatifanyNPcompleteproblemcanbe
solvedinpolynomialtime,theneveryprobleminNPhasapolynomialtimesolu
tion,thatis,PDNP.Despiteyearsofstudy,though,nopolynomialtimealgorithm
haseverbeendiscoveredforanyNPcompleteproblem.
ThelanguageHAMCYCLEisoneNPcompleteproblem.Ifwecoulddecide
HAMCYCLEinpolynomialtime,thenwecouldsolveeveryprobleminNPin
polynomialtime.Infact,ifNPPshouldturnouttobenonempty,wecouldsay
withcertaintythatHAMCYCLE2NPP.
TheNPcompletelanguagesare,inasense,thehardestlanguagesinNP.En
thissection,weshallshowhowtocomparetherelativehardnessoflanguages
usingaprecisenotioncalledpolynomialtimereducibility.Thenweformally
definetheNPcompletelanguages,andwefinishbysketchingaproofthatone
suchlanguage,calledCIRCUITSAT,isNPcomplete.InSections34.4and34.5,
weshallusethenotionofreducibilitytoshowthatmanyotherproblemsareNP
complete.

https://translate.googleusercontent.com/translate_f 131/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
Reducibility
Intuitively,aproblemQcanbereducedtoanotherproblemQ0ifanyinstanceofQ
canbeeasilyrephrasedasaninstanceofQ0,thesolutiontowhichprovidesa
solutiontotheinstanceofQ.Forexample,theproblemofsolvinglinearequations
inanindeterminatexreducestotheproblemofsolvingquadraticequations.Dado
aninstanceaxCbD0,wetransformitto0x2CaxCbD0,whosesolution
providesasolutiontoaxCbD0.Thus,ifaproblemQreducestoanother
problemQ0,thenQis,inasense,nohardertosolvethanQ0.
Returningtoourformallanguageframeworkfordecisionproblems,wesaythat
alanguageL1ispolynomialtimereducibletoalanguageL2,writtenL1PL2,
ifthereexistsapolynomialtimecomputablefunctionfWf01g!f01gsuch
thatforallx2f01g,
x2L1ifandonlyiff.x/2L2: (34,1)
Wecallthefunctionfthereductionfunction,andapolynomialtimealgorithmF
thatcomputesfisareductionalgorithm.
Figure34.4illustratestheideaofapolynomialtimereductionfromalan
guageL1toanotherlanguageL2.Eachlanguageisasubsetoff01g.El
reductionfunctionfprovidesapolynomialtimemappingsuchthatifx2L1,

Pgina189

1068 Chapter34NPCompleteness

{0,1}* F {0,1}*

L2
L1

Figure34.4AnillustrationofapolynomialtimereductionfromalanguageL1toalanguageL2
viaareductionfunctionf.Foranyinputx2f01g,thequestionofwhetherx2L1hasthesame
answerasthequestionofwhetherf.x/2L2.

thenf.x/2L2.Moreover,ifx62L1,thenf.x/62L2.Thus,thereductionfunc
tionmapsanyinstancexofthedecisionproblemrepresentedbythelanguageL1
toaninstancef.x/oftheproblemrepresentedbyL2.Providingananswerto
whetherf.x/2L2directlyprovidestheanswertowhetherx2L1.
Polynomialtimereductionsgiveusapowerfultoolforprovingthatvariouslan
guagesbelongtoP.

Lemma34.3
IfL1L2f01garelanguagessuchthatL1PL2,thenL22Pimplies
L12P.

ProofLetA2beapolynomialtimealgorithmthatdecidesL2,andletFbea
polynomialtimereductionalgorithmthatcomputesthereductionfunctionf.Nosotros
shallconstructapolynomialtimealgorithmA1thatdecidesL1.
Figure34.5illustrateshowweconstructA1.Foragiveninputx2f01g,
algorithmA1usesFtotransformxintof.x/,andthenitusesA2totestwhether
f.x/2L2.AlgorithmA1takestheoutputfromalgorithmA2andproducesthat
answerasitsownoutput.
ThecorrectnessofA1followsfromcondition(34.1).Thealgorithmrunsinpoly
nomialtime,sincebothFandA2runinpolynomialtime(seeExercise34.15).

NPcompleteness

Polynomialtimereductionsprovideaformalmeansforshowingthatoneprob
lemisatleastashardasanother,towithinapolynomialtimefactor.Thatis,if
L1PL2,thenL1isnotmorethanapolynomialfactorharderthanL2,whichis

https://translate.googleusercontent.com/translate_f 132/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,

Pgina190

34.3NPcompletenessandreducibility 1069

yes,f.x/2L 2 yes,x2L 1

x f.x/
F A2

no,f.x/62L 2 no,x62L 1

A
1

Figure34.5TheproofofLemma34.3.ThealgorithmFisareductionalgorithmthatcomputesthe
reductionfunctionffromL1toL2inpolynomialtime,andA2isapolynomialtimealgorithmthat
decidesL2.AlgorithmA1decideswhetherx2L1byusingFtotransformanyinputxintof.x/
andthenusingA2todecidewhetherf.x/2L2.

whythelessthanorequaltonotationforreductionismnemonic.Wecannow
definethesetofNPcompletelanguages,whicharethehardestproblemsinNP.
AlanguageLf01gisNPcompleteif

1.L2NP,and
2.L0PLforeveryL02NP.
IfalanguageLsatisfiesproperty2,butnotnecessarilyproperty1,wesaythatL
isNPhard.WealsodefineNPCtobetheclassofNPcompletelanguages.
Asthefollowingtheoremshows,NPcompletenessisatthecruxofdeciding
whetherPisinfactequaltoNP.

Theorem34.4
IfanyNPcompleteproblemispolynomialtimesolvable,thenPDNP.Equiva
lently,ifanyprobleminNPisnotpolynomialtimesolvable,thennoNPcomplete
problemispolynomialtimesolvable.

ProofSupposethatL2PandalsothatL2NPC.ForanyL02NP,we
haveL0PLbyproperty2ofthedefinitionofNPcompleteness.Thus,by
Lemma34.3,wealsohavethatL02P,whichprovesthefirststatementofthe
teorema.
Toprovethesecondstatement,notethatitisthecontrapositiveofthefirststate
Cin.

ItisforthisreasonthatresearchintothePNPquestioncentersaroundthe
NPcompleteproblems.MosttheoreticalcomputerscientistsbelievethatPNP,
whichleadstotherelationshipsamongP,NP,andNPCshowninFigure34.6.
But,forallweknow,someonemayyetcomeupwithapolynomialtimealgo
rithmforanNPcompleteproblem,thusprovingthatPDNP.Nevertheless,since
nopolynomialtimealgorithmforanyNPcompleteproblemhasyetbeendiscov

Pgina191

1070 Chapter34NPCompleteness

notariopblico
NPC

PAG

Figure34.6HowmosttheoreticalcomputerscientistsviewtherelationshipsamongP,NP,
andNPC.BothPandNPCarewhollycontainedwithinNP,andP\NPCD.

ered,aproofthataproblemisNPcompleteprovidesexcellentevidencethatitis
intractable.

Circuitsatisfiability

https://translate.googleusercontent.com/translate_f 133/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
WehavedefinedthenotionofanNPcompleteproblem,butuptothispoint,we
havenotactuallyprovedthatanyproblemisNPcomplete.Onceweprovethatat
leastoneproblemisNPcomplete,wecanusepolynomialtimereducibilityasa
tooltoproveotherproblemstobeNPcomplete.Thus,wenowfocusondemon
stratingtheexistenceofanNPcompleteproblem:thecircuitsatisfiabilityprob
Lem
Unfortunately,theformalproofthatthecircuitsatisfiabilityproblemisNP
completerequirestechnicaldetailbeyondthescopeofthistext.Instead,weshall
informallydescribeaproofthatreliesonabasicunderstandingofbooleancombi
nationalcircuits.
Booleancombinationalcircuitsarebuiltfrombooleancombinationalelements
thatareinterconnectedbywires.Abooleancombinationalelementisanycircuit
elementthathasaconstantnumberofbooleaninputsandoutputsandthatperforms
awelldefinedfunction.Booleanvaluesaredrawnfromthesetf01g,where0
representsFALSEand1representsTRUE.
Thebooleancombinationalelementsthatweuseinthecircuitsatisfiabilityprob
lemcomputesimplebooleanfunctions,andtheyareknownaslogicgates.Fig
ure34.7showsthethreebasiclogicgatesthatweuseinthecircuitsatisfiability
problem:theNOTgate(orinverter),theANDgate,andtheORgate.TheNOT
gatetakesasinglebinaryinputx,whosevalueiseither0or1,andproducesa
binaryoutputwhosevalueisoppositethatoftheinputvalue.Eachoftheother
twogatestakestwobinaryinputsxandyandproducesasinglebinaryoutput.
Wecandescribetheoperationofeachgate,andofanybooleancombinational
element,byatruthtable,shownundereachgateinFigure34.7.Atruthtablegives
theoutputsofthecombinationalelementforeachpossiblesettingoftheinputs.por

Pgina192

34.3NPcompletenessandreducibility 1071

x x
x Z Z Z
Y Y

x :x x Y x^y x Y x_y
0 1 00 0 00 0
1 0 01 0 01 1
10 0 10 1
11 1 11 1
(un) (segundo) (do)

Figure34.7Threebasiclogicgates,withbinaryinputsandoutputs.Undereachgateisthetruth
tablethatdescribesthegate'soperation.(a)TheNOTgate.(b)TheANDgate.(c)TheORgate.

example,thetruthtablefortheORgatetellsusthatwhentheinputsarexD0
andyD1,theoutputvalueisD1.Weusethesymbols:todenotetheNOT
function,^todenotetheANDfunction,and_todenotetheORfunction.As,
forexample,0_1D1.
WecangeneralizeANDandORgatestotakemorethantwoinputs.AnAND
gate'soutputis1ifallofitsinputsare1,anditsoutputis0otherwise.AnORgates
outputis1ifanyofitsinputsare1,anditsoutputis0otherwise.
Abooleancombinationalcircuitconsistsofoneormorebooleancombinational
elementsinterconnectedbywires.Awirecanconnecttheoutputofoneelement
totheinputofanother,therebyprovidingtheoutputvalueofthefirstelementasan
inputvalueofthesecond.Figure34.8showstwosimilarbooleancombinational
circuits,differinginonlyonegate.Part(a)ofthefigurealsoshowsthevalueson
theindividualwires,giventheinputhx1D1x2D1x3D0i.Althoughasingle
wiremayhavenomorethanonecombinationalelementoutputconnectedtoit,it
canfeedseveralelementinputs.Thenumberofelementinputsfedbyawireis
calledthefanoutofthewire.Ifnoelementoutputisconnectedtoawire,thewire
isacircuitinput,acceptinginputvaluesfromanexternalsource.Ifnoelement
inputisconnectedtoawire,thewireisacircuitoutput,providingtheresultsof
thecircuit'scomputationtotheoutsideworld.(Aninternalwirecanalsofanout
toacircuitoutput.)Forthepurposeofdefiningthecircuitsatisfiabilityproblem,
welimitthenumberofcircuitoutputsto1,thoughinactualhardwaredesign,a
booleancombinationalcircuitmayhavemultipleoutputs.
Booleancombinationalcircuitscontainnocycles.Inotherwords,supposewe

https://translate.googleusercontent.com/translate_f 134/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
createadirectedgraphGD.VE/withonevertexforeachcombinationalelement
andwithkdirectededgesforeachwirewhosefanoutiskthegraphcontains
adirectededge.u/ifawireconnectstheoutputofelementutoaninputof
element.ThenGmustbeacyclic.

Pgina193

1072 Chapter34NPCompleteness

x1 1 1 x1
x2 1 1
1 x2
0
1
0 1
1 1
1 1 1
x3 0 1 x3
1
(un) (segundo)

Figure34.8Twoinstancesofthecircuitsatisfiabilityproblem.(a)Theassignmenthx1D1
x2D1x3D0itotheinputsofthiscircuitcausestheoutputofthecircuittobe1.Thecircuit
isthereforesatisfiable.(b)Noassignmenttotheinputsofthiscircuitcancausetheoutputofthe
circuittobe1.Thecircuitisthereforeunsatisfiable.

Atruthassignmentforabooleancombinationalcircuitisasetofbooleaninput
valores.Wesaythataoneoutputbooleancombinationalcircuitissatisfiableifit
hasasatisfyingassignment:atruthassignmentthatcausestheoutputofthecircuit
tobe1.Forexample,thecircuitinFigure34.8(a)hasthesatisfyingassignment
hx1D1x2D1x3D0i,andsoitissatisfiable.AsExercise34.31asksyouto
show,noassignmentofvaluestox1,x2,andx3causesthecircuitinFigure34.8(b)
toproducea1outputitalwaysproduces0,andsoitisunsatisfiable.
Thecircuitsatisfiabilityproblemis,Givenabooleancombinationalcircuit
composedofAND,OR,andNOTgates,isitsatisfiable?Inordertoposethis
questionformally,however,wemustagreeonastandardencodingforcircuits.
Thesizeofabooleancombinationalcircuitisthenumberofbooleancombina
tionalelementsplusthenumberofwiresinthecircuit.Wecoulddeviseagraphlike
encodingthatmapsanygivencircuitCintoabinarystringhCiwhoselengthis
polynomialinthesizeofthecircuititself.Asaformallanguage,wecantherefore
definir
CIRCUITSATDfhCiWCisasatisfiablebooleancombinationalcircuitg:
Thecircuitsatisfiabilityproblemarisesintheareaofcomputeraidedhardware
optimization.Ifasubcircuitalwaysproduces0,thatsubcircuitisunnecessary
thedesignercanreplaceitbyasimplersubcircuitthatomitsalllogicgatesand
providestheconstant0valueasitsoutput.Youcanseewhywewouldliketohave
apolynomialtimealgorithmforthisproblem.
GivenacircuitC,wemightattempttodeterminewhetheritissatisfiableby
simplycheckingallpossibleassignmentstotheinputs.Unfortunately,ifthecircuit
haskinputs,thenwewouldhavetocheckupto2kpossibleassignments.Cuando

Pgina194

34.3NPcompletenessandreducibility 1073

thesizeofCispolynomialink,checkingeachonetakes.2k/time,whichis
superpolynomialinthesizeofthecircuit.9Infact,aswehaveclaimed,thereis
strongevidencethatnopolynomialtimealgorithmexiststhatsolvesthecircuit
satisfiabilityproblembecausecircuitsatisfiabilityisNPcomplete.Webreakthe
proofofthisfactintotwoparts,basedonthetwopartsofthedefinitionofNP
locompleto.

Lemma34.5
ThecircuitsatisfiabilityproblembelongstotheclassNP.
https://translate.googleusercontent.com/translate_f 135/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,

ProofWeshallprovideatwoinput,polynomialtimealgorithmAthatcanverify
CIRCUITSAT.OneoftheinputstoAis(astandardencodingof)abooleancom
binationalcircuitC.Theotherinputisacertificatecorrespondingtoanassignment
ofbooleanvaluestothewiresinC.(SeeExercise34.34forasmallercertificate.)
WeconstructthealgorithmAasfollows.Foreachlogicgateinthecircuit,it
checksthatthevalueprovidedbythecertificateontheoutputwireiscorrectly
computedasafunctionofthevaluesontheinputwires.Then,iftheoutputofthe
entirecircuitis1,thealgorithmoutputs1,sincethevaluesassignedtotheinputs
ofCprovideasatisfyingassignment.Otherwise,Aoutputs0.
WheneverasatisfiablecircuitCisinputtoalgorithmA,thereexistsacertificate
whoselengthispolynomialinthesizeofCandthatcausesAtooutputa1.When
everanunsatisfiablecircuitisinput,nocertificatecanfoolAintobelievingthat
thecircuitissatisfiable.AlgorithmArunsinpolynomialtime:withagoodimple
mentation,lineartimesuffices.Thus,wecanverifyCIRCUITSATinpolynomial
time,andCIRCUITSAT2NP.

ThesecondpartofprovingthatCIRCUITSATisNPcompleteistoshowthat
thelanguageisNPhard.Thatis,wemustshowthateverylanguageinNPis
polynomialtimereducibletoCIRCUITSAT.Theactualproofofthisfactisfull
oftechnicalintricacies,andsoweshallsettleforasketchoftheproofbasedon
someunderstandingoftheworkingsofcomputerhardware.
Acomputerprogramisstoredinthecomputermemoryasasequenceofin
structions.Atypicalinstructionencodesanoperationtobeperformed,addresses
ofoperandsinmemory,andanaddresswheretheresultistobestored.Aspe
cialmemorylocation,calledtheprogramcounter,keepstrackofwhichinstruc

9Ontheotherhand,ifthesizeofthecircuitCis.2 k/,thenanalgorithmwhoserunningtime
isO.2k/hasarunningtimethatispolynomialinthecircuitsize.EvenifPNP,thissitua
tionwouldnotcontradicttheNPcompletenessoftheproblemtheexistenceofapolynomialtime
algorithmforaspecialcasedoesnotimplythatthereisapolynomialtimealgorithmforallcases.

Pgina195

1074 Chapter34NPCompleteness

tionistobeexecutednext.Theprogramcounterautomaticallyincrementsupon
fetchingeachinstruction,therebycausingthecomputertoexecuteinstructionsse
quentially.Theexecutionofaninstructioncancauseavaluetobewrittentothe
programcounter,however,whichaltersthenormalsequentialexecutionandallows
thecomputertoloopandperformconditionalbranches.
Atanypointduringtheexecutionofaprogram,thecomputer'smemoryholds
theentirestateofthecomputation.(Wetakethememorytoincludetheprogram
itself,theprogramcounter,workingstorage,andanyofthevariousbitsofstate
thatacomputermaintainsforbookkeeping.)Wecallanyparticularstateofcom
putermemoryaconfiguration.Wecanviewtheexecutionofaninstructionas
mappingoneconfigurationtoanother.Thecomputerhardwarethataccomplishes
thismappingcanbeimplementedasabooleancombinationalcircuit,whichwe
denotebyMintheproofofthefollowinglemma.

Lemma34.6
ThecircuitsatisfiabilityproblemisNPhard.

ProofLetLbeanylanguageinNP.Weshalldescribeapolynomialtimealgo
rithmFcomputingareductionfunctionfthatmapseverybinarystringxtoa
circuitCDf.x/suchthatx2LifandonlyifC2CIRCUITSAT.
SinceL2NP,theremustexistanalgorithmAthatverifiesLinpolynomial
hora.ThealgorithmFthatweshallconstructusesthetwoinputalgorithmAto
computethereductionfunctionf.
LetT.n/denotetheworstcaserunningtimeofalgorithmAonlengthninput
strings,andletk 1beaconstantsuchthatT.n/DOnk/andthelengthofthe
certificateisOnk/.(TherunningtimeofAisactuallyapolynomialinthetotal
inputsize,whichincludesbothaninputstringandacertificate,butsincethelength
ofthecertificateispolynomialinthelengthnoftheinputstring,therunningtime
ispolynomialinn.)
ThebasicideaoftheproofistorepresentthecomputationofAasasequence

https://translate.googleusercontent.com/translate_f 136/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
ofconfigurations.AsFigure34.9illustrates,wecanbreakeachconfigurationinto
partsconsistingoftheprogramforA,theprogramcounterandauxiliarymachine
state,theinputx,thecertificatey,andworkingstorage.Thecombinationalcir
cuitM,whichimplementsthecomputerhardware,mapseachconfigurationcito
thenextconfigurationciC1,startingfromtheinitialconfigurationc0.AlgorithmA
writesitsoutput0or1tosomedesignatedlocationbythetimeitfinishesex
ecuting,andifweassumethatthereafterAhalts,thevalueneverchanges.As,
ifthealgorithmrunsforatmostT.n/steps,theoutputappearsasoneofthebits
incT.n/.
ThereductionalgorithmFconstructsasinglecombinationalcircuitthatcom
putesallconfigurationsproducedbyagiveninitialconfiguration.Theideaisto

Page196

34.3NPcompletenessandreducibility 1075

c0 UN ordenadorpersonal
auxmachinestate x Y workingstorage

METRO

c1 UN ordenadorpersonal
auxmachinestate x Y workingstorage

METRO

c2 UN ordenadorpersonal
auxmachinestate x Y workingstorage

METRO
...

METRO

cT(n ) UN ordenadorpersonal
auxmachinestate x Y workingstorage

0/1output

Figure34.9ThesequenceofconfigurationsproducedbyanalgorithmArunningonaninputxand
certificatey.Eachconfigurationrepresentsthestateofthecomputerforonestepofthecomputation
and,besidesA,x,andy,includestheprogramcounter(PC),auxiliarymachinestate,andworking
almacenamiento.Exceptforthecertificatey,theinitialconfigurationc0isconstant.Abooleancombinational
circuitMmapseachconfigurationtothenextconfiguration.Theoutputisadistinguishedbitinthe
workingstorage.

Pgina197

1076 Chapter34NPCompleteness

pastetogetherT.n/copiesofthecircuitM.Theoutputoftheithcircuit,which
producesconfigurationci,feedsdirectlyintotheinputofthe.iC1/stcircuit.Thus,
https://translate.googleusercontent.com/translate_f 137/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
theconfigurations,ratherthanbeingstoredinthecomputer'smemory,simplyre
sideasvaluesonthewiresconnectingcopiesofM.
RecallwhatthepolynomialtimereductionalgorithmFmustdo.Givenanin
putx,itmustcomputeacircuitCDf.x/thatissatisfiableifandonlyifthere
existsacertificateysuchthatAxy/D1.WhenFobtainsaninputx,itfirst
computesnDjxjandconstructsacombinationalcircuitC0consistingofT.n/
copiesofM.TheinputtoC0isaninitialconfigurationcorrespondingtoacompu
tationonAxy/,andtheoutputistheconfigurationcT.n/.
AlgorithmFmodifiescircuitC0slightlytoconstructthecircuitCDf.x/.
First,itwirestheinputstoC0correspondingtotheprogramforA,theinitialpro
gramcounter,theinputx,andtheinitialstateofmemorydirectlytotheseknown
valores.Thus,theonlyremaininginputstothecircuitcorrespondtothecertifi
catey.Second,itignoresalloutputsfromC0,exceptfortheonebitofcT.n/
correspondingtotheoutputofA.ThiscircuitC,soconstructed,computes
Cy/DAxy/foranyinputyoflengthOnk/.ThereductionalgorithmF,
whenprovidedaninputstringx,computessuchacircuitCandoutputsit.
Weneedtoprovetwoproperties.First,wemustshowthatFcorrectlycomputes
areductionfunctionf.Thatis,wemustshowthatCissatisfiableifandonlyif
thereexistsacertificateysuchthatAxy/D1.Second,wemustshowthatF
runsinpolynomialtime.
ToshowthatFcorrectlycomputesareductionfunction,letussupposethatthere
existsacertificateyoflengthOnk/suchthatAxy/D1.Then,ifweapplythe
bitsofytotheinputsofC,theoutputofCisCy/DAxy/D1.Thus,ifa
certificateexists,thenCissatisfiable.Fortheotherdirection,supposethatCis
satisfiable.Hence,thereexistsaninputytoCsuchthatCy/D1,fromwhich
weconcludethatAxy/D1.Thus,Fcorrectlycomputesareductionfunction.
Tocompletetheproofsketch,weneedonlyshowthatFrunsintimepolynomial
innDjxj.Thefirstobservationwemakeisthatthenumberofbitsrequiredto
representaconfigurationispolynomialinn.TheprogramforAitselfhasconstant
size,independentofthelengthofitsinputx.Thelengthoftheinputxisn,and
thelengthofthecertificateyisOnk/.SincethealgorithmrunsforatmostOnk/
steps,theamountofworkingstoragerequiredbyAispolynomialinnaswell.
(WeassumethatthismemoryiscontiguousExercise34.35asksyoutoextend
theargumenttothesituationinwhichthelocationsaccessedbyAarescattered
acrossamuchlargerregionofmemoryandtheparticularpatternofscatteringcan
differforeachinputx.)
ThecombinationalcircuitMimplementingthecomputerhardwarehassize
polynomialinthelengthofaconfiguration,whichisOnk/hence,thesizeofM
ispolynomialinn.(Mostofthiscircuitryimplementsthelogicofthememory

Page198

34.3NPcompletenessandreducibility 1077

system.)ThecircuitCconsistsofatmosttDOnk/copiesofM,andhenceit
hassizepolynomialinn.ThereductionalgorithmFcanconstructCfromxin
polynomialtime,sinceeachstepoftheconstructiontakespolynomialtime.

ThelanguageCIRCUITSATisthereforeatleastashardasanylanguageinNP,
andsinceitbelongstoNP,itisNPcomplete.

Theorem34.7
ThecircuitsatisfiabilityproblemisNPcomplete.

ProofImmediatefromLemmas34.5and34.6andfromthedefinitionofNP
locompleto.

Exercises

34.31
VerifythatthecircuitinFigure34.8(b)isunsatisfiable.

34.32
ShowthatthePrelationisatransitiverelationonlanguages.Thatis,showthatif
L1PL2andL2PL3,thenL1PL3.

34.33
ProvethatLPLifandonlyifLPL.

https://translate.googleusercontent.com/translate_f 138/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
34.34
Showthatwecouldhaveusedasatisfyingassignmentasacertificateinanalter
nativeproofofLemma34.5.Whichcertificatemakesforaneasierproof?

34.35
TheproofofLemma34.6assumesthattheworkingstorageforalgorithmAoccu
piesacontiguousregionofpolynomialsize.Whereintheproofdoweexploitthis
assumption?Arguethatthisassumptiondoesnotinvolveanylossofgenerality.

34.36
AlanguageLiscompleteforalanguageclassCwithrespecttopolynomialtime
reductionsifL2CandL0PLforallL02C.Showthatandf01garethe
onlylanguagesinPthatarenotcompleteforPwithrespecttopolynomialtime
reductions.

Pgina199

1078 Chapter34NPCompleteness

34.37
Showthat,withrespecttopolynomialtimereductions(seeExercise34.36),Lis
completeforNPifandonlyifLiscompleteforcoNP.

34.38
ThereductionalgorithmFintheproofofLemma34.6constructsthecircuit
CDf.x/basedonknowledgeofx,A,andk.ProfessorSartreobservesthat
thestringxisinputtoF,butonlytheexistenceofA,k,andtheconstantfactor
implicitintheOnk/runningtimeisknowntoF(sincethelanguageLbelongs
toNP),nottheiractualvalues.Thus,theprofessorconcludesthatFcan'tpossi
blyconstructthecircuitCandthatthelanguageCIRCUITSATisnotnecessarily
NPhard.Explaintheflawintheprofessor'sreasoning.

34.4NPcompletenessproofs

WeprovedthatthecircuitsatisfiabilityproblemisNPcompletebyadirectproof
thatLPCIRCUITSATforeverylanguageL2NP.Inthissection,weshall
showhowtoprovethatlanguagesareNPcompletewithoutdirectlyreducingevery
languageinNPtothegivenlanguage.Weshallillustratethismethodologyby
provingthatvariousformulasatisfiabilityproblemsareNPcomplete.Section34.5
providesmanymoreexamplesofthemethodology.
Thefollowinglemmaisthebasisofourmethodforshowingthatalanguageis
NPcomplete.

Lemma34.8
IfLisalanguagesuchthatL0PLforsomeL02NPC,thenLisNPhard.If,in
addition,L2NP,thenL2NPC.

ProofSinceL0isNPcomplete,forallL002NP,wehaveL00PL0.Bysup
position,L0PL,andthusbytransitivity(Exercise34.32),wehaveL00PL,
whichshowsthatLisNPhard.IfL2NP,wealsohaveL2NPC.

Inotherwords,byreducingaknownNPcompletelanguageL0toL,weimplic
itlyreduceeverylanguageinNPtoL.Thus,Lemma34.8givesusamethodfor
provingthatalanguageLisNPcomplete:
1.ProveL2NP.
2.SelectaknownNPcompletelanguageL0.

https://translate.googleusercontent.com/translate_f 139/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
Pgina200

34.4NPcompletenessproofs 1079

3.Describeanalgorithmthatcomputesafunctionfmappingeveryinstance
x2f01gofL0toaninstancef.x/ofL.
4.Provethatthefunctionfsatisfiesx2L0ifandonlyiff.x/2Lforall
x2f01g.
5.Provethatthealgorithmcomputingfrunsinpolynomialtime.
(Steps25showthatLisNPhard.)Thismethodologyofreducingfromasin
gleknownNPcompletelanguageisfarsimplerthanthemorecomplicatedpro
cessofshowingdirectlyhowtoreducefromeverylanguageinNP.Proving
CIRCUITSAT2NPChasgivenusafootinthedoor.Becauseweknowthatthe
circuitsatisfiabilityproblemisNPcomplete,wenowcanprovemuchmoreeasily
thatotherproblemsareNPcomplete.Moreover,aswedevelopacatalogofknown
NPcompleteproblems,wewillhavemoreandmorechoicesforlanguagesfrom
whichtoreduce.

Formulasatisfiability
WeillustratethereductionmethodologybygivinganNPcompletenessprooffor
theproblemofdeterminingwhetherabooleanformula,notacircuit,issatisfiable.
Thisproblemhasthehistoricalhonorofbeingthefirstproblemevershowntobe
NPcomplete.
Weformulatethe(formula)satisfiabilityproblemintermsofthelanguageSAT
comosigue.AninstanceofSATisabooleanformulacomposedof
1.nbooleanvariables:x1x2:::xn
2.mbooleanconnectives:anybooleanfunctionwithoneortwoinputsandone
output,suchas^(AND),_(OR),:(NOT),!(implication),$(ifandonly
if)y
3.parentheses.(Withoutlossofgenerality,weassumethattherearenoredundant
parentheses,ie,aformulacontainsatmostonepairofparenthesesperboolean
connective.)
WecaneasilyencodeabooleanformulainalengththatispolynomialinnCm.
Asinbooleancombinationalcircuits,atruthassignmentforabooleanformula
isasetofvaluesforthevariablesof,andasatisfyingassignmentisatruth
assignmentthatcausesittoevaluateto1.Aformulawithasatisfyingassignment
isasatisfiableformula.Thesatisfiabilityproblemaskswhetheragivenboolean
formulaissatisfiableinformallanguageterms,
SATDfhiWisasatisfiablebooleanformulag:
Asanexample,theformula

Pgina201

1080 Chapter34NPCompleteness

D..x1!x2/_:..:x1$x3/_x4//^:x2
hasthesatisfyingassignmenthx1D0x2D0x3D1x4D1i,since
D..0!0/_:..:0$1/_1//^:0 (34.2)
D.1_:.1_1//^1
D.1_0/^1
D1
andthusthisformulabelongstoSAT.
Thenaivealgorithmtodeterminewhetheranarbitrarybooleanformulaissatis
fiabledoesnotruninpolynomialtime.Aformulawithnvariableshas2npossible
assignments.Ifthelengthofhiispolynomialinn,thencheckingeveryassign
mentrequires.2n/time,whichissuperpolynomialinthelengthofhi.Asthe
followingtheoremshows,apolynomialtimealgorithmisunlikelytoexist.

Theorem34.9

https://translate.googleusercontent.com/translate_f 140/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
SatisfiabilityofbooleanformulasisNPcomplete.
ProofWestartbyarguingthatSAT2NP.ThenweprovethatSATisNPhardby
showingthatCIRCUITSATPSATbyLemma34.8,thiswillprovethetheorem.
ToshowthatSATbelongstoNP,weshowthatacertificateconsistingofa
satisfyingassignmentforaninputformulacanbeverifiedinpolynomialtime.
Theverifyingalgorithmsimplyreplaceseachvariableintheformulawithitscor
respondingvalueandthenevaluatestheexpression,muchaswedidinequa
tion(34.2)above.Thistaskiseasytodoinpolynomialtime.Iftheexpression
evaluatesto1,thenthealgorithmhasverifiedthattheformulaissatisfiable.As,
thefirstconditionofLemma34.8forNPcompletenessholds.
ToprovethatSATisNPhard,weshowthatCIRCUITSATPSAT.Inother
words,weneedtoshowhowtoreduceanyinstanceofcircuitsatisfiabilitytoan
instanceofformulasatisfiabilityinpolynomialtime.Wecanuseinductionto
expressanybooleancombinationalcircuitasabooleanformula.Wesimplylook
atthegatethatproducesthecircuitoutputandinductivelyexpresseachofthe
gate'sinputsasformulas.Wethenobtaintheformulaforthecircuitbywritingan
expressionthatappliesthegate'sfunctiontoitsinputs'formulas.
Unfortunately,thisstraightforwardmethoddoesnotamounttoapolynomial
timereduction.AsExercise34.41asksyoutoshow,sharedsubformulaswhich
arisefromgateswhoseoutputwireshavefanoutof2ormorecancausethe
sizeofthegeneratedformulatogrowexponentially.Thus,thereductionalgorithm
mustbesomewhatmoreclever.
Figure34.10illustrateshowweovercomethisproblem,usingasanexample
thecircuitfromFigure34.8(a).ForeachwirexiinthecircuitC,theformula

Pgina202

34.4NPcompletenessproofs 1081

x1 x5
x2
x8
x6
x9 x10
x4 x7
x3

Figure34.10Reducingcircuitsatisfiabilitytoformulasatisfiability.Theformulaproducedbythe
reductionalgorithmhasavariableforeachwireinthecircuit.

hasavariablexi.Wecannowexpresshoweachgateoperatesasasmallformula
involvingthevariablesofitsincidentwires.Forexample,theoperationofthe
outputANDgateisx10$.x7^x8^x9/.Wecalleachofthesesmallformulasa
clause.
TheformulaproducedbythereductionalgorithmistheANDofthecircuit
outputvariablewiththeconjunctionofclausesdescribingtheoperationofeach
portn.Forthecircuitinthefigure,theformulais
Dx10^.x4$:x3/
^.x5$.x1_x2//
^.x6$:x4/
^.x7$.x1^x2^x4//
^.x8$.x5_x6//
^.x9$.x6_x7//
^.x10$.x7^x8^x9//:

GivenacircuitC,itisstraightforwardtoproducesuchaformulainpolynomial
hora.
WhyisthecircuitCsatisfiableexactlywhentheformulaissatisfiable?IfC
hasasatisfyingassignment,theneachwireofthecircuithasawelldefinedvalue,
andtheoutputofthecircuitis1.Therefore,whenweassignwirevaluesto
variablesin,eachclauseof evaluatesto1,andthustheconjunctionofall
evaluatesto1.Conversely,ifsomeassignmentcauses toevaluateto1,the
circuitCissatisfiablebyananalogousargument.Thus,wehaveshownthat
CIRCUITSATPSAT,whichcompletestheproof.
https://translate.googleusercontent.com/translate_f 141/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,

Pgina203

1082 Chapter34NPCompleteness

3CNFsatisfiability
WecanprovemanyproblemsNPcompletebyreducingfromformulasatisfiability.
Thereductionalgorithmmusthandleanyinputformula,though,andthisrequire
mentcanleadtoahugenumberofcasesthatwemustconsider.Weoftenprefer
toreducefromarestrictedlanguageofbooleanformulas,sothatweneedtocon
siderfewercases.Ofcourse,wemustnotrestrictthelanguagesomuchthatit
becomespolynomialtimesolvable.Oneconvenientlanguageis3CNFsatisfiabil
ity,or3CNFSAT.
Wedefine3CNFsatisfiabilityusingthefollowingterms.Aliteralinaboolean
formulaisanoccurrenceofavariableoritsnegation.Abooleanformulaisin
conjunctivenormalform,orCNF,ifitisexpressedasanANDofclauses,each
ofwhichistheORofoneormoreliterals.Abooleanformulaisin3conjunctive
normalform,or3CNF,ifeachclausehasexactlythreedistinctliterals.
Forexample,thebooleanformula
.x1_:x1_:x2/^.x3_x2_x4/^.:x1_:x3_:x4/
isin3CNF.Thefirstofitsthreeclausesis.x1_:x1_:x2/,whichcontainsthe
threeliteralsx1,:x1,and:x2.
In3CNFSAT,weareaskedwhetheragivenbooleanformula in3CNFis
satisfiable.Thefollowingtheoremshowsthatapolynomialtimealgorithmthat
candeterminethesatisfiabilityofbooleanformulasisunlikelytoexist,evenwhen
theyareexpressedinthissimplenormalform.

Theorem34.10
Satisfiabilityofbooleanformulasin3conjunctivenormalformisNPcomplete.

ProofTheargumentweusedintheproofofTheorem34.9toshowthatSAT2
NPappliesequallywellheretoshowthat3CNFSAT2NP.ByLemma34.8,
therefore,weneedonlyshowthatSATP3CNFSAT.
Webreakthereductionalgorithmintothreebasicsteps.Eachstepprogressively
transformstheinputformulaclosertothedesired3conjunctivenormalform.
ThefirststepissimilartotheoneusedtoproveCIRCUITSATPSATin
Theorem34.9.First,weconstructabinaryparsetreefortheinputformula,
withliteralsasleavesandconnectivesasinternalnodes.Figure34.11showssuch
aparsetreefortheformula
D..x1!x2/_:..:x1$x3/_x4//^:x2: (34.3)
ShouldtheinputformulacontainaclausesuchastheORofseveralliterals,weuse
associativitytoparenthesizetheexpressionfullysothateveryinternalnodeinthe
resultingtreehas1or2children.Wecannowthinkofthebinaryparsetreeasa
circuitforcomputingthefunction.

Pgina204

34.4NPcompletenessproofs 1083

y1

^
y
2

_ :x 2

y 3 y4

! :
y5

x1 x2 _
y 6

https://translate.googleusercontent.com/translate_f 142/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
ps x 4

:x 1 x 3

Figure34.11ThetreecorrespondingtotheformulaD..x1!x2/_:..:x1$x3/_x4//^:x2:

MimickingthereductionintheproofofTheorem34.9,weintroduceavari
ableyifortheoutputofeachinternalnode.Then,werewritetheoriginalfor
mulaastheANDoftherootvariableandaconjunctionofclausesdescribingthe
operationofeachnode.Fortheformula(34.3),theresultingexpressionis
0Dy1^.y1$.y2^:x2//
^.y2$.y3_y4//
^.y3$.x1!x2//
^.y4$:y5/
^.y5$.y6_x4//
^.y6$.:x1$x3//:

Observethattheformula0thusobtainedisaconjunctionofclauses0
i,eachof
whichhasatmost3literals.Theonlyrequirementthatwemightfailtomeetis
thateachclausehastobeanORof3literals.
Thesecondstepofthereductionconvertseachclause0 iintoconjunctivenormal
formar.Weconstructatruthtablefor0
ibyevaluatingallpossibleassignmentsto
itsvariables.Eachrowofthetruthtableconsistsofapossibleassignmentofthe
variablesoftheclause,togetherwiththevalueoftheclauseunderthatassignment.
Usingthetruthtableentriesthatevaluateto0,webuildaformulaindisjunctive
normalform(orDNF)anORofANDsthatisequivalentto:0 i. Nosotrosentonces
negatethisformulaandconvertitintoaCNFformula00 ibyusingDeMorgans

Pgina205

1084 Chapter34NPCompleteness

y1 y2 x2 .y1$.y2^:x2//
1 1 1 0
1 1 0 1
1 0 1 0
1 0 0 0
0 1 1 1
0 1 0 0
0 0 1 1
0 0 0 1

Figure34.12Thetruthtablefortheclause.y1$.y2^:x2//.

lawsforpropositionallogic,
:.a^b/D:a_:b
:.a_b/D:a^:b
tocomplementallliterals,changeORsintoANDs,andchangeANDsintoORs.
Inourexample,weconverttheclause0 1D.y1$.y2^:x2//intoCNF
comosigue.Thetruthtablefor0
1appearsinFigure34.12.TheDNFformula
equivalentto:0 1is
.y1^y2^x2/_.y1^:y2^x2/_.y1^:y2^:x2/_.:y1^y2^:x2/:
NegatingandapplyingDeMorgan'slaws,wegettheCNFformula
00 D.:y1_:y2_:x2/^.:y1_y2_:x2/
1
^.:y1_y2_x2/^.y1_:y2_x2/
whichisequivalenttotheoriginalclause0 1.
Atthispoint,wehaveconvertedeachclause0 ioftheformula0intoaCNF
formula00 i,andthus0isequivalenttotheCNFformula00consistingofthe
conjunctionofthe00
i. Moreover,eachclauseof00hasatmost3literals.

https://translate.googleusercontent.com/translate_f 143/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
Thethirdandfinalstepofthereductionfurthertransformstheformulasothat000
eachclausehasexactly3distinctliterals.Weconstructthefinal3CNFformula
fromtheclausesoftheCNFformula00.Theformula000alsousestwoauxiliary
variablesthatweshallcallpandq.ForeachclauseCiof00,weincludethe
followingclausesin000:
IfCihas3distinctliterals,thensimplyincludeCiasaclauseof000.
IfCihas2distinctliterals,thatis,ifCiD.l1_l2/,wherel1andl2areliterals,
theninclude.l1_l2_p/^.l1_l2_:p/asclausesof000.Theliterals
pand:pmerelyfulfillthesyntacticrequirementthateachclauseof000has

Pgina206

34.4NPcompletenessproofs 1085

exactly3distinctliterals.WhetherpD0orpD1,oneoftheclausesis
equivalenttol1_l2,andtheotherevaluatesto1,whichistheidentityforAND.
IfCihasjust1distinctliterall,theninclude.l_p_q/^.l_p_:q/^
.l_:p_q/^.l_:p_:q/asclausesof000.Regardlessofthevaluesofp
andq,oneofthefourclausesisequivalenttol,andtheother3evaluateto1.
Wecanseethatthe3CNFformula000issatisfiableifandonlyifissatisfiable
byinspectingeachofthethreesteps.LikethereductionfromCIRCUITSATto
SAT,theconstructionof0from inthefirststeppreservessatisfiability.los
secondstepproducesaCNFformula00thatisalgebraicallyequivalentto0.El
thirdstepproducesa3CNFformula000thatiseffectivelyequivalentto00,since
anyassignmenttothevariablespandqproducesaformulathatisalgebraically
equivalentto00.
Wemustalsoshowthatthereductioncanbecomputedinpolynomialtime.Estafa
structing0fromintroducesatmost1variableand1clauseperconnectivein.
Constructing00from0canintroduceatmost8clausesinto00foreachclause
from0,sinceeachclauseof0hasatmost3variables,andthetruthtablefor
eachclausehasatmost23D8rows.Theconstructionof000from00introduces
atmost4clausesinto000foreachclauseof00.Thus,thesizeoftheresulting
formula000ispolynomialinthelengthoftheoriginalformula.Eachofthecon
structionscaneasilybeaccomplishedinpolynomialtime.

Exercises

34.41
Considerthestraightforward(nonpolynomialtime)reductionintheproofofThe
orem34.9.Describeacircuitofsizenthat,whenconvertedtoaformulabythis
method,yieldsaformulawhosesizeisexponentialinn.

34.42
Showthe3CNFformulathatresultswhenweusethemethodofTheorem34.10
ontheformula(34.3).

34.43
ProfessorJaggerproposestoshowthatSATP3CNFSATbyusingonlythe
truthtabletechniqueintheproofofTheorem34.10,andnottheothersteps.Ese
is,theprofessorproposestotakethebooleanformula,formatruthtablefor
itsvariables,derivefromthetruthtableaformulain3DNFthatisequivalent
to:,andthennegateandapplyDeMorgan'slawstoproducea3CNFformula
equivalentto.Showthatthisstrategydoesnotyieldapolynomialtimereduction.

Pgina207

1086 Chapter34NPCompleteness

34.44
Showthattheproblemofdeterminingwhetherabooleanformulaisatautologyis
completeforcoNP.(Hint:SeeExercise34.37.)

https://translate.googleusercontent.com/translate_f 144/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
34.45
Showthattheproblemofdeterminingthesatisfiabilityofbooleanformulasindis
junctivenormalformispolynomialtimesolvable.

34.46
Supposethatsomeonegivesyouapolynomialtimealgorithmtodecideformula
satisfiability.Describehowtousethisalgorithmtofindsatisfyingassignmentsin
polynomialtime.

34.47
Let2CNFSATbethesetofsatisfiablebooleanformulasinCNFwithexactly2
literalsperclause.Showthat2CNFSAT2P.Makeyouralgorithmasefficientas
posible.(Hint:Observethatx_yisequivalentto:x!y.Reduce2CNFSAT
toanefficientlysolvableproblemonadirectedgraph.)

34.5NPcompleteproblems

NPcompleteproblemsariseindiversedomains:booleanlogic,graphs,arithmetic,
networkdesign,setsandpartitions,storageandretrieval,sequencingandschedul
ing,mathematicalprogramming,algebraandnumbertheory,gamesandpuzzles,
automataandlanguagetheory,programoptimization,biology,chemistry,physics,
yms.Inthissection,weshallusethereductionmethodologytoprovideNP
completenessproofsforavarietyofproblemsdrawnfromgraphtheoryandset
partitioning.
Figure34.13outlinesthestructureoftheNPcompletenessproofsinthissection
andSection34.4.WeproveeachlanguageinthefiguretobeNPcompleteby
reductionfromthelanguagethatpointstoit.AttherootisCIRCUITSAT,which
weprovedNPcompleteinTheorem34.7.

34.5.1Thecliqueproblem
AcliqueinanundirectedgraphGD.VE/isasubsetV0Vofvertices,each
pairofwhichisconnectedbyanedgeinE.Inotherwords,acliqueisacomplete
subgraphofG.Thesizeofacliqueisthenumberofverticesitcontains.El
cliqueproblemistheoptimizationproblemoffindingacliqueofmaximumsizein

Pgina208

34.5NPcompleteproblems 1087

CIRCUITSAT

SAT

3CNFSAT

CLIQUE SUBSETSUM

VERTEXCOVER

HAMCYCLE

TSP

Figure34.13ThestructureofNPcompletenessproofsinSections34.4and34.5.Allproofsulti
matelyfollowbyreductionfromtheNPcompletenessofCIRCUITSAT.

agraph.Asadecisionproblem,weasksimplywhetheracliqueofagivensizek
existsinthegraph.Theformaldefinitionis
CLIQUEDfhGkiWGisagraphcontainingacliqueofsizekg:
AnaivealgorithmfordeterminingwhetheragraphGD.VE/withjVjver
ticeshasacliqueofsizekistolistallksubsetsofV,andcheckeachoneto
seewhetheritformsaclique.Therunningtimeofthisalgorithmis.k2jVj /,
https://translate.googleusercontent.com/translate_f 145/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
whichispolynomialifkisaconstant.Ingeneral,however,kcouldbenearjVj=2,
K
inwhichcasethealgorithmrunsinsuperpolynomialtime.Indeed,anefficient
algorithmforthecliqueproblemisunlikelytoexist.

Theorem34.11
ThecliqueproblemisNPcomplete.

ProofToshowthatCLIQUE2NP,foragivengraphGD.VE/,weusethe
setV0VofverticesinthecliqueasacertificateforG.WecancheckwhetherV0
isacliqueinpolynomialtimebycheckingwhether,foreachpairu2V0,the
edge.u/belongstoE.
Wenextprovethat3CNFSATPCLIQUE,whichshowsthatthecliqueprob
lemisNPhard.Youmightbesurprisedthatweshouldbeabletoprovesucha
result,sinceonthesurfacelogicalformulasseemtohavelittletodowithgraphs.
Thereductionalgorithmbeginswithaninstanceof3CNFSAT.Dejar re
C1^C2^^Ckbeabooleanformulain3CNFwithkclauses.ForrD

Pgina209

1088 Chapter34NPCompleteness

C Dx _:x _:x
1 1 2 3

x 1 :x 2 :x 3

:x
1 x1

C D:x _x _x
2 1 2 3 x 2 x 2 C Dx _x _x
3 1 2 3

x 3 x 3

Figure34.14ThegraphGderivedfromthe3CNFformula DC1^C2^C3,whereC1D
.x1_:x2_:x3/,C2D.:x1_x2_x3/,andC3D.x1_x2_x3/,inreducing3CNFSATto
CLIQUE.Asatisfyingassignmentoftheformulahasx2D0,x3D1,andx1either0or1.This
assignmentsatisfiesC1with:x2,anditsatisfiesC2andC3withx3,correspondingtotheclique
withlightlyshadedvertices.

12:::k,eachclauseCrhasexactlythreedistinctliteralslr 1,l2
r,andlr
3. Weshall
constructagraphGsuchthatissatisfiableifandonlyifGhasacliqueofsizek.
WeconstructthegraphGD.VE/asfollows.ForeachclauseCr re
.l1
r
_lr2_lr3/in,weplaceatripleofverticesr 1, r2,andr 3intoV.Weput
anedgebetweentwoverticesr iands jifbothofthefollowinghold:
R
iands jareindifferenttriples,thatis,rs,and
theircorrespondingliteralsareconsistent,thatis,lriisnotthenegationofls j.
Wecaneasilybuildthisgraphfrominpolynomialtime.Asanexampleofthis
construction,ifwehave
D.x1_:x2_:x3/^.:x1_x2_x3/^.x1_x2_x3/

thenGisthegraphshowninFigure34.14.
WemustshowthatthistransformationofintoGisareduction.First,suppose
ese hasasatisfyingassignment.TheneachclauseCrcontainsatleastone
literallr
ithatisassigned1,andeachsuchliteralcorrespondstoavertexr i. Picking
onesuchtrueliteralfromeachclauseyieldsasetV0ofkvertices.Weclaimthat
V0isaclique.Foranytwoverticesr iSj2V0,wherers,bothcorresponding
literalslr
iandlsjmapto1bythegivensatisfyingassignment,andthustheliterals

Pgina210

https://translate.googleusercontent.com/translate_f 146/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,

34.5NPcompleteproblems 1089

cannotbecomplements.Thus,bytheconstructionofG,theedge.R iSj/belongs
toE.
Conversely,supposethatGhasacliqueV0ofsizek.NoedgesinGconnect
verticesinthesametriple,andsoV0containsexactlyonevertexpertriple.Podemos
assign1toeachliterallr isuchthatr i2V0withoutfearofassigning1tobotha
literalanditscomplement,sinceGcontainsnoedgesbetweeninconsistentliterals.
Eachclauseissatisfied,andsoissatisfied.(Anyvariablesthatdonotcorrespond
toavertexinthecliquemaybesetarbitrarily.)

IntheexampleofFigure34.14,asatisfyingassignmentof hasx2D0and
x3D1.AcorrespondingcliqueofsizekD3consistsoftheverticescorrespond
ingto:x2fromthefirstclause,x3fromthesecondclause,andx3fromthethird
clause.Becausethecliquecontainsnoverticescorrespondingtoeitherx1or:x1,
wecansetx1toeither0or1inthissatisfyingassignment.
ObservethatintheproofofTheorem34.11,wereducedanarbitraryinstance
of3CNFSATtoaninstanceofCLIQUEwithaparticularstructure.Youmight
thinkthatwehaveshownonlythatCLIQUEisNPhardingraphsinwhichthe
verticesarerestrictedtooccurintriplesandinwhichtherearenoedgesbetween
verticesinthesametriple.Indeed,wehaveshownthatCLIQUEisNPhardonly
inthisrestrictedcase,butthisproofsufficestoshowthatCLIQUEisNPhardin
generalgraphs.Porqu?IfwehadapolynomialtimealgorithmthatsolvedCLIQUE
ongeneralgraphs,itwouldalsosolveCLIQUEonrestrictedgraphs.
Theoppositeapproachreducinginstancesof3CNFSATwithaspecialstruc
turetogeneralinstancesofCLIQUEwouldnothavesufficed,however.Porqu
no?Perhapstheinstancesof3CNFSATthatwechosetoreducefromwere
easy,andsowewouldnothavereducedanNPhardproblemtoCLIQUE.
Observealsothatthereductionusedtheinstanceof3CNFSAT,butnotthe
solucin.Wewouldhaveerredifthepolynomialtimereductionhadreliedon
knowingwhethertheformulaissatisfiable,sincewedonotknowhowtodecide
whetherissatisfiableinpolynomialtime.

34.5.2Thevertexcoverproblem

AvertexcoverofanundirectedgraphGD.VE/isasubsetV0Vsuchthat
if.u/2E,thenu2V0or2V0(orboth).Thatis,eachvertexcoversits
incidentedges,andavertexcoverforGisasetofverticesthatcoversalltheedges
inE.Thesizeofavertexcoveristhenumberofverticesinit.Forexample,the
graphinFigure34.15(b)hasavertexcoverfwgofsize2.
Thevertexcoverproblemistofindavertexcoverofminimumsizeinagiven
grafico.Restatingthisoptimizationproblemasadecisionproblem,wewishto

Page211

1090 Chapter34NPCompleteness

Usted V Usted V

Z W Z W

Y x Y x

(un) (segundo)

Figure34.15ReducingCLIQUEtoVERTEXCOVER.(a)AnundirectedgraphGD.VE/with
cliqueV0Dfuxyg.(b)ThegraphGproducedbythereductionalgorithmthathasvertexcover
V V0Dfwg.

determinewhetheragraphhasavertexcoverofagivensizek.Asalanguage,we
definir

VERTEXCOVERDfhGkiWgraphGhasavertexcoverofsizekg:
https://translate.googleusercontent.com/translate_f 147/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,

ThefollowingtheoremshowsthatthisproblemisNPcomplete.

Theorem34.12
ThevertexcoverproblemisNPcomplete.

ProofWefirstshowthatVERTEXCOVER2NP.Supposewearegivenagraph
GD.VE/andanintegerk.ThecertificatewechooseisthevertexcoverV0V
smismo.TheverificationalgorithmaffirmsthatjV0jDk,andthenitchecks,foreach
edge.u/2E,thatu2V0or 2V0.Wecaneasilyverifythecertificatein
polynomialtime.
WeprovethatthevertexcoverproblemisNPhardbyshowingthatCLIQUEP
VERTEXCOVER.Thisreductionreliesonthenotionofthecomplementofa
grafico.GivenanundirectedgraphGD.VE/,wedefinethecomplementofG
asGD.VE/,whereEDfu/Wu2Vuand.u/62Eg.Inother
words,GisthegraphcontainingexactlythoseedgesthatarenotinG.Figure34.15
showsagraphanditscomplementandillustratesthereductionfromCLIQUEto
VERTEXCOVER.
ThereductionalgorithmtakesasinputaninstancehGkiofthecliqueproblem.
ItcomputesthecomplementG,whichwecaneasilydoinpolynomialtime.los
outputofthereductionalgorithmistheinstancehGjVjkiofthevertexcover
problema.Tocompletetheproof,weshowthatthistransformationisindeeda

Pgina212

34.5NPcompleteproblems 1091

reduction:thegraphGhasacliqueofsizekifandonlyifthegraphGhasavertex
coverofsizejVjk.
SupposethatGhasacliqueV0VwithjV0jDk.WeclaimthatV V0isa
vertexcoverinG.Let.u/beanyedgeinE.Then,.u/62E,whichimplies
thatatleastoneofuordoesnotbelongtoV0,sinceeverypairofverticesinV0is
connectedbyanedgeofE.Equivalently,atleastoneofuorisinVV0,which
meansthatedge.u/iscoveredbyV V0.Since.u/waschosenarbitrarily
fromE,everyedgeofEiscoveredbyavertexinV V0.Hence,thesetV V0,
whichhassizejVjk,formsavertexcoverforG.
Conversely,supposethatGhasavertexcoverV0V,wherejV0jDjVjk.
Then,forallu2V,if.u/2E,thenu2V0or 2V0orboth.El
contrapositiveofthisimplicationisthatforallu2V,ifu62V0and62V0,
then.u/2E.Inotherwords,VV0isaclique,andithassizejVjjV0jDk.

SinceVERTEXCOVERisNPcomplete,wedon'texpecttofindapolynomial
timealgorithmforfindingaminimumsizevertexcover.Section35.1presentsa
polynomialtimeapproximationalgorithm,however,whichproducesapproxi
matesolutionsforthevertexcoverproblem.Thesizeofavertexcoverproduced
bythealgorithmisatmosttwicetheminimumsizeofavertexcover.
Thus,weshouldn'tgiveuphopejustbecauseaproblemisNPcomplete.Nosotros
maybeabletodesignapolynomialtimeapproximationalgorithmthatobtains
nearoptimalsolutions,eventhoughfindinganoptimalsolutionisNPcomplete.
Chapter35givesseveralapproximationalgorithmsforNPcompleteproblems.

34.5.3Thehamiltoniancycleproblem

WenowreturntothehamiltoniancycleproblemdefinedinSection34.2.

Theorem34.13
ThehamiltoniancycleproblemisNPcomplete.

ProofWefirstshowthatHAMCYCLEbelongstoNP.GivenagraphGD
.VE/,ourcertificateisthesequenceofjVjverticesthatmakesupthehamiltonian
ciclo.Theverificationalgorithmchecksthatthissequencecontainseachvertex
inVexactlyonceandthatwiththefirstvertexrepeatedattheend,itformsacycle
inG.Thatis,itchecksthatthereisanedgebetweeneachpairofconsecutive
verticesandbetweenthefirstandlastvertices.Wecanverifythecertificatein
polynomialtime.
WenowprovethatVERTEXCOVERPHAMCYCLE,whichshowsthat
HAMCYCLEisNPcomplete.GivenanundirectedgraphGD.VE/andan

https://translate.googleusercontent.com/translate_f 148/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,

Pgina213

1092 Chapter34NPCompleteness

[u,v,1] [v,u,1] [u,v,1] [v,u,1] [u,v,1] [v,u,1] [u,v,1] [v,u,1]


[u,v,2] [v,u,2]
[u,v,3]
Wuv [v,u,3] Wuv Wuv Wuv
[u,v,4] [v,u,4]
[u,v,5] [v,u,5]
[u,v,6] [v,u,6] [u,v,6] [v,u,6] [u,v,6] [v,u,6] [u,v,6] [v,u,6]
(un) (segundo) (do) (re)

Figure34.16Thewidgetusedinreducingthevertexcoverproblemtothehamiltoniancycleprob
LemAnedge.u/ofgraphGcorrespondstowidgetWuinthegraphG0createdinthereduction.
(a)Thewidget,withindividualverticeslabeled.(b)(d)Theshadedpathsaretheonlypossibleones
throughthewidgetthatincludeallvertices,assumingthattheonlyconnectionsfromthewidgetto
theremainderofG0arethroughverticesu1,u6,u1,andu6.

integerk,weconstructanundirectedgraphG0D.V0E0/thathasahamiltonian
cycleifandonlyifGhasavertexcoverofsizek.
Ourconstructionusesawidget,whichisapieceofagraphthatenforcescertain
properties.Figure34.16(a)showsthewidgetweuse.Foreachedge.u/2E,the
graphG0thatweconstructwillcontainonecopyofthiswidget,whichwedenote
byWu.WedenoteeachvertexinWubyuiorui,where1i6,so
thateachwidgetWucontains12vertices.WidgetWualsocontainsthe14edges
showninFigure34.16(a).
Alongwiththeinternalstructureofthewidget,weenforcethepropertieswe
wantbylimitingtheconnectionsbetweenthewidgetandtheremainderofthe
graphG0thatweconstruct.Inparticular,onlyverticesu1,u6,u1,
andu6willhaveedgesincidentfromoutsideWu.Anyhamiltoniancycle
ofG0musttraversetheedgesofWuinoneofthethreewaysshowninFig
ures34.16(b)(d).Ifthecycleentersthroughvertexu1,itmustexitthrough
vertexu6,anditeithervisitsall12ofthewidget'svertices(Figure34.16(b))
orthesixverticesu1throughu6(Figure34.16(c)).Inthelattercase,
thecyclewillhavetoreenterthewidgettovisitverticesu1throughu6.
Similarly,ifthecycleentersthroughvertexu1,itmustexitthroughver
texu6,anditeithervisitsall12ofthewidget'svertices(Figure34.16(d))or
thesixverticesu1throughu6(Figure34.16(c)).Nootherpathsthrough
thewidgetthatvisitall12verticesarepossible.Inparticular,itisimpossibleto
constructtwovertexdisjointpaths,oneofwhichconnectsu1tou6y
theotherofwhichconnectsu1tou6,suchthattheunionofthetwopaths
containsallofthewidget'svertices.

Pgina214

34.5NPcompleteproblems 1093

W x

(un)
Z Y

s1

s2

https://translate.googleusercontent.com/translate_f 149/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,

(segundo)
[w,x,1] [x,w,1]
[x,y,1] [y,x,1][w,y,1] [y,w,1] [w,z,1] [z,w,1]

Wwx Wxy Wwy Wwz

[w,x,6] [x,w,6]
[x,y,6] [y,x,6][w,y,6] [y,w,6] [w,z,6] [z,w,6]

Figure34.17Reducinganinstanceofthevertexcoverproblemtoaninstanceofthehamiltonian
cycleproblem.(a)AnundirectedgraphGwithavertexcoverofsize2,consistingofthelightly
shadedverticeswandy.(b)TheundirectedgraphG0producedbythereduction,withthehamilto
nianpathcorrespondingtothevertexcovershaded.Thevertexcoverfwygcorrespondstoedges
.s1wx1/and.s2yx1/appearinginthehamiltoniancycle.

TheonlyotherverticesinV0otherthanthoseofwidgetsareselectorvertices
s1s2:::sk.WeuseedgesincidentonselectorverticesinG0toselectthek
verticesofthecoverinG.
Inadditiontotheedgesinwidgets,E0containstwoothertypesofedges,which
Figure34.17shows.First,foreachvertexu2V,weaddedgestojoinpairs
ofwidgetsinordertoformapathcontainingallwidgetscorrespondingtoedges
incidentonuinG.Wearbitrarilyordertheverticesadjacenttoeachvertex
u2Vasu.1/u.2/:::u.degree.u//,wheredegree.u/isthenumberofvertices
adjacenttou.WecreateapathinG0throughallthewidgetscorresponding
toedgesincidentonubyaddingtoE0theedgesf.uu.i/6uu.iC1/1/W
1idegree.u/1g.InFigure34.17,forexample,weordertheverticesad
jacenttowasxy,andsographG0inpart(b)ofthefigureincludestheedges

Pgina215

1094 Chapter34NPCompleteness

.wx6wy1/and.wy6w1/.Foreachvertexu2V,theseedges
inG0fillinapathcontainingallwidgetscorrespondingtoedgesincidentonu
inG.
Theintuitionbehindtheseedgesisthatifwechooseavertexu2Vinthevertex
coverofG,wecanconstructapathfromuu.1/1touu.degree.u//6inG0that
coversallwidgetscorrespondingtoedgesincidentonu.Thatis,foreachofthese
widgets,sayWuu ,thepatheitherincludesall12vertices(ifuisinthevertex
.i/

coverbutu.i/isnot)orjustthesixverticesuu.i/1uu.i/2:::uu.i/6(if
bothuandu.i/areinthevertexcover).
ThefinaltypeofedgeinE0joinsthefirstvertexuu.1/1andthelastvertex
uu.degree.u//6ofeachofthesepathstoeachoftheselectorvertices.Thatis,we
includetheedges
fsjuu.1/1/Wu2Vand1jkg
[fsjuu.degree.u//6/Wu2Vand1jkg:

Next,weshowthatthesizeofG0ispolynomialinthesizeofG,andhencewe
canconstructG0intimepolynomialinthesizeofG.TheverticesofG0arethose
inthewidgets,plustheselectorvertices.With12verticesperwidget,pluskjVj
selectorvertices,wehaveatotalof
jV0jD12jEjCk
12jEjCjVj

VrticesTheedgesofG0arethoseinthewidgets,thosethatgobetweenwidgets,
andthoseconnectingselectorverticestowidgets.Eachwidgetcontains14edges,
totaling14jEjinallwidgets.Foreachvertexu2V,graphG0hasdegree.u/1
edgesgoingbetweenwidgets,sothatsummedoverallverticesinV,
x
.degree.u/1/D2jEjjVj
u2V
edgesgobetweenwidgets.Finally,G0hastwoedgesforeachpairconsistingofa
selectorvertexandavertexofV,totaling2kjVjsuchedges.Thetotalnumberof
edgesofG0istherefore

https://translate.googleusercontent.com/translate_f 150/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
jE0jD.14jEj/C.2jEjjVj/C.2kjVj/
D16jEjC.2k1/jVj
16jEjC.2jVj1/jVj:

NowweshowthatthetransformationfromgraphGtoG0isareduction.Esdecir,
wemustshowthatGhasavertexcoverofsizekifandonlyifG0hasahamiltonian
ciclo.

Pgina216

34.5NPcompleteproblems 1095

SupposethatGD.VE/hasavertexcoverV Vofsizek.Dejar
V Dfu1u2:::ukg.AsFigure34.17shows,weformahamiltoniancy
cleinG
0byincludingthefollowingedges10foreachvertexuj2V.Include
Bordes .uju.i/ .iC1/1/W1idegree.uj/1 ,whichconnectall
j6uju J
widgetscorrespondingtoedgesincidentonuj.Wealsoincludetheedgeswithin
thesewidgetsasFigures34.16(b)(d)show,dependingonwhethertheedgeiscov
eredbyoneortwoverticesinV.Thehamiltoniancyclealsoincludestheedges

fsjujuj.1/
1/W1jkg
[fsjC1uju .degree.u6/W1jk1g
j //
J
[fs1uku.degree.u6/g:
k //
K
ByinspectingFigure34.17,youcanverifythattheseedgesformacycle.Thecycle
startsats1,visitsallwidgetscorrespondingtoedgesincidentonu1,thenvisitss2,
visitsallwidgetscorrespondingtoedgesincidentonu2,andsoon,untilitreturns
tos1.Thecyclevisitseachwidgeteitheronceortwice,dependingonwhetherone
ortwoverticesofVcoveritscorrespondingedge.BecauseVisavertexcover
forG,eachedgeinEisincidentonsomevertexinV,andsothecyclevisitseach
vertexineachwidgetofG0.Becausethecyclealsovisitseveryselectorvertex,it
ishamiltonian.
Conversely,supposethatG0D.V0E0/hasahamiltoniancycleCE0.Nosotros
claimthattheset
VDfu2VW.sjuu.1/1/2Cforsome1jkg (34.4)
isavertexcoverforG.Toseewhy,partitionCintomaximalpathsthatstartat
someselectorvertexsi,traverseanedge.siuu.1/1/forsomeu2V,andend
ataselectorvertexsjwithoutpassingthroughanyotherselectorvertex.Letuscall
eachsuchpathacoverpath.FromhowG0isconstructed,eachcoverpathmust
startatsomesi,taketheedge.siuu.1/1/forsomevertexu2V,passthrough
allthewidgetscorrespondingtoedgesinEincidentonu,andthenendatsome
selectorvertexsj.Werefertothiscoverpathaspu,andbyequation(34.4),we
putuintoV.EachwidgetvisitedbypumustbeWuorWuforsome2V.
Foreachwidgetvisitedbypu,itsverticesarevisitedbyeitheroneortwocover
caminos.Iftheyarevisitedbyonecoverpath,thenedge.u/2EiscoveredinG
byvertexu.Iftwocoverpathsvisitthewidget,thentheothercoverpathmust
bep,whichimpliesthat2V,andedge.u/2Eiscoveredbybothuand.

10Technically,wedefineacycleintermsofverticesratherthanedges(seeSectionB.4).Enel
interestofclarity,weabusenotationhereanddefinethehamiltoniancycleintermsofedges.

Pgina217

1096 Chapter34NPCompleteness

Usted 4 V
1
3 2
1
x W
https://translate.googleusercontent.com/translate_f 151/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
5

Figure34.18Aninstanceofthetravelingsalesmanproblem.Shadededgesrepresentaminimum
costtour,withcost7.

Becauseeachvertexineachwidgetisvisitedbysomecoverpath,weseethateach
edgeinEiscoveredbysomevertexinV.

34.5.4Thetravelingsalesmanproblem
Inthetravelingsalesmanproblem,whichiscloselyrelatedtothehamiltonian
cycleproblem,asalesmanmustvisitncities.Modelingtheproblemasacomplete
graphwithnvertices,wecansaythatthesalesmanwishestomakeatour,or
hamiltoniancycle,visitingeachcityexactlyonceandfinishingatthecityhestarts
de.Thesalesmanincursanonnegativeintegercostcij/totravelfromcityi
tocityj,andthesalesmanwishestomakethetourwhosetotalcostisminimum,
wherethetotalcostisthesumoftheindividualcostsalongtheedgesofthetour.
Forexample,inFigure34.18,aminimumcosttourishuwxui,withcost7.
Theformallanguageforthecorrespondingdecisionproblemis
TSPDfhGckiWGD.VE/isacompletegraph
cisafunctionfromV V!Z
k2Z,and
Ghasatravelingsalesmantourwithcostatmostkg:
Thefollowingtheoremshowsthatafastalgorithmforthetravelingsalesman
problemisunlikelytoexist.

Theorem34.14
ThetravelingsalesmanproblemisNPcomplete.

ProofWefirstshowthatTSPbelongstoNP.Givenaninstanceoftheproblem,
weuseasacertificatethesequenceofnverticesinthetour.Theverification
algorithmchecksthatthissequencecontainseachvertexexactlyonce,sumsupthe
edgecosts,andcheckswhetherthesumisatmostk.Thisprocesscancertainlybe
doneinpolynomialtime.

Pgina218

34.5NPcompleteproblems 1097

ToprovethatTSPisNPhard,weshowthatHAMCYCLEPTSP.Dejar
GD.VE/beaninstanceofHAMCYCLE.WeconstructaninstanceofTSPas
Sigue.WeformthecompletegraphG0D.VE0/,whereE0Dfij/Wij2V
andijg,andwedefinethecostfunctioncby
(
0if.ij/2E
cij/D
1if.ij/62E:

(NotethatbecauseGisundirected,ithasnoselfloops,andsoc./D1forall
vertices2V.)TheinstanceofTSPisthenhG0c0i,whichwecaneasilycreate
inpolynomialtime.
WenowshowthatgraphGhasahamiltoniancycleifandonlyifgraphG0hasa
tourofcostatmost0.SupposethatgraphGhasahamiltoniancycleh.Eachedge
inhbelongstoEandthushascost0inG0.Thus,hisatourinG0withcost0.
Conversely,supposethatgraphG0hasatourh0ofcostatmost0.Sincethecosts
oftheedgesinE0are0and1,thecostoftourh0isexactly0andeachedgeonthe
tourmusthavecost0.Therefore,h0containsonlyedgesinE.Weconcludethath0
isahamiltoniancycleingraphG.

34.5.5Thesubsetsumproblem
WenextconsideranarithmeticNPcompleteproblem.Inthesubsetsumproblem,
wearegivenafinitesetSofpositiveintegersandanintegertargett>0.Weask
whetherthereexistsasubsetS0Swhoseelementssumtot.Porejemplo,
ifSDf127144998343686240927931680817206117705117993g
andtD138457,thenthesubsetS0Df12798343686240917206117705g
isasolution.

https://translate.googleusercontent.com/translate_f 152/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
Asusual,wedefinetheproblemasalanguage: PAG
SUBSETSUMDfhStiWthereexistsasubsetS0SsuchthattD s2S sg: 0

Aswithanyarithmeticproblem,itisimportanttorecallthatourstandardencoding
assumesthattheinputintegersarecodedinbinary.Withthisassumptioninmind,
wecanshowthatthesubsetsumproblemisunlikelytohaveafastalgorithm.

Theorem34.15
ThesubsetsumproblemisNPcomplete.

ProofToshowthatSUBSETSUMisinNP,foraninstancehStioftheproblem,
weletthesubsetS
PAG 0bethecertificate.Averificationalgorithmcancheckwhether

tD
s2S sinpolynomialtime.
0
Wenowshowthat3CNFSATPSUBSETSUM.Givena3CNFformula
overvariablesx1x2:::xnwithclausesC1C2:::Ck,eachcontainingexactly

Page219

1098 Chapter34NPCompleteness

threedistinctliterals,thereductionalgorithmconstructsaninstancehStiofthe
subsetsumproblemsuchthat issatisfiableifandonlyifthereexistsasubset
ofSwhosesumisexactlyt.Withoutlossofgenerality,wemaketwosimplifying
assumptionsabouttheformula.First,noclausecontainsbothavariableandits
negation,forsuchaclauseisautomaticallysatisfiedbyanyassignmentofvalues
tothevariables.Second,eachvariableappearsinatleastoneclause,becauseit
doesnotmatterwhatvalueisassignedtoavariablethatappearsinnoclauses.
ThereductioncreatestwonumbersinsetSforeachvariablexiandtwonumbers
inSforeachclauseCj.Weshallcreatenumbersinbase10,whereeachnumber
containsnCkdigitsandeachdigitcorrespondstoeitheronevariableoroneclause.
Base10(andotherbases,asweshallsee)hasthepropertyweneedofpreventing
carriesfromlowerdigitstohigherdigits.
AsFigure34.19shows,weconstructsetSandtargettasfollows.Welabel
eachdigitpositionbyeitheravariableoraclause.Theleastsignificantkdigitsare
labeledbytheclauses,andthemostsignificantndigitsarelabeledbyvariables.

Thetargetthasa1ineachdigitlabeledbyavariableanda4ineachdigit
labeledbyaclause.
Foreachvariablexi,setScontainstwointegersiand0 i. Eachofiand0 yo
hasa1inthedigitlabeledbyxiand0sintheothervariabledigits.Ifliteralxi
appearsinclauseCj,thenthedigitlabeledbyCjinicontainsa1.Iflit
eral:xiappearsinclauseCj,thenthedigitlabeledbyCjin0 icontainsa1.
Allotherdigitslabeledbyclausesiniand0 iare0.
Alliand0 ivaluesinsetSareunique. Porqu?Forli,nolor0
lvaluescan
equaliand0
iinthemostsignificantndigits.Furthermore,byoursimplifying
assumptionsabove,noiand0
icanbeequalinallkleastsignificantdigits.
Ifiand0 iwereequal,thenxiand:xiwouldhavetoappearinexactlythe
samesetofclauses.Butweassumethatnoclausecontainsbothxiand:xi
andthateitherxior:xiappearsinsomeclause,andsotheremustbesome
clauseCjforwhichiand0
idiffer.
ForeachclauseCj,setScontainstwointegerssjands0
j. Eachofsjands0 jhas
0sinalldigitsotherthantheonelabeledbyCj.Forsj,thereisa1intheCj
digit,andsj0 hasa2inthisdigit.Theseintegersareslackvariables,whichwe
usetogeteachclauselabeleddigitpositiontoaddtothetargetvalueof4.
SimpleinspectionofFigure34.19demonstratesthatallsjands0 jvaluesinS
areuniqueinsetS.
Notethatthegreatestsumofdigitsinanyonedigitpositionis6,whichoccursin
thedigitslabeledbyclauses(three1sfromtheiand0 ivalues,plus1and2from

Pgina220

34.5NPcompleteproblems 1099
https://translate.googleusercontent.com/translate_f 153/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,

x 1 x
2 x3 C1 C C
2 3 C 4

1 Significadodelapalabra
1 0 0 1 0 0 1
0 Significadodelapalabra
1 0 0 0 1 1 0
1
2 Significadodelapalabra
0 1 0 0 0 0 1
0 Significadodelapalabra
0 1 0 1 1 1 0
2
3 Significadodelapalabra
0 0 1 0 0 1 1
0 Significadodelapalabra
0 0 1 1 1 0 0
3
s1 Significadodelapalabra
0 0 0 1 0 0 0
s1
0 Significadodelapalabra
0 0 0 2 0 0 0
s2 Significadodelapalabra
0 0 0 0 1 0 0
s2
0 Significadodelapalabra
0 0 0 0 2 0 0
s3 Significadodelapalabra
0 0 0 0 0 1 0
s3
0 Significadodelapalabra
0 0 0 0 0 2 0
s4 Significadodelapalabra
0 0 0 0 0 0 1
s4
0 Significadodelapalabra
0 0 0 0 0 0 2
T Significadodelapalabra
1 1 1 4 4 4 4

Figure34.19Thereductionof3CNFSATtoSUBSETSUM.Theformulain3CNFis re
C1^C2^C3^C4,whereC1D.x1_:x2_:x3/,C2D.:x1_:x2_:x3/,C3D.:x1_:x2_x3/,
andC4D.x1_x2_x3/.Asatisfyingassignmentofishx1D0x2D0x3D1i.ThesetS
producedbythereductionconsistsofthebase10numbersshownreadingfromtoptobottom,SD
f10010011000110100001101110100111110010002000100200102012g.Thetargett
is1114444.ThesubsetS0Sislightlyshaded,anditcontains
1,02,and 3
0
,correspondingtothe
satisfyingassignment.Italsocontainsslackvariabless
1,s2 1,s0
0,s3,s4,ands
4toachievethetarget
0
valueof4inthedigitslabeledbyC1throughC4.

thesjands0
jvalues).Interpretingthesenumbersinbase10,therefore,nocarries
canoccurfromlowerdigitstohigherdigits.11
Wecanperformthereductioninpolynomialtime.ThesetScontains2nC2k
values,eachofwhichhasnCkdigits,andthetimetoproduceeachdigitispoly
nomialinnCk.ThetargetthasnCkdigits,andthereductionproduceseachin
constanttime.
Wenowshowthatthe3CNFformulaissatisfiableifandonlyifthereexists
asubsetS0Swhosesumist.First,supposethathasasatisfyingassignment.
ForiD12:::n,ifxiD1inthisassignment,thenincludeiinS0.Deotramanera,
include0
i. Inotherwords,weincludeinS0exactlytheiand0 ivaluesthatcor

11Infact,anybaseb,whereb 7,wouldwork.Theinstanceatthebeginningofthissubsectionis
thesetSandtargettinFigure34.19interpretedinbase7,withSlistedinsortedorder.

Pgina221

1100 Chapter34NPCompleteness

respondtoliteralswiththevalue1inthesatisfyingassignment.Havingincluded
eitherior0
i,butnotboth,foralli,andhavingput0inthedigitslabeledby
variablesinallsjandsj0 ,weseethatforeachvariablelabeleddigit,thesumofthe
valuesofS0mustbe1,whichmatchesthosedigitsofthetargett.Becauseeach
clauseissatisfied,theclausecontainssomeliteralwiththevalue1.Therefore,
eachdigitlabeledbyaclausehasatleastone1contributedtoitssumbyaior0 yo
valueinS0.Infact,1,2,or3literalsmaybe1ineachclause,andsoeachclause
labeleddigithasasumof1,2,or3fromtheiand0 ivaluesinS0.InFigure34.19
forexample,literals:x1,:x2,andx3havethevalue1inasatisfyingassignment.
EachofclausesC1andC4containsexactlyoneoftheseliterals,andsotogether0 1,
0
2,and3contribute1tothesuminthedigitsforC1andC4.ClauseC2contains
twooftheseliterals,and0 1, 02,and3contribute2tothesuminthedigitforC2.
ClauseC3containsallthreeoftheseliterals,and0 1, 02,and3contribute3tothe
suminthedigitforC3.Weachievethetargetof4ineachdigitlabeledbyclauseCj
byincludinginS0theappropriatenonemptysubsetofslackvariablesfsjs0 jg. En
Figure34.19,S0includess1,s 10,s20,s3,s4,ands
4. Sincewehavematchedthetarget
0
inalldigitsofthesum,andnocarriescanoccur,thevaluesofS0sumtot.
Now,supposethatthereisasubsetS0Sthatsumstot.ThesubsetS0must
includeexactlyoneofiand0 iforeachiD12:::n,forotherwisethedigits
labeledbyvariableswouldnotsumto1.Ifi2S0,wesetxiD1.Otherwise,
https://translate.googleusercontent.com/translate_f 154/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
i02S0,andwesetxiD0.WeclaimthateveryclauseCj,forjD12:::k,is
satisfiedbythisassignment.Toprovethisclaim,notethattoachieveasumof4in
thedigitlabeledbyCj,thesubsetS0mustincludeatleastoneior0 ivaluethat
hasa1inthedigitlabeledbyCj,sincethecontributionsoftheslackvariablessj
andsj0 togethersumtoatmost3.IfS0includesaithathasa1inCj'sposition,
thentheliteralxiappearsinclauseCj.SincewehavesetxiD1wheni2S0,
clauseCjissatisfied.IfS0includesa0 ithathasa1inthatposition,thenthe
literal:xiappearsinCj.SincewehavesetxiD0when0 i2S0,clauseCjis
againsatisfied.Thus,allclausesofaresatisfied,whichcompletestheproof.

Exercises

34.51
ThesubgraphisomorphismproblemtakestwoundirectedgraphsG1andG2,and
itaskswhetherG1isisomorphictoasubgraphofG2.Showthatthesubgraph
isomorphismproblemisNPcomplete.

34.52
Givenanintegerm nmatrixAandanintegermvectorb,the01integer
programmingproblemaskswhetherthereexistsanintegernvectorxwithele

Pgina222

ProblemsforChapter34 1101

mentsinthesetf01gsuchthatAxb.Provethat01integerprogrammingis
NPcomplete.(Hint:Reducefrom3CNFSAT.)

34.53
Theintegerlinearprogrammingproblemislikethe01integerprogramming
problemgiveninExercise34.52,exceptthatthevaluesofthevectorxmaybe
anyintegersratherthanjust0or1.Assumingthatthe01integerprogramming
problemisNPhard,showthattheintegerlinearprogrammingproblemisNP
complete.

34.54
Showhowtosolvethesubsetsumprobleminpolynomialtimeifthetargetvaluet
isexpressedinunary.

34.55
ThesetpartitionproblemtakesasinputasetSofnumbers.Thequestionis
whetherthenumberscanbepartitionedintotwosetsAandADS
PAG PAG Asuch
ese
x2AxD x2Ax.ShowthatthesetpartitionproblemisNPcomplete.
34.56
ShowthatthehamiltonianpathproblemisNPcomplete.

34.57
Thelongestsimplecycleproblemistheproblemofdeterminingasimplecycle
(norepeatedvertices)ofmaximumlengthinagraph.Formulatearelateddecision
problem,andshowthatthedecisionproblemisNPcomplete.

34.58
Inthehalf3CNFsatisfiabilityproblem,wearegivena3CNFformulawithn
variablesandmclauses,wheremiseven.Wewishtodeterminewhetherthere
existsatruthassignmenttothevariablesof suchthatexactlyhalftheclauses
evaluateto0andexactlyhalftheclausesevaluateto1.Provethatthehalf3CNF
satisfiabilityproblemisNPcomplete.

Problemas

341Independentset
AnindependentsetofagraphGD.VE/isasubsetV0Vofverticessuch
thateachedgeinEisincidentonatmostonevertexinV0.Theindependentset
problemistofindamaximumsizeindependentsetinG.

https://translate.googleusercontent.com/translate_f 155/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,

Pgina223

1102 Chapter34NPCompleteness

a.Formulatearelateddecisionproblemfortheindependentsetproblem,and
provethatitisNPcomplete.(Hint:Reducefromthecliqueproblem.)

segundo.Supposethatyouaregivenablackboxsubroutinetosolvethedecisionprob
lemyoudefinedinpart(a).Giveanalgorithmtofindanindependentsetofmax
imumsize.TherunningtimeofyouralgorithmshouldbepolynomialinjVj
andjEj,countingqueriestotheblackboxasasinglestep.

AlthoughtheindependentsetdecisionproblemisNPcomplete,certainspecial
casesarepolynomialtimesolvable.

do.Giveanefficientalgorithmtosolvetheindependentsetproblemwheneachver
texinGhasdegree2.Analyzetherunningtime,andprovethatyouralgorithm
workscorrectly.

re.GiveanefficientalgorithmtosolvetheindependentsetproblemwhenGis
bipartite.Analyzetherunningtime,andprovethatyouralgorithmworkscor
Directamente.(Hint:UsetheresultsofSection26.3.)

342BonnieandClyde
BonnieandClydehavejustrobbedabank.Theyhaveabagofmoneyandwant
todivideitup.Foreachofthefollowingscenarios,eithergiveapolynomialtime
algorithm,orprovethattheproblemisNPcomplete.Theinputineachcaseisa
listofthenitemsinthebag,alongwiththevalueofeach.

a.Thebagcontainsncoins,butonly2differentdenominations:somecoinsare
worthxdollars,andsomeareworthydollars.BonnieandClydewishtodivide
themoneyexactlyevenly.

segundo.Thebagcontainsncoins,withanarbitrarynumberofdifferentdenominations,
buteachdenominationisanonnegativeintegerpowerof2,ie,thepossible
denominationsare1dollar,2dollars,4dollars,etc.BonnieandClydewishto
dividethemoneyexactlyevenly.

do.Thebagcontainsnchecks,whichare,inanamazingcoincidence,madeoutto
BonnieorClyde.Theywishtodividethecheckssothattheyeachgetthe
exactsameamountofmoney.

re.Thebagcontainsnchecksasinpart(c),butthistimeBonnieandClydeare
willingtoacceptasplitinwhichthedifferenceisnolargerthan100dollars.

Page224

ProblemsforChapter34 1103

343Graphcoloring
Mapmakerstrytouseasfewcolorsaspossiblewhencoloringcountriesonamap,
aslongasnotwocountriesthatshareaborderhavethesamecolor.Wecanmodel
thisproblemwithanundirectedgraphGD.VE/inwhicheachvertexrepre
sentsacountryandverticeswhoserespectivecountriesshareaborderareadjacent.
Then,akcoloringisafunctioncWV!f12:::kgsuchthatcu/c./for
everyedge.u/2E.Inotherwords,thenumbers12:::krepresentthekcol
ors,andadjacentverticesmusthavedifferentcolors.Thegraphcoloringproblem
istodeterminetheminimumnumberofcolorsneededtocoloragivengraph.

a.Giveanefficientalgorithmtodeterminea2coloringofagraph,ifoneexists.

segundo.Castthegraphcoloringproblemasadecisionproblem.Showthatyourdeci
sionproblemissolvableinpolynomialtimeifandonlyifthegraphcoloring
https://translate.googleusercontent.com/translate_f 156/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
problemissolvableinpolynomialtime.

do.Letthelanguage3COLORbethesetofgraphsthatcanbe3colored.Mostrar
thatif3COLORisNPcomplete,thenyourdecisionproblemfrompart(b)is
NPcomplete.

Toprovethat3COLORisNPcomplete,weuseareductionfrom3CNFSAT.
Givenaformulaofmclausesonnvariablesx1,x2,...,xn,weconstructagraph
GD.VE/asfollows.ThesetVconsistsofavertexforeachvariable,avertex
forthenegationofeachvariable,5verticesforeachclause,and3specialvertices:
TRUE, FALSE,andRED.Theedgesofthegraphareoftwotypes:literaledges
thatareindependentoftheclausesandclauseedgesthatdependontheclauses.
Theliteraledgesformatriangleonthespecialverticesandalsoformatriangleon
xi,:xi,andREDforiD12:::norte.

re.Arguethatinany3coloringcofagraphcontainingtheliteraledges,exactly
oneofavariableanditsnegationiscoloredc.TRUE/andtheotheriscolored
do.FALSE/.Arguethatforanytruthassignmentfor,thereexistsa3coloring
ofthegraphcontainingjusttheliteraledges.

ThewidgetshowninFigure34.20helpstoenforcetheconditioncorrespondingto
aclause.x_y_/.Eachclauserequiresauniquecopyofthe5verticesthatare
heavilyshadedinthefiguretheyconnectasshowntotheliteralsoftheclauseand
thespecialvertexTRUE.

mi.Arguethatifeachofx,y,andiscoloredc.TRUE/orc.FALSE/,thenthe
widgetis3colorableifandonlyifatleastoneofx,y,oriscoloredc.TRUE/.

f.Completetheproofthat3COLORisNPcomplete.

Pgina225

1104 Chapter34NPCompleteness

Y
CIERTO

Figure34.20Thewidgetcorrespondingtoaclause.x_y_/,usedinProblem343.

344Schedulingwithprofitsanddeadlines
Supposethatwehaveonemachineandasetofntasksa1a2:::an,eachof
whichrequirestimeonthemachine.Eachtaskajrequirestjtimeunitsonthe
machine(itsprocessingtime),yieldsaprofitofpj,andhasadeadlinedj.El
machinecanprocessonlyonetaskatatime,andtaskajmustrunwithoutinter
ruptionfortjconsecutivetimeunits.Ifwecompletetaskajbyitsdeadlinedj,we
receiveaprofitpj,butifwecompleteitafteritsdeadline,wereceivenoprofit.Como
anoptimizationproblem,wearegiventheprocessingtimes,profits,anddeadlines
forasetofntasks,andwewishtofindaschedulethatcompletesallthetasksand
returnsthegreatestamountofprofit.Theprocessingtimes,profits,anddeadlines
areallnonnegativenumbers.

a.Statethisproblemasadecisionproblem.

segundo.ShowthatthedecisionproblemisNPcomplete.

do.Giveapolynomialtimealgorithmforthedecisionproblem,assumingthatall
processingtimesareintegersfrom1ton.(Hint:Usedynamicprogramming.)

re.Giveapolynomialtimealgorithmfortheoptimizationproblem,assumingthat
allprocessingtimesareintegersfrom1ton.

Chapternotes

https://translate.googleusercontent.com/translate_f 157/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,

ThebookbyGareyandJohnson[129]providesawonderfulguidetoNPcomplete
ness,discussingthetheoryatlengthandprovidingacatalogueofmanyproblems
thatwereknowntobeNPcompletein1979.TheproofofTheorem34.13is
adaptedfromtheirbook,andthelistofNPcompleteproblemdomainsatthebegin
ningofSection34.5isdrawnfromtheirtableofcontents.Johnsonwroteaseries

Pgina226

NotesforChapter34 1105

of23columnsintheJournalofAlgorithmsbetween1981and1992reportingnew
developmentsinNPcompleteness.Hopcroft,Motwani,andUllman[177],Lewis
andPapadimitriou[236],Papadimitriou[270],andSipser[317]havegoodtreat
mentsofNPcompletenessinthecontextofcomplexitytheory.NPcompleteness
andseveralreductionsalsoappearinbooksbyAho,Hopcroft,andUllman[5]
Dasgupta,Papadimitriou,andVazirani[82]JohnsonbaughandSchaefer[193]
andKleinbergandTardos[208].
TheclassPwasintroducedin1964byCobham[72]and,independently,in1965
byEdmonds[100],whoalsointroducedtheclassNPandconjecturedthatPNP.
ThenotionofNPcompletenesswasproposedin1971byCook[75],whogave
thefirstNPcompletenessproofsforformulasatisfiabilityand3CNFsatisfiabil
ity.Levin[234]independentlydiscoveredthenotion,givinganNPcompleteness
proofforatilingproblem.Karp[199]introducedthemethodologyofreductions
in1972anddemonstratedtherichvarietyofNPcompleteproblems.Karp'spa
perincludedtheoriginalNPcompletenessproofsoftheclique,vertexcover,and
hamiltoniancycleproblems.Sincethen,thousandsofproblemshavebeenproven
tobeNPcompletebymanyresearchers.InatalkatameetingcelebratingKarps
60thbirthdayin1995,Papadimitriouremarked,about6000paperseachyearhave
theterm'NPcomplete'ontheirtitle,abstract,orlistofkeywords.Thisismore
thaneachoftheterms'compiler,''database,''expert,''neuralnetwork,'or'oper
atingsystem.'"
Recentworkincomplexitytheoryhasshedlightonthecomplexityofcomputing
approximatesolutions.ThisworkgivesanewdefinitionofNPusingprobabilis
ticallycheckableproofs.Thisnewdefinitionimpliesthatforproblemssuchas
clique,vertexcover,thetravelingsalesmanproblemwiththetriangleinequality,
andmanyothers,computinggoodapproximatesolutionsisNPhardandhenceno
easierthancomputingoptimalsolutions.Anintroductiontothisareacanbefound
inArora'sthesis[20]achapterbyAroraandLundinHochbaum[172]asurvey
articlebyArora[21]abookeditedbyMayr,Prmel,andSteger[246]Yun
surveyarticlebyJohnson[191].

Pgina227

35 ApproximationAlgorithms

https://translate.googleusercontent.com/translate_f 158/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,

ManyproblemsofpracticalsignificanceareNPcomplete,yettheyaretooimpor
tanttoabandonmerelybecausewedon'tknowhowtofindanoptimalsolutionin
polynomialtime.EvenifaproblemisNPcomplete,theremaybehope.Wehaveat
leastthreewaystogetaroundNPcompleteness.First,iftheactualinputsaresmall,
analgorithmwithexponentialrunningtimemaybeperfectlysatisfactory.Second,
wemaybeabletoisolateimportantspecialcasesthatwecansolveinpolynomial
hora.Third,wemightcomeupwithapproachestofindnearoptimalsolutionsin
polynomialtime(eitherintheworstcaseortheexpectedcase).Inpractice,near
optimalityisoftengoodenough.Wecallanalgorithmthatreturnsnearoptimal
solutionsanapproximationalgorithm.Thischapterpresentspolynomialtimeap
proximationalgorithmsforseveralNPcompleteproblems.

Performanceratiosforapproximationalgorithms
Supposethatweareworkingonanoptimizationprobleminwhicheachpotential
solutionhasapositivecost,andwewishtofindanearoptimalsolution.Dependiente
ontheproblem,wemaydefineanoptimalsolutionasonewithmaximumpossi
blecostoronewithminimumpossiblecostthatis,theproblemmaybeeithera
maximizationoraminimizationproblem.
Wesaythatanalgorithmforaproblemhasanapproximationratioof.n/if,
foranyinputofsizen,thecostCofthesolutionproducedbythealgorithmis
withinafactorof.n/ofthecostCofanoptimalsolution:
UN UN
do do
Mximo .n/: (35.1)
do do
Ifanalgorithmachievesanapproximationratioof.n/,wecallita.n/approx
imationalgorithm.Thedefinitionsoftheapproximationratioandofa.n/
approximationalgorithmapplytobothminimizationandmaximizationproblems.
Foramaximizationproblem,0<CC,andtheratioC=Cgivesthefactor
bywhichthecostofanoptimalsolutionislargerthanthecostoftheapproximate

Pgina228

Chapter35ApproximationAlgorithms 1107

solucin.Similarly,foraminimizationproblem,0<CC,andtheratioC=C
givesthefactorbywhichthecostoftheapproximatesolutionislargerthanthe
costofanoptimalsolution.Becauseweassumethatallsolutionshavepositive
cost,theseratiosarealwayswelldefined.Theapproximationratioofanapprox
imationalgorithmisneverlessthan1,sinceC=C1impliesC=C 1.
Therefore,a1approximationalgorithm1producesanoptimalsolution,andanap
proximationalgorithmwithalargeapproximationratiomayreturnasolutionthat
ismuchworsethanoptimal.
Formanyproblems,wehavepolynomialtimeapproximationalgorithmswith
smallconstantapproximationratios,althoughforotherproblems,thebestknown
polynomialtimeapproximationalgorithmshaveapproximationratiosthatgrow
asfunctionsoftheinputsizen.Anexampleofsuchaproblemisthesetcover
problempresentedinSection35.3.
SomeNPcompleteproblemsallowpolynomialtimeapproximationalgorithms
thatcanachieveincreasinglybetterapproximationratiosbyusingmoreandmore
Tiempodecomputacin.Thatis,wecantradecomputationtimeforthequalityofthe
approximation.AnexampleisthesubsetsumproblemstudiedinSection35.5.
Thissituationisimportantenoughtodeserveanameofitsown.
Anapproximationschemeforanoptimizationproblemisanapproximational
gorithmthattakesasinputnotonlyaninstanceoftheproblem,butalsoavalue
>0suchthatforanyfixed,theschemeisa.1C/approximationalgorithm.
Wesaythatanapproximationschemeisapolynomialtimeapproximationscheme
ifforanyfixed>0,theschemerunsintimepolynomialinthesizenofitsinput
ejemplo.
Therunningtimeofapolynomialtimeapproximationschemecanincreasevery
rapidlyasdecreases.Forexample,therunningtimeofapolynomialtimeap
proximationschememightbeOn2= /.Ideally,ifdecreasesbyaconstantfactor,
therunningtimetoachievethedesiredapproximationshouldnotincreasebymore

https://translate.googleusercontent.com/translate_f 159/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
thanaconstantfactor(thoughnotnecessarilythesameconstantfactorbywhich
decreased).
Wesaythatanapproximationschemeisafullypolynomialtimeapproximation
schemeifitisanapproximationschemeanditsrunningtimeispolynomialin
both1=andthesizenoftheinputinstance.Forexample,theschememighthave
arunningtimeofO..1=/2n3/.Withsuchascheme,anyconstantfactordecrease
incomeswithacorrespondingconstantfactorincreaseintherunningtime.

1Whentheapproximationratioisindependentofn,weusethetermsapproximationratioofand
approximationalgorithm,indicatingnodependenceonn.

Page229

1108 Chapter35ApproximationAlgorithms

Chapteroutline
Thefirstfoursectionsofthischapterpresentsomeexamplesofpolynomialtime
approximationalgorithmsforNPcompleteproblems,andthefifthsectionpresents
afullypolynomialtimeapproximationscheme.Section35.1beginswithastudy
ofthevertexcoverproblem,anNPcompleteminimizationproblemthathasan
approximationalgorithmwithanapproximationratioof2.Section35.2presents
anapproximationalgorithmwithanapproximationratioof2forthecaseofthe
travelingsalesmanprobleminwhichthecostfunctionsatisfiesthetrianglein
igualdad.Italsoshowsthatwithoutthetriangleinequality,foranyconstant 1,
aapproximationalgorithmcannotexistunlessPDNP.InSection35.3,we
showhowtouseagreedymethodasaneffectiveapproximationalgorithmforthe
setcoveringproblem,obtainingacoveringwhosecostisatworstalogarithmic
factorlargerthantheoptimalcost.Section35.4presentstwomoreapproximation
AlgoritmosFirstwestudytheoptimizationversionof3CNFsatisfiabilityand
giveasimplerandomizedalgorithmthatproducesasolutionwithanexpectedap
proximationratioof8=7.Thenweexamineaweightedvariantofthevertexcover
problemandshowhowtouselinearprogrammingtodevelopa2approximation
algoritmo.Finally,Section35.5presentsafullypolynomialtimeapproximation
schemeforthesubsetsumproblem.

35.1Thevertexcoverproblem

Section34.5.2definedthevertexcoverproblemandproveditNPcomplete.Recordar
thatavertexcoverofanundirectedgraphGD.VE/isasubsetV0Vsuch
thatif.u/isanedgeofG,theneitheru2V0or2V0(orboth).Thesizeofa
vertexcoveristhenumberofverticesinit.
Thevertexcoverproblemistofindavertexcoverofminimumsizeinagiven
undirectedgraph.Wecallsuchavertexcoveranoptimalvertexcover.Thisprob
lemistheoptimizationversionofanNPcompletedecisionproblem.
Eventhoughwedon'tknowhowtofindanoptimalvertexcoverinagraphG
inpolynomialtime,wecanefficientlyfindavertexcoverthatisnearoptimal.
ThefollowingapproximationalgorithmtakesasinputanundirectedgraphGand
returnsavertexcoverwhosesizeisguaranteedtobenomorethantwicethesize
ofanoptimalvertexcover.

Pgina230

35.1Thevertexcoverproblem 1109

https://translate.googleusercontent.com/translate_f 160/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
segundo do re segundo do re

un mi F gramo un mi F gramo
(un) (segundo)

segundo do re segundo do re

un mi F gramo un mi F gramo
(do) (re)

segundo do re segundo do re

un mi F gramo un mi F gramo
(mi) (f)

Figure35.1TheoperationofAPPROXVERTEXCOVER.(a)TheinputgraphG,whichhas7
verticesand8edges.(b)Theedge.bc/,shownheavy,isthefirstedgechosenbyAPPROXVERTEX
COVER.Verticesbandc,shownlightlyshaded,areaddedtothesetCcontainingthevertexcover
beingcreated.Edges.ab/,.ce/,and.cd/,showndashed,areremovedsincetheyarenowcovered
bysomevertexinC.(c)Edge.ef/ischosenverticeseandfareaddedtoC.(d)Edge.dg/
ischosenverticesdandgareaddedtoC.(e)ThesetC,whichisthevertexcoverproducedby
APPROXVERTEXCOVER,containsthesixverticesbcdefg.(f)Theoptimalvertexcoverfor
thisproblemcontainsonlythreevertices:b,d,ande.

APPROXVERTEXCOVER.G/
1CD
2E0DG:E
3whileE0
4 let.u/beanarbitraryedgeofE0
5 CDC[fugramo
6 removefromE0everyedgeincidentoneitheruor
7returnC

Figure35.1illustrateshowAPPROXVERTEXCOVERoperatesonanexample
grafico.ThevariableCcontainsthevertexcoverbeingconstructed.Line1ini
tializesCtotheemptyset.Line2setsE0tobeacopyoftheedgesetG:Eof
thegraph.Theloopoflines36repeatedlypicksanedge.u/fromE0,addsits

Pgina231

1110 Chapter35ApproximationAlgorithms

endpointsuandtoC,anddeletesalledgesinE0thatarecoveredbyeitheru
or.Finally,line7returnsthevertexcoverC.Therunningtimeofthisalgorithm
isOVCE/,usingadjacencyliststorepresentE0.

Theorem35.1
APPROXVERTEXCOVERisapolynomialtime2approximationalgorithm.

ProofWehavealreadyshownthatAPPROXVERTEXCOVERrunsinpolyno
mialtime.
ThesetCofverticesthatisreturnedbyAPPROXVERTEXCOVERisavertex
cover,sincethealgorithmloopsuntileveryedgeinG:Ehasbeencoveredbysome
vertexinC.
ToseethatAPPROXVERTEXCOVERreturnsavertexcoverthatisatmosttwice
thesizeofanoptimalcover,letAdenotethesetofedgesthatline4ofAPPROX
VERTEXCOVERpicked.InordertocovertheedgesinA,anyvertexcoverin
particular,anoptimalcoverCmustincludeatleastoneendpointofeachedge
inA.NotwoedgesinAshareanendpoint,sinceonceanedgeispickedinline4,
allotheredgesthatareincidentonitsendpointsaredeletedfromE0inline6.Thus,
notwoedgesinAarecoveredbythesamevertexfromC,andwehavethelower
bound
jCjjAj (35.2)

https://translate.googleusercontent.com/translate_f 161/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
onthesizeofanoptimalvertexcover.Eachexecutionofline4picksanedgefor
whichneitherofitsendpointsisalreadyinC,yieldinganupperbound(anexact
upperbound,infact)onthesizeofthevertexcoverreturned:
jCjD2jAj: (35.3)
Combiningequations(35.2)and(35.3),weobtain
jCjD2jAj
2jCj
therebyprovingthetheorem.

Letusreflectonthisproof.Atfirst,youmightwonderhowwecanpossibly
provethatthesizeofthevertexcoverreturnedbyAPPROXVERTEXCOVERisat
mosttwicethesizeofanoptimalvertexcover,whenwedonotevenknowthesize
ofanoptimalvertexcover.Insteadofrequiringthatweknowtheexactsizeofan
optimalvertexcover,werelyonalowerboundonthesize.AsExercise35.12asks
youtoshow,thesetAofedgesthatline4ofAPPROXVERTEXCOVERselectsis
actuallyamaximalmatchinginthegraphG.(Amaximalmatchingisamatching
thatisnotapropersubsetofanyothermatching.)Thesizeofamaximalmatching

Pgina232

35.2Thetravelingsalesmanproblem 1111

is,aswearguedintheproofofTheorem35.1,alowerboundonthesizeofan
optimalvertexcover.Thealgorithmreturnsavertexcoverwhosesizeisatmost
twicethesizeofthemaximalmatchingA.Byrelatingthesizeofthesolution
returnedtothelowerbound,weobtainourapproximationratio.Wewillusethis
methodologyinlatersectionsaswell.

Exercises

35.11
GiveanexampleofagraphforwhichAPPROXVERTEXCOVERalwaysyieldsa
suboptimalsolution.

35.12
Provethatthesetofedgespickedinline4ofAPPROXVERTEXCOVERformsa
maximalmatchinginthegraphG.

35.13?
ProfessorBndchenproposesthefollowingheuristictosolvethevertexcover
problema.Repeatedlyselectavertexofhighestdegree,andremoveallofitsin
cidentedges.Giveanexampletoshowthattheprofessor'sheuristicdoesnothave
anapproximationratioof2.(Hint:Tryabipartitegraphwithverticesofuniform
degreeontheleftandverticesofvaryingdegreeontheright.)

35.14
Giveanefficientgreedyalgorithmthatfindsanoptimalvertexcoverforatreein
lineartime.

35.15
FromtheproofofTheorem34.12,weknowthatthevertexcoverproblemandthe
NPcompletecliqueproblemarecomplementaryinthesensethatanoptimalvertex
coveristhecomplementofamaximumsizecliqueinthecomplementgraph.Hace
thisrelationshipimplythatthereisapolynomialtimeapproximationalgorithm
withaconstantapproximationratioforthecliqueproblem?Justificaturespuesta.

35.2Thetravelingsalesmanproblem

InthetravelingsalesmanproblemintroducedinSection34.5.4,wearegivena
completeundirectedgraphGD.VE/thathasanonnegativeintegercostcu/
associatedwitheachedge.u/2E,andwemustfindahamiltoniancycle(a
tour)ofGwithminimumcost.Asanextensionofournotation,letcA/denote
thetotalcostoftheedgesinthesubsetAE:

https://translate.googleusercontent.com/translate_f 162/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,

Pgina233

1112 Chapter35ApproximationAlgorithms
x
cA/D cu/:
.u/2A
Inmanypracticalsituations,theleastcostlywaytogofromaplaceutoaplacew
istogodirectly,withnointermediatesteps.Putanotherway,cuttingoutaninter
mediatestopneverincreasesthecost.Weformalizethisnotionbysayingthatthe
costfunctioncsatisfiesthetriangleinequalityif,forallverticesuw2V,
cuw/cu/Cc.w/:
Thetriangleinequalityseemsasthoughitshouldnaturallyhold,anditisau
tomaticallysatisfiedinseveralapplications.Forexample,iftheverticesofthe
grapharepointsintheplaneandthecostoftravelingbetweentwoverticesisthe
ordinaryeuclideandistancebetweenthem,thenthetriangleinequalityissatisfied.
Furthermore,manycostfunctionsotherthaneuclideandistancesatisfythetriangle
desigualdad.
AsExercise35.22shows,thetravelingsalesmanproblemisNPcompleteeven
ifwerequirethatthecostfunctionsatisfythetriangleinequality.Thus,weshould
notexpecttofindapolynomialtimealgorithmforsolvingthisproblemexactly.
Instead,welookforgoodapproximationalgorithms.
InSection35.2.1,weexaminea2approximationalgorithmforthetraveling
salesmanproblemwiththetriangleinequality.InSection35.2.2,weshowthat
withoutthetriangleinequality,apolynomialtimeapproximationalgorithmwitha
constantapproximationratiodoesnotexistunlessPDNP.

35.2.1Thetravelingsalesmanproblemwiththetriangleinequality
Applyingthemethodologyoftheprevioussection,weshallfirstcomputeastruc
tureaminimumspanningtreewhoseweightgivesalowerboundonthelength
ofanoptimaltravelingsalesmantour.Weshallthenusetheminimumspanning
treetocreateatourwhosecostisnomorethantwicethatoftheminimumspanning
tree'sweight,aslongasthecostfunctionsatisfiesthetriangleinequality.Thefol
lowingalgorithmimplementsthisapproach,callingtheminimumspanningtree
algorithmMSTPRIMfromSection23.2asasubroutine.TheparameterGisa
completeundirectedgraph,andthecostfunctioncsatisfiesthetriangleinequality.

APPROXTSPTOUR.Gc/
1selectavertexr2G:Vtobearootvertex
2computeaminimumspanningtreeTforGfromrootr
usingMSTPRIM.Gcr/
3letHbealistofvertices,orderedaccordingtowhentheyarefirstvisited
inapreordertreewalkofT
4returnthehamiltoniancycleH

Pgina234

35.2Thetravelingsalesmanproblem 1113

un re un re un re
mi mi mi
segundo F gramo segundo F gramo segundo F gramo
do do do
marido marido marido
(un) (segundo) (do)

un re un re
mi mi
segundo F gramo segundo F gramo

https://translate.googleusercontent.com/translate_f 163/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
do do
marido marido
(re) (mi)

Figure35.2TheoperationofAPPROXTSPTOUR.(a)Acompleteundirectedgraph.Verticeslie
onintersectionsofintegergridlines.Forexample,fisoneunittotherightandtwounitsupfromh.
Thecostfunctionbetweentwopointsistheordinaryeuclideandistance.(b)Aminimumspanning
treeTofthecompletegraph,ascomputedbyMSTPRIM.Vertexaistherootvertex.Onlyedges
intheminimumspanningtreeareshown.Theverticeshappentobelabeledinsuchawaythatthey
areaddedtothemaintreebyMSTPRIMinalphabeticalorder.(c)AwalkofT,startingata.UN
fullwalkofthetreevisitstheverticesintheorderasegundocsegundomaridosegundoadmifmi
walkofTlistsavertexjustwhenitisfirstencountered,asindicatedbythedotnexttoeachvertex,
gramomida.Apreorder
yieldingtheorderingasegundocmaridodmifgramo.(d)Atourobtainedbyvisitingtheverticesinthe
givenbythepreorderwalk,whichisthetourHreturnedbyA
order PPROXTSPTOUR.Itstotalcost
isapproximately19:074.(e)AnoptimaltourHfortheoriginalcompletegraph.Itstotalcostis
approximately14:715.

RecallfromSection12.1thatapreordertreewalkrecursivelyvisitseveryvertex
inthetree,listingavertexwhenitisfirstencountered,beforevisitinganyofits
nios.
Figure35.2illustratestheoperationofAPPROXTSPTOUR.Part(a)ofthefig
ureshowsacompleteundirectedgraph,andpart(b)showstheminimumspanning
treeTgrownfromrootvertexabyMSTPRIM.Part(c)showshowapreorder
walkofTvisitsthevertices,andpart(d)displaysthecorrespondingtour,whichis
thetourreturnedbyAPPROXTSPTOUR.Part(e)displaysanoptimaltour,which
isabout23%shorter.

Pgina235

1114 Chapter35ApproximationAlgorithms

ByExercise23.22,evenwithasimpleimplementationofMSTPRIM,therun
ningtimeofAPPROXTSPTOURis.V2/.Wenowshowthatifthecostfunction
foraninstanceofthetravelingsalesmanproblemsatisfiesthetriangleinequality,
thenAPPROXTSPTOURreturnsatourwhosecostisnotmorethantwicethecost
ofanoptimaltour.

Theorem35.2
APPROXTSPTOURisapolynomialtime2approximationalgorithmforthe
travelingsalesmanproblemwiththetriangleinequality.

ProofWehavealreadyseenthatAPPROXTSPTOURrunsinpolynomialtime.
LetHdenoteanoptimaltourforthegivensetofvertices.Weobtainaspanning
treebydeletinganyedgefromatour,andeachedgecostisnonnegative.Porlotanto,
theweightoftheminimumspanningtreeTcomputedinline2ofAPPROXTSP
TOURprovidesalowerboundonthecostofanoptimaltour:
cT/cH/: (35.4)
AfullwalkofTliststheverticeswhentheyarefirstvisitedandalsowhenever
theyarereturnedtoafteravisittoasubtree.LetuscallthisfullwalkW.Thefull
walkofourexamplegivestheorder
asegundocsegundomaridosegundoadmifmigramomida:
SincethefullwalktraverseseveryedgeofTexactlytwice,wehave(extending
ourdefinitionofthecostcinthenaturalmannertohandlemultisetsofedges)
cW/D2c.T/: (35,5)
Inequality(35.4)andequation(35.5)implythat
cW/2c.H/ (35.6)
andsothecostofWiswithinafactorof2ofthecostofanoptimaltour.
Unfortunately,thefullwalkWisgenerallynotatour,sinceitvisitssomever
ticesmorethanonce.Bythetriangleinequality,however,wecandeleteavisitto
anyvertexfromWandthecostdoesnotincrease.(IfwedeleteavertexfromW
betweenvisitstouandw,theresultingorderingspecifiesgoingdirectlyfromu
tow.)Byrepeatedlyapplyingthisoperation,wecanremovefromWallbutthe
firstvisittoeachvertex.Inourexample,thisleavestheordering
asegundocmaridodmifg:
ThisorderingisthesameasthatobtainedbyapreorderwalkofthetreeT.LetH
https://translate.googleusercontent.com/translate_f 164/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
bethecyclecorrespondingtothispreorderwalk.Itisahamiltoniancycle,sinceev

Pgina236

35.2Thetravelingsalesmanproblem 1115

eryvertexisvisitedexactlyonce,andinfactitisthecyclecomputedbyAPPROX
TSPTOUR.SinceHisobtainedbydeletingverticesfromthefullwalkW,we
tener
cH/cW/: (35.7)
Combininginequalities(35.6)and(35.7)givescH/2c.H/,whichcompletes
theproof.

InspiteoftheniceapproximationratioprovidedbyTheorem35.2,APPROX
TSPTOURisusuallynotthebestpracticalchoiceforthisproblem.Thereareother
approximationalgorithmsthattypicallyperformmuchbetterinpractice.(Seethe
referencesattheendofthischapter.)

35.2.2Thegeneraltravelingsalesmanproblem

Ifwedroptheassumptionthatthecostfunctioncsatisfiesthetriangleinequality,
thenwecannotfindgoodapproximatetoursinpolynomialtimeunlessPDNP.

Theorem35.3
IfPNP,thenforanyconstant 1,thereisnopolynomialtimeapproximation
algorithmwithapproximationratioforthegeneraltravelingsalesmanproblem.

ProofTheproofisbycontradiction.Supposetothecontrarythatforsomenum
Ber 1,thereisapolynomialtimeapproximationalgorithmAwithapprox
imationratio.Withoutlossofgenerality,weassumethat isaninteger,by
roundingitupifnecessary.WeshallthenshowhowtouseAtosolveinstances
ofthehamiltoniancycleproblem(definedinSection34.2)inpolynomialtime.
SinceTheorem34.13tellsusthatthehamiltoniancycleproblemisNPcomplete,
Theorem34.4impliesthatifwecansolveitinpolynomialtime,thenPDNP.
LetGD.VE/beaninstanceofthehamiltoniancycleproblem.Deseamos
determineefficientlywhetherGcontainsahamiltoniancyclebymakinguseof
thehypothesizedapproximationalgorithmA.WeturnGintoaninstanceofthe
travelingsalesmanproblemasfollows.LetG0D.VE0/bethecompletegraph
onVesdecir,
E0Dfu/Wu2Vandug:
AssignanintegercosttoeachedgeinE
( 0asfollows:

1 if.u/2E
cu/D
jVjC1otherwise:
WecancreaterepresentationsofG0andcfromarepresentationofGintimepoly
nomialinjVjandjEj.

Pgina237

1116 Chapter35ApproximationAlgorithms

Now,considerthetravelingsalesmanproblem.G0c/.IftheoriginalgraphG
hasahamiltoniancycleH,thenthecostfunctioncassignstoeachedgeofHa
costof1,andso.G0c/containsatourofcostjVj.Ontheotherhand,ifGdoes
notcontainahamiltoniancycle,thenanytourofG0mustusesomeedgenotinE.
ButanytourthatusesanedgenotinEhasacostofatleast
.jVjC1/C.jVj1/D jVjCjVj
>jVj:

https://translate.googleusercontent.com/translate_f 165/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
BecauseedgesnotinGaresocostly,thereisagapofatleastjVjbetweenthecost
ofatourthatisahamiltoniancycleinG(costjVj)andthecostofanyothertour
(costatleastjVjCjVj).Therefore,thecostofatourthatisnotahamiltonian
cycleinGisatleastafactorofC1greaterthanthecostofatourthatisa
hamiltoniancycleinG.
Now,supposethatweapplytheapproximationalgorithmAtothetraveling
salesmanproblem.G0c/.BecauseAisguaranteedtoreturnatourofcostno
morethantimesthecostofanoptimaltour,ifGcontainsahamiltoniancycle,
thenAmustreturnit.IfGhasnohamiltoniancycle,thenAreturnsatourofcost
morethanjVj.Therefore,wecanuseAtosolvethehamiltoniancycleproblem
inpolynomialtime.

TheproofofTheorem35.3servesasanexampleofageneraltechniquefor
provingthatwecannotapproximateaproblemverywell.Supposethatgivenan
NPhardproblemX,wecanproduceinpolynomialtimeaminimizationprob
lemYsuchthatyesinstancesofXcorrespondtoinstancesofYwithvalueat
mostk(forsomek),butthatnoinstancesofXcorrespondtoinstancesofY
withvaluegreaterthank.Then,wehaveshownthat,unlessPDNP,thereisno
polynomialtimeapproximationalgorithmforproblemY.

Exercises

35.21
SupposethatacompleteundirectedgraphGD.VE/withatleast3verticeshas
acostfunctioncthatsatisfiesthetriangleinequality.Provethatcu/ 0forall
u2V.

35.22
Showhowinpolynomialtimewecantransformoneinstanceofthetraveling
salesmanproblemintoanotherinstancewhosecostfunctionsatisfiesthetriangle
desigualdad.Thetwoinstancesmusthavethesamesetofoptimaltours.Explique
whysuchapolynomialtimetransformationdoesnotcontradictTheorem35.3,as
sumingthatPNP.

Pgina238

35.3Thesetcoveringproblem 1117

35.23
Considerthefollowingclosestpointheuristicforbuildinganapproximatetrav
elingsalesmantourwhosecostfunctionsatisfiesthetriangleinequality.Empezar
withatrivialcycleconsistingofasinglearbitrarilychosenvertex.Ateachstep,
identifythevertexuthatisnotonthecyclebutwhosedistancetoanyvertexonthe
cycleisminimum.Supposethatthevertexonthecyclethatisnearestuisvertex.
Extendthecycletoincludeubyinsertingujustafter.Repeatuntilallvertices
areonthecycle.Provethatthisheuristicreturnsatourwhosetotalcostisnotmore
thantwicethecostofanoptimaltour.

35.24
Inthebottlenecktravelingsalesmanproblem,wewishtofindthehamiltoniancy
clethatminimizesthecostofthemostcostlyedgeinthecycle.Suponiendoqueel
costfunctionsatisfiesthetriangleinequality,showthatthereexistsapolynomial
timeapproximationalgorithmwithapproximationratio3forthisproblem.(Hint:
Showrecursivelythatwecanvisitallthenodesinabottleneckspanningtree,as
discussedinProblem233,exactlyoncebytakingafullwalkofthetreeandskip
pingnodes,butwithoutskippingmorethantwoconsecutiveintermediatenodes.
Showthatthecostliestedgeinabottleneckspanningtreehasacostthatisatmost
thecostofthecostliestedgeinabottleneckhamiltoniancycle.)

35.25
Supposethattheverticesforaninstanceofthetravelingsalesmanproblemare
pointsintheplaneandthatthecostcu/istheeuclideandistancebetween
pointsuand.Showthatanoptimaltournevercrossesitself.

35.3Thesetcoveringproblem

https://translate.googleusercontent.com/translate_f 166/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
Thesetcoveringproblemisanoptimizationproblemthatmodelsmanyproblems
thatrequireresourcestobeallocated.Itscorrespondingdecisionproblemgeneral
izestheNPcompletevertexcoverproblemandisthereforealsoNPhard.Theap
proximationalgorithmdevelopedtohandlethevertexcoverproblemdoesn'tapply
here,however,andsoweneedtotryotherapproaches.Weshallexamineasimple
greedyheuristicwithalogarithmicapproximationratio.Thatis,asthesizeofthe
instancegetslarger,thesizeoftheapproximatesolutionmaygrow,relativetothe
sizeofanoptimalsolution.Becausethelogarithmfunctiongrowsratherslowly,
however,thisapproximationalgorithmmaynonethelessgiveusefulresults.

Pgina239

1118 Chapter35ApproximationAlgorithms

S1

S2

S6
S3 S4 S5

Figure35.3Aninstance.XF/ofthesetcoveringproblem,whereXconsistsofthe12black
pointsandFDfS1S2S3S4S5S6g.AminimumsizesetcoverisCDfS3S4S5g,with
size3.Thegreedyalgorithmproducesacoverofsize4byselectingeitherthesetsS1,S4,S5,
andS3orthesetsS1,S4,S5,andS6,inorder.

Aninstance.XF/ofthesetcoveringproblemconsistsofafinitesetXand
afamilyFofsubsetsofX,suchthateveryelementofXbelongstoatleastone
subsetinF:
[
XD S:
S2F
WesaythatasubsetS2Fcoversitselements.Theproblemistofindaminimum
sizesubsetCFwhosememberscoverallofX:
[
XD S: (35.8)
S2C
WesaythatanyCsatisfyingequation(35.8)coversX.Figure35.3illustratesthe
setcoveringproblem.ThesizeofCisthenumberofsetsitcontains,ratherthan
thenumberofindividualelementsinthesesets,sinceeverysubsetCthatcoversX
mustcontainalljXjindividualelements.InFigure35.3,theminimumsetcover
hassize3.
Thesetcoveringproblemabstractsmanycommonlyarisingcombinatorialprob
ProblemasAsasimpleexample,supposethatXrepresentsasetofskillsthatareneeded
tosolveaproblemandthatwehaveagivensetofpeopleavailabletoworkonthe
problema.Wewishtoformacommittee,containingasfewpeopleaspossible,
suchthatforeveryrequisiteskillinX,atleastonememberofthecommitteehas
thatskill.Inthedecisionversionofthesetcoveringproblem,weaskwhethera
coveringexistswithsizeatmostk,wherekisanadditionalparameterspecified
intheprobleminstance.ThedecisionversionoftheproblemisNPcomplete,as
Exercise35.32asksyoutoshow.

Pgina240

35.3Thesetcoveringproblem 1119.

Agreedyapproximationalgorithm

https://translate.googleusercontent.com/translate_f 167/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
Thegreedymethodworksbypicking,ateachstage,thesetSthatcoversthegreat
estnumberofremainingelementsthatareuncovered.

GREEDYSETCOVER.XF/
1UDX
2CD
3whileU
4 selectanS2FthatmaximizesjS\Uj
5 UDU S
6 CDC[fSg
7returnC

IntheexampleofFigure35.3,GREEDYSETCOVERaddstoC,inorder,thesets
S1,S4,andS5,followedbyeitherS3orS6.
Thealgorithmworksasfollows.ThesetUcontains,ateachstage,thesetof
remaininguncoveredelements.ThesetCcontainsthecoverbeingconstructed.
Line4isthegreedydecisionmakingstep,choosingasubsetSthatcoversasmany
uncoveredelementsaspossible(breakingtiesarbitrarily).AfterSisselected,
line5removesitselementsfromU,andline6placesSintoC.Whenthealgorithm
terminates,thesetCcontainsasubfamilyofFthatcoversX.
WecaneasilyimplementGREEDYSETCOVERtorunintimepolynomialinjXj
andjFj.Sincethenumberofiterationsofthelooponlines36isboundedfrom
abovebymin.jXjjFj/,andwecanimplementtheloopbodytorunintime
O.jXjjFj/,asimpleimplementationrunsintimeO.jXjjFjmin.jXjjFj//.Ex
ercise35.33asksforalineartimealgorithm.

Anlisis
Wenowshowthatthegreedyalgorithmreturnsasetcoverthatisnottoomuch
largerthananoptimalsetcover.Forconvenience,inthischapterwedenotethedth
Pd
harmonicnumberHdD
iD11=i(seeSectionA.1)byHd/.Asaboundary
condition,wedefineH.0/D0.

Theorem35.4
GREEDYSETCOVERisapolynomialtime.n/approximationalgorithm,where
.n/DH.maxfjSjWS2Fg/:

ProofWehavealreadyshownthatGREEDYSETCOVERrunsinpolynomial
hora.

Page241

1120 Chapter35ApproximationAlgorithms

ToshowthatGREEDYSETCOVERisa.n/approximationalgorithm,weas
signacostof1toeachsetselectedbythealgorithm,distributethiscostover
theelementscoveredforthefirsttime,andthenusethesecoststoderivethede
siredrelationshipbetweenthesizeofanoptimalsetcoverCandthesizeofthe
setcoverCreturnedbythealgorithm.LetSidenotetheithsubsetselectedby
GREEDYSETCOVERthealgorithmincursacostof1whenitaddsSitoC.We
spreadthiscostofselectingSievenlyamongtheelementscoveredforthefirsttime
bySi.Letcxdenotethecostallocatedtoelementx,foreachx2X.Eachelement
isassignedacostonlyonce,whenitiscoveredforthefirsttime.Ifxiscovered
forthefirsttimebySi,then
1
cxD :
jSi .S1[S2[[Si1/j
Eachstepofthealgorithmassigns1unitofcost,andso
x
jCjD cx: (35.9)
x2X
Eachelementx2XisinatleastonesetintheoptimalcoverC,andsowehave
x x x
cx cx: (35.10)
S2C x2S x2X
Combiningequation(35.9)andinequality(35.10),wehavethat
x x
jCj cx: (35.11)
S2C x2S

https://translate.googleusercontent.com/translate_f 168/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
Theremainderoftheproofrestsonthefollowingkeyinequality,whichweshall
proveshortly.ForanysetSbelongingtothefamilyF,
x
cxH.jSj/: (35.12)
x2S
Frominequalities(35.11)and(35.12),itfollowsthat
x
jCj H.jSj/
S2C
jCjH.maxfjSjWS2Fg/
thusprovingthetheorem.
Allthatremainsistoproveinequality(35.12).ConsideranysetS2Fandany
iD12:::jCj,andlet
uiDjS.S1[S2[[Si/j
bethenumberofelementsinSthatremainuncoveredafterthealgorithmhas
selectedsetsS1S2:::Si.Wedefineu0DjSjtobethenumberofelements

Pgina242

35.3Thesetcoveringproblem 1121

ofS,whichareallinitiallyuncovered.LetkbetheleastindexsuchthatukD0,
sothateveryelementinSiscoveredbyatleastoneofthesetsS1S2:::Skand
someelementinSisuncoveredbyS1[S2[[Sk1.Then,ui1 ui,and
ui1 uielementsofSarecoveredforthefirsttimebySi,foriD12:::K
As,
x xK
1
cx re .ui1 ui/ :
jSi .S1[S2[[Si1/j
x2S ID1
Observaeso
jSi .S1[S2[[Si1/jjS.S1[S2[[Si1/j
Dui1
becausethegreedychoiceofSiguaranteesthatScannotcovermorenewele
mentsthanSidoes(otherwise,thealgorithmwouldhavechosenSinsteadofSi).
Consequently,weobtain
x xK
1
cx UN .ui1 ui/ :
ui1
x2S ID1
Wenowboundthisquantityasfollows:
x xK 1
cx UN .ui1 ui/
ui1
x2S ID1
x K u
x i1
1
re
ui1
ID1jDu C1 i

x K u x i1
1
UN (becausejui1)
J
ID1jDu C1 i
!
x K u x 1 xu 1
i1 i

re
J J
ID1 jD1 jD1
xK
re .Hui1/Hui//
ID1
DHu0/Huk/ (becausethesumtelescopes)
DHu0/H.0/
DHu0/ (becauseH.0/D0)
DH.jSj/
whichcompletestheproofofinequality(35.12).

https://translate.googleusercontent.com/translate_f 169/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,

Pgina243

1122 Chapter35ApproximationAlgorithms

Corollary35.5
GREEDYSETCOVERisapolynomialtime.lnjXjC1/approximationalgorithm.

ProofUseinequality(A.14)andTheorem35.4.

Insomeapplications,maxfjSjWS2Fgisasmallconstant,andsothesolution
returnedbyGREEDYSETCOVERisatmostasmallconstanttimeslargerthan
ptimoOnesuchapplicationoccurswhenthisheuristicfindsanapproximate
vertexcoverforagraphwhoseverticeshavedegreeatmost3.Inthiscase,the
solutionfoundbyGREEDYSETCOVERisnotmorethanH.3/D11=6timesas
largeasanoptimalsolution,aperformanceguaranteethatisslightlybetterthan
thatofAPPROXVERTEXCOVER.

Exercises

35.31
Considereachofthefollowingwordsasasetofletters:fariddashdrain
heardlostnoseshunslatesnarethreadg.Showwhichsetcover
GREEDYSETCOVERproduceswhenwebreaktiesinfavorofthewordthatap
pearsfirstinthedictionary.

35.32
ShowthatthedecisionversionofthesetcoveringproblemisNPcompleteby
reducingitfromthevertexcoverproblem.

35.33
ShowhowtoimplementG
PAG UN REEDYSETCOVERinsuchawaythatitrunsintime

O jSj .
S2F
35.34
ShowthatthefollowingweakerformofTheorem35.4istriviallytrue:
jCjjCjmaxfjSjWS2Fg:

35.35
GREEDYSETCOVERcanreturnanumberofdifferentsolutions,dependingon
howwebreaktiesinline4.GiveaprocedureBADSETCOVERINSTANCE.n/
thatreturnsannelementinstanceofthesetcoveringproblemforwhich,depend
ingonhowwebreaktiesinline4,GREEDYSETCOVERcanreturnanumberof
differentsolutionsthatisexponentialinn.

Pgina244

35.4Randomizationandlinearprogramming 1123

35.4Randomizationandlinearprogramming

Inthissection,westudytwousefultechniquesfordesigningapproximationalgo
rithms:randomizationandlinearprogramming.Weshallgiveasimplerandomized
algorithmforanoptimizationversionof3CNFsatisfiability,andthenweshalluse
linearprogrammingtohelpdesignanapproximationalgorithmforaweightedver
sionofthevertexcoverproblem.Thissectiononlyscratchesthesurfaceofthese
twopowerfultechniques.Thechapternotesgivereferencesforfurtherstudyof
theseareas.

ArandomizedapproximationalgorithmforMAX3CNFsatisfiability
Justassomerandomizedalgorithmscomputeexactsolutions,somerandomized
algorithmscomputeapproximatesolutions.Wesaythatarandomizedalgorithm
foraproblemhasanapproximationratioof.n/if,foranyinputofsizen,the

https://translate.googleusercontent.com/translate_f 170/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
expectedcostCofthesolutionproducedbytherandomizedalgorithmiswithina
factorof.n/ofthecostCofanoptimalsolution:
UN UN
do do
Mximo .n/: (35.13)
do do
Wecallarandomizedalgorithmthatachievesanapproximationratioof.n/a
randomized.n/approximationalgorithm.Inotherwords,arandomizedap
proximationalgorithmislikeadeterministicapproximationalgorithm,exceptthat
theapproximationratioisforanexpectedcost.
Aparticularinstanceof3CNFsatisfiability,asdefinedinSection34.4,mayor
maynotbesatisfiable.Inordertobesatisfiable,theremustexistanassignmentof
thevariablessothateveryclauseevaluatesto1.Ifaninstanceisnotsatisfiable,we
maywanttocomputehowclosetosatisfiableitis,thatis,wemaywishtofindan
assignmentofthevariablesthatsatisfiesasmanyclausesaspossible.Wecallthe
resultingmaximizationproblemMAX3CNFsatisfiability.TheinputtoMAX3
CNFsatisfiabilityisthesameasfor3CNFsatisfiability,andthegoalistoreturn
anassignmentofthevariablesthatmaximizesthenumberofclausesevaluating
to1.Wenowshowthatrandomlysettingeachvariableto1withprobability1=2
andto0withprobability1=2yieldsarandomized8=7approximationalgorithm.
Accordingtothedefinitionof3CNFsatisfiabilityfromSection34.4,werequire
eachclausetoconsistofexactlythreedistinctliterals.Wefurtherassumethat
noclausecontainsbothavariableanditsnegation.(Exercise35.41asksyouto
removethislastassumption.)

Pgina245

1124 Chapter35ApproximationAlgorithms

Theorem35.6
GivenaninstanceofMAX3CNFsatisfiabilitywithnvariablesx1x2:::xn
andmclauses,therandomizedalgorithmthatindependentlysetseachvari
ableto1withprobability1=2andto0withprobability1=2isarandomized
8=7approximationalgorithm.

ProofSupposethatwehaveindependentlyseteachvariableto1withprobabil
ity1=2andto0withprobability1=2.ForiD12:::m,wedefinetheindicator
randomvariable
YiDIfclauseiissatisfiedg
sothatYiD1aslongaswehavesetatleastoneoftheliteralsintheithclause
to1.Sincenoliteralappearsmorethanonceinthesameclause,andsincewehave
assumedthatnovariableanditsnegationappearinthesameclause,thesettingsof
thethreeliteralsineachclauseareindependent.Aclauseisnotsatisfiedonlyifall
threeofitsliteralsaresetto0,andsoPrfclauseiisnotsatisfiedgD.1=2/3D1=8.
Thus,wehavePrfclauseiissatisfiedgD1 1=8D7=8,andbyLemma5.1,
wehaveEYiD7=8.LetYbethenumberofsatisfiedclausesoverall,sothat
YDY1CY2CCYm.Then,wehave
"m #
x
EYDE Yi
ID1
xmetro
re EYi (bylinearityofexpectation)
ID1
xmetro
re 7=8
ID1
D7m=8:
Clearly,misanupperboundonthenumberofsatisfiedclauses,andhencethe
approximationratioisatmostm=.7m=8/D8=7.

Approximatingweightedvertexcoverusinglinearprogramming
Intheminimumweightvertexcoverproblem,wearegivenanundirectedgraph
GD.VE/inwhicheachvertex2Vhasanassociatedpositiveweightw./.
ForanyvertexcoverV0V,wedefinetheweightofthevertexcoverwV0/D
PAG
2V w./.Thegoalistofindavertexcoverofminimumweight.
0

https://translate.googleusercontent.com/translate_f 171/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
Wecannotapplythealgorithmusedforunweightedvertexcover,norcanweuse
arandomsolutionbothmethodsmayreturnsolutionsthatarefarfromoptimal.
Weshall,however,computealowerboundontheweightoftheminimumweight

Pgina246

35.4Randomizationandlinearprogramming 1125

vertexcover,byusingalinearprogram.Weshallthenroundthissolutionand
useittoobtainavertexcover.
Supposethatweassociateavariablex./witheachvertex 2V,andletus
requirethatx./equalseither0or1foreach2V.Weputintothevertexcover
ifandonlyifx./D1.Then,wecanwritetheconstraintthatforanyedge.u/,
atleastoneofuandmustbeinthevertexcoverasxu/Cx./ 1.Thisview
givesrisetothefollowing01integerprogramforfindingaminimumweight
vertexcover:
x
minimize w./x./ (35.14)
2V
sujetoa
xu/Cx./ 1 foreach.u/2E (35.15)
x./2f01gforeach2V: (35.16)
Inthespecialcaseinwhichalltheweightsw./areequalto1,thisformu
lationistheoptimizationversionoftheNPhardvertexcoverproblem.Sup
pose,however,thatweremovetheconstraintthatx./2f01gandreplaceit
by0x./1.Wethenobtainthefollowinglinearprogram,whichisknownas
thelinearprogrammingrelaxation:
x
minimize w./x./ (35.17)
2V
sujetoa
xu/Cx./ 1foreach.u/2E (35.18)
x./1foreach2V (35.19)
x./ 0foreach2V: (35.20)

Anyfeasiblesolutiontothe01integerprograminlines(35.14)(35.16)isalso
afeasiblesolutiontothelinearprograminlines(35.17)(35.20).Porlotanto,la
valueofanoptimalsolutiontothelinearprogramgivesalowerboundonthevalue
ofanoptimalsolutiontothe01integerprogram,andhencealowerboundonthe
optimalweightintheminimumweightvertexcoverproblem.
Thefollowingprocedureusesthesolutiontothelinearprogrammingrelaxation
toconstructanapproximatesolutiontotheminimumweightvertexcoverproblem:

Pgina247

1126 Chapter35ApproximationAlgorithms

APPROXMINWEIGHTVC.Gw/
1CD
2computeNx,anoptimalsolutiontothelinearprograminlines(35.17)(35.20)
3foreach2V
4 ifNx./ 1=2
5 CDC[fg
6returnC

TheAPPROXMINWEIGHTVCprocedureworksasfollows.Line1initial
izesthevertexcovertobeempty.Line2formulatesthelinearprogramin
https://translate.googleusercontent.com/translate_f 172/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
lines(35.17)(35.20)andthensolvesthislinearprogram.Anoptimalsolution
giveseachvertexanassociatedvalueNx./,where0Nx./1.Weusethis
valuetoguidethechoiceofwhichverticestoaddtothevertexcoverCinlines35.
IfNx./ 1=2,weaddtoCotherwisewedonot.Ineffect,wearerounding
eachfractionalvariableinthesolutiontothelinearprogramto0or1inorderto
obtainasolutiontothe01integerprograminlines(35.14)(35.16).Finally,line6
returnsthevertexcoverC.

Theorem35.7
AlgorithmAPPROXMINWEIGHTVCisapolynomialtime2approximational
gorithmfortheminimumweightvertexcoverproblem.

ProofBecausethereisapolynomialtimealgorithmtosolvethelinearprogram
inline2,andbecausetheforloopoflines35runsinpolynomialtime,APPROX
MINWEIGHTVCisapolynomialtimealgorithm.
NowweshowthatAPPROXMINWEIGHTVCisa2approximationalgo
rithm.LetCbeanoptimalsolutiontotheminimumweightvertexcoverprob
lem,andletbethevalueofanoptimalsolutiontothelinearprogramin
lines(35.17)(35.20).Sinceanoptimalvertexcoverisafeasiblesolutiontothe
linearprogram,mustbealowerboundonwC/,thatis,
wC/: (35.21)

Next,weclaimthatbyroundingthefractionalvaluesofthevariablesNx./,we
produceasetCthatisavertexcoverandsatisfieswC/2.ToseethatCis
avertexcover,consideranyedge.u/2E.Byconstraint(35.18),weknowthat
xu/Cx./ 1,whichimpliesthatatleastoneofNx.u/andNx./isatleast1=2.
Therefore,atleastoneofuandisincludedinthevertexcover,andsoeveryedge
iscovered.
Now,weconsidertheweightofthecover.Tenemos

Pgina248

35.4Randomizationandlinearprogramming 1127
x
re w./Nx./
2Vx
w./Nx./
2VWNx./1=2
x 1
w./
2
2VWNx./1=2
x 1
re w./
2
2C
1x
re w./
2
2C
1
re wC/: (35.22)
2
Combininginequalities(35.21)and(35.22)gives
wC/22w.C/

andhenceAPPROXMINWEIGHTVCisa2approximationalgorithm.

Exercises

35.41
Showthatevenifweallowaclausetocontainbothavariableanditsnegation,ran
domlysettingeachvariableto1withprobability1=2andto0withprobability1=2
stillyieldsarandomized8=7approximationalgorithm.

35.42
TheMAXCNFsatisfiabilityproblemisliketheMAX3CNFsatisfiabilityprob
lem,exceptthatitdoesnotrestricteachclausetohaveexactly3literals.Givea
randomized2approximationalgorithmfortheMAXCNFsatisfiabilityproblem.

https://translate.googleusercontent.com/translate_f 173/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
35.43
IntheMAXCUTproblem,wearegivenanunweightedundirectedgraphGD
.VE/.Wedefineacut.SV S/asinChapter23andtheweightofacutasthe
numberofedgescrossingthecut.Thegoalistofindacutofmaximumweight.
Supposethatforeachvertex,werandomlyandindependentlyplaceinSwith
probability1=2andinV Swithprobability1=2.Showthatthisalgorithmisa
randomized2approximationalgorithm.

Pgina249

1128 Chapter35ApproximationAlgorithms

35.44
Showthattheconstraintsinline(35.19)areredundantinthesensethatifwere
movethemfromthelinearprograminlines(35.17)(35.20),anyoptimalsolution
totheresultinglinearprogrammustsatisfyx./1foreach2V.

35.5Thesubsetsumproblem

RecallfromSection34.5.5thataninstanceofthesubsetsumproblemisa
pair.St/,whereSisasetfx1x2:::xngofpositiveintegersandtisaposi
tiveinteger.ThisdecisionproblemaskswhetherthereexistsasubsetofSthat
addsupexactlytothetargetvaluet.AswesawinSection34.5.5,thisproblemis
NPcomplete.
Theoptimizationproblemassociatedwiththisdecisionproblemarisesinprac
ticalapplications.Intheoptimizationproblem,wewishtofindasubsetof
fx1x2:::xngwhosesumisaslargeaspossiblebutnotlargerthant.Porejemplo,
ample,wemayhaveatruckthatcancarrynomorethantpounds,andndifferent
boxestoship,theithofwhichweighsxipounds.Wewishtofillthetruckwithas
heavyaloadaspossiblewithoutexceedingthegivenweightlimit.
Inthissection,wepresentanexponentialtimealgorithmthatcomputestheop
timalvalueforthisoptimizationproblem,andthenweshowhowtomodifythe
algorithmsothatitbecomesafullypolynomialtimeapproximationscheme.(Re
callthatafullypolynomialtimeapproximationschemehasarunningtimethatis
polynomialin1=aswellasinthesizeoftheinput.)

Anexponentialtimeexactalgorithm
Supposethatwecomputed,foreachsubsetS0ofS,thesumoftheelements
inS0,andthenweselected,amongthesubsetswhosesumdoesnotexceedt,
theonewhosesumwasclosesttot.Clearlythisalgorithmwouldreturntheop
timalsolution,butitcouldtakeexponentialtime.Toimplementthisalgorithm,
wecoulduseaniterativeprocedurethat,initerationi,computesthesumsof
allsubsetsoffx1x2:::xig,usingasastartingpointthesumsofallsubsets
offx1x2:::xi1g.Indoingso,wewouldrealizethatonceaparticularsubsetS0
hadasumexceedingt,therewouldbenoreasontomaintainit,sincenosuper
setofS0couldbetheoptimalsolution.Wenowgiveanimplementationofthis
estrategia.
TheprocedureEXACTSUBSETSUMtakesaninputsetSDfx1x2:::xng
andatargetvaluetwe'llseeitspseudocodeinamoment.Thisprocedureit

Pgina250

35.5Thesubsetsumproblem 1129

erativelycomputesLi,thelistofsumsofallsubsetsoffx1:::xigthatdonot
exceedt,andthenitreturnsthemaximumvalueinLn.
IfLisalistofpositiveintegersandxisanotherpositiveinteger,thenwelet

https://translate.googleusercontent.com/translate_f 174/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
LCxdenotethelistofintegersderivedfromLbyincreasingeachelementofL
byx.Forexample,ifLDh12359i,thenLC2Dh345711i.Wealsouse
thisnotationforsets,sothat
SCxDfsCxWs2Sg:
WealsouseanauxiliaryprocedureMERGELISTS.LL0/,whichreturnsthe
sortedlistthatisthemergeofitstwosortedinputlistsLandL0withduplicate
valuesremoved.LiketheMERGEprocedureweusedinmergesort(Section2.3.1),
MERGELISTSrunsintimeO.jLjCjL0j/.WeomitthepseudocodeforMERGE
LISTS.

EXACTSUBSETSUM.St/
1nDjSj
2L0Dh0i
3foriD1ton
4 LiDMERGELISTS.Li1Li1Cxi/
5 removefromLieveryelementthatisgreaterthant
6returnthelargestelementinLn

ToseehowEXACTSUBSETSUMworks,letPidenotethesetofallvalues
obtainedbyselectinga(possiblyempty)subsetoffx1x2:::xigandsumming
itsmembers.Forexample,ifSDf145g,then
P1 Df01g
P2 Df0145g
P3 Df01456910g:
Giventheidentity
PiDPi1[.Pi1Cxi/ (35.23)
wecanprovebyinductiononi(seeExercise35.51)thatthelistLiisasortedlist
containingeveryelementofPiwhosevalueisnotmorethant.Sincethelength
ofLicanbeasmuchas2i,EXACTSUBSETSUMisanexponentialtimealgorithm
ingeneral,althoughitisapolynomialtimealgorithminthespecialcasesinwhicht
ispolynomialinjSjorallthenumbersinSareboundedbyapolynomialinjSj.

Afullypolynomialtimeapproximationscheme
Wecanderiveafullypolynomialtimeapproximationschemeforthesubsetsum
problembytrimmingeachlistLiafteritiscreated.Theideabehindtrimmingis

Pgina251

1130 Chapter35ApproximationAlgorithms

thatiftwovaluesinLareclosetoeachother,thensincewewantjustanapproxi
matesolution,wedonotneedtomaintainbothofthemexplicitly.Moreprecisely,
weuseatrimmingparametersuchthat0<<1.WhenwetrimalistLby,
weremoveasmanyelementsfromLaspossible,insuchawaythatifL0isthe
resultoftrimmingL,thenforeveryelementythatwasremovedfromL,thereis
anelementstillinL0thatapproximatesy,thatis,
Y
y: (35.24)
1C
WecanthinkofsuchaasrepresentingyinthenewlistL0.Eachremoved
elementyisrepresentedbyaremainingelementsatisfyinginequality(35.24).
Forexample,ifD0:1and
LDh10111215202122232429i
thenwecantrimLtoobtain
L0Dh101215202329i
wherethedeletedvalue11isrepresentedby10,thedeletedvalues21and22
arerepresentedby20,andthedeletedvalue24isrepresentedby23.Because
everyelementofthetrimmedversionofthelistisalsoanelementoftheoriginal
versionofthelist,trimmingcandramaticallydecreasethenumberofelementskept
whilekeepingaclose(andslightlysmaller)representativevalueinthelistforeach
deletedelement.
ThefollowingproceduretrimslistLDhy1y2:::ymiintime.m/,givenL
and,andassumingthatLissortedintomonotonicallyincreasingorder.los
outputoftheprocedureisatrimmed,sortedlist.

https://translate.googleusercontent.com/translate_f 175/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,

TRIM.L/
1letmbethelengthofL
2L0Dhy1i
3lastDy1
4foriD2tom
5 ifyi>last.1C/ //yi lastbecauseLissorted
6 appendyiontotheendofL0
7 lastDyi
8returnL0
TheprocedurescanstheelementsofLinmonotonicallyincreasingorder.Anum
berisappendedontothereturnedlistL0onlyifitisthefirstelementofLorifit
cannotberepresentedbythemostrecentnumberplacedintoL0.
GiventheprocedureTRIM,wecanconstructourapproximationschemeasfol
lows.ThisproceduretakesasinputasetSDfx1x2:::xngofnintegers(in
arbitraryorder),atargetintegert,andanapproximationparameter,where

Pgina252

35.5Thesubsetsumproblem 1131

0<<1: (35.25)
Itreturnsavaluewhosevalueiswithina1Cfactoroftheoptimalsolution.
APPROXSUBSETSUM.St/
1nDjSj
2L0Dh0i
3foriD1ton
4 LiDMERGELISTS.Li1Li1Cxi/
5 LiDTRIM.Li=2n/
6 removefromLieveryelementthatisgreaterthant
7letbethelargestvalueinLn
8return
Line2initializesthelistL0tobethelistcontainingjusttheelement0.Thefor
loopinlines36computesLiasasortedlistcontainingasuitablytrimmedver
sionofthesetPi,withallelementslargerthantremoved.SincewecreateLi
fromLi1,wemustensurethattherepeatedtrimmingdoesn'tintroducetoomuch
compoundedinaccuracy.Inamoment,weshallseethatAPPROXSUBSETSUM
returnsacorrectapproximationifoneexists.
Asanexample,supposewehavetheinstance
SDh104102201101i
withtD308andD0:40.Thetrimmingparameteris=8D0:05.APPROX
SUBSETSUMcomputesthefollowingvaluesontheindicatedlines:
line2: L0 Dh0i

line4: L1 Dh0104i
line5: L1 Dh0104i
line6: L1 Dh0104i

line4: L2 Dh0102104206i
line5: L2 Dh0102206i
line6: L2 Dh0102206i

line4: L3 Dh0102201206303407i
line5: L3 Dh0102201303407i
line6: L3 Dh0102201303i

line4: L4 Dh0101102201203302303404i
line5: L4 Dh0101201302404i
line6: L4 Dh0101201302i:

Pgina253

https://translate.googleusercontent.com/translate_f 176/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,

1132 Chapter35ApproximationAlgorithms

ThealgorithmreturnsD302asitsanswer,whichiswellwithinD40%of
theoptimalanswer307D104C102C101infact,itiswithin2%.

Theorem35.8
APPROXSUBSETSUMisafullypolynomialtimeapproximationschemeforthe
subsetsumproblem.

ProofTheoperationsoftrimmingLiinline5andremovingfromLieveryele
mentthatisgreaterthantmaintainthepropertythateveryelementofLiisalsoa
memberofPi.Therefore,thevaluereturnedinline8isindeedthesumofsome
subsetofS.Lety2Pndenoteanoptimalsolutiontothesubsetsumproblem.
Then,fromline6,weknowthaty.Byinequality(35.1),weneedtoshow
thaty=1C.Wemustalsoshowthattherunningtimeofthisalgorithmis
polynomialinboth1=andthesizeoftheinput.
AsExercise35.52asksyoutoshow,foreveryelementyinPithatisatmostt,
thereexistsanelement2Lisuchthat
Y
y: (35.26)
.1C=2n/i
Inequality(35.26)mustholdfory2Pn,andthereforethereexistsanelement
2Lnsuchthat
Y
y
.1C=2n/n
yporlotanto
n
Y
1C : (35.27)
2n
Sincethereexistsanelement2Lnfulfillinginequality(35.27),theinequality
mustholdfor,whichisthelargestvalueinLnesdecir,
n
Y
1C : (35.28)
2n
Now,weshowthaty=1C.Wedosobyshowingthat.1C=2n/ n
1C.Byequation(3.14),wehavelimn!1.1C=2n/nDe=2.Exercise35.53
asksyoutoshowthat
n
re
1C >0: (35.29)
Dn 2n
Therefore,thefunction.1C=2n/ norte
increaseswithnasitapproachesitslimit
ofe=2,andwehave

Pgina254

35.5Thesubsetsumproblem 1133
n
1C e=2
2n
1C=2C.=2/2 (byinequality(3.13))
1C (byinequality(35.25)). (35.30)
Combininginequalities(35.28)and(35.30)completestheanalysisoftheapproxi
mationratio.
ToshowthatAPPROXSUBSETSUMisafullypolynomialtimeapproximation
scheme,wederiveaboundonthelengthofLi.Aftertrimming,successiveele
mentsand0ofLimusthavetherelationship0=>1C=2n.Thatis,theymust
differbyafactorofatleast1C=2n.Eachlist,therefore,containsthevalue0,

possiblythevalue1,anduptolog1C=2nt additionalvalues.Elnmerode
elementsineachlistLiisatmost
lnt
log1C=2ntC2D C2
ln.1C=2n/
2n.1C=2n/lnt
UN C2(byinequality(3.17))
3nlnt
https://translate.googleusercontent.com/translate_f 177/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
< C2 (byinequality(35.25)).
Thisboundispolynomialinthesizeoftheinputwhichisthenumberofbitslgt
neededtorepresenttplusthenumberofbitsneededtorepresentthesetS,whichis
inturnpolynomialinnandin1=.SincetherunningtimeofAPPROXSUBSET
SUMispolynomialinthelengthsoftheLi,weconcludethatAPPROXSUBSET
SUMisafullypolynomialtimeapproximationscheme.

Exercises

35.51
Proveequation(35.23).Thenshowthatafterexecutingline5ofEXACTSUBSET
SUM,LiisasortedlistcontainingeveryelementofPiwhosevalueisnotmore
thant.

35.52
Usinginductiononi,proveinequality(35.26).

35.53
Proveinequality(35.29).

Pgina255

1134 Chapter35ApproximationAlgorithms

35.54
Howwouldyoumodifytheapproximationschemepresentedinthissectiontofind
agoodapproximationtothesmallestvaluenotlessthantthatisasumofsome
subsetofthegiveninputlist?

35.55
ModifytheAPPROXSUBSETSUMproceduretoalsoreturnthesubsetofSthat
sumstothevalue.

Problemas

351Binpacking
Supposethatwearegivenasetofnobjects,wherethesizesioftheithobject
satisfies0<si<1.Wewishtopackalltheobjectsintotheminimumnumberof
unitsizebins.Eachbincanholdanysubsetoftheobjectswhosetotalsizedoes
notexceed1.

a.Provethattheproblemofdeterminingtheminimumnumberofbinsrequiredis
NPhard.(Hint:Reducefromthesubsetsumproblem.)

Thefirstfitheuristictakeseachobjectinturnandplacesitintothefirstbinthat
Pn
canaccommodateit.LetSD iD1si.
segundo.ArguethattheoptimalnumberofbinsrequiredisatleastdSe.

do.Arguethatthefirstfitheuristicleavesatmostonebinlessthanhalffull.

re.Provethatthenumberofbinsusedbythefirstfitheuristicisnevermore
thand2Se.

mi.Proveanapproximationratioof2forthefirstfitheuristic.

f.Giveanefficientimplementationofthefirstfitheuristic,andanalyzeitsrunning
hora.

352Approximatingthesizeofamaximumclique
LetGD.VE/beanundirectedgraph.Foranyk 1,defineG.k/tobetheundi
rectedgraph.V.k/E.k//,whereV.k/isthesetofallorderedktuplesofvertices
fromVandE.k/isdefinedsothat.12:::k/isadjacentto.w1w2:::wk/
ifandonlyifforiD12:::k,eithervertexiisadjacenttowiinG,orelse
https://translate.googleusercontent.com/translate_f 178/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,

iDwi.

Pgina256

ProblemsforChapter35 1135

a.ProvethatthesizeofthemaximumcliqueinG.k/isequaltothekthpowerof
thesizeofthemaximumcliqueinG.

segundo.Arguethatifthereisanapproximationalgorithmthathasaconstantapproxi
mationratioforfindingamaximumsizeclique,thenthereisapolynomialtime
approximationschemefortheproblem.

353Weightedsetcoveringproblem
SupposethatwegeneralizethesetcoveringproblemsothateachsetSiinthe
PAG
familyFhasanassociatedweightwiandtheweightofacoverCis S 2Cwi.
i
Wewishtodetermineaminimumweightcover.(Section35.3handlesthecasein
whichwiD1foralli.)
Showhowtogeneralizethegreedysetcoveringheuristicinanaturalmanner
toprovideanapproximatesolutionforanyinstanceoftheweightedsetcovering
problema.ShowthatyourheuristichasanapproximationratioofHd/,wheredis
themaximumsizeofanysetSi.

354Maximummatching
RecallthatforanundirectedgraphG,amatchingisasetofedgessuchthatno
twoedgesinthesetareincidentonthesamevertex.InSection26.3,wesawhow
tofindamaximummatchinginabipartitegraph.Inthisproblem,wewilllookat
matchingsinundirectedgraphsingeneral(ie,thegraphsarenotrequiredtobe
bipartite).

a.Amaximalmatchingisamatchingthatisnotapropersubsetofanyother
pareo.Showthatamaximalmatchingneednotbeamaximummatchingby
exhibitinganundirectedgraphGandamaximalmatchingMinGthatisnota
maximummatching.(Hint:Youcanfindsuchagraphwithonlyfourvertices.)

segundo.ConsideranundirectedgraphGD.VE/.GiveanOE/timegreedyalgo
rithmtofindamaximalmatchinginG.

Inthisproblem,weshallconcentrateonapolynomialtimeapproximationalgo
rithmformaximummatching.Whereasthefastestknownalgorithmformaximum
matchingtakessuperlinear(butpolynomial)time,theapproximationalgorithm
herewillruninlineartime.Youwillshowthatthelineartimegreedyalgorithm
formaximalmatchinginpart(b)isa2approximationalgorithmformaximum
pareo.

do.ShowthatthesizeofamaximummatchinginGisalowerboundonthesize
ofanyvertexcoverforG.

Pgina257

1136 Chapter35ApproximationAlgorithms

re.ConsideramaximalmatchingMinGD.VE/.Dejar
TDf2VWsomeedgeinMisincidentong:

WhatcanyousayaboutthesubgraphofGinducedbytheverticesofGthat
arenotinT?

mi.Concludefrompart(d)that2jMjisthesizeofavertexcoverforG.

f.Usingparts(c)and(e),provethatthegreedyalgorithminpart(b)isa2approx
imationalgorithmformaximummatching.

https://translate.googleusercontent.com/translate_f 179/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
355Parallelmachinescheduling
Intheparallelmachineschedulingproblem,wearegivennjobs,J1J2:::Jn,
whereeachjobJkhasanassociatednonnegativeprocessingtimeofpk.Weare
alsogivenmidenticalmachines,M1M2:::Mm.Anyjobcanrunonanyma
chine.Aschedulespecifies,foreachjobJk,themachineonwhichitrunsand
thetimeperiodduringwhichitruns.EachjobJkmustrunonsomemachineMi
forpkconsecutivetimeunits,andduringthattimeperiodnootherjobmayrun
onMi.LetCkdenotethecompletiontimeofjobJk,thatis,thetimeatwhich
jobJkcompletesprocessing.Givenaschedule,wedefineCmaxDmax1jnCjto
bethemakespanoftheschedule.Thegoalistofindaschedulewhosemakespan
isminimum.
Forexample,supposethatwehavetwomachinesM1andM2andthatwehave
fourjobsJ1J2J3J4,withp1D2,p2D12,p3D4,andp4D5.Thenone
possiblescheduleruns,onmachineM1,jobJ1followedbyjobJ2,andonma
chineM2,itrunsjobJ4followedbyjobJ3.Forthisschedule,C1D2,C2D14,
C3D9,C4D5,andCmaxD14.AnoptimalschedulerunsJ2onmachineM1,and
itrunsjobsJ1,J3,andJ4onmachineM2.Forthisschedule,C1D2,C2D12,
C3D6,C4D11,andCmaxD12.
Givenaparallelmachineschedulingproblem,weletCmaxdenotethemakespan
ofanoptimalschedule.
a.Showthattheoptimalmakespanisatleastaslargeasthegreatestprocessing
time,thatis,
Cmax Mximo
pk:
1kn
segundo.Showthattheoptimalmakespanisatleastaslargeastheaveragemachineload,
esdecir,
1 x
Cmax pk:
metro
1kn

Pgina258

ProblemsforChapter35 1137

Supposethatweusethefollowinggreedyalgorithmforparallelmachineschedul
ing:wheneveramachineisidle,scheduleanyjobthathasnotyetbeenscheduled.

do.Writepseudocodetoimplementthisgreedyalgorithm.Whatistherunning
timeofyouralgorithm?

re.Fortheschedulereturnedbythegreedyalgorithm,showthat

1 x
Cmax pkCmax pk:
metro 1kn
1kn
Concludethatthisalgorithmisapolynomialtime2approximationalgorithm.

356Approximatingamaximumspanningtree
LetGD.VE/beanundirectedgraphwithdistinctedgeweightswu/oneach
edge.u/2E.Foreachvertex2V,letmax./Dmax.u/2Efw.u/gbe
themaximumweightedgeincidentonthatvertex.LetSGDfmax./W2Vg
bethesetofmaximumweightedgesincidentoneachvertex,andletTGbethe
maximumweightspanningtreeofG,thatis,thespanningtreeofmaximumtotal
PAG
peso.ForanysubsetofedgesE0E,definewE0/D .u/2E wu/.
0

a.Giveanexampleofagraphwithatleast4verticesforwhichSGDTG.

segundo.Giveanexampleofagraphwithatleast4verticesforwhichSGTG.

do.ProvethatSGTGforanygraphG.

re.ProvethatwTG/ wSG/=2foranygraphG.

mi.GiveanOVCE/timealgorithmtocomputea2approximationtothemaxi
mumspanningtree.

357Anapproximationalgorithmforthe01knapsackproblem
RecalltheknapsackproblemfromSection16.2.Therearenitems,wheretheith
https://translate.googleusercontent.com/translate_f 180/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
itemisworthidollarsandweighswipounds.Wearealsogivenaknapsack
thatcanholdatmostWpounds.Here,weaddthefurtherassumptionsthateach
weightwiisatmostWandthattheitemsareindexedinmonotonicallydecreasing
orderoftheirvalues:1 2 n.
Inthe01knapsackproblem,wewishtofindasubsetoftheitemswhosetotal
weightisatmostWandwhosetotalvalueismaximum.Thefractionalknapsack
problemislikethe01knapsackproblem,exceptthatweareallowedtotakea
fractionofeachitem,ratherthanbeingrestrictedtotakingeitherallornoneof

Pgina259

1138 Chapter35ApproximationAlgorithms

eachitem.Ifwetakeafractionxiofitemi,where0xi1,wecontribute
xiwitotheweightoftheknapsackandreceivevaluexii.Ourgoalistodevelop
apolynomialtime2approximationalgorithmforthe01knapsackproblem.
Inordertodesignapolynomialtimealgorithm,weconsiderrestrictedinstances
ofthe01knapsackproblem.GivenaninstanceIoftheknapsackproblem,we
formrestrictedinstancesIj,forjD12:::n,byremovingitems12:::j1
andrequiringthesolutiontoincludeitemj(allofitemjinboththefractional
and01knapsackproblems).NoitemsareremovedininstanceI1.ForinstanceIj,
letPjdenoteanoptimalsolutiontothe01problemandQjdenoteanoptimal
solutiontothefractionalproblem.
a.ArguethatanoptimalsolutiontoinstanceIofthe01knapsackproblemisone
offP1P2:::Png.

segundo.ProvethatwecanfindanoptimalsolutionQjtothefractionalproblemforin
stanceIjbyincludingitemjandthenusingthegreedyalgorithminwhich
ateachstep,wetakeasmuchaspossibleoftheunchosenitemintheset
fjC1jC2:::ngwithmaximumvalueperpoundi=wi.

do.ProvethatwecanalwaysconstructanoptimalsolutionQjtothefractional
problemforinstanceIjthatincludesatmostoneitemfractionally.Thatis,for
allitemsexceptpossiblyone,weeitherincludealloftheitemornoneofthe
itemintheknapsack.

re.GivenanoptimalsolutionQjtothefractionalproblemforinstanceIj,form
solutionRjfromQjbydeletinganyfractionalitemsfromQj.Let.S/denote
thetotalvalueofitemstakeninasolutionS.Provethat.Rj/ .Qj/=2
.Pj/=2.

mi.Giveapolynomialtimealgorithmthatreturnsamaximumvaluesolutionfrom
thesetfR1R2:::Rng,andprovethatyouralgorithmisapolynomialtime
2approximationalgorithmforthe01knapsackproblem.

Chapternotes

Althoughmethodsthatdonotnecessarilycomputeexactsolutionshavebeen
knownforthousandsofyears(forexample,methodstoapproximatethevalue
of),thenotionofanapproximationalgorithmismuchmorerecent.Hochbaum
[172]creditsGarey,Graham,andUllman[128]andJohnson[190]withformal
izingtheconceptofapolynomialtimeapproximationalgorithm.Thefirstsuch
algorithmisoftencreditedtoGraham[149].

Pgina260

NotesforChapter35 1139

Sincethisearlywork,thousandsofapproximationalgorithmshavebeende
signedforawiderangeofproblems,andthereisawealthofliteratureonthis
campo.RecenttextsbyAusielloetal.[26],Hochbaum[172],andVazirani[345]
dealexclusivelywithapproximationalgorithms,asdosurveysbyShmoys[315]
andKleinandYoung[207].Severalothertexts,suchasGareyandJohnson[129]
https://translate.googleusercontent.com/translate_f 181/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
andPapadimitriouandSteiglitz[271],havesignificantcoverageofapproximation
algorithmsaswell.Lawler,Lenstra,RinnooyKan,andShmoys[225]providean
extensivetreatmentofapproximationalgorithmsforthetravelingsalesmanprob
Lem
PapadimitriouandSteiglitzattributethealgorithmAPPROXVERTEXCOVER
toF.GavrilandM.Yannakakis.Thevertexcoverproblemhasbeenstudiedexten
sively(Hochbaum[172]lists16differentapproximationalgorithmsforthisprob
lem),butalltheapproximationratiosareatleast2o.1/.
ThealgorithmAPPROXTSPTOURappearsinapaperbyRosenkrantz,Stearns,
andLewis[298].Christofidesimprovedonthisalgorithmandgavea3=2approx
imationalgorithmforthetravelingsalesmanproblemwiththetriangleinequality.
Arora[22]andMitchell[257]haveshownthatifthepointsareintheeuclidean
plane,thereisapolynomialtimeapproximationscheme.Theorem35.3isdueto
SahniandGonzalez[301].
Theanalysisofthegreedyheuristicforthesetcoveringproblemismodeled
aftertheproofpublishedbyChvtal[68]ofamoregeneralresultthebasicresult
aspresentedhereisduetoJohnson[190]andLovsz[238].
ThealgorithmAPPROXSUBSETSUManditsanalysisarelooselymodeledafter
relatedapproximationalgorithmsfortheknapsackandsubsetsumproblemsby
IbarraandKim[187].
Problem357isacombinatorialversionofamoregeneralresultonapproximat
ingknapsacktypeintegerprogramsbyBienstockandMcClosky[45].
TherandomizedalgorithmforMAX3CNFsatisfiabilityisimplicitinthework
ofJohnson[190].TheweightedvertexcoveralgorithmisbyHochbaum[171].
Section35.4onlytouchesonthepowerofrandomizationandlinearprogram
minginthedesignofapproximationalgorithms.Acombinationofthesetwoideas
yieldsatechniquecalledrandomizedrounding,whichformulatesaproblemas
anintegerlinearprogram,solvesthelinearprogrammingrelaxation,andinterprets
thevariablesinthesolutionasprobabilities.Theseprobabilitiesthenhelpguide
thesolutionoftheoriginalproblem.ThistechniquewasfirstusedbyRaghavan
andThompson[290],andithashadmanysubsequentuses.(SeeMotwani,Naor,
andRaghavan[261]forasurvey.)Severalothernotablerecentideasinthefield
ofapproximationalgorithmsincludetheprimaldualmethod(seeGoemansand
Williamson[135]forasurvey),findingsparsecutsforuseindivideandconquer
algorithms[229],andtheuseofsemidefiniteprogramming[134].

Pgina261

1140 Chapter35ApproximationAlgorithms

AsmentionedinthechapternotesforChapter34,recentresultsinprobabilisti
callycheckableproofshaveledtolowerboundsontheapproximabilityofmany
problems,includingseveralinthischapter.Inadditiontothereferencesthere,
thechapterbyAroraandLund[23]containsagooddescriptionoftherelation
shipbetweenprobabilisticallycheckableproofsandthehardnessofapproximating
variousproblems.

https://translate.googleusercontent.com/translate_f 182/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,

Pgina263
Pgina262

VIIIAppendix:MathematicalBackground

Pgina264

https://translate.googleusercontent.com/translate_f 183/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,

Introduccin

Whenweanalyzealgorithms,weoftenneedtodrawuponabodyofmathematical
herramientas.Someofthesetoolsareassimpleashighschoolalgebra,butothersmaybe
newtoyou.InPartI,wesawhowtomanipulateasymptoticnotationsandsolve
Recurrencias.Thisappendixcomprisesacompendiumofseveralotherconceptsand
methodsweusetoanalyzealgorithms.AsnotedintheintroductiontoPartI,you
mayhaveseenmuchofthematerialinthisappendixbeforehavingreadthisbook
(althoughthespecificnotationalconventionsweusemightoccasionallydifferfrom
thoseyouhaveseenelsewhere).Hence,youshouldtreatthisappendixasreference
material.Asintherestofthisbook,however,wehaveincludedexercisesand
problems,inorderforyoutoimproveyourskillsintheseareas.
AppendixAoffersmethodsforevaluatingandboundingsummations,which
occurfrequentlyintheanalysisofalgorithms.Manyoftheformulashereappear
inanycalculustext,butyouwillfinditconvenienttohavethesemethodscompiled
inoneplace.
AppendixBcontainsbasicdefinitionsandnotationsforsets,relations,functions,
graphs,andtrees.Italsogivessomebasicpropertiesofthesemathematicalobjects.
AppendixCbeginswithelementaryprinciplesofcounting:permutations,com
binations,andthelike.Theremaindercontainsdefinitionsandpropertiesofbasic
probabilidad.Mostofthealgorithmsinthisbookrequirenoprobabilityfortheir
analysis,andthusyoucaneasilyomitthelattersectionsofthechapteronafirst
reading,evenwithoutskimmingthem.Later,whenyouencounteraprobabilistic
analysisthatyouwanttounderstandbetter,youwillfindAppendixCwellorga
nizedforreferencepurposes.

Pgina265

1144 PartVIIIAppendix:MathematicalBackground

AppendixDdefinesmatrices,theiroperations,andsomeoftheirbasicprop
erties.Youhaveprobablyseenmostofthismaterialalreadyifyouhavetakena
courseinlinearalgebra,butyoumightfindithelpfultohaveoneplacetolookfor
ournotationanddefinitions.

https://translate.googleusercontent.com/translate_f 184/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,

Pgina266

UN Summations

Whenanalgorithmcontainsaniterativecontrolconstructsuchasawhileorfor
loop,wecanexpressitsrunningtimeasthesumofthetimesspentoneachexe
cutionofthebodyoftheloop.Forexample,wefoundinSection2.2thatthejth
iterationofinsertionsorttooktimeproportionaltojintheworstcase.Byadding
upthetimespentoneachiteration,weobtainedthesummation(orseries)
xnorte
j:
jD2
Whenweevaluatedthissummation,weattainedaboundof.n2/ontheworst
caserunningtimeofthealgorithm.Thisexampleillustrateswhyyoushouldknow
howtomanipulateandboundsummations.
SectionA.1listsseveralbasicformulasinvolvingsummations.SectionA.2of
fersusefultechniquesforboundingsummations.WepresenttheformulasinSec
tionA.1withoutproof,thoughproofsforsomeofthemappearinSectionA.2to
illustratethemethodsofthatsection.Youcanfindmostoftheotherproofsinany
calculustext.

A.1Summationformulasandproperties

Givenasequencea1a2:::anofnumbers,wherenisanonnegativeinteger,we
canwritethefinitesuma1Ca2CCanas
xnorte
ak:
kD1
IfnD0,thevalueofthesummationisdefinedtobe0.Thevalueofafiniteseries
isalwayswelldefined,andwecanadditstermsinanyorder.
Givenaninfinitesequencea1a2:::ofnumbers,wecanwritetheinfinitesum
a1Ca2Cas

https://translate.googleusercontent.com/translate_f 185/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,

Pgina267

1146 AppendixASummations

x1
ak
kD1
whichweinterprettomean
xnorte
Lim ak:
n!1
kD1
Ifthelimitdoesnotexist,theseriesdivergesotherwise,itconverges.Lostrminos
ofaconvergentseriescannotalwaysbeaddedinanyorder.Wecan,however, P1
rearrangethetermsofanabsolutelyconvergentseries,thatis,aseries
P1 kD1ak
forwhichtheseries kD1jakjalsoconverges.

Linealidad
Foranyrealnumbercandanyfinitesequencesa1a2:::anandb1b2:::bn,
xnorte x norte xnorte
.cakCbk/Dc akC bk:
kD1 kD1 kD1
Thelinearitypropertyalsoappliestoinfiniteconvergentseries.
Wecanexploitthelinearitypropertytomanipulatesummationsincorporating
asymptoticnotation.Porejemplo,
!
xnorte xnorte
.f.k//D f.k/ :
kD1 kD1
Inthisequation,thenotationonthelefthandsideappliestothevariablek,but
ontherighthandside,itapplieston.Wecanalsoapplysuchmanipulationsto
infiniteconvergentseries.

Arithmeticseries
Thesummation
xnorte
kD1C2CCn
kD1
isanarithmeticseriesandhasthevalue
xnorte 1
kD nnC1/ (A.1)
2
kD1
D.n2/: (A.2)

Pgina268

A.1Summationformulasandproperties 1147

Sumsofsquaresandcubes
Wehavethefollowingsummationsofsquaresandcubes:
xnorte nnC1/.2nC1/
k2 re (A.3)
6
kD0
xnorte n2.nC1/2
k3 re : (A.4)
4
kD0

Geometricseries
Forrealx1,thesummation
xnorte
https://translate.googleusercontent.com/translate_f 186/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
xkD1CxCx2CCxn
kD0
isageometricorexponentialseriesandhasthevalue
xnorte xnC1 1
xkD : (A.5)
x1
kD0
Whenthesummationisinfiniteandjxj<1,wehavetheinfinitedecreasinggeo
metricseries
x1
1
xkD : (A.6)
1x
kD0

Harmonicseries
Forpositiveintegersn,thenthharmonicnumberis
1 1 1 1
Hn D1C do do CC
2 3 4 norte
xnorte
1
re
K
kD1
DlnnCO.1/: (A.7)
(WeshallprovearelatedboundinSectionA.2.)

Integratinganddifferentiatingseries
Byintegratingordifferentiatingtheformulasabove,additionalformulasarise.por
example,bydifferentiatingbothsidesoftheinfinitegeometricseries(A.6)and
multiplyingbyx,weget

Pgina269

1148 AppendixASummations

x1 x
kxkD (A.8)
.1x/2
kD0
forjxj<1.

Telescopingseries

Foranysequencea0a1:::an,
xnorte
.ak ak1/Dan a0 (A.9)
kD1
sinceeachofthetermsa1a2:::an1isaddedinexactlyonceandsubtractedout
exactlyonce.Wesaythatthesumtelescopes.Similar,
xN1
.ak akC1/Da0 an:
kD0
Asanexampleofatelescopingsum,considertheseries
xN1 1
:
kkC1/
kD1
Sincewecanrewriteeachtermas
1 1 1
re
kkC1/ K kC1
obtenemos
xN1 xN1 UN UN
1 1 1
re
kkC1/ K kC1
kD1 kD1
1
D1 :
norte

Productos

Wecanwritethefiniteproducta1a2 anas
Ynorte
ak:
https://translate.googleusercontent.com/translate_f 187/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,

kD1
IfnD0,thevalueoftheproductisdefinedtobe1.Wecanconvertaformulawith
aproducttoaformulawithasummationbyusingtheidentity
!
Ynorte xnorte
Lg ak re lgak:
kD1 kD1

Pgina270

A.2Boundingsummations 1149

Exercises

A.11 Pn
Findasimpleformulafor
kD1.2k1/.
A.12? Pn pag
Showthat 1/Dln. n/CO.1/bymanipulatingtheharmonic
kD11=.2k
serie.

A.13 P1
Showthat kD0k2xkDx.1Cx/=.1x/3for0<jxj<1.

A.14? P1
Showthat kD0.k1/=2kD0.

A.15? P1
Evaluatethesum kD1.2kC1/x2k.

A.16 Pn Pn
Provethat kD1Ofk.i//DO kD1fk.i/byusingthelinearitypropertyof
summations.

A.17 Qn
Evaluatetheproduct kD124k.

A.18? Qn
Evaluatetheproduct kD2.11=k2/.

A.2Boundingsummations

Wehavemanytechniquesatourdisposalforboundingthesummationsthatde
scribetherunningtimesofalgorithms.Herearesomeofthemostfrequentlyused
Mtodos.

Mathematicalinduction
Themostbasicwaytoevaluateaseriesistousemathematicalinduction.Comoun
Pn
example,letusprovethatthearithmeticseries kD1kevaluatesto12n.nC1/.Nosotros
caneasilyverifythisassertionfornD1.Wemaketheinductiveassumptionthat

Pgina271

1150 AppendixASummations

itholdsforn,andweprovethatitholdsfornC1.Wehave
xnC1 xnorte
kD kC.nC1/
kD1 kD1
1
re nnC1/C.nC1/
2
https://translate.googleusercontent.com/translate_f 188/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,

re 1 .nC1/.nC2/:
2
Youdon'talwaysneedtoguesstheexactvalueofasummationinordertouse
mathematicalinduction.Instead,youcanuseinductiontoproveaboundonasum
Pn
Macin.Asanexample,letusprovethatthegeometricseries
Pn kD03kisO.3n/.
Morespecifically,letusprovethatP0 kD03kc3nforsomeconstantc. Parael
initialconditionnD0,wehave 1.Assuming
kD03kD1c1aslongasc
thattheboundholdsforn,letusprovethatitholdsfornC1.Wehave
x
nC1 x norte
3k re 3kC3nC1
kD0 kD0
c3UN
nC3nC1
UN (bytheinductivehypothesis)
1 1
re do c3nC1
3 do
c3nC1
Pn
aslongas.1=3C1=c/1or,equivalently,c 3=2.As,
kD03kDO.3n/,
aswewishedtoshow.
Wehavetobecarefulwhenweuseasymptoticnotationtoproveboundsbyin
Pn
duction.Considerthefollowingfallaciousproofthat
P1 kD1kDOn/.Certainly,
kD1kDO.1/.Assumingthattheboundholdsforn,wenowproveitfornC1:
xnC1 xnorte
kD kC.nC1/
kD1 kD1
DOn/C.nC1/ wrong!!
DOnC1/:
Thebugintheargumentisthattheconstanthiddenbythebigohgrowswithn
andthusisnotconstant.Wehavenotshownthatthesameconstantworksforalln.

Boundingtheterms
Wecansometimesobtainagoodupperboundonaseriesbyboundingeachterm
oftheseries,anditoftensufficestousethelargesttermtoboundtheothers.por

Pgina272

A.2Boundingsummations 1151

example,aquickupperboundonthearithmeticseries(A.1)is
xnorte xnorte
k norte
kD1 kD1
Dn2:
Pn
Ingeneral,foraseries kD1ak,ifweletamaxDmax1knak,then
xnorte
aknamax:
kD1
Thetechniqueofboundingeachterminaseriesbythelargesttermisaweak
methodwhentheseriescaninfactbeboundedbyageometricseries.Dadoque
Pn
serie kD0ak,supposethatakC1=akrforallk 0,where0<r<1isa
constante.Wecanboundthesumbyaninfinitedecreasinggeometricseries,since
aka0rk,andthus
xnorte x1
ak UN a0rk
kD0 kD0
x1
Da0 rk
kD0
1
Da0 :
1r
P1
Wecanapplythismethodtoboundthesummation P1 kD1.k=3k/. Afindeque
startthesummationatkD0,werewriteitas kD0..kC1/=3kC1/. Elprimero
term(a0)is1=3,andtheratio(r)ofconsecutivetermsis

https://translate.googleusercontent.com/translate_f 189/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
.kC2/=3kC2 re 1 kC2
.kC1/=3kC1 3 kC1
2
UN
3
forallk 0.Thus,wehave
x1 K x1 kC1
re
3k 3kC1
kD1 kD0
1 1
UN
3 12=3
D1:

Pgina273

1152 AppendixASummations

Acommonbuginapplyingthismethodistoshowthattheratioofconsecu
tivetermsislessthan1andthentoassumethatthesummationisboundedbya
geometricseries.Anexampleistheinfiniteharmonicseries,whichdivergessince
x1 1 xnorte
1
Dlim
K n!1 K
kD1 kD1
Dlim .lgn/
n!1
D1:

Theratioofthe.kC1/standkthtermsinthisseriesisk=.kC1/<1,buttheseries
isnotboundedbyadecreasinggeometricseries.Toboundaseriesbyageometric
series,wemustshowthatthereisanr<1,whichisaconstant,suchthattheratio
ofallpairsofconsecutivetermsneverexceedsr.Intheharmonicseries,nosuchr
existsbecausetheratiobecomesarbitrarilycloseto1.

Splittingsummations
Onewaytoobtainboundsonadifficultsummationistoexpresstheseriesasthe
sumoftwoormoreseriesbypartitioningtherangeoftheindexandthentobound
eachoftheresultingseries.Forexample,supposewetrytofindalowerbound
Pn
onthearithmeticseries kD1k,whichwehavealreadyseenhasanupperbound
ofn2.Wemightattempttoboundeachterminthesummationbythesmallestterm,
butsincethattermis1,wegetalowerboundofnforthesummationfarofffrom
ourupperboundofn2.
Wecanobtainabetterlowerboundbyfirstsplittingthesummation.Assumefor
conveniencethatniseven.Tenemos
xnorte xn=2 xnorte
kD kC K
kD1 kD1 kDn=2C1
xn=2 xnorte
0C .n=2/
kD1 kDn=2C1
D.n=2/2
re .n2/
Pn
whichisanasymptoticallytightbound,since kD1kDOn2/.
Forasummationarisingfromtheanalysisofanalgorithm,wecanoftensplit
thesummationandignoreaconstantnumberoftheinitialterms.Generally,this
Pn
techniqueapplieswheneachtermakinasummation kD0akisindependentofn.

Pgina274

A.2Boundingsummations 1153

https://translate.googleusercontent.com/translate_f 190/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
Thenforanyconstantk0>0,wecanwrite
xnorte x 1
k 0 xnorte
ak re akC ak
kD0 kD0 kDk 0

xnorte
D.1/C ak
kDk 0

sincetheinitialtermsofthesummationareallconstantandthereareaconstant
Pn
numberofthem.Wecanthenuseothermethodstobound ak.Thistech
kDk 0
niqueappliestoinfinitesummationsaswell.Forexample,tofindanasymptotic
upperboundon
x1 k2

2k
kD0
weobservethattheratioofconsecutivetermsis
.kC1/2=2kC1 .kC1/2
re
k2=2k 2k2
8
UN
9
ifk 3.Thus,thesummationcanbesplitinto
x1 k2 x 2 k2 x1 k2
re do
2k 2k 2k
kD0 kD0 kD3 UNk
x 2 k2 9 x1 8
UN do
2k 8 9
kD0 kD0
DO.1/
sincethefirstsummationhasaconstantnumberoftermsandthesecondsummation
isadecreasinggeometricseries.
Thetechniqueofsplittingsummationscanhelpusdetermineasymptoticbounds
inmuchmoredifficultsituations.Forexample,wecanobtainaboundofO.lgn/
ontheharmonicseries(A.7):
xnorte
1
HnD :
K
kD1
Wedosobysplittingtherange1tonintoblgncC1piecesandupperbounding
thecontributionofeachpieceby1.ForiD01:::blgnc,theithpiececonsists

Pgina275

1154 AppendixASummations

ofthetermsstartingat1=2iandgoinguptobutnotincluding1=2iC1.Thelast
piecemightcontaintermsnotintheoriginalharmonicseries,andthuswehave
xnorte
1 x 2 x 1
blgnc i
1
UN
K 2iCj
kD1 ID0 jD0
x 2 x 11
blgnc i

UN
2i
ID0 jD0
x
blgnc
re 1
ID0
lgnC1: (A.10)

Approximationbyintegrals
Pn
Whenasummationhastheform kDmf.k/,wheref.k/isamonotonicallyin
creasingfunction,wecanapproximateitbyintegrals:
Zn xnorte Z nC1

fx/dx f.k/ f.x/dx: (A.11)


m1 kDm metro

https://translate.googleusercontent.com/translate_f 191/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
FigureA.1justifiesthisapproximation.Thesummationisrepresentedasthearea
oftherectanglesinthefigure,andtheintegralistheshadedregionunderthecurve.
Whenf.k/isamonotonicallydecreasingfunction,wecanuseasimilarmethod
toprovidethebounds
ZnC1 xnorte Zn
fx/dx f.k/ f.x/dx: (A.12)
metro kDm m1
Theintegralapproximation(A.12)givesatightestimateforthenthharmonic
nmero.Foralowerbound,weobtain
xnorte ZnC1
1 dx
K 1 x
kD1
Dln.nC1/: (A.13)
Fortheupperbound,wederivetheinequality
xnorte Zn
1 dx
UN
K 1 x
kD2
Dlnn

Pgina276

A.2Boundingsummations 1155

f(x)

F
(metro F F
F (metro (norte F
( F
(metro +1)
) +2) ... ... 2) norte
1) (norte
)

x
m1 metrom+1 m+2 ... ... n2 n1 norten+1
(un)

f(x)

F F F F F
(metro (metro
+1)
(metro
+2) ...
(norte
2)
(norte
1)
F
(norte
) ... )

x
m1 metrom+1 m+2 ... ... n2 n1 norten+1
(segundo)

Pn
FigureA.1Approximationof kDmf.k/byintegrals.Theareaofeachrectangleisshown
withintherectangle,andthetotalrectanglearearepresentsthevalueofthesummation.Elin
tegralisrepresentedbytheshadedareaunderthecurve.Bycomparingareasin(a),weget
Rn Pn
Pm1
n fx/dx RnC1 kDmf.k/,andthenbyshiftingtherectanglesoneunittotheright,weget
kDmf.k/ metro fx/dxin(b).

https://translate.googleusercontent.com/translate_f 192/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,

Pgina277

1156 AppendixASummations

whichyieldsthebound
xnorte
1
lnnC1: (A.14)
K
kD1

Exercises

A.21 Pn
Showthat kD11=k2isboundedabovebyaconstant.

A.22
Findanasymptoticupperboundonthesummation
x
blgnc
n=2k :
kD0

A.23
Showthatthenthharmonicnumberis.lgn/bysplittingthesummation.

A.24 Pn
Aproximado
kD1k3withanintegral.
A.25 Pn
Whydidn'tweusetheintegralapproximation(A.12)directlyon
kD11=kto
obtainanupperboundonthenthharmonicnumber?

Problemas

A1Boundingsummations
Giveasymptoticallytightboundsonthefollowingsummations.Assumethatr 0
ands 0areconstants.
x norte
a. kr.
kD1
x norte
segundo.
lgsk.
kD1

Pgina278

NotesforAppendixA 1157

x norte
do. krlgsk.
kD1

Appendixnotes

Knuth[209]providesanexcellentreferenceforthematerialpresentedhere.Usted
canfindbasicpropertiesofseriesinanygoodcalculusbook,suchasApostol[18]
orThomasetal.[334].

https://translate.googleusercontent.com/translate_f 193/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,

Page279

segundo Sets,Etc.

Manychaptersofthisbooktouchontheelementsofdiscretemathematics.Esta
appendixreviewsmorecompletelythenotations,definitions,andelementaryprop
ertiesofsets,relations,functions,graphs,andtrees.Ifyouarealreadywellversed
inthismaterial,youcanprobablyjustskimthischapter.

B.1Sets

Asetisacollectionofdistinguishableobjects,calleditsmembersorelements.Si
anobjectxisamemberofasetS,wewritex2S(readxisamemberofS
or,morebriefly,xisinS).IfxisnotamemberofS,wewritex62S.We
candescribeasetbyexplicitlylistingitsmembersasalistinsidebraces.por
example,wecandefineasetStocontainpreciselythenumbers1,2,and3by
writingSDf123g.Since2isamemberofthesetS,wecanwrite22S,and
since4isnotamember,wehave4S.Asetcannotcontainthesameobjectmore
thanonce,1anditselementsarenotordered.TwosetsAandBareequal,written
ADB,iftheycontainthesameelements.Forexample,f1231gDf123gD
f321g.
Weadoptspecialnotationsforfrequentlyencounteredsets:
denotestheemptyset,thatis,thesetcontainingnomembers.
Zdenotesthesetofintegers,thatis,thesetf:::21012:::g.
Rdenotesthesetofrealnumbers.
Ndenotesthesetofnaturalnumbers,thatis,thesetf012:::g.2

https://translate.googleusercontent.com/translate_f 194/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,

1Avariationofaset,whichcancontainthesameobjectmorethanonce,iscalledamultiset.
2Someauthorsstartthenaturalnumberswith1insteadof0.Themoderntrendseemstobetostart
with0.

Pgina280

B.1Sets 1159

IfalltheelementsofasetAarecontainedinasetB,thatis,ifx2Aimplies
x2B,thenwewriteABandsaythatAisasubsetofB.AsetAisa
propersubsetofB,writtenA B,ifABbutAB.(Someauthorsusethe
symboltodenotetheordinarysubsetrelation,ratherthanthepropersubset
relation.)ForanysetA,wehaveAA.FortwosetsAandB,wehaveADB
ifandonlyifABandBA.ForanythreesetsA,B,andC,ifAB
andBC,thenAC.ForanysetA,wehaveA.
Wesometimesdefinesetsintermsofothersets.GivenasetA,wecandefinea
setBAbystatingapropertythatdistinguishestheelementsofB.Forexample,
wecandefinethesetofevenintegersbyfxWx2Zandx=2isanintegerg.los
coloninthisnotationisreadsuchthat.(Someauthorsuseaverticalbarinplace
ofthecolon.)
GiventwosetsAandB,wecanalsodefinenewsetsbyapplyingsetoperations:
TheintersectionofsetsAandBistheset

A\BDfxWx2Aandx2Bg:

TheunionofsetsAandBistheset

A[BDfxWx2Aorx2Bg:

ThedifferencebetweentwosetsAandBistheset

ABDfxWx2AandxBg:

Setoperationsobeythefollowinglaws:

Emptysetlaws:
A\D
A[DA:

Idempotencylaws:
A\ADA
A[ADA:

Commutativelaws:
A\BDB\A
A[BDB[A:

Page281

1160 AppendixBSets,Etc.

UN segundo UN segundo UN segundo UN segundo UN segundo


re re [

do do do do do
UN .B\C/ re A.B\C/ re .AB/ [ .AC/

https://translate.googleusercontent.com/translate_f 195/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
andCisrepresentedasacircle.
FigureB.1AVenndiagramillustratingthefirstofDeMorgan'slaws(B.2).EachofthesetsA,B,

Associativelaws:
A\.B\C/D.A\B/\C
A[.B[C/D.A[B/[C:

Distributivelaws:

A\.B[C/D.A\B/[.A\C/
(B.1)
A[.B\C/D.A[B/\.A[C/:

Absorptionlaws:
A\.A[B/DA
A[.A\B/DA:

DeMorgan'slaws:

A.B\C/D.AB/[.AC/
(B.2)
A.B[C/D.AB/\.AC/:

FigureB.1illustratesthefirstofDeMorgan'slaws,usingaVenndiagram:agraph
icalpictureinwhichsetsarerepresentedasregionsoftheplane.
Often,allthesetsunderconsiderationaresubsetsofsomelargersetUcalledthe
universe.Forexample,ifweareconsideringvarioussetsmadeuponlyofintegers,
thesetZofintegersisanappropriateuniverse.GivenauniverseU,wedefinethe
complementofasetAasADU ADfxWx2Uandx62Ag.Foranyset
AU,wehavethefollowinglaws:

ADA
A\AD
A[ADU:

Pgina282

B.1Sets 1161

WecanrewriteDeMorgan'slaws(B.2)withsetcomplements.Foranytwosets
BCU,wehave
B\CDB[C
B[CDB\C:
TwosetsAandBaredisjointiftheyhavenoelementsincommon,thatis,if
A\BD.AcollectionSDfSigofnonemptysetsformsapartitionofasetSif
thesetsarepairwisedisjoint,thatis,SiSj2SandijimplySi\SjD,
y
theirunionisS,thatis,
[
SD Si:
S 2S
i

Inotherwords,SformsapartitionofSifeachelementofSappearsinexactly
oneSi2S.
Thenumberofelementsinasetisthecardinality(orsize)oftheset,denotedjSj.
Twosetshavethesamecardinalityiftheirelementscanbeputintoaonetoone
correspondencia.ThecardinalityoftheemptysetisjjD0.Ifthecardinalityofa
setisanaturalnumber,wesaythesetisfiniteotherwise,itisinfinite.Aninfinite
setthatcanbeputintoaonetoonecorrespondencewiththenaturalnumbersNis
countablyinfiniteotherwise,itisuncountable.Forexample,theintegersZare
countable,buttherealsRareuncountable.
ForanytwofinitesetsAandB,wehavetheidentity
jA[BjDjAjCjBjjA\Bj (B.3)
fromwhichwecanconcludethat
jA[BjjAjCjBj:
https://translate.googleusercontent.com/translate_f 196/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,

IfAandBaredisjoint,thenjA\BjD0andthusjA[BjDjAjCjBj.Si
AB,thenjAjjBj.
Afinitesetofnelementsissometimescalledannset.A1setiscalleda
singleton.Asubsetofkelementsofasetissometimescalledaksubset.
WedenotethesetofallsubsetsofasetS,includingtheemptysetandSitself,
by2Swecall2SthepowersetofS.Forexample,2fabgDffagfbgfabgg.
ThepowersetofafinitesetShascardinality2jSj(seeExerciseB.15).
Wesometimescareaboutsetlikestructuresinwhichtheelementsareordered.
Anorderedpairoftwoelementsaandbisdenoted.ab/andisdefinedformally
astheset.ab/Dfafabgg.Thus,theorderedpair.ab/isnotthesameasthe
orderedpair.ba/.

Pgina283

1162 AppendixBSets,Etc.

TheCartesianproductoftwosetsAandB,denotedAB,isthesetofall
orderedpairssuchthatthefirstelementofthepairisanelementofAandthe
secondisanelementofB.Moreformally,
ABDfab/Wa2Aandb2Bg:

Forexample,fabgfasegundocgDf.aa/.ab/.ac/.ba/.bb/.bc/g.Cuando
AandBarefinitesets,thecardinalityoftheirCartesianproductis

jABjDjAjjBj: (B.4)
TheCartesianproductofnsetsA1A2:::Anisthesetofntuples
A1 A2 AnDfa1a2:::an/Wai2AiforiD12:::ng

whosecardinalityis
jA1 A2 AnjDjA1jjA2jjAnj

ifallsetsarefinite.WedenoteannfoldCartesianproductoverasinglesetAby
theset
AnDAA A

whosecardinalityisjAnjDjAj norte
ifAisfinite.Wecanalsoviewanntupleasa
finitesequenceoflengthn(seepage1166).

Exercises

B.11
DrawVenndiagramsthatillustratethefirstofthedistributivelaws(B.1).

B.12
ProvethegeneralizationofDeMorgan'slawstoanyfinitecollectionofsets:
A1\A2\\An DA1[A2[[An
A1[A2[[An DA1\A2\\An:

Page284

B.2Relations 1163

B.13?

https://translate.googleusercontent.com/translate_f 197/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
Provethegeneralizationofequation(B.3),whichiscalledtheprincipleofinclu
sionandexclusion:
jA1[A2[[AnjD
jA1jCjA2jCCjAnj
jA1\A2jjA1\A3j (allpairs)
CjA1\A2\A3jC (alltriples)
Unesdoc.unesco.orgunesdoc.unesco.org

C.1/n1jA1\A2\\Anj:

B.14
Showthatthesetofoddnaturalnumbersiscountable.

B.15
ShowthatforanyfinitesetS,thepowerset2Shas2jSjelements(thatis,there
are2jSjdistinctsubsetsofS).

B.16
Giveaninductivedefinitionforanntuplebyextendingthesettheoreticdefinition
foranorderedpair.

B.2Relations

AbinaryrelationRontwosetsAandBisasubsetoftheCartesianproductAB.
If.ab/2R,wesometimeswriteaRb.WhenwesaythatRisabinaryrelation
onasetA,wemeanthatRisasubsetofAA.Forexample,thelessthan
relationonthenaturalnumbersisthesetfab/Wab2Nanda<bg.Annary
relationonsetsA1A2:::AnisasubsetofA1 A2 An.
AbinaryrelationRAAisreflexiveif
aRa
foralla2A.Forexample,DandarereflexiverelationsonN,but<is
no.TherelationRissymmetricif
aRbimpliesbRa
forallab2A.Forexample,Dissymmetric,but<andarenot.los
relationRistransitiveif
aRbandbRcimplyaRc

Pgina285

1164 AppendixBSets,Etc.

forallasegundoc2A.Forexample,therelations<,,andDaretransitive,but
therelationRDfab/Wab2NandaDb1gisnot,since3R4and4R5
donotimply3R5.
Arelationthatisreflexive,symmetric,andtransitiveisanequivalencerelation.
Forexample,Disanequivalencerelationonthenaturalnumbers,but<isnot.
IfRisanequivalencerelationonasetA,thenfora2A,theequivalenceclass
ofaisthesetaDfb2AWaRbg,thatis,thesetofallelementsequivalenttoa.
Forexample,ifwedefineRDfab/Wab2NandaCbisanevennumberg,
thenRisanequivalencerelation,sinceaCaiseven(reflexive),aCbiseven
impliesbCaiseven(symmetric),andaCbisevenandbCcisevenimply
aCciseven(transitive).Theequivalenceclassof4is4Df0246:::g,and
theequivalenceclassof3is3Df1357:::g.Abasictheoremofequivalence
classesisthefollowing.

TheoremB.1(Anequivalencerelationisthesameasapartition)
TheequivalenceclassesofanyequivalencerelationRonasetAformapartition
ofA,andanypartitionofAdeterminesanequivalencerelationonAforwhichthe
setsinthepartitionaretheequivalenceclasses.

ProofForthefirstpartoftheproof,wemustshowthattheequivalenceclasses
ofRarenonempty,pairwisedisjointsetswhoseunionisA.BecauseRisreflex
ive,a2a,andsotheequivalenceclassesarenonemptymoreover,sinceevery
elementa2Abelongstotheequivalenceclassa,theunionoftheequivalence

https://translate.googleusercontent.com/translate_f 198/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
classesisA.Itremainstoshowthattheequivalenceclassesarepairwisedisjoint,
thatis,iftwoequivalenceclassesaandbhaveanelementcincommon,then
theyareinfactthesameset.SupposethataRcandbRc.Bysymmetry,cRb,
andbytransitivity,aRb.Thus,foranyarbitraryelementx2a,wehavexRa
and,bytransitivity,xRb,andthusab.Similarly,ba,andthus
aDb.
Forthesecondpartoftheproof,letADfAigbeapartitionofA,anddefine
RDfab/Wthereexistsisuchthata2Aiandb2Aig.WeclaimthatRisan
equivalencerelationonA.Reflexivityholds,sincea2AiimpliesaRa.Symme
tryholds,becauseifaRb,thenaandbareinthesamesetAi,andhencebRa.
IfaRbandbRc,thenallthreeelementsareinthesamesetAi,andthusaRc
andtransitivityholds.Toseethatthesetsinthepartitionaretheequivalence
classesofR,observethatifa2Ai,thenx2aimpliesx2Ai,andx2Ai
impliesx2a.

AbinaryrelationRonasetAisantisymmetricif
aRbandbRaimplyaDb:

Pgina286

B.2Relations 1165

Forexample,therelationonthenaturalnumbersisantisymmetric,sinceab
andbaimplyaDb.Arelationthatisreflexive,antisymmetric,andtransitive
isapartialorder,andwecallasetonwhichapartialorderisdefinedapartially
orderedset.Forexample,therelationisadescendantofisapartialorderonthe
setofallpeople(ifweviewindividualsasbeingtheirowndescendants).
InapartiallyorderedsetA,theremaybenosinglemaximumelementasuch
thatbRaforallb2A.Instead,thesetmaycontainseveralmaximalelementsa
suchthatfornob2A,whereba,isitthecasethataRb.Porejemplo,un
collectionofdifferentsizedboxesmaycontainseveralmaximalboxesthatdon't
fitinsideanyotherbox,yetithasnosinglemaximumboxintowhichanyother
boxwillfit.3
ArelationRonasetAisatotalrelationifforallab2A,wehaveaRb
orbRa(orboth),thatis,ifeverypairingofelementsofAisrelatedbyR.A
partialorderthatisalsoatotalrelationisatotalorderorlinearorder.Porejemplo,
therelationisatotalorderonthenaturalnumbers,buttheisadescendant
ofrelationisnotatotalorderonthesetofallpeople,sincethereareindividuals
neitherofwhomisdescendedfromtheother.Atotalrelationthatistransitive,but
notnecessarilyreflexiveandantisymmetric,isatotalpreorder.

Exercises

B.21
ProvethatthesubsetrelationonallsubsetsofZisapartialorderbutnota
totalorder.

B.22
Showthatforanypositiveintegern,therelationequivalentmodulonisanequiv
alencerelationontheintegers.(Wesaythatab.modn/ifthereexistsan
integerqsuchthatabDqn.)Intowhatequivalenceclassesdoesthisrelation
partitiontheintegers?

B.23
Giveexamplesofrelationsthatare
a.reflexiveandsymmetricbutnottransitive,
segundo.reflexiveandtransitivebutnotsymmetric,
do.symmetricandtransitivebutnotreflexive.

3Tobeprecise,inorderforthefitinsiderelationtobeapartialorder,weneedtoviewaboxas
fittinginsideitself.

https://translate.googleusercontent.com/translate_f 199/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,

Pgina287

1166 AppendixBSets,Etc.

B.24
LetSbeafiniteset,andletRbeanequivalencerelationonS S.Showthatif
inadditionRisantisymmetric,thentheequivalenceclassesofSwithrespecttoR
aresingletons.

B.25
ProfessorNarcissusclaimsthatifarelationRissymmetricandtransitive,thenitis
alsoreflexive.Heoffersthefollowingproof.Bysymmetry,aRbimpliesbRa.
Transitivity,therefore,impliesaRa.Istheprofessorcorrect?

B.3Functions

GiventwosetsAandB,afunctionfisabinaryrelationonAandBsuchthat
foralla2A,thereexistspreciselyoneb2Bsuchthat.ab/2f.ThesetAis
calledthedomainoff,andthesetBiscalledthecodomainoff.Wesometimes
writefWA!Bandif.ab/2f,wewritebDf.a/,sincebisuniquely
determinedbythechoiceofa.
Intuitively,thefunctionfassignsanelementofBtoeachelementofA.No
elementofAisassignedtwodifferentelementsofB,butthesameelementofB
canbeassignedtotwodifferentelementsofA.Forexample,thebinaryrelation

fDfab/Wab2NandbDamod2g
isafunctionfWN!f01g,sinceforeachnaturalnumbera,thereisexactlyone
valuebinf01gsuchthatbDamod2.Forthisexample,0Df.0/,1Df.1/,
0Df.2/,etc.Incontrast,thebinaryrelation
gDfab/Wab2NandaCbiseveng

isnotafunction,since.13/and.15/arebothing,andthusforthechoiceaD1,
thereisnotpreciselyonebsuchthat.ab/2g.
GivenafunctionfWA!B,ifbDf.a/,wesaythataistheargumentoff
andthatbisthevalueoffata.Wecandefineafunctionbystatingitsvaluefor
everyelementofitsdomain.Forexample,wemightdefinef.n/D2nforn2N,
whichmeansfDfn2n/Wn2Ng.Twofunctionsfandgareequalifthey
havethesamedomainandcodomainandif,forallainthedomain,f.a/Dga/.
Afinitesequenceoflengthnisafunctionfwhosedomainisthesetofn
integersf01:::n1g.Weoftendenoteafinitesequencebylistingitsvalues:
hf.0/f.1/:::f.n1/i.Aninfinitesequenceisafunctionwhosedomainis
thesetNofnaturalnumbers.Forexample,theFibonaccisequence,definedby
recurrence(3.22),istheinfinitesequenceh01123581321:::i.

Pgina288

B.3Functions 1167

WhenthedomainofafunctionfisaCartesianproduct,weoftenomittheextra
parenthesessurroundingtheargumentoff.Forexample,ifwehadafunction
fWA1 A2 An!B,wewouldwritebDf.a1a2:::an/instead
ofbDf..a1a2:::an//.Wealsocalleachaianargumenttothefunctionf,
thoughtechnicallythe(single)argumenttofisthentuple.a1a2:::an/.
IffWA!BisafunctionandbDf.a/,thenwesometimessaythatbisthe
imageofaunderf.TheimageofasetA0Aunderfisdefinedby
f.A0/Dfb2BWbDf.a/forsomea2A0g:
Therangeoffistheimageofitsdomain,thatis,f.A/.Forexample,therange
ofthefunctionfWN!Ndefinedbyf.n/D2nisfN/DfmWmD2nfor
somen2Ng,inotherwords,thesetofnonnegativeevenintegers.
Afunctionisasurjectionifitsrangeisitscodomain.Forexample,thefunction
f.n/Dbn=2cisasurjectivefunctionfromNtoN,sinceeveryelementinN
appearsasthevalueoffforsomeargument.Incontrast,thefunctionf.n/D2n
isnotasurjectivefunctionfromNtoN,sincenoargumenttofcanproduce3asa
valor.Thefunctionf.n/D2nis,however,asurjectivefunctionfromthenatural
https://translate.googleusercontent.com/translate_f 200/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
numberstotheevennumbers.AsurjectionfWA!Bissometimesdescribedas
mappingAontoB.Whenwesaythatfisonto,wemeanthatitissurjective.
AfunctionfWA!Bisaninjectionifdistinctargumentstofproduce
distinctvalues,thatis,ifaa0impliesf.a/f.a0/.Forexample,thefunction
f.n/D2nisaninjectivefunctionfromNtoN,sinceeachevennumberbisthe
imageunderfofatmostoneelementofthedomain,namelyb=2.Thefunction
f.n/Dbn=2cisnotinjective,sincethevalue1isproducedbytwoarguments:2
and3.Aninjectionissometimescalledaonetoonefunction.
AfunctionfWA!Bisabijectionifitisinjectiveandsurjective.Porejemplo,
thefunctionf.n/D.1/ndn=2eisabijectionfromNtoZ:
0!0
1! 1
2!1
3! 2
4!2
Unesdoc.unesco.orgunesdoc.unesco.org

Thefunctionisinjective,sincenoelementofZistheimageofmorethanone
elementofN.Itissurjective,sinceeveryelementofZappearsastheimageof
someelementofN.Hence,thefunctionisbijective.Abijectionissometimes
calledaonetoonecorrespondence,sinceitpairselementsinthedomainand
codomain.AbijectionfromasetAtoitselfissometimescalledapermutation.
Whenafunctionfisbijective,wedefineitsinversef1as
f1.b/Daifandonlyiff.a/Db:

Pgina289

1168 AppendixBSets,Etc.

Forexample,theinverseofthefunctionf.n/D.1/ndn=2eis
(
2m ifm 0
f1.m/D
2m1ifm<0:

Exercises

B.31
LetAandBbefinitesets,andletfWA!Bbeafunction.Showthat
a.iffisinjective,thenjAjjBj
segundo.iffissurjective,thenjAjjBj.

B.32
Isthefunctionf.x/DxC1bijectivewhenthedomainandthecodomainareN?
IsitbijectivewhenthedomainandthecodomainareZ?

B.33
Giveanaturaldefinitionfortheinverseofabinaryrelationsuchthatifarelation
isinfactabijectivefunction,itsrelationalinverseisitsfunctionalinverse.

B.34?
GiveabijectionfromZtoZZ.

B.4Graphs

Thissectionpresentstwokindsofgraphs:directedandundirected.Certaindef
initionsintheliteraturedifferfromthosegivenhere,butforthemostpart,the
differencesareslight.Section22.1showshowwecanrepresentgraphsincom
putermemory.
Adirectedgraph(ordigraph)Gisapair.VE/,whereVisafinitesetandE
isabinaryrelationonV.ThesetViscalledthevertexsetofG,anditselements
arecalledvertices(singular:vertex).ThesetEiscalledtheedgesetofG,andits
elementsarecallededges.FigureB.2(a)isapictorialrepresentationofadirected
graphonthevertexsetf123456g.Verticesarerepresentedbycirclesinthe
figure,andedgesarerepresentedbyarrows.Notethatselfloopsedgesfroma
https://translate.googleusercontent.com/translate_f 201/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
vertextoitselfarepossible.
InanundirectedgraphGD.VE/,theedgesetEconsistsofunordered
pairsofvertices,ratherthanorderedpairs.Thatis,anedgeisasetfug,where

Pgina290

B.4Graphs 1169

1 2 3 1 2 3 1 2 3

4 5 6 4 5 6 6
(un) (segundo) (do)

FigureB.2Directedandundirectedgraphs.(a)AdirectedgraphGD.VE/,whereVD
f123456gandEDf.12/.22/.24/.25/.41/.45/.54/.63/g.Theedge.22
isaselfloop.(b)AnundirectedgraphGD.VE/,whereVDf123456gandED
f.12/.15/.25/.36/g.Thevertex4isisolated.(c)Thesubgraphofthegraphinpart(a)
inducedbythevertexsetf1236g.

u2Vandu.Byconvention,weusethenotation.u/foranedge,rather
thanthesetnotationfug,andweconsider.u/and.u/tobethesameedge.
Inanundirectedgraph,selfloopsareforbidden,andsoeveryedgeconsistsoftwo
distinctvertices.FigureB.2(b)isapictorialrepresentationofanundirectedgraph
onthevertexsetf123456g.
Manydefinitionsfordirectedandundirectedgraphsarethesame,althoughcer
taintermshaveslightlydifferentmeaningsinthetwocontexts.If.u/isanedge
inadirectedgraphGD.VE/,wesaythat.u/isincidentfromorleaves
vertexuandisincidenttoorentersvertex.Forexample,theedgesleavingver
tex2inFigureB.2(a)are.22/,.24/,and.25/.Theedgesenteringvertex2are
.12/and.22/.If.u/isanedgeinanundirectedgraphGD.VE/,wesay
that.u/isincidentonverticesuand.InFigureB.2(b),theedgesincidenton
vertex2are.12/and.25/.
If.u/isanedgeinagraphGD.VE/,wesaythatvertexisadjacentto
vertexu.Whenthegraphisundirected,theadjacencyrelationissymmetric.Cuando
thegraphisdirected,theadjacencyrelationisnotnecessarilysymmetric.Ifis
adjacenttouinadirectedgraph,wesometimeswriteu!.Inparts(a)and(b)
ofFigureB.2,vertex2isadjacenttovertex1,sincetheedge.12/belongstoboth
Grficos.Vertex1isnotadjacenttovertex2inFigureB.2(a),sincetheedge.21/
doesnotbelongtothegraph.
Thedegreeofavertexinanundirectedgraphisthenumberofedgesincidenton
eso.Forexample,vertex2inFigureB.2(b)hasdegree2.Avertexwhosedegreeis0,
suchasvertex4inFigureB.2(b),isisolated.Inadirectedgraph,theoutdegree
ofavertexisthenumberofedgesleavingit,andtheindegreeofavertexisthe
numberofedgesenteringit.Thedegreeofavertexinadirectedgraphisitsin

Page291

1170 AppendixBSets,Etc.

degreeplusitsoutdegree.Vertex2inFigureB.2(a)hasindegree2,outdegree3,
anddegree5.
Apathoflengthkfromavertexutoavertexu0inagraphGD.VE/
isasequenceh012:::kiofverticessuchthatuD0,u0Dk,and
.i1i/2EforiD12:::KThelengthofthepathisthenumberof
edgesinthepath.Thepathcontainsthevertices01:::kandtheedges
.01/.12/:::.k1k/.(Thereisalwaysa0lengthpathfromutou.)If
thereisapathpfromutou0,wesaythatu0isreachablefromuviap,whichwe
sometimeswriteasu u pag0ifGisdirected.Apathissimple4ifallverticesinthe

https://translate.googleusercontent.com/translate_f 202/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
patharedistinct.InFigureB.2(a),thepathh1254iisasimplepathoflength3.
Thepathh2545iisnotsimple.
AsubpathofpathpDh01:::kiisacontiguoussubsequenceofitsver
TicesThatis,forany0ijk,thesubsequenceofverticeshiiC1:::ji
isasubpathofp.
Inadirectedgraph,apathh01:::kiformsacycleif0Dkandthe
pathcontainsatleastoneedge.Thecycleissimpleif,inaddition,12:::K
aredistinct.Aselfloopisacycleoflength1.Twopathsh012:::k10i
andh000102:::0
k10 0iformthesamecycleifthereexistsanintegerjsuch
that0 iD.iCj/modkforiD01:::k1.InFigureB.2(a),thepathh1241i
formsthesamecycleasthepathsh2412iandh4124i.Thiscycleissimple,
butthecycleh124541iisnot.Thecycleh22iformedbytheedge.22/is
aselfloop.Adirectedgraphwithnoselfloopsissimple.Inanundirectedgraph,
apathh01:::kiformsacycleifk 3and0Dkthecycleissimpleif
12:::karedistinct.Forexample,inFigureB.2(b),thepathh1251iisa
simplecycle.Agraphwithnocyclesisacyclic.
Anundirectedgraphisconnectedifeveryvertexisreachablefromallother
VrticesTheconnectedcomponentsofagrapharetheequivalenceclassesof
verticesundertheisreachablefromrelation.ThegraphinFigureB.2(b)has
threeconnectedcomponents:f125g,f36g,andf4g.Everyvertexinf125gis
reachablefromeveryothervertexinf125g.Anundirectedgraphisconnected
ifithasexactlyoneconnectedcomponent.Theedgesofaconnectedcomponent
arethosethatareincidentononlytheverticesofthecomponentinotherwords,
edge.u/isanedgeofaconnectedcomponentonlyifbothuandarevertices
ofthecomponent.
Adirectedgraphisstronglyconnectedifeverytwoverticesarereachablefrom
Elunoalotro.Thestronglyconnectedcomponentsofadirectedgrapharetheequiv

4Someauthorsrefertowhatwecallapathasawalkandtowhatwecallasimplepathasjusta
path.Weusethetermspathandsimplepaththroughoutthisbookinamannerconsistentwith
theirdefinitions.

Pgina292

B.4Graphs 1171

1 2 1 2

GRAMO 6 3
5 3
5 4 4

G Usted V W x Y Z Usted V W x Y

(un) (segundo)

FigureB.3(a)Apairofisomorphicgraphs.Theverticesofthetopgrapharemappedtothe
verticesofthebottomgraphbyf.1/Duf.2/Df.3/Dwf.4/Dxf.5/Dyf.6/D.
(b)Twographsthatarenotisomorphic,sincethetopgraphhasavertexofdegree4andthebottom
graphdoesnot.

alenceclassesofverticesunderthearemutuallyreachablerelation.Adirected
graphisstronglyconnectedifithasonlyonestronglyconnectedcomponent.los
graphinFigureB.2(a)hasthreestronglyconnectedcomponents:f1245g,f3g,
andf6g.Allpairsofverticesinf1245garemutuallyreachable.Thever
ticesf36gdonotformastronglyconnectedcomponent,sincevertex6cannot
bereachedfromvertex3.
TwographsGD.VE/andG0D.V0E0/areisomorphicifthereexistsa
bijectionfWV!V0suchthat.u/2Eifandonlyif.f.u/F.//2E0.
Inotherwords,wecanrelabeltheverticesofGtobeverticesofG0,maintain
ingthecorrespondingedgesinGandG0.FigureB.3(a)showsapairofiso
morphicgraphsGandG0withrespectivevertexsetsVDf123456gand
V0Dfuwxyg.ThemappingfromVtoV0givenbyf.1/Duf.2/D

https://translate.googleusercontent.com/translate_f 203/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
f.3/Dwf.4/Dxf.5/Dyf.6/Dprovidestherequiredbijectivefunc
Cin.ThegraphsinFigureB.3(b)arenotisomorphic.Althoughbothgraphshave
5verticesand7edges,thetopgraphhasavertexofdegree4andthebottomgraph
doesnot.
WesaythatagraphG0D.V0E0/isasubgraphofGD.VE/ifV0V
andE0E.GivenasetV0V,thesubgraphofGinducedbyV0isthegraph
G0D.V0E0/,where

E0Dfu/2EWu2V0g:

Pgina293

1172 AppendixBSets,Etc.

Thesubgraphinducedbythevertexsetf1236ginFigureB.2(a)appearsin
FigureB.2(c)andhastheedgesetf.12/.22/.63/g.
GivenanundirectedgraphGD.VE/,thedirectedversionofGisthedirected
graphG0D.VE0/,where.u/2E0ifandonlyif.u/2E.Thatis,we
replaceeachundirectededge.u/inGbythetwodirectededges.u/and.u/
inthedirectedversion.GivenadirectedgraphGD.VE/,theundirectedversion
ofGistheundirectedgraphG0D.VE0/,where.u/2E0ifandonlyifu
and.u/2E.Thatis,theundirectedversioncontainstheedgesofGwith
theirdirectionsremovedandwithselfloopseliminated.(Since.u/and.u/
arethesameedgeinanundirectedgraph,theundirectedversionofadirected
graphcontainsitonlyonce,evenifthedirectedgraphcontainsbothedges.u/
Y.u/.)InadirectedgraphGD.VE/,aneighborofavertexuisanyvertex
thatisadjacenttouintheundirectedversionofG.Thatis,isaneighborofuif
uandeither.u/2Eor.u/2E.Inanundirectedgraph,uandare
neighborsiftheyareadjacent.
Severalkindsofgraphshavespecialnames.Acompletegraphisanundirected
graphinwhicheverypairofverticesisadjacent.Abipartitegraphisanundirected
graphGD.VE/inwhichVcanbepartitionedintotwosetsV1andV2suchthat
.u/2Eimplieseitheru2V1and2V2oru2V2and2V1.Thatis,all
edgesgobetweenthetwosetsV1andV2.Anacyclic,undirectedgraphisaforest,
andaconnected,acyclic,undirectedgraphisa(free)tree(seeSectionB.5).Nosotros
oftentakethefirstlettersofdirectedacyclicgraphandcallsuchagraphadag.
Therearetwovariantsofgraphsthatyoumayoccasionallyencounter.Amulti
graphislikeanundirectedgraph,butitcanhavebothmultipleedgesbetweenver
ticesandselfloops.Ahypergraphislikeanundirectedgraph,buteachhyperedge,
ratherthanconnectingtwovertices,connectsanarbitrarysubsetofvertices.Muchos
algorithmswrittenforordinarydirectedandundirectedgraphscanbeadaptedto
runonthesegraphlikestructures.
ThecontractionofanundirectedgraphGD.VE/byanedgeeD.u/isa
graphG0D.V0E0/,whereV0DV fug[fxgandxisanewvertex.Theset
ofedgesE0isformedfromEbydeletingtheedge.u/and,foreachvertexw
incidentonuor,deletingwhicheverof.uw/and.w/isinEandaddingthe
newedge.xw/.Ineffect,uandarecontractedintoasinglevertex.

Exercises

B.41
Attendeesofafacultypartyshakehandstogreeteachother,andeachprofessor
remembershowmanytimesheorsheshookhands.Attheendoftheparty,the
departmentheadaddsupthenumberoftimesthateachprofessorshookhands.

Page294

B.5Trees 1173

Showthattheresultisevenbyprovingthehandshakinglemma:ifGD.VE/is
anundirectedgraph,then
x

https://translate.googleusercontent.com/translate_f 204/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
degree./D2jEj:
2V

B.42
Showthatifadirectedorundirectedgraphcontainsapathbetweentwoverticesu
and,thenitcontainsasimplepathbetweenuand.Showthatifadirectedgraph
containsacycle,thenitcontainsasimplecycle.

B.43
Showthatanyconnected,undirectedgraphGD.VE/satisfiesjEjjVj1.

B.44
Verifythatinanundirectedgraph,theisreachablefromrelationisanequiv
alencerelationontheverticesofthegraph.Whichofthethreepropertiesofan
equivalencerelationholdingeneralfortheisreachablefromrelationonthe
verticesofadirectedgraph?

B.45
WhatistheundirectedversionofthedirectedgraphinFigureB.2(a)?Culesel
directedversionoftheundirectedgraphinFigureB.2(b)?

B.46?
Showthatwecanrepresentahypergraphbyabipartitegraphifweletincidencein
thehypergraphcorrespondtoadjacencyinthebipartitegraph.(Hint:Letoneset
ofverticesinthebipartitegraphcorrespondtoverticesofthehypergraph,andlet
theothersetofverticesofthebipartitegraphcorrespondtohyperedges.)

B.5Trees

Aswithgraphs,therearemanyrelated,butslightlydifferent,notionsoftrees.Esta
sectionpresentsdefinitionsandmathematicalpropertiesofseveralkindsoftrees.
Sections10.4and22.1describehowwecanrepresenttreesincomputermemory.

B.5.1Freetrees

AsdefinedinSectionB.4,afreetreeisaconnected,acyclic,undirectedgraph.Nosotros
oftenomittheadjectivefreewhenwesaythatagraphisatree.Ifanundirected
graphisacyclicbutpossiblydisconnected,itisaforest.Manyalgorithmsthatwork

Pgina295

1174 AppendixBSets,Etc.

(un) (segundo) (do)

FigureB.4(a)Afreetree.(b)Aforest.(c)Agraphthatcontainsacycleandisthereforeneither
atreenoraforest.

fortreesalsoworkforforests.FigureB.4(a)showsafreetree,andFigureB.4(b)
showsaforest.TheforestinFigureB.4(b)isnotatreebecauseitisnotconnected.
ThegraphinFigureB.4(c)isconnectedbutneitheratreenoraforest,becauseit
containsacycle.
Thefollowingtheoremcapturesmanyimportantfactsaboutfreetrees.

TheoremB.2(Propertiesoffreetrees)
LetGD.VE/beanundirectedgraph.Thefollowingstatementsareequivalent.

1.Gisafreetree.
2.AnytwoverticesinGareconnectedbyauniquesimplepath.
3.Gisconnected,butifanyedgeisremovedfromE,theresultinggraphisdis
conectado.
https://translate.googleusercontent.com/translate_f 205/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,

4.Gisconnected,andjEjDjVj1.
5.Gisacyclic,andjEjDjVj1.
6.Gisacyclic,butifanyedgeisaddedtoE,theresultinggraphcontainsacycle.

Proof(1))(2):Sinceatreeisconnected,anytwoverticesinGareconnected
byatleastonesimplepath.Suppose,forthesakeofcontradiction,thatverticesu
andareconnectedbytwodistinctsimplepathsp1andp2,asshowninFigureB.5.
Letwbethevertexatwhichthepathsfirstdivergethatis,wisthefirstvertex
onbothp1andp2whosesuccessoronp1isxandwhosesuccessoronp2isy,
wherexy.Letbethefirstvertexatwhichthepathsreconvergethatis,is
thefirstvertexfollowingwonp1thatisalsoonp2.Letp0bethesubpathofp1
fromwthroughxto,andletp00bethesubpathofp2fromwthroughyto.
Pathsp0andp00sharenoverticesexcepttheirendpoints.Thus,thepathobtainedby
concatenatingp0andthereverseofp00isacycle,whichcontradictsourassumption

Pgina296

B.5Trees 1175

x
W V
Y Z
Usted
p

FigureB.5AstepintheproofofTheoremB.2:if(1)Gisafreetree,then(2)anytwovertices
inGareconnectedbyauniquesimplepath.Assumeforthesakeofcontradictionthatverticesu
andareconnectedbytwodistinctsimplepathsp1andp2.Thesepathsfirstdivergeatvertexw,
andtheyfirstreconvergeatvertex.Thepathp0concatenatedwiththereverseofthepathp00forms
acycle,whichyieldsthecontradiction.

thatGisatree.Thus,ifGisatree,therecanbeatmostonesimplepathbetween
twovertices.
(2))(3):IfanytwoverticesinGareconnectedbyauniquesimplepath,
thenGisconnected.Let.u/beanyedgeinE.Thisedgeisapathfromuto,
andsoitmustbetheuniquepathfromuto.Ifweremove.u/fromG,there
isnopathfromuto,andhenceitsremovaldisconnectsG.
(3))(4):Byassumption,thegraphGisconnected,andbyExerciseB.43,we
havejEjjVj1.WeshallprovejEjjVj1byinduction.Aconnected
graphwithnD1ornD2verticeshasn1edges.SupposethatGhasn 3
verticesandthatallgraphssatisfying(3)withfewerthannverticesalsosatisfy
jEjjVj1.RemovinganarbitraryedgefromGseparatesthegraphintok 2
connectedcomponents(actuallykD2).Eachcomponentsatisfies(3),orelseG
wouldnotsatisfy(3).IfwevieweachconnectedcomponentVi,withedgesetEi,
asitsownfreetree,thenbecauseeachcomponenthasfewerthanjVjvertices,by
theinductivehypothesiswehavejEijjVij1.Thus,thenumberofedgesinall
componentscombinedisatmostjVjkjVj2.Addingintheremovededge
yieldsjEjjVj1.
(4))(5):SupposethatGisconnectedandthatjEjDjVj1.Wemustshow
thatGisacyclic.SupposethatGhasacyclecontainingkvertices12:::k,
andwithoutlossofgeneralityassumethatthiscycleissimple.LetGkD.VkEk/
bethesubgraphofGconsistingofthecycle.NotethatjVkjDjEkjDk.
Ifk<jVj,theremustbeavertexkC12V Vkthatisadjacenttosomever
texi2Vk,sinceGisconnected.DefineGkC1D.VkC1EkC1/tobethesub
graphofGwithVkC1DVk[fkC1gandEkC1DEk[f.ikC1/g.Tengaencuentaque
jVkC1jDjEkC1jDkC1.IfkC1<jVj,wecancontinue,definingGkC2in
thesamemanner,andsoforth,untilweobtainGnD.VnEn/,wherenDjVj,

Page297
https://translate.googleusercontent.com/translate_f 206/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,

1176 AppendixBSets,Etc.

VnDV,andjEnjDjVnjDjVj.SinceGnisasubgraphofG,wehaveEnE,
andhencejEjjVj,whichcontradictstheassumptionthatjEjDjVj1.Thus,
Gisacyclic.
(5))(6):SupposethatGisacyclicandthatjEjDjVj1.Letkbethe
numberofconnectedcomponentsofG.Eachconnectedcomponentisafreetree
bydefinition,andsince(1)implies(5),thesumofalledgesinallconnectedcom
ponentsofGisjVjk.Consequently,wemusthavekD1,andGisinfacta
rbol.Since(1)implies(2),anytwoverticesinGareconnectedbyauniquesimple
camino.Thus,addinganyedgetoGcreatesacycle.
(6))(1):SupposethatGisacyclicbutthataddinganyedgetoEcreatesa
ciclo.WemustshowthatGisconnected.LetuandbearbitraryverticesinG.
Ifuandarenotalreadyadjacent,addingtheedge.u/createsacycleinwhich
alledgesbut.u/belongtoG.Thus,thecycleminusedge.u/mustcontaina
pathfromuto,andsinceuandwerechosenarbitrarily,Gisconnected.

B.5.2Rootedandorderedtrees
Arootedtreeisafreetreeinwhichoneoftheverticesisdistinguishedfromthe
otros.Wecallthedistinguishedvertextherootofthetree.Weoftenrefertoa
vertexofarootedtreeasanode5ofthetree.FigureB.6(a)showsarootedtreeon
asetof12nodeswithroot7.
ConsideranodexinarootedtreeTwithrootr.Wecallanynodeyonthe
uniquesimplepathfromrtoxanancestorofx.Ifyisanancestorofx,thenxis
adescendantofy.(Everynodeisbothanancestorandadescendantofitself.)Ify
isanancestorofxandxy,thenyisaproperancestorofxandxisaproper
descendantofy.Thesubtreerootedatxisthetreeinducedbydescendantsofx,
rootedatx.Forexample,thesubtreerootedatnode8inFigureB.6(a)contains
nodes8,6,5,and9.
IfthelastedgeonthesimplepathfromtherootrofatreeTtoanodexis.yx/,
thenyistheparentofx,andxisachildofy.TherootistheonlynodeinTwith
noparent.Iftwonodeshavethesameparent,theyaresiblings.Anodewithno
childrenisaleaforexternalnode.Anonleafnodeisaninternalnode.

5Thetermnodeisoftenusedinthegraphtheoryliteratureasasynonymforvertex.Wereserve
thetermnodetomeanavertexofarootedtree.

Pgina298

B.5Trees 1177

7 depth0 7

3 10 4 depth1 3 10 4

height=4 8 12 11 2 depth2 12 8 11 2

6 5 1 depth3 1 6 5

9 Profundidad4 9
(un) (segundo)

FigureB.6Rootedandorderedtrees.(a)Arootedtreewithheight4.Thetreeisdrawnina
standardway:theroot(node7)isatthetop,itschildren(nodeswithdepth1)arebeneathit,their
children(nodeswithdepth2)arebeneaththem,andsoforth.Ifthetreeisordered,therelativeleft
torightorderofthechildrenofanodemattersotherwiseitdoesn't.(b)Anotherrootedtree.Comoun
rootedtree,itisidenticaltothetreein(a),butasanorderedtreeitisdifferent,sincethechildrenof
node3appearinadifferentorder.
https://translate.googleusercontent.com/translate_f 207/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,

ThenumberofchildrenofanodexinarootedtreeTequalsthedegreeofx.6
ThelengthofthesimplepathfromtherootrtoanodexisthedepthofxinT.
Alevelofatreeconsistsofallnodesatthesamedepth.Theheightofanodeina
treeisthenumberofedgesonthelongestsimpledownwardpathfromthenodeto
aleaf,andtheheightofatreeistheheightofitsroot.Theheightofatreeisalso
equaltothelargestdepthofanynodeinthetree.
Anorderedtreeisarootedtreeinwhichthechildrenofeachnodeareordered.
Thatis,ifanodehaskchildren,thenthereisafirstchild,asecondchild,...,
andakthchild.ThetwotreesinFigureB.6aredifferentwhenconsideredtobe
orderedtrees,butthesamewhenconsideredtobejustrootedtrees.

B.5.3Binaryandpositionaltrees
Wedefinebinarytreesrecursively.AbinarytreeTisastructuredefinedonafinite
setofnodesthateither

containsnonodes,or

6NoticethatthedegreeofanodedependsonwhetherweconsiderTtobearootedtreeorafreetree.
Thedegreeofavertexinafreetreeis,asinanyundirectedgraph,thenumberofadjacentvertices.
Inarootedtree,however,thedegreeisthenumberofchildrentheparentofanodedoesnotcount
towarditsdegree.

Pgina299

1178 AppendixBSets,Etc.

3 3 3

2 7 2 7 2 7

4 1 5 4 1 5 4 1 5

6 6 6

(un) (segundo) (do)

FigureB.7Binarytrees.(a)Abinarytreedrawninastandardway.Theleftchildofanodeis
drawnbeneaththenodeandtotheleft.Therightchildisdrawnbeneathandtotheright.(b)Abinary
treedifferentfromtheonein(a).In(a),theleftchildofnode7is5andtherightchildisabsent.
In(b),theleftchildofnode7isabsentandtherightchildis5.Asorderedtrees,thesetreesare
thesame,butasbinarytrees,theyaredistinct.(c)Thebinarytreein(a)representedbytheinternal
nodesofafullbinarytree:anorderedtreeinwhicheachinternalnodehasdegree2.Theleavesin
thetreeareshownassquares.

iscomposedofthreedisjointsetsofnodes:arootnode,abinarytreecalledits
leftsubtree,andabinarytreecalleditsrightsubtree.
Thebinarytreethatcontainsnonodesiscalledtheemptytreeornulltree,some
timesdenotedNIL.Iftheleftsubtreeisnonempty,itsrootiscalledtheleftchildof
therootoftheentiretree.Likewise,therootofanonnullrightsubtreeistheright
childoftherootoftheentiretree.IfasubtreeisthenulltreeNIL,wesaythatthe
childisabsentormissing.FigureB.7(a)showsabinarytree.
Abinarytreeisnotsimplyanorderedtreeinwhicheachnodehasdegreeat
most2.Forexample,inabinarytree,ifanodehasjustonechild,theposition
ofthechildwhetheritistheleftchildortherightchildmatters.Inanor
deredtree,thereisnodistinguishingasolechildasbeingeitherleftorright.Higo
ureB.7(b)showsabinarytreethatdiffersfromthetreeinFigureB.7(a)becauseof
thepositionofonenode.Consideredasorderedtrees,however,thetwotreesare
idntico.
Wecanrepresentthepositioninginformationinabinarytreebytheinternal
nodesofanorderedtree,asshowninFigureB.7(c).Theideaistoreplaceeach
missingchildinthebinarytreewithanodehavingnochildren.Theseleafnodes
aredrawnassquaresinthefigure.Thetreethatresultsisafullbinarytree:each
nodeiseitheraleaforhasdegreeexactly2.Therearenodegree1nodes.Conse
quently,theorderofthechildrenofanodepreservesthepositioninformation.

https://translate.googleusercontent.com/translate_f 208/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
Wecanextendthepositioninginformationthatdistinguishesbinarytreesfrom
orderedtreestotreeswithmorethan2childrenpernode.Inapositionaltree,the

Pgina300

B.5Trees 1179

depth0

depth1
height=3
depth2

depth3

FigureB.8Acompletebinarytreeofheight3with8leavesand7internalnodes.

childrenofanodearelabeledwithdistinctpositiveintegers.Theithchildofa
nodeisabsentifnochildislabeledwithintegeri.Akarytreeisapositionaltree
inwhichforeverynode,allchildrenwithlabelsgreaterthankaremissing.As,
abinarytreeisakarytreewithkD2.
Acompletekarytreeisakarytreeinwhichallleaveshavethesamedepth
andallinternalnodeshavedegreek.FigureB.8showsacompletebinarytreeof
height3.Howmanyleavesdoesacompletekarytreeofheighthhave?Theroot
haskchildrenatdepth1,eachofwhichhaskchildrenatdepth2,etc.Thus,the
numberofleavesatdepthhiskh.Consequently,theheightofacompletekary
treewithnleavesislogkn.Thenumberofinternalnodesofacompletekarytree
ofheighthis
xH1
1CkCk2CCkh1 re ki
ID0
kh 1
re
k1
byequation(A.5).Thus,acompletebinarytreehas2h 1internalnodes.

Exercises

B.51
Drawallthefreetreescomposedofthethreeverticesx,y,and.Drawallthe
rootedtreeswithnodesx,y,andwithxastheroot.Drawalltheorderedtrees
withnodesx,y,andwithxastheroot.Drawallthebinarytreeswithnodesx,
y,andwithxastheroot.

Page301

1180 AppendixBSets,Etc.

B.52
LetGD.VE/beadirectedacyclicgraphinwhichthereisavertex02V
suchthatthereexistsauniquepathfrom0toeveryvertex2V.Provethatthe
undirectedversionofGformsatree.

B.53
Showbyinductionthatthenumberofdegree2nodesinanynonemptybinarytree
is1fewerthanthenumberofleaves.Concludethatthenumberofinternalnodes
inafullbinarytreeis1fewerthanthenumberofleaves.

B.54
Useinductiontoshowthatanonemptybinarytreewithnnodeshasheightat
https://translate.googleusercontent.com/translate_f 209/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,

leastblgnc.

B.55?
Theinternalpathlengthofafullbinarytreeisthesum,takenoverallinternal
nodesofthetree,ofthedepthofeachnode.Likewise,theexternalpathlengthis
thesum,takenoverallleavesofthetree,ofthedepthofeachleaf.Considerafull
binarytreewithninternalnodes,internalpathlengthi,andexternalpathlengthe.
ProvethateDiC2n.

B.56?
Letusassociateaweightwx/D2dwitheachleafxofdepthdinabinary
PAG
treeT,andletLbethesetofleavesofT.Provethat
x2Lwx/1.(Thisis
knownastheKraftinequality.)

B.57?
ShowthatifL 2,theneverybinarytreewithLleavescontainsasubtreehaving
betweenL=3and2L=3leaves,inclusive.

Problemas

B1Graphcoloring
GivenanundirectedgraphGD.VE/,akcoloringofGisafunctioncWV!
f01:::k1gsuchthatcu/c./foreveryedge.u/2E.Inotherwords,
thenumbers01:::k1representthekcolors,andadjacentverticesmusthave
differentcolors.

a.Showthatanytreeis2colorable.

Pgina302

ProblemsforAppendixB 1181

segundo.Showthatthefollowingareequivalent:
1.Gisbipartite.
2.Gis2colorable.
3.Ghasnocyclesofoddlength.

do.LetdbethemaximumdegreeofanyvertexinagraphG.Provethatwecan
colorGwithdC1colors.
pag
re.ShowthatifGhasO.jVj/edges,thenwecancolorGwithO. jVj/colors.

B2Friendlygraphs
Rewordeachofthefollowingstatementsasatheoremaboutundirectedgraphs,
andthenproveit.Assumethatfriendshipissymmetricbutnotreflexive.
a.Anygroupofatleasttwopeoplecontainsatleasttwopeoplewiththesame
numberoffriendsinthegroup.

segundo.Everygroupofsixpeoplecontainseitheratleastthreemutualfriendsoratleast
threemutualstrangers.

do.Anygroupofpeoplecanbepartitionedintotwosubgroupssuchthatatleast
halfthefriendsofeachpersonbelongtothesubgroupofwhichthatpersonis
notamember.

re.Ifeveryoneinagroupisthefriendofatleasthalfthepeopleinthegroup,then
thegroupcanbeseatedaroundatableinsuchawaythateveryoneisseated
betweentwofriends.

B3Bisectingtrees
Manydivideandconqueralgorithmsthatoperateongraphsrequirethatthegraph
bebisectedintotwonearlyequalsizedsubgraphs,whichareinducedbyapartition
ofthevertices.Thisprobleminvestigatesbisectionsoftreesformedbyremovinga
smallnumberofedges.Werequirethatwhenevertwoverticesendupinthesame
https://translate.googleusercontent.com/translate_f 210/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
subtreeafterremovingedges,thentheymustbeinthesamepartition.
a.Showthatwecanpartitiontheverticesofanynvertexbinarytreeintotwo
setsAandB,suchthatjAj3n=4andjBj3n=4,byremovingasingle
borde.

segundo.Showthattheconstant3=4inpart(a)isoptimalintheworstcasebygiving
anexampleofasimplebinarytreewhosemostevenlybalancedpartitionupon
removalofasingleedgehasjAjD3n=4.

Pgina303

1182 AppendixBSets,Etc.

do.ShowthatbyremovingatmostO.lgn/edges,wecanpartitionthevertices
ofanynvertexbinarytreeintotwosetsAandBsuchthatjAjDbn=2c
andjBjDdn=2e.

Appendixnotes

G.Boolepioneeredthedevelopmentofsymboliclogic,andheintroducedmanyof
thebasicsetnotationsinabookpublishedin1854.Modernsettheorywascreated
byG.Cantorduringtheperiod18741895.Cantorfocusedprimarilyonsetsof
infinitecardinality.ThetermfunctionisattributedtoGWLeibniz,whousedit
torefertoseveralkindsofmathematicalformulas.Hislimiteddefinitionhasbeen
generalizedmanytimes.Graphtheoryoriginatedin1736,whenL.Eulerproved
thatitwasimpossibletocrosseachofthesevenbridgesinthecityofKnigsberg
exactlyonceandreturntothestartingpoint.
ThebookbyHarary[160]providesausefulcompendiumofmanydefinitions
andresultsfromgraphtheory.

Pgina304

do CountingandProbability

https://translate.googleusercontent.com/translate_f 211/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,

Thisappendixreviewselementarycombinatoricsandprobabilitytheory.Situ
haveagoodbackgroundintheseareas,youmaywanttoskimthebeginningofthis
appendixlightlyandconcentrateonthelatersections.Mostofthisbook'schapters
donotrequireprobability,butforsomechaptersitisessential.
SectionC.1reviewselementaryresultsincountingtheory,includingstandard
formulasforcountingpermutationsandcombinations.Theaxiomsofprobability
andbasicfactsconcerningprobabilitydistributionsformSectionC.2.Aleatorio
variablesareintroducedinSectionC.3,alongwiththepropertiesofexpectation
andvariance.SectionC.4investigatesthegeometricandbinomialdistributions
thatarisefromstudyingBernoullitrials.Thestudyofthebinomialdistribution
continuesinSectionC.5,anadvanceddiscussionofthetailsofthedistribution.

C.1Counting

CountingtheorytriestoanswerthequestionHowmany?withoutactuallyenu
meratingallthechoices.Forexample,wemightask,Howmanydifferentnbit
numbersarethere?orHowmanyorderingsofndistinctelementsarethere?In
thissection,wereviewtheelementsofcountingtheory.Sincesomeofthematerial
assumesabasicunderstandingofsets,youmightwishtostartbyreviewingthe
materialinSectionB.1.

Rulesofsumandproduct
Wecansometimesexpressasetofitemsthatwewishtocountasaunionofdisjoint
setsorasaCartesianproductofsets.
Theruleofsumsaysthatthenumberofwaystochooseoneelementfromone
oftwodisjointsetsisthesumofthecardinalitiesofthesets.Thatis,ifAandB
aretwofinitesetswithnomembersincommon,thenjA[BjDjAjCjBj,which

Pgina305

1184 AppendixCCountingandProbability

followsfromequation(B.3).Forexample,eachpositiononacar'slicenseplate
isaletteroradigit.Thenumberofpossibilitiesforeachpositionistherefore
26C10D36,sincethereare26choicesifitisaletterand10choicesifitisa
dgito.
Theruleofproductsaysthatthenumberofwaystochooseanorderedpairisthe
numberofwaystochoosethefirstelementtimesthenumberofwaystochoosethe
secondelement.Thatis,ifAandBaretwofinitesets,thenjABjDjAjjBj,
whichissimplyequation(B.4).Forexample,ifanicecreamparloroffers28
flavorsoficecreamand4toppings,thenumberofpossiblesundaeswithonescoop
oficecreamandonetoppingis284D112.

Strings
AstringoverafinitesetSisasequenceofelementsofS.Forexample,thereare8
binarystringsoflength3:
000001010011100101110111:
Wesometimescallastringoflengthkakstring.Asubstrings0ofastrings
isanorderedsequenceofconsecutiveelementsofs.Aksubstringofastring
isasubstringoflengthk.Forexample,010isa3substringof01101001(the
3substringthatbeginsinposition4),but111isnotasubstringof01101001.
WecanviewakstringoverasetSasanelementoftheCartesianproductSk
ofktuplesthus,therearejSj Kstringsoflengthk.Forexample,thenumberof
binarykstringsis2k.Intuitively,toconstructakstringoverannset,wehaven
waystopickthefirstelementforeachofthesechoices,wehavenwaystopickthe
secondelementandsoforthktimes.Thisconstructionleadstothekfoldproduct
https://translate.googleusercontent.com/translate_f 212/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
nnnDnkasthenumberofkstrings.

Permutations

ApermutationofafinitesetSisanorderedsequenceofalltheelementsofS,
witheachelementappearingexactlyonce.Forexample,ifSDfasegundocg,thenS
has6permutations:
abcacbbacbcacabcba:
Therearenpermutationsofasetofnelements,sincewecanchoosethefirst
elementofthesequenceinnways,thesecondinn1ways,thethirdinn2
ways,andsoon.
AkpermutationofSisanorderedsequenceofkelementsofS,withnoele
mentappearingmorethanonceinthesequence.(Thus,anordinarypermutationis
annpermutationofannset.)Thetwelve2permutationsofthesetfabcdgare

Pgina306

C.1Counting 1185

abacadbabcbdcacbcddadbdc:
Thenumberofkpermutationsofannsetis
n
nn1/.n2/.nkC1/D (C.1)
.nk/
sincewehavenwaystochoosethefirstelement,n1waystochoosethesecond
element,andsoon,untilwehaveselectedkelements,thelastbeingaselection
fromtheremainingnkC1elements.

Combinaciones

AkcombinationofannsetSissimplyaksubsetofS.Forexample,the4set
fabcdghassix2combinations:
abacadbcbdcd:
(Hereweusetheshorthandofdenotingthe2subsetfabgbyab,andsoon.)
Wecanconstructakcombinationofannsetbychoosingkdistinct(different)
elementsfromthenset.Theorderinwhichweselecttheelementsdoesnotmatter.
Wecanexpressthenumberofkcombinationsofannsetintermsofthenumber
ofkpermutationsofannset.Everykcombinationhasexactlykpermutations
ofitselements,eachofwhichisadistinctkpermutationofthenset.Porlotanto,la
numberofkcombinationsofannsetisthenumberofkpermutationsdivided
bykfromequation(C.1),thisquantityis
n
: (C.2)
k.nk/
ForkD0,thisformulatellsusthatthenumberofwaystochoose0elementsfrom
annsetis1(not0),since0D1.

Binomialcoefficients

Thenotation norte (readnchoosek)denotesthenumberofkcombinationsof


K
annset.Fromequation(C.2),wehave
!
norte n
re :
K k.nk/

Thisformulaissymmetricinkandnk:
! !
norte norte
re : (C.3)
K nk

Page307

https://translate.googleusercontent.com/translate_f 213/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
1186 AppendixCCountingandProbability

Thesenumbersarealsoknownasbinomialcoefficients,duetotheirappearancein
thebinomialexpansion:
!
xnortenorte
.xCy/nD xkynk: (C.4)
K
kD0
AspecialcaseofthebinomialexpansionoccurswhenxDyD1:
!
x nortenorte
2nD :
K
kD0
Thisformulacorrespondstocountingthe2nbinarynstringsbythenumberof1s
theycontain: nortebinarynstringscontainexactlyk1s,sincewehave nortewaysto
K K
choosekoutofthenpositionsinwhichtoplacethe1s.
Manyidentitiesinvolvebinomialcoefficients.Theexercisesattheendofthis
sectiongiveyoutheopportunitytoproveafew.

Binomialbounds
Wesometimesneedtoboundthesizeofabinomialcoefficient.For1kn,
wehavethelowerbound
!
norte nn1/.nkC1/
re
K kk1/1
UN UN UN
norte n1 nkC1
re
K k1 1
k
norte
:
K
Takingadvantageoftheinequalityk .k=e/kderivedfromStirling'sapproxi
mation(3.18),weobtaintheupperbounds
!
norte nn1/.nkC1/
re
K kk1/1
nk
UN
k k
En
UN : (C.5)
K
Forallintegersksuchthat0kn,wecanuseinduction(seeExerciseC.112)
toprovethebound

Pgina308

C.1Counting 1187
!
norte nn
UN (C.6)
K kk.nk/nk

whereforconvenienceweassumethat00D1.ForkDn,where01,we
canrewritethisboundas
!
norte nn
UN
norte .n/n..1 /n/.1/n
UN 1 !norte
1 1
re
1
D2nH./

dnde
H./Dlg.1 /lg.1 / (C.7)
isthe(binary)entropyfunctionandwhere,forconvenience,weassumethat
0lg0D0,sothatH.0/DH.1/D0.

https://translate.googleusercontent.com/translate_f 214/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
Exercises
C.11
Howmanyksubstringsdoesannstringhave?(Consideridenticalksubstringsat
differentpositionstobedifferent.)Howmanysubstringsdoesannstringhavein
total?

C.12
Anninput,moutputbooleanfunctionisafunctionfromfTRUEFALSEg norte
a
fTRUEFALSEg metro
.Howmanyninput,1outputbooleanfunctionsarethere?Cmo
manyninput,moutputbooleanfunctionsarethere?

C.13
Inhowmanywayscannprofessorssitaroundacircularconferencetable?Estafa
sidertwoseatingstobethesameifonecanberotatedtoformtheother.

C.14
Inhowmanywayscanwechoosethreedistinctnumbersfromthesetf12:::99g
sothattheirsumiseven?

Pgina309

1188 AppendixCCountingandProbability

C.15
Probarlaidentidad
! !
norte norte n1
re (C.8)
K K k1
for0<kn.

C.16
Probarlaidentidad
! !
norte norte n1
re
K nk K
for0k<n.

C.17
Tochoosekobjectsfromn,youcanmakeoneoftheobjectsdistinguishedand
considerwhetherthedistinguishedobjectischosen.Usethisapproachtoprove
ese ! ! !
norte n1 n1
re do :
K K k1

C.18
UsingtheresultofExerciseC.17,makeatablefornD01:::6and0kn
ofthebinomialcoefficients norte
con 0 atthetop, 1 y 1 onthenextline,and
K 0 0 1
soforth.SuchatableofbinomialcoefficientsiscalledPascal'striangle.

C.19
Provethat !
xnorte nC1
iD :
2
ID1

C.110
Showthatforanyintegersn 0and0kn,theexpressionn achievesits
K
maximumvaluewhenkDbn=2corkDdn=2e.

C.111?
Arguethatforanyintegersn 0,j 0,k 0,andjCkn,
! ! !
norte norteNuevaJersey
UN : (C.9)
jCk J K

https://translate.googleusercontent.com/translate_f 215/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,

Pgina310

C.2Probability 1189

Providebothanalgebraicproofandanargumentbasedonamethodforchoosing
jCkitemsoutofn.Giveanexampleinwhichequalitydoesnothold.

C.112?
Useinductiononallintegersksuchthat0kn=2toproveinequality(C.6),
anduseequation(C.3)toextendittoallintegersksuchthat0kn.

C.113?
UseStirling'sapproximationtoprovethat
!
2n 22n
re pag .1CO.1=n//: (C.10)
norte norte

C.114?
BydifferentiatingtheentropyfunctionH./,showthatitachievesitsmaximum
valueatD1=2.WhatisH.1=2/?

C.115?
Showthatforanyintegern 0,
!
xnortenorte
kDn2n1: (C.11)
K
kD0

C.2Probability

Probabilityisanessentialtoolforthedesignandanalysisofprobabilisticandran
domizedalgorithms.Thissectionreviewsbasicprobabilitytheory.
WedefineprobabilityintermsofasamplespaceS,whichisasetwhoseele
mentsarecalledelementaryevents.Wecanthinkofeachelementaryeventasa
possibleoutcomeofanexperiment.Fortheexperimentofflippingtwodistinguish
ablecoins,witheachindividualflipresultinginahead(H)oratail(T),wecanview
thesamplespaceasconsistingofthesetofallpossible2stringsoverfHTg:
SDfHHHTTHTTg:

Pgina311

1190 AppendixCCountingandProbability

Aneventisasubset1ofthesamplespaceS.Forexample,intheexperimentof
flippingtwocoins,theeventofobtainingoneheadandonetailisfHTTHg.El
eventSiscalledthecertainevent,andtheeventiscalledthenullevent.Wesay
thattwoeventsAandBaremutuallyexclusiveifA\BD.Wesometimestreat
anelementaryevents2Sastheeventfsg.Bydefinition,allelementaryevents
aremutuallyexclusive.

Axiomsofprobability
AprobabilitydistributionPrfgonasamplespaceSisamappingfromeventsofS
torealnumberssatisfyingthefollowingprobabilityaxioms:
1.PrfAg0foranyeventA.

https://translate.googleusercontent.com/translate_f 216/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
2.PrfSgD1.
3.PrfA[BgDPrfAgCPrfBgforanytwomutuallyexclusiveeventsA
andB.Moregenerally,forany(finiteorcountablyinfinite)sequenceofevents
A1A2:::thatarepairwisemutuallyexclusive,
( )
[ x
Pr Ai re PrfAig:
yo yo
WecallPrfAgtheprobabilityoftheeventA.Wenoteherethataxiom2isa
normalizationrequirement:thereisreallynothingfundamentalaboutchoosing1
astheprobabilityofthecertainevent,exceptthatitisnaturalandconvenient.
Severalresultsfollowimmediatelyfromtheseaxiomsandbasicsettheory(see
SectionB.1).ThenulleventhasprobabilityPrfgD0.IfAB,then
PrfAgPrfBg.UsingAtodenotetheeventS
A(thecomplementofA),
wehavePr UN D1PrfAg.ForanytwoeventsAandB,

PrfA[BgDPrfAgCPrfBgPrfA\Bg (C.12)
PrfAgCPrfBg: (C.13)

1Forageneralprobabilitydistribution,theremaybesomesubsetsofthesamplespaceSthatarenot
consideredtobeevents.Thissituationusuallyariseswhenthesamplespaceisuncountablyinfinite.
Themainrequirementforwhatsubsetsareeventsisthatthesetofeventsofasamplespacebeclosed
undertheoperationsoftakingthecomplementofanevent,formingtheunionofafiniteorcountable
numberofevents,andtakingtheintersectionofafiniteorcountablenumberofevents.Lamayorade
theprobabilitydistributionsweshallseeareoverfiniteorcountablesamplespaces,andweshall
generallyconsiderallsubsetsofasamplespacetobeevents.Anotableexceptionisthecontinuous
uniformprobabilitydistribution,whichweshallseeshortly.

Pgina312

C.2Probability 1191

Inourcoinflippingexample,supposethateachofthefourelementaryevents
hasprobability1=4.Thentheprobabilityofgettingatleastoneheadis
PrfHHHTTHgDPrfHHgCPrfHTgCPrfTHg
D3=4:
Alternatively,sincetheprobabilityofgettingstrictlylessthanoneheadis
PrfTTgD1=4,theprobabilityofgettingatleastoneheadis11=4D3=4.

Discreteprobabilitydistributions

Aprobabilitydistributionisdiscreteifitisdefinedoverafiniteorcountablyinfinite
samplespace.LetSbethesamplespace.ThenforanyeventA,
x
PrfAgD Prfsg
s2A
sinceelementaryevents,specificallythoseinA,aremutuallyexclusive.IfSis
finiteandeveryelementaryevents2Shasprobability
PrfsgD1=jSj
thenwehavetheuniformprobabilitydistributiononS.Insuchacasetheexperi
mentisoftendescribedaspickinganelementofSatrandom.
Asanexample,considertheprocessofflippingafaircoin,oneforwhichthe
probabilityofobtainingaheadisthesameastheprobabilityofobtainingatail,that
is,1=2.Ifweflipthecoinntimes,wehavetheuniformprobabilitydistribution
definedonthesamplespaceSDfHTg norte
,asetofsize2n.Wecanrepresenteach
elementaryeventinSasastringoflengthnoverfHTg,eachstringoccurringwith
probability1=2n.Theevent
ADfexactlykheadsandexactlynktailsoccurg
isasubsetofSofsizejAjD norte,since norte
K kstringsoflengthnoverfHTgcontain
exactlykHs.TheprobabilityofeventAisthusPrfAgD norte =2n.
K

Continuousuniformprobabilitydistribution

https://translate.googleusercontent.com/translate_f 217/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
Thecontinuousuniformprobabilitydistributionisanexampleofaprobability
distributioninwhichnotallsubsetsofthesamplespaceareconsideredtobe
eventos.Thecontinuousuniformprobabilitydistributionisdefinedoveraclosed
intervalabofthereals,wherea<b.Ourintuitionisthateachpointinthein
tervalabshouldbeequallylikely.Thereareanuncountablenumberofpoints,
however,soifwegiveallpointsthesamefinite,positiveprobability,wecannotsi
multaneouslysatisfyaxioms2and3.Forthisreason,wewouldliketoassociatea

Pgina313

1192 AppendixCCountingandProbability

probabilityonlywithsomeofthesubsetsofS,insuchawaythattheaxiomsare
satisfiedfortheseevents.
Foranyclosedintervalcd,whereacdb,thecontinuousuniform
probabilitydistributiondefinestheprobabilityoftheeventcdtobe
re do
PrfcdgD :
licenciadoenLetras
NotethatforanypointxDxx,theprobabilityofxis0.Ifweremove
theendpointsofanintervalcd,weobtaintheopeninterval.cd/.Yaque
cdDcc[.cd/[dd,axiom3givesusPrfcdgDPrf.cd/g.Gen
erally,thesetofeventsforthecontinuousuniformprobabilitydistributioncontains
anysubsetofthesamplespaceabthatcanbeobtainedbyafiniteorcountable
unionofopenandclosedintervals,aswellascertainmorecomplicatedsets.

Conditionalprobabilityandindependence
Sometimeswehavesomepriorpartialknowledgeabouttheoutcomeofanexper
iment.Forexample,supposethatafriendhasflippedtwofaircoinsandhastold
youthatatleastoneofthecoinsshowedahead.Whatistheprobabilitythatboth
coinsareheads?Theinformationgiveneliminatesthepossibilityoftwotails.los
threeremainingelementaryeventsareequallylikely,soweinferthateachoccurs
withprobability1=3.Sinceonlyoneoftheseelementaryeventsshowstwoheads,
theanswertoourquestionis1=3.
Conditionalprobabilityformalizesthenotionofhavingpriorpartialknowledge
oftheoutcomeofanexperiment.TheconditionalprobabilityofaneventAgiven
thatanothereventBoccursisdefinedtobe
PrfA\Bg
PrfAjBgD (C.14)
PrfBg
wheneverPrfBg0.(WereadPrfAjBgastheprobabilityofAgivenB.)
Intuitively,sincewearegiventhateventBoccurs,theeventthatAalsooccurs
isA\B.Thatis,A\BisthesetofoutcomesinwhichbothAandBoccur.
BecausetheoutcomeisoneoftheelementaryeventsinB,wenormalizetheprob
abilitiesofalltheelementaryeventsinBbydividingthembyPrfBg,sothatthey
sumto1.TheconditionalprobabilityofAgivenBis,therefore,theratioofthe
probabilityofeventA\BtotheprobabilityofeventB.Intheexampleabove,A
istheeventthatbothcoinsareheads,andBistheeventthatatleastonecoinisa
cabeza.Thus,PrfAjBgD.1=4/=.3=4/D1=3.
Twoeventsareindependentif
PrfA\BgDPrfAgPrfBg (C.15)
whichisequivalent,ifPrfBg0,tothecondition

Pgina314

C.2Probability 1193

PrfAjBgDPrfAg:
Forexample,supposethatwefliptwofaircoinsandthattheoutcomesareinde
pendent.Thentheprobabilityoftwoheadsis.1=2/.1=2/D1=4.Nowsuppose
thatoneeventisthatthefirstcoincomesupheadsandtheothereventisthatthe
coinscomeupdifferently.Eachoftheseeventsoccurswithprobability1=2,and

https://translate.googleusercontent.com/translate_f 218/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
theprobabilitythatbotheventsoccuris1=4thus,accordingtothedefinitionof
independence,theeventsareindependenteventhoughyoumightthinkthatboth
eventsdependonthefirstcoin.Finally,supposethatthecoinsareweldedto
gethersothattheybothfallheadsorbothfalltailsandthatthetwopossibilitiesare
equallylikely.Thentheprobabilitythateachcoincomesupheadsis1=2,butthe
probabilitythattheybothcomeupheadsis1=2.1=2/.1=2/.Consequently,the
eventthatonecomesupheadsandtheeventthattheothercomesupheadsarenot
independent.
AcollectionA1A2:::Anofeventsissaidtobepairwiseindependentif
PrfAi\AjgDPrfAigPrfAjg

forall1i<jn.Wesaythattheeventsofthecollectionare(mutually)
independentifeveryksubsetAi Ai :::Ai ofthecollection,where2kn
1 2 k

and1i1<i2< <ikn,satisfies

PrfAi \Ai \\Ai gDPrfAi gPrfAi gPrfAi g:


1 2 k 1 2 k

Forexample,supposewefliptwofaircoins.LetA1betheeventthatthefirstcoin
isheads,letA2betheeventthatthesecondcoinisheads,andletA3betheevent
thatthetwocoinsaredifferent.Tenemos

PrfA1gD1=2
PrfA2gD1=2
PrfA3gD1=2
PrfA1\A2gD1=4
PrfA1\A3gD1=4
PrfA2\A3gD1=4
PrfA1\A2\A3gD0:
Sincefor1i<j3,wehavePrfAi\AjgDPrfAigPrfAjgD1=4,the
eventsA1,A2,andA3arepairwiseindependent.Theeventsarenotmutuallyinde
pendent,however,becausePrfA1\A2\A3gD0andPrfA1gPrfA2gPrfA3gD
1=80.

Page315

1194 AppendixCCountingandProbability

Bayes'stheorem
Fromthedefinitionofconditionalprobability(C.14)andthecommutativelaw
A\BDB\A,itfollowsthatfortwoeventsAandB,eachwithnonzero
probability,
PrfA\BgDPrfBgPrfAjBg (C.16)
DPrfAgPrfBjAg:
SolvingforPrfAjBg,weobtain
PrfAgPrfBjAg
PrfAjBgD (C.17)
PrfBg
whichisknownasBayes'stheorem.ThedenominatorPrfBgisanormalizing
constant,whichwecanreformulateasfollows.SinceBD.B\A/[.B\A/,
andsinceB\AandB\Aaremutuallyexclusiveevents,

PrfBgDPrfB\AgCPr B\A

DPrfAgPrfBjAgCPr UNPr BjA :
Substitutingintoequation(C.17),weobtainanequivalentformofBayes'stheo
rem:
PrfAgPrfBjAg
:
PrfAjBgD (C.18)
PrfAgPrfBjAgCPr UNPr BjA
Bayes'stheoremcansimplifythecomputingofconditionalprobabilities.por
example,supposethatwehaveafaircoinandabiasedcointhatalwayscomesup
heads.Werunanexperimentconsistingofthreeindependentevents:wechoose
oneofthetwocoinsatrandom,weflipthatcoinonce,andthenweflipitagain.
Supposethatthecoinwehavechosencomesupheadsbothtimes.Culesel
probabilitythatitisbiased?

https://translate.googleusercontent.com/translate_f 219/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
WesolvethisproblemusingBayes'stheorem.LetAbetheeventthatwechoose
thebiasedcoin,andletBbetheeventthatthechosencoincomesupheadsboth
veces.WewishtodeterminePrfAjBg.WehavePrfAgD1=2,PrfBjAgD1,

Pr UN D1=2,andPr BjA D1=4hence,
.1=2/1
PrfAjBgD
.1=2/1C.1=2/.1=4/
D4=5:

Exercises

C.21
ProfessorRosencrantzflipsafaircoinonce. ProfessorGuildensternflipsafair
cointwice.WhatistheprobabilitythatProfessorRosencrantzobtainsmoreheads
thanProfessorGuildenstern ?

Pgina316

C.2Probability 1195

C.22
ProveBoole'sinequality:Foranyfiniteorcountablyinfinitesequenceofevents
A1A2:::,
PrfA1[A2[gPrfA1gCPrfA2gC: (C.19)

C.23
Supposeweshuffleadeckof10cards,eachbearingadistinctnumberfrom1to10,
tomixthecardsthoroughly.Wethenremovethreecards,oneatatime,fromthe
cubierta.Whatistheprobabilitythatweselectthethreecardsinsorted(increasing)
order?

C.24
Provethat

PrfAjBgCPr AjB D1:

C.25
ProvethatforanycollectionofeventsA1A2:::An,
PrfA1\A2\\AngDPrfA1gPrfA2jA1gPrfA3jA1\A2g
PrfAnjA1\A2\\An1g:

C.26?
Describeaprocedurethattakesasinputtwointegersaandbsuchthat0<a<b
and,usingfaircoinflips,producesasoutputheadswithprobabilitya=bandtails
withprobability.b a/=b.Giveaboundontheexpectednumberofcoinflips,
whichshouldbeO.1/.(Hint:Representa=binbinary.)

C.27?
Showhowtoconstructasetofneventsthatarepairwiseindependentbutsuchthat
nosubsetofk>2ofthemismutuallyindependent.

C.28?
TwoeventsAandBareconditionallyindependent,givenC,if
PrfA\BjCgDPrfAjCgPrfBjCg:
Giveasimplebutnontrivialexampleoftwoeventsthatarenotindependentbutare
conditionallyindependentgivenathirdevent.

C.29?
Youareacontestantinagameshowinwhichaprizeishiddenbehindoneof
threecurtains.Youwillwintheprizeifyouselectthecorrectcurtain.Despusdeti

Pgina317

1196 AppendixCCountingandProbability

https://translate.googleusercontent.com/translate_f 220/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,

havepickedonecurtainbutbeforethecurtainislifted,theemceeliftsoneofthe
othercurtains,knowingthatitwillrevealanemptystage,andasksifyouwould
liketoswitchfromyourcurrentselectiontotheremainingcurtain.Howwould
yourchanceschangeifyouswitch?(ThisquestionisthecelebratedMontyHall
problem,namedafteragameshowhostwhooftenpresentedcontestantswithjust
thisdilemma.)

C.210?
Aprisonwardenhasrandomlypickedoneprisoneramongthreetogofree.los
othertwowillbeexecuted.Theguardknowswhichonewillgofreebutisforbid
dentogiveanyprisonerinformationregardinghisstatus.Letuscalltheprisoners
X,Y,andZ.PrisonerXaskstheguardprivatelywhichofYorZwillbeexe
cuted,arguingthatsincehealreadyknowsthatatleastoneofthemmustdie,the
guardwon'tberevealinganyinformationabouthisownstatus.TheguardtellsX
thatYistobeexecuted.PrisonerXfeelshappiernow,sincehefiguresthateither
heorprisonerZwillgofree,whichmeansthathisprobabilityofgoingfreeis
now1=2.Isheright,orarehischancesstill1=3?Explique.

C.3Discreterandomvariables

A(discrete)randomvariableXisafunctionfromafiniteorcountablyinfinite
samplespaceStotherealnumbers.Itassociatesarealnumberwitheachpossible
outcomeofanexperiment,whichallowsustoworkwiththeprobabilitydistribu
tioninducedontheresultingsetofnumbers.Randomvariablescanalsobedefined
foruncountablyinfinitesamplespaces,buttheyraisetechnicalissuesthatareun
necessarytoaddressforourpurposes.Henceforth,weshallassumethatrandom
variablesarediscrete.
ForarandomvariableXandarealnumberx,wedefinetheeventXDxtobe
fs2SWXs/Dxgas,
x
PrfXDxgD Prfsg:
s2SWX.s/Dx
Thefunction
f.x/DPrfXDxg

istheprobabilitydensityfunctionoftherandomvariableX.Fromtheprobability
PAG
axioms,PrfXDxg0and xPrfXDxgD1.
Asanexample,considertheexperimentofrollingapairofordinary,6sided
dice.Thereare36possibleelementaryeventsinthesamplespace.Weassume

Pgina318

C.3Discreterandomvariables 1197

thattheprobabilitydistributionisuniform,sothateachelementaryevents2Sis
equallylikely:PrfsgD1=36.DefinetherandomvariableXtobethemaximumof
thetwovaluesshowingonthedice.WehavePrfXD3gD5=36,sinceXassigns
avalueof3to5ofthe36possibleelementaryevents,namely,.13/,.23/,.33/,
.32/,and.31/.
Weoftendefineseveralrandomvariablesonthesamesamplespace.IfXandY
arerandomvariables,thefunction
fxy/DPrfXDxandYDyg
isthejointprobabilitydensityfunctionofXandY.Forafixedvaluey,
x
PrfYDygD PrfXDxandYDyg
x
andsimilarly,forafixedvaluex,
x
PrfXDxgD PrfXDxandYDyg:
Y
Usingthedefinition(C.14)ofconditionalprobability,wehave
PrfXDxandYDyg
PrfXDxjYDygD :
PrfYDyg

https://translate.googleusercontent.com/translate_f 221/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
WedefinetworandomvariablesXandYtobeindependentifforallxandy,the
eventsXDxandYDyareindependentor,equivalently,ifforallxandy,we
havePrfXDxandYDygDPrfXDxgPrfYDyg.
Givenasetofrandomvariablesdefinedoverthesamesamplespace,wecan
definenewrandomvariablesassums,products,orotherfunctionsoftheoriginal
Variables.

Expectedvalueofarandomvariable
Thesimplestandmostusefulsummaryofthedistributionofarandomvariableis
theaverageofthevaluesittakeson.Theexpectedvalue(or,synonymously,
expectationormean)ofadiscreterandomvariableXis
x
EXD xPrfXDxg (C.20)
x
whichiswelldefinedifthesumisfiniteorconvergesabsolutely.Avecesel
expectationofXisdenotedbyXor,whentherandomvariableisapparentfrom
context,simplyby.
Consideragameinwhichyoufliptwofaircoins.Youearn$3foreachheadbut
lose$2foreachtail.TheexpectedvalueoftherandomvariableXrepresenting

Pgina319

1198 AppendixCCountingandProbability

yourearningsis
EXD6Prf2H'sgC1Prf1H,1Tg4Prf2T'sg
D6.1=4/C1.1=2/4.1=4/
D1:
Theexpectationofthesumoftworandomvariablesisthesumoftheirexpecta
tions,thatis,
EXCYDEXCEY (C.21)
wheneverEXandEYaredefined.Wecallthispropertylinearityofexpecta
tion,anditholdsevenifXandYarenotindependent.Italsoextendstofiniteand
absolutelyconvergentsummationsofexpectations.Linearityofexpectationisthe
keypropertythatenablesustoperformprobabilisticanalysesbyusingindicator
randomvariables(seeSection5.2).
IfXisanyrandomvariable,anyfunctiongx/definesanewrandomvari
ablegX/.IftheexpectationofgX/isdefined,then
x
Eg.X/D gx/PrfXDxg:
x
Lettinggx/Dax,wehaveforanyconstanta,
EaXDaEX: (C.22)
Consequently,expectationsarelinear:foranytworandomvariablesXandYand
anyconstanta,
EaXCYDaEXCEY: (C.23)
WhentworandomvariablesXandYareindependentandeachhasadefined
expectation,
x x
EXYD xyPrfXDxandYDyg
xx xY
re xyPrfXDxgPrfYDyg
x Y ! !
x x
re xPrfXDxg yPrfYDyg
x Y
DEXEY:
Ingeneral,whennrandomvariablesX1X2:::Xnaremutuallyindependent,
EX1X2 XnDEX1EX2EXn: (C.24)

https://translate.googleusercontent.com/translate_f 222/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,

Pgina320

C.3Discreterandomvariables 1199

WhenarandomvariableXtakesonvaluesfromthesetofnaturalnumbers
NDf012:::g,wehaveaniceformulaforitsexpectation:
x1
EXD iPrfXDig
ID0
x1
re i.PrfX igPrfX iC1g/
ID0
x1
re PrfX ig (C.25)
ID1
sinceeachtermPrfX igisaddedinitimesandsubtractedouti 1times
(exceptPrfX 0g,whichisaddedin0timesandnotsubtractedoutatall).
Whenweapplyaconvexfunctionf.x/toarandomvariableX,Jensen'sin
equalitygivesus

Ef.X/ f.EX/ (C.26)


providedthattheexpectationsexistandarefinite.(Afunctionf.x/isconvex
ifforallxandyandforall0 1,wehavef.xC.1 /y/
f.x/C.1 /f.y/.)

Varianceandstandarddeviation
Theexpectedvalueofarandomvariabledoesnottellushowspreadoutthe
variable'svaluesare.Forexample,ifwehaverandomvariablesXandYforwhich
PrfXD1=4gDPrfXD3=4gD1=2andPrfYD0gDPrfYD1gD1=2,
thenbothEXandEYare1=2,yettheactualvaluestakenonbyYarefarther
fromthemeanthantheactualvaluestakenonbyX.
Thenotionofvariancemathematicallyexpresseshowfarfromthemeanaran
domvariable'svaluesarelikelytobe.ThevarianceofarandomvariableXwith
meanEXis

VarXDE.XEX/2
DEX2 2XEXCE2X
DEX2 2EXEXCE2X
DEX2 2E2XCE2X
DEX2 E2X: (C.27)

TojustifytheequalityEE2XDE2X,notethatbecauseEXisarealnum
berandnotarandomvariable,soisE2X.TheequalityEXEXDE2X

Pgina321

1200 AppendixCCountingandProbability

followsfromequation(C.22),withaDEX.Rewritingequation(C.27)yields
anexpressionfortheexpectationofthesquareofarandomvariable:
EX2DVarXCE2X: (C.28)

ThevarianceofarandomvariableXandthevarianceofaXarerelated(see
ExerciseC.310):

VaraXDa2VarX:
WhenXandYareindependentrandomvariables,
VarXCYDVarXCVarY:

Ingeneral,ifnrandomvariablesX1X2:::Xnarepairwiseindependent,then
"n #
x xnorte

https://translate.googleusercontent.com/translate_f 223/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
Var Xi re VarXi: (C.29)
ID1 ID1
ThestandarddeviationofarandomvariableXisthenonnegativesquareroot
ofthevarianceofX.ThestandarddeviationofarandomvariableXissometimes
denotedXorsimplywhentherandomvariableXisunderstoodfromcontext.
Withthisnotation,thevarianceofXisdenoted2.

Exercises

C.31
Supposewerolltwoordinary,6sideddice.Whatistheexpectationofthesum
ofthetwovaluesshowing?Whatistheexpectationofthemaximumofthetwo
valuesshowing?

C.32
AnarrayA1::ncontainsndistinctnumbersthatarerandomlyordered,witheach
permutationofthennumbersbeingequallylikely.Whatistheexpectationofthe
indexofthemaximumelementinthearray?Whatistheexpectationoftheindex
oftheminimumelementinthearray?

C.33
Acarnivalgameconsistsofthreediceinacage.Aplayercanbetadollaronany
ofthenumbers1through6.Thecageisshaken,andthepayoffisasfollows.Siel
player'snumberdoesn'tappearonanyofthedice,heloseshisdollar.Deotramanera,
ifhisnumberappearsonexactlykofthethreedice,forkD123,hekeepshis
dollarandwinskmoredollars.Whatishisexpectedgainfromplayingthecarnival
gameonce?

Pgina322

C.4Thegeometricandbinomialdistributions 1201

C.34
ArguethatifXandYarenonnegativerandomvariables,then
Emax.XY/EXCEY:

C.35?
LetXandYbeindependentrandomvariables.Provethatf.X/andgY/are
independentforanychoiceoffunctionsfandg.

C.36?
LetXbeanonnegativerandomvariable,andsupposethatEXiswelldefined.
ProveMarkov'sinequality:

PrfX tgEX=t (C.30)


forallt>0.

C.37?
LetSbeasamplespace,andletXandX0berandomvariablessuchthat
Xs/ X0.s/foralls2S.Provethatforanyrealconstantt,
PrfX tgPrfX0 tg:

C.38
Whichislarger:theexpectationofthesquareofarandomvariable,orthesquare
ofitsexpectation?

C.39
ShowthatforanyrandomvariableXthattakesononlythevalues0and1,wehave
VarXDEXE1X.

C.310
ProvethatVaraXDa2VarXfromthedefinition(C.27)ofvariance.

C.4Thegeometricandbinomialdistributions

https://translate.googleusercontent.com/translate_f 224/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
WecanthinkofacoinflipasaninstanceofaBernoullitrial,whichisanexperi
mentwithonlytwopossibleoutcomes:success,whichoccurswithprobabilityp,
andfailure,whichoccurswithprobabilityqD1p.WhenwespeakofBernoulli
trialscollectively,wemeanthatthetrialsaremutuallyindependentand,unlesswe
specificallysayotherwise,thateachhasthesameprobabilitypforsuccess.Dos

Pgina323

1202 AppendixCCountingandProbability

UN UN
k1

2 1
3 3

0,35

0,30

0,25

0,20

0.15

0,10

0,05

K
123456789101112131415

FigureC.1AgeometricdistributionwithprobabilitypD1=3ofsuccessandaprobability
qD1poffailure.Theexpectationofthedistributionis1=pD3.

importantdistributionsarisefromBernoullitrials:thegeometricdistributionand
thebinomialdistribution.

Thegeometricdistribution

SupposewehaveasequenceofBernoullitrials,eachwithaprobabilitypofsuc
cessandaprobabilityqD1poffailure.Howmanytrialsoccurbeforeweobtain
asuccess?LetusdefinetherandomvariableXbethenumberoftrialsneededto
obtainasuccess.ThenXhasvaluesintherangef12:::g,andfork 1,
PrfXDkgDqk1p (C.31)

sincewehavek1failuresbeforetheonesuccess.Aprobabilitydistributionsat
isfyingequation(C.31)issaidtobeageometricdistribution.FigureC.1illustrates
suchadistribution.

Pgina324

C.4Thegeometricandbinomialdistributions 1203

Assumingthatq<1,wecancalculatetheexpectationofageometricdistribu
tionusingidentity(A.8):
x1
EXD kqk1p
kD1
pagx1
re kqk
Q
kD0
https://translate.googleusercontent.com/translate_f 225/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,

re pag Q
Q .1q/2
pag Q
re
Q p2
D1=p: (C.32)
Thus,onaverage,ittakes1=ptrialsbeforeweobtainasuccess,anintuitiveresult.
Thevariance,whichcanbecalculatedsimilarly,butusingExerciseA.13,is
VarXDq=p2: (C.33)
Asanexample,supposewerepeatedlyrolltwodiceuntilweobtaineithera
sevenoraneleven.Ofthe36possibleoutcomes,6yieldasevenand2yieldan
once.Thus,theprobabilityofsuccessispD8=36D2=9,andwemustroll
1=pD9=2D4:5timesonaveragetoobtainasevenoreleven.

Thebinomialdistribution

HowmanysuccessesoccurduringnBernoullitrials,whereasuccessoccurswith
probabilitypandafailurewithprobabilityqD1p?Definetherandomvari
ableXtobethenumberofsuccessesinntrials.ThenXhasvaluesintherange
f01:::ng,andforkD01:::n,
!
norte
PrfXDkgD pkqnk (C.34)
K
sincethereare norte waystopickwhichkofthentrialsaresuccesses,andthe
K
probabilitythateachoccursispkqnk.Aprobabilitydistributionsatisfyingequa
tion(C.34)issaidtobeabinomialdistribution.Forconvenience,wedefinethe
familyofbinomialdistributionsusingthenotation
!
norte
b.kInp/D pk.1p/nk: (C.35)
K
FigureC.2illustratesabinomialdistribution.Thenamebinomialcomesfromthe
righthandsideofequation(C.34)beingthekthtermoftheexpansionof.pCq/n.
Consequently,sincepCqD1,

Pgina325

1204 AppendixCCountingandProbability

b(k15,1/3)

0,25

0,20

0.15

0,10

0,05

K
0123456789101112131415

FigureC.2Thebinomialdistributionb.kI151=3/resultingfromnD15Bernoullitrials,each
withprobabilitypD1=3ofsuccess.TheexpectationofthedistributionisnpD5.

xnorte
b.kInp/D1 (C.36)
kD0
asaxiom2oftheprobabilityaxiomsrequires.
Wecancomputetheexpectationofarandomvariablehavingabinomialdistri
butionfromequations(C.8)and(C.36).LetXbearandomvariablethatfollows
thebinomialdistributionb.kInp/,andletqD1p.Bythedefinitionofexpec
tation,wehave
xnorte
EXD kPrfXDkg
kD0
https://translate.googleusercontent.com/translate_f 226/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
xnorte
re kb.kInp/
kD0 !
xnorte norte
re K pkqnk
K
kD1 !
xnorten1
Dnp pk1qnk (byequation(C.8))
k1
kD1 !
xN1 n1
Dnp pkq.n1/k
K
kD0

Pgina326

C.4Thegeometricandbinomialdistributions 1205

xN1
Dnp b.kIn1p/
kD0
Dnp (byequation(C.36)). (C.37)
Byusingthelinearityofexpectation,wecanobtainthesameresultwithsub
stantiallylessalgebra.LetXibetherandomvariabledescribingthenumberof
successesintheithtrial.ThenEXiDp1Cq0Dp,andbylinearityof
expectation(equation(C.21)),theexpectednumberofsuccessesforntrialsis
"n #
x
EXDE Xi
ID1
xnorte
re EXi
ID1
xnorte
re pag
ID1
Dnp: (C.38)
Wecanusethesameapproachtocalculatethevarianceofthedistribution.Utilizando
equation(C.27),wehaveVarXiDEXi2E2Xi.SinceXionlytakesonthe
values0and1,wehaveX2
iDXi,whichimpliesEX2 iDEXiDp. Porlotanto,
VarXiDpp2Dp.1p/Dpq: (C.39)

TocomputethevarianceofX,wetakeadvantageoftheindependenceofthen
trialsthus,byequation(C.29),
"n #
x
VarXDVar Xi
ID1
xnorte
re VarXi
ID1
xnorte
re Pq
ID1
Dnpq: (C.40)
AsFigureC.2shows,thebinomialdistributionb.kInp/increaseswithkuntil
itreachesthemeannp,andthenitdecreases.Wecanprovethatthedistribution
alwaysbehavesinthismannerbylookingattheratioofsuccessiveterms:

Pgina327

1206 AppendixCCountingandProbability

b.kInp/ norte
pkqnk
https://translate.googleusercontent.com/translate_f 227/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
bk1Inp/ re norte
Kpk1qnkC1
K1
n.k1/.nkC1/p
re
k.nk/nq
.nkC1/p
re (C.41)
Kq
.nC1/pk
D1C :
Kq
Thisratioisgreaterthan1preciselywhen.nC1/p kispositive.Conse
quently,b.kInp/>bk1Inp/fork<.nC1/p(thedistributionincreases),
andb.kInp/<bk 1Inp/fork>.nC1/p(thedistributiondecreases).
IfkD.nC1/pisaninteger,thenb.kInp/Dbk1Inp/,andsothedistri
butionthenhastwomaxima:atkD.nC1/pandatk1D.nC1/p1Dnpq.
Otherwise,itattainsamaximumattheuniqueintegerkthatliesintherange
npq<k<.nC1/p.
Thefollowinglemmaprovidesanupperboundonthebinomialdistribution.

LemmaC.1
Letn 0,let0<p<1,letqD1p,andlet0kn.Then
k nk
np nq
b.kInp/ :
K nk

ProofUsingequation(C.6),wehave
!
norte
b.kInp/D pkqnk
K
k nk
norte norte
UN pkqnk
K k nk nk
np nq
re :
K nk

Exercises

C.41
Verifyaxiom2oftheprobabilityaxiomsforthegeometricdistribution.

C.42
Howmanytimesonaveragemustweflip6faircoinsbeforeweobtain3heads
and3tails?

Pgina328

C.4Thegeometricandbinomialdistributions 1207

C.43
Showthatb.kInp/DbnkInq/,whereqD1p.

C.44
Showthatvalueofthemaximumofthebinomialdistributionb.kInp/isapprox
pag
imately1= 2npq,whereqD1p.

C.45?
ShowthattheprobabilityofnosuccessesinnBernoullitrials,eachwithprobability
pD1=n,isapproximately1=e.Showthattheprobabilityofexactlyonesuccess
isalsoapproximately1=e.

C.46?
ProfessorRosencrantzflipsafaircoinntimes,andsodoesProfessorGuildenstern.
Showthattheprobabilitythattheygetthesamenumberofheadsis 2n =4n.(Hint:
norte
ForProfessorRosencrantz,callaheadasuccessforProfessorGuildenstern,call
atailasuccess.)Useyourargumenttoverifytheidentity
!2 !
xnortenorte 2n
re :
K norte
kD0

C.47?
Showthatfor0kn,
https://translate.googleusercontent.com/translate_f 228/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,

b.kIn1=2/2nHk=n/n
whereHx/istheentropyfunction(C.7).

C.48?
ConsidernBernoullitrials,whereforiD12:::n,theithtrialhasprobabil
itypiofsuccess,andletXbetherandomvariabledenotingthetotalnumberof
successes.Letp piforalliD12:::norte.Provethatfor1kn,
xK1
PrfX<kg b.iInp/:
ID0

C.49?
LetXbetherandomvariableforthetotalnumberofsuccessesinasetAofn
Bernoullitrials,wheretheithtrialhasaprobabilitypiofsuccess,andletX0
betherandomvariableforthetotalnumberofsuccessesinasecondsetA0ofn
Bernoullitrials,wheretheithtrialhasaprobabilityp0 yo piofsuccess.Provethat
for0kn,

Pgina329

1208 AppendixCCountingandProbability

PrfX0 kgPrfX kg:


(Hint:ShowhowtoobtaintheBernoullitrialsinA0byanexperimentinvolving
thetrialsofA,andusetheresultofExerciseC.37.)

?C.5Thetailsofthebinomialdistribution

Theprobabilityofhavingatleast,oratmost,ksuccessesinnBernoullitrials,
eachwithprobabilitypofsuccess,isoftenofmoreinterestthantheprobabilityof
havingexactlyksuccesses.Inthissection,weinvestigatethetailsofthebinomial
distribution:thetworegionsofthedistributionb.kInp/thatarefarfromthe
meannp.Weshallproveseveralimportantboundson(thesumofalltermsin)a
cola.
Wefirstprovideaboundontherighttailofthedistributionb.kInp/.Podemos
determineboundsonthelefttailbyinvertingtherolesofsuccessesandfailures.

TheoremC.2
ConsiderasequenceofnBernoullitrials,wheresuccessoccurswithprobabilityp.
LetXbetherandomvariabledenotingthetotalnumberofsuccesses.Thenfor
0kn,theprobabilityofatleastksuccessesis
xnorte
PrfX kgD b.iInp/
iDk!
norte
UN pk:
K

ProofForSf12:::ng,weletASdenotetheeventthattheithtrialisa
successforeveryi2S.ClearlyPrfASgDpkifjSjDk.Tenemos
PrfX kgDPrfthereexistsSf12:::ngWjSjDkandA
[ Sg

DPr A S

Sf12:::ngWjSjDk
x
UN PrfASg (byinequality(C.19))
Sf12:::ngWjSjDk
!
norte
re pk:
K

https://translate.googleusercontent.com/translate_f 229/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,

Pgina330

C.5Thetailsofthebinomialdistribution 1209

Thefollowingcorollaryrestatesthetheoremforthelefttailofthebinomial
distribucin.Ingeneral,weshallleaveittoyoutoadapttheproofsfromonetailto
elotro.

CorollaryC.3
ConsiderasequenceofnBernoullitrials,wheresuccessoccurswithprobabil
ityp.IfXistherandomvariabledenotingthetotalnumberofsuccesses,thenfor
0kn,theprobabilityofatmostksuccessesis
xK
PrfXkgD b.iInp/
ID0 !
norte
UN .1p/nk
nk
!
norte
re .1p/nk:
K

Ournextboundconcernsthelefttailofthebinomialdistribution.Itscorollary
showsthat,farfromthemean,thelefttaildiminishesexponentially.

TheoremC.4
ConsiderasequenceofnBernoullitrials,wheresuccessoccurswithprobabilityp
andfailurewithprobabilityqD1p.LetXbetherandomvariabledenotingthe
totalnumberofsuccesses.Thenfor0<k<np,theprobabilityoffewerthank
successesis
xK1
PrfX<kgD b.iInp/
ID0
Kq
< b.kInp/:
npk
Pk1
ProofWeboundtheseries iD0b.iInp/byageometricseriesusingthetech
niquefromSectionA.2,page1151.ForiD12:::k,wehavefromequa
tion(C.41),
bi1Inp/ Iq
re
b.iInp/ .niC1/p
Iq
<
.ni/p
Kq
UN :
.nk/p

Pgina331

1210 AppendixCCountingandProbability

Ifwelet
Kq
xD
.nk/p
Kq
<
.nnp/p
Kq
re
nqp
K
re
np
<1

itfollowsthat
bi1Inp/<xb.iInp/

for0<ik.Iterativelyapplyingthisinequalitykitimes,weobtain
https://translate.googleusercontent.com/translate_f 230/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,

b.iInp/<xkib.kInp/
for0i<k,andhence
xK1 xK1
b.iInp/< xkib.kInp/
ID0 ID0
x1
<b.kInp/ xi
ID0
x
re b.kInp/
1x
Kq
re b.kInp/:
npk

CorollaryC.5
ConsiderasequenceofnBernoullitrials,wheresuccessoccurswithprobabilityp
andfailurewithprobabilityqD1p.Thenfor0<knp=2,theprobabilityof
fewerthanksuccessesislessthanonehalfoftheprobabilityoffewerthankC1
successes.

ProofBecauseknp=2,wehave
Kq .np=2/q
UN
npk np.np=2/

Pgina332

C.5Thetailsofthebinomialdistribution 1211

.np=2/q
re
np=2
1 (C.42)
sinceq1.LettingXbetherandomvariabledenotingthenumberofsuccesses,
TheoremC.4andinequality(C.42)implythattheprobabilityoffewerthanksuc
cessesis
xK1
PrfX<kgD b.iInp/<b.kInp/:
ID0
Thuswehave
Pk1
PrfX<kg
re PkiD0b.iInp/
PrfX<kC1g
iD0P
b.iInp/
k1

re Pk1 iD0b.iInp/
iD0b.iInp/Cb.kInp/
<1=2
Pk1
yaque
iD0b.iInp/<b.kInp/.
Boundsontherighttailfollowsimilarly.ExerciseC.52asksyoutoprovethem.

CorollaryC.6
ConsiderasequenceofnBernoullitrials,wheresuccessoccurswithprobabilityp.
LetXbetherandomvariabledenotingthetotalnumberofsuccesses.Thenfor
np<k<n,theprobabilityofmorethanksuccessesis
xnorte
PrfX>kgD b.iInp/
iDkC1
.nk/p
< b.kInp/:
knp

CorollaryC.7
ConsiderasequenceofnBernoullitrials,wheresuccessoccurswithprobabilityp
andfailurewithprobabilityqD1 pag.Thenfor.npCn/=2<k<n,the
probabilityofmorethanksuccessesislessthanonehalfoftheprobabilityof
morethank1successes.

https://translate.googleusercontent.com/translate_f 231/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,

ThenexttheoremconsidersnBernoullitrials,eachwithaprobabilitypiof
success,foriD12:::norte.Asthesubsequentcorollaryshows,wecanusethe

Pgina333

1212 AppendixCCountingandProbability

theoremtoprovideaboundontherighttailofthebinomialdistributionbysetting
piDpforeachtrial.

TheoremC.8
ConsiderasequenceofnBernoullitrials,whereintheithtrial,foriD12:::n,
successoccurswithprobabilitypiandfailureoccurswithprobabilityqiD1pi.
LetXbetherandomvariabledescribingthetotalnumberofsuccesses,andlet
DEX.Thenforr>,
r
mi
PrfX rg :
R

ProofSinceforany>0,thefunctione
xisstrictlyincreasinginx,

PrfX rgDPr e.X/ er (C.43)


wherewewilldeterminelater.UsingMarkov'sinequality(C.30),weobtain

Pr e.X/ er Ee.X/er: (C.44)
ThebulkoftheproofconsistsofboundingEe.X/ andsubstitutingasuit
ablevalueforininequality(C.44).First,weevaluateEe.X/.Utilizandoel
techniqueofindicatorrandomvariables(seeSection5.2),letXiDIftheith
BernoullitrialisasuccessgforiD12:::nortethatis,Xiistherandomvari
ablethatis1iftheithBernoullitrialisasuccessand0ifitisafailure.As,
xnorte
XD Xi
ID1
andbylinearityofexpectation,
"n #
x xnorte x norte
DEXDE Xi re EXiD pi
ID1 ID1 ID1
whichimplies
xnorte
x re .Xi pi/:
ID1
ToevaluateEe.X/,wesubstituteforX ,obtaining
P n

Ee.X/ DEe .X p /
"n ID1
i i

#
Y
Delaware e.X p / i i

ID1
Ynorte
re Ee.X i
p /
i

ID1

Pgina334

C.5Thetailsofthebinomialdistribution 1213

whichfollowsfrom(C.24),sincethemutualindependenceoftherandomvari
ablesXiimpliesthemutualindependenceoftherandomvariablese.X p /(see
i i

ExerciseC.35).Bythedefinitionofexpectation,

Ee.Xi
p /
i De.1p /piCe.0p /qi
i i

Dpieq Cqiep
i i

pieC1 (C.45)
exp.pie/

https://translate.googleusercontent.com/translate_f 232/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
whereexp.x/denotestheexponentialfunction:exp.x/De
followsfromtheinequalities>0,qi1,eq e,andexp.(Inequality(C.45)
i i 1,andthelast
linefollowsfrominequality(3.12).)Consequently,
Ynorte
Ee.X/ re Ee.X p /
i i

ID1
Ynorte
UN exp.pie/
ID1 !
x norte
Dexp pie
ID1
Dexp.e/ (C.46)
Pn
yaque re
iD1pi.Therefore,fromequation(C.43)andinequalities(C.44)
and(C.46),itfollowsthat
PrfX rgexp.e r/: (C.47)
ChoosingDln.r=/(seeExerciseC.57),weobtain
PrfX rgexp.eln.r=/ rln.r=//
Dexp.rrln.r=//
er
re
.r=/
r
r
mi
re :
R

WhenappliedtoBernoullitrialsinwhicheachtrialhasthesameprobabilityof
success,TheoremC.8yieldsthefollowingcorollaryboundingtherighttailofa
binomialdistribution.

Pgina335

1214 AppendixCCountingandProbability

CorollaryC.9
ConsiderasequenceofnBernoullitrials,whereineachtrialsuccessoccurswith
probabilitypandfailureoccurswithprobabilityqD1p.Thenforr>np,
x norte
PrfXnp rgD b.kInp/
kDdnpCre
r
npe
UN :
R

ProofByequation(C.37),wehaveDEXDnp.

Exercises

C.51?
Whichislesslikely:obtainingnoheadswhenyouflipafaircoinntimes,or
obtainingfewerthannheadswhenyouflipthecoin4ntimes?

C.52?
ProveCorollariesC.6andC.7.

C.53?
Showthat
!
xK1
norte K
ai<.aC1/n b.kIna=.aC1//
yo nakaC1/
ID0
foralla>0andallksuchthat0<k<na=.aC1/.

C.54?
Provethatif0<k<np,where0<p<1andqD1p,then
xK1 k nk
Kq np nq
piqni< :
https://translate.googleusercontent.com/translate_f 233/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
ID0 npk K nk

C.55?
ShowthattheconditionsofTheoremC.8implythat
UN r
.norte /mi
PrfX rg :
R
Similarly,showthattheconditionsofCorollaryC.9implythat
r
nqe
PrfnpX rg :
R

Pgina336

ProblemsforAppendixC 1215

C.56?
ConsiderasequenceofnBernoullitrials,whereintheithtrial,foriD12:::n,
successoccurswithprobabilitypiandfailureoccurswithprobabilityqiD1pi.
LetXbetherandomvariabledescribingthetotalnumberofsuccesses,andlet
DEX.Showthatforr 0,

PrfX rger =2n:


2

(Hint:Provethatpieq Cqiep e =2.Thenfollowtheoutlineoftheproof


i i 2

ofTheoremC.8,usingthisinequalityinplaceofinequality(C.45).)

C.57?
ShowthatchoosingDln.r=/minimizestherighthandsideofinequal
ity(C.47).

Problemas

C1Ballsandbins
Inthisproblem,weinvestigatetheeffectofvariousassumptionsonthenumberof
waysofplacingnballsintobdistinctbins.

a.Supposethatthenballsaredistinctandthattheirorderwithinabindoesnot
importar.Arguethatthenumberofwaysofplacingtheballsinthebinsisbn.

segundo.Supposethattheballsaredistinctandthattheballsineachbinareordered.
Provethatthereareexactly.bCn1/=.b1/waystoplacetheballsinthe
bins.(Hint:Considerthenumberofwaysofarrangingndistinctballsandb1
indistinguishablesticksinarow.)

do.Supposethattheballsareidentical,andhencetheirorderwithinabindoesnot
importar.ShowthatthenumberofwaysofplacingtheballsinthebinsisbCn1 .
norte
(Hint:Ofthearrangementsinpart(b),howmanyarerepeatediftheballsare
madeidentical?)

re.Supposethattheballsareidenticalandthatnobinmaycontainmorethanone
ball,sothatnb.Showthatthenumberofwaysofplacingtheballsis segundo
.
norte
mi.Supposethattheballsareidenticalandthatnobinmaybeleftempty.Assuming
thatn b,showthatthenumberofwaysofplacingtheballsisn1 .
B1

Pgina337

1216 AppendixCCountingandProbability

Appendixnotes

https://translate.googleusercontent.com/translate_f 234/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,

Thefirstgeneralmethodsforsolvingprobabilityproblemswerediscussedina
famouscorrespondencebetweenB.PascalandP.deFermat,whichbeganin1654,
andinabookbyC.Huygensin1657.Rigorousprobabilitytheorybeganwiththe
workofJ.Bernoulliin1713andA.DeMoivrein1730.Furtherdevelopmentsof
thetheorywereprovidedbyP.S.Laplace,S.D.Poisson,andCFGauss.
SumsofrandomvariableswereoriginallystudiedbyPLChebyshevandAA
Markov.ANKolmogorovaxiomatizedprobabilitytheoryin1933.Chernoff[66]
andHoeffding[173]providedboundsonthetailsofdistributions.Seminalwork
inrandomcombinatorialstructureswasdonebyP.Erds.
Knuth[209]andLiu[237]aregoodreferencesforelementarycombinatorics
andcounting.StandardtextbookssuchasBillingsley[46],Chung[67],Drake[95],
Feller[104],andRozanov[300]offercomprehensiveintroductionstoprobability.

Pgina338

re Matrices

Matricesariseinnumerousapplications,including,butbynomeanslimitedto,
scientificcomputing.Ifyouhaveseenmatricesbefore,muchofthematerialinthis
appendixwillbefamiliartoyou,butsomeofitmightbenew.SectionD.1covers
basicmatrixdefinitionsandoperations,andSectionD.2presentssomebasicmatrix
properties.

D.1Matricesandmatrixoperations

Inthissection,wereviewsomebasicconceptsofmatrixtheoryandsomefunda
mentalpropertiesofmatrices.

https://translate.googleusercontent.com/translate_f 235/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
Matricesandvectors
Amatrixisarectangulararrayofnumbers.Porejemplo,
UN UN
a11 a12 a13
ANUNCIO
a21 a22 a23
UN UN
123
re (D.1)
456
isa23matrixAD.aij/,whereforiD12andjD123,wedenotethe
elementofthematrixinrowiandcolumnjbyaij.Weuseuppercaseletters
todenotematricesandcorrespondingsubscriptedlowercaseletterstodenotetheir
elementos.WedenotethesetofallmnmatriceswithrealvaluedentriesbyRmn
and,ingeneral,thesetofmnmatriceswithentriesdrawnfromasetSbySmn.
ThetransposeofamatrixAisthematrixATobtainedbyexchangingtherows
andcolumnsofA.ForthematrixAofequation(D.1),

Pgina339

1218 AppendixDMatrices

14
ATD 25 :
36
Avectorisaonedimensionalarrayofnumbers.Porejemplo,
2
xD 3
5
isavectorofsize3.Wesometimescallavectoroflengthnannvector.Nosotros
uselowercaseletterstodenotevectors,andwedenotetheithelementofasizen
vectorxbyxi,foriD12:::norte.Wetakethestandardformofavectortobe
asacolumnvectorequivalenttoann1matrixthecorrespondingrowvectoris
obtainedbytakingthetranspose:
xTD.235/:
Theunitvectoreiisthevectorwhoseithelementis1andallofwhoseother
elementsare0.Usually,thesizeofaunitvectorisclearfromthecontext.
Azeromatrixisamatrixallofwhoseentriesare0.Suchamatrixisoften
denoted0,sincetheambiguitybetweenthenumber0andamatrixof0sisusually
easilyresolvedfromcontext.Ifamatrixof0sisintended,thenthesizeofthe
matrixalsoneedstobederivedfromthecontext.

Squarematrices

Squarennmatricesarisefrequently.Severalspecialcasesofsquarematrices
areofparticularinterest:
1.AdiagonalmatrixhasaijD0wheneverij.Becausealloftheoffdiagonal
elementsarezero,wecanspecifythematrixbylistingtheelementsalongthe
diagonal:

a11 0:::0
0a22 :::0
diag.a11a22:::ann/D :
Unesdoc.unesco.orgunesdoc.unesco.org
Unesdoc.unesco.orgunesdoc.unesco.org
Unesdoc.unesco.orgunesdoc.unesco.org
Unesdoc.unesco.orgunesdoc.unesco.org
0 0:::ann
2.ThennidentitymatrixInisadiagonalmatrixwith1salongthediagonal:

En Ddiag.11:::1/
10:::0
01:::0
re :::::::::::: :

00:::1

https://translate.googleusercontent.com/translate_f 236/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
Pgina340
D.1Matricesandmatrixoperations 1219

WhenIappearswithoutasubscript,wederiveitssizefromthecontext.Theith
columnofanidentitymatrixistheunitvectorei.
3.AtridiagonalmatrixTisoneforwhichtijD0ifjijj>1.Nonzeroentries
appearonlyonthemaindiagonal,immediatelyabovethemaindiagonal(tiiC1

foriD12:::norte 1),orimmediatelybelowthemaindiagonal(tiC1ifor
iD12:::n1):

t11 t12 0 0::: 0 0 0


t21 t22 t23 0::: 0 0 0
0t32 t33 t34 Unesdoc.unesco.orgunesdoc.unesco.org
0 0 0
Unesdoc.unesco.orgunesdoc.unesco.org
Unesdoc.unesco.orgunesdoc.unesco.org
Unesdoc.unesco.orgunesdoc.unesco.org
Unesdoc.unesco.orgunesdoc.unesco.org
Unesdoc.unesco.orgunesdoc.unesco.org
Unesdoc.unesco.orgunesdoc.unesco.org
Unesdoc.unesco.orgunesdoc.unesco.org
Unesdoc.unesco.orgunesdoc.unesco.org
TD :
0 0 0 0:::tn2n2 tn2n1 0
0 0 0 0:::tn1n2 tn1n1 tn1n
0 0 0 0::: 0 tnn1 tnn

4.AnuppertriangularmatrixUisoneforwhichuijD0ifi>j.Allentries
belowthediagonalarezero:

u11 u12 :::u1n


0u22 :::u2n
UD :
Unesdoc.unesco.orgunesdoc.unesco.org
Unesdoc.unesco.orgunesdoc.unesco.org
Unesdoc.unesco.orgunesdoc.unesco.org
Unesdoc.unesco.orgunesdoc.unesco.org
0 0:::unn

Anuppertriangularmatrixisunituppertriangularifithasall1salongthe
diagonal.
5.AlowertriangularmatrixLisoneforwhichlijD0ifi<j.Allentries
abovethediagonalarezero:

l11 0:::0
l21 l22 :::0
LD :
Unesdoc.unesco.orgunesdoc.unesco.org
Unesdoc.unesco.orgunesdoc.unesco.org
Unesdoc.unesco.orgunesdoc.unesco.org
Unesdoc.unesco.orgunesdoc.unesco.org
ln1 ln2 :::lnn

Alowertriangularmatrixisunitlowertriangularifithasall1salongthe
diagonal.

Pgina341

1220 AppendixDMatrices

6.ApermutationmatrixPhasexactlyone1ineachroworcolumn,and0s
enotraparte.Anexampleofapermutationmatrixis

01000
00010
PD 10000 :
00001
00100

Suchamatrixiscalledapermutationmatrixbecausemultiplyingavectorx
byapermutationmatrixhastheeffectofpermuting(rearranging)theelements
ofx.ExerciseD.14exploresadditionalpropertiesofpermutationmatrices.
7.AsymmetricmatrixAsatisfiestheconditionADAT.Porejemplo,

123
264
345

https://translate.googleusercontent.com/translate_f 237/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,

isasymmetricmatrix.

Basicmatrixoperations
Theelementsofamatrixorvectorarenumbersfromanumbersystem,suchas
therealnumbers,thecomplexnumbers,orintegersmoduloaprime.Elnmero
systemdefineshowtoaddandmultiplynumbers.Wecanextendthesedefinitions
toencompassadditionandmultiplicationofmatrices.
Wedefinematrixadditionasfollows.IfAD.aij/andBD.bij/aremn
matrices,thentheirmatrixsumCD.cij/DACBisthemnmatrixdefinedby
cijDaijCbij

foriD12:::mandjD12:::norte.Thatis,matrixadditionisperformed
componentwise.Azeromatrixistheidentityformatrixaddition:
AC0DAD0CA:
Si isanumberandAD.aij/isamatrix,thenAD.aij/isthescalar
multipleofAobtainedbymultiplyingeachofitselementsby.Asaspecialcase,
wedefinethenegativeofamatrixAD.aij/tobe1ADA,sothattheijth
entryofAisaij.Thus,
AC.A/D0D.A/CA:

Pgina342

D.1Matricesandmatrixoperations 1221

Weusethenegativeofamatrixtodefinematrixsubtraction:ABDAC.B/.
Wedefinematrixmultiplicationasfollows.WestartwithtwomatricesAandB
thatarecompatibleinthesensethatthenumberofcolumnsofAequalsthenumber
ofrowsofB.(Ingeneral,anexpressioncontainingamatrixproductABisalways
assumedtoimplythatmatricesAandBarecompatible.)IfAD.aik/isanmn
matrixandBD.bkj/isannpmatrix,thentheirmatrixproductCDABisthe
mpmatrixCD.cij/,where
x norte
cijD aikbkj (D.2)
kD1
foriD12:::mandjD12:::pag.TheprocedureSQUAREMATRIX
MULTIPLYinSection4.2implementsmatrixmultiplicationinthestraightfor
wardmannerbasedonequation(D.2),assumingthatthematricesaresquare:
mDnDp.Tomultiplyn nmatrices,SQUAREMATRIXMULTIPLYper
formsn3multiplicationsandn2.n1/additions,andsoitsrunningtimeis.n3/.
Matriceshavemany(butnotall)ofthealgebraicpropertiestypicalofnumbers.
Identitymatricesareidentitiesformatrixmultiplication:
ImADAInDA
foranymnmatrixA.Multiplyingbyazeromatrixgivesazeromatrix:
A0D0:
Matrixmultiplicationisassociative:
A.BC/D.AB/C
forcompatiblematricesA,B,andC.Matrixmultiplicationdistributesoveraddi
tion:
ABCC/DABCAC
.BCC/DDBDCCD:
Forn>1,multiplicationofnnmatricesisnotcommutative.Forexample,if
UN UN UN UN
01 00
ANUNCIO andBD ,then
00 10
UN UN
10
ABD
00
y
UN UN
https://translate.googleusercontent.com/translate_f 238/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,

BAD 00 :
01

Page343

1222 AppendixDMatrices

Wedefinematrixvectorproductsorvectorvectorproductsasifthevectorwere
theequivalentn1matrix(ora1nmatrix,inthecaseofarowvector).As,
ifAisanmnmatrixandxisannvector,thenAxisanmvector.Ifxandy
arenvectors,then
xnorte
xTyD xiyi
ID1
isanumber(actuallya11matrix)calledtheinnerproductofxandy.Elma
trixxyTisannnmatrixZcalledtheouterproductofxandy,withijDxiyj.
The(euclidean)normkxkofannvectorxisdefinedby
kxkD.x2
1Cx22CCx2 n/1=2
D.xTx/1=2:

Thus,thenormofxisitslengthinndimensionaleuclideanspace.

Exercises

D.11
ShowthatifAandBaresymmetricnnmatrices,thensoareACBandAB.

D.12
Provethat.AB/TDBTATandthatATAisalwaysasymmetricmatrix.

D.13
Provethattheproductoftwolowertriangularmatricesislowertriangular.

D.14
ProvethatifPisannnpermutationmatrixandAisannnmatrix,thenthe
matrixproductPAisAwithitsrowspermuted,andthematrixproductAPisA
withitscolumnspermuted.Provethattheproductoftwopermutationmatricesis
apermutationmatrix.

D.2Basicmatrixproperties

Inthissection,wedefinesomebasicpropertiespertainingtomatrices:inverses,
lineardependenceandindependence,rank,anddeterminants.Wealsodefinethe
classofpositivedefinitematrices.

Pgina344

D.2Basicmatrixproperties 1223

Matrixinverses,ranks,anddeterminants
WedefinetheinverseofannnmatrixAtobethennmatrix,denotedA1(if
itexists),suchthatAA1DInDA1A.Forexample,
UN 1 UN UN
11 0 1
re :
10 1 1
Manynonzeronnmatricesdonothaveinverses.Amatrixwithoutaninverseis
callednoninvertible,orsingular.Anexampleofanonzerosingularmatrixis
UN UN
10
:
https://translate.googleusercontent.com/translate_f 239/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
10
Ifamatrixhasaninverse,itiscalledinvertible,ornonsingular.Matrixinverses,
whentheyexist,areunique.(SeeExerciseD.21.)IfAandBarenonsingular
nnmatrices,then
.BA/1DA1B1:

Theinverseoperationcommuteswiththetransposeoperation:
.A1/TD.AT/1:

Thevectorsx1x2:::xnarelinearlydependentifthereexistcoefficients
c1c2:::cn,notallofwhicharezero,suchthatc1x1Cc2x2CCcnxnD0.
Therowvectorsx1D.123/,x2D.264/,andx3D.4119/are
linearlydependent,forexample,since2x1C3x2 2x3D0.Ifvectorsarenot
linearlydependent,theyarelinearlyindependent.Forexample,thecolumnsofan
identitymatrixarelinearlyindependent.
Thecolumnrankofanonzerom nmatrixAisthesizeofthelargestset
oflinearlyindependentcolumnsofA.Similarly,therowrankofAisthesize
ofthelargestsetoflinearlyindependentrowsofA.Afundamentalpropertyof
anymatrixAisthatitsrowrankalwaysequalsitscolumnrank,sothatwecan
simplyrefertotherankofA.Therankofanmnmatrixisanintegerbetween0
andmin.mn/,inclusive.(Therankofazeromatrixis0,andtherankofannn
identitymatrixisn.)Analternate,butequivalentandoftenmoreuseful,definition
isthattherankofanonzeromnmatrixAisthesmallestnumberrsuchthat
thereexistmatricesBandCofrespectivesizesmrandrnsuchthat
ADBC:
Asquarennmatrixhasfullrankifitsrankisn.Anmnmatrixhasfull
columnrankifitsrankisn.Thefollowingtheoremgivesafundamentalproperty
ofranks.

Pgina345

1224 AppendixDMatrices

TheoremD.1
Asquarematrixhasfullrankifandonlyifitisnonsingular.

AnullvectorforamatrixAisanonzerovectorxsuchthatAxD0.The
followingtheorem(whoseproofisleftasExerciseD.27)anditscorollaryrelate
thenotionsofcolumnrankandsingularitytonullvectors.

TheoremD.2
AmatrixAhasfullcolumnrankifandonlyifitdoesnothaveanullvector.

CorollaryD.3
AsquarematrixAissingularifandonlyifithasanullvector.

TheijthminorofannnmatrixA,forn>1,isthe.n1/.n1/matrixAij
obtainedbydeletingtheithrowandjthcolumnofA.Wedefinethedeterminant

ofannnmatrixArecursivelyintermsofitsminorsby
a11 ifnD1
det.A/D xnorte
.1/1Cja1jdet.A1j/ifn>1:
jD1
Theterm.1/iCjdet.Aij/isknownasthecofactoroftheelementaij.
Thefollowingtheorems,whoseproofsareomittedhere,expressfundamental
propertiesofthedeterminant.

TheoremD.4(Determinantproperties)
ThedeterminantofasquarematrixAhasthefollowingproperties:
IfanyroworanycolumnofAiszero,thendet.A/D0.
ThedeterminantofAismultipliedbyiftheentriesofanyonerow(orany

https://translate.googleusercontent.com/translate_f 240/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
onecolumn)ofAareallmultipliedby.
ThedeterminantofAisunchangediftheentriesinonerow(respectively,col
umn)areaddedtothoseinanotherrow(respectively,column).
ThedeterminantofAequalsthedeterminantofAT.
ThedeterminantofAismultipliedby1ifanytworows(oranytwocolumns)
areexchanged.
Also,foranysquarematricesAandB,wehavedet.AB/Ddet.A/det.B/.

Pgina346

D.2Basicmatrixproperties 1225

TheoremD.5
AnnnmatrixAissingularifandonlyifdet.A/D0.

Positivedefinitematrices
Positivedefinitematricesplayanimportantroleinmanyapplications.Annn
matrixAispositivedefiniteifxTAx>0forallnvectorsx0.For
example,theidentitymatrixispositivedefinite,sinceforanynonzerovector
xD.x1 x2 xn/T,
xTInxDxTx
xnorte
re x2
yo
ID1
>0:
Matricesthatariseinapplicationsareoftenpositivedefiniteduetothefollowing
teorema.

TheoremD.6
ForanymatrixAwithfullcolumnrank,thematrixATAispositivedefinite.

ProofWemustshowthatxT.ATA/x>0foranynonzerovectorx.Paracualquier
vectorx,
xT.ATA/xD.Ax/T.Ax/(byExerciseD.12)
DkAxk 2 :
NotethatkAxk 2 isjustthesumofthesquaresoftheelementsofthevectorAx.
Therefore,kAxk 2 0.IfkAxk 2 D0,everyelementofAxis0,whichistosay
AxD0.SinceAhasfullcolumnrank,AxD0impliesxD0,byTheoremD.2.
Hence,ATAispositivedefinite.

Section28.3exploresotherpropertiesofpositivedefinitematrices.

Exercises

D.21
Provethatmatrixinversesareunique,thatis,ifBandCareinversesofA,then
BDC.

D.22
Provethatthedeterminantofalowertriangularoruppertriangularmatrixisequal
totheproductofitsdiagonalelements.Provethattheinverseofalowertriangular
matrix,ifitexists,islowertriangular.

Pgina347

1226 AppendixDMatrices

D.23
ProvethatifPisapermutationmatrix,thenPisinvertible,itsinverseisPT,
andPTisapermutationmatrix.

https://translate.googleusercontent.com/translate_f 241/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
D.24
LetAandBbennmatricessuchthatABDI.ProvethatifA0isobtained
fromAbyaddingrowjintorowi,thensubtractingcolumnifromcolumnjofB
yieldstheinverseB0ofA0.

D.25
LetAbeanonsingularnnmatrixwithcomplexentries.Showthateveryentry
ofA1isrealifandonlyifeveryentryofAisreal.

D.26
ShowthatifAisanonsingular,symmetric,nnmatrix,thenA1issymmetric.
ShowthatifBisanarbitrarymnmatrix,thenthemmmatrixgivenbythe
productBABTissymmetric.

D.27
ProveTheoremD.2.Thatis,showthatamatrixAhasfullcolumnrankifandonly
ifAxD0impliesxD0.(Hint:Expressthelineardependenceofonecolumnon
theothersasamatrixvectorequation.)

D.28
ProvethatforanytwocompatiblematricesAandB,

rank.AB/min.rank.A/rank.B//
whereequalityholdsifeitherAorBisanonsingularsquarematrix.(Hint:Use
thealternatedefinitionoftherankofamatrix.)

Problemas

D1Vandermondematrix
Givennumbersx0x1:::xn1,provethatthedeterminantoftheVandermonde
matrix
1x0 x2 xn1
0 0
1x1 x2 xn1
V.x0x1:::xn1/D 1 1Unesdoc.unesco.orgunesdoc.unesco.org
Unesdoc.unesco.orgunesdoc.unesco.org
Unesdoc.unesco.orgunesdoc.unesco.org
Unesdoc.unesco.orgunesdoc.unesco.org
Unesdoc.unesco.orgunesdoc.unesco.org
1xn1 x2 xn1
N1 N1

Pgina348

ProblemsforAppendixD 1227

es
Y
det.V.x0x1:::xn1//D .xk xj/:
0j<kn1
(Hint:Multiplycolumnibyx0andaddittocolumniC1foriDn 1
n2:::1,andthenuseinduction.)

D2PermutationsdefinedbymatrixvectormultiplicationoverGF.2/
OneclassofpermutationsoftheintegersinthesetSnDf012:::2n 1gis
definedbymatrixmultiplicationoverGF.2/.ForeachintegerxinSn,weviewits
binaryrepresentationasannbitvector

x0
x1
x2
Unesdoc.unesco.orgunesdoc.unesco.org

xn1
Pn1
wherexD
iD0xi2i.IfAisannnmatrixinwhicheachentryiseither0
or1,thenwecandefineapermutationmappingeachvaluexinSntothenumber
whosebinaryrepresentationisthematrixvectorproductAx.Here,weperform
allarithmeticoverGF.2/:allvaluesareeither0or1,andwithoneexceptionthe
usualrulesofadditionandmultiplicationapply.Theexceptionisthat1C1D0.
YoucanthinkofarithmeticoverGF.2/asbeingjustlikeregularintegerarithmetic,
exceptthatyouuseonlytheleastsignificantbit.

https://translate.googleusercontent.com/translate_f 242/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
Asanexample,forS
UN UN 2Df0123g,thematrix

10
ANUNCIO
11

definesthefollowingpermutationA:A.0/D0,A.1/D3,A.2/D2,
A.3/D1.ToseewhyA.3/D1,observethat,workinginGF.2/,
UN UN
10 1
A.3/D 11 1
UN UN
11C01
re
11C11
UN UN
1
re
0
whichisthebinaryrepresentationof1.

Pgina349

1228 AppendixDMatrices

Fortheremainderofthisproblem,weworkoverGF.2/,andallmatrixand
vectorentriesare0or1.Wedefinetherankofa01matrix(amatrixforwhich
eachentryiseither0or1)overGF.2/thesameasforaregularmatrix,butwithall
arithmeticthatdetermineslinearindependenceperformedoverGF.2/.Wedefine
therangeofannn01matrixAby
RA/DfyWyDAxforsomex2Sng

sothatRA/isthesetofnumbersinSnthatwecanproducebymultiplyingeach
valuexinSnbyA.

a.IfristherankofmatrixA,provethatjR.A/jD2r.ConcludethatAdefinesa
permutationonSnonlyifAhasfullrank.

ForagivennnmatrixAandagivenvaluey2RA/,wedefinethepreimage
ofyby
P.Ay/DfxWAxDyg
sothatP.Ay/isthesetofvaluesinSnthatmaptoywhenmultipliedbyA.

segundo.IfristherankofnnmatrixAandy2RA/,provethatjP.Ay/jD2nr.

Let0mn,andsupposewepartitionthesetSnintoblocksofconsec
utivenumbers,wheretheithblockconsistsofthe2mnumbersi2mi2mC1
i2mC2:::.iC1/2m 1.ForanysubsetSSn,defineBSm/tobethe
setofsize2mblocksofSncontainingsomeelementofS.Asanexample,when
nD3,mD1,andSDf145g,thenBSm/consistsofblocks0(since1isin
the0thblock)and2(sinceboth4and5areinblock2).

do.Letrbetherankofthelowerleft.n m/msubmatrixofA,thatis,the
matrixformedbytakingtheintersectionofthebottomn mrowsandthe
leftmostmcolumnsofA.LetSbeanysize2mblockofSn,andletS0D
fyWyDAxforsomex2Sg.ProvethatjB.S0m/jD2randthatforeach
blockinBS0m/,exactly2mrnumbersinSmaptothatblock.

Becausemultiplyingthezerovectorbyanymatrixyieldsazerovector,theset
ofpermutationsofSndefinedbymultiplyingbynn01matriceswithfullrank
overGF.2/cannotincludeallpermutationsofSn.Letusextendtheclassofper
mutationsdefinedbymatrixvectormultiplicationtoincludeanadditiveterm,so
thatx2SnmapstoAxCc,wherecisannbitvectorandadditionisperformed
overGF.2/.Porejemplo,cuando
UN UN
10
ANUNCIO
11

Pgina350

https://translate.googleusercontent.com/translate_f 243/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
NotesforAppendixD 1229

y
UN UN
0
cD
1
wegetthefollowingpermutationAc:Ac.0/D2,Ac.1/D1,Ac.2/D0,
Ac.3/D3.Wecallanypermutationthatmapsx2SntoAxCc,forsomenn
01matrixAwithfullrankandsomenbitvectorc,alinearpermutation.

re.UseacountingargumenttoshowthatthenumberoflinearpermutationsofSn
ismuchlessthanthenumberofpermutationsofSn.

mi.GiveanexampleofavalueofnandapermutationofSnthatcannotbeachieved
byanylinearpermutation.(Hint:Foragivenpermutation,thinkabouthow
multiplyingamatrixbyaunitvectorrelatestothecolumnsofthematrix.)

Appendixnotes

Linearalgebratextbooksprovideplentyofbackgroundinformationonmatrices.
ThebooksbyStrang[323,324]areparticularlygood.

Pgina352
Pgina351

Bibliografa

[1]MiltonAbramowitzandIreneA.Stegun,editors.HandbookofMathematicalFunctions.
Dover,1965.
[2]GMAdel'sonVel'skiandEMLandis.Analgorithmfortheorganizationofinformation.
SovietMathematicsDoklady,3(5):12591263,1962.
[3]AlokAggarwalandJeffreyScottVitter.Theinput/outputcomplexityofsortingandrelated
problemas.CommunicationsoftheACM,31(9):11161127,1988.
[4]ManindraAgrawal,NeerajKayal,andNitinSaxena.PRIMESisinP.AnnalsofMathe
https://translate.googleusercontent.com/translate_f 244/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
matics,160(2):781793,2004.
[5]AlfredV.Aho,JohnE.Hopcroft,andJeffreyD.Ullman.TheDesignandAnalysisof
ComputerAlgorithms.AddisonWesley,1974.
[6]AlfredV.Aho,JohnE.Hopcroft,andJeffreyD.Ullman.DataStructuresandAlgorithms.
AddisonWesley,1983.
[7]RavindraK.Ahuja,ThomasL.Magnanti,andJamesB.Orlin.NetworkFlows:Theory,
Algorithms,andApplications.PrenticeHall,1993.
[8]RavindraK.Ahuja,KurtMehlhorn,JamesB.Orlin,andRobertE.Tarjan.Fasteralgorithms
fortheshortestpathproblem.JournaloftheACM,37:213223,1990.
[9]RavindraK.AhujaandJamesB.Orlin.Afastandsimplealgorithmforthemaximumflow
problema.OperationsResearch,37(5):748759,1989.
[10]RavindraK.Ahuja,JamesB.Orlin,andRobertE.Tarjan.Improvedtimeboundsforthe
maximumflowproblem.SIAMJournalonComputing,18(5):939954,1989.
[11]MiklsAjtai,NimrodMegiddo,andOrliWaarts.Improvedalgorithmsandanalysisfor
secretaryproblemsandgeneralizations.InProceedingsofthe36thAnnualSymposiumon
FoundationsofComputerScience,pages473482,1995.
[12]SelimG.Akl.TheDesignandAnalysisofParallelAlgorithms.PrenticeHall,1989.
[13]MohamadAkraandLouayBazzi.Onthesolutionoflinearrecurrenceequations.Compu
tationalOptimizationandApplications,10(2):195210,1998.
[14]NogaAlon.Generatingpseudorandompermutationsandmaximumflowalgorithms.En
formationProcessingLetters,35:201204,1990.

Pgina353

1232 Bibliografa

[15]ArneAndersson.Balancedsearchtreesmadesimple.InProceedingsoftheThirdWorkshop
onAlgorithmsandDataStructures,volume709ofLectureNotesinComputerScience,
pages6071.Springer,1993.
[16]ArneAndersson.Fasterdeterministicsortingandsearchinginlinearspace.InProceedings
ofthe37thAnnualSymposiumonFoundationsofComputerScience,pages135141,1996.
[17]ArneAndersson,TorbenHagerup,StefanNilsson,andRajeevRaman.Sortinginlinear
time?JournalofComputerandSystemSciences,57:7493,1998.
[18]TomM.Apostol.Calculus,volume1.BlaisdellPublishingCompany,secondedition,1967.
[19]NimarS.Arora,RobertD.Blumofe,andC.GregPlaxton.Threadschedulingformultipro
grammedmultiprocessors.InProceedingsofthe10thAnnualACMSymposiumonParallel
AlgorithmsandArchitectures,pages119129,1998.
[20]SanjeevArora.Probabilisticcheckingofproofsandthehardnessofapproximationprob
lems.PhDthesis,UniversityofCalifornia,Berkeley,1994.
[21]SanjeevArora.TheapproximabilityofNPhardproblems.InProceedingsofthe30th
AnnualACMSymposiumonTheoryofComputing,pages337348,1998.
[22]SanjeevArora.Polynomialtimeapproximationschemesforeuclideantravelingsalesman
andothergeometricproblems.JournaloftheACM,45(5):753782,1998.
[23]SanjeevAroraandCarstenLund.Hardnessofapproximations.InDoritS.Hochbaum,
editor,ApproximationAlgorithmsforNPHardProblems,pages399446.PWSPublishing
Company,1997.
[24]JavedA.Aslam.Asimpleboundontheexpectedheightofarandomlybuiltbinarysearch
rbol.TechnicalReportTR2001387,DartmouthCollegeDepartmentofComputerScience,
2001.
[25]MikhailJ.Atallah,editor.AlgorithmsandTheoryofComputationHandbook.CRCPress,
1999.
[26]G.Ausiello,P.Crescenzi,G.Gambosi,V.Kann,A.MarchettiSpaccamela,andM.Protasi.
ComplexityandApproximation:CombinatorialOptimizationProblemsandTheirApprox
imabilityProperties.Springer,1999.
[27]ShaiAvidanandArielShamir.Seamcarvingforcontentawareimageresizing.ACMTrans
actionsonGraphics,26(3),article10,2007.
[28]SaraBaaseandAlanVanGelder.ComputerAlgorithms:IntroductiontoDesignandAnal
ysis.AddisonWesley,thirdedition,2000.
[29]EricBach.Privatecommunication,1989.
[30]EricBach.Numbertheoreticalgorithms.InAnnualReviewofComputerScience,volume4,
pages119172.AnnualReviews,Inc.,1990.
[31]EricBachandJeffreyShallit.AlgorithmicNumberTheoryVolumeI:EfficientAlgorithms.
TheMITPress,1996.
[32]DavidH.Bailey,KingLee,andHorstD.Simon.UsingStrassen'salgorithmtoaccelerate
thesolutionoflinearsystems.TheJournalofSupercomputing,4(4):357371,1990.

https://translate.googleusercontent.com/translate_f 245/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,

Pgina354

Bibliografa 1233.

[33]SurenderBaswana,RameshHariharan,andSandeepSen.Improveddecrementalalgo
rithmsformaintainingtransitiveclosureandallpairsshortestpaths.JournalofAlgorithms,
62(2):7492,2007.
[34]R.Bayer.SymmetricbinaryBtrees:Datastructureandmaintenancealgorithms.Acta
Informatica,1(4):290306,1972.
[35]R.BayerandEMMcCreight.Organizationandmaintenanceoflargeorderedindexes.
ActaInformatica,1(3):173189,1972.
[36]PierreBeauchemin,GillesBrassard,ClaudeCrpeau,ClaudeGoutier,andCarlPomerance.
Thegenerationofrandomnumbersthatareprobablyprime.JournalofCryptology,1(1):53
64,1988.
[37]RichardBellman.DynamicProgramming.PrincetonUniversityPress,1957.
[38]RichardBellman.Onaroutingproblem.QuarterlyofAppliedMathematics,16(1):8790,
1958.
[39]MichaelBenOr.Lowerboundsforalgebraiccomputationtrees.EnActasdela
FifteenthAnnualACMSymposiumonTheoryofComputing,pages8086,1983.
[40]MichaelA.Bender,ErikD.Demaine,andMartinFarachColton.CacheobliviousBtrees.
InProceedingsofthe41stAnnualSymposiumonFoundationsofComputerScience,pages
399409,2000.
[41]SamuelW.BentandJohnW.John.Findingthemedianrequires2ncomparisons.enPro
ceedingsoftheSeventeenthAnnualACMSymposiumonTheoryofComputing,pages213
216,1985.
[42]JonL.Bentley.WritingEfficientPrograms.PrenticeHall,1982.
[43]JonL.Bentley.ProgrammingPearls.AddisonWesley,1986.
[44]JonL.Bentley,DorotheaHaken,andJamesB.Saxe.Ageneralmethodforsolvingdivide
andconquerrecurrences.SIGACTNews,12(3):3644,1980.
[45]DanielBienstockandBenjaminMcClosky.Tighteningsimplexmixedintegersetswith
guaranteedbounds.OptimizationOnline,July2008.
[46]PatrickBillingsley.ProbabilityandMeasure.JohnWiley&Sons,secondedition,1986.
[47]GuyE.Blelloch.ScanPrimitivesandParallelVectorModels.PhDthesis,Departmentof
ElectricalEngineeringandComputerScience,MIT,1989.AvailableasMITLaboratoryfor
ComputerScienceTechnicalReportMIT/LCS/TR463.
[48]GuyE.Blelloch.Programmingparallelalgorithms. CommunicationsoftheACM,
39(3):8597,1996.
[49]GuyE.Blelloch,PhillipB.Gibbons,andYossiMatias.Provablyefficientschedulingfor
languageswithfinegrainedparallelism.InProceedingsofthe7thAnnualACMSymposium
onParallelAlgorithmsandArchitectures,pages112,1995.
[50]ManuelBlum,RobertW.Floyd,VaughanPratt,RonaldL.Rivest,andRobertE.Tarjan.
Timeboundsforselection.JournalofComputerandSystemSciences,7(4):448461,1973.
[51]RobertD.Blumofe,ChristopherF.Joerg,BradleyC.Kuszmaul,CharlesE.Leiserson,
KeithH.Randall,andYuliZhou.Cilk:Anefficientmultithreadedruntimesystem.diario
ofParallelandDistributedComputing,37(1):5569,1996.

Pgina355

1234 Bibliografa

[52]RobertD.BlumofeandCharlesE.Leiserson.Schedulingmultithreadedcomputationsby
workstealing.JournaloftheACM,46(5):720748,1999.
[53]BlaBollobs.RandomGraphs.AcademicPress,1985.
[54]GillesBrassardandPaulBratley.FundamentalsofAlgorithmics.PrenticeHall,1996.
[55]RichardP.Brent.Theparallelevaluationofgeneralarithmeticexpressions.Journalofthe
ACM,21(2):201206,1974.
[56]RichardP.Brent.AnimprovedMonteCarlofactorizationalgorithm.BIT,20(2):176184,
1980.
[57]JPBuhler,HWLenstra,Jr.,andCarlPomerance.Factoringintegerswiththenumber
fieldsieve.InAKLenstraandHWLenstra,Jr.,editors,TheDevelopmentoftheNumber
FieldSieve,volume1554ofLectureNotesinMathematics,pages5094.Springer,1993.
[58]J.LawrenceCarterandMarkN.Wegman.Universalclassesofhashfunctions.Diariode
https://translate.googleusercontent.com/translate_f 246/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
ComputerandSystemSciences,18(2):143154,1979.
[59]BarbaraChapman,GabrieleJost,andRuudvanderPas.UsingOpenMP:PortableShared
MemoryParallelProgramming.TheMITPress,2007.
[60]BernardChazelle.AminimumspanningtreealgorithmwithinverseAckermanntypecom
plexity.JournaloftheACM,47(6):10281047,2000.
[61]JosephCheriyanandTorbenHagerup.Arandomizedmaximumflowalgorithm.SIAM
JournalonComputing,24(2):203226,1995.
[62]JosephCheriyanandSNMaheshwari.Analysisofpreflowpushalgorithmsformaximum
networkflow.SIAMJournalonComputing,18(6):10571086,1989.
[63]BorisV.CherkasskyandAndrewV.Goldberg.Onimplementingthepushrelabelmethod
forthemaximumflowproblem.Algorithmica,19(4):390410,1997.
[64]BorisV.Cherkassky,AndrewV.Goldberg,andTomaszRadzik.Shortestpathsalgorithms:
Theoryandexperimentalevaluation.MathematicalProgramming,73(2):129174,1996.
[65]BorisV.Cherkassky,AndrewV.Goldberg,andCraigSilverstein.Buckets,heaps,listsand
monotonepriorityqueues.SIAMJournalonComputing,28(4):13261346,1999.
[66]H.Chernoff.Ameasureofasymptoticefficiencyfortestsofahypothesisbasedonthesum
ofobservations.AnnalsofMathematicalStatistics,23(4):493507,1952.
[67]KaiLaiChung.ElementaryProbabilityTheorywithStochasticProcesses.Springer,1974.
[68]V.Chvtal.Agreedyheuristicforthesetcoveringproblem.MathematicsofOperations
Research,4(3):233235,1979.
[69]V.Chvtal.LinearProgramming.WHFreemanandCompany,1983.
[70]V.Chvtal,DAKlarner,andDEKnuth.Selectedcombinatorialresearchproblems.
TechnicalReportSTANCS72292,ComputerScienceDepartment,StanfordUniversity,
1972.
[71]CilkArts,Inc.,Burlington,Massachusetts.Cilk++Programmer'sGuide,2008.Available
athttp://www.cilk.com/archive/docs/cilk1guide.

Pgina356

Bibliografa 1235

[72]AlanCobham.Theintrinsiccomputationaldifficultyoffunctions.EnActasdela
1964CongressforLogic,Methodology,andthePhilosophyofScience,pages2430.North
Holland,1964.
[73]H.CohenandHWLenstra,Jr.PrimalitytestingandJacobisums.MathematicsofCom
putation,42(165):297330,1984.
[74]D.Comer.TheubiquitousBtree.ACMComputingSurveys,11(2):121137,1979.
[75]StephenCook.Thecomplexityoftheoremprovingprocedures.InProceedingsoftheThird
AnnualACMSymposiumonTheoryofComputing,pages151158,1971.
[76]JamesW.CooleyandJohnW.Tukey.Analgorithmforthemachinecalculationofcomplex
Fourierseries.MathematicsofComputation,19(90):297301,1965.
[77]DonCoppersmith.Modificationstothenumberfieldsieve.JournalofCryptology,
6(3):169180,1993.
[78]DonCoppersmithandShmuelWinograd.Matrixmultiplicationviaarithmeticprogression.
JournalofSymbolicComputation,9(3):251280,1990.
[79]ThomasH.Cormen,ThomasSundquist,andLeonardF.Wisniewski.Asymptoticallytight
boundsforperformingBMMCpermutationsonparalleldisksystems.SIAMJournalon
Computing,28(1):105136,1998.
[80]DonDaileyandCharlesE.Leiserson.UsingCilktowritemultiprocessorchessprograms.
InHJvandenHerikandB.Monien,editors,AdvancesinComputerGames,volume9,
pages2552.UniversityofMaastricht,Netherlands,2001.
[81]PaoloD'AlbertoandAlexandruNicolau.AdaptiveStrassen'smatrixmultiplication.En
Proceedingsofthe21stAnnualInternationalConferenceonSupercomputing,pages284
292,June2007.
[82]SanjoyDasgupta,ChristosPapadimitriou,andUmeshVazirani.Algorithms.McGrawHill,
2008.
[83]RomanDementiev,LutzKettner,JensMehnert,andPeterSanders.Engineeringasortedlist
datastructurefor32bitkeys.InProceedingsoftheSixthWorkshoponAlgorithmEngineer
ingandExperimentsandtheFirstWorkshoponAnalyticAlgorithmicsandCombinatorics,
pages142151,January2004.
[84]CamilDemetrescuandGiuseppeF.Italiano.Fullydynamicallpairsshortestpathswithreal
edgeweights.JournalofComputerandSystemSciences,72(5):813837,2006.
[85]EricV.DenardoandBennettL.Fox.Shortestroutemethods:1.Reaching,pruning,and
buckets.OperationsResearch,27(1):161186,1979.
[86]MartinDietzfelbinger,AnnaKarlin,KurtMehlhorn,FriedhelmMeyeraufderHeide,Hans
https://translate.googleusercontent.com/translate_f 247/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
Rohnert,andRobertE.Tarjan.Dynamicperfecthashing:Upperandlowerbounds.SIAM
JournalonComputing,23(4):738761,1994.
[87]WhitfieldDiffieandMartinE.Hellman.Newdirectionsincryptography.IEEETransac
tionsonInformationTheory,IT22(6):644654,1976.
[88]EWDijkstra.Anoteontwoproblemsinconnexionwithgraphs.NumerischeMathematik,
1(1):269271,1959.

Pgina357

1236 Bibliografa

[89]EADinic.Algorithmforsolutionofaproblemofmaximumflowinanetworkwithpower
Estimacion.SovietMathematicsDoklady,11(5):12771280,1970.
[90]BrandonDixon,MonikaRauch,andRobertE.Tarjan.Verificationandsensitivityanalysis
ofminimumspanningtreesinlineartime.SIAMJournalonComputing,21(6):11841192,
1992.
[91]JohnD.Dixon.Factorizationandprimalitytests.TheAmericanMathematicalMonthly,
91(6):333352,1984.
[92]DoritDor,JohanHstad,StaffanUlfberg,andUriZwick.Onlowerboundsforselecting
themedian.SIAMJournalonDiscreteMathematics,14(3):299311,2001.
[93]DoritDorandUriZwick.Selectingthemedian.SIAMJournalonComputing,28(5):1722
1758,1999.
[94]DoritDorandUriZwick.Medianselectionrequires.2C/ncomparisons.SIAMJournal
onDiscreteMathematics,14(3):312325,2001.
[95]AlvinW.Drake.FundamentalsofAppliedProbabilityTheory.McGrawHill,1967.
[96]JamesR.Driscoll,HaroldN.Gabow,RuthShrairman,andRobertE.Tarjan.Relaxedheaps:
AnalternativetoFibonacciheapswithapplicationstoparallelcomputation.Communica
tionsoftheACM,31(11):13431354,1988.
[97]JamesR.Driscoll,NeilSarnak,DanielD.Sleator,andRobertE.Tarjan.Makingdata
structurespersistent.JournalofComputerandSystemSciences,38(1):86124,1989.
[98]DerekL.Eager,JohnZahorjan,andEdwardD.Lazowska.Speedupversusefficiencyin
parallelsystems.IEEETransactionsonComputers,38(3):408423,1989.
[99]HerbertEdelsbrunner.AlgorithmsinCombinatorialGeometry,volume10ofEATCSMono
graphsonTheoreticalComputerScience.Springer,1987.
[100]JackEdmonds.Paths,trees,andflowers.CanadianJournalofMathematics,17:449467,
1965.
[101]JackEdmonds.Matroidsandthegreedyalgorithm.MathematicalProgramming,1(1):127
136,1971.
[102]JackEdmondsandRichardM.Karp.Theoreticalimprovementsinthealgorithmiceffi
ciencyfornetworkflowproblems.JournaloftheACM,19(2):248264,1972.
[103]ShimonEven.GraphAlgorithms.ComputerSciencePress,1979.
[104]WilliamFeller.AnIntroductiontoProbabilityTheoryandItsApplications.JohnWiley&
Sons,thirdedition,1968.
[105]RobertW.Floyd.Algorithm97(SHORTESTPATH).CommunicationsoftheACM,
5(6):345,1962.
[106]RobertW.Floyd.Algorithm245(TREESORT).CommunicationsoftheACM,7(12):701,
1964.
[107]RobertW.Floyd.Permutinginformationinidealizedtwolevelstorage.InRaymondE.
MillerandJamesW.Thatcher,editors,ComplexityofComputerComputations,pages105
109.PlenumPress,1972.

Pgina358

Bibliografa 1237

[108]RobertW.FloydandRonaldL.Rivest.Expectedtimeboundsforselection.Communica
tionsoftheACM,18(3):165172,1975.
[109]LestorR.Ford,Jr.andDRFulkerson.FlowsinNetworks.PrincetonUniversityPress,
1962.
[110]LestorR.Ford,Jr.andSelmerM.Johnson.Atournamentproblem.TheAmericanMathe
maticalMonthly,66(5):387389,1959.
https://translate.googleusercontent.com/translate_f 248/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,

[111]MichaelL.Fredman.Newboundsonthecomplexityoftheshortestpathproblem.SIAM
JournalonComputing,5(1):8389,1976.
[112]MichaelL.Fredman,JnosKomls,andEndreSzemerdi.StoringasparsetablewithO.1/
worstcaseaccesstime.JournaloftheACM,31(3):538544,1984.
[113]MichaelL.FredmanandMichaelE.Saks.Thecellprobecomplexityofdynamicdatastruc
tures.InProceedingsoftheTwentyFirstAnnualACMSymposiumonTheoryofComputing,
pages345354,1989.
[114]MichaelL.FredmanandRobertE.Tarjan.Fibonacciheapsandtheirusesinimproved
networkoptimizationalgorithms.JournaloftheACM,34(3):596615,1987.
[115]MichaelL.FredmanandDanE.Willard.Surpassingtheinformationtheoreticboundwith
fusiontrees.JournalofComputerandSystemSciences,47(3):424436,1993.
[116]MichaelL.FredmanandDanE.Willard.Transdichotomousalgorithmsforminimumspan
ningtreesandshortestpaths.JournalofComputerandSystemSciences,48(3):533551,
1994.
[117]MatteoFrigoandStevenG.Johnson.ThedesignandimplementationofFFTW3.Proceed
ingsoftheIEEE,93(2):216231,2005.
[118]MatteoFrigo,CharlesE.Leiserson,andKeithH.Randall.TheimplementationoftheCilk5
multithreadedlanguage.InProceedingsofthe1998ACMSIGPLANConferenceonPro
grammingLanguageDesignandImplementation,pages212223,1998.
[119]HaroldN.Gabow.Pathbaseddepthfirstsearchforstrongandbiconnectedcomponents.
InformationProcessingLetters,74(34):107114,2000.
[120]HaroldN.Gabow,Z.Galil,T.Spencer,andRobertE.Tarjan.Efficientalgorithmsforfind
ingminimumspanningtreesinundirectedanddirectedgraphs.Combinatorica,6(2):109
122,1986.
[121]HaroldN.GabowandRobertE.Tarjan.Alineartimealgorithmforaspecialcaseofdisjoint
setunion.JournalofComputerandSystemSciences,30(2):209221,1985.
[122]HaroldN.GabowandRobertE.Tarjan.Fasterscalingalgorithmsfornetworkproblems.
SIAMJournalonComputing,18(5):10131036,1989.
[123]ZviGalilandOdedMargalit.Allpairsshortestdistancesforgraphswithsmallinteger
lengthedges.InformationandComputation,134(2):103139,1997.
[124]ZviGalilandOdedMargalit.Allpairsshortestpathsforgraphswithsmallintegerlength
edges.JournalofComputerandSystemSciences,54(2):243254,1997.
[125]ZviGalilandKunsooPark.Dynamicprogrammingwithconvexity,concavityandsparsity.
TheoreticalComputerScience,92(1):4976,1992.

Pgina359

1238 Bibliografa

[126]ZviGalilandJoelSeiferas.Timespaceoptimalstringmatching.JournalofComputerand
SystemSciences,26(3):280294,1983.
[127]IgalGalperinandRonaldL.Rivest.Scapegoattrees.InProceedingsofthe4thACMSIAM
SymposiumonDiscreteAlgorithms,pages165174,1993.
[128]MichaelR.Garey,RLGraham,andJDUllman.Worstcaseanalyisofmemoryal
locationalgorithms.InProceedingsoftheFourthAnnualACMSymposiumonTheoryof
Computing,pages143150,1972.
[129]MichaelR.GareyandDavidS.Johnson.ComputersandIntractability:AGuidetothe
TheoryofNPCompleteness.WHFreeman,1979.
[130]SaulGass.LinearProgramming:MethodsandApplications.InternationalThomsonPub
lishing,fourthedition,1975.
[131]FanicaGavril.Algorithmsforminimumcoloring,maximumclique,minimumcoveringby
cliques,andmaximumindependentsetofachordalgraph.SIAMJournalonComputing,
1(2):180187,1972.
[132]AlanGeorgeandJosephWHLiu.ComputerSolutionofLargeSparsePositiveDefinite
Sistemas.PrenticeHall,1981.
[133]ENGilbertandEFMoore.Variablelengthbinaryencodings.BellSystemTechnical
Journal,38(4):933967,1959.
[134]MichelX.GoemansandDavidP.Williamson.Improvedapproximationalgorithmsfor
maximumcutandsatisfiabilityproblemsusingsemidefiniteprogramming.Journalofthe
ACM,42(6):11151145,1995.
[135]MichelX.GoemansandDavidP.Williamson.Theprimaldualmethodforapproximation
algorithmsanditsapplicationtonetworkdesignproblems.InDoritS.Hochbaum,editor,
ApproximationAlgorithmsforNPHardProblems,pages144191.PWSPublishingCom
pany,1997.
[136]AndrewV.Goldberg.EfficientGraphAlgorithmsforSequentialandParallelComputers.
PhDthesis,DepartmentofElectricalEngineeringandComputerScience,MIT,1987.

https://translate.googleusercontent.com/translate_f 249/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
[137]AndrewV.Goldberg.Scalingalgorithmsfortheshortestpathsproblem.SIAMJournalon
Computing,24(3):494504,1995.
[138]AndrewV.GoldbergandSatishRao.Beyondtheflowdecompositionbarrier.Diariode
theACM,45(5):783797,1998.
[139]AndrewV.Goldberg,vaTardos,andRobertE.Tarjan.Networkflowalgorithms.InBern
hardKorte,LszlLovsz,HansJrgenPrmel,andAlexanderSchrijver,editors,Paths,
Flows,andVLSILayout,pages101164.Springer,1990.
[140]AndrewV.GoldbergandRobertE.Tarjan.Anewapproachtothemaximumflowproblem.
JournaloftheACM,35(4):921940,1988.
[141]D.GoldfarbandMJTodd.Linearprogramming.InGLNemhauser,AHGRinnooy
Kan,andMJTodd,editors,HandbookinOperationsResearchandManagementScience,
Vol.1,Optimization,pages73170.ElsevierSciencePublishers,1989.
[142]ShafiGoldwasserandSilvioMicali.Probabilisticencryption.JournalofComputerand
SystemSciences,28(2):270299,1984.

Pgina360

Bibliografa 1239

[143]ShafiGoldwasser,SilvioMicali,andRonaldL.Rivest.Adigitalsignatureschemesecure
againstadaptivechosenmessageattacks.SIAMJournalonComputing,17(2):281308,
1988.
[144]GeneH.GolubandCharlesF.VanLoan.MatrixComputations.TheJohnsHopkinsUni
versityPress,thirdedition,1996.
[145]GHGonnet.HandbookofAlgorithmsandDataStructures.AddisonWesley,1984.
[146]RafaelC.GonzalezandRichardE.Woods.ProcesamientoDigitaldeImgenes.AddisonWesley,
1992.
[147]MichaelT.GoodrichandRobertoTamassia.DataStructuresandAlgorithmsinJava.John
Wiley&Sons,1998.
[148]MichaelT.GoodrichandRobertoTamassia.AlgorithmDesign:Foundations,Analysis,and
InternetExamples.JohnWiley&Sons,2001.
[149]RonaldL.Graham.Boundsforcertainmultiprocessoranomalies.BellSystemTechnical
Journal,45(9):15631581,1966.
[150]RonaldL.Graham.Anefficientalgorithmfordeterminingtheconvexhullofafiniteplanar
conjunto.InformationProcessingLetters,1(4):132133,1972.
[151]RonaldL.GrahamandPavolHell.Onthehistoryoftheminimumspanningtreeproblem.
AnnalsoftheHistoryofComputing,7(1):4357,1985.
[152]RonaldL.Graham,DonaldE.Knuth,andOrenPatashnik. ConcreteMathematics.
AddisonWesley,secondedition,1994.
[153]DavidGries.TheScienceofProgramming.Springer,1981.
[154]M.Grtschel,LszlLovsz,andAlexanderSchrijver.GeometricAlgorithmsandCombi
natorialOptimization.Springer,1988.
[155]LeoJ.GuibasandRobertSedgewick.Adichromaticframeworkforbalancedtrees.En
Proceedingsofthe19thAnnualSymposiumonFoundationsofComputerScience,pages
821,1978.
[156]DanGusfield.AlgorithmsonStrings,Trees,andSequences:ComputerScienceandCom
putationalBiology.CambridgeUniversityPress,1997.
[157]H.HalberstamandREIngram,editors.TheMathematicalPapersofSirWilliamRowan
Hamilton,volumeIII(Algebra).CambridgeUniversityPress,1967.
[158]YijieHan.Improvedfastintegersortinginlinearspace.InProceedingsofthe12thACM
SIAMSymposiumonDiscreteAlgorithms,pages793796,2001.
[159]YijieHan.AnOn3.loglogn=logn/5=4/timealgorithmforallpairsshortestpath.Algo
rithmica,51(4):428434,2008.
[160]FrankHarary.GraphTheory.AddisonWesley,1969.
[161]GregoryC.HarfstandEdwardM.Reingold.Apotentialbasedamortizedanalysisofthe
unionfinddatastructure.SIGACTNews,31(3):8695,2000.
[162]J.HartmanisandREStearns.Onthecomputationalcomplexityofalgorithms.Transac
tionsoftheAmericanMathematicalSociety,117:285306,May1965.

Pgina361

1240 Bibliografa

https://translate.googleusercontent.com/translate_f 250/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,

[163]MichaelT.Heideman,DonH.Johnson,andC.SidneyBurrus.Gaussandthehistoryofthe
FastFourierTransform.IEEEASSPMagazine,1(4):1421,1984.
[164]MonikaR.HenzingerandValerieKing.Fullydynamicbiconnectivityandtransitiveclo
sure.InProceedingsofthe36thAnnualSymposiumonFoundationsofComputerScience,
pages664672,1995.
[165]MonikaR.HenzingerandValerieKing.Randomizedfullydynamicgraphalgorithmswith
polylogarithmictimeperoperation.JournaloftheACM,46(4):502516,1999.
[166]MonikaR.Henzinger,SatishRao,andHaroldN.Gabow.Computingvertexconnectivity:
Newboundsfromoldtechniques.JournalofAlgorithms,34(2):222250,2000.
[167]NicholasJ.Higham.Exploitingfastmatrixmultiplicationwithinthelevel3BLAS.ACM
TransactionsonMathematicalSoftware,16(4):352368,1990.
[168]W.DanielHillisandJr.GuyL.Steele.Dataparallelalgorithms.Communicationsofthe
ACM,29(12):11701183,1986.
[169]CARHoare.Algorithm63(PARTITION)andalgorithm65(FIND).Comunicaciones
oftheACM,4(7):321322,1961.
[170]CARHoare.Quicksort.ComputerJournal,5(1):1015,1962.
[171]DoritS.Hochbaum.Efficientboundsforthestableset,vertexcoverandsetpackingprob
ProblemasDiscreteAppliedMathematics,6(3):243254,1983.
[172]DoritS.Hochbaum,editor.ApproximationAlgorithmsforNPHardProblems.PWSPub
lishingCompany,1997.
[173]W.Hoeffding.Onthedistributionofthenumberofsuccessesinindependenttrials.Annals
ofMathematicalStatistics,27(3):713721,1956.
[174]MichaHofri.ProbabilisticAnalysisofAlgorithms.Springer,1987.
[175]MichaHofri.AnalysisofAlgorithms.OxfordUniversityPress,1995.
[176]JohnE.HopcroftandRichardM.Karp.Ann5=2algorithmformaximummatchingsin
bipartitegraphs.SIAMJournalonComputing,2(4):225231,1973.
[177]JohnE.Hopcroft,RajeevMotwani,andJeffreyD.Ullman.IntroductiontoAutomataThe
ory,Languages,andComputation.AddisonWesley,thirdedition,2006.
[178]JohnE.HopcroftandRobertE.Tarjan.Efficientalgorithmsforgraphmanipulation.Com
municationsoftheACM,16(6):372378,1973.
[179]JohnE.HopcroftandJeffreyD.Ullman.Setmergingalgorithms.SIAMJournalonCom
puting,2(4):294303,1973.
[180]JohnE.HopcroftandJeffreyD.Ullman.IntroductiontoAutomataTheory,Languages,and
Computation.AddisonWesley,1979.
[181]EllisHorowitz,SartajSahni,andSanguthevarRajasekaran.ComputerAlgorithms.Com
puterSciencePress,1998.
[182]TCHuandMTShing.Computationofmatrixchainproducts.PartI.SIAMJournalon
Computing,11(2):362373,1982.
[183]TCHuandMTShing.Computationofmatrixchainproducts.ParteII.SIAMJournalon
Computing,13(2):228251,1984.

Pgina362

Bibliografa 1241

[184]TCHuandACTucker.Optimalcomputersearchtreesandvariablelengthalphabetic
Cdigos.SIAMJournalonAppliedMathematics,21(4):514532,1971.
[185]DavidA.Huffman.Amethodfortheconstructionofminimumredundancycodes.Pro
ceedingsoftheIRE,40(9):10981101,1952.
[186]StevenHussLederman,ElaineM.Jacobson,JeremyR.Johnson,AnnaTsao,andThomas
Turnbull.ImplementationofStrassen'salgorithmformatrixmultiplication.InProceedings
ofthe1996ACM/IEEEConferenceonSupercomputing,article32,1996.
[187]OscarH.IbarraandChulE.Kim.Fastapproximationalgorithmsfortheknapsackandsum
ofsubsetproblems.JournaloftheACM,22(4):463468,1975.
[188]EJIsaacandRCSingleton.Sortingbyaddresscalculation.JournaloftheACM,
3(3):169174,1956.
[189]RAJarvis.Ontheidentificationoftheconvexhullofafinitesetofpointsintheplane.
InformationProcessingLetters,2(1):1821,1973.
[190]DavidS.Johnson.Approximationalgorithmsforcombinatorialproblems.JournalofCom
puterandSystemSciences,9(3):256278,1974.
[191]DavidS.Johnson.TheNPcompletenesscolumn:AnongoingguideThetaleofthesec
ondprover.JournalofAlgorithms,13(3):502524,1992.
[192]DonaldB.Johnson.Efficientalgorithmsforshortestpathsinsparsenetworks.Diariode
theACM,24(1):113,1977.
[193]RichardJohnsonbaughandMarcusSchaefer.Algorithms.PearsonPrenticeHall,2004.

https://translate.googleusercontent.com/translate_f 251/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
[194]A.KaratsubaandYu.Ofman.Multiplicationofmultidigitnumbersonautomata.Soviet
PhysicsDoklady,7(7):595596,1963.TranslationofanarticleinDokladyAkademiiNauk
SSSR,145(2),1962.
[195]DavidR.Karger,PhilipN.Klein,andRobertE.Tarjan.Arandomizedlineartimealgorithm
tofindminimumspanningtrees.JournaloftheACM,42(2):321328,1995.
[196]DavidR.Karger,DaphneKoller,andStevenJ.Phillips.Findingthehiddenpath:Time
boundsforallpairsshortestpaths.SIAMJournalonComputing,22(6):11991217,1993.
[197]HowardKarloff.LinearProgramming.Birkhuser,1991.
[198]N.Karmarkar.Anewpolynomialtimealgorithmforlinearprogramming.Combinatorica,
4(4):373395,1984.
[199]RichardM.Karp.Reducibilityamongcombinatorialproblems.InRaymondE.Millerand
JamesW.Thatcher,editors,ComplexityofComputerComputations,pages85103.Plenum
Press,1972.
[200]RichardM.Karp.Anintroductiontorandomizedalgorithms.DiscreteAppliedMathemat
ics,34(13):165201,1991.
[201]RichardM.KarpandMichaelO.Rabin.Efficientrandomizedpatternmatchingalgorithms.
IBMJournalofResearchandDevelopment,31(2):249260,1987.
[202]AVKarzanov.Determiningthemaximalflowinanetworkbythemethodofpreflows.
SovietMathematicsDoklady,15(2):434437,1974.

Pgina363

1242 Bibliografa

[203]ValerieKing.Asimplerminimumspanningtreeverificationalgorithm.Algorithmica,
18(2):263270,1997.
[204]ValerieKing,SatishRao,andRobertE.Tarjan.Afasterdeterministicmaximumflowalgo
rithm.JournalofAlgorithms,17(3):447474,1994.
[205]JeffreyH.Kingston.AlgorithmsandDataStructures:Design,Correctness,Analysis.
AddisonWesley,secondedition,1997.
[206]DGKirkpatrickandR.Seidel.Theultimateplanarconvexhullalgorithm?SIAMJournal
onComputing,15(2):287299,1986.
[207]PhilipN.KleinandNealE.Young.ApproximationalgorithmsforNPhardoptimization
problemas.InCRCHandbookonAlgorithms,pages3413419.CRCPress,1999.
[208]JonKleinbergandvaTardos.AlgorithmDesign.AddisonWesley,2006.
[209]DonaldE.Knuth.FundamentalAlgorithms,volume1ofTheArtofComputerProgram
ming.AddisonWesley,1968.Thirdedition,1997.
[210]DonaldE.Knuth.SeminumericalAlgorithms,volume2ofTheArtofComputerProgram
ming.AddisonWesley,1969.Thirdedition,1997.
[211]DonaldE.Knuth.SortingandSearching,volume3ofTheArtofComputerProgramming.
AddisonWesley,1973.Secondedition,1998.
[212]DonaldE.Knuth.Optimumbinarysearchtrees.ActaInformatica,1(1):1425,1971.
[213]DonaldE.Knuth.Bigomicronandbigomegaandbigtheta.SIGACTNews,8(2):1823,
1976.
[214]DonaldE.Knuth,JamesH.Morris,Jr.,andVaughanR.Pratt.Fastpatternmatchingin
instrumentosdecuerda.SIAMJournalonComputing,6(2):323350,1977.
[215]J.Komls.Linearverificationforspanningtrees.Combinatorica,5(1):5765,1985.
[216]BernhardKorteandLszlLovsz.Mathematicalstructuresunderlyinggreedyalgorithms.
InF.Gecseg,editor,FundamentalsofComputationTheory,volume117ofLectureNotesin
ComputerScience,pages205209.Springer,1981.
[217]BernhardKorteandLszlLovsz.Structuralpropertiesofgreedoids.Combinatorica,
3(34):359374,1983.
[218]BernhardKorteandLszlLovsz.GreedoidsAstructuralframeworkforthegreedy
algoritmo.InW.Pulleybank,editor,ProgressinCombinatorialOptimization,pages221
243.AcademicPress,1984.
[219]BernhardKorteandLszlLovsz.Greedoidsandlinearobjectivefunctions.SIAMJournal
onAlgebraicandDiscreteMethods,5(2):229238,1984.
[220]DexterC.Kozen.TheDesignandAnalysisofAlgorithms.Springer,1992.
[221]DavidW.Krumme,GeorgeCybenko,andKNVenkataraman.Gossipinginminimaltime.
SIAMJournalonComputing,21(1):111139,1992.
[222]JosephB.Kruskal,Jr.Ontheshortestspanningsubtreeofagraphandthetravelingsalesman
problema.ProceedingsoftheAmericanMathematicalSociety,7(1):4850,1956.
[223]LeslieLamport.Howtomakeamultiprocessorcomputerthatcorrectlyexecutesmultipro
cessprograms.IEEETransactionsonComputers,C28(9):690691,1979.

https://translate.googleusercontent.com/translate_f 252/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,

Pgina364

Bibliografa 1243

[224]EugeneL.Lawler.CombinatorialOptimization:NetworksandMatroids.Holt,Rinehart,
andWinston,1976.
[225]EugeneL.Lawler,JKLenstra,AHGRinnooyKan,andDBShmoys,editors.los
TravelingSalesmanProblem.JohnWiley&Sons,1985.
[226]CYLee.Analgorithmforpathconnectionanditsapplications.IRETransactionson
ElectronicComputers,EC10(3):346365,1961.
[227]TomLeighton.Tightboundsonthecomplexityofparallelsorting.TransaccionesIEEEen
Computers,C34(4):344354,1985.
[228]TomLeighton.Notesonbettermastertheoremsfordivideandconquerrecurrences.Class
notas.Availableathttp://citeseer.ist.psu.edu/252350.html,October1996.
[229]TomLeightonandSatishRao.Multicommoditymaxflowmincuttheoremsandtheiruse
indesigningapproximationalgorithms.JournaloftheACM,46(6):787832,1999.
[230]DaanLeijenandJuddHall.Optimizemanagedcodeformulticoremachines.MSDN
Magazine,October2007.
[231]DebraA.LelewerandDanielS.Hirschberg.Datacompression.ACMComputingSurveys,
19(3):261296,1987.
[232]AKLenstra,HWLenstra,Jr.,MSManasse,andJMPollard.Thenumberfieldsieve.
InAKLenstraandHWLenstra,Jr.,editors,TheDevelopmentoftheNumberFieldSieve,
volume1554ofLectureNotesinMathematics,pages1142.Springer,1993.
[233]HWLenstra,Jr.Factoringintegerswithellipticcurves.AnnalsofMathematics,
126(3):649673,1987.
[234]LALevin.Universalsortingproblems.ProblemyPeredachiInformatsii,9(3):265266,
1973.InRussian.
[235]AnanyLevitin.IntroductiontotheDesign&AnalysisofAlgorithms.AddisonWesley,
2007.
[236]HarryR.LewisandChristosH.Papadimitriou.ElementsoftheTheoryofComputation.
PrenticeHall,secondedition,1998.
[237]CLLiu.IntroductiontoCombinatorialMathematics.McGrawHill,1968.
[238]LszlLovsz.Ontheratioofoptimalintegralandfractionalcovers.DiscreteMathemat
ics,13(4):383390,1975.
[239]LszlLovszandMDPlummer.MatchingTheory,volume121ofAnnalsofDiscrete
Mathematics.NorthHolland,1986.
[240]BruceM.MaggsandSergeA.Plotkin.Minimumcostspanningtreeasapathfinding
problema.InformationProcessingLetters,26(6):291293,1988.
[241]MichaelMain.DataStructuresandOtherObjectsUsingJava.AddisonWesley,1999.
[242]UdiManber.IntroductiontoAlgorithms:ACreativeApproach.AddisonWesley,1989.
[243]ConradoMartnezandSalvadorRoura.Randomizedbinarysearchtrees.Journalofthe
ACM,45(2):288323,1998.
[244]WilliamJ.MasekandMichaelS.Paterson.Afasteralgorithmcomputingstringeditdis
tances.JournalofComputerandSystemSciences,20(1):1831,1980.

Pgina365

1244 Bibliografa

[245]HAMaurer,Th.Ottmann,andH.W.Seis.Implementingdictionariesusingbinarytreesof
verysmallheight.InformationProcessingLetters,5(1):1114,1976.
[246]ErnstW.Mayr,HansJrgenPrmel,andAngelikaSteger,editors.LecturesonProofVerifi
cationandApproximationAlgorithms,volume1367ofLectureNotesinComputerScience.
Springer,1998.
[247]CCMcGeoch.Allpairsshortestpathsandtheessentialsubgraph.Algorithmica,
13(5):426441,1995.
[248]MDMcIlroy.Akilleradversaryforquicksort.SoftwarePracticeandExperience,
29(4):341344,1999.
[249]KurtMehlhorn.SortingandSearching,volume1ofDataStructuresandAlgorithms.
Springer,1984.
[250]KurtMehlhorn.GraphAlgorithmsandNPCompleteness,volume2ofDataStructuresand
Algorithms.Springer,1984.
[251]KurtMehlhorn.MultidimensionalSearchingandComputationalGeometry,volume3of
https://translate.googleusercontent.com/translate_f 253/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
DataStructuresandAlgorithms.Springer,1984.
[252]KurtMehlhornandStefanNher.BoundedordereddictionariesinO.loglogN/timeand
On/space.InformationProcessingLetters,35(4):183189,1990.
[253]KurtMehlhornandStefanNher.LEDA:APlatformforCombinatorialandGeometric
Computing.CambridgeUniversityPress,1999.
[254]AlfredJ.Menezes,PaulC.vanOorschot,andScottA.Vanstone.HandbookofApplied
Cryptography.CRCPress,1997.
[255]GaryL.Miller.Riemann'shypothesisandtestsforprimality.JournalofComputerand
SystemSciences,13(3):300317,1976.
[256]JohnC.Mitchell.FoundationsforProgrammingLanguages.TheMITPress,1996.
[257]JosephSBMitchell.Guillotinesubdivisionsapproximatepolygonalsubdivisions:Asim
plepolynomialtimeapproximationschemeforgeometricTSP,kMST,andrelatedprob
ProblemasSIAMJournalonComputing,28(4):12981309,1999.
[258]LouisMonier.AlgorithmesdeFactorisationD'Entiers.PhDthesis,L'UniversitParisSud,
1980.
[259]LouisMonier.Evaluationandcomparisonoftwoefficientprobabilisticprimalitytesting
AlgoritmosTheoreticalComputerScience,12(1):97108,1980.
[260]EdwardF.Moore.Theshortestpaththroughamaze.InProceedingsoftheInternational
SymposiumontheTheoryofSwitching,pages285292.HarvardUniversityPress,1959.
[261]RajeevMotwani,Joseph(Seffi)Naor,andPrabakharRaghavan.Randomizedapproxima
tionalgorithmsincombinatorialoptimization.InDoritHochbaum,editor,Approximation
AlgorithmsforNPHardProblems,chapter11,pages447481.PWSPublishingCompany,
1997.
[262]RajeevMotwaniandPrabhakarRaghavan.RandomizedAlgorithms.UniversidaddeCambridge
Press,1995.
[263]JIMunroandV.Raman.FaststableinplacesortingwithOn/datamoves.Algorithmica,
16(2):151160,1996.

Pgina366

Bibliografa 1245

[264]J.NievergeltandEMReingold.Binarysearchtreesofboundedbalance.SIAMJournal
onComputing,2(1):3343,1973.
[265]IvanNivenandHerbertS.Zuckerman.AnIntroductiontotheTheoryofNumbers.John
Wiley&Sons,fourthedition,1980.
[266]AlanV.OppenheimandRonaldW.Schafer,withJohnR.Buck.DiscreteTimeSignal
Processing.PrenticeHall,secondedition,1998.
[267]AlanV.OppenheimandAlanS.Willsky,withS.HamidNawab.SignalsandSystems.
PrenticeHall,secondedition,1997.
[268]JamesB.Orlin.Apolynomialtimeprimalnetworksimplexalgorithmforminimumcost
flows.MathematicalProgramming,78(1):109129,1997.
[269]JosephO'Rourke.ComputationalGeometryinC.CambridgeUniversityPress,second
edition,1998.
[270]ChristosH.Papadimitriou.ComputationalComplexity.AddisonWesley,1994.
[271]ChristosH.PapadimitriouandKennethSteiglitz.CombinatorialOptimization:Algorithms
andComplexity.PrenticeHall,1982.
[272]MichaelS.Paterson.Progressinselection.InProceedingsoftheFifthScandinavianWork
shoponAlgorithmTheory,pages368379,1996.
[273]MihaiPatrascuandMikkelThorup.Timespacetradeoffsforpredecessorsearch.enPro
ceedingsofthe38thAnnualACMSymposiumonTheoryofComputing,pages232240,
2006.
[274]MihaiPatrascuandMikkelThorup.Randomizationdoesnothelpsearchingpredecessors.
InProceedingsofthe18thACMSIAMSymposiumonDiscreteAlgorithms,pages555564,
2007.
[275]PavelA.Pevzner.ComputationalMolecularBiology:AnAlgorithmicApproach.TheMIT
Press,2000.
[276]StevenPhillipsandJefferyWestbrook.Onlineloadbalancingandnetworkflow.enPro
ceedingsofthe25thAnnualACMSymposiumonTheoryofComputing,pages402411,
1993.
[277]JMPollard.AMonteCarlomethodforfactorization.BIT,15(3):331334,1975.
[278]JMPollard.Factoringwithcubicintegers.InAKLenstraandHWLenstra,Jr.,editors,
TheDevelopmentoftheNumberFieldSieve,volume1554ofLectureNotesinMathematics,
pages410.Springer,1993.
[279]CarlPomerance.Onthedistributionofpseudoprimes.MathematicsofComputation,
37(156):587593,1981.
[280]CarlPomerance,editor.ProceedingsoftheAMSSymposiainAppliedMathematics:Com
https://translate.googleusercontent.com/translate_f 254/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
putationalNumberTheoryandCryptography.AmericanMathematicalSociety,1990.
[281]WilliamK.Pratt.ProcesamientoDigitaldeImgenes.JohnWiley&Sons,fourthedition,2007.
[282]FrancoP.PreparataandMichaelIanShamos.ComputationalGeometry:AnIntroduction.
Springer,1985.

Pgina367

1246 Bibliografa

[283]WilliamH.Press,SaulA.Teukolsky,WilliamT.Vetterling,andBrianP.Flannery.Numer
icalRecipesinC++:TheArtofScientificComputing.CambridgeUniversityPress,second
edition,2002.
[284]WilliamH.Press,SaulA.Teukolsky,WilliamT.Vetterling,andBrianP.Flannery.Numer
icalRecipes:TheArtofScientificComputing.CambridgeUniversityPress,thirdedition,
2007.
[285]RCPrim.Shortestconnectionnetworksandsomegeneralizations.BellSystemTechnical
Journal,36(6):13891401,1957.
[286]WilliamPugh.Skiplists:Aprobabilisticalternativetobalancedtrees.Communicationsof
theACM,33(6):668676,1990.
[287]PaulW.Purdom,Jr.andCynthiaA.Brown.TheAnalysisofAlgorithms.Holt,Rinehart,
andWinston,1985.
[288]MichaelO.Rabin.Probabilisticalgorithms.InJFTraub,editor,AlgorithmsandCom
plexity:NewDirectionsandRecentResults,pages2139.AcademicPress,1976.
[289]MichaelO.Rabin.Probabilisticalgorithmfortestingprimality.JournalofNumberTheory,
12(1):128138,1980.
[290]P.RaghavanandCDThompson.Randomizedrounding:Atechniqueforprovablygood
algorithmsandalgorithmicproofs.Combinatorica,7(4):365374,1987.
[291]RajeevRaman.Recentresultsonthesinglesourceshortestpathsproblem.SIGACTNews,
28(2):8187,1997.
[292]JamesReinders.IntelThreadingBuildingBlocks:OutfittingC++forMulticoreProcessor
Parallelism.O'ReillyMedia,Inc.,2007.
[293]EdwardM.Reingold,JrgNievergelt,andNarsinghDeo.CombinatorialAlgorithms:The
oryandPractice.PrenticeHall,1977.
[294]EdwardM.Reingold,KennethJ.Urban,andDavidGries.KMPstringmatchingrevisited.
InformationProcessingLetters,64(5):217223,1997.
[295]HansRiesel.PrimeNumbersandComputerMethodsforFactorization,volume126of
ProgressinMathematics.Birkhuser,secondedition,1994.
[296]RonaldL.Rivest,AdiShamir,andLeonardM.Adleman.Amethodforobtainingdigital
signaturesandpublickeycryptosystems.CommunicationsoftheACM,21(2):120126,
1978.SeealsoUSPatent4,405,829.
[297]HerbertRobbins.AremarkonStirling'sformula.AmericanMathematicalMonthly,
62(1):2629,1955.
[298]DJRosenkrantz,REStearns,andPMLewis.Ananalysisofseveralheuristicsforthe
travelingsalesmanproblem.SIAMJournalonComputing,6(3):563581,1977.
[299]SalvadorRoura.Animprovedmastertheoremfordivideandconquerrecurrences.En
ProceedingsofAutomata,LanguagesandProgramming,24thInternationalColloquium,
ICALP'97,volume1256ofLectureNotesinComputerScience,pages449459.Springer,
1997.
[300]YARozanov.ProbabilityTheory:AConciseCourse.Dover,1969.

Pgina368

Bibliografa 1247

[301]S.SahniandT.Gonzalez.Pcompleteapproximationproblems.JournaloftheACM,
23(3):555565,1976.
[302]A.Schnhage,M.Paterson,andN.Pippenger.Findingthemedian.JournalofComputer
andSystemSciences,13(2):184199,1976.
[303]AlexanderSchrijver.TheoryofLinearandIntegerProgramming.JohnWiley&Sons,
1986.
[304]AlexanderSchrijver.PathsandflowsAhistoricalsurvey.CWIQuarterly,6(3):169183,
https://translate.googleusercontent.com/translate_f 255/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
1993.
[305]RobertSedgewick.Implementingquicksortprograms.CommunicationsoftheACM,
21(10):847857,1978.
[306]RobertSedgewick.Algorithms.AddisonWesley,secondedition,1988.
[307]RobertSedgewickandPhilippeFlajolet.AnIntroductiontotheAnalysisofAlgorithms.
AddisonWesley,1996.
[308]RaimundSeidel.Ontheallpairsshortestpathprobleminunweightedundirectedgraphs.
JournalofComputerandSystemSciences,51(3):400403,1995.
[309]RaimundSeidelandCRAragon.Randomizedsearchtrees.Algorithmica,16(45):464
497,1996.
[310]JoaoSetubalandJoaoMeidanis.IntroductiontoComputationalMolecularBiology.PWS
PublishingCompany,1997.
[311]CliffordA.Shaffer.APracticalIntroductiontoDataStructuresandAlgorithmAnalysis.
PrenticeHall,secondedition,2001.
[312]JeffreyShallit.OriginsoftheanalysisoftheEuclideanalgorithm.HistoriaMathematica,
21(4):401419,1994.
[313]MichaelI.ShamosandDanHoey.Geometricintersectionproblems.EnActasdela
17thAnnualSymposiumonFoundationsofComputerScience,pages208215,1976.
[314]M.Sharir.Astrongconnectivityalgorithmanditsapplicationsindataflowanalysis.Com
putersandMathematicswithApplications,7(1):6772,1981.
[315]DavidB.Shmoys.Computingnearoptimalsolutionstocombinatorialoptimizationprob
ProblemasInWilliamCook,LszlLovsz,andPaulSeymour,editors,CombinatorialOpti
mization,volume20ofDIMACSSeriesinDiscreteMathematicsandTheoreticalComputer
Ciencia.AmericanMathematicalSociety,1995.
[316]AviShoshanandUriZwick.Allpairsshortestpathsinundirectedgraphswithinteger
PesosInProceedingsofthe40thAnnualSymposiumonFoundationsofComputerSci
ence,pages605614,1999.
[317]MichaelSipser.IntroductiontotheTheoryofComputation.ThomsonCourseTechnology,
secondedition,2006.
[318]StevenS.Skiena.TheAlgorithmDesignManual.Springer,secondedition,1998.
[319]DanielD.SleatorandRobertE.Tarjan.Adatastructurefordynamictrees.Diariode
ComputerandSystemSciences,26(3):362391,1983.

Pgina369

1248 Bibliografa

[320]DanielD.SleatorandRobertE.Tarjan.Selfadjustingbinarysearchtrees.Journalofthe
ACM,32(3):652686,1985.
[321]JoelSpencer.TenLecturesontheProbabilisticMethod,volume64ofCBMSNSFRegional
ConferenceSeriesinAppliedMathematics.SocietyforIndustrialandAppliedMathematics,
1993.
[322]DanielA.SpielmanandShangHuaTeng.Smoothedanalysisofalgorithms:Whythesim
plexalgorithmusuallytakespolynomialtime.JournaloftheACM,51(3):385463,2004.
[323]GilbertStrang.IntroductiontoAppliedMathematics.WellesleyCambridgePress,1986.
[324]GilbertStrang.LinearAlgebraandItsApplications.ThomsonBrooks/Cole,fourthedition,
2006.
[325]VolkerStrassen.Gaussianeliminationisnotoptimal.NumerischeMathematik,14(3):354
356,1969.
[326]TGSzymanski.Aspecialcaseofthemaximalcommonsubsequenceproblem.Tcnico
ReportTR170,ComputerScienceLaboratory,PrincetonUniversity,1975.
[327]RobertE.Tarjan.Depthfirstsearchandlineargraphalgorithms.SIAMJournalonCom
puting,1(2):146160,1972.
[328]RobertE.Tarjan.Efficiencyofagoodbutnotlinearsetunionalgorithm.Journalofthe
ACM,22(2):215225,1975.
[329]RobertE.Tarjan.Aclassofalgorithmswhichrequirenonlineartimetomaintaindisjoint
ConjuntosJournalofComputerandSystemSciences,18(2):110127,1979.
[330]RobertE.Tarjan.DataStructuresandNetworkAlgorithms.SocietyforIndustrialand
AppliedMathematics,1983.
[331]RobertE.Tarjan.Amortizedcomputationalcomplexity.SIAMJournalonAlgebraicand
DiscreteMethods,6(2):306318,1985.
[332]RobertE.Tarjan.Classnotes:Disjointsetunion.COS423,PrincetonUniversity,1999.
[333]RobertE.TarjanandJanvanLeeuwen.Worstcaseanalysisofsetunionalgorithms.Jour
naloftheACM,31(2):245281,1984.
[334]GeorgeB.Thomas,Jr.,MauriceD.Weir,JoelHass,andFrankR.Giordano.Thomas
https://translate.googleusercontent.com/translate_f 256/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
Calculus.AddisonWesley,eleventhedition,2005.
[335]MikkelThorup.Fasterdeterministicsortingandpriorityqueuesinlinearspace.enPro
ceedingsofthe9thACMSIAMSymposiumonDiscreteAlgorithms,pages550555,1998.
[336]MikkelThorup.Undirectedsinglesourceshortestpathswithpositiveintegerweightsin
lineartime.JournaloftheACM,46(3):362394,1999.
[337]MikkelThorup.OnRAMpriorityqueues.SIAMJournalonComputing,30(1):86109,
2000.
[338]RichardTolimieri,MyoungAn,andChaoLu.MathematicsofMultidimensionalFourier
TransformAlgorithms.Springer,secondedition,1997.
[339]P.vanEmdeBoas.Preservingorderinaforestinlessthanlogarithmictime.InProceedings
ofthe16thAnnualSymposiumonFoundationsofComputerScience,pages7584,1975.

Pgina370

Bibliografa 1249

[340]P.vanEmdeBoas.Preservingorderinaforestinlessthanlogarithmictimeandlinear
espacio.InformationProcessingLetters,6(3):8082,1977.
[341]P.vanEmdeBoas,R.Kaas,andE.Zijlstra.Designandimplementationofanefficient
priorityqueue.MathematicalSystemsTheory,10(1):99127,1976.
[342]JanvanLeeuwen,editor.HandbookofTheoreticalComputerScience,VolumeA:Algo
rithmsandComplexity.ElsevierSciencePublishersandtheMITPress,1990.
[343]CharlesVanLoan.ComputationalFrameworksfortheFastFourierTransform.Societyfor
IndustrialandAppliedMathematics,1992.
[344]RobertJ.Vanderbei.LinearProgramming:FoundationsandExtensions.KluwerAcadmico
Publishers,1996.
[345]VijayV.Vazirani.ApproximationAlgorithms.Springer,2001.
[346]RakeshM.Verma.Generaltechniquesforanalyzingrecursivealgorithmswithapplications.
SIAMJournalonComputing,26(2):568581,1997.
[347]HaoWangandBillLin.PipelinedvanEmdeBoastree:Algorithms,analysis,andap
Plicaciones.In26thIEEEInternationalConferenceonComputerCommunications,pages
24712475,2007.
[348]AntonyF.Ware.FastapproximateFouriertransformsforirregularlyspaceddata.SIAM
Review,40(4):838856,1998.
[349]StephenWarshall.Atheoremonbooleanmatrices.JournaloftheACM,9(1):1112,1962.
[350]MichaelS.Waterman.IntroductiontoComputationalBiology,Maps,Sequencesand
Genomes.Chapman&Hall,1995.
[351]MarkAllenWeiss.DataStructuresandProblemSolvingUsingC++.AddisonWesley,
secondedition,2000.
[352]MarkAllenWeiss.DataStructuresandProblemSolvingUsingJava.AddisonWesley,
thirdedition,2006.
[353]MarkAllenWeiss.DataStructuresandAlgorithmAnalysisinC++.AddisonWesley,third
edition,2007.
[354]MarkAllenWeiss.DataStructuresandAlgorithmAnalysisinJava.AddisonWesley,
secondedition,2007.
[355]HasslerWhitney.Ontheabstractpropertiesoflineardependence.AmericanJournalof
Mathematics,57(3):509533,1935.
[356]HerbertS.Wilf.AlgorithmsandComplexity.AKPeters,secondedition,2002.
[357]JWJWilliams.Algorithm232(HEAPSORT).CommunicationsoftheACM,7(6):347
348,1964.
[358]ShmuelWinograd.Onthealgebraiccomplexityoffunctions.InActesduCongr`esInterna
tionaldesMathmaticiens,volume3,pages283288,1970.
[359]AndrewC.C.Yao.Alowerboundtofindingconvexhulls.JournaloftheACM,28(4):780
787,1981.
[360]CheeYap.Arealelementaryapproachtothemasterrecurrenceandgeneralizations.NacionesUnidas
Manuscritopublicado.Availableathttp://cs.nyu.edu/yap/papers/,July2008.

Pgina371

1250 Bibliografa

https://translate.googleusercontent.com/translate_f 257/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
[361]YinyuYe.InteriorPointAlgorithms:TheoryandAnalysis.JohnWiley&Sons,1997.
[362]DanielZwillinger,editor.CRCStandardMathematicalTablesandFormulae.Chapman&
Hall/CRCPress,31stedition,2003.

Pgina372

ndice

Thisindexusesthefollowingconventions.Numbersarealphabetizedasifspelled
outforexample,234treeisindexedasifitweretwothreefourtree.When
anentryreferstoaplaceotherthanthemaintext,thepagenumberisfollowedby
atag:ex.forexercise,pr.forproblem,fig.forfigure,andn.forfootnote.Atagged
pagenumberoftenindicatesthefirstpageofanexerciseorproblem,whichisnot
necessarilythepageonwhichthereferenceactuallyappears.

.n/,574 (setdifference),1159
(goldenratio),59,108pr. jj
y(conjugateofthegoldenratio),59 (flowvalue),710
.n/(Euler'sphifunction),943 (lengthofastring),986
.n/approximationalgorithm,1106,1123 (setcardinality),1161
https://translate.googleusercontent.com/translate_f 258/259
13/1/2017 Cuyovalorptimoesidnticoaldelprogramalinealoriginal.Cuandosehacereferenciaaprogramaslinealesduales,
onotation,5051,64
Onotation,45fig.,4748,64 (Cartesianproduct),1162
O0notation,62pr. (crossproduct),1016
eOnotation,62pr. hi
!notation,51 (sequence),1166
notation,45fig.,4849,64 (standardencoding),1057
1 norte
notation,62pr. k(choose),1185
enotation,62pr. kk(euclideannorm),1222
notation,4447,45fig.,64 (factorial),57
enotation,62pr. de(ceiling),54
fg(set),1158 bc(floor),54
pag (lowersquareroot),546
2(setmember),1158 #
pag
62(notasetmember),1158 "PAG(uppersquareroot),546
Q (sum),1145
(emptylanguage),1058 (product),1148
(emptyset),1158 !(adjacencyrelation),1169
(subset),1159 (reachabilityrelation),1170
(propersubset),1159 ^(AND),697,1071
W(suchthat),1159 :(NOT),1071
\(setintersection),1159 _(OR),697,1071
[(setunion),1159 (groupoperator),939
(convolutionoperator),901

Pgina373

1252 ndice

(closureoperator),1058 ADDSUBARRAY,805pr.
j(dividesrelation),927 adjacencylistrepresentation,590
(doesnotdividerelation),927 replacedbyahashtable,593ex.
(equivalentmodulon),54,1165ex. adjacencymatrixrepresentation,591
6(notequivalentmodulon),54 adjacencyrelation(!),1169
an(equivalenceclassmodulon),928 adjacentvertices,1169
Cn(additionmodulon),940 admissibleedge,749
n(multiplicationmodulon),940 admissiblenetwork,749750
.p
un
/(Legendresymbol),982pr.
adversary,190
"(emptystring),986,1058 aggregateanalysis,452456
<(prefixrelation),986 forbinarycounters,454455
=(suffixrelation),986 forbreadthfirstsearch,597
<x(aboverelation),1022 fordepthfirstsearch,606
//(commentsymbol),21 forDijkstra'salgorithm,661
(muchgreaterthanrelation),574 fordisjointsetdatastructures,566567,
(muchlessthanrelation),783 568ex.
P(polynomialtimereducibilityrelation), fordynamictables,465
1067,1077ex. forFibonacciheaps,518,522ex.
forGraham'sscan,1036
AAtree,338 fortheKnuthMorrisPrattalgorithm,1006
abeliangroup,940 forPrim'salgorithm,636
ABOVE,1024 forrodcutting,367
aboverelation(<x),1022 forshortestpathsinadag,655
absentchild,1178 forstackoperations,452454
absolutelyconvergentseries,1146 aggregateflow,863
absorptionlawsforsets,1160 AkraBazzimethodforsolvingarecurrence,
abstractproblem,1054 112113
acceptablepairofintegers,972 algorithm,5
acceptance correctnessof,6
byanalgorithm,1058 originofword,42
byafiniteautomaton,996 runningtimeof,25
acceptingstate,995 asatechnology,13
accountingmethod,456459 Alice,959
forbinarycounters,458 ALLOCATENODE,492
fordynamictables,465466 UN
forstackoperations,457458,458ex.
Ackermann'sfunction,585
activityselectionproblem,415422,450
acyclicgraph,1170
relationtomatroids,448pr.
addinstruction,23
adicin
ofbinaryintegers,22ex.
ofmatrices,1220
modulon(Cn),940
ofpolynomials,898
additivegroupmodulon,940
addressing,open,seeopenaddresshashtable

https://translate.googleusercontent.com/translate_f 259/259

Вам также может понравиться