forked from dark-knight009/Programming-Helpers
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDijkstraAlgorithm.java
More file actions
163 lines (162 loc) · 3.66 KB
/
Copy pathDijkstraAlgorithm.java
File metadata and controls
163 lines (162 loc) · 3.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package GraphAlgorithms;
import java.util.*;
class Distpar
{
public int distance;
public int parentVert;
public Distpar(int pv,int d)
{
distance=d;
parentVert=pv;
}
}
class Vertex2
{
public char label;
public boolean isinTree;
public Vertex2(char lab)
{
label=lab;
isinTree=false;
}
}
class Graph2
{
private final int maxVerts=20;
private final int Infinity=100000;
private Vertex2 []vertexList;
private int [][]adjMat;
private int nverts;
private int nTree;
private int currentVert;
private Distpar sPath[];
private int startToCurrent;
public Graph2()
{
vertexList=new Vertex2[maxVerts];
adjMat=new int[maxVerts][maxVerts];
nverts=0;
for(int i=0;i<maxVerts;i++)
{
for(int j=0;j<maxVerts;j++)
{
adjMat[i][j]=Infinity;
}
}
sPath=new Distpar[maxVerts];
}
public void addVertex(char lab)
{
vertexList[nverts++]=new Vertex2(lab);
}
public void addEdge(int start,int end,int weight)
{
adjMat[start][end]=weight;
}
public void path()
{
int startTree=0;
vertexList[startTree].isinTree=false;
nTree++;
for(int i=0;i<nverts;i++)
{
int temp=adjMat[startTree][i];
sPath[i]=new Distpar(startTree,temp);
}
while(nTree<nverts)
{
currentVert=startTree;
int indexMin=getMin();
int mindis=sPath[indexMin].distance;
if(mindis==Infinity)
{
System.out.println("Unreacheable vertices");
break;
}
else
{
currentVert=indexMin;
startToCurrent=sPath[indexMin].distance;
}
vertexList[currentVert].isinTree=true;
nTree++;
adjustpath();
}
displaypath();
nTree=0;
for(int i=0;i<nverts;i++)
vertexList[i].isinTree=false;
}
public int getMin()
{
int minDist =Infinity;
int indexMin=0;
for(int i=1;i<nverts;i++)
{
if(!vertexList[i].isinTree&&sPath[i].distance<minDist)
{
minDist=sPath[i].distance;
indexMin=i;
}
}
return indexMin;
}
public void adjustpath()
{
int column=1;
while(column<nverts)
{
if(vertexList[column].isinTree)
{
column++;
continue;
}
int currentToFringe=adjMat[currentVert][column];
int startToFringe=currentToFringe+startToCurrent;
int sPathDist=sPath[column].distance;
if(startToFringe<sPathDist)
{
sPath[column].parentVert=currentVert;
sPath[column].distance=startToFringe;
}
column++;
}
}
public void displaypath()
{
for(int i=0;i<nverts;i++)
{
System.out.print(vertexList[i].label+"=");
if(sPath[i].distance==Infinity)
System.out.print("Invalid");
else
System.out.print(sPath[i].distance);
char parent=vertexList[sPath[i].parentVert].label;
System.out.print("("+parent+")");
}
System.out.println("");
}
}
public class DijkstraAlgorithm
{
public static void main(String args[])
{
Graph2 theGraph=new Graph2();
theGraph.addVertex('A');
theGraph.addVertex('B');
theGraph.addVertex('C');
theGraph.addVertex('D');
theGraph.addVertex('E');
theGraph.addEdge(0,1,50);
theGraph.addEdge(0,3,80);
theGraph.addEdge(1,2,60);
theGraph.addEdge(1,3,90);
theGraph.addEdge(2,4,40);
theGraph.addEdge(3,2,20);
theGraph.addEdge(3,4,70);
theGraph.addEdge(4,1,50);
System.out.println("Minimum spanning tree");
theGraph.path();
System.out.println();
}
}