Question:
Given an undirected graph of N nodes and N-1 edges. The length of all edges are 1. Each edge i has a weight of Wi. (0 <= Wi <= 10.000)
The graph is guaranteed to not have any loops. So there's always one (and only) shortest path between 2 nodes.
Take a pair of node (u, v) from the graph:
Given the number K, count the number of pair (u, v) from the graph such that w - l >= K
Example:
N = 3, K = 1
Edges:
1 - 2 - 3
1 - 3 - 2
(Edges are described as: u - v - w)
Answer: 6. All the possible pairs are: (1, 2), (1, 3), (2, 1), (2, 3), (3, 1), (3, 2)
Brute Force Solution (N < 100):
Go over all pairs of (u, v), find the shortest path along side with w and l. Increase the answer if w - l >= K.
Time complexity: O(N^3)
P/S: I have tried and succeeded the brute force solution (with N < 100). But I'm struggling to find a solution with N up to 100.000
Given an undirected graph and a set of vertices, we have to count the number of non-reachable nodes from the given head node using a depth-first search. In this graph, if we consider 0 as a head node, then the node 0, 1 and 2 are reachable. We mark all the reachable nodes as visited.
First, mark all vertices as unvisited. Iterate over all vertices. If a vertex is not visited, perform DFS on that vertex and increment the count by 1. After iterating over all vertices, the value of count will be the number of connected components in the graph.
Here for finding the number of cycles in the undirected graph, the time complexity is the same as DFS traversal, i.e., O(V+E), where V is the number of vertices and E is the number of edges in the graph. Space complexity is O(V) as extra color[ ] and par[ ] is used here.
The maximum number of edges a graph with N vertices can contain is X = N * (N – 1) / 2. Hence, the total number of graphs that can be formed with n vertices will be: C0 + XC1 + XC2 + … + XCX = 2X.
Count Pairs Of Nodes You are given an undirected graph defined by an integer n, the number of nodes, and a 2D integer array edges, the edges in the graph, where edges [i] = [u i, v i] indicates that there is an undirected edge between u i and v i. You are also given an integer array queries.
There is an undirected graph with n nodes, numbered from 0 to n - 1. You are given a 2D integer array edges where edges [i] = [a i, b i] denotes that there exists an undirected edge connecting nodes a i and b i. Return the number of pairs of different nodes that are unreachable from each other.
Consider below undirected graph with two disconnected components: In this graph, if we consider 0 as a head node, then the node 0, 1 and 2 are reachable. We mark all the reachable nodes as visited.
We mark all the reachable nodes as visited. All those nodes which are not marked as visited i.e, node 3 and 4 are non-reachable nodes. Hence their count is 2. Recommended: Please try your approach on {IDE} first, before moving on to the solution.
To work out user202729's hint:
Find the centroid (vertex whose removal leaves subtrees that each have at most half of the vertices of the whole tree).
Count the pairs (u, v) whose path contains the centroid.
Delete the centroid and operate recursively on each of the subtrees.
Step 1 is O(n) (there's a standard algorithm) and Step 2 will be O(n log n), so the Master Theorem gives O(n log2 n) for the whole.
To implement Step 2, root the tree at the centroid and calculate, for the centroid and each of its children, the number of descendants at each depth. Put the results in Fenwick trees for O(log n)-time prefix sums.
Now, sort the edges by weight. Starting with the heaviest edge e and working our way to the lightest, we count the pairs whose path has e as its heaviest edge. The edge e connects a parent and a child; let x be the child. For each (improper, so including x) descendant u of x, let ℓ* = w − K be the greatest ℓ such that w − ℓ ≥ K. With two prefix sums, count the number of vertices v in other subtrees at depth at most ℓ* − depth(u). Then issue updates to the Fenwick trees to remove u from the count.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With