Algorithm design, implement an algorithm for a directed graph -
i found interesting question related algorithm design, wasn't able solve properly.
given directed graph g = (v,e), uses adjancency lists, , integer k < |v|, implement linear-time-complexity algorithm ( o(n) ) , check if graph g has @ least k vertexes same indegree number.
suppose n == |v| + |e|
it enough pass through edges, or through edge in-nodes, , maintain number of vertices possible indegrees.
sketch of method in pyhon style:
def check(graph, k): # each vertex count indegree indegrees = [0] * graph.number_of_nodes() # 'maps' number of vertices indegree num_with_indegree = [graph.number_of_nodes()] + [0] * (graph.number_of_nodes()-2) # pass through edge innodes. # iteration easy implement adjancency list graph implementation. in_node in graph.in_nodes(): # increase indegree node indegrees[in_node] += 1 # 'move' vertex it's indegree bucket indegree = indegrees[in_node] num_with_indegree[indegree-1] -= 1 num_with_indegree[indegree] += 1 # returns true if bucket has @ least k vertices return any(n >= k n in num_with_indegree)
Comments
Post a Comment