-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathDijkstrasAlgorighm.java
More file actions
115 lines (100 loc) · 3.63 KB
/
DijkstrasAlgorighm.java
File metadata and controls
115 lines (100 loc) · 3.63 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
import java.util.*;
class Graph_pq {
int dist[];
Set<Integer> visited;
PriorityQueue<Node> pqueue;
int V; // Number of vertices
List<List<Node> > adj_list;
//class constructor
public Graph_pq(int V) {
this.V = V;
dist = new int[V];
visited = new HashSet<Integer>();
pqueue = new PriorityQueue<Node>(V, new Node());
}
// Dijkstra's Algorithm implementation
public void algo_dijkstra(List<List<Node> > adj_list, int src_vertex)
{
this.adj_list = adj_list;
for (int i = 0; i < V; i++)
dist[i] = Integer.MAX_VALUE;
// first add source vertex to PriorityQueue
pqueue.add(new Node(src_vertex, 0));
// Distance to the source from itself is 0
dist[src_vertex] = 0;
while (visited.size() != V) {
// u is removed from PriorityQueue and has min distance
int u = pqueue.remove().node;
// add node to finalized list (visited)
visited.add(u);
graph_adjacentNodes(u);
}
}
// this methods processes all neighbours of the just visited node
private void graph_adjacentNodes(int u) {
int edgeDistance = -1;
int newDistance = -1;
// process all neighbouring nodes of u
for (int i = 0; i < adj_list.get(u).size(); i++) {
Node v = adj_list.get(u).get(i);
// proceed only if current node is not in 'visited'
if (!visited.contains(v.node)) {
edgeDistance = v.cost;
newDistance = dist[u] + edgeDistance;
// compare distances
if (newDistance < dist[v.node])
dist[v.node] = newDistance;
// Add the current vertex to the PriorityQueue
pqueue.add(new Node(v.node, dist[v.node]));
}
}
}
}
class Main{
public static void main(String arg[]) {
int V = 6;
int source = 0;
// adjacency list representation of graph
List<List<Node> > adj_list = new ArrayList<List<Node> >();
// Initialize adjacency list for every node in the graph
for (int i = 0; i < V; i++) {
List<Node> item = new ArrayList<Node>();
adj_list.add(item);
}
// Input graph edges
adj_list.get(0).add(new Node(1, 5));
adj_list.get(0).add(new Node(2, 3));
adj_list.get(0).add(new Node(3, 2));
adj_list.get(0).add(new Node(4, 3));
adj_list.get(0).add(new Node(5, 3));
adj_list.get(2).add(new Node(1, 2));
adj_list.get(2).add(new Node(3, 3));
// call Dijkstra's algo method
Graph_pq dpq = new Graph_pq(V);
dpq.algo_dijkstra(adj_list, source);
// Print the shortest path from source node to all the nodes
System.out.println("The shorted path from source node to other nodes:");
System.out.println("Source\t\t" + "Node#\t\t" + "Distance");
for (int i = 0; i < dpq.dist.length; i++)
System.out.println(source + " \t\t " + i + " \t\t " + dpq.dist[i]);
}
}
// Node class
class Node implements Comparator<Node> {
public int node;
public int cost;
public Node() { } //empty constructor
public Node(int node, int cost) {
this.node = node;
this.cost = cost;
}
@Override
public int compare(Node node1, Node node2)
{
if (node1.cost < node2.cost)
return -1;
if (node1.cost > node2.cost)
return 1;
return 0;
}
}