0

the graph

How can i make a FindMaxDepth method / This function will find the longest depth of the tree (the distance between the root node and the last leaf node), the input that the function will take is: (graph, rootNode) and the output will be a numeric value.

Example: If the following graph is entered and the startNode = A, the output must be 4

graph = {
    "A": ["B", "C"],
    "B": ["D", "E"],
    "C": ["F"],
    "D": [],
    "E": ["F"],
    "F": [],
}
visited = []


def dfs(visited, graph, root):
    if root not in visited:
        visited.append(root)
        print(root)
        for child in graph[root]:
            dfs(visited, graph, child)


dfs(visited, graph, "A")


def bfs(graph, root):
    visited = []
    queue = []
    visited.append(root)
    queue.append(root)
    while queue:
        x = queue.pop(0)
        print(x)
        for child in graph[x]:
            if child not in visited:
                visited.append(child)
                queue.append(child)


# bfs(graph, "A")
MattDMo
  • 100,794
  • 21
  • 241
  • 231
  • You'll need to add a "depth" parameter to `dfs` and `bfs` (default value 0), passing `depth+1` each time you call it. Then you'll need to track `maxdepth` in a global. – Tim Roberts Dec 17 '21 at 19:54

3 Answers3

1

bfs can track this internally, since it isn't recursive. dfs needs a global (or simulated global) to track the maximum.

graph = {
    "A": ["B", "C"],
    "B": ["D", "E"],
    "C": ["F"],
    "D": [],
    "E": ["F"],
    "F": [],
}
visited = []


def dfs(visited, graph, root, depth=0):
    global maxdepth
    maxdepth = max(depth,maxdepth)
    if root not in visited:
        visited.append(root)
        print(root)
        for child in graph[root]:
            dfs(visited, graph, child, depth+1)


maxdepth = 0
dfs(visited, graph, "A")
print("max depth", maxdepth)


def bfs(graph, root):
    maxdepth = 0
    visited = []
    queue = []
    visited.append(root)
    queue.append((root,1))
    while queue:
        x,depth = queue.pop(0)
        maxdepth = max(maxdepth,depth)
        print(x)
        for child in graph[x]:
            if child not in visited:
                visited.append(child)
                queue.append((child,depth+1))
    return maxdepth

print("max depth", bfs(graph, "A"))
Tim Roberts
  • 48,973
  • 4
  • 21
  • 30
0

A simpler solution is to just add 1 to every child's depth:

def depth(root):
  children = graph[root]
  
  if len(children) == 0:
    return 1
  
  return 1 + max( depth(child) for child in children )
md2perpe
  • 3,372
  • 2
  • 18
  • 22
0

you don't need to track visited nodes for a tree structure because there will be no circular reference. A recursive traversal of nodes will suffice:

def maxDepth(graph,node):
    return 1 + max((maxDepth(graph,n) for n in graph[node]),default=0)

Output:

graph = {
    "A": ["B", "C"],
    "B": ["D", "E"],
    "C": ["F"],
    "D": [],
    "E": ["F"],
    "F": [],
}
print(maxDepth(graph,"A")) # 4
Alain T.
  • 40,517
  • 4
  • 31
  • 51