|
| 1 | +""" |
| 2 | +Contains all the algorithms associated with graph |
| 3 | +data structure. |
| 4 | +""" |
| 5 | +# TODO: REPLACE COLLECTIONS QUEUE WITH PYDATASTRUCTS QUEUE |
| 6 | +from collections import deque as Queue |
| 7 | +from pydatastructs.utils.misc_util import AdjacencyListGraphNode |
| 8 | + |
| 9 | +__all__ = [ |
| 10 | + 'breadth_first_search', |
| 11 | +] |
| 12 | + |
| 13 | +def breadth_first_search( |
| 14 | + graph, source_node, operation, *args, **kwargs): |
| 15 | + """ |
| 16 | + Implementation of serial breadth first search(BFS) |
| 17 | + algorithm. |
| 18 | +
|
| 19 | + Parameters |
| 20 | + ========== |
| 21 | +
|
| 22 | + graph: Graph |
| 23 | + The graph on which BFS is to be performed. |
| 24 | + source_node: str |
| 25 | + The name of the source node from where the BFS is |
| 26 | + to be initiated. |
| 27 | + operation: function |
| 28 | + The function which is to be applied |
| 29 | + on every node when it is visited. |
| 30 | + The prototype which is to be followed is, |
| 31 | + `function_name(curr_node, next_node, |
| 32 | + arg_1, arg_2, . . ., arg_n)`. |
| 33 | + Here, the first two arguments denote, the |
| 34 | + current node and the node next to current node. |
| 35 | + The rest of the arguments are optional and you can |
| 36 | + provide your own stuff there. |
| 37 | +
|
| 38 | + Note |
| 39 | + ==== |
| 40 | +
|
| 41 | + You should pass all the arguments which you are going |
| 42 | + to use in the prototype of your `operation` after |
| 43 | + passing the operation function. |
| 44 | +
|
| 45 | + Examples |
| 46 | + ======== |
| 47 | +
|
| 48 | + >>> from pydatastructs import Graph, AdjacencyListGraphNode |
| 49 | + >>> V1 = AdjacencyListGraphNode("V1") |
| 50 | + >>> V2 = AdjacencyListGraphNode("V2") |
| 51 | + >>> V3 = AdjacencyListGraphNode("V3") |
| 52 | + >>> G = Graph(V1, V2, V3) |
| 53 | + >>> from pydatastructs import breadth_first_search |
| 54 | + >>> def f(curr_node, next_node, dest_node): |
| 55 | + ... return curr_node != dest_node |
| 56 | + ... |
| 57 | + >>> G.add_edge(V1.name, V2.name) |
| 58 | + >>> G.add_edge(V2.name, V3.name) |
| 59 | + >>> breadth_first_search(G, V1.name, f, V3.name) |
| 60 | + """ |
| 61 | + import pydatastructs.graphs.algorithms as algorithms |
| 62 | + func = "_breadth_first_search_" + graph._impl |
| 63 | + if not hasattr(algorithms, func): |
| 64 | + raise NotImplementedError( |
| 65 | + "Currently breadth first search isn't implemented for " |
| 66 | + "%s graphs."%(graph._impl)) |
| 67 | + return getattr(algorithms, func)( |
| 68 | + graph, source_node, operation, *args, **kwargs) |
| 69 | + |
| 70 | +def _breadth_first_search_adjacency_list( |
| 71 | + graph, source_node, operation, *args, **kwargs): |
| 72 | + bfs_queue = Queue() |
| 73 | + visited = dict() |
| 74 | + bfs_queue.append(source_node) |
| 75 | + visited[source_node] = True |
| 76 | + while len(bfs_queue) != 0: |
| 77 | + curr_node = bfs_queue.popleft() |
| 78 | + next_nodes = graph.neighbors(curr_node) |
| 79 | + if len(next_nodes) != 0: |
| 80 | + for next_node in next_nodes: |
| 81 | + if visited.get(next_node.name, False) is False: |
| 82 | + status = operation(curr_node, next_node.name, *args, **kwargs) |
| 83 | + if not status: |
| 84 | + return None |
| 85 | + bfs_queue.append(next_node.name) |
| 86 | + visited[next_node.name] = True |
| 87 | + else: |
| 88 | + status = operation(curr_node, "", *args, **kwargs) |
| 89 | + if not status: |
| 90 | + return None |
| 91 | + |
| 92 | +_breadth_first_search_adjacency_matrix = _breadth_first_search_adjacency_list |
0 commit comments