Today I went down a rabbit hole: Queue + BFS 🕳️🐇 I started with a question: 🧐 When people say BFS, why do they always bring up a queue? Here’s what I learned: ✅ Queue basics (Python): Queue = FIFO (first in, first out) 🚶♂️➡️🚶♀️ In Python, collections.deque is the right tool because popleft() is O(1) ⚡ list.pop(0) looks similar, but it becomes O(n) because everything shifts left 🐢 ✅ What BFS is actually doing: BFS explores a graph level by level 🪜 Start node → all nodes 1 step away → then 2 steps away → and so on. That “level order” is exactly why BFS uses a queue. ✅ The big missing piece: seen/visited 👀 Without seen, cycles can make BFS loop forever 🔁😬 So marking nodes as visited (usually when enqueuing) keeps traversal correct and efficient. 🤔 Fun detail: neighbor order changes traversal order A: [B, C] vs A: [C, B] → same BFS, different visitation sequence. #Python #DSA #BFS #Queue #Deque #Algorithms #InterviewPrep #BackendEngineering
Understanding BFS with Queue and Deque in Python
More Relevant Posts
-
Day 8 of 365 days of code: Qn 1) Remove the duplicates from the array. Given an integer array nums sorted in non-decreasing order, remove the duplicates in-place such that each unique element appears only once. The relative order of the elements should be kept the same. Consider the number of unique elements in nums to be k. After removing duplicates, return the number of unique elements k. The first k elements of nums should contain the unique numbers in sorted order. The remaining elements beyond index k - 1 can be ignored. use two pointer technique. let L=1 starting from position r=1, we overwrite the number at L if nums[r-1]!=nums[r]. this process is like copy pasting the unique elements on the left side of the array, in a sorted order. #365daysOfCode #NeetCode #leetcode #DSA #python #LeetCode #ProblemSolving #Algorithms #365dayschallenge
To view or add a comment, sign in
-
-
🌟 New Blog Just Published! 🌟 📌 5 Python Scripts to Automate Data Cleaning and Cut Hours 🚀 📖 Data-driven projects waste 60-80% of their timeline on cleaning alone. That means if you budget three months for a model, two of those months disappear before you ever touch an algorithm. Make sense?... 🔗 Read more: https://lnkd.in/dXZPJPBa 🚀✨ #python-data-cleani #pandas-automation #data-cleaning-scri
To view or add a comment, sign in
-
-
Day 61 of #Python108DaysChallenge 🐍 Learned 0-1 BFS, an optimized shortest-path algorithm for graphs with edge weights 0 or 1. ✔ Uses deque instead of priority queue ✔ Faster than Dijkstra for binary weights ✔ Time complexity O(V+E) ✔ Very common in interview problems Small constraints → big optimizations 💡 #Python #DSA #Graphs #BFS #Algorithms #LearningInPublic #CodingJourney
To view or add a comment, sign in
-
-
𝐃𝐚𝐲 𝟔: 𝐂𝐨𝐧𝐝𝐢𝐭𝐢𝐨𝐧𝐚𝐥 𝐒𝐭𝐚𝐭𝐞𝐦𝐞𝐧𝐭𝐬 𝐢𝐧 𝐏𝐲𝐭𝐡𝐨𝐧 🐍 Conditional statements are used to execute specific blocks of code based on whether a condition is true or false. 𝐓𝐲𝐩𝐞𝐬 𝐨𝐟 𝐂𝐨𝐧𝐝𝐢𝐭𝐢𝐨𝐧𝐚𝐥 𝐒𝐭𝐚𝐭𝐞𝐦𝐞𝐧𝐭𝐬: 1️⃣ if statement: Execute a code block only when the condition is True. 2️⃣ if–else statement: 🔹If condition is True → if code block runs 🔹 If condition is False → else code block runs 3️⃣ if–elif–else ladder: Used to check multiple conditions in sequence. 4️⃣ Nested if: An if statement inside another if for complex checks. 5️⃣Ternary Operator (Conditional Expressions): 🔹A single-line shorthand for a simple if-else statement. 🔹Ex: print("Yes") if condition else print("No") #Python #LearningDaily #DataScienceJourney #LearningInPublic #Consistency
To view or add a comment, sign in
-
Python with DSA — Day 33 What I worked on: Prime checks: moved from naive divisibility to the √n optimization and the 6k±1 rule to cut unnecessary iterations. Time complexity intuition: compared O(n) vs O(√n) for primality tests; saw how early exits change best/worst cases. Clean loops & edge cases: handled n <= 1, negative inputs, and printed primes from 1–100 with a tight loop. Key snippet (conceptual): Idea: Only test divisors up to √n; if none divide, the number is prime. #Day33 #PythonWithDSA #DataStructures #DSAJourney #ProblemSolving #PythonProgramming #SoftwareEngineer
To view or add a comment, sign in
-
-
Day 38/50 – DSA Problem-Solving Challenge Problem: Detect Cycle in Module Dependency Graph Concepts: Graphs | DFS | Cycle Detection | Recursion Stack Difficulty: Medium Optimized Approach: - Represent each module as a node - Each dependency u → v as a directed edge - Use DFS with a recursion stack - If a node is revisited while still in the recursion stack, a cycle is detected This technique efficiently catches even indirect cycles like: A → B → C → A Key Learnings: - Directed graphs model real-world dependency systems - DFS can detect cycles using a recursion path - Recursion stacks help identify back-edges - Circular dependencies are dangerous in production systems GitHub: https://lnkd.in/djSgeyci #Day38 #DSA #Graphs #CycleDetection #Python #ProblemSolving #CodingInterviews #LearningInPublic #Consistency #50DaysChallenge #SoftwareEngineering
To view or add a comment, sign in
-
-
#Day02 of 50 Days of Learning hashtag#Python through hashtag#Automation On Day 02, I moved from basics to a real-world automation task 📸 Today, I learned how to automatically resize multiple images using Python and the Pillow (PIL) library. What I covered: • What Pillow (PIL) is and why it’s needed • How to install external libraries using pip • Looping through folders to process files automatically • Resizing images while maintaining aspect ratio • Using Python for bulk image automation This script can resize hundreds of images in seconds — useful for: ✔ Websites ✔ Applications ✔ Automation workflows Read the full blog here 👇 https://lnkd.in/gSFXiAjW
To view or add a comment, sign in
-
𝗗𝗮𝘆 𝟭3: 𝗧𝘂𝗽𝗹𝗲𝘀 𝗶𝗻 𝗣𝘆𝘁𝗵𝗼𝗻 🐍 🔹 Tuples are ordered and immutable collections of items, separated by commas and enclosed in round brackets. 🔹 Items can be of multiple data types 🔹 Once created, tuples cannot be changed, added to, or deleted 𝗖𝗿𝗲𝗮𝘁𝗶𝗻𝗴 𝗧𝘂𝗽𝗹𝗲𝘀: ✔ Single-item tuple (comma is mandatory) ✔ Using parentheses: (1, 2, 3) ✔ Without parentheses: tup = 1, 2, 3 𝗖𝗼𝗺𝗺𝗼𝗻 𝗢𝗽𝗲𝗿𝗮𝘁𝗶𝗼𝗻𝘀: ✔ Indexing and slicing (same as lists) ✔ Unpacking – assign tuple values to multiple variables ✔ Concatenation – combine tuples using + 📝𝗡𝗼𝘁𝗲: 🔸 Tuples cannot be modified directly. 🔸 To make changes, convert the tuple to a list, modify it, and convert it back to a tuple. Building strong Python fundamentals, one concept at a time 🚀 #Python #Tuples #LearningPython #LearningInPublic #AspiringDataScientist #Consistency
To view or add a comment, sign in
-
Day 7 of 365 Days of code 🤌 Qn 1):Given head, the head of a linked list, determine if the linked list has a cycle in it. There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the next pointer. Internally, pos is used to denote the index of the node that tail's next pointer is connected to. Note that pos is not passed as a parameter. Return true if there is a cycle in the linked list. Otherwise, return false. Explanation: Use two different pointers, one is going to be a slow pointer and another one will be a fast pointer that moves twice the number of nodes of the slow pointer in a single iteration. If both the pointers meet at a same node, then the linked list consists of a cycle. #365daysOfCode #NeetCode #leetcode #DSA #python #LeetCode #ProblemSolving #Algorithms #365dayschallenge
To view or add a comment, sign in
-
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development