Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.9/dist-packages/networkx/generators/ego.py: 27%

15 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2023-10-20 07:00 +0000

1""" 

2Ego graph. 

3""" 

4__all__ = ["ego_graph"] 

5 

6import networkx as nx 

7 

8 

9@nx._dispatch(edge_attrs="distance") 

10def ego_graph(G, n, radius=1, center=True, undirected=False, distance=None): 

11 """Returns induced subgraph of neighbors centered at node n within 

12 a given radius. 

13 

14 Parameters 

15 ---------- 

16 G : graph 

17 A NetworkX Graph or DiGraph 

18 

19 n : node 

20 A single node 

21 

22 radius : number, optional 

23 Include all neighbors of distance<=radius from n. 

24 

25 center : bool, optional 

26 If False, do not include center node in graph 

27 

28 undirected : bool, optional 

29 If True use both in- and out-neighbors of directed graphs. 

30 

31 distance : key, optional 

32 Use specified edge data key as distance. For example, setting 

33 distance='weight' will use the edge weight to measure the 

34 distance from the node n. 

35 

36 Notes 

37 ----- 

38 For directed graphs D this produces the "out" neighborhood 

39 or successors. If you want the neighborhood of predecessors 

40 first reverse the graph with D.reverse(). If you want both 

41 directions use the keyword argument undirected=True. 

42 

43 Node, edge, and graph attributes are copied to the returned subgraph. 

44 """ 

45 if undirected: 

46 if distance is not None: 

47 sp, _ = nx.single_source_dijkstra( 

48 G.to_undirected(), n, cutoff=radius, weight=distance 

49 ) 

50 else: 

51 sp = dict( 

52 nx.single_source_shortest_path_length( 

53 G.to_undirected(), n, cutoff=radius 

54 ) 

55 ) 

56 else: 

57 if distance is not None: 

58 sp, _ = nx.single_source_dijkstra(G, n, cutoff=radius, weight=distance) 

59 else: 

60 sp = dict(nx.single_source_shortest_path_length(G, n, cutoff=radius)) 

61 

62 H = G.subgraph(sp).copy() 

63 if not center: 

64 H.remove_node(n) 

65 return H