Esempio n. 1
0
	def testNeighbors(self):
		# Directed
		G = nk.Graph(4, False, True)

		G.addEdge(0, 1)
		G.addEdge(0, 2)
		G.addEdge(3, 1)
		G.addEdge(3, 2)
		G.addEdge(1, 2)

		self.assertEqual(sorted(G.neighbors(0)), [1, 2])
		self.assertEqual(sorted(G.neighbors(1)), [2])
		self.assertEqual(sorted(G.neighbors(2)), [])
		self.assertEqual(sorted(G.neighbors(3)), [1, 2])

		# Undirected
		G = nk.Graph(4, False, False)

		G.addEdge(0, 1)
		G.addEdge(0, 2)
		G.addEdge(3, 1)
		G.addEdge(3, 2)
		G.addEdge(1, 2)

		self.assertEqual(sorted(G.neighbors(0)), [1, 2])
		self.assertEqual(sorted(G.neighbors(1)), [0, 2, 3])
		self.assertEqual(sorted(G.neighbors(2)), [0, 1, 3])
		self.assertEqual(sorted(G.neighbors(3)), [1, 2])
Esempio n. 2
0
    def testIterator(self):
        # Undirected
        G = nk.Graph(4, False, False)

        G.addEdge(0, 1)
        G.addEdge(0, 2)
        G.addEdge(1, 2)
        G.addEdge(3, 1)
        G.addEdge(3, 2)

        def nbrFunc(u, v, weight, edgeId):
            forEdgesNbrs.append(v)

        # Iterate through neighbours of node using iterNeighbors
        def nodeIter(node):
            nodeList = []
            nbrs = G.iterNeighbors(node)
            try:
                while nbrs is not None:
                    nodeList.append(next(nbrs))
            except StopIteration:
                pass
            return nodeList

        for node in range(G.upperNodeIdBound()):
            forEdgesNbrs = []
            G.forEdgesOf(node, nbrFunc)
            nodeNbrs = nodeIter(node)
            self.assertEqual(sorted(forEdgesNbrs), sorted(nodeNbrs))

        # Directed
        G = nk.Graph(4, False, True)

        G.addEdge(0, 1)
        G.addEdge(0, 2)
        G.addEdge(3, 1)
        G.addEdge(3, 2)
        G.addEdge(1, 2)

        # Iterate through neighbours of node using iterNeighbors
        def nodeInIter(node):
            nodeList = []
            nbrs = G.iterInNeighbors(node)
            try:
                while nbrs is not None:
                    nodeList.append(next(nbrs))
            except StopIteration:
                pass
            return nodeList

        for node in range(G.upperNodeIdBound()):
            forEdgesNbrs = []
            G.forInEdgesOf(node, nbrFunc)
            nodeInNbrs = nodeInIter(node)
            self.assertEqual(sorted(forEdgesNbrs), sorted(nodeInNbrs))
Esempio n. 3
0
    def testAddEdgeWithMissingNodes(self):
        G = nk.Graph(0)

        with self.assertRaises(RuntimeError):
            G.addEdge(0, 1)

        self.assertEqual(G.numberOfNodes(), 0)
        self.assertEqual(G.numberOfEdges(), 0)

        G.addEdge(0, 2, addMissing=True)

        self.assertEqual(G.numberOfNodes(), 3)
        self.assertEqual(G.numberOfEdges(), 1)

        G.removeNode(1)

        self.assertEqual(G.numberOfNodes(), 2)
        self.assertEqual(G.numberOfEdges(), 1)

        G.addEdge(0, 1, addMissing=True)

        self.assertEqual(G.numberOfNodes(), 3)
        self.assertEqual(G.numberOfEdges(), 2)

        G.removeNode(2)

        G.addEdge(1, 2, addMissing=True)

        self.assertEqual(G.numberOfNodes(), 3)
        self.assertEqual(G.numberOfEdges(), 2)
