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

Conteúdo do Curso

Breadth First Search

Breadth First Search

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

Is a Tree

BFS check if graph is a tree

And the last one required method is a bit simpler. Tree graph is a one-component acyclic graph, so you have to check first, if it is acyclic.

Acyclic

During BFS, you check vertices on different levels, until you find the end vertex. When traversing a tree, each time you check vertices that haven’t been visited yet. The cycle is detected when you check neighbors for the current node and at least one of them is visited - at this point you can approve that at least one cycle is detected.

Connectivity

After that, if the cycle is detected, return False, because one condition is already unsatisfied.

If cycle is not found, you should check the connectivity, and you can do it either:

  • Call hasOneComponentFunction()
  • Check the visited array directly

The second approach is much faster, and you don’t have to traverse again, like with calling hasOneComponent(). Return if all vertices are visited.

Tarefa

Implement isTree() method step by step.

Tarefa

Implement isTree() method step by step.

Mude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo

Tudo estava claro?

Seção 2. Capítulo 6
toggle bottom row

Is a Tree

BFS check if graph is a tree

And the last one required method is a bit simpler. Tree graph is a one-component acyclic graph, so you have to check first, if it is acyclic.

Acyclic

During BFS, you check vertices on different levels, until you find the end vertex. When traversing a tree, each time you check vertices that haven’t been visited yet. The cycle is detected when you check neighbors for the current node and at least one of them is visited - at this point you can approve that at least one cycle is detected.

Connectivity

After that, if the cycle is detected, return False, because one condition is already unsatisfied.

If cycle is not found, you should check the connectivity, and you can do it either:

  • Call hasOneComponentFunction()
  • Check the visited array directly

The second approach is much faster, and you don’t have to traverse again, like with calling hasOneComponent(). Return if all vertices are visited.

Tarefa

Implement isTree() method step by step.

Tarefa

Implement isTree() method step by step.

Mude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo

Tudo estava claro?

Seção 2. Capítulo 6
toggle bottom row

Is a Tree

BFS check if graph is a tree

And the last one required method is a bit simpler. Tree graph is a one-component acyclic graph, so you have to check first, if it is acyclic.

Acyclic

During BFS, you check vertices on different levels, until you find the end vertex. When traversing a tree, each time you check vertices that haven’t been visited yet. The cycle is detected when you check neighbors for the current node and at least one of them is visited - at this point you can approve that at least one cycle is detected.

Connectivity

After that, if the cycle is detected, return False, because one condition is already unsatisfied.

If cycle is not found, you should check the connectivity, and you can do it either:

  • Call hasOneComponentFunction()
  • Check the visited array directly

The second approach is much faster, and you don’t have to traverse again, like with calling hasOneComponent(). Return if all vertices are visited.

Tarefa

Implement isTree() method step by step.

Tarefa

Implement isTree() method step by step.

Mude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo

Tudo estava claro?

BFS check if graph is a tree

And the last one required method is a bit simpler. Tree graph is a one-component acyclic graph, so you have to check first, if it is acyclic.

Acyclic

During BFS, you check vertices on different levels, until you find the end vertex. When traversing a tree, each time you check vertices that haven’t been visited yet. The cycle is detected when you check neighbors for the current node and at least one of them is visited - at this point you can approve that at least one cycle is detected.

Connectivity

After that, if the cycle is detected, return False, because one condition is already unsatisfied.

If cycle is not found, you should check the connectivity, and you can do it either:

  • Call hasOneComponentFunction()
  • Check the visited array directly

The second approach is much faster, and you don’t have to traverse again, like with calling hasOneComponent(). Return if all vertices are visited.

Tarefa

Implement isTree() method step by step.

Mude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Seção 2. Capítulo 6
Mude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
We're sorry to hear that something went wrong. What happened?
some-alt