def add_edge_to_graph(G, U, M, num, val): # Adding an edge to the exisiting Graph. for x in range(num): u = get_random_user(U) # val = random() for m in M: if (m.cumulative_prob > val): G.insert_edge(u, m) cumulative_prob_dist(M) break
def add_new_movie(G, U, M, num, val): newM = Node(0, 'M' + str(len(M))) # Add a new Object for x in range(num): for u in U: # Movie will be attached with the most preferable user. if (u.cumulative_prob > val): M.append(newM) G.insert_edge(u, newM) cumulative_prob_dist(U) break
def add_new_user(G, U, M, num, val): # should add this user to m nodes from M # preserving the preferencial attachment behavior user = Node(0, 'U' + str(len(U))) for x in range(num): for m in M: # 1000 * 50,000,000 # val (is a random value) will fit into the bigger span of cumulative probability line. if (m.cumulative_degree > val): U.append(user) G.insert_edge(user, m) cumulative_prob_dist(M) break
def BA_Model(M, U, G): for i in range(10000): u = get_random_user(U) for m in M: if (m.cumulative_prob > random()): # add an edge to k newU = Node(0, 'U' + str(len(U))) U.append(newU) G.insert_edge(newU, m) cumulative_prob_dist(M) break else: G.insert_edge(u, m) cumulative_prob_dist(M)
def add_edge_with_pref(G, U, M, num, val): # Adding an edge with pref prob tempM = None tempU = None for x in range(num): for m in M: if (m.cumulative_prob > val): tempM = m break for u in U: if (u.cumulative_prob > val): tempU = u break G.insert_edge(tempU, tempM) cumulative_prob_dist(M) cumulative_prob_dist(U)
# Now there isn't a competition. So Movies can be viewed by infinite number of users. # This is against the given definition of Zipf's Law, which states there is scarcity of the sharing resources. # But for our scenario we have used time as a scarce resources. # Now create a Graph with set of nodes connected in a bipartite way G = Graph(M, U) create_init_graph(M, U, G) G.print_degree_dist(M) L_m = list() # if degree of U is 0, then it will be connected to the most degree node. for i in range (20000) : # we are going to do operations 100 times to see what will happen to the network # get a random user. For this I didn't consider the probabilities of User Nodes. u = get_random_user(U) if (u.degree in range(0,3)) : # User is a new one. If this is the case. # connect u with one of the most degree M nodes. cumulative_prob_dist(M) for m in M : if (m.cum_prob > random()) : G.insert_edge(m, u) cumulative_prob_dist(M) break else: # now this can connect to any other node. print("Else") m = get_lower_degree_Node(L_m, M, 10) G.insert_edge(m, u) cumulative_prob_dist(M) # if (i % 2000 == 0) : # plot_degree_distibution(plt, M, next(colors))