-
Notifications
You must be signed in to change notification settings - Fork 253
Maximal Network Rank
- 🔗 Leetcode Link: https://leetcode.com/problems/maximal-network-rank
- 💡 Problem Difficulty: Medium
- ⏰ Time to complete: __ mins
- 🛠️ Topics: Graphs
- 🗒️ Similar Questions: TBD
Understand what the interviewer is asking for by using test cases and questions about the problem.
- Established a set (2-3) of test cases to verify their own solution later.
- Established a set (1-2) of edge cases to verify their solution handles complexities.
- Have fully understood the problem and have no clarifying questions.
- Have you verified any Time/Space Constraints for this problem?
- How can you efficiently check if there is a road connecting two different cities?
- Would it be efficient to use a matrix to save all the edges between vertex?
- If the edge is close to
city_1
andcity_2
, then during counting, are they counted twice?
HAPPY CASE
Input: n = 5, roads = [[0,1], [0,3], [1,2], [1,3], [2,3], [2,4]]
Output: 5
HAPPY CASE
Input: n = 8, roads = [[0,1], [1,2], [2,3], [2,4], [5,6], [5,7]]
Output: 5
EDGE CASE
Input: n = 4, roads = []
Output: 0
Match what this problem looks like to known categories of problems, e.g. Linked List or Dynamic Programming, and strategies or patterns in those categories.
- Use a hashtable of size `n` which stores sets for each city. Members of the sets are cities which are directly connected to the city the set corresponds to. Then, examine all unique pairs `(city_1, city_2)` and sum up the edges of both cities. If there is an edge between both cities, the sum needs to be reduced by one since the edge is counted twice. We store the highest sum of edges and return it.
Plan the solution with appropriate visualizations and pseudocode.
1) Use a hashtable of size `n` which stores sets for each city. Members of the sets are cities which are directly connected to the city the set corresponds to.
2) Then, examine all unique pairs `(city_1, city_2)`
3) Sum up the edges of both cities.
4) If there is an edge between both cities, the sum needs to be reduces by 1 since the edge is counted 2x.
4) Then, store the highest sum of edges and return it.
- A common mistake would be iterating 2 times, in other words, 2 for loops, to find the 1st and 2nd maximum values while traversing the loop. The 2 cities with most connections may not be necessarily connected with each other, and if they are connected, the common connection is counted only once. You go through every single road and in the worst case, every pair of nodes might be connected to each other, so you will have n^2 entries.
Implement the code to solve the algorithm.
To find the edges of nodes and finding the best pair of nodes to maximize the answer.
class Solution {
public int maximalNetworkRank(int n, int[][] roads) {
# create hashmap
Map<Integer, Set<Integer>> map = buildMap(n, roads);
int ans = 0;
# Try every pair of different cities and calculate its network rank.
# The network rank of two vertices is sum of their degrees discarding the common edge.
# For all combinations of nodes check network rank.
# If two nodes are connected then consider the edge between them only once,
# that is add -1 to sum of their indegrees else add 0.
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i != j) {
int sum = map.get(i).size() + map.get(j).size();
if (map.get(i).contains(j)) {
sum--;
}
ans = Math.max(ans, sum);
}
}
}
return ans;
}
private Map<Integer, Set<Integer>> buildMap(int n, int[][] roads) {
Map<Integer, Set<Integer>> map = new HashMap<>();
for (int i = 0; i < n; i++) {
map.put(i, new HashSet<>());
}
for (int[] road : roads) {
map.get(road[0]).add(road[1]);
map.get(road[1]).add(road[0]);
}
return map;
}
}
class Solution:
def maximalNetworkRank(self, n: int, roads: List[List[int]]) -> int:
# use a set to store the neighbors
city_to_cities = [ set() for i in range( n ) ]
max_network_rank = 0
# check each pair of cities, add their ranks together
# For each (i, j) pair, if i is the neighbor of j or the vice versa,
# we minus 1 on the rank
for road in roads:
city_to_cities[ road[0] ].add( road[1] )
city_to_cities[ road[1] ].add( road[0] )
for city_1 in range( n ):
for city_2 in range( city_1 + 1, n ):
network_rank = len( city_to_cities[city_1] ) + len( city_to_cities[city_2] )
if ( city_1 in city_to_cities[city_2] ):
network_rank -= 1
max_network_rank = max(max_network_rank, network_rank)
return max_network_rank
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
- Trace through your code with an input to check for the expected output
- Catch possible edge cases and off-by-one errors and verify the code works for the happy and edge cases you created in the “Understand” section
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
-
Time Complexity:
O(E + V^2)
, where V represents the vertices and E represents the edges. Note: The worst case runtime isO(V^2)
. This will happen when all nodes have the same amount of edges. -
Space Complexity:
O(E)
, accounting for the use of a hash table to store the neighbors of each city