Remove Max Number of Edges to Keep Graph Fully Traversable

This page explains Java solution to problem Remove Max Number of Edges to Keep Graph Fully Traversable using Graph data structure.

Problem Statement

Alice and Bob have an undirected graph of n nodes and 3 types of edges:

  • Type 1: Can be traversed by Alice only.
  • Type 2: Can be traversed by Bob only.
  • Type 3: Can by traversed by both Alice and Bob.

Given an array edges where edges[i] = [typei, ui, vi] represents a bidirectional edge of type typei between nodes ui and vi, find the maximum number of edges you can remove so that after removing the edges, the graph can still be fully traversed by both Alice and Bob. The graph is fully traversed by Alice and Bob if starting from any node, they can reach all other nodes.

Return the maximum number of edges you can remove, or return -1 if it's impossible for the graph to be fully traversed by Alice and Bob.

Example 1:

Input: n = 4, edges = [[3,1,2],[3,2,3],[1,1,3],[1,2,4],[1,1,2],[2,3,4]]
Output: [[1,2],[2,3]]
Explanation: If we remove the 2 edges [1,1,2] and [1,1,3]. The graph will still be fully traversable by Alice and Bob. Removing any additional edge will not make it so. So the maximum number of edges we can remove is 2.

Example 2:

Input: n = 4, edges = [[3,1,2],[3,2,3],[1,1,4],[2,1,4]]
Output: 0
Explanation: Notice that removing any edge will not make the graph fully traversable by Alice and Bob.

Example 3:

Input: n = 4, edges = [[3,2,3],[1,1,2],[2,3,4]]
Output: -1

Solution

If you have any suggestions in below code, please create a pull request by clicking here.

package com.vc.hard;

class RemoveMaxNumberOfEdgesToKeepGraphFullyTraversable {
    public int maxNumEdgesToRemove(int n, int[][] edges) {
        //Edges Needed for Alice & Bob
        int[] parentArrAlice = new int[n + 1];
        int[] parentArrBob = new int[n + 1];
        for(int i = 0; i < parentArrBob.length; i++) {
            parentArrAlice[i] = i;
            parentArrBob[i] = i;
        }

        int toRemove = 0;
        //Process All Type 3 Edges, because both Alice & Bob can use it
        for(int i = 0; i < edges.length; i++) {
            if(edges[i][0] != 3) continue;

            int fromParentAlice = find(edges[i][1], parentArrAlice);
            int toParentAlice = find(edges[i][2], parentArrAlice);

            int fromParentBob = find(edges[i][1], parentArrBob);
            int toParentBob = find(edges[i][2], parentArrAlice);

            if(fromParentAlice == toParentAlice && fromParentBob == toParentBob) {
                toRemove++;
                continue;
            }

            parentArrAlice[fromParentAlice] = toParentAlice;
            parentArrBob[fromParentBob] = toParentBob;
        }

        //Process All Type 2 & 3 Edges
        for(int i = 0; i < edges.length; i++) {
            //Alice's Edge
            if(edges[i][0] == 1) {
                int fromParent = find(edges[i][1], parentArrAlice);
                int toParent = find(edges[i][2], parentArrAlice);

                if(fromParent != toParent) parentArrAlice[fromParent] = parentArrAlice[toParent];
                else toRemove++;
            }
            //Bob's Edge
            else if(edges[i][0] == 2) {
                int fromParent = find(edges[i][1], parentArrBob);
                int toParent = find(edges[i][2], parentArrBob);

                if(fromParent != toParent) parentArrBob[fromParent] = parentArrBob[toParent];
                else toRemove++;
            }
        }

        //Check if Alice & Bob can reach to all the nodes if not return -1;
        return check(parentArrAlice) && check(parentArrBob) ? toRemove : -1;
    }

    private int find(int x, int[] parentArr) {
        if(parentArr[x] == x) return x;
        else return parentArr[x] = find(parentArr[x], parentArr);
    }

    private boolean check(int[] parentArr) {
        int commonParent = find(parentArr[1], parentArr);
        for(int i = 2; i < parentArr.length; i++) {
            int elementParent = find(parentArr[i], parentArr);
            if(elementParent != commonParent) return false;
        }
        return true;
    }
}

Time Complexity

O(M * log(N)) Where
N is number of nodes
M is number of edges in an input array edges

Space Complexity

O(N) Where
N is number of nodes