Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Searching_Graph;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) {
- List<String> nodes = readFile("nodes_UTF-8.csv"); //gibt nodes aus
- // ignore CSV file header line
- nodes.remove(0);
- for (String node : nodes) {
- System.out.println(node);
- }
- System.out.println( "\n" );
- List<String> arces = readFile("arces_UTF-8.csv"); //gibt arces aus
- for (String arc : arces) {
- System.out.println(arc);
- }
- //example data extraction from string lists
- // Get the node (do this via for loop when processing all)
- for (String node : nodes) {
- // splitting the string at the specified character (this uses array because split returns array)
- String[] nodeParts = node.split(";");
- System.out.println("Node: " + nodeParts[0] + " Modifier: " + nodeParts[1]);
- // Get the arc the same way
- String arc = arces.get(2);
- String[] arcParts = arc.split(";");
- System.out.println("From: " + arcParts[0] + " To: " + arcParts[1] + " Distance: " + arcParts[2]);
- // Use the Node class to save all vertices
- // Save them in a Map linking the id to the node for quicker access
- // e.g.:
- Map<Integer, Node> parsedNodes = new HashMap<Integer, Node>();
- // using the example node from before
- // Here we parse the node from string to integer
- Integer nodeID = Integer.parseInt(nodeParts[0]);
- // Add the new node at position nodeID
- parsedNodes.put(nodeID, new Node(nodeParts[1]));
- }
- // Link the nodes by reading the arces file
- // example continued from above
- // this is commented because it does not run correctly (since I never added node 7 and 0)
- // get the node the arc is coming from
- // Node from = parsedNodes.get(Integer.parseInt(arcParts[0]));
- // get the node the arc is going to
- // Node to = parsedNodes.get(Integer.parseInt(arcParts[1]));
- // from.setNeighbour(to, Integer.parseInt(arcParts[2]));
- // then you can use the parsedNodes Map to find the first node (Node firstNode = parsedNodes.get(0))
- // and get all its neighbours (firstNode.getNeighbours())
- // Building the actual algorithm is something I can't give advice on
- // since the standard algorithms like Dijkstra's algorithm do not work with speed
- // You can probably solve the problem by brute forcing it since there are only nine vertices and 20 edges
- }
- public static List<String> readFile(String file) { // fügt CSV Datei in Array ein
- try {
- Scanner s1 = new Scanner (new File(file));
- List<String> nodes = new ArrayList<String>();
- while (s1.hasNextLine()) {
- nodes.add(s1.next());
- }
- s1.close();
- return nodes;
- }
- catch (FileNotFoundException e) {
- }
- return null;
- }
- }
Add Comment
Please, Sign In to add comment