Esempio n. 4
0
    def testDegreePreservingShuffleDirectedTriangle(self):
        """Test whether a directed triangle is reoriented in 50% of cases"""
        G = nk.Graph(3, False, True)
        G.addEdge(0, 1)
        G.addEdge(1, 2)
        G.addEdge(2, 0)

        num_clockwise = 0
        num_iterations = 1000

        for i in range(num_iterations):
            dps = nk.randomization.DegreePreservingShuffle(G)
            dps.run()
            G2 = dps.getGraph()
            check_graphs(G, G2)

            # check orientation
            clockwise = G2.hasEdge(0, 1)
            anticlkw = G2.hasEdge(1, 0)
            self.assertNotEqual(clockwise, anticlkw)

            num_clockwise += clockwise
            G = G2

        # confidence interval with an error rate of ~ 1e-6
        self.assertGreater(num_clockwise, 400)
        self.assertLess(num_clockwise, 600)
Esempio n. 5
0
def fit_ba(g, fully_connected_start):
    random.seed(42, version=2)
    networkit.setSeed(seed=42, useThreadId=False)
    n, m = g.size()
    m_0 = math.ceil(m / n)
    ba = networkit.Graph(n)
    nodes = ba.nodes()
    edges_added = 0
    if fully_connected_start:
        start_connections = itertools.combinations(nodes[:m_0], 2)
    else:  # circle
        start_connections = (
            [(nodes[m_0-1], nodes[0])] +
            [(nodes[i], nodes[i+1]) for i in range(m_0-1)]
        )
    for u, v in start_connections:
        ba.addEdge(u, v)
        edges_added += 1

    for i, v in list(enumerate(nodes))[m_0:]:
        num_new_edges = min(i, int((m-edges_added)/(n-i)))
        to_connect = set()
        while len(to_connect) < num_new_edges:
            num_draws = num_new_edges - len(to_connect)
            to_connect_draws = [
                random.choice(ba.randomEdge())
                for i in range(num_draws)
            ]
            to_connect |= set(
                u for u in to_connect_draws if not ba.hasEdge(v, u)
            )
        for u in to_connect:
            ba.addEdge(u, v)
        edges_added += num_new_edges
    return ba
Esempio n. 6
0
def get_commit_graph(repo, period='month'):
    g = nk.Graph(weighted=True)
    date_format = period_formatstring(period)
    modularity = {}
    nodes = {}
    fp_commits = get_fp_chain(repo)
    month = fp_commits[0].authored_datetime.strftime(date_format)
    for parent_idx, commit in enumerate(fp_commits[1:]):
        commit_month = commit.authored_datetime.strftime(date_format)
        if commit_month > month:
            community = nk.community.PLM(g)  # PLP is ~5% faster but coarse
            community.run()
            partition = community.getPartition()
            modularity[month] = (partition.numberOfSubsets(), len(nodes),
                                 len(g.edges()))
            month = commit_month
        parent = fp_commits[parent_idx]
        files = []  # files changed in this commit
        for diff in commit.diff(parent):
            fname = diff.a_path
            if fname not in nodes:
                nodes[fname] = g.addNode()
            files.append(fname)
        for i, file1 in enumerate(files[:-1]):
            for file2 in files[i + 1:]:
                g.increaseWeight(nodes[file1], nodes[file2], 1)
    return g, modularity, nodes
Esempio n. 7
0
def execute_with_networkit(file_name):
    """
    Utilizzo le funzioni di networkit per verificare la correttezza degli algoritmi implementati.
    Networkit fornisce lo stesso risultato degli algoritmi solamente nel caso in cui il grafo sia diretto (DIR=True)
    poichè nella funzione addEdge di nk.Graph "It is not checked whether this edge already exists, thus it is possible
    to create multi-edges.", il quale controllo invece viene effettuato nelle implementazioni realizzate,
    presenti nel file Graph.py
    """
    conn, ver = create_connections_from_file(file_name, networkit=True)
    g = nk.Graph(n=max(ver) + 1, weighted=True, directed=True)
    for tupla in conn:
        g.addEdge(tupla[0], tupla[1], tupla[2])
    dist = nk.distance.APSP(g)
    dist.run()
    matrix = dist.getDistances()
    for riga in range(len(matrix)):
        for col in range(len(matrix)):
            if matrix[riga][col] > sys.maxsize / 2:
                matrix[riga][
                    col] = sys.maxsize  # per avere uguaglianza con Floyd-Warshall e Dijkstra
            else:
                matrix[riga][col] = int(matrix[riga][col])
    mat = create_square_matrix(ver)
    for i in range(len(ver)):
        for j in range(len(ver)):
            if i != j:
                mat[i][j] = matrix[ver[i]][ver[j]]
    return mat
