Created
December 27, 2021 18:49
-
-
Save mailtodanish/24fd86ee79f1c6f41e93d4b6b11a4208 to your computer and use it in GitHub Desktop.
Algorithm that can detect the routers with the highest number of connections
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 1 -> 2 -> 3 -> 5 -> 2 -> 1 | |
sample_input_graph_dict_1={ | |
"1":["2"], | |
"2":["3","1"], | |
"3":["5"], | |
"4":[], | |
"5":["2"], | |
"6":[], | |
} | |
# 1 -> 3 -> 5 -> 6 -> 4 -> 5 -> 2 -> 6 | |
sample_input_graph_dict_2={ | |
"1":["3"], | |
"2":["6"], | |
"3":["5"], | |
"4":["5"], | |
"5":["6","2"], | |
"6":["4"], | |
} | |
# 2 -> 4 -> 6 -> 2 -> 5 -> 6 | |
sample_input_graph_dict_3={ | |
"2":["4","5"], | |
"4":["6"], | |
"6":["2"], | |
"5":["6"] | |
} | |
def identify_router(input_graph): | |
temp={} | |
for k in input_graph.keys(): | |
if not temp.get(k,None): | |
temp[k]=0 | |
temp[k]= temp[k] + len(input_graph[k]) #count output connections | |
for i in input_graph[k]: | |
if not temp.get(i,None): | |
temp[i]=0 | |
temp[i]= temp[i] + 1 #count input connections | |
maxValue = max(temp.values()) | |
return [k for k, v in temp.items() if v == maxValue] | |
print("max connection for dict_1: ",identify_router(sample_input_graph_dict_1)) | |
print("max connection for dict_2: ",identify_router(sample_input_graph_dict_2)) | |
print("max connection for dict_3: ",identify_router(sample_input_graph_dict_3)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
max connection for dict_1: ['2']
max connection for dict_2: ['5']
max connection for dict_3: ['2', '6']