清华大学算法分析与设计 课件 第08讲shortestpaths.docx

上传人:b****3 文档编号:5338868 上传时间:2022-12-15 格式:DOCX 页数:29 大小:1.12MB
下载 相关 举报
清华大学算法分析与设计 课件 第08讲shortestpaths.docx_第1页
第1页 / 共29页
清华大学算法分析与设计 课件 第08讲shortestpaths.docx_第2页
第2页 / 共29页
清华大学算法分析与设计 课件 第08讲shortestpaths.docx_第3页
第3页 / 共29页
清华大学算法分析与设计 课件 第08讲shortestpaths.docx_第4页
第4页 / 共29页
清华大学算法分析与设计 课件 第08讲shortestpaths.docx_第5页
第5页 / 共29页
点击查看更多>>
下载资源
资源描述

清华大学算法分析与设计 课件 第08讲shortestpaths.docx

《清华大学算法分析与设计 课件 第08讲shortestpaths.docx》由会员分享,可在线阅读,更多相关《清华大学算法分析与设计 课件 第08讲shortestpaths.docx(29页珍藏版)》请在冰豆网上搜索。

清华大学算法分析与设计 课件 第08讲shortestpaths.docx

清华大学算法分析与设计课件第08讲shortestpaths

Le

cture8.shortest-paths

⏹Background

⏹Tofindashortestroutefromonecitytoanother

⏹Tofindaleasttimeroutefromoneplacetotheothers

⏹工作流中的最短完成时间?

⏹Anymore?

 

清华大学1

 

Problemanditsvariants

⏹Single-Sourceshortest-path

⏹Single-Destinationshortest-path

⏹Single-pairshortest-path

⏹All-pairsshortest-paths

 

清华大学3

 

Optimalsubstructureofashortestpath

⏹Shortest-pathsalgorithmstypicallyrelyonthepropertythatashortestpathbetweentwoverticescontainsothershortestpathswithinit.

⏹ConditionsforapplyingtheDynamicprogrammingandgreedymethod:

⏹Optimalsubstructure.

 

清华大学4

Le

mma24.1.

⏹Subpathsofshortestpathsareshortestpaths

⏹Proof.Usingmethodofcutandpatse

 

清华大学5

 

Negativeweightedges

⏹Mostoftheinstanceoftheproblemsarealloftheedgesarenonnegativeweight

⏹Someoftheinstanceoftheproblemshavenegativeweightedges

⏹Negativecycles:

thenegativeinfinitivelength

 

清华大学6

3-1

0511-∞

-∞-∞

 

清华大学7

 

Cycles

⏹Canashortestpathcontainscycles:

⏹Case1.PositiveCycles:

neveroccurs

⏹Case2.NegativeCycles:

-∞

⏹Case3.zeroCycles:

removable

 

清华大学9

 

Representingshortestpaths

⏹Usingpredecessorlabelcangettheresult.

⏹LetG=(V,E)beagraph

⏹π:

V⓪V∪{null}

⏹Shortest-pathstreerootedatsisadirectedsubgraphG’=(V’,E’)ofG.

⏹V’isthesetofverticesreachablefromsinG

⏹G’formsarootedtreewithrootsand

⏹Forallv∈V’,theuniquesimplepathfromstovinG’

isashortestpathfromstovinG

清华大学10

 

 

Relaxation

⏹Weusingthetechniqueofrelaxation:

⏹Letd[v]beashortest-pathofestimate,itmustbegreatthanorequaltoitsrealdistanceofvfroms,thesource.

⏹Initialize-Single-Source(G,s)

1.Foreachvertexv∈V[G]

1.d[v]=∞

2.π[v]=null

2.d[s]=0

 

清华大学14

 

Somecommentsonrelaxation

⏹松弛什么?

⏹Relaxtheconstrainoftriangleinequality

 

清华大学16

Relax

⏹Relax(u,v,w)

1.Ifd[v]>d[u]+w(u,v)

1.d[v]⓪d[u]+w(u,v)

2.π[v]⓪u

 

清华大学17

 

Propertiesandrelaxation

1.Triangleinequality:

forany(u,v)∈Ewehave

δ(s,v)≤δ(s,u)+w(u,v)

2.Upperboundproperty:

wealwayshaved[v]≥

δ(s,v)forallverticesv∈V,andonced[v]achievesthevalueδ(s,v),itneverchanges

3.Nopathproperty:

ifthereisnopathfromstov,wealwayshaved[v]=δ(s,v)=∞

清华大学18

Properties

4.Convergenceproperty:

ifp=isashortestpathinGforsomeu,v∈V,andifd[u]=δ(s,u)atanytimepriortorelaxingedge(u,v),thend[v]=δ(s,v)atalltimeafterward

5.Pathrelaxationproperty:

ifp=isashortestpathfromv0=stovk,andtheedgesinparerelaxedintheorder(v0,v1),(v1,v2),…(vk-1,vk),thend[vk]=δ(s,vk).Thispropertyholdsregardlessofanyotherrelaxationstepsthatoccurs.

 

清华大学19

 

Properties:

6.Predecessor-subgraphproperty:

Onced[v]=

δ(s,v)forallvinV,thepredecessorsubgraphisashortest-pathstreerootedats.

 

清华大学20

Bellman-Fordalgorithm

⏹Bellman-Ford(G,w,s)

1.Initialize-Single-Source(G,s)

2.Fori⓪1to|V[G]|-1

1.Foreachedge(u,v)∈E[G]

1.Relax(u,v,w)

3.Foreachedge(u,v)∈E[G]

1.Ifd[v]>d[u]+w(u,v)returnfalse

4.Returntrue

 

清华大学

 

21

 

 

 

 

ThecorrectnessofBellman-Fordalgorithm

1.如果没有由s可达的负圈:

1.则再结束时,验证通过,且距离正确。

2.如果有由s可达的负圈:

1.设此圈为,若所有三角不等式成立,则与此负圈矛盾。

【】

 

清华大学29

 

Sidi

ngle-sourceshortestpathsinarectedacyclicgraph

⏹Dag-shortest-paths(G,w,s)

1.TopologicalsorttheverticesofG

2.Initialize-Single-Source(G,w,s)

3.Foreachvertexu,takenintopologicalorder

1.ForeachvinAdj[u]

1.Relax(u,v,w)

 

清华大学30

Dag’sShortest-paths

 

 

0

 

 

 

 

 

清华大学

 

31

 

Dijkstra’sAlgorithm

⏹Itsolvestheproblemofnonnegativeweightedgraph’sshortest-paths

⏹ItsmaintrickistomaintainasetSofverticeswhosefinalshortest-pathweightsfromthesourceshavealreadybeendetermined.

 

清华大学32

⏹Dijkstra(G,w,s)

1.Initialize-SingleSource(G,s)

2.S⓪φ

3.Q⓪V[G]

4.WhileQ!

1.u⓪Q.extractMin()

2.S⓪SU{u}

3.ForeachvertexvinAdj[u]

1.Relax(u,v,w)

清华大学33

 

 

 

Complexanalysis

TheMaintainingofMinimumQueuebycalling:

1.

Insert

2.

Extract

3.

Decreasekey

4.

Usingaggregateanalysis,wehaveO((V+E)lgV)

5.

ByusingFibonacciheap,therunningtimeisO(Vlg

V+E),byreducingtheamortizedcostof

decreasingkeyoperations

 

清华大学

 

38

ApplicationofShortestpaths

⏹图像缝合问题:

⏹Linearprogrammingwithdifferenceconstraintscanbeequivalentlyconvertedtoaproblemofshortestpathofagraph.【可行解问题】

 

清华大学39

 

Thelinearprogrammingproblem

⏹Object:

∑ni=1cixisubjectto

⏹xj-xi≤bkfori,jin{1,2,…,n}

⏹Convertingtoagraph!

如何找到可行解!

⏹nverticesrepresentthenvariablesplusanartificialvertexv0,

⏹xj-xi≤bkmeanstoaddanedge(i,j)withweightbk