Esempio n. 8
0
		def generateGraph(directed):
			G = nk.Graph(5, False, directed)
			G.addEdge(0, 1)
			G.addEdge(0, 2)
			G.addEdge(2, 1)
			G.addEdge(1, 3)
			G.addEdge(4, 2)
			return G
Esempio n. 9
0
def make_graph_from_json(json_data):
    G = nk.Graph()

    for node in json_data:
        for neighbor in json_data[node]:
            G.addEdge(int(node), int(neighbor), addMissing=True)

    return G
Esempio n. 10
0
    def testSubgraphFromNodes(self):
        # Directed
        G = nk.Graph(4, True, True)

        G.addEdge(0, 1, 1.0)
        G.addEdge(0, 2, 2.0)
        G.addEdge(3, 1, 4.0)
        G.addEdge(3, 2, 5.0)
        G.addEdge(1, 2, 3.0)

        res = G.subgraphFromNodes([0])
        self.assertTrue(res.isWeighted())
        self.assertTrue(res.isDirected())
        self.assertEqual(res.numberOfNodes(), 1)
        self.assertEqual(res.numberOfEdges(), 0)

        res = G.subgraphFromNodes([0], True)
        self.assertEqual(res.numberOfNodes(), 3)
        self.assertEqual(res.numberOfEdges(), 2)

        res = G.subgraphFromNodes([0, 1])
        self.assertEqual(res.numberOfNodes(), 2)
        self.assertEqual(res.numberOfEdges(), 1)

        res = G.subgraphFromNodes([0, 1], True)
        self.assertEqual(res.numberOfNodes(), 3)
        self.assertEqual(res.numberOfEdges(), 3)

        res = G.subgraphFromNodes([0, 1], True, True)
        self.assertEqual(res.numberOfNodes(), 4)
        self.assertEqual(res.numberOfEdges(), 4)

        # Undirected
        G = G.toUndirected()

        res = G.subgraphFromNodes([0])
        self.assertTrue(res.isWeighted())
        self.assertFalse(res.isDirected())
        self.assertEqual(res.numberOfNodes(), 1)
        self.assertEqual(res.numberOfEdges(), 0)

        res = G.subgraphFromNodes([0], True)
        self.assertEqual(res.numberOfNodes(), 3)
        self.assertEqual(res.numberOfEdges(), 2)
        self.assertEqual(G.weight(0, 1), 1.0)
        self.assertEqual(G.weight(0, 2), 2.0)

        res = G.subgraphFromNodes([0, 1])
        self.assertEqual(res.numberOfNodes(), 2)
        self.assertEqual(res.numberOfEdges(), 1)

        res = G.subgraphFromNodes([0, 1], True)
        self.assertEqual(res.numberOfNodes(), 4)
        self.assertEqual(res.numberOfEdges(), 4)

        res = G.subgraphFromNodes(set([0, 1]), True)
        self.assertEqual(res.numberOfNodes(), 4)
        self.assertEqual(res.numberOfEdges(), 4)
