1
+ /*
2
+ Copyright (C) Deepali Srivastava - All Rights Reserved
3
+ This code is part of DSA course available on CourseGalaxy.com
4
+ */
5
+
6
+ package Dijkstras ;
7
+
8
+ public class DirectedWeightedGraph
9
+ {
10
+ public final int MAX_VERTICES = 30 ;
11
+
12
+ int n ;
13
+ int e ;
14
+ int [][] adj ;
15
+ Vertex [] vertexList ;
16
+
17
+ private static final int TEMPORARY = 1 ;
18
+ private static final int PERMANENT = 2 ;
19
+ private static final int NIL = -1 ;
20
+ private static final int INFINITY = 99999 ;
21
+
22
+ public DirectedWeightedGraph ()
23
+ {
24
+ adj = new int [MAX_VERTICES ][MAX_VERTICES ];
25
+ vertexList = new Vertex [MAX_VERTICES ];
26
+ }
27
+
28
+ private void dijkstra (int s )
29
+ {
30
+ int v ,c ;
31
+
32
+ for (v =0 ; v <n ; v ++)
33
+ {
34
+ vertexList [v ].status = TEMPORARY ;
35
+ vertexList [v ].pathLength = INFINITY ;
36
+ vertexList [v ].predecessor = NIL ;
37
+ }
38
+
39
+ vertexList [s ].pathLength = 0 ;
40
+
41
+ while (true )
42
+ {
43
+ c =tempVertexMinPL ();
44
+
45
+ if (c ==NIL )
46
+ return ;
47
+
48
+ vertexList [c ].status = PERMANENT ;
49
+
50
+ for (v =0 ; v <n ; v ++)
51
+ {
52
+ if (isAdjacent (c ,v ) && vertexList [v ].status == TEMPORARY )
53
+ if ( vertexList [c ].pathLength + adj [c ][v ] < vertexList [v ].pathLength )
54
+ {
55
+ vertexList [v ].predecessor = c ;
56
+ vertexList [v ].pathLength = vertexList [c ].pathLength + adj [c ][v ];
57
+ }
58
+ }
59
+ }
60
+ }
61
+
62
+ private int tempVertexMinPL ( )
63
+ {
64
+ int min =INFINITY ;
65
+ int x =NIL ;
66
+ for (int v =0 ; v <n ; v ++)
67
+ {
68
+ if (vertexList [v ].status == TEMPORARY && vertexList [v ].pathLength < min )
69
+ {
70
+ min =vertexList [v ].pathLength ;
71
+ x =v ;
72
+ }
73
+ }
74
+ return x ;
75
+ }
76
+
77
+ public void findPaths (String source )
78
+ {
79
+ int s = getIndex (source );
80
+
81
+ dijkstra (s );
82
+
83
+ System .out .println ("Source Vertex : " + source + "\n " );
84
+ for (int v =0 ; v <n ; v ++)
85
+ {
86
+ System .out .println ("Destination Vertex : " + vertexList [v ]);
87
+ if ( vertexList [v ].pathLength == INFINITY )
88
+ System .out .println ("There is no path from " + source + " to vertex " + vertexList [v ] + "\n " );
89
+ else
90
+ findPath (s ,v );
91
+ }
92
+ }
93
+
94
+ private void findPath (int s , int v )
95
+ {
96
+ int i ,u ;
97
+ int [] path = new int [n ];
98
+ int sd =0 ;
99
+ int count =0 ;
100
+
101
+ while (v !=s )
102
+ {
103
+ count ++;
104
+ path [count ] = v ;
105
+ u = vertexList [v ].predecessor ;
106
+ sd += adj [u ][v ];
107
+ v =u ;
108
+ }
109
+ count ++;
110
+ path [count ]=s ;
111
+
112
+ System .out .print ("Shortest Path is : " );
113
+ for (i =count ; i >=1 ; i --)
114
+ System .out .print (path [i ] + " " );
115
+ System .out .println ("\n Shortest distance is : " + sd + "\n " );
116
+ }
117
+
118
+
119
+ private int getIndex (String s )
120
+ {
121
+ for (int i =0 ; i <n ; i ++)
122
+ if (s .equals (vertexList [i ].name ))
123
+ return i ;
124
+ throw new RuntimeException ("Invalid Vertex" );
125
+ }
126
+
127
+ public void insertVertex (String name )
128
+ {
129
+ vertexList [n ++] = new Vertex (name );
130
+ }
131
+
132
+
133
+ private boolean isAdjacent (int u , int v )
134
+ {
135
+ return (adj [u ][v ]!=0 );
136
+ }
137
+
138
+ /*Insert an edge (s1,s2) */
139
+ public void insertEdge (String s1 , String s2 , int wt )
140
+ {
141
+ int u = getIndex (s1 );
142
+ int v = getIndex (s2 );
143
+ if (u ==v )
144
+ throw new IllegalArgumentException ("Not a valid edge" );
145
+
146
+ if (adj [u ][v ] !=0 )
147
+ System .out .print ("Edge already present" );
148
+ else
149
+ {
150
+ adj [u ][v ]=wt ;
151
+ e ++;
152
+ }
153
+ }
154
+
155
+ }
156
+
157
+
158
+
159
+
0 commit comments