p2document.docx

上传人:b****7 文档编号:10317740 上传时间:2023-02-10 格式:DOCX 页数:13 大小:190.94KB
下载 相关 举报
p2document.docx_第1页
第1页 / 共13页
p2document.docx_第2页
第2页 / 共13页
p2document.docx_第3页
第3页 / 共13页
p2document.docx_第4页
第4页 / 共13页
p2document.docx_第5页
第5页 / 共13页
点击查看更多>>
下载资源
资源描述

p2document.docx

《p2document.docx》由会员分享,可在线阅读,更多相关《p2document.docx(13页珍藏版)》请在冰豆网上搜索。

p2document.docx

p2document

Project2-----HuffmanCodes

-----ByGroup062008.12-10

Index

Chapter1:

Introduction..........................................................................................-

1.4Stepstoruntheprogram

Chapter2:

AlgorithmsSpecification...................................................................-

2.1DataStructures

2.2Algorithms

Chapter3:

TestingResults.......................................................................................–

Chapter4:

AnalysisandComments.....................................................................-

4.1

4.2someFurtherComments

4.3timecomplexity&spacecomplexity

4.4FurtherImprovements

AppendixI:

SourceCode(inC).............................................................................–

A.Mianfunctionoftheproject.

B.Testgeneratefunctionfortester.

AppendixII:

DutyAssignments......................................................................

 

Chapter1:

Introduction

ProblemDescription:

In1953,DavidA.Huffmanpublishedhispaper“AMethodfortheConstructionofMinimum-RedundancyCodes”,andhenceprintedhisnameinthehistoryofcomputerscience.AsaprofessorwhogivesthefinalexamproblemonHuffmancodes,Iamencounteringabigproblem:

theHuffmancodesareNOTunique.Forexample,givenastring“aaaxuaxz”,wecanobservethatthefrequenciesofthecharacters'a','x','u'and'z'are4,2,1and1,respectively.Wemayeitherencodethesymbolsas{'a'=0,'x'=10,'u'=110,'z'=111},orinanotherwayas{'a'=1,'x'=01,'u'=001,'z'=000},bothcompressthestringinto14bits.Anothersetofcodecanbegivenas{'a'=0,'x'=11,'u'=100,'z'=101},but{'a'=0,'x'=01,'u'=011,'z'=001}isNOTcorrectsince“aaaxuaxz”and“aazuaxax”canbothbedecodedfromthecode00001011001001.Thestudentsaresubmittingallkindsofcodes,andIneedacomputerprogramtohelpmedeterminewhichonesarecorrectandwhichonesarenot.

OurpurposeistojudgewhetherthesubmissionofthestudentsisHuffmancodeornot.

Firstweshouldmentionthatthecodethatgenetaredbythehuffmanalgorithmisuniquecausethenodeswillbesortedbeforecreatingthetree,while,generally,Huffmancodeisnotunique,justbecauseunsotednodeshavemanysequences.Itwillbeanimportantpointwhileconsideringthealgorithm.Youwillseemoredetailsinthenextchapter:

AlgorithmsSpecification.

 

Background:

AboutDavidA.Huffman(August9,1925–October7,1999):

DavidA.Huffmanwasapioneerinthecomputersciencefield.Throughouthislife,Huffmanmademanyimportantcontributionstothestudyoffinitestatemachines,switchingcircuits,synthesisprocedures,andsignaldesignsetc.

However,DavidHuffmanisbestknownforhislegendary“Huffmancode”,whichwewillstudyinthisproject,acompressionschemeforlosslessvariablelengthencoding.ItwastheresultofatermpaperhewrotewhileagraduatestudentattheMassachusettsInstituteofTechnology(MIT),whereheearnedaScDdegreeonathesisnamedTheSynthesisofSequentialSwitchingCircuits,advisedbySamuelH.Caldwell(1953).[1]

"HuffmanCodes"arewidelyusedinnearlyeveryapplicationthatinvolvesthecompressionandtransmissionofdigitaldata,suchasfaxmachines,modems,computernetworks,andhigh-definitiontelevision(HDTV),whichiswidelyusedinmodernsociety.

AboutHuffmancode:

Chapter2:

AlgorithmSpecification

DataStructures:

MainDataStructureforaHuffmanTreeTreeNode:

typedefstruct

{

unsignedintweight;

unsignedintparent,lchild,rchild;

}HTNode;

Algorithms:

1.First,bulidtheHuffmantree:

for(i=n+1;i<=m;++i)

{

select(*HT,i-1,&s1,&s2);/*selectthetwosmallestnodewhoseparentiszerofromHT[1]toHT[i-1]*/

(*HT)[s1].parent=(*HT)[s2].parent=i;

(*HT)[i].lchild=s1;

(*HT)[i].rchild=s2;

(*HT)[i].weight=(*HT)[s1].weight+(*HT)[s2].weight;

}

BulidingmethodHuffmantree:

1.choosethesmallesttwonumberofthearray,assighitsparentpointertoi;

2.assighitsweightrightafterthat.

Thendoitrepeatedly.sowegottheHuffmantree.

Nowillustratingit:

Doitbyuppermethodrepeadly:

Doitbyuppermethodrepeadly:

Finalstate:

NowwegotHuffmantree!

2.ThenwecalculatetheWPLofthestandardHuffmantree.

for(i=1;i<=n;i++)

{

WPL=WPL+w[i-1]*strlen(HC[i]);/*calculatetogetthevalueofWPL*/

}

3.CalculatetheWPLoftheinputsubmissions.

sum=0;/*initializationsum*/

for(i=0;i

{

scanf("%s",&c);

getchar();

gets(string[i]);

sum=sum+w[i]*strlen(string[i]);/*thetotallengthofcodeneededtodetermine*/

}

4.CompareWPLandWPL’

Ifthemareequalinputcaseright.Elsecheckwhetherinputcasehavetherelationshipofprefixion

if(sum!

=WPL)/*ifsumnotequalsWPL,thenwecansaynoimmediately*/

printf("No\n");

else/*ifnot,weneedtolookwhethertheyhavetherelationshipofprefixion*/

{

for(k=0;k

{

for(i=k+1;i

{

l=strlen(string[k]);

if(strlen(string[i])

l=strlen(string[i]);

r=strncmp(string[i],string[k],l);

if(r==0)break;}

if(r==0)

{

printf("No\n");break;

}

else

{

if(k==n-1){

printf("Yes\n");

}

}

}

Chapter3:

TestingResults

1.Sampletest

Sampleinputofthisproject:

SampleInput:

4

a4x2u1z1

2

a0

x10

u110

z111

a0

x01

u011

z001

7

A1B1C1D3E3F6G6

2

A00000

B00001

C0001

D001

E01

F10

G11

A000

B001

C010

D011

E100

F101

G110

0

Ouputresultoftheprogrmme:

Compareitwiththecorrectoutput:

Case1:

Yes

No

Case2:

Yes

No

Wecanseeitcorrespondsverywell,soitisRight!

Analysis[justonNo]:

AboutfirsNo:

a0x01sowecanseethata’scodeistheprefixofx’s,thoughithavetheshotestWPL.

AboutsecondNo:

Thoughtheredonotexistprefix-relationship,butitsWPLisnottheshotestsoitisnotHuffmancode.

2.FurtherTest

A.somesmall-mediumscaletestdata:

BecauseitiseasytocreatesmalltestcasessoIjustcreatethembyhand.

4

a3b4c5d6

4

a00

b01

c10

d11

a11

b10

c01

d00

a00

b10

c01

d11

a01

b10

c00

d11

20

a1b2c3d4e5f6g7h8i9j10k11l12m13n14o15p16q17r18s19t20

1

a1010000

b1010001

c101001

d101010

e101011

f10000

g10001

h11000

i11001

j0100

k0101

l0110

m0111

n1001

o1011

p1101

q1110

r1111

s000

t001

0

Output:

Case1:

Yes

Yes

Yes

Yes

Case2:

No

Analysis:

Isimplyaddsomecodeintheprogromtheprogrammerhandtome.

Here:

And:

 

Sothecontentintheouputfilearechangedasfollows:

WPL:

36Case1:

WPL':

36Yes

WPL':

36Yes

WPL':

36Yes

WPL':

36Yes

WPL:

864Case2:

WPL':

866No

Nowwecanseeitclearlytheformerfourcases’WPLcorrespondwithshotestWPLwell,sotheprogramprint“Yes”immediately。

Whileinthecasetwo.WPL'=866>WPL=864,soitisnottheHuffmancode.soprintfNo!

Toconclude:

Testingofthesesetoftestcasesareallright.

B.LargeScaletestdata

inthistestIprovidesometestcasethataregeneratedbyprogram,sothattestwhetherthenon-prifixjudgingisrightornot.

Ijustchangethesampleinput.

Inputcases:

 

Chapter4:

AnalysisandComments

 

Timecomplexity&spacecomplexityAnalysis

SomeFurtherComments

Furtherimprovement:

A.Aboutalgorithm:

ThisalgorithmfirstneedtocreateanHuffmantreetocalculatethesmallestWPLofcertaininputcharacters,andthenitcalculatetheinput

WPL,andcomparethem.ImeancalculatingWPLbycreatingaHuffmantreewillbecosty.Itcanbeimprovedusinganotheralgorithm,whileitisreallydifficulttowrite.Iftimepermititwillbeapotentialbreakthroughpointinthisprogramme.

B.AboutOthers:

1.Theuserinterfaceoftheprogramcanbemorefriendly.

2.Theoverallstyleofthedocumentcanbesmarter.

3.Ourgroupwrkingcanbemoreefficient.

AppendixI:

SourceCode(inC)

 

AppendixII:

DutyAssignments

Programmer王园园3072211113

Tester王迪3079901015

ReportWriter郭宇波3079901016

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 成人教育 > 成考

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1