Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5951017
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:28:36+00:00 2026-05-22T17:28:36+00:00

I came across a BFS code which involves collections and deques but I could

  • 0

I came across a BFS code which involves collections and deques but I could not understand it much. I hope some of the pythonistas here can help a n00b out.

from collections import deque

def bfs(g, start):
    queue, enqueued = deque([(None, start)]), set([start])
    while queue:
        parent, n = queue.popleft()
        yield parent, n
        new = set(g[n]) - enqueued
        enqueued |= new
        queue.extend([(n, child) for child in new])

Questions:

1) The |= operator seems to be related to bitwise operations – I have no idea how it relates to a BFS, any hints?

2) popleft() should return only one value from what I understand, so how is it returning parent and n here?

3) Is new the series of nodes visited? If I want the nodes, do I just keep appending them to a list?

Thanks in advance.

Craig

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T17:28:37+00:00Added an answer on May 22, 2026 at 5:28 pm
    1. a |= b for sets is the same
      as a = a.union(b).

    2. popleft() does indeed return
      only one element, which happens to
      be a 2-tuple, and therefore can be
      unpacked into two values.

    3. new is the set of not yet
      visited nodes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Came across a problem which google searches could not help me with. I have
I came across code written by somebody which caught some exception and tried logging
I came across this code today and don't really understand it. Please could someone
Came across this conditional in some uncommented Objective-C code: if (w & (w -
I came across this code in ShareKit, and I don't understand what the writer
I came across this syntax which i have not seen before struct A {
I came across some java code for doing a prefix search on a JList.
I came across this in some example code: - (IBAction) startPlayLowNote:(id)sender { UInt32 noteNum
I came across this strange looking declaration in some code early this morning (before
I came across a controller in an older set of code (Rails 1.2.3) that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.