graph
langroid/utils/algorithms/graph.py
Graph algos.
topological_sort(order)
¶
Given a directed adjacency matrix, return a topological sort of the nodes. order[i,j] = -1 means there is an edge from i to j. order[i,j] = 0 means there is no edge from i to j. order[i,j] = 1 means there is an edge from j to i.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
order |
array
|
The adjacency matrix. |
required |
Returns:
Type | Description |
---|---|
List[int]
|
List[int]: The topological sort of the nodes. |
Source code in langroid/utils/algorithms/graph.py
components(order)
¶
Find the connected components in an undirected graph represented by a matrix.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
order |
ndarray
|
A matrix with values 0 or 1 indicating
undirected graph edges. |
required |
Returns:
Type | Description |
---|---|
List[List[int]]
|
List[List[int]]: A list of List where each List contains the indices of nodes in the same connected component. |
Example
order = np.array([ [1, 1, 0, 0], [1, 1, 1, 0], [0, 1, 1, 0], [0, 0, 0, 1] ]) components(order)