Esempio n. 11
0
    def testNeighbors(self):
        # Directed
        G = nk.Graph(4, False, True)

        G.addEdge(0, 1)
        G.addEdge(0, 2)
        G.addEdge(3, 1)
        G.addEdge(3, 2)
        G.addEdge(1, 2)

        def getNeighbors(u):
            neighbors = []
            for v in G.iterNeighbors(u):
                neighbors.append(v)
            return sorted(neighbors)

        def getInNeighbors(u):
            inNeighbors = []
            for v in G.iterInNeighbors(u):
                inNeighbors.append(v)
            return sorted(inNeighbors)

        self.assertListEqual(getNeighbors(0), [1, 2])
        self.assertListEqual(getNeighbors(1), [2])
        self.assertListEqual(getNeighbors(2), [])
        self.assertListEqual(getNeighbors(3), [1, 2])

        self.assertListEqual(getInNeighbors(0), [])
        self.assertListEqual(getInNeighbors(1), [0, 3])
        self.assertListEqual(getInNeighbors(2), [0, 1, 3])
        self.assertListEqual(getInNeighbors(3), [])

        # Undirected
        G = nk.Graph(4, False, False)

        G.addEdge(0, 1)
        G.addEdge(0, 2)
        G.addEdge(3, 1)
        G.addEdge(3, 2)
        G.addEdge(1, 2)

        self.assertEqual(getNeighbors(0), [1, 2])
        self.assertEqual(getNeighbors(1), [0, 2, 3])
        self.assertEqual(getNeighbors(2), [0, 1, 3])
        self.assertEqual(getNeighbors(3), [1, 2])
def subgraph(g, s):
    res = nk.Graph(len(s), weighted=g.isWeighted())
    node_ids = OrderedDict(zip(s, range(len(s))))
    for u in s:
        for n in g.neighbors(u):
            if u < n and n in s:
                res.addEdge(node_ids[u], node_ids[n], g.weight(u, n))

    return res
Esempio n. 13
0
def make_graphs_into_one(G1, G2, num_edges_to_connect):

    counter = 0

    G = nk.Graph()

    print(G.nodes())
    print(G.edges())

    G1_edges = list(G1.edges())
    G2_edges = list(G2.edges())

    G1_nodes = list(G1.nodes())
    G2_nodes = list(G2.nodes())

    size_of_G1 = len(G1_nodes)

    G2_nodes = list(map(lambda x: size_of_G1 + x, G2_nodes))

    for i in G1_nodes:
        G.addNode()

    for i in G2_nodes:
        G.addNode()

    #print(G.nodes())
    print(len(G.nodes()))

    for i, j in G1_edges:
        G.addEdge(i, j)

    print(G.edges())
    #print(len(G.edges()))

    for i, j in G2_edges:
        u = size_of_G1 + i
        v = size_of_G1 + j
        G.addEdge(u, v)

    print(G.edges())
    print(len(G1.edges()))
    print(len(G2.edges()))
    print(len(G.edges()))

    for i in range(num_edges_to_connect):

        u = random.choice(G1_nodes)
        v = random.choice(G2_nodes)

        G.addEdge(u, v)

    print(num_edges_to_connect)
    print(len(G.edges()))

    print(G.edges())

    return G
Esempio n. 14
0
 def testGraphPickling(self):
     G = nk.Graph(2)
     G.addEdge(0, 1)
     G.indexEdges()
     pickledGraph = pickle.dumps(G)
     G2 = pickle.loads(pickledGraph)
     self.assertEqual(G.numberOfNodes(), G2.numberOfNodes())
     self.assertEqual(G.numberOfEdges(), G2.numberOfEdges())
     self.assertEqual(G.edgeId(0, 1), G2.edgeId(0, 1))
    def getSmallGraph(self, weighted=False, directed=False):
        G = nk.Graph(4, weighted, directed)
        G.addEdge(0, 1, 1.0)
        G.addEdge(0, 2, 2.0)
        G.addEdge(3, 1, 4.0)
        G.addEdge(3, 2, 5.0)
        G.addEdge(1, 2, 3.0)

        return G
Esempio n. 16
0
 def buildMesh(rows, cols):
     G = nk.Graph(rows * cols, False, False)
     for i in range(rows):
         for j in range(cols):
             if j < cols - 1:
                 G.addEdge(i * cols + j, i * cols + j + 1)
             if i < rows - 1:
                 G.addEdge(i * cols + j, (i + 1) * cols + j)
     return G
