@@ -1497,6 +1497,65 @@ describe("Graph", () => {
14971497 } )
14981498 } )
14991499
1500+ describe ( "neighbors with undirected graphs" , ( ) => {
1501+ it ( "should return correct neighbors for single edge" , ( ) => {
1502+ const graph = Graph . undirected < number , void > ( ( mutable ) => {
1503+ Graph . addNode ( mutable , 0 )
1504+ Graph . addNode ( mutable , 1 )
1505+ Graph . addEdge ( mutable , 0 , 1 , undefined )
1506+ } )
1507+
1508+ expect ( Graph . neighbors ( graph , 0 ) ) . toEqual ( [ 1 ] )
1509+ expect ( Graph . neighbors ( graph , 1 ) ) . toEqual ( [ 0 ] )
1510+ } )
1511+
1512+ it ( "should return correct neighbors for linear graph" , ( ) => {
1513+ const graph = Graph . undirected < number , void > ( ( mutable ) => {
1514+ Graph . addNode ( mutable , 0 )
1515+ Graph . addNode ( mutable , 1 )
1516+ Graph . addNode ( mutable , 2 )
1517+ Graph . addEdge ( mutable , 0 , 1 , undefined )
1518+ Graph . addEdge ( mutable , 1 , 2 , undefined )
1519+ } )
1520+
1521+ expect ( Graph . neighbors ( graph , 0 ) ) . toEqual ( [ 1 ] )
1522+ expect ( Graph . neighbors ( graph , 1 ) . sort ( ) ) . toEqual ( [ 0 , 2 ] )
1523+ expect ( Graph . neighbors ( graph , 2 ) ) . toEqual ( [ 1 ] )
1524+ } )
1525+
1526+ it ( "should handle multiple edges between same nodes" , ( ) => {
1527+ const graph = Graph . undirected < number , void > ( ( mutable ) => {
1528+ Graph . addNode ( mutable , 0 )
1529+ Graph . addNode ( mutable , 1 )
1530+ Graph . addEdge ( mutable , 0 , 1 , undefined )
1531+ Graph . addEdge ( mutable , 0 , 1 , undefined )
1532+ } )
1533+
1534+ // Should deduplicate neighbors
1535+ expect ( Graph . neighbors ( graph , 0 ) ) . toEqual ( [ 1 ] )
1536+ expect ( Graph . neighbors ( graph , 1 ) ) . toEqual ( [ 0 ] )
1537+ } )
1538+
1539+ it ( "should handle self-loops" , ( ) => {
1540+ const graph = Graph . undirected < number , void > ( ( mutable ) => {
1541+ Graph . addNode ( mutable , 0 )
1542+ Graph . addEdge ( mutable , 0 , 0 , undefined )
1543+ } )
1544+
1545+ expect ( Graph . neighbors ( graph , 0 ) ) . toEqual ( [ 0 ] )
1546+ } )
1547+
1548+ it ( "should handle node with no neighbors" , ( ) => {
1549+ const graph = Graph . undirected < number , void > ( ( mutable ) => {
1550+ Graph . addNode ( mutable , 0 )
1551+ Graph . addNode ( mutable , 1 )
1552+ } )
1553+
1554+ expect ( Graph . neighbors ( graph , 0 ) ) . toEqual ( [ ] )
1555+ expect ( Graph . neighbors ( graph , 1 ) ) . toEqual ( [ ] )
1556+ } )
1557+ } )
1558+
15001559 describe ( "neighborsDirected" , ( ) => {
15011560 it ( "should return incoming neighbors" , ( ) => {
15021561 let nodeA : Graph . NodeIndex
0 commit comments