Skip to main content
Code Review

Return to Question

expand "BFS"
Source Link
Gareth Rees
  • 50.1k
  • 3
  • 130
  • 210

Computing the Minimum Numbernumber of Flight Segmentsflight segments using BFSbreadth-first search

I'm practising with graphs, and trying to solve a problem of calculating the minimum number of flight segments, applying bfsbreadth-first search.

Computing the Minimum Number of Flight Segments using BFS

I'm practising with graphs, and trying to solve a problem of calculating the minimum number of flight segments, applying bfs.

Minimum number of flight segments using breadth-first search

I'm practising with graphs, and trying to solve a problem of calculating the minimum number of flight segments, applying breadth-first search.

Described more deeply graph representation.
Source Link

I represent graph in such way. The first line contains non-negative integers n and m — the number of vertices and the number of edges respectively. The vertices are always numbered from 1 to n. Each of the following m lines defines an edge in the format u v where 1 ≤ u, v ≤ n are endpoints of the edge :

4 5
2 1
4 3
1 4
2 4
3 2
1 3

The last two digits stands for two vertices, we need to find path between.

enter image description here

I represent graph in such way:

4 5
2 1
4 3
1 4
2 4
3 2

The last two digits stands for two vertices, we need to find path between.

I represent graph in such way. The first line contains non-negative integers n and m — the number of vertices and the number of edges respectively. The vertices are always numbered from 1 to n. Each of the following m lines defines an edge in the format u v where 1 ≤ u, v ≤ n are endpoints of the edge :

4 5
2 1
4 3
1 4
2 4
3 2
1 3

The last two digits stands for two vertices, we need to find path between.

enter image description here

grammar/spelling
Source Link
forsvarir
  • 11.8k
  • 7
  • 39
  • 72

I'm practicingpractising with graphgraphs, and trying to solve a problem of calculating the minimum number of flight segments, applying bfs.
The

The code is working, but I think, that itsit's not clean.

Can anyone suggest how can I better refactor it to make it cleaner?

def distance(adj, s, t):
 n = len(adj)
 queue = []
 visited = set()
 path = []
 queue.append([s])
 dist = 0
 while (len(queue) > 0):
 path = queue.pop(0)
 last_vertex = path[-1]
 if last_vertex == t:
 # print(path)
 dist = len(path)-1
 elif last_vertex not in visited:
 for w in adj[last_vertex]:
 new_path = list(path)
 new_path.append((w))
 queue.append(new_path)
 visited.add(last_vertex)
 if dist != 0:
 return dist
 else:
 return -1
if __name__ == '__main__':
 input = sys.stdin.read()
 data = list(map(int, input.split()))
 n, m = data[0:2]
 data = data[2:]
 edges = list(zip(data[0:(2 * m):2], data[1:(2 * m):2]))
 adj = [[] for _ in range(n)]
 for (a, b) in edges:
 adj[a - 1].append(b - 1)
 adj[b - 1].append(a - 1)
 s, t = data[2 * m] - 1, data[2 * m + 1] - 1
 print(distance(adj, s, t))

I represent graph in such way:

4 5
2 1
4 3
1 4
2 4
3 2

The last two digits stands for two vertices, we need to find path between.

I'm practicing with graph, and trying to solve a problem of calculating minimum number of flight segments, applying bfs.
The code is working, but I think, that its not clean.

Can anyone suggest how can I better refactor it?

def distance(adj, s, t):
 n = len(adj)
 queue = []
 visited = set()
 path = []
 queue.append([s])
 dist = 0
 while (len(queue) > 0):
 path = queue.pop(0)
 last_vertex = path[-1]
 if last_vertex == t:
 # print(path)
 dist = len(path)-1
 elif last_vertex not in visited:
 for w in adj[last_vertex]:
 new_path = list(path)
 new_path.append((w))
 queue.append(new_path)
 visited.add(last_vertex)
 if dist != 0:
 return dist
 else:
 return -1
if __name__ == '__main__':
 input = sys.stdin.read()
 data = list(map(int, input.split()))
 n, m = data[0:2]
 data = data[2:]
 edges = list(zip(data[0:(2 * m):2], data[1:(2 * m):2]))
 adj = [[] for _ in range(n)]
 for (a, b) in edges:
 adj[a - 1].append(b - 1)
 adj[b - 1].append(a - 1)
 s, t = data[2 * m] - 1, data[2 * m + 1] - 1
 print(distance(adj, s, t))

I represent graph in such way:

4 5
2 1
4 3
1 4
2 4
3 2

The last two digits stands for two vertices, we need to find path between.

I'm practising with graphs, and trying to solve a problem of calculating the minimum number of flight segments, applying bfs.

The code is working, but I think, that it's not clean.

Can anyone suggest how I refactor it to make it cleaner?

def distance(adj, s, t):
 n = len(adj)
 queue = []
 visited = set()
 path = []
 queue.append([s])
 dist = 0
 while (len(queue) > 0):
 path = queue.pop(0)
 last_vertex = path[-1]
 if last_vertex == t:
 # print(path)
 dist = len(path)-1
 elif last_vertex not in visited:
 for w in adj[last_vertex]:
 new_path = list(path)
 new_path.append((w))
 queue.append(new_path)
 visited.add(last_vertex)
 if dist != 0:
 return dist
 else:
 return -1
if __name__ == '__main__':
 input = sys.stdin.read()
 data = list(map(int, input.split()))
 n, m = data[0:2]
 data = data[2:]
 edges = list(zip(data[0:(2 * m):2], data[1:(2 * m):2]))
 adj = [[] for _ in range(n)]
 for (a, b) in edges:
 adj[a - 1].append(b - 1)
 adj[b - 1].append(a - 1)
 s, t = data[2 * m] - 1, data[2 * m + 1] - 1
 print(distance(adj, s, t))

I represent graph in such way:

4 5
2 1
4 3
1 4
2 4
3 2

The last two digits stands for two vertices, we need to find path between.

Loading
Source Link
Loading
lang-py

AltStyle によって変換されたページ (->オリジナル) /