Esempio n. 17
0
def plot_functions(g, outfile, recalculate=False):
    x1, y1 = calculate_fractal_dimension(nk.Graph(g),
                                         nk.centrality.DegreeCentrality,
                                         recalculate)
    x2, y2 = calculate_fractal_dimension(nk.Graph(g),
                                         nk.centrality.Betweenness,
                                         recalculate)
    x3, y3 = calculate_fractal_dimension(nk.Graph(g), nk.centrality.Closeness,
                                         recalculate)
    x5, y5 = calculate_fractal_dimension(nk.Graph(g), random_ranking,
                                         recalculate)

    pylab.figure(1, dpi=500)
    pylab.xlabel(r"Fraction of vertices removed ($\rho$)")
    pylab.ylabel(r"Fractal dimension ($\sigma$)")

    pylab.plot(x1, y1, "b-", alpha=0.6, linewidth=2.0)
    pylab.plot(x2, y2, "g-", alpha=0.6, linewidth=2.0)
    pylab.plot(x3, y3, "r-", alpha=0.6, linewidth=2.0)
    pylab.plot(x5, y5, "k-", alpha=0.6, linewidth=2.0)

    pylab.legend((r"Degree", "Betweenness", "Closeness", "Random"),
                 loc="upper right",
                 shadow=False)

    pylab.savefig(outfile, format="png")
    pylab.close(1)

    # Generate csv file
    import numpy as np
    matrix = np.matrix([x1, y1, y2, y3, y5])

    filename = outfile.rsplit(".", 1)[0] + ".csv"
    header = ", degree, betweeness, closeness, random"
    separator = ", "

    np.savetxt(filename,
               matrix.transpose(),
               fmt="%s",
               delimiter=separator,
               header=header,
               comments="")
Esempio n. 18
0
        def generateTwoComponents(directed, weighted):
            G = nk.Graph(n, directed, weighted)
            G.addEdge(0, 1)
            G.addEdge(1, 2)
            G.addEdge(2, 0)

            G.addEdge(3, 4)
            G.addEdge(4, 5)
            G.addEdge(5, 3)

            return G
Esempio n. 19
0
def new_make_modular_network_ER(N, k_intra, k_inter, num_modules, SEED, alpha):

    size_of_one_module = int(N / num_modules)

    num_edges_to_connect = int(k_inter * N / 2)

    counter = 0

    G = nk.Graph()

    nodes_list = []

    size_G_nodes = 0

    for mod_num in range(num_modules):

        new_SEED = SEED * (mod_num + 2) + 1

        print(size_of_one_module)
        print(k_intra)

        G_mod = make_ER_Graph(size_of_one_module, k_intra, new_SEED)

        G_nodes = list(G_mod.nodes())
        G_edges = list(G_mod.edges())

        G_nodes = list(map(lambda x: x + size_G_nodes, G_nodes))

        nodes_list.append(G_nodes)

        for n in G_nodes:
            G.addNode()

        print(G_edges)
        print(list(G.nodes()))

        for (a, b) in G_edges:
            u = size_G_nodes + a
            v = size_G_nodes + b
            G.addEdge(u, v)

        size_G_nodes += len(G_nodes)

    set_of_connected_nodes = set([])

    for i in range(num_edges_to_connect):

        (u, v) = get_random_u_v(nodes_list)
        G.addEdge(u, v)

        set_of_connected_nodes.add(u)
        set_of_connected_nodes.add(v)

    return (G, set_of_connected_nodes)
Esempio n. 20
0
def iterative_rev_degree_order(depG):
    G = nk.Graph(depG, weighted=True)
    edges = sorted(G.edges(), key=lambda x: G.weightedDegree(x[0]) + G.weightedDegree(x[1]), reverse=True)
    for u, v in edges:
        try:
            w = G.weight(u, v) / (G.weightedDegree(u) * G.weightedDegree(v))
        except ZeroDivisionError as e:
            continue
        if w == 0:
            G.removeEdge(u, v)
        else:
            G.setWeight(u, v, w=w)
    return G
Esempio n. 21
0
	def testExtractLargestConnectedComponent(self):
		G = nk.Graph(10)
		for i in range(3):
			G.addEdge(i, i+1)

		for i in range(4, 9):
			G.addEdge(i, i+1)

		G1 = nk.components.ConnectedComponents.extractLargestConnectedComponent(G, True)
		self.assertEqual(G1.numberOfNodes(), 6)
		self.assertEqual(G1.numberOfEdges(), 5)

		G2 = nk.components.ConnectedComponents.extractLargestConnectedComponent(G, False)
		for i in range(G.numberOfNodes()):
			self.assertEqual(G2.hasNode(i), (4 <= i <= 9))
