Title:

Minimize the Spread of Malware - Strategy 2

Link to LeetCode:

https://leetcode.com/problems/minimize-malware-spread/

Specification:

In a network of nodes, each node i is directly connected to another node j if and only if graph[i][j] = 1.
Some nodes initial are initially infected by malware.
Whenever two nodes are directly connected and at least one of those two nodes is infected by malware, both nodes will be infected by malware.
This spread of malware will continue until no more nodes can be infected in this manner.

Suppose M(initial) is the final number of nodes infected with malware in the entire network, after the spread of malware stops.
We will remove one node from the initial list. Return the node that if removed, would minimize M(initial) .
If multiple nodes could be removed to minimize M(initial), return such a node with the smallest index.
Note that if a node was removed from the initial list of infected nodes, it may still be infected later as a result of the malware spread.

Note:
1 < graph.length = graph[0].length <= 300
0 <= graph[i][j] == graph[j][i] <= 1
graph[i][i] = 1
1 <= initial.length < graph.length
0 <= initial[i] < graph.length

Examples:

Example 1:

Input: graph = {{1,1,0},{1,1,0},{0,0,1}}, initial = {0,1}
Output: 0

Example 2:

Input: graph = {{1,0,0},{0,1,0},{0,0,1}}, initial = {0,2}
Output: 0

Example 3:

Input: graph = {{1,1,1},{1,1,1},{1,1,1}}, initial = {1,2}
Output: 1

Initial Code and Signature:

public interface MalwareSpreadMinimizer {

	
	public int minMalwareSpread(int[][] graph, int[] initial);
}

Algorithm:

In an undirected graph, if two nodes (let's say A and B) are neighbors,
their reachability graphs are the same,
i.e.; whatever node is reachable from A is reachable from B, and vice versa,
i.e.; R (A) = R (B); R(A) and R (B) are initially equal to N (A) U N (B).
Therefore, neighbors can be unified and considered as one larger virtual node.

  1. Start from a given node n.
  2. Enqueue neighbors of the given node (other than the node itself) [N(n)] if not visited.
  3. Mark the neighbors as visited.
  4. Neighbor i = Dequeue;
  5. R (n) = N (n) U N (i).
  6. R (i) = R (n).
  7. Go to (2-) and repeat.
  8. Repeat until the queue is empty.

Run-Time Analysis:

GitHub Project:

Code:


import java.util.*;
/**
 * 
 * @author Mahsa Sadi
 * @since 2020-02-24
 *
 */

public class MalwareSpreadMinimizerV3 implements MalwareSpreadMinimizer {

	/**
	 * In an undirected graph, if two nodes (let's say A and B) are neighbors, their
	 * reachability graphs are the same, i.e.; whatever nodes is reachable from A is
	 * reachable from B, and vice versa, i.e.; * R (A) = R (B) = N (A) U N (B)
	 * Therefore, neighbor can be unified and considered as one larger virtual node.
	 * 
	 * 
	 * 
	 * 
	 * Solution:
	 * 
	 * 1- Start from a given node n. 
	 * 2- Enqueue neighbors of the given node (other than the node itself): (N(n)).
	 * 3- mark the neighbors as visited. 
	 * 4- neighbor i = Dequeue; 
	 * 5- R (n) = N (n) U N (i).
	 * 6- R (i) = R (n).
	 * 7- Go to (2-) and repeat.
	 * 8- Repeat until the queue is empty.
	 * 
	 */
	int mostInfluentialNode;
	int maxInfluence;
	int[][] reachabilityGraph;
	int numberOfNodes;
	int[] visited;

	int[] initialized;

	public int minMalwareSpread(int[][] graph, int[] initial) {

		numberOfNodes = graph.length;

		mostInfluentialNode = -1;
		maxInfluence = -1;

		reachabilityGraph = new int[numberOfNodes][numberOfNodes];
		

		visited = new int[numberOfNodes];
		initialized = new int[numberOfNodes];

		for (Integer infectedNode : initial) {
			findReachableNodesFor(graph, infectedNode);

			int sizeOfReachableNodes = 0;

			for (Integer i : reachabilityGraph[infectedNode])
				sizeOfReachableNodes += i;

			if ((sizeOfReachableNodes >= maxInfluence && infectedNode < mostInfluentialNode)
					|| mostInfluentialNode == -1) {
				mostInfluentialNode = infectedNode;
				maxInfluence = sizeOfReachableNodes;

			}

		}

		return mostInfluentialNode;

	}

	public void findReachableNodesFor(int[][] graph, int givenNode) {

		List<Integer> queue = new ArrayList<Integer>();

		if (visited[givenNode] != 1)

		{

			int currentNode = givenNode;
			visited[currentNode] = 1;

			boolean traverseFinished = false;

			while (!traverseFinished) {
				for (int j = 0; j < graph[currentNode].length; j++) {
					if (graph[currentNode][j] == 1 && visited[j] != 1 && j != currentNode) {
						queue.add(j);
						visited[j] = 1;
					}
				}

				if (!queue.isEmpty()) {
					currentNode = queue.remove(0);

					if (initialized[currentNode] != 1) {

						reachabilityGraph[currentNode] = graph[currentNode];
						initialized[currentNode] = 1;

					}

					if (initialized[givenNode] != 1) {

						reachabilityGraph[givenNode] = graph[givenNode];
						initialized[currentNode] = 1;

					}

					unify(givenNode, currentNode);
				}

				else
					traverseFinished = true;

			}

		}

	}

	public void unify(int givenNode, int currentNode) {

		for (int i = 0; i < numberOfNodes; i++) {
			if (reachabilityGraph[givenNode][i] + reachabilityGraph[currentNode][i] > 0)
				reachabilityGraph[givenNode][i] = 1;

			// The following else is unnecessary and can be removed.
			else
				reachabilityGraph[givenNode][i] = 0;
		}

		reachabilityGraph[currentNode] = reachabilityGraph[givenNode];

	}

}