|
| 1 | +package com.subham.ta.graph; |
| 2 | + |
| 3 | +import java.util.HashMap; |
| 4 | +import java.util.HashSet; |
| 5 | +import java.util.Map; |
| 6 | +import java.util.Set; |
| 7 | +import java.util.concurrent.atomic.AtomicInteger; |
| 8 | + |
| 9 | +public final class ArticulationPointOnDirectedGraph { |
| 10 | + public static <K, V> Set<Vertex<K, V>> findArticulationPoint(final Graph<K, V> graph) { |
| 11 | + final Map<Vertex<K, V>, Integer> highTime = new HashMap<>(); |
| 12 | + final Map<Vertex<K, V>, Integer> lowTime = new HashMap<>(); |
| 13 | + final Set<Vertex<K, V>> articulationPoints = new HashSet<>(); |
| 14 | + final Set<Vertex<K, V>> visitedSet = new HashSet<>(); |
| 15 | + final AtomicInteger timer = new AtomicInteger(0); |
| 16 | + |
| 17 | + Vertex<K, V> root = graph.getVertexSet().iterator().next(); |
| 18 | + DFS(timer, graph, root, null, highTime, lowTime, articulationPoints, visitedSet); |
| 19 | + |
| 20 | + return articulationPoints; |
| 21 | + } |
| 22 | + |
| 23 | + private static <K, V> void DFS( |
| 24 | + final AtomicInteger timer, |
| 25 | + final Graph<K, V> graph, |
| 26 | + final Vertex<K, V> currentVertex, |
| 27 | + final Vertex<K, V> parent, |
| 28 | + final Map<Vertex<K, V>, Integer> highTime, |
| 29 | + final Map<Vertex<K, V>, Integer> lowTime, |
| 30 | + final Set<Vertex<K, V>> articulationPoints, |
| 31 | + final Set<Vertex<K, V>> visitedSet) { |
| 32 | + |
| 33 | + if (visitedSet.contains(currentVertex)) { |
| 34 | + return; |
| 35 | + } |
| 36 | + |
| 37 | + visitedSet.add(currentVertex); |
| 38 | + int currentTime = timer.incrementAndGet(); |
| 39 | + |
| 40 | + highTime.put(currentVertex, currentTime); |
| 41 | + lowTime.put(currentVertex, currentTime); |
| 42 | + |
| 43 | + for (final Vertex<K, V> children : currentVertex.getNeighbors()) { |
| 44 | + if (children.equals(parent)) { |
| 45 | + continue; |
| 46 | + } |
| 47 | + DFS(timer, graph, children, currentVertex, highTime, lowTime, articulationPoints, visitedSet); |
| 48 | + } |
| 49 | + |
| 50 | + // calculate low time for current vertex. |
| 51 | + for (final Vertex<K, V> children : currentVertex.getNeighbors()) { |
| 52 | + if (children.equals(parent)) { |
| 53 | + continue; |
| 54 | + } |
| 55 | + lowTime.put( |
| 56 | + currentVertex, |
| 57 | + Math.min(lowTime.getOrDefault(children, Integer.MAX_VALUE), lowTime.get(currentVertex))); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + public static void main(String[] args) { |
| 62 | + Graph<Character, Character> graph = new Graph<>(false); |
| 63 | + graph.addEdge(Edge.createSimpleEdgeUnWeighted('A', 'B')); |
| 64 | + graph.addEdge(Edge.createSimpleEdgeUnWeighted('A', 'C')); |
| 65 | + graph.addEdge(Edge.createSimpleEdgeUnWeighted('C', 'B')); |
| 66 | + graph.addEdge(Edge.createSimpleEdgeUnWeighted('C', 'D')); |
| 67 | + graph.addEdge(Edge.createSimpleEdgeUnWeighted('E', 'D')); |
| 68 | + graph.addEdge(Edge.createSimpleEdgeUnWeighted('E', 'G')); |
| 69 | + graph.addEdge(Edge.createSimpleEdgeUnWeighted('E', 'F')); |
| 70 | + graph.addEdge(Edge.createSimpleEdgeUnWeighted('G', 'F')); |
| 71 | + graph.addEdge(Edge.createSimpleEdgeUnWeighted('H', 'F')); |
| 72 | + |
| 73 | + System.out.println(findArticulationPoint(graph)); |
| 74 | + } |
| 75 | +} |
0 commit comments