def select_random_nodes(digraph, m, indeg=None, sum=-1): ''' Randomly select nodes based on probability as per DPA :param V: input indeg dictionaries :return: a list of selected nodes V1 ''' if indeg == None: indeg = graph_util.compute_in_degrees(digraph) sum = totalindeg(indeg) prob = [(indeg[j] + 1.0) / (sum + len(digraph)) for j in digraph.keys()] # print digraph.keys() # print prob V1 = nm.random.choice(digraph.keys(), m, True, prob) return V1
def DPA(n, m): ''' DPA Algorithm to generate random directed graphs :param n: number of nodes :param m: integer (1 <= m <= n) :return:A directed graph ''' gE = graph_util.make_complete_graph(m) indeg = graph_util.compute_in_degrees(gE) totindeg = totalindeg(indeg) for i in range(m, n): V1 = select_random_nodes(gE, m, indeg, totindeg) gE[i] = set(V1) for j in V1: indeg[j] = indeg[j] + 1 indeg[i] = 0 totindeg = totindeg + len(V1) return gE