Advertisement
Guest User

PathMain

a guest
Oct 23rd, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.22 KB | None | 0 0
  1. import java.util.HashMap;
  2. import java.util.LinkedHashSet;
  3. import java.util.LinkedList;
  4. import java.util.Map;
  5.  
  6. public final class Path {
  7.  
  8. public final class Edge{
  9. private int k;
  10. private float w;
  11. public Edge(int k, float w){
  12. setK(k);
  13. setW(w);
  14. }
  15.  
  16. public int getK() {
  17. return this.k;
  18. }
  19.  
  20. public double getW(){
  21. return this.w;
  22. }
  23.  
  24. public void setK(int k){
  25. this.k = k;
  26. }
  27.  
  28. public void setW(float w){
  29. this.w = w;
  30. }
  31. }
  32.  
  33. private int last;
  34. private double min;
  35. private final Map<Integer, LinkedHashSet<Edge>> map;
  36.  
  37. public Path (int last){
  38. this.map = new HashMap<Integer, LinkedHashSet<Edge>>();
  39. setMin(Float.MAX_VALUE);
  40. setLast(last);
  41. }
  42.  
  43. public void setLast(int last){
  44. this.last = last;
  45. }
  46.  
  47. public void setMin(double min){
  48. this.min = min;
  49. }
  50.  
  51. public double getMin(){
  52. return this.min;
  53. }
  54.  
  55. public int getLast(){
  56. return this.last;
  57. }
  58. void addEdge(int knot1, Edge knot2){
  59. LinkedHashSet<Edge> adjacent = map.get(knot1);
  60. if (adjacent==null){
  61. adjacent = new LinkedHashSet<Edge>();
  62. map.put(knot1, adjacent);
  63. }
  64. adjacent.add(knot2);
  65. }
  66.  
  67. private LinkedList<Edge> adjacentknots(Integer last){
  68. LinkedHashSet<Edge> adjacent = map.get(last);
  69. if (adjacent==null) {
  70. return new LinkedList<Edge>();
  71. }
  72. return new LinkedList<Edge>(adjacent);
  73. }
  74.  
  75. public void depthFirst(LinkedList<Edge> visited) {
  76. LinkedList<Edge> knots = adjacentknots(visited.getLast().getK());
  77. boolean pom=false;
  78. for (Edge knot : knots){
  79. pom=knot.getK() == getLast();
  80. if (visited.contains(knot))
  81. continue;
  82.  
  83. if (pom){
  84. visited.add(knot);
  85. checkPath(visited);
  86. visited.removeLast();
  87. break;
  88. }
  89. }
  90. for (Edge knot : knots){
  91. if (pom){
  92. continue;
  93. }
  94.  
  95. if( visited.contains(knot) || pom){
  96. continue;
  97. }
  98.  
  99. visited.addLast(knot);
  100. depthFirst(visited);
  101. visited.removeLast();
  102. }
  103. }
  104.  
  105. private void checkPath(LinkedList<Edge> visited){
  106. double prev = 0;
  107. double temp = 0;
  108.  
  109. for (Edge knot : visited){
  110. if (prev != 0 && knot.w != 0){
  111. temp = temp + 1./knot.w + Math.abs(prev-knot.w);
  112. prev = knot.w;
  113. }
  114. if (prev == 0 && knot.w != 0){
  115. temp = temp + 1./knot.w;
  116. prev = knot.w;
  117. }
  118. }
  119. if (temp < getMin())
  120. setMin(temp);
  121. }
  122. }
  123.  
  124.  
  125.  
  126. import java.sql.Connection;
  127. import java.sql.DriverManager;
  128. import java.sql.ResultSet;
  129. import java.sql.SQLException;
  130. import java.sql.Statement;
  131. import java.util.*;
  132.  
  133.  
  134. public class Main {
  135. private static Path graph;
  136. private static void initConnection(final String database, int endPoint) throws SQLException{
  137. Connection conn = DriverManager.getConnection(database);
  138. graph = new Path(endPoint);
  139. buildGraph(conn);
  140. }
  141.  
  142. private static void buildGraph(Connection conn) throws SQLException{
  143. Statement st = conn.createStatement();
  144. ResultSet rs = st.executeQuery("SELECT x, y, p FROM Gtable");
  145.  
  146. while (rs.next()) {
  147. graph.addEdge(rs.getInt(1), graph.new Edge(rs.getInt(2), rs.getFloat(3)));
  148. }
  149. }
  150.  
  151. public static void main(String[] args) throws SQLException {
  152. //String database = args[0].trim();
  153. String database ="jdbc:sqlserver://localhost:1433;databaseName=master;user=test1;password=test1";
  154. //int endPoint = Integer.parseInt(args[1]);
  155. int endPoint = 1;
  156. initConnection(database, endPoint);
  157. LinkedList<Path.Edge> visited = new LinkedList<Path.Edge>();
  158. visited.add(graph.new Edge(1, 0));
  159. graph.depthFirst(visited);
  160. System.out.format("Koszt: %.3f%n", graph.getMin());
  161. }
  162.  
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement