ALGORITHMS AND COMPLEXITY
def hcf(a, b): ''' Calculate the Highest Common Factor of both numbers ''' print(f'Highest Common Factor of {a} and {b}...') x = a y = b counter = 0 while x != y: counter += 1 if x > y: x = x - y print(f'\tAdjust X to: {x:6}') elif x < y: y = y - x print(f'\tAdjust Y to: {y:6}') print(f'Highest Common Factor is: {x:6}') print(f'Iterations used: {counter}\n\n') hcf(97, 83) hcf(10, 25) hcf(14, 147) hcf(34565, 2000) graph = { 'A' : ['B','C'], 'B' : ['D', 'G'], 'C' : ['E'], 'D' : ['C'], 'E' : ['F'], 'F' : [], 'G' : [] } bfi={} visited = set() # List to keep track of visited nodes. queue= [] #Initialize a level def bfs( graph, starting_node): queue.append(starting_node) for key in graph: bfi[key]=0 level=1 bfi[starting_node]=level while queue: s = queue.pop(0) print (s) for vertex in graph[s]: if bfi[vertex]==0: level += 1 bfi[vertex]=level visited.add((s,vertex)) print(s,vertex) queue.append(vertex) print (bfi) #note set order is not in order of adding print (visited) # Driver Code bfs( graph, 'A')