-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create Dijkstra.java with implementation of Dijkstra's Algorithm in J…
…ava, featuring a priority queue for shortest path determination and a HashMap for the adjacency list
- Loading branch information
Showing
1 changed file
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import java.util.*; | ||
|
||
class Graph { | ||
private Map<Integer, List<Pair>> adjList = new HashMap<>(); | ||
|
||
private static class Pair { | ||
int vertex; | ||
int weight; | ||
Pair(int vertex, int weight) { | ||
this.vertex = vertex; | ||
this.weight = weight; | ||
} | ||
} | ||
|
||
public void addEdge(int u, int v, int weight) { | ||
adjList.putIfAbsent(u, new ArrayList<>()); | ||
adjList.get(u).add(new Pair(v, weight)); | ||
adjList.putIfAbsent(v, new ArrayList<>()); | ||
adjList.get(v).add(new Pair(u, weight)); // For undirected graph | ||
} | ||
|
||
public Map<Integer, Integer> dijkstra(int start) { | ||
PriorityQueue<Pair> queue = new PriorityQueue<>(Comparator.comparingInt(a -> a.weight)); | ||
Map<Integer, Integer> distances = new HashMap<>(); | ||
for (int vertex : adjList.keySet()) { | ||
distances.put(vertex, Integer.MAX_VALUE); | ||
} | ||
distances.put(start, 0); | ||
queue.add(new Pair(start, 0)); | ||
|
||
while (!queue.isEmpty()) { | ||
Pair current = queue.poll(); | ||
for (Pair neighbor : adjList.getOrDefault(current.vertex, new ArrayList<>())) { | ||
int newDist = distances.get(current.vertex) + neighbor.weight; | ||
if (newDist < distances.get(neighbor.vertex)) { | ||
distances.put(neighbor.vertex, newDist); | ||
queue.add(new Pair(neighbor.vertex, newDist)); | ||
} | ||
} | ||
} | ||
return distances; | ||
} | ||
|
||
// Example usage | ||
public static void main(String[] args) { | ||
Graph g = new Graph(); | ||
g.addEdge(0, 1, 10); | ||
g.addEdge(0, 2, 5); | ||
g.addEdge(1, 2, 2); | ||
g.addEdge(1, 3, 1); | ||
g.addEdge(2, 1, 3); | ||
g.addEdge(2, 3, 9); | ||
g.addEdge(2, 4, 2); | ||
g.addEdge(3, 4, 6); | ||
g.addEdge(4, 3, 4); | ||
|
||
Map<Integer, Integer> distances = g.dijkstra(0); | ||
for (Map.Entry<Integer, Integer> entry : distances.entrySet()) { | ||
System.out.println("Distance from 0 to " + entry.getKey() + " is " + entry.getValue()); | ||
} | ||
} | ||
} |