def make_complete_graph(num_nodes):
    """
    Takes the number of nodes num_nodes and returns a dictionary
    corresponding to a complete undirected graph with the specified
    number of nodes.
    """
    graph = {}
    upa = alg.UPATrial(num_nodes)

    for i in range(num_nodes):
        if i not in graph:
            graph[i] = set()

        neighbors = upa.run_trial(num_nodes)
        #print neighbors

        for each in neighbors:
            if each != i:
                graph[i].add(each)

            if (each in graph) and (each != i):
                graph[each].add(i)
            elif (each != i):
                graph[each] = set()
                graph[each].add(i)

    return graph
def upa_graph(n, m):

    # step 1: make a complete graph with m nodes
    graph_dic = make_complete_graph(m)

    graph = alg.UPATrial(m)

    # step 2: add to graph from m to n nodes, one node per iteration
    # for each iteration, add m out-degree to each new node

    for i in range(m, n):
        if i not in graph_dic:
            graph_dic[i] = set()

        number = random.randint(m, m + 1)
        neighbors = graph.run_trial(number)
        #print neighbors

        for each in neighbors:
            if each != i:
                graph_dic[i].add(each)

            if (each in graph_dic) and (each != i):
                graph_dic[each].add(i)
            elif (each != i):
                graph_dic[each] = set()
                graph_dic[each].add(i)

    return graph_dic
Exemplo n.º 3
0
def random_UPA_undirected_graph(num_nodes, m_nodes):
    """
    Function uses the Undirected Preferential Attachment algoritm (UPA algorithm) 
    to create a random undirected graph. The UPA algorithm sets edges based on a 
    preferential attachment mechanism giving more edges to nodes created earlier 
    in the simulation.

    Args:
        num_nodes: integer, the numbe of nodes to be in the graph.
        
        m_nodes: integer,  the number of existing nodes to 
        which a new node is connected during each iteration.

    Returns:
        A dictionary representation of an undirected graph where keys are node names
        and values are sets of neighbors.
    """
    # Create an instance of UPATrial
    upa_obj = alg_upa_trial.UPATrial(m_nodes)

    # Make a complete ugraph with m nodes and add it to the final output
    graph = make_complete_graph(m_nodes)

    for node in range(m_nodes, num_nodes):
        # Add neighbors to nodes
        neighbors = upa_obj.run_trial(m_nodes)
        graph[node] = neighbors
        # Add nodes to neighbors
        for n in neighbors:
            graph[n].add(node)
    return graph
def algorithm_upa(n, m):
    graph = make_complete_graph(m)
    upa = alg_upa_trial.UPATrial(m)
    for i in xrange(m, n):
        neighbors = upa.run_trial(m)
        graph[i] = neighbors
        for neighbor in neighbors:
            graph[neighbor].add(i)
    return graph
def load_upa_graph(total_nodes, m):
    upa_graph = make_complete_graph(m)
    upa_trial = upa.UPATrial(m)
    for node in range(m, total_nodes):
        upa_graph[node] = upa_trial.run_trial(m)
        neighbors = upa_graph[node]
        for neighbor in neighbors:
            upa_graph[neighbor].add(node)
    return upa_graph
Exemplo n.º 6
0
def UPA(n, m):
    assert 1 <= m <= n
    ugraph = make_complete_graph(
        m)  # we first make a complete undirected graph
    upa_obj = upa.UPATrial(m)
    for i in range(m, n):
        neighbours = upa_obj.run_trial(m)
        ugraph[
            i] = neighbours  # each time we call 'run_trial' we use the constant amount of new edges 'm'
        for neighbour in neighbours:
            ugraph[neighbour].add(i)
    return ugraph
def upa(n_nodes, m_nodes):
    '''
    Helper function to implement the UPA algorithm
    '''
    graph = make_complete_graph(m_nodes)
    upa_alg = alg_upa_trial.UPATrial(m_nodes)
    for node in range(m_nodes, n_nodes):
        new_node_neighbors = upa_alg.run_trial(m_nodes)
        graph[node] = new_node_neighbors
        for neighbor in new_node_neighbors:
            graph[neighbor].add(node)
    return graph
Exemplo n.º 8
0
def load_upa_graph(total_nodes, m):
    """
    Returns a undirected graph with UPA algorithm
    with parameters equal to the arguments.
    """
    upa_graph = make_complete_graph(m)
    upa_trial = upa.UPATrial(m)
    for node in range(m, total_nodes):
        upa_graph[node] = upa_trial.run_trial(m)
        neighbors = upa_graph[node]
        for neighbor in neighbors:
            upa_graph[neighbor].add(node)
    return upa_graph
def upa(num_nodes, num_new_nodes):
    """
    Create undirected graph where every new node added randomly connects to a set 
    number of existing nodes.
    """
    upa_graph = make_complete_graph(num_new_nodes)
    graph = alg.UPATrial(num_new_nodes)
    for num in range(num_new_nodes, num_nodes):
        new_node_neighbors = graph.run_trial(num_new_nodes)
        upa_graph[num] = new_node_neighbors
        # make graph undirected
        for neighbor in new_node_neighbors:
            upa_graph[neighbor].add(num)

    return upa_graph
