@@ -117,6 +117,38 @@ def test_graph_data2():
117117 assert Graph .validate_data (other .__data__ )
118118
119119
120+ def test_shortest_path ():
121+ graph = Graph ()
122+ graph .add_edge (1 , 2 )
123+ graph .add_edge (2 , 3 )
124+ graph .add_edge (3 , 4 )
125+ graph .add_edge (5 , 6 )
126+
127+ # Test shortest path from node 1 to node 4
128+ path = graph .shortest_path (1 , 4 )
129+ assert path == [1 , 2 , 3 , 4 ]
130+
131+ # Test shortest path from node 1 to node 3
132+ path = graph .shortest_path (1 , 3 )
133+ assert path == [1 , 2 , 3 ]
134+
135+ # Test shortest path from node 2 to node 4
136+ path = graph .shortest_path (2 , 4 )
137+ assert path == [2 , 3 , 4 ]
138+
139+ # Test shortest path from node 4 to node 1
140+ path = graph .shortest_path (4 , 1 )
141+ assert path == [4 , 3 , 2 , 1 ]
142+
143+ # Test shortest path from node 5 to node 6
144+ path = graph .shortest_path (5 , 6 )
145+ assert path == [5 , 6 ]
146+
147+ # Test shortest path from node 3 to node 5 (should be None)
148+ path = graph .shortest_path (3 , 5 )
149+ assert path is None
150+
151+
120152# ==============================================================================
121153# Properties
122154# ==============================================================================
0 commit comments