Press "Enter" to skip to content

Posts tagged as “graph”

花花酱 LeetCode 952. Largest Component Size by Common Factor

Problem

Given a non-emptyĀ array of unique positive integersĀ A, consider the following graph:

  • There areĀ A.lengthĀ nodes, labelledĀ A[0]Ā toĀ A[A.length - 1];
  • There is an edge betweenĀ A[i]Ā andĀ A[j]Ā if and only ifĀ A[i]Ā andĀ A[j]Ā share a common factor greater than 1.

Return the size of the largest connected component in the graph.

Example 1:

Input: [4,6,15,35]
Output: 4

Example 2:

Input: [20,50,9,63]
Output: 2

Example 3:

Input: [2,3,6,7,4,12,21,39]
Output: 8

Note:

  1. 1 <= A.length <= 20000
  2. 1 <= A[i] <= 100000

Solution: Union Find

For each number, union itself with all its factors.

E.g. 6, union(6,2), union(6,3)

Time complexity:Ā \( O(\Sigma{sqrt(A[i])})Ā Ā \)

Space complexity: \( O(max(A)) \)

C++

Python3

花花酱 LeetCode 924. Minimize Malware Spread

Problem

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.

 

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

Note:

  1. 1 < graph.length = graph[0].length <= 300
  2. 0 <= graph[i][j] == graph[j][i] <= 1
  3. graph[i][i] = 1
  4. 1 <= initial.length < graph.length
  5. 0 <= initial[i] < graph.length

Solution: BFS

Time complexity: O(n^3)

Space complexity: O(n^2)

C++

 

花花酱 LeetCode 886. Possible Bipartition

Problem

Given a set ofĀ NĀ people (numberedĀ 1, 2, ..., N), we would like to split everyone into two groups ofĀ anyĀ size.

Each person may dislike some other people, and they should not go into the same group.

Formally, ifĀ dislikes[i] = [a, b], it means it is not allowed to put the people numberedĀ aĀ andĀ bĀ into the same group.

ReturnĀ trueĀ if and only if it is possible to split everyone into two groups in this way.

Example 1:

Input: N = 4, dislikes = [[1,2],[1,3],[2,4]]
Output: true
Explanation: group1 [1,4], group2 [2,3]

Example 2:

Input: N = 3, dislikes = [[1,2],[1,3],[2,3]]
Output: false

Example 3:

Input: N = 5, dislikes = [[1,2],[2,3],[3,4],[4,5],[1,5]]
Output: false

Note:

  1. 1 <= N <= 2000
  2. 0 <= dislikes.length <= 10000
  3. 1 <= dislikes[i][j] <= N
  4. dislikes[i][0] < dislikes[i][1]
  5. There does not existĀ i != jĀ for whichĀ dislikes[i] == dislikes[j].

 



Solution: Graph Coloring

Color a node with one color, and color all it’s disliked nodes with another color, if can not finish return false.

Time complexity: O(V+E)

Space complexity: O(V+E)

C++ / DFS

C++ / BFS

Related Problem

花花酱 LeetCode 882. Reachable Nodes In Subdivided Graph

Problem

Starting with anĀ undirectedĀ graph (the “original graph”) with nodes fromĀ 0Ā toĀ N-1, subdivisions are made to some of the edges.

The graph is given as follows:Ā edges[k]Ā is a list of integer pairsĀ (i, j, n)Ā such thatĀ (i, j)Ā is an edge of the original graph,

andĀ nĀ is the total number ofĀ newĀ nodes on that edge.

Then, the edgeĀ (i, j)Ā is deleted from the original graph,Ā nĀ new nodesĀ (x_1, x_2, ..., x_n)Ā are added to the original graph,

andĀ n+1Ā newĀ edgesĀ (i, x_1), (x_1, x_2), (x_2, x_3), ..., (x_{n-1}, x_n), (x_n, j)Ā are added to the originalĀ graph.

Now, you start at nodeĀ 0Ā from the original graph, and in each move, you travel along oneĀ edge.

Return how many nodes you can reach in at mostĀ MĀ moves.

 

Example 1:

Input: edge = [[0,1,10],[0,2,1],[1,2,2]], M = 6, N = 3 
Output: 13 
Explanation:  The nodes that are reachable in the final graph after M = 6 moves are indicated below. 

Example 2:

Input: edges = [[0,1,4],[1,2,6],[0,2,8],[1,3,1]], M = 10, N = 4 
Output: 23

Note:

  1. 0 <= edges.length <= 10000
  2. 0 <= edges[i][0] <Ā edges[i][1] < N
  3. There does not exist anyĀ i != jĀ for whichĀ edges[i][0] == edges[j][0]Ā andĀ edges[i][1] == edges[j][1].
  4. The original graphĀ has no parallel edges.
  5. 0 <= edges[i][2] <= 10000
  6. 0 <= M <= 10^9
  7. 1 <= N <= 3000

Solution: Dijkstra Shortest Path

Compute the shortest from 0 to rest of the nodes. Use HP to mark the maximum moves left to reach each node.

HP[u] = a, HP[v] = b, new_nodes[u][v] = c

nodes covered between a<->b = min(c, a + b)

Time complexity: O(ElogE)

Space complexity: O(E)

C++

Optimized Dijkstra (replace hashmap with vector)

Using SPFA

 

BFS

 

花花酱 LeetCode 785. Is Graph Bipartite?

Video is forĀ čŠ±čŠ±é…± LeetCode 886. Possible Bipartition, but the algorithm is exact the same.

Problem

https://leetcode.com/problems/is-graph-bipartite/

Given an undirectedĀ graph, returnĀ trueĀ if and only if it is bipartite.

Recall that a graph isĀ bipartiteĀ if we can split it’s set of nodes into two independentĀ subsets A and B such that every edge in the graph has one node in A and another node in B.

The graph is given in the following form:Ā graph[i]Ā is a list of indexesĀ jĀ for which the edge between nodesĀ iĀ andĀ jĀ exists.Ā  Each node is an integer betweenĀ 0Ā andĀ graph.length - 1.Ā  There are no self edges or parallel edges:Ā graph[i]Ā does not containĀ i, and it doesn’t contain any element twice.

Example 1:
Input: [[1,3], [0,2], [1,3], [0,2]]
Output: true
Explanation: 
The graph looks like this:
0----1
|    |
|    |
3----2
We can divide the vertices into two groups: {0, 2} and {1, 3}.
Example 2:
Input: [[1,2,3], [0,2], [0,1,3], [0,2]]
Output: false
Explanation: 
The graph looks like this:
0----1
| \  |
|  \ |
3----2
We cannot find a way to divide the set of nodes into two independent subsets.

 

Note:

  • graphĀ will have length in rangeĀ [1, 100].
  • graph[i]Ā will contain integers in rangeĀ [0, graph.length - 1].
  • graph[i]Ā will not containĀ iĀ or duplicate values.
  • The graph is undirected: if any elementĀ jĀ is inĀ graph[i], thenĀ iĀ will be inĀ graph[j].

Solution: Graph Coloring

For each node

  • If has not been colored, color it to RED(1).
  • Color its neighbors with a different color RED(1) to BLUE(-1) or BLUE(-1) to RED(-1).

If we can finish the coloring then the graph is bipartite. All red nodes on the left no connections between them and all blues nodes on the right, again no connections between them. red and blue nodes are neighbors.

Time complexity: O(V+E)

Space complexity: O(V)

C++ / DFS

Related Problem