Skip to content

Commit faec94f

Browse files
Implemented Dijkstra's Algorithm in Java with an Adjacency List
1 parent 5165db5 commit faec94f

File tree

1 file changed

+136
-0
lines changed

1 file changed

+136
-0
lines changed
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
import javafx.util.Pair;
2+
import java.util.LinkedList;
3+
import java.util.Comparator;
4+
import java.util.PriorityQueue;
5+
6+
//use when you want to find the shortest path between two vertices in a weighted, undirected graph
7+
//adjacency list implementation is best when the graph is sparse
8+
public class Dijkstra_Adjacency_List {
9+
10+
//class to represent edges in the graph
11+
static class E {
12+
int src;
13+
int dest;
14+
int weight;
15+
16+
public E(int src, int dest, int weight) {
17+
this.src = src;
18+
this.dest = dest;
19+
this.weight = weight;
20+
}
21+
}
22+
23+
//class to represent the graph
24+
static class Graph {
25+
int vertices;
26+
27+
//creates an adjacency list
28+
LinkedList<E>[] adjList;
29+
30+
Graph(int vertices) {
31+
//creates the adjacency list
32+
this.vertices = vertices;
33+
adjList = new LinkedList[vertices];
34+
//initializes the adjacency list
35+
for (int i = 0; i <vertices ; i++) {
36+
adjList[i] = new LinkedList<>();
37+
}
38+
}
39+
40+
//addEdge method allows user to add another edge to the graph
41+
public void addEdge(int src, int dest, int weight) {
42+
E newEdge = new E(src, dest, weight);
43+
adjList[src].addFirst(newEdge);
44+
newEdge= new E(dest, src, weight);
45+
adjList[dest].addFirst(newEdge);
46+
}
47+
48+
public void dijkstra(int src){
49+
50+
//array of booleans to tell if a vertex has been visited or not
51+
boolean[] visited = new boolean[vertices];
52+
53+
//stores current shortest distance to each vertex from src
54+
int [] distances = new int[vertices];
55+
56+
//initializes the shortest distance to each vertex to infinity
57+
for (int i = 0; i <vertices ; i++) {
58+
distances[i] = Integer.MAX_VALUE;
59+
}
60+
61+
//initializes priority queue and overrides comparator so that pairs can be compared
62+
PriorityQueue<Pair<Integer, Integer>> priorityQueue = new PriorityQueue<>(vertices, new Comparator<Pair<Integer, Integer>>() {
63+
@Override
64+
public int compare(Pair<Integer, Integer> pair1, Pair<Integer, Integer> pair2) {
65+
//sort using distance values
66+
int k1= pair1.getKey();
67+
int k2 = pair2.getKey();
68+
return k1-k2;
69+
}
70+
});
71+
72+
//creates the pair for the first vertex with itself
73+
distances[0] = 0;
74+
Pair<Integer, Integer> p0 = new Pair<>(distances[0],0);
75+
//add it to pq
76+
priorityQueue.offer(p0);
77+
78+
//while priority queue is not empty, extract the minimum pair from the priority queue
79+
while(!priorityQueue.isEmpty()) {
80+
Pair<Integer, Integer> pair = priorityQueue.poll();
81+
82+
//the vertex in the extracted pair
83+
int v = pair.getValue();
84+
//sets the vertex to visited
85+
if (visited[v] == false) {
86+
visited[v] = true;
87+
88+
//iterates through all the adjacent vertices and updates the keys
89+
LinkedList<E> list = adjList[v];
90+
for (int i = 0; i < list.size(); i++) {
91+
E edge = list.get(i);
92+
int dest = edge.dest;
93+
//updates the adjacent vertices
94+
if (visited[dest] == false) {
95+
//check if new distance is shorter than old distance from src to each adj vertex, and update as necessary
96+
int newDistance = distances[v] + edge.weight;
97+
int currDistance = distances[dest];
98+
if (currDistance > newDistance) {
99+
Pair<Integer, Integer> p = new Pair<>(newDistance, dest);
100+
priorityQueue.offer(p);
101+
distances[dest] = newDistance;
102+
}
103+
}
104+
}
105+
}
106+
107+
//calls the print method
108+
printDijkstra(distances, src);
109+
}
110+
}
111+
112+
//creates an example graph and calls Dijkstra's Algorithm
113+
public static void main(String[] args) {
114+
int vertices = 6;
115+
Graph graph = new Graph(vertices);
116+
graph.addEdge(0, 1, 3);
117+
graph.addEdge(0, 3, 4);
118+
graph.addEdge(1, 5, 1);
119+
graph.addEdge(1, 4, 7);
120+
graph.addEdge(2, 3, 2);
121+
graph.addEdge(3, 2, 4);
122+
graph.addEdge(3, 4, 1);
123+
graph.addEdge(4, 5, 6);
124+
graph.dijkstra(0);
125+
}
126+
127+
128+
//prints the shortest distance between the source vertex and each other vertex in the graph
129+
public void printDijkstra(int[] distances, int src){
130+
for (int i = 0; i < distances.length; i++) {
131+
System.out.println("src vertex: " + src + " destination vertex: " + i +
132+
" shortest path distance: " + distances[i]);
133+
}
134+
}
135+
}
136+
}

0 commit comments

Comments
 (0)