( graph: Graph<N, E, T> | MutableGraph<N, E, T>, nodeIndex: NodeIndex )
| 1490 | * @category getters |
| 1491 | */ |
| 1492 | export const neighbors = <N, E, T extends Kind = "directed">( |
| 1493 | graph: Graph<N, E, T> | MutableGraph<N, E, T>, |
| 1494 | nodeIndex: NodeIndex |
| 1495 | ): Array<NodeIndex> => { |
| 1496 | // For undirected graphs, use the specialized helper that returns the other endpoint |
| 1497 | if (graph.type === "undirected") { |
| 1498 | return getUndirectedNeighbors(graph as any, nodeIndex) |
| 1499 | } |
| 1500 | |
| 1501 | const adjacencyList = graph.adjacency.get(nodeIndex) |
| 1502 | if (adjacencyList === undefined) { |
| 1503 | return [] |
| 1504 | } |
| 1505 | |
| 1506 | const result: Array<NodeIndex> = [] |
| 1507 | for (const edgeIndex of adjacencyList) { |
| 1508 | const edge = graph.edges.get(edgeIndex) |
| 1509 | if (edge !== undefined) { |
| 1510 | result.push(edge.target) |
| 1511 | } |
| 1512 | } |
| 1513 | |
| 1514 | return result |
| 1515 | } |
| 1516 | |
| 1517 | /** |
| 1518 | * Get neighbors of a node in a specific direction for bidirectional traversal. |
no test coverage detected