Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package bellmanFord;
import java.util.ArrayList;
import node.WeightedNode;

public class PathFindingByBellmanFord {

//Will store all the vertices
ArrayList<WeightedNode> nodeList = new ArrayList<WeightedNode>();

//Constructor
public PathFindingByBellmanFord(ArrayList<WeightedNode> nodeList) {
this.nodeList = nodeList;
for(WeightedNode aNode: nodeList){
aNode.setDistance(Integer.MAX_VALUE/10);
}
}//end of method


// BellmanFord from a source node
void bellmanFord(WeightedNode sourceNode) {
sourceNode.setDistance(0); // set source distance to zero

for (int i = 1; i < nodeList.size(); i++) { // repeat n-1 times
for (WeightedNode presentNode : nodeList) { // for each Vertex
for (WeightedNode neighbor : presentNode.getNeighbors()) { // for each Neighbor
// if distance of neighbor is greater than tentative new path then
// update distance of neighbor with new parent as presentNode
if (neighbor.getDistance() > (presentNode.getDistance() + presentNode.getWeightMap().get(neighbor))) {
neighbor.setDistance((presentNode.getDistance() + presentNode.getWeightMap().get(neighbor)));
neighbor.setParent(presentNode);
}
} // end of inner loop
} // end of mid loop
} // end of loop

System.out.println("Checking for Negative Cycle ...");
//for each edge check if update possible, if true then negative cycle is there report error
for(WeightedNode presentNode: nodeList) {
for(WeightedNode neighbor: presentNode.getNeighbors()) {
// if distance of neighbor is greater than tentative new path then we have a negative cycle, return from here..
if(neighbor.getDistance() > (presentNode.getDistance()+presentNode.getWeightMap().get(neighbor))) {
System.out.println("Negative cycle found: \n");
System.out.println("Vertex Name: " + neighbor.getName());
System.out.println("Old Distance: " + neighbor.getDistance());
int newDistance = presentNode.getDistance()+presentNode.getWeightMap().get(neighbor);
System.out.println("New distance: " + newDistance);
return;
}
}
}//end of loop
System.out.println("Negative cycle not found !");


//Print table of node with minimum distance and shortest path from source
System.out.println("\n\nPrinting Paths now: ");
for (WeightedNode nodeToCheck : nodeList) {
if (nodeToCheck.getDistance() != Integer.MAX_VALUE / 10) {
System.out.print("Node " + nodeToCheck + ", distance: " + nodeToCheck.getDistance() + ", Path: ");
pathPrint(nodeToCheck);
} else {
System.out.print("No path for node " + nodeToCheck);
}
System.out.println();
}//end of loop
}//end of method


//Printing path
private static void pathPrint(WeightedNode node) {
if(node.getParent()!=null) {
pathPrint(node.getParent());
System.out.print("->"+node);
}
else
System.out.print(node);
}//end of method


// add a weighted edge between two nodes
public void addWeightedEdge(int i, int j, int d) {
WeightedNode first = nodeList.get(i-1);
WeightedNode second = nodeList.get(j-1);
first.getNeighbors().add(second);
first.getWeightMap().put(second,d);
}//end of method

}//end of class


Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package bellmanFord;
import java.util.ArrayList;
import node.WeightedNode;

