[Parallel Set] B_684. Redundant connection (Parallel Set | Topological Sorting)

1. Title Description

In this problem, the tree refers to an undirected graph that is connected and acyclic.

Enter a graph, which consists of a tree with N nodes (node ​​values ​​do not repeat 1, 2,…, N) and an additional edge. The two vertices of the additional edge are contained between 1 and N. This additional edge does not belong to an existing edge in the tree.

The resulting graph is a two-dimensional array of edges. The elements of each edge are a pair of [u, v], satisfying u <v, indicating the edge of the undirected graph connecting the vertices u and v.

Return an edge that can be deleted, making the resulting graph a tree with N nodes. If there are multiple answers, the last edge in the two-dimensional array is returned. The answer edge [u, v] should satisfy the same format u <v.

输入: [[1,2], [2,3], [3,4], [1,4], [1,5]]
输出: [1,4]
解释: 给定的无向图为:
5 - 1 - 2
    |   |
    4 - 3

Second, the solution

Method 1: Check

  • In the initial state, each node has its own leader.
  • Traverse the whole picture, if there are different fathers between the two nodes, then merge them together.
  • If two or two nodes have been merged before and the same leader is checked again, then prove that the edge formed by these two points is redundant.
public int[] findRedundantConnection(int[][] edges) {
    UF uf = new UF(edges.length);
    for (int[] edge : edges) {
        if (uf.union(edge[0], edge[1]))
            return edge;
    }
    return new int[2];
}
class UF {
    int[] id;
    UF(int N) {
        id = new int[N+50];
        for (int i = 0; i < N; i++)
            id[i] = i;
    }
    public int find(int p) {
        while (p != id[p]) {
            p = id[p];
        }
        return p;
    }
    public boolean union(int p, int q) {
        int pID = find(p);
        int qID = find(q);
        if (pID == qID) {
            return true;
        }
        id[pID] = qID;
        return false;
    }
}

Complexity analysis

  • time complexity: O ( N ) O (N)
  • Space complexity: O ( N ) O (N)

Method 2: Topological sorting

  • The starting point of topological sorting is all nodes with a degree of 1.
  • Because the graph is a directed graph, the in-degree of most nodes is greater than 1, so these nodes are enough to form a ring. The edges in the ring can be arbitrarily deleted, which will not affect the connectivity of the graph.
  • It can be seen that we perform a topological sort from the node with the degree of 1, and finally traverse the reverse direction edges. If the degree of the two vertices of an edge is greater than 1, it proves that the two vertices are on the ring Just delete it.

Q&A

  • Q1: Why is it necessary to traverse the edges backwards at the end, can not all the edges on the ring be deleted arbitrarily?
    A1: hh, the title has stated: If there are multiple answers, the last edge in the two-dimensional array is returned.
class Solution {
	List<List<Integer>> g;
	int[] in;
	boolean[] inq;
    public int[] findRedundantConnection(int[][] edges) {
        int N = edges.length;
		g = new ArrayList<>();
		for (int i = 0; i <= N; i++) {
			g.add(new ArrayList<>());
		}
        in = new int[N+1];
        inq = new boolean[N+1];
        
		for (int[] e : edges) {
			in[e[0]]++; 	 in[e[1]]++;
			g.get(e[1]).add(e[0]);
            g.get(e[0]).add(e[1]);
		}
		topo();
		for (int i = edges.length-1; i != 0; i--) {
			int u = edges[i][0], v = edges[i][1];
			if (in[u] > 1 && in[v] > 1)
				return edges[i];
		}
		return new int[2];
    }
	private void topo() {
		Queue<Integer> q = new ArrayDeque<>();
		for (int i  = 0; i < in.length; i++) {
			if (in[i] == 1) {
				q.add(i);
				inq[i] = true;
			}
		}
		while (!q.isEmpty()) {
			int v = q.poll();
			inq[v] = false;
			for (int nei : g.get(v)) {
                if (inq[nei])
                    continue;
				in[nei]--;
				if (in[nei] == 1) {
					q.add(nei);
					inq[nei] = true;
				}
			}
		}
	}
}

Complexity analysis

  • time complexity: O ( ) O()
  • Space complexity: O ( ) O()
Published 714 original articles · praised 199 · 50,000+ views

Guess you like

Origin blog.csdn.net/qq_43539599/article/details/105583381