Algoritma Dijkstra: Perbedaan antara revisi

Konten dihapus Konten ditambahkan
k Bot: Mengganti Dijksta_Anim.gif dengan Dijkstra_Animation.gif
Novita838 (bicara | kontrib)
→‎Pseudocode: Update code
Baris 17:
 
== Pseudocode ==
1 '''function''' Dijkstra(G''Graph'', w, s''source''):
2 '''for each''' vertex ''v'' in V[G]''Graph'': ''// Initializations''
3 ddist[''v''] := infinity ; ''// Unknown distance function from''
74 Q := V[G] ''// Set ofsource allto verticesv''
4 previous[v] := undefined
5 d[s] := 0 previous[''v''] := undefined ; ''// DistancePrevious fromnode sin tooptimal s''path
6 '''end for''' ''// from source''
6 S := empty set
7
7 Q := V[G] ''// Set of all vertices''
8 dist['''while'source''] := 0 ; Q is not an empty set ''// TheDistance from source algorithmto itselfsource''
9 ''Q'' := the set of all nodes in ''Graph'' ; ''// All nodes in the graph are''
9 u := Extract_Min(Q)
10 ''// unoptimized - thus are in Q''
10 S := S union {u}
11 '''while''' ''Q'' '''is not''' empty: ''// The main loop''
11 '''for each''' edge (u,v) outgoing from u
12 ''u'' := vertex in '''if'Q'' d[u]with +smallest w(u,v)distance <in ddist[v] ; ''// RelaxStart (u,v)node in first case''
13 remove d[v] := d[''u]'' +from w(u,v)''Q'' ;
14 '''if''' previousdist[v''u''] := uinfinity:
15 '''break''' ; ''// all remaining vertices are''
16 '''end if''' ''// inaccessible from source''
17
18 '''for each''' neighbor ''v'' of ''u'': ''// where v has not yet been''
19 ''// removed from Q.''
20 ''alt'' := dist[''u''] + dist_between(''u'', ''v'') ;
21 '''if''' ''alt'' < dist[''v'']: ''// Relax (u,v,a)''
22 dist[''v''] := ''alt'' ;
23 4 previous[''v''] := undefined''u'' ;
24 decrease-key ''v'' in ''Q''; ''// Reorder v in the Queue''
1025 S := S union'''end {u}if'''
26 '''end for'''
27 '''end while'''
28 '''return''' dist;
 
== Rujukan ==