@@ -232,7 +232,7 @@ def test_custom_graph_unnormalized(self):
232232 self .assertAlmostEqual (v , expected [k ])
233233
234234
235- class TestDegreeCentrality (unittest .TestCase ):
235+ class TestGraphDegreeCentrality (unittest .TestCase ):
236236 def setUp (self ):
237237 self .graph = rustworkx .PyGraph ()
238238 self .a = self .graph .add_node ("A" )
@@ -247,7 +247,7 @@ def setUp(self):
247247 self .graph .add_edges_from (edge_list )
248248
249249 def test_degree_centrality (self ):
250- centrality = rustworkx .degree_centrality_py (self .graph )
250+ centrality = rustworkx .graph_degree_centrality (self .graph )
251251 expected = {
252252 0 : 1 / 3 , # Node A has 1 edge, normalized by (n-1) = 3
253253 1 : 2 / 3 , # Node B has 2 edges
@@ -258,18 +258,62 @@ def test_degree_centrality(self):
258258
259259 def test_degree_centrality_complete_graph (self ):
260260 graph = rustworkx .generators .complete_graph (5 )
261- centrality = rustworkx .degree_centrality_py (graph )
261+ centrality = rustworkx .graph_degree_centrality (graph )
262262 expected = {0 : 1.0 , 1 : 1.0 , 2 : 1.0 , 3 : 1.0 , 4 : 1.0 }
263263 self .assertEqual (expected , centrality )
264264
265265 def test_degree_centrality_star_graph (self ):
266266 graph = rustworkx .generators .star_graph (5 )
267- centrality = rustworkx .degree_centrality_py (graph )
267+ centrality = rustworkx .graph_degree_centrality (graph )
268268 expected = {0 : 1.0 , 1 : 0.25 , 2 : 0.25 , 3 : 0.25 , 4 : 0.25 }
269269 self .assertEqual (expected , centrality )
270270
271271 def test_degree_centrality_empty_graph (self ):
272272 graph = rustworkx .PyGraph ()
273- centrality = rustworkx .degree_centrality_py (graph )
273+ centrality = rustworkx .graph_degree_centrality (graph )
274274 expected = {}
275275 self .assertEqual (expected , centrality )
276+
277+ class TestDiGraphDegreeCentrality (unittest .TestCase ):
278+ def setUp (self ):
279+ self .digraph = rustworkx .PyDiGraph ()
280+ self .a = self .digraph .add_node ("A" )
281+ self .b = self .digraph .add_node ("B" )
282+ self .c = self .digraph .add_node ("C" )
283+ self .d = self .digraph .add_node ("D" )
284+ edge_list = [
285+ (self .a , self .b , 1 ),
286+ (self .b , self .c , 1 ),
287+ (self .c , self .d , 1 ),
288+ ]
289+ self .digraph .add_edges_from (edge_list )
290+
291+ def test_digraph_degree_centrality (self ):
292+ centrality = rustworkx .digraph_degree_centrality (self .digraph )
293+ expected = {
294+ 0 : 1 / 3 , # Node A has 1 outgoing edge
295+ 1 : 2 / 3 , # Node B has 1 incoming and 1 outgoing edge
296+ 2 : 2 / 3 , # Node C has 1 incoming and 1 outgoing edge
297+ 3 : 1 / 3 , # Node D has 1 incoming edge
298+ }
299+ self .assertEqual (expected , centrality )
300+
301+ def test_digraph_degree_centrality_with_direction (self ):
302+ centrality_incoming = rustworkx .digraph_degree_centrality (self .digraph , direction = rustworkx .Direction .Incoming )
303+ expected_incoming = {
304+ 0 : 0.0 , # Node A has 0 incoming edges
305+ 1 : 1 / 3 , # Node B has 1 incoming edge
306+ 2 : 1 / 3 , # Node C has 1 incoming edge
307+ 3 : 1 / 3 , # Node D has 1 incoming edge
308+ }
309+ self .assertEqual (expected_incoming , centrality_incoming )
310+
311+ centrality_outgoing = rustworkx .digraph_degree_centrality (self .digraph , direction = rustworkx .Direction .Outgoing )
312+ expected_outgoing = {
313+ 0 : 1 / 3 , # Node A has 1 outgoing edge
314+ 1 : 1 / 3 , # Node B has 1 outgoing edge
315+ 2 : 1 / 3 , # Node C has 1 outgoing edge
316+ 3 : 0.0 , # Node D has 0 outgoing edges
317+ }
318+ self .assertEqual (expected_outgoing , centrality_outgoing )
319+
0 commit comments