Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.15 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Collections;
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import java.util.Map;
  6.  
  7. class Node {
  8.     private Integer data;
  9.     private Integer dNum;
  10.     private List<Node> children;
  11.     private List<Node> backLinks;
  12.     private Integer low;
  13.  
  14.     public Node(int data, int count) {
  15.         this.setData(data);
  16.         this.setdNum(count);
  17.         this.setLow(Integer.MAX_VALUE);
  18.         setChildren(new ArrayList<>());
  19.         setBackLinks(new ArrayList<>());
  20.     }
  21.  
  22.     public List<Node> getChildren() {
  23.         return children;
  24.     }
  25.  
  26.     public void setChildren(List<Node> children) {
  27.         this.children = children;
  28.     }
  29.  
  30.     public List<Node> getBackLinks() {
  31.         return backLinks;
  32.     }
  33.  
  34.     public void setBackLinks(List<Node> backLinks) {
  35.         this.backLinks = backLinks;
  36.     }
  37.  
  38.     public Integer getData() {
  39.         return data;
  40.     }
  41.  
  42.     public void setData(Integer data) {
  43.         this.data = data;
  44.     }
  45.  
  46.     public Integer getdNum() {
  47.         return dNum;
  48.     }
  49.  
  50.     public void setdNum(Integer dNum) {
  51.         this.dNum = dNum;
  52.     }
  53.  
  54.     public Integer getLow() {
  55.         return low;
  56.     }
  57.  
  58.     public void setLow(Integer low) {
  59.         this.low = low;
  60.     }
  61. }
  62.  
  63. public class Main {
  64.     private static Integer COUNT = 1;
  65.  
  66.     public static void main(String[] args) {
  67.         int numRouters1 = 5;
  68.         int numLinks1 = 6;
  69.         int[][] links1 = {{0, 1}, {1, 2}, {0, 2}, {2, 3}, {2, 4}, {3, 4}};
  70.         System.out.println(getCriticalNodes(links1, numLinks1, numRouters1));
  71.         int numRouters2 = 5;
  72.         int numLinks2 = 5;
  73.         int[][] links2 = {{0, 1}, {1, 2}, {0, 2}, {0, 3}, {3, 4}};
  74.         System.out.println(getCriticalNodes(links2, numLinks2, numRouters2));
  75.         int numRouters3 = 4;
  76.         int numLinks3 = 3;
  77.         int[][] links3 = {{0, 1}, {1, 2}, {2, 3}};
  78.         System.out.println(getCriticalNodes(links3, numLinks3, numRouters3));
  79.         int numRouters4 = 7;
  80.         int numLinks4 = 7;
  81.         int[][] links4 = {{0, 1}, {0, 2}, {1, 3}, {2, 3}, {2, 5}, {5, 6}, {3, 4}};
  82.         System.out.println(getCriticalNodes(links4, numLinks4, numRouters4));
  83.         int numRouters5 = 4;
  84.         int numLinks5 = 4;
  85.         int[][] links5 = {{0, 1}, {0, 2}, {0, 3}};
  86.         System.out.println(getCriticalNodes(links5, numLinks5, numRouters5));
  87.     }
  88.  
  89.     private static List<Integer> getList(Map<Integer, List<Integer>> graph, int key) {
  90.         List<Integer> nodes;
  91.         if (graph.get(key) == null) {
  92.             nodes = new ArrayList<Integer>();
  93.         } else {
  94.             nodes = graph.get(key);
  95.         }
  96.         return nodes;
  97.     }
  98.  
  99.     private static List<Integer> getCriticalNodes(int[][] links, int numLinks, int numRouters) {
  100.  
  101.         Map<Integer, List<Integer>> graph = new HashMap<>();
  102.         for (int[] link : links) {
  103.             int first = link[0];
  104.             int second = link[1];
  105.             List<Integer> adjFirst = getList(graph, first);
  106.             List<Integer> adjSecond = getList(graph, second);
  107.             adjFirst.add(second);
  108.             adjSecond.add(first);
  109.             graph.put(first, adjFirst);
  110.             graph.put(second, adjSecond);
  111.         }
  112.  
  113.         if (graph.isEmpty()) {
  114.             return new ArrayList<>();
  115.         }
  116.  
  117.         Node head = new Node(links[0][0], 1);
  118.         dfs(head, graph, new HashMap<Integer, Node>());
  119.         findLow(head);
  120.         List<Integer> result = new ArrayList<>();
  121.  
  122.         if (head.getChildren().size() == 1) {
  123.             findCriticalRouters(head.getChildren().get(0), result);
  124.         } else {
  125.             findCriticalRouters(head, result);
  126.         }
  127.         return result;
  128.     }
  129.  
  130.     private static void dfs(Node node, Map<Integer, List<Integer>> graph, Map<Integer, Node> visited) {
  131.         List<Integer> adjecentNodes = graph.get(node.getData());
  132.         if (adjecentNodes == null) {
  133.             return;
  134.         }
  135.         visited.put(node.getData(), node);
  136.         for (Integer adj : adjecentNodes) {
  137.             if (visited.get(adj) == null) {
  138.                 Node adjNode = new Node(adj, ++COUNT);
  139.                 List<Node> children = node.getChildren();
  140.                 children.add(adjNode);
  141.                 node.setChildren(children);
  142.                 dfs(adjNode, graph, visited);
  143.             } else {
  144.                 Node adjNode = visited.get(adj);
  145.                 if (Math.abs(node.getdNum() - adjNode.getdNum()) != 1) {
  146.                     if (node.getdNum() > adjNode.getdNum()) {
  147.                         List<Node> backLinks = node.getBackLinks();
  148.                         backLinks.add(adjNode);
  149.                         node.setBackLinks(backLinks);
  150.                     } else {
  151.  
  152.                         List<Node> backLinks = adjNode.getBackLinks();
  153.                         backLinks.add(node);
  154.                         adjNode.setBackLinks(backLinks);
  155.                     }
  156.                 }
  157.             }
  158.         }
  159.     }
  160.  
  161.     private static void findLow(Node head) {
  162.         for (Node c : head.getChildren()) {
  163.             findLow(c);
  164.             List<Integer> val = new ArrayList<>();
  165.             val.add(c.getdNum());
  166.             List<Integer> childLowVals = new ArrayList<>();
  167.             for (Node n : c.getChildren()) {
  168.                 childLowVals.add(n.getLow());
  169.             }
  170.             if (!childLowVals.isEmpty()) {
  171.                 val.add(Collections.min(childLowVals));
  172.             }
  173.             List<Integer> backLinksDNum = new ArrayList<>();
  174.             for (Node n : c.getBackLinks()) {
  175.                 backLinksDNum.add(n.getdNum());
  176.             }
  177.             if (!backLinksDNum.isEmpty()) {
  178.                 val.add(Collections.min(backLinksDNum));
  179.             }
  180.             c.setLow(Collections.min(val));
  181.         }
  182.     }
  183.  
  184.     private static void findCriticalRouters(Node node, List<Integer> result) {
  185.         for (Node c : node.getChildren()) {
  186.             if (node.getdNum() <= c.getLow()) {
  187.                 if (!result.contains(node.getData())) {
  188.                     result.add(node.getData());
  189.                 }
  190.             }
  191.             findCriticalRouters(c, result);
  192.         }
  193.     }
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement