algorithms
Algorithms for temporal path calculation and graph metrics.
The functions and submodules in this module allow to compute time-respecting or causal paths in temporal graphs and to calculate (temporal) and higher-order graph metrics like centralities.
Example
# Import pathpyG
import pathpyG as pp
# Generate a toy example for a temporal graph.
g = pp.TemporalGraph.from_edge_list([
('b', 'c', 2),
('a', 'b', 1),
('c', 'd', 3),
('d', 'a', 4),
('b', 'd', 2),
('d', 'a', 6),
('a', 'b', 7)
])
# Extract DAG capturing causal interaction sequences in temporal graph.
e_i = pp.algorithms.lift_order_temporal(g, delta=1)
dag = pp.Graph.from_edge_index(e_i)
print(dag)
# Calculate shortest time-respecting pathas
dist, pred = pp.algorithms.temporal.temporal_shortest_paths(g, delta=1)
RollingTimeWindow
¶
An iterable rolling time window that can be used to perform time slice analysis of temporal graphs.
Source code in src/pathpyG/algorithms/rolling_time_window.py
__init__
¶
Initialize RollingTimeWindow.
Initialize a RollingTimeWindow instance that can be used to iterate through a sequence of time-slice networks for a given TemporalNetwork instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
temporal_graph
|
TemporalGraphinstance that will be used to generate the sequence of time-slice networks. |
required | |
window_size
|
The width of the rolling time window used to create time-slice networks. |
required | |
step_size
|
The step size in time units by which the starting time of the rolling window will be incremented on each iteration. |
1
|
|
return_window
|
Whether or not the iterator shall return the current time window as a second return value. Default is False. |
False
|
|
weighted
|
Whether or not to return a weighted graph |
True
|
Example
tedges = [('a', 'b', 1), ('b', 'c', 5), ('c', 'd', 9), ('c', 'e', 9),
('c', 'f', 11), ('f', 'a', 13), ('a', 'g', 18), ('b', 'f', 21),
('a', 'g', 26), ('c', 'f', 27), ('h', 'f', 27), ('g', 'h', 28),
('a', 'c', 30), ('a', 'b', 31), ('c', 'h', 32), ('f', 'h', 33),
('b', 'i', 42), ('i', 'b', 42), ('c', 'i', 47), ('h', 'i', 50)]
t = pp.TemporalGraph.from_edge_list(tedges)
r = pp.algorithms.RollingTimeWindow(t, 10, 10, return_window=True)
for g, w in r:
print('Time window ', w)
print(g)
print(g.data.edge_index)
print('---')
Source code in src/pathpyG/algorithms/rolling_time_window.py
__iter__
¶
__next__
¶
Return the next time-slice network in the rolling time window sequence.
Source code in src/pathpyG/algorithms/rolling_time_window.py
WeisfeilerLeman_test
¶
Run Weisfeiler-Leman isomorphism test on two graphs.
The algorithm heuristically checks whether two graphs are isomorphic. If it returns False, we can be sure that the graphs are non-isomoprhic. If the test returns True we did not find conclusive evidence that they are not isomorphic, i.e. the graphs may or may not be isomophic.
The two graphs must have IndexMap mappings that assign different node IDs to the nodes in both graphs. The function will raise an error if the node labels of both graphs overlap.
The function returns a tuple (bool, list, list), where the first entry is the result of the test and the two lists represent the fingerprints of the two graphs. If the test yields true the fingerprints are identical. If the test fails, the fingerprints do not correspond.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
g1
|
pathpyG.core.graph.Graph
|
First graph. |
required |
g2
|
pathpyG.core.graph.Graph
|
Second graph. |
required |
features_g1
|
dict | None
|
Optional initial node features for graph 1. |
None
|
features_g2
|
dict | None
|
Optional initial node features for graph 2. |
None
|
Source code in src/pathpyG/algorithms/weisfeiler_leman.py
connected_components
¶
Compute the connected components of a graph.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph
|
pathpyG.core.graph.Graph
|
The input graph. |
required |
connection
|
str
|
Type of connection to consider. Options are "weak" or "strong". Defaults to "weak". |
'weak'
|
Returns:
| Type | Description |
|---|---|
typing.Tuple[int, numpy.ndarray]
|
Tuple[int, np.ndarray]: A tuple containing the number of connected components and an array with component labels for each node. |
Source code in src/pathpyG/algorithms/components.py
largest_connected_component
¶
Extract the largest connected component from a graph.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph
|
pathpyG.core.graph.Graph
|
The input graph. |
required |
connection
|
str
|
Type of connection to consider. Options are "weak" or "strong". Defaults to "weak". |
'weak'
|
Returns:
| Name | Type | Description |
|---|---|---|
Graph |
pathpyG.core.graph.Graph
|
A new graph instance containing only the largest connected component. |
Source code in src/pathpyG/algorithms/components.py
lift_order_temporal
¶
Lift a temporal graph to a second-order temporal event graph.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
g
|
pathpyG.core.temporal_graph.TemporalGraph
|
Temporal graph to lift. |
required |
delta
|
float | int
|
Maximum time difference between events to consider them connected. |
1
|
Returns:
| Name | Type | Description |
|---|---|---|
ho_index |
Edge index of the second-order temporal event graph. |
Source code in src/pathpyG/algorithms/temporal.py
temporal_shortest_paths
¶
Compute shortest time-respecting paths in a temporal graph.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
g
|
pathpyG.core.temporal_graph.TemporalGraph
|
Temporal graph to compute shortest paths on. |
required |
delta
|
int
|
Maximum time difference between events in a path. |
required |
Returns:
| Type | Description |
|---|---|
numpy.ndarray
|
Tuple of two numpy arrays: |
numpy.ndarray
|
|
typing.Tuple[numpy.ndarray, numpy.ndarray]
|
|