# Finding the closest node

I have a function that I need to optimize:

def search(graph, node, maxdepth = 10, depth = 0):
nodes = []
for neighbor in graph.neighbors_iter(node):
if graph.node[neighbor].get('station', False):
return neighbor
nodes.append(neighbor)
for i in nodes:
if depth+1 > maxdepth:
return False
if search(graph, i, maxdepth, depth+1):
return i
return False


graph should be a networkx graph object. How can I optimize this? This should find the closest node in the network with 'station' attribute to True.

• You might have a bug: if search(graph, i, ...): return i ==> the search() function might return something other than i, yet you are returning i. – Hai Vu Mar 31 '13 at 1:17
• Not a bug, this should return the first step to the station. – fread2281 Mar 31 '13 at 1:18

def search(graph, node, maxdepth = 10, depth = 0):
nodes = []
for neighbor in graph.neighbors_iter(node):
if graph.node[neighbor].get('station', False):
return neighbor
nodes.append(neighbor)


Why store the neighbor in the list? Instead of putting it in a list, just combine your two loops.

for i in nodes:


i typically stands for index. I suggest using neighbor to make your code easier to follow

    if depth+1 > maxdepth:
return False


This doesn't relate to this individual node. What is it doing inside this loop?

        if search(graph, i, maxdepth, depth+1):
return i
return False


Failure to find is better reported using None rather than False.

• The first loop needs to be run on all litems and if none are station, it should search recursively – fread2281 Mar 30 '13 at 19:01
• @fread2281, my bad... I didn't think of that. Actually, I think that makes your code wrong. Its implementing a DFS, so it wasn't neccesairlly find the closest "station" – Winston Ewert Mar 30 '13 at 19:32

Is there any reason that you are shy from using networkx's functions such as breadth-first search bfs_tree() or depth-first search dfs_tree()? Here is an example of breadth-first search:

import networkx as nx
...
for visiting_node in nx.bfs_tree(graph, node):
if graph.node[visiting_node].get('station', False):
print 'Found it' # Do something with visiting_node

• Good point, although it seems he couldn't do the max_depth with it... – Winston Ewert Mar 31 '13 at 1:06
• I think max_depth is to prevent circular links in a graph. the bfs_tree() function ensures no circular references. – Hai Vu Mar 31 '13 at 1:12
• Ah, you may well be right. – Winston Ewert Mar 31 '13 at 1:14
• max_depth is to limit time. I need speed. – fread2281 Mar 31 '13 at 1:17
• I reimplemented it using a custom BFS and thanks for the indirect pointer! – fread2281 Mar 31 '13 at 16:54

Are you implementing some routing protocol? Can you give me more details?

If your networking is based on equal weight (length) of each edges (let's say wired network or wireless network with backbone base station), than you can use breadth first search (fastest searching algorithm which takes $O(|V|+|E|)$ times). If you want to find closest nodes base on geometry distance(it could be wireless networks or MANET ad-hoc network), than you need a greedy algorithm.

There are two ways to optimize your func:

• Searching algorithm: If you are looking for the shortest path or closest node in geometry distance, than you'd better to use greedy approach, BFS return shortest path only when edges have equal weight (cost, length, etc..). You probably know Dijkstra algorithm because it's widely used in network routings. So use a greedy algorithm would make your network links more stable and robust. You can choose Best first search, Dijkstra algorithm, A* algorithm(if you have information of target nodes).

Although Breadth first search and depth first search are available to use in python, but they have some limitations: BFS tree is based on the entry of your neighbor list, it's won't guarantee to find shortest path (it's not a greedy algorithm). Same to DFS and your algorithm.

• choose a proper data structure to build your graph: A good data structure is always the easiest way to make your program faster. If you have more edges then vertexes ($E > V^2$) than use a neighbor matrix. Because in this scenario, if you are using a neighbor list, it will take a considerable time to find out each edge whether belong to your graph or not.

As a side effect, the memory overhead is huge for neighbor matrix.

• My code should be a simple breadth first search. – fread2281 May 10 '14 at 5:08
• average edges per vertex? are you using this for wired or wireless? – Charles Chow May 10 '14 at 5:12
{"ft_lang_label":"__label__en","ft_lang_prob":0.7561696,"math_prob":0.79700035,"size":663,"snap":"2019-51-2020-05","text_gpt3_token_len":174,"char_repetition_ratio":0.14719272,"word_repetition_ratio":0.0,"special_character_ratio":0.2820513,"punctuation_ratio":0.2112676,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.97079897,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2019-12-07T02:46:02Z\",\"WARC-Record-ID\":\"<urn:uuid:b2e9a7c7-9b00-41b0-97fd-0d227fa49125>\",\"Content-Length\":\"163483\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:446ad597-77d6-4fb0-94ec-1db5ea9c2267>\",\"WARC-Concurrent-To\":\"<urn:uuid:4ed236b7-5eb8-4311-a81e-6196eaecb340>\",\"WARC-IP-Address\":\"151.101.65.69\",\"WARC-Target-URI\":\"https://codereview.stackexchange.com/questions/24546/finding-the-closest-node\",\"WARC-Payload-Digest\":\"sha1:AIO7E5KYBDB5CBV6MCAYKI3NHFR7YRLZ\",\"WARC-Block-Digest\":\"sha1:5BOVEAMBEHNB6X546BTOJW2ZDMJVDGR3\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2019/CC-MAIN-2019-51/CC-MAIN-2019-51_segments_1575540491871.35_warc_CC-MAIN-20191207005439-20191207033439-00028.warc.gz\"}"}