public class PathFindingByBellmanFordMain {

public static void main(String[] args) {

ArrayList<WeightedNode> nodeList = new ArrayList<>();

//create 5 nodes: A,B,C,D,E
for(int i=0;i<5; i++) {
nodeList.add(new WeightedNode(""+(char)(65+i)));
}

//Constructor
PathFindingByBellmanFord graph = new PathFindingByBellmanFord(nodeList);

graph.addWeightedEdge(1,3,6); //Add A-> C , weight 6
graph.addWeightedEdge(2,1,3); //Add B-> A , weight 3
graph.addWeightedEdge(1,4,6); //Add A-> D , weight 6
//graph.addWeightedEdge(1,4,-6); //Add A-> D , weight -6 TEST NEGATIVE WEIGHT HERE
graph.addWeightedEdge(4,3,1); //Add D-> C , weight 1
graph.addWeightedEdge(3,4,2); //Add C-> D , weight 2
graph.addWeightedEdge(4,2,1); //Add D-> B , weight 1
graph.addWeightedEdge(5,4,2); //Add E-> D , weight 2
graph.addWeightedEdge(5,2,4); //Add E-> B , weight 4

graph.bellmanFord(nodeList.get(4));

}//end of method

}//end of class
46 changes: 46 additions & 0 deletions Bellman Ford/bellmanford using java/node/BinaryNode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package node;

public class BinaryNode {
private int value;
private int height;
private BinaryNode left;
private BinaryNode right;

public int getHeight() {
return height;
}//end of method

public void setHeight(int height) {
this.height = height;
}//end of method

public int getValue() {
return value;
}//end of method

public void setValue(int value) {
this.value = value;
}//end of method

public BinaryNode getLeft() {
return left;
}//end of method

public void setLeft(BinaryNode left) {
this.left = left;
}//end of method

public BinaryNode getRight() {
return right;
}//end of method

public void setRight(BinaryNode right) {
this.right = right;
}//end of method

@Override
public String toString() {
return value + "";
}//end of method

}
46 changes: 46 additions & 0 deletions Bellman Ford/bellmanford using java/node/BinaryNodeWithParent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package node;

public class BinaryNodeWithParent {
private int value;
private BinaryNodeWithParent parent;
private BinaryNodeWithParent left;
private BinaryNodeWithParent right;

public int getValue() {
return value;
}

public void setValue(int value) {
this.value = value;
}

public BinaryNodeWithParent getParent() {
return parent;
}

public void setParent(BinaryNodeWithParent parent) {
this.parent = parent;
}

public BinaryNodeWithParent getLeft() {
return left;
}

public void setLeft(BinaryNodeWithParent left) {
this.left = left;
}

public BinaryNodeWithParent getRight() {
return right;
}

public void setRight(BinaryNodeWithParent right) {
this.right = right;
}

@Override
public String toString() {
return value+"";
}

}
38 changes: 38 additions & 0 deletions Bellman Ford/bellmanford using java/node/DoubleNode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package node;


public class DoubleNode {
private int value;
private DoubleNode next;
private DoubleNode prev;

public int getValue() {
return value;
}

public void setValue(int value) {
this.value = value;
}

public DoubleNode getNext() {
return next;
}

public void setNext(DoubleNode next) {
this.next = next;
}

public DoubleNode getPrev() {
return prev;
}

public void setPrev(DoubleNode prev) {
this.prev = prev;
}

@Override
public String toString() {
return value + "";
}

}
66 changes: 66 additions & 0 deletions Bellman Ford/bellmanford using java/node/GraphNode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package node;

import java.util.*;

public class GraphNode {
private String name;
private int index; //index is used to map this Node's name with index of Adjacency Matrix' cell#
private ArrayList<GraphNode> neighbors = new ArrayList<GraphNode>();
private boolean isVisited = false;
private GraphNode parent;

public GraphNode(String name, int index) {
this.name = name;
this.index = index;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getIndex() {
return index;
}

public void setIndex(int index) {
this.index = index;
}

public ArrayList<GraphNode> getNeighbors() {
return neighbors;
}

public void setNeighbors(ArrayList<GraphNode> neighbors) {
this.neighbors = neighbors;
}

public boolean isVisited() {
return isVisited;
}

public void setVisited(boolean isVisited) {
this.isVisited = isVisited;
}

public GraphNode getParent() {
return parent;
}

public void setParent(GraphNode parent) {
this.parent = parent;
}

public GraphNode(String name) {
this.name = name;
}

@Override
public String toString() {
return name ;
}

}
Loading