Tải bản đầy đủ - 0 (trang)
5 Strassen's Matrix Multiplication Algorithm

5 Strassen's Matrix Multiplication Algorithm

Tải bản đầy đủ - 0trang

Intheexercises,youwillshowthatthisiscorrect.



Tomultiplytwo2x2matrices,Strassen'smethodrequiresseven

multiplicationsand18additions/subtractions,whereasthestraightforward

methodrequireseightmultiplicationsandfouradditions/subtractions.We

havesavedourselvesonemultiplicationattheexpenseofdoing14

additionaladditionsorsubtractions.Thisisnotveryimpressive,and

indeeditisnotinthecaseof2x2matricesthatStrassen'smethodisof

value.Becausethecommutativityofmultiplicationsisnotusedin

Strassen'sformulas,thoseformulaspertaintolargermatricesthatare

eachdividedintofoursubmatrices.FirstwedividethematricesAandB,

asillustratedinFigure2.4.Assumingthatnisapowerof2,thematrix

A11,forexample,ismeanttorepresentthefollowingsubmatrixofA:





Figure2.4:ThepartitioningintosubmatricesinStrassen's

algorithm.

UsingStrassen'smethod,firstwecompute



whereouroperationsarenowmatrixadditionandmultiplication.Inthe

sameway,wecomputeM2throughM7.Nextwecompute



andC12,C21,andC22.Finally,theproductCofAandBisobtainedby

combiningthefoursubmatricesCij.Thefollowingexampleillustrates

thesesteps.

Example2.5

Supposethat



Figure2.5illustratesthepartitioninginStrassen'smethod.The

computationsproceedasfollows:





Figure2.5:ThepartitioninginStrassen'salgorithmwithn=4and

valuesgiventothematrices.

Whenthematricesaresufficientlysmall,wemultiplyinthestandardway.

Inthisexample,wedothiswhenn=2.Therefore



Afterthis,M2throughM7arecomputedinthesameway,andthenthe

valuesofC11,C12,C21,andC22arecomputed.Theyarecombinedto

yieldC.



NextwepresentanalgorithmforStrassen'smethodwhennisapowerof

2.

Algorithm2.8:Strassen

Problem:Determinetheproductoftwonxnmatriceswherenisapower

of2.

Inputs:anintegernthatisapowerof2,andtwonxnmatricesAandB.

Outputs:theproductCofAandB.

voidstrassen(intn

n×n_matrixA,

n×n_matrixB,

n×n_matrix&C)

{

if(n<=threshold)

computeC=A×Busingthestandardalgorithm;

else{

partitionAintofoursubmatricesA11,A12,A21,

partitionBintofoursubmatricesB11,B12,B21,

computeC=A×BusingStrassen'smethod;

//examplerecursivecall;

//strassen(n/2,A11+A22,B11+B22,M1)

}

}



Thevalueofthresholdisthepointatwhichwefeelitismoreefficientto

usethestandardalgorithmthanitwouldbetocallprocedureStrassen

recursively.InSection2.7wediscussamethodfordetermining

thresholds.

AnalysisofAlgorithm2.8Every-CaseTimeComplexityAnalysis

ofNumberofMultiplication(Strassen)

Basicoperation:oneelementarymultiplication.

Inputsize:n,thenumberofrowsandcolumnsinthematrices.

Forsimplicity,weanalyzethecaseinwhichwekeepdividinguntilwe

havetwo1×1matrices,atwhichpointwesimplymultiplythe

numbersineachmatrix.Theactualthresholdvalueuseddoesnot

affecttheorder.Whenn=1,exactlyonemultiplicationisdone.When

wehavetwon×nmatriceswithn>1,thealgorithmiscalledexactly

seventimeswithan(n/2)×(n/2)matrixpassedeachtime,andno

multiplicationsaredoneatthetoplevel.Wehaveestablishedthe

recurrence



ThisrecurrenceissolvedinExampleB.2inAppendixB.Thesolution

is



AnalysisofAlgorithm2.8Every-CaseTimeComplexityAnalysis

ofNumberofAdditions/Subtractions(Strassen)

Basicoperation:oneelementaryadditionorsubtraction.



Inputsize:n,thenumberofrowsandcolumnsinthematrices.

Againweassumethatwekeepdividinguntilwehavetwo1×1

matrices.Whenn=1,noadditions/subtractionsaredone.Whenwe

havetwon×nmatriceswithn>1,thealgorithmiscalledexactly

seventimeswithan(n/2)×(n/2)matrixpassedineachtime,and18

matrixadditions/subtractionsaredoneon(n/2)×(n/2)matrices.When

two(n/2)×(n/2)matricesareaddedorsubtracted,(n/2)2additionsor

subtractionsaredoneontheitemsinthematrices.Wehave

establishedtherecurrence



ThisrecurrenceissolvedinExampleB.20inAppendixB.Thesolution

is



Whennisnotapowerof2,wemustmodifythepreviousalgorithm.One

simplemodificationistoaddsufficientnumbersofcolumnsandrowsof

0stotheoriginalmatricestomakethedimensionapowerof2.

Alternatively,intherecursivecallswecouldaddjustoneextrarowand

oneextracolumnof0swheneverthenumberofrowsandcolumnsis

odd.Strassen(1969)suggestedthefollowing,morecomplex

modification.Weembedthematricesinlargeroneswith2kmrowsand

columns,wherek=[lgn−4]andm=[n/2k]+1.WeuseStrassen's

methoduptoathresholdvalueofmandusethestandardalgorithm

afterreachingthethreshold.Itcanbeshownthatthetotalnumberof

arithmeticoperations(multiplications,additions,andsubtractions)isless

than4.7n2.81.

Table2.3comparesthetimecomplexitiesofthestandardalgorithmand



Strassen'salgorithmfornapowerof2.Ifweignoreforthemomentthe

overheadinvolvedintherecursivecalls,Strassen'salgorithmisalways

moreefficientintermsofmultiplicaitons,andforlargevaluesofn,

Strassen'salgorithmismoreefficientintermsofadditions/subtractions.

InSection2.7wewilldiscussananalysistechniquethataccountsforthe

timetakenbytherecursivecalls.

Table2.3:Acomparisonoftwoalgorithmsthatmultiplyn×n

matrices





StandardAlgorithm



Strassen'sAlgorithm



Multiplications



n3



n2.81



Additions/Subtractions



n3–n2



6n2.81–6n2



ShmuelWinograddevelopedavariantofStrassen'salgorithmthat

requiresonly15additions/subtractions.ItappearsinBrassardand

Bratley(1988).Forthisalgorithm,thetimecomplexityofthe

additions/subtractionsisgivenby



CoppersmithandWinograd(1987)developedamatrixmultiplication

algorithmwhosetimecomlexityforthenumberofmultiplicationsisinO

(n2.38).However,theconstantissolargethatStrassen'salgorithmis

usuallymoreefficient.

Itispossibletoprovethatmatrixmultiplicationrequiresanalgorithm

whosetimecomplexityisatleastquadratic.Whethermatrix

multiplicationscanbedoneinquadratictimeremainsanopenquestion;

noonehasevercreatedaquadratic-timealgorithmformatrix

multiplication,andnoonehasproventhatitisnotpossibletocreatesuch

analgorithm.

Onelastpointisthatothermatrixoperationssuchasinvertingamatrix

andfindingthedeterminantofamatrixaredirectlyrelatedtomatrix

multiplication.Therefore,wecanreadilycreatealgorithmsforthese



operationsthatareasefficientasStrassen'salgorithmformatrix

multiplication.



2.6ArithmeticwithLargeIntegers

Supposethatweneedtodoarithmeticoperationsonintegerswhosesize

exceedsthecomputer'shardwarecapabilityofrepresentingintegers.If

weneedtomaintainallthesignificantdigitsinourresults,switchingtoa

floating-pointrepresentationwouldbeofnovalue.Insuchcases,our

onlyalternativeistousesoftwaretorepresentandmanipulatethe

integers.Wecanaccomplishthiswiththehelpofthedivide-and-conquer

approach.Ourdiscussionfocusesonintegersrepresentedinbase10.

However,themethodsdevelopedcanreadilybemodifiedforuseinother

bases.



2.6.1RepresentationofLargeIntegers:AdditionandOther

Linear-TimeOperaions

Astraightforwardwaytorepresentalargeintegeristouseanarrayof

integers,inwhicheacharrayslotstoresonedigit.Forexample,the

integer543,127canberepresentedinthearraySasfollows:



Torepresentbothpositiveandnegativeintegersweneedonlyreserve

thehigh-orderarrayslotforthesign.Wecoulduse0inthatslotto

representapositiveintegerand1torepresentanegativeinteger.Wewill

assumethisrepresentationandusethedefineddatatypelarge_integer

tomeananarraybigenoughtorepresenttheintegersintheapplication

ofinterest.

Itisnotdifficulttowritelinear-timealgorithmsforadditionand

subtraction,wherenisthenumberofdigitsinthelargeintegers.The

basicoperationconsistsofthemanipulationofonedecimaldigit.Inthe

exercisesyouareaskedtowriteandanalyzethesealgorithms.

Furthermore,linear-timealgorithmscanreadilybewrittenthatdothe

operation



whereurepresentsalargerinteger,misanonnegativeinteger,divide

returnsthequotientinintegerdivision,andremreturnstheremainder.

This,too,isdoneintheexercises.



2.6.2MultiplicationofLargeIntegers

Asimplequadratic-timealgorithmformultiplyinglargeintegersisonethat

mimicsthestandardwaylearnedingrammarschool.Wewilldevelop

onethatisbetterthanquadratictime.Ouralgorithmisbasedonusing

divide-and-conquertosplitann-digitintegerintotwointegersof

approximatelyn/2digits.

Followingaretwoexamplesofsuchsplits.



Ingeneral,ifnisthenumberofdigitsintheintegeru,wewillsplitthe

integerintotwointegers,onewith[n/2]andtheotherwith[n/2],as

follows:



Withthisrepresentation,theexponentmof10isgivenby



Ifwehavetwon-digitintegers



theirproductisgivenby



Wecanmultiplyuandvbydoingfourmultiplicationsonintegerswith

abouthalfasmanydigitsandperforminglinear-timeoperations.The

followingexampleillustratesthismethod.

Example2.6

Considerthefollowing:



Recursively,thesesmallerintegerscanthenbemultipliedbydividing

themintoyetsmallerintegers.Thisdivisionprocessiscontinueduntila

thresholdvalueisreached,atwhichtimethemultiplicationcanbedone

inthestandardway.



Althoughweillustratethemethodusingintegerswithaboutthesame

numberofdigits,itisstillapplicablewhenthenumberofdigitsis

different.Wesimplyusem=[n/2]tosplitbothofthem,wherenisthe

numberofdigitsinthelargerinteger.Thealgorithmnowfollows.We

keepdividinguntiloneoftheintegersis0orwereachsomethreshold

valueforthelargerinteger,atwhichtimethemultiplicationisdoneusing

thehardwareofthecomputer(thatis,intheusualway).

Algorithm2.9:LargeIntegerMultiplication

Problem:Multiplytwolargeintegers,uandv.

Inputs:largeintegersuandv.

Outputs:prod,theproductofuandv.

large_integerprod(large_integeru,large_integerv)

{

large_integerx,y,w,z;



intn,m;

n=maximum(numberofdigitsinu,numberofdigitsin

if(u==0||v==0)

return0;

elseif(n<=threshold)

returnu×vobtainedintheusualway;

else{

m=[n/2];

x=udivide10m;y=urem10m;

w=vdivide10m;z=vrem10m;

returnprod(x,w)x102m+(prod(w,y))x10m+prod(y,z);

}

}



Noticethatnisanimplicitinputtothealgorithmbecauseitisthenumber

ofdigitsinthelargerofthetwointegers.Rememberthatdivide,rem,

and×representlinear-timefunctionsthatweneedtowrite.

AnalysisofAlgorithm2.9Worst-CaseTimeComplexity(Large

IntegerMultiplication)

Weanalyzehowlongittakestomultiplytwon-digitintegers.

Basicoperation:Themanipulationofonedecimaldigitinalarge

integerwhenadding,subtracting,ordoingdivide10m,rem10m,or

×10m.Eachoftheselatterthreecallsresultsinthebasicoperation

beingdonemtimes.

Inputsize:n,thenumberofdigitsineachofthetwointegers.

Theworstcaseiswhenbothintegershavenodigitsequalto0,

becausetherecursiononlyendswhenthresholdispassed.Wewill

analyzethiscase.

Supposenisapowerof2.Thenx,y,w,andzallhaveexactlyn/2



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

5 Strassen's Matrix Multiplication Algorithm

Tải bản đầy đủ ngay(0 tr)

×