Advertisement
Guest User

Untitled

a guest
May 25th, 2017
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.68 KB | None | 0 0
  1. package com.strkk.other.graph.other;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.nio.file.Files;
  6. import java.nio.file.Paths;
  7. import java.time.Instant;
  8. import java.util.*;
  9. import java.util.Map.Entry;
  10.  
  11. public class Planner {
  12.  
  13.     public static void main(String[] args) throws IOException {
  14.  
  15.         if(args.length < 1){
  16.             System.out.println("Need a path to file, exit...");
  17.             System.exit(1);
  18.         }
  19.  
  20.         long start = Instant.now().getEpochSecond();
  21.         Map<String, Node> map = new HashMap<>();
  22.         try (BufferedReader br = Files.newBufferedReader(Paths.get(args[0]))) {
  23.             String line = null;
  24.             while ((line = br.readLine()) != null) {
  25.                 int i = line.indexOf(' ');
  26.                 String source = line.substring(0, i);
  27.                 String dest = line.substring(i + 1);
  28.  
  29.                 Node s;
  30.                 if (map.containsKey(source)) {
  31.                     s = map.get(source);
  32.                 } else {
  33.                     s = new Node();
  34.                     map.put(source, s);
  35.                 }
  36.  
  37.                 Node d;
  38.                 if (map.containsKey(dest)) {
  39.                     d = map.get(dest);
  40.                 } else {
  41.                     d = new Node();
  42.                     map.put(dest, d);
  43.                 }
  44.                 d.counter++;
  45.                 s.nodes.add(d);
  46.             }
  47.         }
  48.  
  49.         List<List<String>> result = new ArrayList<>();
  50.         while (true) {
  51.             List<String> list = new ArrayList<>();
  52.             for (Iterator<Entry<String, Node>> it = map.entrySet().iterator(); it.hasNext(); ) {
  53.                 Entry<String, Node> e = it.next();
  54.                 Node value = e.getValue();
  55.                 if (!value.visited && value.counter == 0) {
  56.                     list.add(e.getKey());
  57.                     for (Node n : value.nodes) {
  58.                         n.counter--;
  59.                         n.visited = true;
  60.                     }
  61.                     it.remove();
  62.                 }
  63.             }
  64.             map.values().forEach(n -> n.visited = false);
  65.  
  66.             if (list.isEmpty()) {
  67.                 break;
  68.             }
  69.             result.add(list);
  70.         }
  71.         if (map.size() != 0) {
  72.             throw new IllegalArgumentException("Tasks graph contains cycles!");
  73.         }
  74.         System.out.println((Instant.now().getEpochSecond() - start) + " seconds");
  75. //        uncomment to see result
  76. //        result.forEach(System.out::println);
  77.         System.out.println("lists: " + result.size());
  78.     }
  79. }
  80.  
  81. class Node {
  82.     int counter;
  83.     List<Node> nodes = new ArrayList<>();
  84.     boolean visited;
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement