Professional Documents
Culture Documents
CODE:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.PriorityQueue;
class Edge {
this.to = to;
this.weight = weight;
}
}
class Graph {
int vertices;
List<List<Edge>> adjacencyList;
this.vertices = vertices;
adjacencyList.add(new ArrayList<>());
Integer.MAX_VALUE);
dist[start] = 0;
Integer.compare(a.weight, b.weight));
current = minHeap.poll();
continue;
}
dist[neighbor.to] = newWeight;
return dist;
int vertices = 5;
graph.addEdge(0, 1, 2);
graph.addEdge(0, 3, 1);
graph.addEdge(1, 2, 3);
graph.addEdge(1, 3, 2);
graph.addEdge(2, 4, 1);
graph.addEdge(3, 4, 4);
int startVertex = 0;
OUTPUT:
To Vertex 0: 0
To Vertex 1: 2
To Vertex 2: 5
To Vertex 3: 1
To Vertex 4: 6