package com.data.struct;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.Set;

public class BellmanFord {
private Node [][]graphic;
private Node s;
private Set<Node> nextNodes=new HashSet<Node>();
private Set<Node> addList=new HashSet<Node>();
private Set<Node> removeList=new HashSet<Node>();

public BellmanFord(int v,int e){
graphic=new Node[v][v];
for(int i=0;i<e;i++){
int v1=new Random().nextInt(v);
int v2=new Random().nextInt(v);
Node node=new Node();
node.d=Integer.MAX_VALUE-1000;
node.w=new Random().nextInt(e);
node.start=v1;
node.end=v2;
graphic[v1][v2]=node;
}
for(int i=0;i<v;i++){
if(graphic[i][i]==null){
Node node=new Node();
node.d=Integer.MAX_VALUE-1000;
node.w=new Random().nextInt(e);
node.start=i;
node.end=i;
graphic[i][i]=node;
}
}
s=graphic[0][0];
s.d=0;
}

public void relex(Node u,Node v){
if(graphic[v.end][v.end].d>u.d+graphic[u.start][v.start].w){
graphic[v.end][v.end].d=u.d+graphic[u.start][v.start].w;
graphic[v.end][v.end].parent=u;
addList.add(graphic[v.end][v.end]);
System.out.println(graphic[v.end][v.end].start+"=>"+u.start);
}
}

public boolean bellmanFord(){
nextNodes.add(s);
for(int k=0;k<graphic.length;k++){
if(k!=s.start&&graphic[s.start][k]!=null){
relex(s,graphic[k][k]);
}
}
nextNodes.addAll(addList);
nextNodes.remove(s);
while(nextNodes.size()>0){
addList.clear();removeList.clear();
Iterator<Node> it=nextNodes.iterator();
while(it.hasNext()){
Node node=it.next();
for(int k=0;k<graphic.length;k++){
if(k!=node.start&&graphic[node.start][k]!=null){
relex(node,graphic[k][k]);
}
}
removeList.add(node);
}
nextNodes.removeAll(removeList);
nextNodes.addAll(addList);

}

for(int j=0;j<graphic.length;j++){
for(int k=0;k<graphic.length;k++){
if(j!=k){
if(graphic[j][k]!=null&&graphic[k][k].d> graphic[j][j].d+graphic[j][k].w){
return false;
}
}
}
}
return true;

}

public void print(){
for(int i=0;i<graphic.length;i++){
for(int j=0;j<graphic[i].length;j++){
if(graphic[i][j]==null){
System.out.print(-1+"|"+-1+"|"+-1+" ");
}else{
System.out.print(graphic[i][j].start+"|"+graphic[i][j].end+"|"+graphic[i][j].w+" ");
}

}
System.out.println();
}
}

public void printPath(){
List<Integer> indexList=new ArrayList<Integer>();
List<Integer> removeList=new ArrayList<Integer>();
List<Integer> addList=new ArrayList<Integer>();
indexList.add(0);
while(indexList.size()>0){
removeList.clear();
addList.clear();
for(int x=0;x<indexList.size();x++){
int l=indexList.get(x);
removeList.add(l);
for(int j=0;j<graphic.length;j++){
if(graphic[j][j].parent==graphic[l][l]){
graphic[l][l].children.add(graphic[j][j]);
addList.add(j);
}
}

}
indexList.removeAll(removeList);
indexList.addAll(addList);
}

Node h = this.s;
this.print(0, h,h);



System.out.println();
}


private void print(int level,Node parent, Node node){
for (int i = 0; i < level; i++) {
System.out.format(" ");
}
System.out.format("|");
if(parent!=node){
System.out.print("("+graphic[parent.start][node.end].w+")");
for (int i = 0; i < level; i++) {
System.out.format("-");
}
}else{
for (int i = 0; i < level; i++) {
System.out.format("-");
}
}

System.out.format("%d%n", node.start);
List<Node> children = node.children;
for(int i=0;i<children.size();i++){
print(level+1,node,children.get(i));
}

}

private class Node{
private int d;
private int w;
private int start;
private int end;
private Node parent;
private List<Node> children=new ArrayList<Node>();
}

public static void main(String[] args) {
BellmanFord b=new BellmanFord(5,20);
b.print();
System.out.println(b.bellmanFord());
b.printPath();

}

}