import java.util.*; public class Articulation { private int n; private int[][] arr; private int[] dfn; private int[] low; private int num; private Set<Integer> s; public Articulation(int n) { this.n = n; arr = new int[n][n]; dfn = new int[n]; low = new int[n]; num = 1; s = new HashSet<>(); } public void read(Scanner scan) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) arr[i][j] = scan.nextInt(); dfn[i] = 0; } } public void art(int u, int v) { dfn[u] = num; low[u] = num; num++; int child = 0; for (int j = 0; j < n; j++) { if (arr[u][j] == 1 && dfn[j] == 0) { if (v == -1) child++; art(j, u); if (v != -1 && low[j] >= dfn[u]) s.add(u); low[u] = Math.min(low[u], low[j]); } else if (arr[u][j] == 1 && j != v) low[u] = Math.min(low[u], dfn[j]); } if (v == -1 && child > 1) s.add(u); } public void printVisited() { System.out.println("articulation points" + s); for (int i = 0; i < n; i++) System.out.print(dfn[i] - 1 + " "); System.out.println(); } public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("enter no of vertices"); int n = scan.nextInt(); Articulation art = new Articulation(n); System.out.println("adjacent matrix"); art.read(scan); art.art(0, -1); System.out.println("vertices"); art.printVisited(); } } OUTPUT: enter no of vertices 5 adjacent matrix 0 1 1 0 0 1 0 1 0 0 1 1 0 1 0 0 0 1 0 1 0 0 0 1 0 vertices articulation points[2, 3] 0 1 2 3 4
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter