Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

breadth first search python

"""
breadFirstSearch method traverses a tree using
the Breadth-first search approach storing
its node values in an array and then
returns the resulting array.
"""
class Node:
    def __init__(self, value):
        self.value = value
        self.children = []

    def breadFirstSearch(self, array):
        queue = [self]
        while len(queue) > 0:
            current = queue.pop(0)
            array.append(current.value)
            for child in current.children:
                queue.append(child)
        return array
        
Comment

breadth first search graph python

def bfs(node):
    """ graph breadth-first search - iterative """
    from collections import deque
    
    q = deque()
    q.append(node)
    node.visited = True
    
    while q:
        current = d.popleft()
        print(current)  
            
        for node in current.adjList:
            if not node.visited: 
                q.append(node)
                node.visited = True
Comment

PREVIOUS NEXT
Code Example
Python :: numpy make 2d array 1d 
Python :: head first python 
Python :: how to take a list as input in python using sys.srgv 
Python :: python portfolio projects 
Python :: python move item in list to another list 
Python :: negative indexing in python 
Python :: recursive binary search python 
Python :: python3 format leading 0 
Python :: python template strings 
Python :: download youtube video 
Python :: convert radians to degrees python 
Python :: how to check if given primary key exists in django model 
Python :: jsonresponse django 
Python :: how to repeat code in python until a condition is met 
Python :: create exact window size tkinter 
Python :: change python from 3.8 to 3.7 
Python :: tkinter python button 
Python :: Difference between append() and extend() method in Python 
Python :: for loop 
Python :: python close a socket 
Python :: python how to print variable value 
Python :: python for loop 
Python :: python program to reverse a list 
Python :: how to change the size of datapoint in plot python 
Python :: How do I schedule an email to send at a certain time using cron and smtp, in python 
Python :: selenium delete cookies python 
Python :: how to make reportlab table header bold in python 
Python :: Changing the data type to category 
Python :: create database python 
Python :: how to plot side by side bar horizontal bar graph in python 
ADD CONTENT
Topic
Content
Source link
Name
4+2 =