Guest User

Untitled

a guest
Jun 23rd, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.80 KB | None | 0 0
  1. package Searching_Graph;
  2.  
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5. import java.util.ArrayList;
  6. import java.util.HashMap;
  7. import java.util.List;
  8. import java.util.Map;
  9. import java.util.Scanner;
  10.  
  11. public class Main {
  12.  
  13. public static void main(String[] args) {
  14.  
  15. List<String> nodes = readFile("nodes_UTF-8.csv"); //gibt nodes aus
  16. // ignore CSV file header line
  17. nodes.remove(0);
  18.  
  19. for (String node : nodes) {
  20. System.out.println(node);
  21. }
  22. System.out.println( "\n" );
  23.  
  24.  
  25. List<String> arces = readFile("arces_UTF-8.csv"); //gibt arces aus
  26.  
  27. for (String arc : arces) {
  28. System.out.println(arc);
  29. }
  30.  
  31. //example data extraction from string lists
  32. // Get the node (do this via for loop when processing all)
  33. for (String node : nodes) {
  34. // splitting the string at the specified character (this uses array because split returns array)
  35. String[] nodeParts = node.split(";");
  36.  
  37. System.out.println("Node: " + nodeParts[0] + " Modifier: " + nodeParts[1]);
  38.  
  39. // Get the arc the same way
  40. String arc = arces.get(2);
  41. String[] arcParts = arc.split(";");
  42.  
  43. System.out.println("From: " + arcParts[0] + " To: " + arcParts[1] + " Distance: " + arcParts[2]);
  44.  
  45. // Use the Node class to save all vertices
  46. // Save them in a Map linking the id to the node for quicker access
  47. // e.g.:
  48. Map<Integer, Node> parsedNodes = new HashMap<Integer, Node>();
  49. // using the example node from before
  50. // Here we parse the node from string to integer
  51. Integer nodeID = Integer.parseInt(nodeParts[0]);
  52. // Add the new node at position nodeID
  53. parsedNodes.put(nodeID, new Node(nodeParts[1]));
  54. }
  55.  
  56. // Link the nodes by reading the arces file
  57. // example continued from above
  58. // this is commented because it does not run correctly (since I never added node 7 and 0)
  59. // get the node the arc is coming from
  60. // Node from = parsedNodes.get(Integer.parseInt(arcParts[0]));
  61. // get the node the arc is going to
  62. // Node to = parsedNodes.get(Integer.parseInt(arcParts[1]));
  63. // from.setNeighbour(to, Integer.parseInt(arcParts[2]));
  64.  
  65. // then you can use the parsedNodes Map to find the first node (Node firstNode = parsedNodes.get(0))
  66. // and get all its neighbours (firstNode.getNeighbours())
  67. // Building the actual algorithm is something I can't give advice on
  68. // since the standard algorithms like Dijkstra's algorithm do not work with speed
  69. // You can probably solve the problem by brute forcing it since there are only nine vertices and 20 edges
  70.  
  71. }
  72.  
  73. public static List<String> readFile(String file) { // fügt CSV Datei in Array ein
  74.  
  75. try {
  76. Scanner s1 = new Scanner (new File(file));
  77. List<String> nodes = new ArrayList<String>();
  78. while (s1.hasNextLine()) {
  79. nodes.add(s1.next());
  80. }
  81. s1.close();
  82.  
  83. return nodes;
  84. }
  85. catch (FileNotFoundException e) {
  86.  
  87. }
  88. return null;
  89. }
  90. }
Add Comment
Please, Sign In to add comment