Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
This repository was archived by the owner on Oct 29, 2020. It is now read-only.

Latest commit

 

History

History
History
53 lines (45 loc) · 1.74 KB

File metadata and controls

53 lines (45 loc) · 1.74 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
public class Dijkstra {
// Dijkstra's algorithm to find shortest path from s to all other nodes
public static int [] dijkstra (WeightedGraph G, int s) {
final int [] dist = new int [G.size()]; // shortest known distance from "s"
final int [] pred = new int [G.size()]; // preceeding node in path
final boolean [] visited = new boolean [G.size()]; // all false initially
for (int i=0; i<dist.length; i++) {
dist[i] = Integer.MAX_VALUE;
}
dist[s] = 0;
for (int i=0; i<dist.length; i++) {
final int next = minVertex (dist, visited);
visited[next] = true;
// The shortest path to next is dist[next] and via pred[next].
final int [] n = G.neighbors (next);
for (int j=0; j<n.length; j++) {
final int v = n[j];
final int d = dist[next] + G.getWeight(next,v);
if (dist[v] > d) {
dist[v] = d;
pred[v] = next;
}
}
}
return pred; // (ignore pred[s]==0!)
}
private static int minVertex (int [] dist, boolean [] v) {
int x = Integer.MAX_VALUE;
int y = -1; // graph not connected, or no unvisited vertices
for (int i=0; i<dist.length; i++) {
if (!v[i] && dist[i]<x) {y=i; x=dist[i];}
}
return y;
}
public static void printPath (WeightedGraph G, int [] pred, int s, int e) {
final java.util.ArrayList path = new java.util.ArrayList();
int x = e;
while (x!=s) {
path.add (0, G.getLabel(x));
x = pred[x];
}
path.add (0, G.getLabel(s));
System.out.println (path);
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.