🧠
Alternative Approach (Hash Map Frequency Counting)
💡 This approach uses a hash map to count node frequencies from edges, reinforcing the degree counting concept but with a different data structure.
Intuition
The center node appears in every edge, so it will have the highest frequency equal to n-1 in the hash map.
Algorithm
- Initialize an empty hash map to count node frequencies.
- Iterate over each edge and increment the count for both nodes in the map.
- Find the node with frequency equal to n-1 in the map.
- Return that node as the center.
💡 This approach is similar to degree counting but uses a hash map instead of an array, useful when node labels are not contiguous.
from typing import List
def findCenter(edges: List[List[int]]) -> int:
freq = {}
n = len(edges) + 1
for u, v in edges:
freq[u] = freq.get(u, 0) + 1
freq[v] = freq.get(v, 0) + 1
for node, count in freq.items():
if count == n - 1:
return node
# Driver code
if __name__ == '__main__':
edges = [[1,2],[2,3],[4,2]]
print(findCenter(edges)) # Output: 2
Line Notes
freq = {}Initialize empty dictionary for frequency counts
n = len(edges) + 1Calculate number of nodes
for u, v in edges:Iterate over edges to count frequencies
freq[u] = freq.get(u, 0) + 1Increment frequency for node u
freq[v] = freq.get(v, 0) + 1Increment frequency for node v
for node, count in freq.items():Iterate over frequency map to find center
if count == n - 1:Center node appears in all edges
return nodeReturn the center node
import java.util.*;
public class FindCenter {
public static int findCenter(int[][] edges) {
int n = edges.length + 1;
Map<Integer, Integer> freq = new HashMap<>();
for (int[] edge : edges) {
freq.put(edge[0], freq.getOrDefault(edge[0], 0) + 1);
freq.put(edge[1], freq.getOrDefault(edge[1], 0) + 1);
}
for (Map.Entry<Integer, Integer> entry : freq.entrySet()) {
if (entry.getValue() == n - 1) {
return entry.getKey();
}
}
return -1; // Should not happen
}
public static void main(String[] args) {
int[][] edges = {{1,2},{2,3},{4,2}};
System.out.println(findCenter(edges)); // Output: 2
}
}
Line Notes
int n = edges.length + 1;Calculate number of nodes
Map<Integer, Integer> freq = new HashMap<>();Initialize frequency map
for (int[] edge : edges)Iterate over edges to count frequencies
freq.put(edge[0], freq.getOrDefault(edge[0], 0) + 1);Increment frequency for first node
freq.put(edge[1], freq.getOrDefault(edge[1], 0) + 1);Increment frequency for second node
for (Map.Entry<Integer, Integer> entry : freq.entrySet())Iterate over map entries
if (entry.getValue() == n - 1)Check if frequency matches center criteria
return entry.getKey();Return center node
#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;
int findCenter(vector<vector<int>>& edges) {
int n = edges.size() + 1;
unordered_map<int, int> freq;
for (auto& edge : edges) {
freq[edge[0]]++;
freq[edge[1]]++;
}
for (auto& [node, count] : freq) {
if (count == n - 1) {
return node;
}
}
return -1; // Should not happen
}
int main() {
vector<vector<int>> edges = {{1,2},{2,3},{4,2}};
cout << findCenter(edges) << endl; // Output: 2
return 0;
}
Line Notes
int n = edges.size() + 1;Calculate number of nodes
unordered_map<int, int> freq;Initialize frequency map
for (auto& edge : edges)Iterate over edges to count frequencies
freq[edge[0]]++;Increment frequency for first node
freq[edge[1]]++;Increment frequency for second node
for (auto& [node, count] : freq)Iterate over map entries
if (count == n - 1)Check if frequency matches center criteria
return node;Return center node
function findCenter(edges) {
const freq = new Map();
const n = edges.length + 1;
for (const [u, v] of edges) {
freq.set(u, (freq.get(u) || 0) + 1);
freq.set(v, (freq.get(v) || 0) + 1);
}
for (const [node, count] of freq.entries()) {
if (count === n - 1) {
return node;
}
}
return -1; // Should not happen
}
// Test
const edges = [[1,2],[2,3],[4,2]];
console.log(findCenter(edges)); // Output: 2
Line Notes
const freq = new Map();Initialize frequency map
const n = edges.length + 1;Calculate number of nodes
for (const [u, v] of edges)Iterate over edges to count frequencies
freq.set(u, (freq.get(u) || 0) + 1);Increment frequency for node u
freq.set(v, (freq.get(v) || 0) + 1);Increment frequency for node v
for (const [node, count] of freq.entries())Iterate over map entries
if (count === n - 1)Check if frequency matches center criteria
return node;Return center node
We iterate over all edges once and store counts in a hash map, then iterate over map entries once.
💡 For n=100,000, this means about 200,000 operations plus hash map overhead, still efficient.
Interview Verdict: Accepted
This approach is flexible and useful when node labels are not guaranteed to be contiguous integers.