图论算法(五)--求解割点、割边(JAVA)
割点算法
时间复杂度:O(N+M) 割点:对于一个连通图来说,如果删除某个点之后图不再连通,这个点就称为割点 但是下面给出的算法时间复杂度为O(N^2),这是因为下面的存储结构都是邻接矩阵,这样的话用该算法就完全失去了意义,毕竟如果使用邻接矩阵完全可以通过删除一个点,然后进行DFS/BFS遍历即可了。所以在实际应用中还是要使用邻接表来保存数据,这样时间复杂度可以达到O(N+M) Input: 6 7 1 4 1 3 4 2 3 2 2 5 2 6 5 6
Output: 2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57import java.util.Scanner;
public class cutPoint {
static int n, m;
static int[][] e = new int[9][9];
static int[] num = new int[9];
static int[] low = new int[9];
static int[] flag = new int[9];
static int index,root;
static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
n = input.nextInt();
m = input.nextInt();
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
e[i][j] = 0;
}
}
for (int i = 1; i <= m; i++) {
int x = input.nextInt();
int y = input.nextInt();
e[x][y] = 1;
e[y][x] = 1;
}
root = 1;
dfs(1, root);
for (int i = 1; i <= n; i++) {
if (flag[i] == 1) {
System.out.print(i + " ");
}
}
}
private static void dfs(int cur, int father) {
int child = 0;
index++;
num[cur] = index;
low[cur] = index;
for (int i = 1; i <= n; i++) {
if (e[cur][i] == 1) {
if (num[i] == 0) {
child++;
dfs(i, cur);
low[cur] = Math.min(low[cur], low[i]);
if (cur != root && low[i] >= num[cur]) {
flag[cur] = 1;
}
if (cur == root && child == 2) {
flag[cur] = 1;
}
} else if (i != father){
low[cur] = Math.min(low[cur], num[i]);
}
}
}
}
}
割边算法
时间复杂度:O(N+M) 割边:对于一个连通图来说,如果删除某个边之后图不再连通,这个点就称为割边 同割点算法一样,在实际应用中还是要使用邻接表来保存数据,这样时间复杂度可以达到O(N+M) Input: 6 7 1 4 1 3 4 2 3 2 2 5 2 6 5 6
Output: 5-6 2-5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46import java.util.Scanner;
public class cutEdge {
static int n, m;
static int[][] e = new int[9][9];
static int[] num = new int[9];
static int[] low = new int[9];
static int index,root;
static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
n = input.nextInt();
m = input.nextInt();
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
e[i][j] = 0;
}
}
for (int i = 1; i <= m; i++) {
int x = input.nextInt();
int y = input.nextInt();
e[x][y] = 1;
e[y][x] = 1;
}
root = 1;
dfs(1, root);
}
private static void dfs(int cur, int father) {
index++;
num[cur] = index;
low[cur] = index;
for (int i = 1; i <= n; i++) {
if (e[cur][i] == 1) {
if (num[i] == 0) {
dfs(i, cur);
low[cur] = Math.min(low[i], low[cur]);
if (low[i] > num[cur]) {
System.out.println(cur + "-" + i);
}
} else if (i != father) {
low[cur] = Math.min(low[cur], num[i]);
}
}
}
}
}