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

havesavedourselvesonemultiplicationattheexpenseofdoing14

indeeditisnotinthecaseof2x2matricesthatStrassen'smethodisof

value.Becausethecommutativityofmultiplicationsisnotusedin

Strassen'sformulas,thoseformulaspertaintolargermatricesthatare

eachdividedintofoursubmatrices.FirstwedividethematricesAandB,

asillustratedinFigure2.4.Assumingthatnisapowerof2,thematrix

A11,forexample,ismeanttorepresentthefollowingsubmatrixofA:

Figure2.4:ThepartitioningintosubmatricesinStrassen's

algorithm.

UsingStrassen'smethod,firstwecompute

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

Inputsize:n,thenumberofrowsandcolumnsinthematrices.

Againweassumethatwekeepdividinguntilwehavetwo1×1

havetwon×nmatriceswithn>1,thealgorithmiscalledexactly

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

subtractionsaredoneontheitemsinthematrices.Wehave

establishedtherecurrence

ThisrecurrenceissolvedinExampleB.20inAppendixB.Thesolution

is

Whennisnotapowerof2,wemustmodifythepreviousalgorithm.One

0stotheoriginalmatricestomakethedimensionapowerof2.

oneextracolumnof0swheneverthenumberofrowsandcolumnsis

odd.Strassen(1969)suggestedthefollowing,morecomplex

modification.Weembedthematricesinlargeroneswith2kmrowsand

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

methoduptoathresholdvalueofmandusethestandardalgorithm

afterreachingthethreshold.Itcanbeshownthatthetotalnumberof

than4.7n2.81.

Table2.3comparesthetimecomplexitiesofthestandardalgorithmand

Strassen'salgorithmfornapowerof2.Ifweignoreforthemomentthe

moreefficientintermsofmultiplicaitons,andforlargevaluesofn,

InSection2.7wewilldiscussananalysistechniquethataccountsforthe

timetakenbytherecursivecalls.

Table2.3:Acomparisonoftwoalgorithmsthatmultiplyn×n

matrices

StandardAlgorithm

Strassen'sAlgorithm

Multiplications

n3

n2.81

n3–n2

6n2.81–6n2

Bratley(1988).Forthisalgorithm,thetimecomplexityofthe

algorithmwhosetimecomlexityforthenumberofmultiplicationsisinO

(n2.38).However,theconstantissolargethatStrassen'salgorithmis

usuallymoreefficient.

Itispossibletoprovethatmatrixmultiplicationrequiresanalgorithm

multiplication,andnoonehasproventhatitisnotpossibletocreatesuch

analgorithm.

Onelastpointisthatothermatrixoperationssuchasinvertingamatrix

andfindingthedeterminantofamatrixaredirectlyrelatedtomatrix

operationsthatareasefficientasStrassen'salgorithmformatrix

multiplication.

2.6ArithmeticwithLargeIntegers

Supposethatweneedtodoarithmeticoperationsonintegerswhosesize

exceedsthecomputer'shardwarecapabilityofrepresentingintegers.If

weneedtomaintainallthesignificantdigitsinourresults,switchingtoa

floating-pointrepresentationwouldbeofnovalue.Insuchcases,our

onlyalternativeistousesoftwaretorepresentandmanipulatethe

integers.Wecanaccomplishthiswiththehelpofthedivide-and-conquer

approach.Ourdiscussionfocusesonintegersrepresentedinbase10.

bases.

Linear-TimeOperaions

Astraightforwardwaytorepresentalargeintegeristouseanarrayof

integers,inwhicheacharrayslotstoresonedigit.Forexample,the

integer543,127canberepresentedinthearraySasfollows:

Torepresentbothpositiveandnegativeintegersweneedonlyreserve

thehigh-orderarrayslotforthesign.Wecoulduse0inthatslotto

representapositiveintegerand1torepresentanegativeinteger.Wewill

assumethisrepresentationandusethedefineddatatypelarge_integer

tomeananarraybigenoughtorepresenttheintegersintheapplication

ofinterest.

subtraction,wherenisthenumberofdigitsinthelargeintegers.The

basicoperationconsistsofthemanipulationofonedecimaldigit.Inthe

operation

whereurepresentsalargerinteger,misanonnegativeinteger,divide

returnsthequotientinintegerdivision,andremreturnstheremainder.

This,too,isdoneintheexercises.

2.6.2MultiplicationofLargeIntegers

mimicsthestandardwaylearnedingrammarschool.Wewilldevelop

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

followingexampleillustratesthismethod.

Example2.6

Considerthefollowing:

Recursively,thesesmallerintegerscanthenbemultipliedbydividing

themintoyetsmallerintegers.Thisdivisionprocessiscontinueduntila

thresholdvalueisreached,atwhichtimethemultiplicationcanbedone

inthestandardway.

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

×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)

×