Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Shortest Path in Graph | Practice
Breadth First Search
course content

Зміст курсу

Breadth First Search

Breadth First Search

1. What is BFS
2. Practice
3. Improve Your Code
4. Solving the Problems using BFS

Shortest Path in Graph

BFS searching shortest path

Well done! Now, let's implement a method that helps us to find the length of shortest path between two vertices, i. e. minimum number of edges to reach end vertice from start.

You should store the length of the way from start to curr node, and you can do it by modifying visited array: visited[i] equals:

  • -1, if i not visited yet
  • 0, if i is visited as first node
  • 1, if i is a neighbor of node, that has mark 0
  • k, if i is a neighbor of node with mark k-1 etc.

This way, you'll store the distance between start and current node, like at the example:

So, the answer is a visited[end].

Завдання

bfs(start, end) returns a number of edges between start and end nodes. If there is no path, return -1.

Actually this method does traverse as BFT method, but until the end vertex is found. Copy & Paste your BFT algorithm, and add some changes.

Завдання

bfs(start, end) returns a number of edges between start and end nodes. If there is no path, return -1.

Actually this method does traverse as BFT method, but until the end vertex is found. Copy & Paste your BFT algorithm, and add some changes.

Перейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів

Все було зрозуміло?

Секція 2. Розділ 3
toggle bottom row

Shortest Path in Graph

BFS searching shortest path

Well done! Now, let's implement a method that helps us to find the length of shortest path between two vertices, i. e. minimum number of edges to reach end vertice from start.

You should store the length of the way from start to curr node, and you can do it by modifying visited array: visited[i] equals:

  • -1, if i not visited yet
  • 0, if i is visited as first node
  • 1, if i is a neighbor of node, that has mark 0
  • k, if i is a neighbor of node with mark k-1 etc.

This way, you'll store the distance between start and current node, like at the example:

So, the answer is a visited[end].

Завдання

bfs(start, end) returns a number of edges between start and end nodes. If there is no path, return -1.

Actually this method does traverse as BFT method, but until the end vertex is found. Copy & Paste your BFT algorithm, and add some changes.

Завдання

bfs(start, end) returns a number of edges between start and end nodes. If there is no path, return -1.

Actually this method does traverse as BFT method, but until the end vertex is found. Copy & Paste your BFT algorithm, and add some changes.

Перейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів

Все було зрозуміло?

Секція 2. Розділ 3
toggle bottom row

Shortest Path in Graph

BFS searching shortest path

Well done! Now, let's implement a method that helps us to find the length of shortest path between two vertices, i. e. minimum number of edges to reach end vertice from start.

You should store the length of the way from start to curr node, and you can do it by modifying visited array: visited[i] equals:

  • -1, if i not visited yet
  • 0, if i is visited as first node
  • 1, if i is a neighbor of node, that has mark 0
  • k, if i is a neighbor of node with mark k-1 etc.

This way, you'll store the distance between start and current node, like at the example:

So, the answer is a visited[end].

Завдання

bfs(start, end) returns a number of edges between start and end nodes. If there is no path, return -1.

Actually this method does traverse as BFT method, but until the end vertex is found. Copy & Paste your BFT algorithm, and add some changes.

Завдання

bfs(start, end) returns a number of edges between start and end nodes. If there is no path, return -1.

Actually this method does traverse as BFT method, but until the end vertex is found. Copy & Paste your BFT algorithm, and add some changes.

Перейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів

Все було зрозуміло?

BFS searching shortest path

Well done! Now, let's implement a method that helps us to find the length of shortest path between two vertices, i. e. minimum number of edges to reach end vertice from start.

You should store the length of the way from start to curr node, and you can do it by modifying visited array: visited[i] equals:

  • -1, if i not visited yet
  • 0, if i is visited as first node
  • 1, if i is a neighbor of node, that has mark 0
  • k, if i is a neighbor of node with mark k-1 etc.

This way, you'll store the distance between start and current node, like at the example:

So, the answer is a visited[end].

Завдання

bfs(start, end) returns a number of edges between start and end nodes. If there is no path, return -1.

Actually this method does traverse as BFT method, but until the end vertex is found. Copy & Paste your BFT algorithm, and add some changes.

Перейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Секція 2. Розділ 3
Перейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
We're sorry to hear that something went wrong. What happened?
some-alt