Exemplo n.º 10
0
def run_suite(
):  # here we only pass a class reference, from which we are going to create an object later on
    """ Some informal testing code """
    print("\nSTARTING TESTS:")
    suite = poc_simpletest.TestSuite()  # create a TestSuite object

    # 1. check the provided functions directly
    suite.run_test(
        app_2.targeted_order({
            0: set([1, 2, 3]),
            1: set([0, 2, 3]),
            2: set([0, 1, 3]),
            3: set([0, 1, 2])
        }), [0, 1, 2, 3], "Test #1a: 'targeted_order' method"
    )  # all nodes are of same degree so returned the nodes from 0 to 3
    suite.run_test(
        app_2.targeted_order({
            0: set([3]),
            1: set([2, 3]),
            2: set([1, 3]),
            3: set([0, 1, 2])
        }), [3, 1, 0, 2], "Test #1b: 'targeted_order' method"
    )  # after deleting node 3 and 1 there are only empty nodes left

    # 2. check the basic functions directly
    suite.run_test(app_2.random_ugraph(4, 1), {
        0: set([1, 2, 3]),
        1: set([0, 2, 3]),
        2: set([0, 1, 3]),
        3: set([0, 1, 2])
    }, "Test #2a: 'random_digraph' method")
    suite.run_test(app_2.random_ugraph(4, 0), {
        0: set([]),
        1: set([]),
        2: set([]),
        3: set([])
    }, "Test #2b: 'random_digraph' method")

    suite.run_test(
        app_2.compute_edges({
            0: set([1, 2, 3]),
            1: set([0, 2, 3]),
            2: set([0, 1, 3]),
            3: set([0, 1, 2])
        }), 6, "Test #2c: 'compute_edges' method")
    suite.run_test(
        app_2.compute_edges({
            0: set([1, 2, 3, 4]),
            1: set([0, 2, 3, 4]),
            2: set([0, 1, 3, 4]),
            3: set([0, 1, 2, 4]),
            4: set([0, 1, 2, 3])
        }), 10, "Test #2d: 'compute_edges' method")
    suite.run_test(
        app_2.compute_edges({
            0: set([1, 2, 3, 4, 5]),
            1: set([0, 2, 3, 4, 5]),
            2: set([0, 1, 3, 4, 5]),
            3: set([0, 1, 2, 4, 5]),
            4: set([0, 1, 2, 3, 5]),
            5: set([0, 1, 2, 3, 4])
        }), 15, "Test #2e: 'compute_edges' method")

    # 3. Testing basic functionality of the UPA CLASS
    suite.run_test(
        upa.UPATrial(3)._node_numbers, [0, 0, 0, 1, 1, 1, 2, 2, 2],
        "Test #3a: 'self._node_numbers' property")
    suite.run_test(
        upa.UPATrial(4)._node_numbers,
        [0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3],
        "Test #3b: 'self._node_numbers' property")

    suite.run_test_in_range_multi(
        upa.UPATrial(3).run_trial(2), [0, 1, 2],
        "Test #3c: 'run_trial' method")

    # 4. check the basic functions directly
    suite.run_test(
        len(app_2.UPA(4, 2)), 4, "Test #4a: 'UPA' method"
    )  # here we just check if we get the total 'n' amount of nodes
    suite.run_test(
        len(
            app_2.random_order({
                0: set([]),
                1: set([]),
                2: set([]),
                3: set([])
            })), 4, "Test #4b: 'random_order' method")

    # 5. check the basic functions directly
    suite.run_test(
        app_2.targeted_order_fast({
            0: set([3]),
            1: set([2, 3]),
            2: set([1, 3]),
            3: set([0, 1, 2])
        }), [3, 1, 0, 2], "Test #5a: 'targeted_order_fast' method")
    suite.run_test(app_2.targeted_order_fast(test_graphs.GRAPH2),
                   [1, 3, 5, 7, 8, 2, 4, 6],
                   "Test #5b: 'targeted_order_fast' method")
    suite.run_test(app_2.targeted_order_fast(test_graphs.GRAPH5),
                   ['banana', 'dog', 'cat', 'monkey', 'ape'],
                   "Test #5c: 'targeted_order_fast' method")
    suite.run_test(app_2.targeted_order_fast(test_graphs.GRAPH8),
                   app_2.targeted_order(test_graphs.GRAPH8),
                   "Test #5d: 'targeted_order_fast' method")
    suite.run_test(app_2.targeted_order_fast(test_graphs.GRAPH9),
                   app_2.targeted_order(test_graphs.GRAPH9),
                   "Test #5e: 'targeted_order_fast' method")
    suite.run_test(app_2.targeted_order_fast(test_graphs.GRAPH10),
                   app_2.targeted_order(test_graphs.GRAPH10),
                   "Test #5f: 'targeted_order_fast' method")
    suite.run_test(app_2.targeted_order_fast(test_graphs.GRAPH10),
                   app_2.targeted_order(test_graphs.GRAPH10),
                   "Test #5g: 'targeted_order_fast' method")

    # 6. report number of tests and failures
    suite.report_results()