# graphs – Issue regarding time complexity of Dijkstra’s Algorithm

So I tried writing Dijkstra’s Algorithm for the following graph. I used Priority Queue so time complexity could be less than V^2 ( where ‘V’ is the total number of vertices) My approach ->

``````public static class Pair implements Comparator<Pair> {
int vertex;
int weight;

Pair(){}

Pair(int vertex,int weight){
this.vertex=vertex;
this.weight=weight;
}

@Override
public int compare(Pair pairOne, Pair pairTwo) {
return Integer.compare(pairOne.weight,pairTwo.weight);
}
}

public static void shortestPath(ArrayList<ArrayList<Pair>> adjList,int vertices,int source){
int count=0;
int() distance=new int(vertices);
boolean() spt=new boolean(vertices);
for(int i=0;i<vertices;i++){
distance(i)=Integer.MAX_VALUE;
}
distance(source)=0;
spt(source)=true;
PriorityQueue<Pair> ourPriorityQueue=new PriorityQueue<>(vertices,new Pair());

while(!ourPriorityQueue.isEmpty()){
Pair poppedElement=ourPriorityQueue.poll();
spt(poppedElement.vertex)=true;
count++;
if(!spt(pair.vertex) && distance(pair.vertex)>distance(poppedElement.vertex)+pair.weight){
distance(pair.vertex)=distance(poppedElement.vertex)+pair.weight;