Skip to content

Commit f77a88e

Browse files
committed
Dijkstra showcase, weighted graph implementation
1 parent ab00f01 commit f77a88e

File tree

2 files changed

+88
-1
lines changed

2 files changed

+88
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@
134134
### Graphs
135135
- [x] [Terminology and Representations of Graphs](http://www.techiedelight.com/terminology-and-representations-of-graphs/)
136136
- [x] Given a list of edges and tasked to build your own graph from the edges
137+
- [x] Implement Dijkstra’s algorithm (implemented Weighted Graph to showcase this)
138+
137139
### Matrix
138140

139141
### Trees
@@ -268,7 +270,6 @@
268270

269271

270272
- Graphs
271-
- [ ] Implement Dijkstra’s algorithm
272273
- [ ] Implement Topological sort
273274
- [ ] Implement Bellman-Ford algorithm
274275
- [ ] Implement Floyd-Warshall algorithm
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package dataStructures.graphs;
2+
3+
import java.util.*;
4+
5+
import dataStructures.graphs.CustomGraph.Vertex;
6+
7+
public class WeightedGraph {
8+
private Map<Vertex, List<Edge>> adjVertices;
9+
10+
public static class Edge {
11+
Vertex destination;
12+
int weight;
13+
14+
public Edge(Vertex destination, int weight) {
15+
this.destination = destination;
16+
this.weight = weight;
17+
}
18+
19+
@Override
20+
public String toString() {
21+
return "Edge{to=" + destination + ", weight=" + weight + '}';
22+
}
23+
}
24+
25+
public Map<Vertex, Integer> dijkstra(Vertex source) {
26+
Map<Vertex, Integer> distances = new HashMap<>();
27+
Map<Vertex, Vertex> previous = new HashMap<>();
28+
PriorityQueue<VertexDistance> pq = new PriorityQueue<>(Comparator.comparingInt(vd -> vd.distance));
29+
30+
for (Vertex vertex : adjVertices.keySet()) {
31+
distances.put(vertex, Integer.MAX_VALUE);
32+
}
33+
distances.put(source, 0);
34+
pq.add(new VertexDistance(source, 0));
35+
36+
while (!pq.isEmpty()) {
37+
VertexDistance current = pq.poll();
38+
Vertex u = current.vertex;
39+
40+
for (Edge edge : adjVertices.getOrDefault(u, Collections.emptyList())) {
41+
Vertex v = edge.destination;
42+
int newDist = distances.get(u) + edge.weight;
43+
if (newDist < distances.get(v)) {
44+
distances.put(v, newDist);
45+
previous.put(v, u);
46+
pq.add(new VertexDistance(v, newDist));
47+
}
48+
}
49+
}
50+
51+
return distances;
52+
}
53+
54+
private static class VertexDistance {
55+
Vertex vertex;
56+
int distance;
57+
58+
public VertexDistance(Vertex vertex, int distance) {
59+
this.vertex = vertex;
60+
this.distance = distance;
61+
}
62+
}
63+
64+
// demo
65+
66+
public static void main(String[] args) {
67+
Vertex a = new Vertex("A");
68+
Vertex b = new Vertex("B");
69+
Vertex c = new Vertex("C");
70+
Vertex d = new Vertex("D");
71+
72+
WeightedGraph graph = new WeightedGraph();
73+
graph.adjVertices = new HashMap<>();
74+
75+
graph.adjVertices.put(a, Arrays.asList(new Edge(b, 1), new Edge(c, 4)));
76+
graph.adjVertices.put(b, Arrays.asList(new Edge(c, 2), new Edge(d, 5)));
77+
graph.adjVertices.put(c, Arrays.asList(new Edge(d, 1)));
78+
graph.adjVertices.put(d, new ArrayList<>());
79+
80+
Map<Vertex, Integer> distances = graph.dijkstra(a);
81+
for (Map.Entry<Vertex, Integer> entry : distances.entrySet()) {
82+
System.out.println("Distance from A to " + entry.getKey().label + " = " + entry.getValue());
83+
}
84+
}
85+
86+
}

0 commit comments

Comments
 (0)