|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +"""Barabási-Albert Engine module.""" |
| 3 | +from random import sample |
| 4 | +from pyrgg.params import ENGINE_MENU, PYRGG_LOGGER_ERROR_MESSAGE |
| 5 | +from pyrgg.functions import save_log |
| 6 | + |
| 7 | + |
| 8 | +def edge_gen(n, k): |
| 9 | + """ |
| 10 | + Generate each vertex connection number. |
| 11 | +
|
| 12 | + :param n: number of vertices |
| 13 | + :type n: int |
| 14 | + :param k: number of edges to attach to a new node in each iteration, m in the Barabási-Albert model |
| 15 | + :type k: int |
| 16 | + :return: list of dicts |
| 17 | + """ |
| 18 | + # We assume m0 is the same as k, similar to the original paper examples |
| 19 | + edge_dic = {i: [] for i in range(1, k + 1)} |
| 20 | + weight_dict = {i: [] for i in range(1, k + 1)} |
| 21 | + node_from = k + 1 |
| 22 | + node_to = list(range(1, k + 1)) |
| 23 | + nodes_history = [] |
| 24 | + while node_from <= n: |
| 25 | + edge_dic[node_from] = [i for i in node_to] |
| 26 | + weight_dict[node_from] = [1] * k |
| 27 | + nodes_history.extend(node_to) |
| 28 | + nodes_history.extend([node_from] * k) |
| 29 | + node_to = sample(nodes_history, k) |
| 30 | + node_from += 1 |
| 31 | + |
| 32 | + return [edge_dic, weight_dict, (n - k) * k] |
| 33 | + |
| 34 | + |
| 35 | +def gen_using( |
| 36 | + gen_function, |
| 37 | + file_name, |
| 38 | + input_dict): |
| 39 | + """ |
| 40 | + Generate graph using given function based on Barabási-Albert model. |
| 41 | +
|
| 42 | + Refer to (https://en.wikipedia.org/wiki/Barab%C3%A1si%E2%80%93Albert_model). |
| 43 | + We assume that m0 is the same as k and k is the number of edges to attach to a new node. |
| 44 | +
|
| 45 | + :param gen_function: generation function |
| 46 | + :type gen_function: function object |
| 47 | + :param file_name: file name |
| 48 | + :type file_name: str |
| 49 | + :param input_dict: input data |
| 50 | + :type input_dict: dict |
| 51 | + :return: number of edges as int |
| 52 | + """ |
| 53 | + edge_dic, weight_dic, edge_number = edge_gen( |
| 54 | + input_dict['vertices'], |
| 55 | + input_dict['attaching_edge_number']) |
| 56 | + gen_function( |
| 57 | + edge_dic, |
| 58 | + weight_dic, |
| 59 | + { |
| 60 | + "file_name": file_name, |
| 61 | + "vertices_number": input_dict['vertices'], |
| 62 | + "edge_number": edge_number, |
| 63 | + "weighted": False, |
| 64 | + "max_weight": 1, |
| 65 | + "min_weight": 1, |
| 66 | + "direct": False, |
| 67 | + "multigraph": True, |
| 68 | + }) |
| 69 | + return edge_number |
| 70 | + |
| 71 | + |
| 72 | +def logger(file, file_name, elapsed_time, input_dict): |
| 73 | + """ |
| 74 | + Save generated graph logs for Barabási-Albert engine. |
| 75 | +
|
| 76 | + :param file: file to write log into |
| 77 | + :type file: file object |
| 78 | + :param file_name: file name |
| 79 | + :type file_name: str |
| 80 | + :param elapsed_time: elapsed time |
| 81 | + :type elapsed_time: str |
| 82 | + :param input_dict: input data |
| 83 | + :type input_dict: dict |
| 84 | + :return: None |
| 85 | + """ |
| 86 | + try: |
| 87 | + text = "Vertices : {0}\n".format(input_dict['vertices']) |
| 88 | + text += "Edges to Attach to a New Node : {0}\n".format(input_dict['attaching_edge_number']) |
| 89 | + text += "Total Edges : {0}\n".format(input_dict['edge_number']) |
| 90 | + text += "Engine : {0} ({1})\n".format(input_dict['engine'], ENGINE_MENU[input_dict['engine']]) |
| 91 | + save_log(file, file_name, elapsed_time, text) |
| 92 | + except Exception: |
| 93 | + print(PYRGG_LOGGER_ERROR_MESSAGE) |
0 commit comments