9 views

Uploaded by Arockiaruby Ruby

algorithm to find shortest path

- 03 Lab Igraph1
- MatLab - Signal Analysis and Interpolation
- Chapter_5_QS015_2017_18_Nota_Pelajar_1_
- 7 Kundur FFT
- Tutorial 10
- An Indoor Shop Localization and Navigation System for Malls Using Bluetooth Beacons
- Leviticus Algorithm Finding the Shortest Paths
- References Data Nets
- Gloss Ay
- Determinants
- Slides Agco2012
- BEB20203
- C04 Images
- One Variable Optimization
- 2019_Mandelbrot_and_Julia_Sets_via_Jungck-CR_Iteration_with_s-convexity.pdf
- DWDM
- Grid Anatomy
- Nenjukkul Peidhidum Maa Mazhai
- Ilaya Nila Pozhigiradhae Eng Version
- Application Engine Basics

You are on page 1of 5

**The algorithm (Pseudo Code) is as follows
**

procedure Dijkstra (G): weighted connected simple graph, with all weights positive) [G has vertices a = v0, v1, , vn = ! and weights w(v1, v") where w(vi, vj) = #$%#$#&' i( [vi, vj) is not an edge in G) (or i := 1 to n *(vi) := #$%#$#&' *(a) := 0 + := $,** [ the la-els are now initiali!ed so that the la-el o( a is 0 and all other la-els are #$#%#$#&', + is empt. set) while ! is not -elongs to + -egin u := a verte/ not in + with *(u) minimal + := + , [u) (or all vertices u not in + #( *(u) 0 w(u,v) 1 *(v) then *(v) := *(u) 0 w(u,v) [this adds a verte/ to + with minimal la-el and updates the la-els vertices no in +) end [*(!) = length o( a shortest path (rom a to !)

Example: ow lets !ome to an e"ample whi!h further illustrates abo#e algorithm. Consider a weighted graph

$ere a% b% ! .. are nodes of the graph and the number between nodes are weights (distan!es) of the graph. ow we are going to find the shortest path between sour!e (a) and remaining #erti!es. The adja!en!& matri" of the graph is

s positive &he source verte/ must also -e positive (rom 0 < to no:(.ertices > 1 <9 void read()5 9< < %unction initiali!e initiali!es all the data mem-ers at the -egining o( < the e/ecution &he distance -etween source to source is !ero and all other < distances -etween source and vertices are in(init.distance[18)5 -ool mark[18)5 99keep track o( visited node int source5 int num:(. &he mark is initiali!ed < to (alse and predecessor is initiali!ed to >1 <9 void initiali!e()5 9< . =djacenc. 7atri/ and source < 7atri/ (rom the user &he num-er o( vertices must -e greather than < !ero. all mem-ers o( =djacenc. 7atri/ must -e postive as distances < are alwa.ertices5 pu-lic: 9< < %unction read() reads $o o( vertices.ow the following sour!e !ode implements the abo#e e"ample 2include1iostream3 2de(ine #$%#$#&' 444 using namespace std5 class Dijkstra6 private: int adj7atri/[18)[18)5 int predecessor[18).

ertices5i00) 6 mark[i) = (alse5 predecessor[i) = >15 distance[i) = #$%#$#&'5 A distance[source)= 05 .ertices 1= 0) 6 cout11BCnter the num-er o( vertices o( the graph(should -e 3 0)DnB5 cin33num:(. then it search < (or another node <9 int get?losest.ertices5 while(num:(.ertices>111endl5 cout11BCnter the source verte/ againDnB5 cin33source5 A A void Dijkstra::initiali!e()6 (or(int i=05i1num:(. marked as visited.nmarked$ode returns the node which is nearest (rom the < @redecessor marked node #( the node is alread. matri/ (or the graphDnB5 cout11B&o enter in(init. enter B11#$%#$#&'11endl5 (or(int i=05i1num:(.ertices5 A cout11BCnter the adjacenc.ertices5i00) 6 cout11BCnter the (0ve)weights (or the row B11i11endl5 (or(int j=05j1num:(.nmarked$ode()5 9< < %unction calculateDistance calculates the minimum distances (rom the source node to < :ther node <9 void calculateDistance()5 9< < %unction output prints the results <9 void output()5 void print@ath(int)5 A5 void Dijkstra::read()6 cout11BCnter the num-er o( vertices o( the graph(should -e 3 0)DnB5 cin33num:(.< %unction get?losest.ertices5j00) 6 cin33adj7atri/[i)[j)5 while(adj7atri/[i)[j)10) 6 cout11BEeights should -e 0ve Cnter the weight againDnB5 cin33adj7atri/[i)[j)5 A A A cout11BCnter the source verte/DnB5 cin33source5 while((source10) FF (source3num:(.ertices>1)) 6 cout11B+ource verte/ should -e -etween 0 andB11num:(.

nmarked$ode5 (or(int i=05i1num:(.nmarked$ode) 0adj7atri/[closest.ertices5i00) 6 i((i == source) cout11(char)(source 0 4H)11B else print@ath(i)5 cout11B>3B11distance[i)11endl5 A B11source5 .nmarked$ode5 int count = 05 while(count 1 num:(.nmarked$ode)[i)5 predecessor[i) = closest.ertices5i00) 6 i(((Gmark[i)) FF ( minDistance 3= distance[i))) 6 minDistance = distance[i)5 closest.nmarked$ode) 0adj7atri/[closest.nmarked$ode5 A A A count005 A A void Dijkstra::print@ath(int node)6 i((node == source) cout11(char)(node 0 4H)11B B5 else i((predecessor[node) == >1) cout11B$o path (rom I11source11Jto B11(char)(node 0 4H)11endl5 else 6 print@ath(predecessor[node))5 cout11(char) (node 0 4H)11B B5 A A void Dijkstra::output()6 (or(int i=05i1num:(.nmarked$ode)[i)) 6 distance[i) = distance[closest.A int Dijkstra::get?losest.nmarked$ode5 A void Dijkstra::calculateDistance()6 initiali!e()5 int minDistance = #$%#$#&'5 int closest.ertices5i00) 6 i(((Gmark[i)) FF (adj7atri/[closest.nmarked$ode)[i)30) ) 6 i((distance[i) 3 distance[closest.nmarked$ode = i5 A A return closest.ertices) 6 closest.nmarked$ode) = true5 (or(int i=05i1num:(.nmarked$ode = get?losest.nmarked$ode()5 mark[closest.nmarked$ode()6 int minDistance = #$%#$#&'5 int closest.

A int main()6 Dijkstra G5 G read()5 G calculateDistance()5 G output()5 return 05 A The output of abo#e program is .

- 03 Lab Igraph1Uploaded bySamuel Ramirez
- MatLab - Signal Analysis and InterpolationUploaded byJosh Chiu
- Chapter_5_QS015_2017_18_Nota_Pelajar_1_Uploaded byAhmad Mutqma
- 7 Kundur FFTUploaded bykrajasekarantuti
- Tutorial 10Uploaded byapap
- An Indoor Shop Localization and Navigation System for Malls Using Bluetooth BeaconsUploaded byIJRASETPublications
- Leviticus Algorithm Finding the Shortest PathsUploaded byRavi Gohel
- References Data NetsUploaded byJose Peralta
- Gloss AyUploaded byIsuru Hewakoon
- DeterminantsUploaded bySia Kwong Kiat 谢光杰
- Slides Agco2012Uploaded byKevin Mondragon
- BEB20203Uploaded byHairil Hanif
- C04 ImagesUploaded byJavierFelixReynosoPacheco
- One Variable OptimizationUploaded byCristóbal Lara
- 2019_Mandelbrot_and_Julia_Sets_via_Jungck-CR_Iteration_with_s-convexity.pdfUploaded byfractal1313

- Application Engine BasicsUploaded byharikishore14
- 4Uploaded byprof_kt
- 1675185953cs_supportmaterial_xii.pdfUploaded byPradeep Agarwal
- Homework 3Uploaded byPhan Anh Trịnh
- python_classes_objects.pdfUploaded byAbhiram Have Drifted Far
- Image Processing AnkitUploaded byAkash Deep
- Advanced Java Lab ManualUploaded byK M Imtiaz Uddin
- OPERATING SYSTEM NOTESUploaded byishahma
- Common Navigator FrameworkUploaded byMarc Linde
- 3bse035980-510 - En System 800xa Control 5.1 Ac 800m ConfigurationUploaded byNguyễn Anh Tú
- Python ArcGIS Lesson7Uploaded byTeoman Dikerler
- mca syllUploaded byNishant Kumaria
- SeqAn ManualUploaded byFiorella Denos
- Oracle 11g Results CacheUploaded byAbhay Joshi
- Bash VI Editing Mode (readline) Cheat SheetUploaded byPeteris Krumins
- LibFredo6 User Manual - English - V3.4 - 14 Sep 09Uploaded byOskars Elksnis
- mobilebillUploaded byYash Bansal
- SQL Server - Pivot e UnpivotUploaded bymarcel97rj
- File HandilingUploaded byraghu84taduri
- Area Based ADP Administrator GuideUploaded byhgolestani
- Mat Lab ProgrammingUploaded byxsaint1991
- DSALab02 CodeUploaded byMMali96
- Algorithms ProjectUploaded byMostafa Mahmoud
- Hotel Room Booking & availabilityUploaded bySathas Krishnan
- Db2 InterviewUploaded byRakesh Ahuja
- 6971XII Computer Science Paper-2014Uploaded byAbhijit Thorat
- Vi Cheat Sheet.pdfUploaded bynitinsinghsimps
- 07a40501 Principles of Programming LanguagesUploaded byAadarsh Singh
- Struts 1.x vs Struts 2.x Main DifferencesUploaded byAnonymous xtdji4IU
- java-the-legend.epubUploaded byJussimar Siqueira