def make_graphs_into_one_multiple_graphs(G_list,num_edges_to_connect):

	counter = 0

	G = nk.Graph()

	#print(G.nodes())
	#print(G.edges())

	nodes_list = []

	size_G_nodes = 0

	for G_mod in G_list:

		G_nodes = list(G_mod.nodes())
		G_edges = list(G_mod.edges())

		G_nodes = list(map(lambda x : x + size_G_nodes, G_nodes))

		nodes_list.append(G_nodes)

		for n in G_nodes:
			G.addNode()

		print(G_edges)
		print(list(G.nodes()))

		for (a,b) in G_edges:
			u = size_G_nodes + a
			v = size_G_nodes + b
			G.addEdge(u,v)

		size_G_nodes += len(G_nodes)

	set_of_connected_nodes = set([])

	for i in range(num_edges_to_connect):
		
		(u,v) = get_random_u_v(nodes_list)
		G.addEdge(u,v)

		set_of_connected_nodes.add(u)
		set_of_connected_nodes.add(v)


	return (G, set_of_connected_nodes)
Esempio n. 23
0
def read_graph(fin):
    n = int(fin.readline())
    G = nk.Graph(n)
    while True:
        try:
            line = fin.readline()
        except:
            break
        line = line.split()
        if len(line) == 0:
            break
        x = int(line[0][:-1])
        arr = [int(y) for y in line[1:]]
        for y in arr:
            if not G.hasEdge(x, y):
                G.addEdge(x, y, addMissing=True)
    return G, n
Esempio n. 24
0
def make_graphs_into_one_multiple_graphs_alpha(G_list, num_edges_to_connect,
                                               alpha):

    counter = 0

    G = nk.Graph()

    print(G.nodes())
    print(G.edges())

    nodes_list = []

    alpha_nodes_list = []

    size_G_nodes = 0

    for i in G_list:

        G_nodes = list(i.nodes())
        G_edges = list(i.edges())

        G_nodes = list(map(lambda x: x + size_G_nodes, G_nodes))
        size_G_nodes += len(G_nodes)

        nodes_list.append(G_nodes)

        num_nodes_to_sample = len(G_nodes) * alpha

        curr_alpha = random.sample(G_nodes, num_nodes_to_sample)

        alpha_nodes_list.append(curr_alpha)

        for n in G_nodes:
            G.addNode()

        for i, j in G_edges:
            u = size_G_nodes + i
            v = size_G_nodes + j
            G.addEdge(u, v)

    for i in range(num_edges_to_connect):

        (u, v) = get_random_u_v(alpha_nodes_list)
        G.addEdge(u, v)

    return G
Esempio n. 25
0
    def build_networkit(edges, verbose=True, nnodes=None):
        """Builds a `networkit` graph from the input edges.

        edges : array or list
            The input graph edges as a list or array of two-element lists or
            tuples.
            Example: [(1,9), (2,3), (4,7), ...]
        verbose : bool or int, optional, default: True
            If True or 1, it produces lots of logging output.
        **kwargs : dict
            Additional keyword arguments for the `networkit.Graph`
            function.

        Returns
        -------
        G : `networkit` graph object
            The output graph made from the input edges.
        """

        if nnodes is None:
            t0 = datetime.datetime.now()
            unraveled = list(
                chain.from_iterable(edges))  # TODO: np.concat is faster!
            if not isinstance(unraveled[0], str) and not isinstance(
                    unraveled[1], str):
                nnodes = max(
                    unraveled) + 1  # just enough nodes to construct the graph
            else:
                nnodes = len(unraveled)  # length of ('a',5), ('f',2), ...
            if verbose:
                # Important: do not rely on this to cover all the isolated
                # coords (as isolated nodes in the graph).
                # That's why we used `np.arange` later to fill in the possible
                # missing bits.
                elapsed_seconds = round((datetime.datetime.now() - t0).seconds)
                print(
                    f"It took {str(datetime.timedelta(seconds=elapsed_seconds))} hms to identify the number"
                    f"of nodes, i.e. `nnodes`={nnodes}.")
            del unraveled

        G = nk.Graph(nnodes, directed=False)

        for edge in edges:
            G.addEdge(*edge)

        return G
