def DFS(graph, src, prev_len,
max_len, visited):
visited[src] = 1
curr_len = 0
adjacent = None
for i in range(len(graph[src])):
adjacent = graph[src][i]
if (not visited[adjacent[0]]):
curr_len = prev_len + adjacent[1]
DFS(graph, adjacent[0], curr_len,
max_len, visited)
if (max_len[0] < curr_len):
max_len[0] = curr_len
curr_len = 0
def longestCable(graph, n):
max_len = [-999999999999]
for i in range(1, n + 1):
visited = [False] * (n + 1)
DFS(graph, i, 0, max_len, visited)
return max_len[0]
if __name__ == '__main__':
n = 6
graph = [[] for i in range(n + 1)]
graph[1].append([2, 3])
graph[2].append([1, 3])
graph[2].append([3, 4])
graph[3].append([2, 4])
graph[2].append([6, 2])
graph[6].append([2, 2])
graph[4].append([6, 6])
graph[6].append([4, 6])
graph[5].append([6, 5])
graph[6].append([5, 5])
print("Maximum length of cable =",
longestCable(graph, n))
No comments:
Post a Comment