Skip to content

Instantly share code, notes, and snippets.

@viniru
Last active July 5, 2018 07:58
Show Gist options
  • Save viniru/bb06b5e09443946e14ceda7628815747 to your computer and use it in GitHub Desktop.
Save viniru/bb06b5e09443946e14ceda7628815747 to your computer and use it in GitHub Desktop.
Dijkshtra's algorithm using Min Heap in java. input : graph contains exactly 200 elements represented in terms of adjacency list.format : vertex space neighbour_vertex1, weight neighbour_vertex2,weight so on. every new line contains a list for one vertex
import java.util.*;
import java.lang.Math;
public class Dijkshtra{
int xx = 201;
HashMap<Integer,ArrayList<Pair>> al; //adjacency list
boolean X[] = new boolean[xx];
MinHeap heap = new MinHeap();
int[] distance = new int[xx];
Dijkshtra()
{
al = new HashMap<Integer,ArrayList<Pair>>();
int c=-1;
while(++c < xx - 1)
X[c] = false;
for(int i=1;i<=xx - 1;i++) //first element of the array is not used
{
distance[i] = 1000000;
}
distance[1]=0;
}
void addVertex(int val)
{
X[val] = true;
ArrayList<Pair> ar = al.get(val);
int distx = distance[val];
for(Pair p : ar)
{
int vertex = p.getOne();
if(X[vertex] == true)continue;
int pos = heap.position(vertex);
int dist = p.getTwo();
if(distance[vertex] > distx+dist)
{
distance[vertex] = distx+dist ;
/*heap.delete(heap.position(vertex),vertex);
Pair npair = new Pair(vertex,distx+dist);
heap.insert(npair);
*/
heap.set(pos,distx+dist);
heap.reset(pos);
}
}
}
void Dstart()
{
addVertex(1);
HashSet<Integer> hs = new HashSet<>();
while(heap.size()>0)
{
Pair p = heap.extractMin();
addVertex(p.getOne());
heap.delete(0,p.getOne());
}
}
public static void main(String []args){
Scanner sc = new Scanner(System.in);
Dijkshtra dj = new Dijkshtra();
while(sc.hasNextLine())
{
String s = sc.nextLine();
String str[] = s.split(" ");
ArrayList<Pair> ar = new ArrayList<>();
int map = Integer.parseInt(str[0]);
for(int i=1;i < str.length ; i++)
{
String pr[] = str[i].split(",");
Pair p = new Pair(Integer.parseInt(pr[0]),Integer.parseInt(pr[1]));
ar.add(p);
}
dj.al.put(map,ar);
}
dj.heap.buildHeap();
dj.Dstart();
/*for(int i=1;i<=9;i++)
System.out.print(dj.distance[i]+",");*/
System.out.print(dj.distance[7]+",");
System.out.print(dj.distance[37]+",");
System.out.print(dj.distance[59]+",");
System.out.print(dj.distance[82]+",");
System.out.print(dj.distance[99]+",");
System.out.print(dj.distance[115]+",");
System.out.print(dj.distance[133]+",");
System.out.print(dj.distance[165]+",");
System.out.print(dj.distance[188]+",");
System.out.print(dj.distance[197]+",");
}
}
class MinHeap {
int xx = 201;
ArrayList<Pair> heap = new ArrayList<>();
int pos[] = new int[xx];
/*void insert(Pair p)
{
heap.add(p);
pos[p.getOne()]=heap.size()-1;
int parent = heap.size()-1;
int child = parent;
while(parent > 0)
{
parent=(int)Math.ceil((float)child/2 - 1);
if(heap.get(parent).getTwo() > heap.get(child).getTwo())
{
Pair ppp = heap.get(child);
pos[heap.get(parent).getOne()]=child;
pos[heap.get(child).getOne()]=parent;
heap.set(child,heap.get(parent));
heap.set(parent,ppp);
child = parent;
}
else break;
}
heapify(child);
}
*/
void buildHeap()
{
int i= 1;
while(++i <= xx - 1)
{
Pair p = new Pair(i,1000000);
heap.add(p);
pos[i]=i-2;
}
}
void set(int index,int value)
{
heap.get(index).setTwo(value);
}
void reset(int index)
{
int parent = index ;
while(parent > 0)
{
parent = (int)Math.ceil((float)index/2 - 1);
if(heap.get(parent).getTwo() > heap.get(index).getTwo())
{
Pair p = heap.get(parent);
pos[heap.get(parent).getOne()] = index;
pos[heap.get(index).getOne()] = parent;
heap.set(parent,heap.get(index));
heap.set(index,p);
index = parent;
}
else break;
}
heapify(parent);
}
void heapify(int position)
{
int i = position;
int size = size();
int deadline = size/2;
int lchild;
int rchild;
int low;
while(i < deadline)
{
low = i;
lchild=2*i+1;
rchild=2*i+2;
if(rchild < size && heap.get(rchild).getTwo() < heap.get(low).getTwo())
low=rchild;
if(heap.get(lchild).getTwo() < heap.get(low).getTwo())
low=lchild;
if(i==low)
break;
pos[heap.get(i).getOne()]=low;
pos[heap.get(low).getOne()]=i;
Pair temp = heap.get(i);
heap.set(i,heap.get(low));
heap.set(low,temp);
i = low;
}
}
Pair extractMin()
{
return heap.get(0);
}
int position(int val)
{
return pos[val];
}
int size()
{
return heap.size();
}
int Weight(int position)
{
return heap.get(position).getTwo();
}
void delete(int index,int vertex)
{
pos[vertex] = -1;
int size = heap.size();
heap.set(index,heap.get(size -1));
pos[heap.get(index).getOne()] = index;
heap.remove(size - 1 );
for(int i=2;i<=200;i++)
heapify(index);
}
boolean check()
{
int size = heap.size();
while(--size > 0)
{
if(heap.get(size).getTwo() < heap.get((int)Math.ceil((float)size/2-1)).getTwo() )
return false;
}
return true;
}
}
class Pair{
private int one;
private int two;
Pair()
{
one=0;
two=0;
}
Pair(int one,int two)
{
this.one = one;
this.two = two;
}
void setOne(int val){this.one = val;}
void setTwo(int val){this.two = val;}
int getOne(){return one;}
int getTwo(){return two;}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment