Skip to content

Commit c8c4cbd

Browse files
committed
feat: added a few more functions
1 parent 9103fd9 commit c8c4cbd

File tree

5 files changed

+68
-4
lines changed

5 files changed

+68
-4
lines changed

src/igraph_ctypes/constructors.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
from typing import Iterable, Optional, Union
1+
from typing import Iterable, Union
22

33
from .graph import Graph
4-
from ._internal.functions import create, famous, square_lattice
4+
from ._internal.functions import create, famous, grg_game, square_lattice
55

66
__all__ = (
77
"create_empty_graph",
88
"create_famous_graph",
9+
"create_geometric_random_graph",
910
"create_graph_from_edge_list",
1011
"create_square_lattice",
1112
)
@@ -75,3 +76,14 @@ def create_square_lattice(
7576
return Graph(
7677
_wrap=square_lattice(dimvector, nei, directed, mutual, periodic_per_dim)
7778
)
79+
80+
81+
def create_geometric_random_graph(n: int, radius: float, torus: bool = False):
82+
"""Creates a geometric random graph.
83+
84+
Parameters:
85+
n: the number of vertices in the graph
86+
radius: connection distance; two vertices will be connected if they are
87+
closer to each other than this threshold
88+
"""
89+
return Graph(_wrap=grg_game(n, radius, torus)[0])

src/igraph_ctypes/conversion.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from igraph_ctypes._internal.functions import get_edgelist
2+
3+
from .graph import Graph
4+
from .types import IntArray
5+
6+
__all__ = ("get_edge_list",)
7+
8+
9+
def get_edge_list(graph: Graph) -> IntArray:
10+
return get_edgelist(graph).reshape((-1, 2))

src/igraph_ctypes/enums.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#
66
# TODO(ntamas): maybe this file should be autogenerated as well?
77

8+
from ._internal.enums import Connectedness
89
from ._internal.enums import NeighborMode
910

10-
__all__ = ("NeighborMode",)
11+
__all__ = ("Connectedness", "NeighborMode")

src/igraph_ctypes/paths.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22

33
from typing import Iterable, Optional
44

5-
from .enums import NeighborMode
5+
from .enums import Connectedness, NeighborMode
66
from .graph import Graph
77
from .types import IntArray, VertexLike
88

99
from ._internal.functions import (
10+
connected_components,
1011
get_shortest_path,
1112
get_shortest_path_bellman_ford,
1213
get_shortest_path_dijkstra,
@@ -15,6 +16,11 @@
1516
__all__ = ("shortest_path",)
1617

1718

19+
def components(graph: Graph, mode: Connectedness = Connectedness.WEAK):
20+
membership, _, _ = connected_components(graph, mode)
21+
return membership
22+
23+
1824
def shortest_path(
1925
graph: Graph,
2026
source: VertexLike,

test/test_properties.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from numpy import array
2+
from numpy.testing import assert_array_equal
3+
4+
from igraph_ctypes.constructors import create_square_lattice
5+
from igraph_ctypes.conversion import get_edge_list
6+
7+
8+
def test_get_edge_list():
9+
g = create_square_lattice([4, 3])
10+
11+
edges = get_edge_list(g)
12+
assert_array_equal(
13+
edges,
14+
array(
15+
[
16+
[0, 1],
17+
[0, 4],
18+
[1, 2],
19+
[1, 5],
20+
[2, 3],
21+
[2, 6],
22+
[3, 7],
23+
[4, 5],
24+
[4, 8],
25+
[5, 6],
26+
[5, 9],
27+
[6, 7],
28+
[6, 10],
29+
[7, 11],
30+
[8, 9],
31+
[9, 10],
32+
[10, 11],
33+
]
34+
),
35+
)

0 commit comments

Comments
 (0)