Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.69 KB | None | 0 0
  1. package practice;
  2.  
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5. import java.util.*;
  6.  
  7. class GraphTraversal{
  8. private int V; // No. of vertices
  9. private LinkedList<Integer> adj[]; //To store a adjacency list
  10.  
  11. /*
  12. * Initialize a graph
  13. */
  14. GraphTraversal(int v) {
  15. V = v;
  16. adj = new LinkedList[V+1];
  17.  
  18. for (int i = 0; i <= V; i++) {
  19. adj[i] = new LinkedList<Integer>();
  20. }
  21. }
  22.  
  23. /*
  24. * Add an edge into a graph
  25. */
  26. void addEdge(int v, int w) {
  27. adj[v].add(w);
  28. }
  29.  
  30. /*
  31. * This method is to calculate and show the shortest path from a given vertex
  32. */
  33. void shortestPath(int start) {
  34. // Create a queue for visited nodes
  35. boolean visited[] = new boolean[V];
  36.  
  37. // Create a queue for BFS
  38. LinkedList<Integer> queue = new LinkedList<Integer>();
  39.  
  40. // Mark the current node as visited and push it
  41. visited[start]=true;
  42. queue.push(start);
  43.  
  44. int[] height = new int[V];
  45.  
  46. // Repeat until the BFS queue is empty (meaning we traversed all possible paths)
  47. while (queue.size() != 0) {
  48. int currentNode;
  49. // Dequeue a vertex from queue and print it
  50. currentNode = queue.poll();
  51. System.out.println("Distance from vertex " + start + " to vertex " + currentNode + " is: " + height[currentNode]);
  52. // Get all neighbors of the dequeued node
  53. // If a neighbor has not been visited, then mark it visited and push to the BFS queue
  54. Iterator<Integer> i = adj[currentNode].listIterator();
  55. while (i.hasNext())
  56. {
  57. int n = i.next();
  58. if (!visited[n])
  59. {
  60. visited[n] = true;
  61. queue.add(n);
  62. }
  63. height[n]++;
  64. }
  65. }
  66. }
  67. }
  68.  
  69.  
  70. /*
  71. * This class is to read the inputQ2.txt text file.
  72. * This class also calls the GraphTraversal class
  73. * to find the distance from a vertex to all other
  74. * vertices
  75. */
  76. public class Test{
  77.  
  78. public static void main(String args[]) {
  79. File file = new File("C:\\Users\\iheal\\eclipse-workspace\\COSC222\\src\\lab8\\inputQ2.txt"); //File path
  80. Scanner scan;
  81. try {
  82. scan = new Scanner(file);
  83.  
  84. int nodes = scan.nextInt();
  85. int edges = scan.nextInt();
  86.  
  87. GraphTraversal graph = new GraphTraversal(nodes); //Initialize a graph
  88.  
  89. for (int i = 0; i < edges; i++)
  90. graph.addEdge(scan.nextInt(), scan.nextInt()); //For loop to read the text file
  91.  
  92. int startNode = scan.nextInt();
  93. graph.shortestPath(startNode); //To show the shortest path
  94. scan.close();
  95.  
  96. } catch (FileNotFoundException e) {
  97. e.printStackTrace();
  98. }
  99. }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement