Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- class Node {
- private Integer data;
- private Integer dNum;
- private List<Node> children;
- private List<Node> backLinks;
- private Integer low;
- public Node(int data, int count) {
- this.setData(data);
- this.setdNum(count);
- this.setLow(Integer.MAX_VALUE);
- setChildren(new ArrayList<>());
- setBackLinks(new ArrayList<>());
- }
- public List<Node> getChildren() {
- return children;
- }
- public void setChildren(List<Node> children) {
- this.children = children;
- }
- public List<Node> getBackLinks() {
- return backLinks;
- }
- public void setBackLinks(List<Node> backLinks) {
- this.backLinks = backLinks;
- }
- public Integer getData() {
- return data;
- }
- public void setData(Integer data) {
- this.data = data;
- }
- public Integer getdNum() {
- return dNum;
- }
- public void setdNum(Integer dNum) {
- this.dNum = dNum;
- }
- public Integer getLow() {
- return low;
- }
- public void setLow(Integer low) {
- this.low = low;
- }
- }
- public class Main {
- private static Integer COUNT = 1;
- public static void main(String[] args) {
- int numRouters1 = 5;
- int numLinks1 = 6;
- int[][] links1 = {{0, 1}, {1, 2}, {0, 2}, {2, 3}, {2, 4}, {3, 4}};
- System.out.println(getCriticalNodes(links1, numLinks1, numRouters1));
- int numRouters2 = 5;
- int numLinks2 = 5;
- int[][] links2 = {{0, 1}, {1, 2}, {0, 2}, {0, 3}, {3, 4}};
- System.out.println(getCriticalNodes(links2, numLinks2, numRouters2));
- int numRouters3 = 4;
- int numLinks3 = 3;
- int[][] links3 = {{0, 1}, {1, 2}, {2, 3}};
- System.out.println(getCriticalNodes(links3, numLinks3, numRouters3));
- int numRouters4 = 7;
- int numLinks4 = 7;
- int[][] links4 = {{0, 1}, {0, 2}, {1, 3}, {2, 3}, {2, 5}, {5, 6}, {3, 4}};
- System.out.println(getCriticalNodes(links4, numLinks4, numRouters4));
- int numRouters5 = 4;
- int numLinks5 = 4;
- int[][] links5 = {{0, 1}, {0, 2}, {0, 3}};
- System.out.println(getCriticalNodes(links5, numLinks5, numRouters5));
- }
- private static List<Integer> getList(Map<Integer, List<Integer>> graph, int key) {
- List<Integer> nodes;
- if (graph.get(key) == null) {
- nodes = new ArrayList<Integer>();
- } else {
- nodes = graph.get(key);
- }
- return nodes;
- }
- private static List<Integer> getCriticalNodes(int[][] links, int numLinks, int numRouters) {
- Map<Integer, List<Integer>> graph = new HashMap<>();
- for (int[] link : links) {
- int first = link[0];
- int second = link[1];
- List<Integer> adjFirst = getList(graph, first);
- List<Integer> adjSecond = getList(graph, second);
- adjFirst.add(second);
- adjSecond.add(first);
- graph.put(first, adjFirst);
- graph.put(second, adjSecond);
- }
- if (graph.isEmpty()) {
- return new ArrayList<>();
- }
- Node head = new Node(links[0][0], 1);
- dfs(head, graph, new HashMap<Integer, Node>());
- findLow(head);
- List<Integer> result = new ArrayList<>();
- if (head.getChildren().size() == 1) {
- findCriticalRouters(head.getChildren().get(0), result);
- } else {
- findCriticalRouters(head, result);
- }
- return result;
- }
- private static void dfs(Node node, Map<Integer, List<Integer>> graph, Map<Integer, Node> visited) {
- List<Integer> adjecentNodes = graph.get(node.getData());
- if (adjecentNodes == null) {
- return;
- }
- visited.put(node.getData(), node);
- for (Integer adj : adjecentNodes) {
- if (visited.get(adj) == null) {
- Node adjNode = new Node(adj, ++COUNT);
- List<Node> children = node.getChildren();
- children.add(adjNode);
- node.setChildren(children);
- dfs(adjNode, graph, visited);
- } else {
- Node adjNode = visited.get(adj);
- if (Math.abs(node.getdNum() - adjNode.getdNum()) != 1) {
- if (node.getdNum() > adjNode.getdNum()) {
- List<Node> backLinks = node.getBackLinks();
- backLinks.add(adjNode);
- node.setBackLinks(backLinks);
- } else {
- List<Node> backLinks = adjNode.getBackLinks();
- backLinks.add(node);
- adjNode.setBackLinks(backLinks);
- }
- }
- }
- }
- }
- private static void findLow(Node head) {
- for (Node c : head.getChildren()) {
- findLow(c);
- List<Integer> val = new ArrayList<>();
- val.add(c.getdNum());
- List<Integer> childLowVals = new ArrayList<>();
- for (Node n : c.getChildren()) {
- childLowVals.add(n.getLow());
- }
- if (!childLowVals.isEmpty()) {
- val.add(Collections.min(childLowVals));
- }
- List<Integer> backLinksDNum = new ArrayList<>();
- for (Node n : c.getBackLinks()) {
- backLinksDNum.add(n.getdNum());
- }
- if (!backLinksDNum.isEmpty()) {
- val.add(Collections.min(backLinksDNum));
- }
- c.setLow(Collections.min(val));
- }
- }
- private static void findCriticalRouters(Node node, List<Integer> result) {
- for (Node c : node.getChildren()) {
- if (node.getdNum() <= c.getLow()) {
- if (!result.contains(node.getData())) {
- result.add(node.getData());
- }
- }
- findCriticalRouters(c, result);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement