Algorithms

Breadth-First Traversal

4 months, 3 weeks ago ; 74 views
Share this

The solution provided implements a breadth-first search (BFS) traversal algorithm on a tree-like structure represented by the Node class.

Problem

The problem solved by this solution is to perform a breadth-first traversal of the tree, visiting each node in the tree level by level, from left to right.

class Node:
    """
    A class representing a node in a tree-like structure.

    Attributes:
        children (list of Node): The list of children nodes of the current node.
        name (str): The name of the current node.

    """

    def __init__(self, name):
        """
        Initializes a Node object with the given name.

        Args:
            name (str): The name of the node.

        """
        self.children = []
        self.name = name
        
    def addChild(self, name):
        """
        Adds a child node with the given name to the current node.

        Args:
            name (str): The name of the child node to be added.

        """
        self.children.append(Node(name))
        
    # O(v + e) time | O(v) space
    def breadth_first_search(self, array):
        """
        Performs a breadth-first search traversal of the tree starting from the current node.

        Args:
            array (list): The list to store the names of the visited nodes.

        Returns:
            list: The list of names of the visited nodes in breadth-first order.

        Example:
            Consider a tree structure as follows:
                            A
                          / | \
                         B  C  D
                        / \   / \
                       E   F G   H

            If we start the breadth-first search from node A,
            the output array should be ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'].

        """
        # Initialize a queue with the current node
        queue = [self]
        
        # Iterate until the queue is empty
        while queue:
            # Dequeue a node from the front of the queue
            current = queue.pop(0)
            
            # Append the name of the current node to the output array
            array.append(current.name)
            
            # Enqueue all children of the current node
            for child in current.children:
                queue.append(child)
        
        # Return the list of names of the visited nodes
        return array

Approach

The solution employs a queue-based approach to traverse the tree in a breadth-first manner. The algorithm starts at the root node and iteratively explores the nodes at each level of the tree.

It maintains a queue to keep track of the nodes to be visited, and at each iteration, it dequeues a node from the front of the queue, visits it, and enqueues its children.

This process continues until all nodes in the tree have been visited.

Use Case

Breadth-first search traversal is commonly used in various scenarios, such as:

  •     Tree traversal: Visiting all nodes of a tree level by level.
  •     Shortest path finding: Finding the shortest path between two nodes in a graph or tree.
  •     Finding connected components: Identifying connected components in a graph or tree.

Application

This traversal order can be useful for various applications, such as searching for specific nodes, analyzing the structure of the tree, or performing operations that require visiting nodes in a systematic order.

Time Complexity

  •     Traversal: The algorithm traverses each node of the tree exactly once.
  •     Queue Operations: Each node is added to the queue once and removed from the queue once.
  •     Overall Time Complexity: If v represents the number of vertices (nodes) and e represents the number of edges in the tree, the time complexity of BFS is O(v+e).

Space Complexity

  •     Queue Space: At any point in time, the queue can contain at most v nodes (vertices) in the case where the entire tree is traversed breadth-first.
  •     Output Array: The space required for the output array to store the names of visited nodes.
  •     Overall Space Complexity: Considering both the queue space and the space required for the output array, the space complexity is O(v).

Unit Tests

Here are some unit tests using Python's unittest module to test the robustness of the solution above.

import unittest


class TestNodeBFS(unittest.TestCase):
    def test_bfs_example(self):
        # Create a tree structure
        root = Node('A')
        root.addChild('B')
        root.addChild('C')
        root.addChild('D')
        root.children[0].addChild('E')
        root.children[0].addChild('F')
        root.children[2].addChild('G')
        root.children[2].addChild('H')

        # Perform BFS from the root node
        result = root.breadth_first_search([])

        # Expected output: ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']
        self.assertEqual(result, ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'])

    def test_empty_tree(self):
        # Create an empty tree
        root = Node('Empty')

        # Perform BFS from the root node
        result = root.breadth_first_search([])

        # Expected output: ['Empty']
        self.assertEqual(result, ['Empty'])

    def test_single_node_tree(self):
        # Create a tree with a single node
        root = Node('Root')

        # Perform BFS from the root node
        result = root.breadth_first_search([])

        # Expected output: ['Root']
        self.assertEqual(result, ['Root'])

    def test_tree_with_one_level(self):
        # Create a tree with one level
        root = Node('A')
        root.addChild('B')
        root.addChild('C')

        # Perform BFS from the root node
        result = root.breadth_first_search([])

        # Expected output: ['A', 'B', 'C']
        self.assertEqual(result, ['A', 'B', 'C'])


if __name__ == '__main__':
    unittest.main()

These unit tests cover various scenarios like:

  •     Testing with a tree structure example.
  •     Testing with an empty tree.
  •     Testing with a tree containing a single node.
  •     Testing with a tree containing only one level of nodes.

Each test case asserts whether the breadth-first search (breadth_first_search) method returns the expected result for a given input tree structure.

These tests help ensure the correctness and robustness of this BFS implementation.

 

Become a member
Get the latest news right in your inbox. We never spam!

Read next

Island Perimeter

 This solution seeks to find the perimeter of an island in a grid.  The problem considers a grid where each cell represents land (1) or … Read More

Kibsoft 3 months, 3 weeks ago . 76 views

Pacific Atlantic Waterflow

3 months, 3 weeks ago . 82 views