MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / breadth_first_search

Function breadth_first_search

networking_flow/ford_fulkerson.py:20–55  ·  view source on GitHub ↗

This function returns True if there is a node that has not iterated. Args: graph: Adjacency matrix of graph source: Source sink: Sink parents: Parent list Returns: True if there is a node that has not iterated. >>> breadth_first_search(grap

(graph: list, source: int, sink: int, parents: list)

Source from the content-addressed store, hash-verified

18
19
20def breadth_first_search(graph: list, source: int, sink: int, parents: list) -> bool:
21 """
22 This function returns True if there is a node that has not iterated.
23
24 Args:
25 graph: Adjacency matrix of graph
26 source: Source
27 sink: Sink
28 parents: Parent list
29
30 Returns:
31 True if there is a node that has not iterated.
32
33 >>> breadth_first_search(graph, 0, 5, [-1, -1, -1, -1, -1, -1])
34 True
35 >>> breadth_first_search(graph, 0, 6, [-1, -1, -1, -1, -1, -1])
36 Traceback (most recent call last):
37 ...
38 IndexError: list index out of range
39 """
40 visited = [False] * len(graph) # Mark all nodes as not visited
41 queue = [] # breadth-first search queue
42
43 # Source node
44 queue.append(source)
45 visited[source] = True
46
47 while queue:
48 u = queue.pop(0) # Pop the front node
49 # Traverse all adjacent nodes of u
50 for ind, node in enumerate(graph[u]):
51 if visited[ind] is False and node > 0:
52 queue.append(ind)
53 visited[ind] = True
54 parents[ind] = u
55 return visited[sink]
56
57
58def ford_fulkerson(graph: list, source: int, sink: int) -> int:

Callers 1

ford_fulkersonFunction · 0.70

Calls 2

appendMethod · 0.45
popMethod · 0.45

Tested by

no test coverage detected