Esempio n. 26
0
def glove(depG, xmax=100, alpha=0.75):
    G = nk.Graph(depG, weighted=True)
    edges_list = depG.edges()
    dico = dict()
    tot = depG.totalEdgeWeight()
    res = -1
    for n1, n2 in edges_list:
        w = depG.weight(n1, n2)
        if (w > xmax):
            res = 1
        else:
            res = (w / xmax) ** alpha
        dico[(n1, n2)] = res
    for n1, n2 in dico:
        if dico[(n1, n2)] != 0:
            G.setWeight(n1, n2, dico[(n1, n2)])
        else:
            G.removeEdge(n1, n2)
    return G
Esempio n. 27
0
def pmi(depG):
    G = nk.Graph(depG, weighted=True)
    edges_list = depG.edges()
    dico = dict()
    tot = depG.totalEdgeWeight()
    for n1, n2 in edges_list:
        w1 = depG.weightedDegree(n1)
        w2 = depG.weightedDegree(n2)
        w = depG.weight(n1, n2)
        try:
            pmi = log2((w * tot)/(w1 * w2))
        except ZeroDivisionError as e:
            pmi = 0
        dico[(n1, n2)] = pmi
    for n1, n2 in dico:
        if dico[(n1, n2)] != 0:
            G.setWeight(n1, n2, dico[(n1, n2)])
        else:
            G.removeEdge(n1, n2)
    return G
Esempio n. 28
0
    def get_subgraph(self, mask_array):
        masked_num = mask_array.sum()
        new_graph = networkit.Graph(n=masked_num, directed=True, weighted=True)
        # Map old (full) node ids to new (subgraph) node ids
        full_to_sub = {}
        for sub, full in enumerate(np.flatnonzero(mask_array)):
            full_to_sub[full] = sub
        
        # Insert edges
        def insert_new_edges(from_node,to_node,edgeweight,edgeid):
            new_from_node = full_to_sub.get(from_node,-99)
            new_to_node = full_to_sub.get(to_node, -99)
            if -99 in (new_from_node, new_to_node):
                return
            new_graph.addEdge(new_from_node,new_to_node,edgeweight)

        for node in full_to_sub.keys():
            self.graph.forEdgesOf(node, insert_new_edges)
            
        return new_graph
Esempio n. 29
0
	def test_DegreeCentrality(self):
		g = nk.Graph(8, False, False)

		g.addEdge(0, 2)
		g.addEdge(0, 5)
		g.addEdge(1, 2)
		g.addEdge(2, 3)
		g.addEdge(2, 2)
		g.addEdge(2, 4)
		g.addEdge(3, 5)
		g.addEdge(4, 5)
		g.addEdge(5, 5)
		g.addEdge(5, 6)
		g.addEdge(5, 7)
		g.addEdge(7, 7)

		expected_result = [2.0, 1.0, 4.0, 2.0, 2.0, 5.0, 1.0, 1.0]

		dc = nk.centrality.DegreeCentrality(g).run().scores()

		self.assertListEqual(expected_result, dc)
Esempio n. 30
0
def standard(depG):
    G = nk.Graph(depG, weighted=True)
    edges_list = depG.edges()
    dico = dict()
    tot = depG.totalEdgeWeight()
    for n1, n2 in edges_list:
        w1 = depG.weightedDegree(n1)
        w2 = depG.weightedDegree(n2)
        w = depG.weight(n1, n2)
        try:
            res = w / (w1 * w2)
        except ZeroDivisionError as e:
            # print(w1, w2, w)
            res = 0
        dico[(n1, n2)] = res
    for n1, n2 in dico:
        if dico[(n1, n2)] != 0:
            G.setWeight(n1, n2, dico[(n1, n2)])
        else:
            G.removeEdge(n1, n2)
    return G