Advertisement
Guest User

dijkstra

a guest
Oct 23rd, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.14 KB | None | 0 0
  1.  
  2. // A Java program for Dijkstra's single source shortest path algorithm.
  3. // The program is for adjacency matrix representation of the graph
  4. import java.util.*;
  5. import java.util.Scanner;
  6. import java.lang.*;
  7. import java.io.*;
  8.  
  9. public class ShortestPath {
  10. // A utility function to find the vertex with minimum distance value,
  11. // from the set of vertices not yet included in shortest path tree
  12. static int SV = 0;
  13. static int N = 0;
  14. static int dist[];
  15.  
  16. public static int minDistance(int dist[], Boolean sptSet[]) {
  17. // Initialize min value
  18. int min = Integer.MAX_VALUE, min_index = -1;
  19. for (int v = 0; v < N; v++)
  20. if (sptSet[v] == false && dist[v] <= min) {
  21. min = dist[v];
  22. min_index = v;
  23. }
  24.  
  25. return min_index;
  26. }
  27.  
  28. // A utility function to print the constructed distance array
  29. public static void printSolution(int dist[], int n) {
  30. for (int i = 0; i < N; i++)
  31. System.out.println("[" + dist[i] + "]");
  32.  
  33.  
  34.  
  35. }
  36.  
  37. // Funtion that implements Dijkstra's single source shortest path
  38. public void dijkstra(int graph[][], int src) {
  39. int dist[] = new int[N]; // The output array. dist[i] will hold
  40. // the shortest distance from src to i
  41.  
  42. // sptSet[i] will true if vertex i is included in shortest
  43. // path tree or shortest distance from src to i is finalized
  44. Boolean sptSet[] = new Boolean[N];
  45.  
  46. // Initialize all distances as INFINITE and stpSet[] as false
  47. for (int i = 0; i < N; i++) {
  48. dist[i] = Integer.MAX_VALUE;
  49. sptSet[i] = false;
  50. }
  51.  
  52. // Distance of source vertex from itself is always 0
  53. dist[src] = 0;
  54.  
  55. // Find shortest path for all vertices
  56. for (int count = 0; count < N - 1; count++) {
  57. // Pick the minimum distance vertex from the set of vertices
  58. // not yet processed. u is always equal to src in first
  59. // iteration.
  60. int u = minDistance(dist, sptSet);
  61.  
  62. // Mark the picked vertex as processed
  63. sptSet[u] = true;
  64.  
  65. // Update dist value of the adjacent vertices of the
  66. // picked vertex.
  67. for (int v = 0; v < N; v++)
  68.  
  69. // Update dist[v] only if is not in sptSet, there is an
  70. // edge from u to v, and total weight of path from src to
  71. // v through u is smaller than current value of dist[v]
  72. if (!sptSet[v] && graph[u][v] != 0 && dist[u] != Integer.MAX_VALUE && dist[u] + graph[u][v] < dist[v])
  73. dist[v] = dist[u] + graph[u][v];
  74.  
  75.  
  76. }
  77.  
  78. // print the constructed distance array
  79. printSolution(dist, N);
  80.  
  81.  
  82. }
  83.  
  84.  
  85.  
  86. public static void printMatrix(int[][] Matrix) {
  87. for (int i = 0; i < N; i++) {
  88. for (int j = 0; j < N; j++) {
  89. System.out.print(Matrix[i][j]);
  90. System.out.print(" ");
  91.  
  92. }
  93. System.out.println();
  94. }}
  95.  
  96.  
  97. // Driver method
  98. public static void main(String[] args) throws FileNotFoundException {
  99. int[][] M = null;
  100. try {
  101. int i = 0, j = 0; // counters
  102. FileInputStream textFile = new FileInputStream("EXAMPLE(2).txt"); // name of input file must go in here
  103. Scanner scan = new Scanner(textFile);
  104. N = scan.nextInt(); // read in the size
  105. String flush = scan.nextLine(); // gets rid of linefeed
  106. M = new int[N][N]; // instantiates array
  107. // this loop reads in matrix from input file
  108. String line;
  109. while (i < N && (line = scan.nextLine()) != null) {
  110. j = 0;
  111. String delim = " ";
  112. String tokens[] = line.split(delim);
  113. for (String a : tokens) {
  114. M[i][j] = Integer.parseInt(a);
  115. j++;
  116. }
  117. i++;
  118. }
  119. if (i > N);
  120. SV = scan.nextInt();
  121. } catch (Exception e) {
  122. e.printStackTrace();
  123. }
  124. //PrintSolution(dist);
  125. ShortestPath t = new ShortestPath();
  126. System.out.println(N);
  127. printMatrix(M);
  128. System.out.println(SV);
  129. System.out.println("\t\t");
  130. t.dijkstra(M, SV - 1);
  131.  
  132.  
  133. try {
  134. FileWriter fw = new FileWriter("dijkstra.txt"); // writes transitive closure to file
  135. for (int i = 0; i < N; i++)
  136. fw.write(t.dijkstra(M, SV - 1));
  137.  
  138. System.out.println("\n");
  139. fw.write(" \n");
  140.  
  141. // fw.write("time in milliseconds" + Ttime / 1000000); // writes runtime to file
  142. System.out.println("\n");
  143. fw.close();
  144. } catch (Exception e) {
  145. System.out.println(e);
  146. }
  147.  
  148.  
  149.  
  150.  
  151. }
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement