Skip ListWord文档格式.docx
- 文档编号:18431987
- 上传时间:2022-12-16
- 格式:DOCX
- 页数:12
- 大小:59.03KB
Skip ListWord文档格式.docx
《Skip ListWord文档格式.docx》由会员分享,可在线阅读,更多相关《Skip ListWord文档格式.docx(12页珍藏版)》请在冰豆网上搜索。
Search(x)
while
(1)
whilep->
next->
key<
x
p=p->
next
Ifp->
down==NULL
returnp->
down
thelogiciseasy,goright,ifcan’t,godown.
Insertion
Theprocessneedaktodetermineinsertthenodetowhichlevel,Iwilltalkaboutkafterwards.Pseudocodeislikethis
Node_Insert(x)
recordtheprecursoroftheinsertednodeineachlevelinarrayp[i]
fori=1,2…k
ifp[i]=NULL
createanewlevelandinsertthenode
else
Insertthenodetotheexistedlevel
return
kisdeterminedbytheprocess
Random_K
fori=0…31
ifrand()
k++
break
Itshouldhavethiskindofdistributiontoguaranteeitstimebound.
delete
deleteisalsoeasy,wejustneedtodeletethenodefromthelists,ifthelevelisempty,theremovethelevel,thepseudocodeislikethis
Note_Delete
p=highestnodeintheskiplist
whilepintheskiplist
ifthelistisempty
removethelist
deletepfromitslist
p=p->
down
Chapter3:
TestingResults
Wefirstinsertalltheoddintotheskiplist,thentesteachoperationfor50timesandcalculatetheaveragetime.Heregivesthetimetable(unit:
millisecond).
size
500
1000
2000
4000
6000
8000
10000
insert
0.00399
0.005386
0.010896
0.013868
0.024649
0.027711
0.029879
find
0.001199
0.002775
0.008194
0.011241
0.020461
0.025404
0.029748
0.001429
0.003071
0.008369
0.014216
0.024994
0.025379
0.027268
Thecharts
Wecansaythatthetimeofinsertion,deletingandsearchingareroughlyO(lgn).
Chapter4:
AnalysisandComments
Analysis
Toanalysisthetimeandspaceboundofskiplist,somelemmasarenecessary.
●Boole’sinequality
Pr[E1∪E2∪…∪En]≤Pr[E1]+Pr[E2]+…Pr[En]
●Informaldefinitionof“withhighpossibility”
Aneventoccurswithhighprobabilityif,foranyα≥1,thereisanappropriatechoiceofconstantsforwhichEoccurswithprobabilityatleast1-O(1/nα).
ItisthedefinitionfromProfessorErikDemaine,MITinthelectureofskiplists.Surelyitisnotastrictdefinition,butwecanuseitforefficiencyinthisproof.Bytheway,theprecisedefinitionislikethis.
●Precisedefinitionof“withhighpossibility”
A(parameterized)eventEαoccurswithhighprobabilityif,foranyα≥1,Eαoccurswithprobabilityatleast1-cα/nα,wherecαisa“constant”dependingonlyonα.
AndwecancallthetermO(1/nα)errorprobability.
●Lemma:
Withhighprobability,skiplistwithnelementshasO(lgn)levels.
Proof:
Wemayaswelldoexactlytheoppositeandconsidertheprobabilityoffall.Thatwillbe
Pr[thelevelsaremorethanclgn]=Pr[anyelementisinmorethanclgnlevels],supposetheyarex1,x2,...xn
ThenaccordingtoBoole’sinequality
Pr[anyelementisinmorethanclgnlevels]≤Pr[x1isinmorethanclgnlevels]+Pr[x2isinmorethanclgnlevels]+…Pr[xnisinmorethanclgnlevels]=n·
(0.5)clgn=1/nc-1
Sofarweseethatα=c-1,andαcanbearbitrarilylarge.
●Claim:
SearchinginskiplistwillbeinO(lgn)timewithhighprobability.
Proofofclaim:
Wealsodoexactlytheopposite,thatis,analyzethesearchprocessbackwards—fromleaftoroot.
WeknowtheheightisinO(lgn),nowsayitisclgn.Soinasearchprocess,the“up”movecannotbemorethanclgn.Thenextthingistoprovethatinθ(lgn)timesmove,thereareclgntimes“up”movewithhighprobability.
Saywetakeaclgnmoves,accordingtoBernoulliprobabilitymodel
Pr[exactlyclgn“up”moves]=
Pr[atmostclgn“up”moves]=
Accordingtotheinequality
So
Pr[atmostclgn“up”moves]≤
=
=1/nα
Asa→+∞,α=(a-1)-lg(10e)→+∞,q.e.d.
SameanalyzecanbeusedoninsertionanddeletionandtheyhaveO(lgn)timeboundwithhighprobability.
●Spacecomplexity
Fromthepointofexpectation,theithisexpectedtohaven/
nodes,sothenodesintheskiplistshouldbe
n+n/2+n/4+……=2n
sothespacecomplexityisO(n).
Comments
Theskiplist’sadvantageisthatitiseasytoimplement.Thetimeofinsertion,deletingandsearchingisnotonlyanaveragebound,butalsoa“withhighprobability”bound.Thatistosay,asngrows,thetimewillmoreandmoredistributeonclgn,whichisasignificativeandstrongproposition.
Appendix:
SourceCode(ifrequired)
skiplist.h
#ifndef__SKIPLIST_H__
#define__SKIPLIST_H__
#defineMAXLEVEL32
structsNode;
typedefstructsNode*Node;
structsList;
typedefstructsList*List;
NodeInitializeNode(intlevel,intkey,intvalue);
voidDeleteNode(Nodenode);
ListInitializeList(void);
voidDeleteList(Listlist);
intRandomLevel(void);
voidInsert(Listlist,intkey,intvalue);
voidDelete(Listlist,intkey);
intFind(Listlist,intkey);
#endif
skiplist.c
#include<
stdio.h>
stdlib.h>
time.h>
#include"
skiplist.h"
NodeInitializeNode(intlevel,intkey,intvalue)
Nodenode=(Node)malloc(sizeof(structsNode)+level*sizeof(Node));
if(node==NULL)
returnNULL;
node->
key=key;
value=value;
returnnode;
}
voidDeleteNode(Nodenode)
return;
free(node);
node=NULL;
ListInitializeList(void)
inti;
Listlist=(List)malloc(sizeof(structsList));
if(list==NULL)
list->
level=1;
header=InitializeNode(MAXLEVEL,-1,-1);
if(list->
header==NULL)
for(i=0;
i<
MAXLEVEL;
i++)
list->
header->
next[i]=NULL;
returnlist;
voidDeleteList(Listlist)
Nodetemp1,temp2;
temp1=list->
header;
while(temp1)
{
temp2=temp1->
next[0];
DeleteNode(temp1);
temp1=temp2;
}
free(list);
list=NULL;
intRandomLevel(void)
intlevel=1;
srand((unsigned)time(NULL));
for(i=1;
=31;
if(rand()%2)
{
level++;
}
else
break;
returnlevel;
voidInsert(Listlist,intkey,intvalue)
Nodetemp[MAXLEVEL];
Nodet=list->
header,new;
for(i=list->
level-1;
i>
=0;
i--)
while(t->
next[i]&
&
t->
next[i]->
key)
t=t->
next[i];
temp[i]=t;
}
t=t->
if(t&
key==key)
return;
level=RandomLevel();
if(level>
level)
for(i=list->
level;
level;
temp[i]=list->
level=level;
new=InitializeNode(level,key,value);
if(new==NULL)
for(i=0;
new->
next[i]=temp[i]->
temp[i]->
next[i]=new;
voidDelete(Listlist,intkey)
i>
=0;
if(!
t||t->
key!
=key)
i++)
if(temp[i]->
next[i]==t)
temp[i]->
next[i]=t->
while(list->
level>
1&
next[list->
level-1]==NULL)
level--;
DeleteNode(t);
intFind(Listlist,intkey)
inti;
i--)
while(t->
returnt->
value;
return-1;
References
[1]ErikDemaine,“LectureNotesonSkipLists”,March18,2004
[2]MarkAllenWeiss,“DataStructureandAlgorithmAnalysisinC”,机械工业出版社,2014
[3]sponge,“跳表(SkipList)的介绍以及查找插入删除等操作”,
Declaration
Weherebydeclarethatalltheworkdoneinthisprojecttitled"
SkipList"
isofourindependenteffortasagroup.
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Skip List