⏹ConstraintsconverttotheEdgesbetweentheverticesandplusartificialedges(v0,v1),…,(v0,vn)

⏹X=(d(v0,v1),d(v0,v2),…,d(v0,vn),)isafeasible

solutionofthelinearprogrammingproblem

清华大学40

所有

点之间的最短路径

⏹动态规划方法:

⏹最优解结构:

⏹设l(k)ij为最多包含k条边的从顶点i到顶点j的最短路径的距离,如果没有这样的路径,则距离定义为∞,于是

⏹l(0)0ifi=j,∞otherwise

ij=

⏹l

(1)w(i,j),whatisw?

ij=

⏹l(m)ij=min(l(m-1)ij,p(m)ij)

⏹p(m)ij=mink=1,n(l(m-1)ik+w(k,j))

⏹l(m)min(l(m-1)+w(k,j))Why?

ij=k=1,nik

清华大学41

 

π(k)

ij—每个顶点每条路径的前驱

⏹π(k)

ij=?

如何计算

⏹与上面计算相关!

 

清华大学42

问题

的解

 

⏹什么是δ(i,j),Weclaim:

⏹δ(i,j)=l(n-1)ij=l(n)ij=l(n+1)(n+2)

ij=lij

⏹为什么?

 

清华大学43

 

算法1

1.L=W;

2.Fori=1,n

1.L=Extended-Shortest-Paths(L,W)

 

清华大学45

 

 

Floyd-Warshallalgorithm

⏹动态规划方法:

⏹最优解结构:

⏹设d(k)ij为最多包含k个中间点的顶点i到顶点j的最短路径的距离,如果没有这样的路径,则距离定义为∞,于是

⏹d(0)ij=w(i,j),

⏹π(0)ij=nullifthereisnoedge(i,j),elseitisi.

⏹d(m)ij=min(d(m-1)ij,d(m-1)im+d(m-1)mj)Why?

⏹如何计算π(k),从i到j最多包含k个中间点的最短路径中j的

ij

父节点?

⏹由π(n)计算最短路径。

ij

⏹复杂度:

O(V3)

清华大学48

Johnson算法

⏹针对稀疏矩阵,Johnson提出一种更为有效的算法。

⏹引理25.1改变加权函数不改变最短路径:

⏹设:

h:

E⓪R

⏹定义:

wˆ(u,v)=w(u,v)+h(u)-h(v)

⏹设p=是从v0到vk的任意路径,则p是从v0到vk的在权W下的最短路径的充要条件是:

从v0到vk的在权wˆ的最短路径,G在权W下有负回路的充要

条件是G在权wˆ有负回路。

 

清华大学49

W下

W下

 

 

Johnson(G)

1.

计算G’

1.V[G’]=V[G]U{s}

2.

2.E[G’]=E[G]U{(s,v):

vinV[G],且w(s,v)=0}

如果Bellman-Ford(G’,w,s)==flase

1.Print图有负回路,returnnull;

Foreach(u,v)inE[G’]

1.w’(u,v)=w(u,v)+δ(s,u)-δ(s,v)

ForeachuinV[G’]

1.Dijkstra(G,w’,u)得到δ’(u,v),对所有v属于V[G]

2.对所有v属于V[G]

3.

4.

1.duv=δ’(u,v)-δ(s,u)+δ(s,v)

5.ReturnD=(duv)n×n

清华大学

 

52

复杂性分析

1.计算G’:

O(V+E)

2.Bellman-Ford算法:

O(VE)

3.O(E)

4.O(V*Dijkstra算法复杂性)=O(V*(VlgV+E))=O(V2lgV+VE)

 

清华大学53

 

总结

⏹单源最短路径问题

⏹方法:

贪婪:

Dijkstra;BellmanFord

⏹关键点:

松弛

⏹应用:

最优化问题、图像处理、

⏹多源最短路径问题

⏹动态规划问题:

不同子空间设立

⏹群论及其应用

⏹问题转换

 

清华大学

 

54

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

当前位置:首页 > 自然科学 > 物理

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

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