Find the isolated node in the graph Parameters: graph (dict): A dictionary representing a graph. Returns: list: A list of isolated nodes. Examples: >>> graph1 = {1: [2, 3], 2: [1, 3], 3: [1, 2], 4: []} >>> find_isolated_nodes(graph1) [4] >>> graph2 = {'A'
(graph)
| 372 | |
| 373 | |
| 374 | def find_isolated_nodes(graph): |
| 375 | """ |
| 376 | Find the isolated node in the graph |
| 377 | |
| 378 | Parameters: |
| 379 | graph (dict): A dictionary representing a graph. |
| 380 | |
| 381 | Returns: |
| 382 | list: A list of isolated nodes. |
| 383 | |
| 384 | Examples: |
| 385 | >>> graph1 = {1: [2, 3], 2: [1, 3], 3: [1, 2], 4: []} |
| 386 | >>> find_isolated_nodes(graph1) |
| 387 | [4] |
| 388 | |
| 389 | >>> graph2 = {'A': ['B', 'C'], 'B': ['A'], 'C': ['A'], 'D': []} |
| 390 | >>> find_isolated_nodes(graph2) |
| 391 | ['D'] |
| 392 | |
| 393 | >>> graph3 = {'X': [], 'Y': [], 'Z': []} |
| 394 | >>> find_isolated_nodes(graph3) |
| 395 | ['X', 'Y', 'Z'] |
| 396 | |
| 397 | >>> graph4 = {1: [2, 3], 2: [1, 3], 3: [1, 2]} |
| 398 | >>> find_isolated_nodes(graph4) |
| 399 | [] |
| 400 | |
| 401 | >>> graph5 = {} |
| 402 | >>> find_isolated_nodes(graph5) |
| 403 | [] |
| 404 | """ |
| 405 | isolated = [] |
| 406 | for node in graph: |
| 407 | if not graph[node]: |
| 408 | isolated.append(node) |
| 409 | return isolated |