# Degree of centrality (centrality measure)

Python Methods and Functions

Degree of Centrality
Historically, the first and conceptually simplest is the degree of centrality, which is defined as the number of links falling on a node (i.e. the number of links that a node has). The degree can be interpreted in terms of the immediate risk that the node could intercept everything that flows over the network (for example, a virus or some information). In the case of a directed network (where the links have direction), we usually define two separate measures of degree centrality, namely degree and degree. Accordingly, undegree — this is the number of links directed to the node, and outdegree — the number of links that a node directs to others. When ties are associated with some positive aspect, such as friendship or cooperation, independence is often interpreted as a form of popularity, and the degree — as sociability.

The degree of centrality of the vertex for this graph with vertices and edges, defined as

Calculating the degree of centrality for all nodes in the graph takes in a dense adjacency matrix a graph representation, and for edges takes in a sparse matrix representation.

The definition of node-level centrality can be extended to the entire graph, in which case we are talking about centralizing the graph. Allow to be the node with the highest degree of centrality in , Let be node associated graph, which maximizes the next value (with being the highest centralized node in ):

Accordingly, the degree of centralization of the graph is:

Value is maximized when the graph contains one central node to which all other nodes are connected (star graph), in which case

,

Below is the code to calculate the degree of centrality of a graph and its various nodes.

 ` import ` ` networkx as nx ` ` `  ` def ` ` degree_centrality (G, nodes): ` ` r ` ` & quot; & quot; & quot; Calculate the central power for nodes in a two-way network. `   ` ` ` The centrality for node 'v' is the percentage of nodes ` ` ` ` is related to this. `   ` parameters ` ` ----- ----- ` ` G: schedule ` ` Two-way web `   ` nodes: list or container ` ` ` ` A container with all nodes in one set, two nodes. `   ` Returns ` ` ------- ` ` centrality: vocabulary ` ` The dictionary is entered by node with the central value of the degree of duality as the value. `   ` Notes ` ` ----- ` ` Nodes input parameter must contain all nodes in one bipartite node set, ` ` but the returned dictionary contains all nodes from both dicotyledonous nodes ` ` sets. `   ` For single-part networks, centrality values ​​` ` normalized by dividing by the maximum possible ` `' n-1', where 'n' is the number of nodes in G). ` ` `  ` ` ` In the bipartite case, the maximum possible degree of a node is in ` ` bipartite node set is the number of nodes in the opposing node set ` ` [1] _. Degree of centrality for node 'v' in bipartite ` ` ` ` sets' U' with 'n' nodes and 'V' with' m' nodes `   ` .. math :: `   ` d_ {v} = / frac {deg (v)} {m}, / mbox {for} v / in U, `   ` d_ {v } = / frac {deg (v)} {n}, / mbox {for} v / in V, `     ` where 'deg ( v) '- the degree of the' v' node. `     ` ` `" "" ` ` top ` ` = ` ` set ` ` (nodes) ` ` bottom ` ` = ` ` set ` ` (G) ` ` - ` ` top ` ` s ` ` = ` ` 1.0 ` ` / ` ` len ` ` (bottom) ` ` centrality ` ` = ` ` dict ` ` ((n, d ` ` * ` ` s) ` ` for ` ` n, d in G.degree_iter (top)) `` s = 1.0 / len (top) centrality.update ( dict ((n, d * s) for n, d in G.degree_iter (bottom)))   return centrality `

The above function is called using the networkx library and after installing it you can end up using it and the following code must be written in python to implement node degree centrality.

 ` import ` ` networkx as nx ` ` G ` ` = ` ` nx.erdos_renyi_graph (` ` 100 ` `, ` ` 0.5 ` `) ` ` d ` ` = ` ` nx.degree_centrality (G) ` ` print ` ` (d) `

The result looks like this:

The above result is a dictionary displaying the value of the degree of centrality of each node. The above is a continuation of my series on centralization measures. Keep communicating !!!

This article courtesy of Jayant Bisht . If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting an article contribute @ python.engineering. See my article appearing on the Python.Engineering homepage and help other geeks.

 ` {` ` 0 ` `: ` ` 0.5252525252525253 , 1 : 0.4444444444444445 , 2 : 0.5454545454545455 , 3 : 0.36363636363636365 , `` 4 : 0.42424242424242425 , 5 : 0.494949494949495 , 6 : 0.5454545454545455 , 7 : 0.494949494949495 ,  8 : 0.5555555555555556 , 9 : 0.5151515151515152 , 10 : 0.5454545454545455 , 11 : 0.5151515151515152 ,  12 : 0.494949494949495 , 13 : 0.4444444444444445 , 14 : 0.494949494949495 , 15 : 0.4141414141414142 ,  16 : 0.43434343434343436 , 17 : 0.5555555555555556 , 18 : 0.494949494949495 , 19 : 0.5151515151515152 ,  20 : 0.42424242424242425 , 21 : 0.494949494949495 , 22 : 0.5555555555555556 , 23 : 0.5151515151515152 ,  24 : 0.4646464646464647 , 25 : 0.4747474747474748 , 26 : 0.4747474747474748 , 27 : 0.494949494949495 ,  28 : 0.5656565656565657 , 29 : 0.5353535353535354 , 30 : 0.4747474747474748 , 31 : 0.494949494949495 ,  32 : 0.43434343434343436 , 33 : 0.4444444444444445 , 34 : 0.5151515151515152 , 35 : 0.48484848484848486 ,   36 : 0.43434343434343436 , 37 : 0.4040404040404041 , 38 : 0.5656565656565657 , 39 : 0.5656565656565657 ,  40 : 0.494949494949495 , 41 : 0.5252525252525253 , 42 : 0.4545454545454546 , 43 : 0.42424242424242425 ,  44 : 0.494949494949495 , 45 : 0.595959595959596 , 46 : 0.5454545454545455 , 47 : 0.5050505050505051 , 48 : 0.4646464646464647 , 49 : 0.48484848484848486 , 50 : 0.5353535353535354 , 51 : 0.5454545454545455 , 52 : 0.5252525252525253 , 53 : 0.5252525252525253 , 54 : 0.5353535353535354 , 55 : 0.6464646464646465 ,  56 : 0.4444444444444445 , 57 : 0.48484848484848486 , 58 : 0.5353535353535354 , 59 : 0.494949494949495 ,  60 : 0.4646464646464647 , 61 : 0.5858585858585859 , 62 : 0.494949494949495 , 63 : 0.48484848484848486 ,  64 : 0.4444444444444445 , 65 : 0.6262626262626263 , 66 : 0.5151515151515152 , 67 : 0.4444444444444445 ,  68 : 0.4747474747474748 , 69 : 0.5454545454545455 , 70 : 0.48484848484848486 , 71 : 0.5050505050505051 , 72 : 0.4646464646464647 , 73 : 0.4646464646464647 , 74 : 0.5454545454545455 , 75 : 0.4444444444444445 , 76 : 0.42424242424242425 , 77 : 0.4545454545454546 , 78 : 0.494949494949495 , 79 : 0.494949494949495 ,  80 : 0.4444444444444445 , 81 : 0.48484848484848486 , 82 : 0.48484848484848486 , 83 : 0.5151515151515152 , 84 : 0.494949494949495 , 85 : 0.5151515151515152 , 86 : 0.5252525252525253 , 87 : 0.4545454545454546 ,  88 : 0.5252525252525253 , 89 : 0.5353535353535354 , 90 : 0.5252525252525253 , 91 : 0.4646464646464647 ,  `` 92 : 0.4646464646464647 , 93 ` `: ` ` 0.5555555555555556 ` `, ` ` 94 ` `: ` ` 0.5656565656565657 ` `, ` ` 95 ` `: ` ` 0.4646464646464647 ` `, ` ` 96 ` `: ` ` 0.494949494949495 ` `, ` ` 97 ` `: ` ` 0.494949494949495 ` `, ` ` 98 ` `: ` ` 0.5050505050505051 ` `, ` ` 99 ` `: ` ` 0.5050505050505051 ` `} `