Advertisement
Guest User

bellman ford

a guest
Oct 26th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | None | 0 0
  1. import java.util.Random;
  2. import java.util.Scanner;
  3.  
  4. public class Menu {
  5.  
  6. public static void main(String[] args) {
  7. int nodesource = 0;
  8. System.out.println("Type how many nodes you want");
  9. Scanner s = new Scanner(System.in);
  10. Random rand = new Random();
  11. int nodesize = s.nextInt();
  12. int[] nodes = createNodes(nodesize);
  13. int[] finalnode = new int[nodesize];
  14. System.out.println("|-----------------------------------------------------------------------------------------------------|");
  15. System.out.println("You start at 0 meters from the source node. ");
  16. for(int i = nodesize-1; i >= 0 ; i--){
  17. finalnode[i] = rand.nextInt(12+1);
  18. System.out.println(nodes[i] + " meters to reach the node " + (i+1) + " and " + finalnode[i] + " meters to reach the source node");
  19. }
  20. System.out.println("|-----------------------------------------------------------------------------------------------------|");
  21. System.out.println(" ");
  22. System.out.println("The shortest route is... ");
  23. int shortest[] = shortestRoute(nodesize,nodes,finalnode);
  24. System.out.println("Route " + shortest[1] + " which is " + shortest[0] + " meters from the source node.");
  25.  
  26.  
  27. }
  28.  
  29. public static int[] shortestRoute(int nodesize, int nodes[], int finalnode[]){
  30. int[] route = new int[2];
  31. route[0] = 1000;
  32.  
  33. for (int i = nodesize-1; i >=0 ; i--){
  34.  
  35. if(nodes[i] + finalnode[i] < route[0]){
  36. route[1] = i + 1;
  37. route[0] = nodes[i] + finalnode[i];
  38.  
  39. }
  40. }
  41. return route;
  42. }
  43.  
  44.  
  45. public static int[] createNodes(int nodesize){
  46. int[] nodes = new int[nodesize];
  47. Random rand = new Random();
  48.  
  49. for (int i = nodesize-1; i >=0 ; i--){
  50. nodes[i] = rand.nextInt(16+1);
  51. }
  52.  
  53. return nodes;
  54. }
  55.  
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement