路徑

距離

sknetwork.path.get_distances(input_matrix: csr_matrix, source: int | Iterable | None = None, source_row: int | Iterable | None = None, source_col: int | Iterable | None = None, transpose: bool = False, force_bipartite: bool = False) ndarray | Tuple[ndarray, ndarray][source]

取得來源 (或一組來源) 在跳躍數上的距離。

參數:
  • input_matrix – 圖形的鄰接矩陣或二部鄰接矩陣。

  • source – 如果為整數,是來源節點的索引。如果一個清單或陣列,是來源節點的索引。(將傳回跳躍數最少的距離給這些節點之一)。

  • source_row – 二部圖中,在行和欄中的來源節點索引。參數 source_row 是 source 的別名 (它們之中至少一個必須是 None)。

  • source_col – 二部圖中,在行和欄中的來源節點索引。參數 source_row 是 source 的別名 (它們之中至少一個必須是 None)。

  • transpose – 如果 True,轉置輸入矩陣。

  • force_bipartite – 如果 True,將輸入矩陣視為二部圖的雙鄰接矩陣。如果已明確指定參數 source_row 或 source_col,設定為 True

傳回值:

distances – 與來源的距離向量 (如果從來源不存在路徑,則距離 = -1)。對於二部圖,會傳回兩個向量,一行一個,一欄一個。

傳回類型:

形狀為 (n_nodes,) 的 np.ndarray

範例

>>> from sknetwork.data import cyclic_digraph
>>> adjacency = cyclic_digraph(3)
>>> get_distances(adjacency, source=0)
array([0, 1, 2])
>>> get_distances(adjacency, source=[0, 2])
array([0, 1, 0])

最短路徑

sknetwork.path.get_shortest_path(input_matrix: csr_matrix, source: int | Iterable | None = None, source_row: int | Iterable | None = None, source_col: int | Iterable | None = None, force_bipartite: bool = False) ndarray | Tuple[ndarray, ndarray][source]

取得源節點(或一組源節點)數量中跳數最短的路徑。

參數:
  • input_matrix – 圖形的鄰接矩陣或二部鄰接矩陣。

  • source – 若為整數,則為源節點的索引。若為列表,則為源節點的索引(會傳回其中一個節點最短距離)。

  • source_row – 二部圖中,在行和欄中的來源節點索引。參數 source_row 是 source 的別名 (它們之中至少一個必須是 None)。

  • source_col – 二部圖中,在行和欄中的來源節點索引。參數 source_row 是 source 的別名 (它們之中至少一個必須是 None)。

  • force_bipartite – 若為 True,則將輸入矩陣視為二部圖的共鄰接矩陣。若指定 source_row 或 source_col 參數,則設定為 True

傳回值:

路徑 – 起始節點(或起始節點集)到最短路徑圖的鄰接矩陣。如果輸入圖為二部圖,矩陣的形狀為 (n_row + n_col, n_row + n_col),新的索引對應於原始圖的行和列。

傳回類型:

sparse.csr_matrix

範例

>>> from sknetwork.data import cyclic_digraph
>>> adjacency = cyclic_digraph(3)
>>> path = get_shortest_path(adjacency, source=0)
>>> path.toarray().astype(int)
array([[0, 1, 0],
       [0, 0, 1],
       [0, 0, 0]])