algorithm - Efficiently find the depth of a graph from every node -
i have problem find minimum possible depth of graph implies have find maximum depth each node , return least of them all. simple dfs each node trick when things crazy extremely large input, dfs becomes inefficient (time limit). tried keeping distance of each leaf node being explored in memory didn't much.
how efficiently find minimum depth of large graph. worthy of note graph in question has no cycle.
to find graph centre/center of undirected tree graph could:
- do dfs find list of leaf nodes o(n)
- remove these leaf nodes graph , note during deletion new nodes become leaf nodes
- repeat step 2 until graph deleted
the node/nodes deleted in last stage of algorithm graph centres of tree.
each node deleted once, whole process can done in o(n).
Comments
Post a Comment