<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Khushi</title>
    <description>The latest articles on DEV Community by Khushi (@khushi2488).</description>
    <link>https://dev.to/khushi2488</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3038481%2F846bed96-501b-4072-b320-9024abad74da.png</url>
      <title>DEV Community: Khushi</title>
      <link>https://dev.to/khushi2488</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/khushi2488"/>
    <language>en</language>
    <item>
      <title>Generative AI vs Agentic AI</title>
      <dc:creator>Khushi</dc:creator>
      <pubDate>Wed, 21 Jan 2026 07:44:42 +0000</pubDate>
      <link>https://dev.to/khushi2488/generative-ai-vs-agentic-ai-5c27</link>
      <guid>https://dev.to/khushi2488/generative-ai-vs-agentic-ai-5c27</guid>
      <description>&lt;p&gt;&lt;strong&gt;Generative AI vs Agentic AI: Capability vs Behavior&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Generative AI and Agentic AI are often discussed together, but they represent two very different layers of intelligence.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Generative AI is primarily about creation.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;It reacts to a prompt and generates content-text, images, code, or audio-based on learned patterns. Its role is largely responsive: it waits for instructions and delivers an output. In this sense, Generative AI acts as a powerful capability, not an independent decision-maker.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Agentic AI, on the other hand, is about achieving goals.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Instead of simply responding to a single prompt, Agentic AI can plan, reason, take actions, evaluate outcomes, and iterate until a goal is accomplished. It is proactive and autonomous, capable of deciding what to do next without continuous human intervention.&lt;/p&gt;

&lt;p&gt;Think of it this way:&lt;br&gt;
&lt;strong&gt;Generative AI answers questions&lt;br&gt;
Agentic AI solves problems&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Importantly, Generative AI is a building block of Agentic AI. Agentic systems often rely on generative models for reasoning, communication, and content generation-but they go beyond generation by adding decision-making, memory, and control loops.&lt;/p&gt;

&lt;p&gt;In short:&lt;br&gt;
&lt;strong&gt;Generative AI = Capability&lt;/strong&gt; (what the system can do)&lt;br&gt;
&lt;strong&gt;Agentic AI = Behavior&lt;/strong&gt; (how the system acts to achieve goals)&lt;/p&gt;

&lt;p&gt;As AI systems evolve, the shift from purely generative tools to agentic systems marks a move from assistance to autonomy-and that distinction will define the next generation of intelligent applications.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is Generative AI?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Generative AI focuses on creating human-like content such as text, images, code, or videos-only when prompted.&lt;/p&gt;

&lt;p&gt;Examples:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;ChatGPT → text &amp;amp; code&lt;/li&gt;
&lt;li&gt;DALL·E → images&lt;/li&gt;
&lt;li&gt;Sora → video&lt;/li&gt;
&lt;li&gt;Nature: Reactive
You ask a question → it generates a response
No initiative, no long-term goal tracking&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Generative AI is incredibly powerful, but it operates in a request-response loop. It does not decide what needs to be done next.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is Agentic AI?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Agentic AI is designed to achieve goals autonomously.&lt;br&gt;
Instead of waiting for prompts,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;it Understands a high-level objective&lt;/li&gt;
&lt;li&gt;Breaks it into steps&lt;/li&gt;
&lt;li&gt;Uses tools and data&lt;/li&gt;
&lt;li&gt;Executes actions&lt;/li&gt;
&lt;li&gt;Monitors results and adapts&lt;/li&gt;
&lt;li&gt;Nature: Proactive
You give a goal → it plans, executes, and iterates
Can seek approvals, retry failed steps, and optimize outcomes&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In short, Agentic AI behaves more like a digital worker than a smart assistant.&lt;/p&gt;

&lt;h2&gt;
  
  
  Evolution Through Hiring Example
&lt;/h2&gt;

&lt;p&gt;The video shows 4 stages improving a chatbot for hiring a backend engineer:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Basic GenAI&lt;/strong&gt;: Helps draft JD, emails, questions → You do all actions manually.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;RAG (Retrieval-Augmented)&lt;/strong&gt;: Uses company docs → Tailored JDs, questions.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Tools Added&lt;/strong&gt;: APIs for LinkedIn, email, calendar → Auto-posts jobs, schedules.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Agentic AI&lt;/strong&gt;: Understands goal → Plans entire flow, monitors applications, adapts (boosts job if low apps), handles onboarding.
&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Interview Points (1-Minute Pitch)
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;"Generative AI creates content reactively. Agentic AI achieves goals proactively."&lt;/strong&gt;   &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;GenAI&lt;/strong&gt;: Prompt → Output (ChatGPT drafts email).
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Agentic&lt;/strong&gt;: Goal → Plan → Execute → Adapt (Hire engineer end-to-end).
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Built using&lt;/strong&gt;: GenAI (LLMs) + RAG + Tools + Planning/Memory.
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>agents</category>
      <category>ai</category>
      <category>automation</category>
      <category>machinelearning</category>
    </item>
    <item>
      <title>RAG - The Smart Way to Improve AI Answers</title>
      <dc:creator>Khushi</dc:creator>
      <pubDate>Thu, 20 Nov 2025 14:57:36 +0000</pubDate>
      <link>https://dev.to/khushi2488/rag-the-smart-way-to-improve-ai-answers-526a</link>
      <guid>https://dev.to/khushi2488/rag-the-smart-way-to-improve-ai-answers-526a</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F79qdavxe6nfgfgjr19b9.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F79qdavxe6nfgfgjr19b9.jpg" alt=" " width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;R&lt;/strong&gt;etrieval-Augmented Generation (RAG) is transforming how we use AI by allowing models to think with real, live information instead of relying only on what they were trained on.
&lt;/h4&gt;

&lt;h2&gt;
  
  
  What is RAG ?
&lt;/h2&gt;

&lt;p&gt;Retrieval-Augmented Generation (RAG) is an AI architecture that enhances large language models by retrieving up-to-date, domain-specific information from external knowledge sources and combining it with the model’s generation capabilities to produce accurate, factual, and context-aware responses.&lt;/p&gt;

&lt;p&gt;To operationalize this concept, I built an end-to-end RAG pipeline that implements multi-modal PDF parsing (native text, tables, OCR), semantic chunking and sentence-transformer embeddings stored in FAISS for sub-second vector similarity search. Retrieved passages are reranked via cross-encoders and injected into LLM context, producing source-grounded responses with citation tracking—transforming static language models into retrieval-aware systems capable of handling enterprise document QA with verifiable, hallucination-resistant answers.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why RAG was introduced ?
&lt;/h2&gt;

&lt;p&gt;RAG was introduced to solve the limitations of traditional Large Language Models.Even though LLMs are powerful, they have major problems:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;LLMs hallucinate-LLMs generate answers based on patterns they learned during training, not on real-time facts. So they sometimes produce confident but incorrect answers.&lt;/li&gt;
&lt;li&gt;LLMs cannot store enterprise-specific knowledge.&lt;/li&gt;
&lt;li&gt;LLMs have context-length limitations.&lt;/li&gt;
&lt;li&gt;Fine-tuning alone is expensive and slow.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;RAG was introduced to make LLMs more accurate, up-to-date, and trustworthy by giving them access to real-time, external knowledge.&lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding RAG Components
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;1) Retriever (Search System):&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What it does:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Takes your query (question)&lt;/li&gt;
&lt;li&gt;Searches a knowledge base or vector database&lt;/li&gt;
&lt;li&gt;Retrieves the most relevant documents based on similarity&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;2) Knowledge Store:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This is where all your information is stored.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What it contains:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Text documents&lt;/li&gt;
&lt;li&gt;FAQ answers&lt;/li&gt;
&lt;li&gt;PDFs&lt;/li&gt;
&lt;li&gt;Website content&lt;/li&gt;
&lt;li&gt;Company policies&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Any internal data&lt;br&gt;
Each document is broken into chunks and converted into embeddings.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Common vector stores&lt;/strong&gt;: Pinecone, FAISS, Weaviate, Chroma DB&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;3) Generator:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This is the AI model that generates the final answer using the retrieved information.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What it does:&lt;/strong&gt;&lt;br&gt;
It takes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;User question&lt;/li&gt;
&lt;li&gt;Retrieved documents
Then Combines both and gives a factual, accurate answer&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  How RAG works — step-by-step
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4xwfi27bivca7ii8n0o8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4xwfi27bivca7ii8n0o8.png" alt=" " width="746" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Ingest documents&lt;/strong&gt; - Collect the text sources you want the system to use (PDFs, docs, emails, wiki pages).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Chunking&lt;/strong&gt; - Split long documents into smaller passages (e.g., 200–1,000 tokens) so retrieval can return precise context.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Embed the chunks&lt;/strong&gt; - Convert each chunk into a numeric vector using an embedding model (semantic representation).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Index / store vectors&lt;/strong&gt; - Put those vectors into a vector index (ANN/FAISS/Weaviate/Pinecone/Qdrant) for fast similarity search.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;User query → embed&lt;/strong&gt; - When a user asks something, convert the query into an embedding with the same model used for documents.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Vector search (retrieve)&lt;/strong&gt; - Use the query embedding to find the top-k most similar document chunks from the index (e.g., top 5–50).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Optional rerank&lt;/strong&gt; - Reorder the retrieved chunks with a stronger cross-encoder or heuristic to improve relevance (at the cost of extra latency).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Construct prompt / context&lt;/strong&gt; - Build the prompt for the LLM by combining the user query and the selected retrieved chunks (with any instructions or system message).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;LLM generation&lt;/strong&gt; - Send the prompt to the language model; the model generates an answer grounded in the retrieved context.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Post-process &amp;amp; cite&lt;/strong&gt; - Optionally filter the output, extract sources, add citations/footnotes, or run a verifier to check factuality.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cache &amp;amp; feedback loop&lt;/strong&gt; - Cache frequent query results, log user feedback, and update the index or rerank models to improve future results.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Following this architecture, I implemented a production-ready system that handles the complexity of real-world documents—processing heterogeneous PDFs through multi-modal extraction, chunking semantically at 512-token boundaries, and indexing with FAISS for millisecond retrieval. The addition of cross-encoder reranking and structured citation extraction ensures responses aren't just accurate, but verifiable and trustworthy for enterprise use cases.&lt;/p&gt;

&lt;p&gt;🔗 &lt;strong&gt;Implementation&lt;/strong&gt;:&lt;a href="https://github.com/khushi2488/RAG_Document_QA_System" rel="noopener noreferrer"&gt;https://github.com/khushi2488/RAG_Document_QA_System&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Challenges :
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Handling Noisy or Incomplete Knowledge Bases - RAG depends completely on the quality of the data stored in the knowledge base (documents, PDFs, text, etc.).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Latency and scalability bottlenecks occur in RAG because every query requires multiple steps—embedding, vector search, reranking, and LLM generation—which slows down responses. As data volumes and user traffic grow, these pipelines become harder and more expensive to scale, making fast, real-time RAG performance a key challenge in today’s AI systems.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;RAG systems must balance retrieving many documents (breadth) with retrieving only the most relevant ones (depth). Too much breadth increases noise and slows down the model, while too much depth risks missing important context—making this trade-off a critical challenge for building accurate and efficient RAG pipelines.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  How People Are Using RAG
&lt;/h2&gt;

&lt;p&gt;Retrieval-Augmented Generation (RAG) lets users “talk” to their data. By connecting an LLM with a knowledge base—like manuals, documents, logs, or videos—RAG can deliver accurate, context-rich answers.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Where RAG is used:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Healthcare:&lt;/strong&gt; Doctors and nurses get quick, reliable support from systems connected to medical indexes.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Finance:&lt;/strong&gt; Analysts can query real-time or historical market data for insights.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Businesses:&lt;/strong&gt; Any company can turn internal documents into smart assistants that help with:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Customer and field support&lt;/li&gt;
&lt;li&gt;Employee training&lt;/li&gt;
&lt;li&gt;Developer and IT productivity&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;Because RAG can work with any dataset, its applications are multiplying. This is why major companies like &lt;strong&gt;AWS, IBM, Google, Microsoft, NVIDIA, Oracle, Glean, and Pinecone&lt;/strong&gt; are adopting RAG widely.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;RAG Vs Fine Tuning:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3qmejva3l2l16u2qtu2t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3qmejva3l2l16u2qtu2t.png" alt=" " width="780" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh9q6lv5dzi7e43kr2559.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh9q6lv5dzi7e43kr2559.png" alt=" " width="800" height="417"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Tools &amp;amp; Frameworks for Building RAG
&lt;/h2&gt;

&lt;p&gt;RAG systems rely on several key tools working together to retrieve the right information and generate accurate answers.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Embedding Models:&lt;/strong&gt; OpenAI, Sentence Transformers, Cohere, Google Vertex&lt;br&gt;
&lt;strong&gt;Vector Databases:&lt;/strong&gt; Pinecone, Weaviate, Qdrant, Milvus, FAISS&lt;br&gt;
&lt;strong&gt;RAG Frameworks:&lt;/strong&gt; LangChain, LlamaIndex, Haystack, DSPy&lt;br&gt;
&lt;strong&gt;LLMs:&lt;/strong&gt; GPT, Llama 3, Gemini, Mistral, Claude&lt;br&gt;
&lt;strong&gt;Document Processing:&lt;/strong&gt; Apache Tika, PyPDF2, Unstructured.io&lt;br&gt;
&lt;strong&gt;Evaluation &amp;amp; Monitoring:&lt;/strong&gt; TruLens, RAGAS, Arize Phoenix, Weights &amp;amp; Biases&lt;/p&gt;

&lt;h2&gt;
  
  
  Future of the RAG:
&lt;/h2&gt;

&lt;p&gt;The future of RAG is moving toward faster, smarter, and more autonomous AI systems. RAG will become more efficient with advanced vector databases, better retrieval algorithms, and larger context windows, allowing AI to use massive knowledge sources without slowing down. It will also evolve into RAG 2.0 and 3.0 architectures, where models combine retrieval, reasoning, and verification to produce more accurate, trustworthy outputs. As companies adopt AI at scale, RAG will play a key role in keeping enterprise systems up-to-date, cost-effective, and grounded in reliable information.&lt;/p&gt;

</description>
      <category>rag</category>
      <category>architecture</category>
      <category>llm</category>
      <category>ai</category>
    </item>
    <item>
      <title>Cycle Detection in the Directed Graph using the DFS</title>
      <dc:creator>Khushi</dc:creator>
      <pubDate>Sun, 31 Aug 2025 17:12:34 +0000</pubDate>
      <link>https://dev.to/khushi2488/cycle-detection-in-the-directed-graph-using-the-dfs-3lkm</link>
      <guid>https://dev.to/khushi2488/cycle-detection-in-the-directed-graph-using-the-dfs-3lkm</guid>
      <description>&lt;p&gt;Cycle detection in a directed graph is a classic algorithm problem and must-know for backend and platform engineers.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is a cycle in a directed graph?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The edges of a directed graph, which resemble arrows pointing from one node to another, have a specified direction.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;When you can begin at a node, follow the directed edges, and ultimately return to the same node without retracing any edges backward, you have a cycle.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In simple terms: A cycle is a loop of dependencies.&lt;br&gt;
Imagine tasks A, B, and C with these relationships:&lt;br&gt;
A → B (A must be done before B)&lt;br&gt;
B → C (B must be done before C)&lt;br&gt;
C → A (C must be done before A)&lt;br&gt;
Here, you’re stuck in a loop:&lt;br&gt;
A depends on B → B depends on C → C depends on A.&lt;br&gt;
There’s no valid starting point — that’s a cycle&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why cycle detection is important?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Cycles mean circular dependencies → no valid ordering is possible&lt;/li&gt;
&lt;li&gt;When you can continue following arrows and go back to the beginning point, you have a cycle in a directed graph. Tasks or dependencies cannot be arranged in a valid sequence because of this loop.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Why DFS for Cycle Detection?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Because it thoroughly examines each path before turning around, Depth-First Search (DFS) is especially well-suited for identifying cycles in a directed graph and aids in the effective detection of loops.&lt;/li&gt;
&lt;li&gt;Because it tracks the current traversal, follows the paths naturally, and effectively detects cycles in directed graphs, DFS is recommended.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Intuition for DFS-based Cycle Detection&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1: Select any node that hasn't been investigated yet and launch DFS from it.&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;This guarantees that every disconnected graph element is examined.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Step 2: Add it to the recursion stack and mark it as visited.&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Nodes that we've seen at least once are indicated by Visited[].&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Nodes that are a component of the current DFS path are marked by the Recursion Stack (PathVisited[]).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Why are there two arrays?&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Visited[] stops unnecessary node revisits.&lt;br&gt;
  Cycles along the present path are detected with the use of the recursion stack.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3: Recursively examine every neighbor&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Visit each of the current node's neighbors.&lt;/li&gt;
&lt;li&gt;For every neighbor:

&lt;ul&gt;
&lt;li&gt;Call DFS about it if it hasn't been visited.&lt;/li&gt;
&lt;li&gt;A cycle is identified if it is in the recursion stack (we're revisiting a node in the current path).&lt;/li&gt;
&lt;li&gt;Repeat for everyone.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Step 4: Identifying the Cycle - We may identify a loop as soon as we discover a neighbor that is already on the recursion stack.&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;An illustration of intuition

&lt;ul&gt;
&lt;li&gt;Imagine traversing a maze.&lt;/li&gt;
&lt;li&gt;You are going around in circles if you enter a room that you have already visited on this journey.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Step 5: Backtracking - Take the node out of the recursion stack after exploring all of its neighbors.&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;This stage guarantees that the recursion stack only includes nodes that are presently being explored and not ones that have been fully processed.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Step 6: Do this for every node that hasn't been visited.&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Start DFS on the unconnected components that haven't been visited yet.This guarantees full cycle detection throughout the graph.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Important Point to Keep in Mind&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The key to identifying cycles is the recursion stack.&lt;/li&gt;
&lt;li&gt;Visited[] by itself is insufficient.&lt;/li&gt;
&lt;li&gt;You can only determine whether a cycle is present by monitoring the nodes in the current DFS path.&lt;/li&gt;
&lt;li&gt;Consider this: "I'm in a loop if I go back to a node that's still on my current path!"&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Let's understand through the Dry Run,&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvy9czukxw3e58paswrcr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvy9czukxw3e58paswrcr.png" alt=" " width="800" height="800"&gt;&lt;/a&gt;&lt;br&gt;
Let’s take 4 nodes (0, 1, 2, 3) with edges:&lt;br&gt;
0 → 1&lt;br&gt;
1 → 2&lt;br&gt;
2 → 3&lt;br&gt;
3 → 1   (Back edge → creates cycle)&lt;/p&gt;

&lt;p&gt;Initial State&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk1g5ckhmp2mhgovwc0ev.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk1g5ckhmp2mhgovwc0ev.jpeg" alt=" " width="800" height="333"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1.Start DFS at Node 0&lt;/strong&gt;&lt;br&gt;
Mark 0 visited &amp;amp; in recursion stack.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5itrlrhtxkfvg3yqiy5s.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5itrlrhtxkfvg3yqiy5s.jpeg" alt=" " width="800" height="395"&gt;&lt;/a&gt;&lt;br&gt;
Neighbors of 0: 1&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2: DFS(1)&lt;/strong&gt;&lt;br&gt;
Mark 1 visited &amp;amp; in recursion stack.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4ety1gn0fbahkj8rh9r2.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4ety1gn0fbahkj8rh9r2.jpeg" alt=" " width="800" height="495"&gt;&lt;/a&gt;&lt;br&gt;
Neighbors of 1: 2&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3: DFS(2)&lt;/strong&gt;&lt;br&gt;
Mark 2 visited &amp;amp; in recursion stack.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftz6svxvsg3uay2egkysd.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftz6svxvsg3uay2egkysd.jpeg" alt=" " width="800" height="427"&gt;&lt;/a&gt;&lt;br&gt;
Neighbors of 2: 3&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4: DFS(3)&lt;/strong&gt;&lt;br&gt;
Mark 3 visited &amp;amp; in recursion stack.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F003eg0lct750jk0vnmr9.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F003eg0lct750jk0vnmr9.jpeg" alt=" " width="800" height="531"&gt;&lt;/a&gt;&lt;br&gt;
Neighbors of 3: 1&lt;/p&gt;

&lt;p&gt;But 1 is already in recursion stack → &lt;strong&gt;Cycle Detected!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why this detects a cycle:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When a DFS from node u sees an edge u → v where v is already on the current recursion stack, it means there is a path from v down to u (because v is an ancestor in the recursion), and now u points back to v — so we have a directed cycle.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Backtracking (after detection)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;After detecting the cycle, typical DFS cycle-detection code will return True up the call chain to stop further exploration (or record the cycle).&lt;br&gt;
Recursion unwinds, and recStack entries are cleared (set back to False) as each call returns.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;C++ Code: Cycle Detection in Directed Graph (DFS)&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#include &amp;lt;bits/stdc++.h&amp;gt;
using namespace std;

bool dfsCycle(int node, vector&amp;lt;vector&amp;lt;int&amp;gt;&amp;gt; &amp;amp;graph, vector&amp;lt;bool&amp;gt; &amp;amp;visited, vector&amp;lt;bool&amp;gt; &amp;amp;recStack) {
    visited[node] = true;
    recStack[node] = true;

    for (int neighbor : graph[node]) {
        if (!visited[neighbor]) {
            if (dfsCycle(neighbor, graph, visited, recStack))
                return true;
        }
        else if (recStack[neighbor]) {
            // Found a back edge → cycle
            return true;
        }
    }

    recStack[node] = false; // backtrack
    return false;
}

bool isCyclic(int V, vector&amp;lt;pair&amp;lt;int,int&amp;gt;&amp;gt; &amp;amp;edges) {
    vector&amp;lt;vector&amp;lt;int&amp;gt;&amp;gt; graph(V);
    for (auto &amp;amp;e : edges) {
        graph[e.first].push_back(e.second);
    }

    vector&amp;lt;bool&amp;gt; visited(V, false);
    vector&amp;lt;bool&amp;gt; recStack(V, false);

    for (int i = 0; i &amp;lt; V; i++) {
        if (!visited[i]) {
            if (dfsCycle(i, graph, visited, recStack))
                return true;
        }
    }
    return false;
}

int main() {
    int V = 4;
    vector&amp;lt;pair&amp;lt;int,int&amp;gt;&amp;gt; edges = {
        {0, 1},
        {1, 2},
        {2, 3},
        {3, 1} // cycle here
    };

    if (isCyclic(V, edges))
        cout &amp;lt;&amp;lt; "Cycle detected in the graph!" &amp;lt;&amp;lt; endl;
    else
        cout &amp;lt;&amp;lt; "No cycle found." &amp;lt;&amp;lt; endl;

    return 0;
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Time Complexity&lt;/strong&gt;&lt;br&gt;
We perform a DFS traversal of the graph.&lt;br&gt;
In DFS, each vertex (V) is visited once, and each edge (E) is explored once.&lt;br&gt;
Thus, &lt;strong&gt;total complexity: O(V+E)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Space Complexity&lt;/strong&gt;&lt;br&gt;
Visited Array → O(V)&lt;br&gt;
Recursion Stack Array → O(V)&lt;br&gt;
Recursive Call Stack (function calls) → O(V) in the worst case (when the graph is like a long chain).&lt;br&gt;
So, &lt;strong&gt;total space complexity: O(V)&lt;/strong&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Topological Sort</title>
      <dc:creator>Khushi</dc:creator>
      <pubDate>Sun, 31 Aug 2025 17:02:57 +0000</pubDate>
      <link>https://dev.to/khushi2488/topological-sort-1hm3</link>
      <guid>https://dev.to/khushi2488/topological-sort-1hm3</guid>
      <description>&lt;p&gt;&lt;strong&gt;What is Topological Sorting?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Topological Sorting is a method to organize the vertices of a Directed Acyclic Graph (DAG) in a sequence where, for every directed edge u → v, vertex u precedes vertex v in the sequence.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Topological sorting is the act of sequencing “items with prerequisites”&lt;br&gt;
in an order such that each thing comes only after everything it depends on.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Why only DAGs?&lt;/strong&gt;&lt;br&gt;
First let's understand ,&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1) Why Directed?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Dependencies always exhibit a specific direction: If A → B, it means “A must come before B.” in an undirected graph the relationship can be unclear.&lt;/li&gt;
&lt;li&gt;If A and B are connected, does A depend on B or B depend on A? There’s no way to tell. Therefore, directed edges are necessary to clearly indicate which task is dependent on which.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;2)Why Acyclic?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A cycle refers to a situation where tasks are mutually dependent on one another in a circular manner.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;For instance:&lt;/strong&gt;&lt;br&gt;
A → B (A precedes B)&lt;br&gt;
B → C (B precedes C)&lt;br&gt;
C → A (C precedes A)&lt;br&gt;
At this point, we're in a stalemate:&lt;br&gt;
A needs to occur before B,&lt;br&gt;
B before C,&lt;br&gt;
C before A.&lt;br&gt;
This presents a paradox → no permissible sequence can be determined. &lt;br&gt;
This is why cycles render topological sorting unachievable.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Real-life example&lt;/strong&gt;:-&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1) Software Development (Compilation Sequence)&lt;/strong&gt;&lt;br&gt;
During the compilation of extensive software projects, it is crucial for the compiler to adhere to file dependencies.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmqskcb27e90lc70tf4p5.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmqskcb27e90lc70tf4p5.jpeg" alt=" " width="800" height="499"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For instance:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Utility.h needs to be compiled prior to Main.cpp (since Main.cpp includes it).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Math.cpp relies on Math.h (which means Math.h must be compiled first).&lt;br&gt;
Neglecting these dependencies will result in compilation errors.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;👉 The compiler uses topological sorting internally to determine the appropriate order for compiling files.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2) Project Management (Task Scheduling)&lt;/strong&gt;&lt;br&gt;
In project tasks, there are often dependencies, similar to nodes in a graph.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fexao82cw3gce5oyvsf1a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fexao82cw3gce5oyvsf1a.png" alt=" " width="591" height="405"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Example Project Flow:&lt;br&gt;
Task A: Collect requirements.&lt;br&gt;
Task B: Design system architecture (depends on A).&lt;br&gt;
Task C: Develop code (depends on B).&lt;br&gt;
Task D: Conduct software testing (depends on C).&lt;br&gt;
Task E: Create documentation (depends on B, but is independent of C/D).&lt;br&gt;
In this case, a suitable sequence could be:&lt;br&gt;
A → B → C → D and B → E (allowing E to proceed in parallel after B).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Approaches for Topological Sorting&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;There are two primary methods:&lt;br&gt;
1.The In-degree Method (BFS) or Kahn's Algorithm&lt;br&gt;
2.The Depth-First Search (DFS) method&lt;/p&gt;

&lt;p&gt;Let’s understand &lt;strong&gt;Kahn’s Algorithm&lt;/strong&gt; in detail in this blog.&lt;br&gt;
&lt;strong&gt;Core Concept:&lt;/strong&gt;&lt;br&gt;
Kahn’s Algorithm is a BFS-based technique that builds a topological order by repeatedly removing nodes with no incoming edges (in-degree = 0).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Intuition&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Nodes with in-degree = 0 represent tasks with no pending prerequisites → they can be executed immediately.&lt;/li&gt;
&lt;li&gt;Once such a task is completed, we update the in-degree of its dependent tasks.&lt;/li&gt;
&lt;li&gt;If any dependent task’s in-degree becomes 0, it is ready to execute next.
This process continues until all nodes are processed.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Steps in Kahn’s Algorithm&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1: Compute In-Degree of Each Node&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In-degree = number of incoming edges to a node.&lt;br&gt;
For every edge A → B, increment in-degree of B by 1.&lt;br&gt;
Store in-degree values in an array or hash map.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2: Initialize a Queue with All In-degree 0 Nodes&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;These nodes have no dependencies → they can be done right away.&lt;br&gt;
Use a queue (FIFO) to process them.&lt;br&gt;
If multiple nodes have in-degree 0, any order is valid&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3: Process Queue Iteratively&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;While the queue is not empty:&lt;br&gt;
1.Remove a node u from the queue.&lt;br&gt;
2.Append u to the topological order result list.&lt;br&gt;
3.For each outgoing edge (u → v):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Decrease the in-degree of v by 1 (since u is processed).&lt;/li&gt;
&lt;li&gt;If v’s in-degree becomes 0, add it to the queue.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Step 4: Check for Cycles&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;Following processing, if there are less nodes in the result list than there are nodes in the graph overall, then:&lt;br&gt;
In-degree = 0 was never reached by some nodes.&lt;br&gt;
This indicates that a cycle (circular dependency) exists.Topological sorting is therefore impossible.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Let’s walk through a dry run to better understand how this algorithm works in practice&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F60trvc3a7maurl5dsurb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F60trvc3a7maurl5dsurb.png" alt=" " width="800" height="575"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let’s say we have 6 tasks (nodes):&lt;br&gt;
0, 1, 2, 3, 4, 5&lt;br&gt;
5 → 2&lt;br&gt;
5 → 0&lt;br&gt;
4 → 0&lt;br&gt;
4 → 1&lt;br&gt;
2 → 3&lt;br&gt;
3 → 1&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1: Compute In-degree of Each Node&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Count incoming edges for each node:&lt;br&gt;
Node 0: in-degree = 2 (from 5, 4)&lt;br&gt;
Node 1: in-degree = 2 (from 4, 3)&lt;br&gt;
Node 2: in-degree = 1 (from 5)&lt;br&gt;
Node 3: in-degree = 1 (from 2)&lt;br&gt;
Node 4: in-degree = 0&lt;br&gt;
Node 5: in-degree = 0&lt;br&gt;
📌 In-degree array: [2, 2, 1, 1, 0, 0]&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2: Initialize Queue with In-degree = 0 nodes&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Nodes 4 and 5 have in-degree = 0 → enqueue them.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9qgo7jjaeejkjx48jl5z.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9qgo7jjaeejkjx48jl5z.jpeg" alt=" " width="800" height="278"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3: Process Queue Iteratively&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Iteration 1

&lt;ul&gt;
&lt;li&gt;Dequeue 4 → add to result.&lt;/li&gt;
&lt;li&gt;For edges from 4:

&lt;ul&gt;
&lt;li&gt;4 → 0: reduce in-degree[0] → 1&lt;/li&gt;
&lt;li&gt;4 → 1: reduce in-degree[1] → 1&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fizwthet964ms36d0jo2z.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fizwthet964ms36d0jo2z.jpeg" alt=" " width="800" height="367"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Iteration 2

&lt;ul&gt;
&lt;li&gt;Dequeue 5 → add to result.&lt;/li&gt;
&lt;li&gt;For edges from 5:&lt;/li&gt;
&lt;li&gt;5 → 2: reduce in-degree[2] → 0 → enqueue 2&lt;/li&gt;
&lt;li&gt;5 → 0: reduce in-degree[0] → 0 → enqueue 0&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbr4o5d3i3tm46ac0j8ee.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbr4o5d3i3tm46ac0j8ee.jpeg" alt=" " width="800" height="318"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Iteration 3

&lt;ul&gt;
&lt;li&gt;Dequeue 2 → add to result.&lt;/li&gt;
&lt;li&gt;For edges from 2:

&lt;ul&gt;
&lt;li&gt;2 → 3: reduce in-degree[3] → 0 → enqueue 3&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkqv1id4jvj5b98xx7m2q.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkqv1id4jvj5b98xx7m2q.jpeg" alt=" " width="800" height="197"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Iteration 4

&lt;ul&gt;
&lt;li&gt;Dequeue 0 → add to result.&lt;/li&gt;
&lt;li&gt;Node 0 has no outgoing edges.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F82ke94rnzk6mcv1i3xfj.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F82ke94rnzk6mcv1i3xfj.jpeg" alt=" " width="800" height="256"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Iteration 5

&lt;ul&gt;
&lt;li&gt;Dequeue 3 → add to result.&lt;/li&gt;
&lt;li&gt;For edges from 3:

&lt;ul&gt;
&lt;li&gt;3 → 1: reduce in-degree[1] → 0 → enqueue 1&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff2m3al9v9q12kbevkg7d.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff2m3al9v9q12kbevkg7d.jpeg" alt=" " width="800" height="204"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Iteration 6

&lt;ul&gt;
&lt;li&gt;Dequeue 1 → add to result.&lt;/li&gt;
&lt;li&gt;Node 1 has no outgoing edges.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fafxyvpr7gz6vxvcom2um.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fafxyvpr7gz6vxvcom2um.jpeg" alt=" " width="800" height="180"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4:Check for Cycles&lt;/strong&gt;&lt;br&gt;
All 6 nodes are processed → no cycle.&lt;br&gt;
✅ Valid topological order found.&lt;br&gt;
[4, 5, 2, 0, 3, 1]&lt;br&gt;
Also note that different valid orders are possible depending on queue processing order.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code snippet&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#include &amp;lt;bits/stdc++.h&amp;gt;
using namespace std;

vector&amp;lt;int&amp;gt; topologicalSortKahn(int V, vector&amp;lt;pair&amp;lt;int,int&amp;gt;&amp;gt; &amp;amp;edges) {
    // Step 1: Build graph and compute in-degree
    vector&amp;lt;vector&amp;lt;int&amp;gt;&amp;gt; graph(V);
    vector&amp;lt;int&amp;gt; in_degree(V, 0);

    for (auto &amp;amp;edge : edges) {
        int u = edge.first, v = edge.second;
        graph[u].push_back(v);
        in_degree[v]++;
    }

    // Step 2: Initialize queue with in-degree = 0 nodes
    queue&amp;lt;int&amp;gt; q;
    for (int i = 0; i &amp;lt; V; i++) {
        if (in_degree[i] == 0)
            q.push(i);
    }

    vector&amp;lt;int&amp;gt; topo_order;

    // Step 3: Process queue
    while (!q.empty()) {
        int node = q.front();
        q.pop();
        topo_order.push_back(node);

        // Reduce in-degree of neighbors
        for (int neighbor : graph[node]) {
            in_degree[neighbor]--;
            if (in_degree[neighbor] == 0)
                q.push(neighbor);
        }
    }

    // Step 4: Check for cycle
    if ((int)topo_order.size() != V) {
        cout &amp;lt;&amp;lt; "Cycle detected! Topological sort not possible." &amp;lt;&amp;lt; endl;
        return {};
    }

    return topo_order;
}

int main() {
    int V = 6;
    vector&amp;lt;pair&amp;lt;int,int&amp;gt;&amp;gt; edges = {
        {5, 2},
        {5, 0},
        {4, 0},
        {4, 1},
        {2, 3},
        {3, 1}
    };

    vector&amp;lt;int&amp;gt; order = topologicalSortKahn(V, edges);

    cout &amp;lt;&amp;lt; "Topological Order: ";
    for (int node : order)
        cout &amp;lt;&amp;lt; node &amp;lt;&amp;lt; " ";
    cout &amp;lt;&amp;lt; endl;

    return 0;
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's analyze the time complexity and space complexity&lt;br&gt;
Each node is enqueued/dequeued exactly once.&lt;br&gt;
Each edge is processed exactly once (when decreasing in-degree).&lt;br&gt;
so time complexity is &lt;strong&gt;Time Complexity: O(V + E)&lt;/strong&gt;&lt;br&gt;
We use  in-degree array and queue.&lt;br&gt;
so &lt;strong&gt;Space Complexity :O(V)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Kahn’s Algorithm (BFS-based) is iterative, so it doesn’t face recursion depth issues that can occur in the DFS approach.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Sorting and Bubble Sort Explained</title>
      <dc:creator>Khushi</dc:creator>
      <pubDate>Sun, 24 Aug 2025 08:55:06 +0000</pubDate>
      <link>https://dev.to/khushi2488/sorting-and-bubble-sort-explained-9cd</link>
      <guid>https://dev.to/khushi2488/sorting-and-bubble-sort-explained-9cd</guid>
      <description>&lt;p&gt;&lt;strong&gt;Sorting: What Is It?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Sorting is the process of placing data according to one or more criteria in a certain order, usually either descending (biggest → smallest) or ascending (smallest → largest).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why Does Sorting Matter in Practical Uses?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In computer science, sorting algorithms are vital because they help with effective data organization.  Sorting is used behind the scenes everywhere, from refining social media feeds to powering search engines.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Faster Searching&lt;/strong&gt;: Binary Search and other effective searches are made possible by sorted data.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Improved User Experience&lt;/strong&gt;: Products are shown in e-commerce apps according to factors like price and rating.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Effective Processing&lt;/strong&gt;: Sorted data is necessary for many algorithms to function better.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Clear Visualization&lt;/strong&gt;: Charts and trends are easier to comprehend when data is sorted.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Real-World Use Cases&lt;/strong&gt;: Healthcare (organized appointments), Banking (organized transactions), etc.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The table below shows the time complexity, stability, and other properties of popular sorting algorithms. It helps to quickly compare which algorithm to use based on the problem requirements.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe7ky5xyc3xncq3nbv9to.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe7ky5xyc3xncq3nbv9to.png" alt=" " width="744" height="390"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Introduction to Bubble Sort&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Bubble Sort is one of the simplest and easiest-to-understand sorting algorithms. It is usually the first algorithm taught to beginners due to its clear logic and step-by-step process.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How it works:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Compare adjacent elements.&lt;/li&gt;
&lt;li&gt;If the current element is greater than the next, swap them.&lt;/li&gt;
&lt;li&gt;After each pass, the largest element “bubbles up” to its correct position at the end.&lt;/li&gt;
&lt;li&gt;Repeat until no swaps are needed (array is sorted).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Bubble Sort Example – Step-by-Step Dry Run&lt;/strong&gt;&lt;br&gt;
👉 &lt;strong&gt;Input Array: [64, 34, 25, 12, 22, 11, 90]&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Pass 1 (i = 0)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fimampw8w8swlzhjhmlbv.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fimampw8w8swlzhjhmlbv.jpeg" alt=" " width="800" height="734"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Largest element 90 is placed at the end.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Pass 2 (i = 1)&lt;/strong&gt;&lt;br&gt;
Now last element (90) is already sorted, so we check till n-2.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6oh65is1l2lpkthe6ywy.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6oh65is1l2lpkthe6ywy.jpeg" alt=" " width="800" height="590"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Second largest element 64 is now in place.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Pass 3 (i = 2)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Check till n-3 (last 2 sorted).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo0k94to6uqzgzd5dwllt.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo0k94to6uqzgzd5dwllt.jpeg" alt=" " width="800" height="491"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Third largest element 34 is fixed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Pass 4 (i = 3)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Check till n-4.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmy4j2c9ll62vtdmewqae.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmy4j2c9ll62vtdmewqae.jpeg" alt=" " width="800" height="584"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;25 is in position.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Pass 5 (i = 4)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Check till n-5.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0rk1fefknq0k4p7ej16y.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0rk1fefknq0k4p7ej16y.jpeg" alt=" " width="800" height="365"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;22 is placed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Pass 6 (i = 5)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Check till n-6.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F79hjsgoc87799mc83hze.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F79hjsgoc87799mc83hze.jpeg" alt=" " width="800" height="181"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Compare 11 &amp;amp; 12 → no swap.&lt;br&gt;
Already sorted → algorithm stops.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Final Sorted Array:&lt;/strong&gt;&lt;br&gt;
[11, 12, 22, 25, 34, 64, 90]&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Bubble Sort in C++&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#include &amp;lt;iostream&amp;gt;
using namespace std;

void bubbleSort(int arr[], int n) {
    for (int i = 0; i &amp;lt; n - 1; i++) {
        bool swapped = false;  // Optimization: check if swap happens
        for (int j = 0; j &amp;lt; n - i - 1; j++) {
            if (arr[j] &amp;gt; arr[j + 1]) {
                swap(arr[j], arr[j + 1]);
                swapped = true;
            }
        }
        // If no swaps in this pass, array is already sorted
        if (!swapped)
            break;
    }
}

void printArray(int arr[], int n) {
    for (int i = 0; i &amp;lt; n; i++)
        cout &amp;lt;&amp;lt; arr[i] &amp;lt;&amp;lt; " ";
    cout &amp;lt;&amp;lt; endl;
}

int main() {
    int arr[] = {64, 34, 25, 12, 22, 11, 90};
    int n = sizeof(arr) / sizeof(arr[0]);

    cout &amp;lt;&amp;lt; "Original array: ";
    printArray(arr, n);

    bubbleSort(arr, n);

    cout &amp;lt;&amp;lt; "Sorted array: ";
    printArray(arr, n);

    return 0;
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Time and Space Complexity&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Best Case (Already Sorted): O(n)&lt;/li&gt;
&lt;li&gt;Worst Case: O(n²)&lt;/li&gt;
&lt;li&gt;Average Case: O(n²)&lt;/li&gt;
&lt;li&gt;Space Complexity: O(1) (in-place)&lt;/li&gt;
&lt;li&gt;Stability: ✅ Yes (does not change relative order of equal elements)&lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
    <item>
      <title>Depth-First Search</title>
      <dc:creator>Khushi</dc:creator>
      <pubDate>Fri, 25 Apr 2025 04:11:22 +0000</pubDate>
      <link>https://dev.to/khushi2488/depth-first-search-g9m</link>
      <guid>https://dev.to/khushi2488/depth-first-search-g9m</guid>
      <description>&lt;p&gt;&lt;strong&gt;🔁Quick BFS Recap&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In Breadth-First Search (BFS), you explore the graph level by level.&lt;/li&gt;
&lt;li&gt;You visit all immediate neighbors first, then move on to their neighbors, and so on.&lt;/li&gt;
&lt;li&gt;Uses a queue (FIFO)&lt;/li&gt;
&lt;li&gt;Best for shortest path in unweighted graphs&lt;/li&gt;
&lt;li&gt;Real-life: friend suggestions, web crawling, peer-to-peer networks&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Now, Let’s Dive into DFS – Depth-First Search&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;No more level-by-level. It’s time to go DEEP!&lt;/li&gt;
&lt;li&gt;In DFS, you pick a path and go as deep as possible, only backtracking when there’s nowhere left to go.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Let's Imagine this&lt;/strong&gt;,&lt;/p&gt;

&lt;p&gt;You enter a maze and just keep walking forward till you hit a dead-end, then backtrack and explore the next path.&lt;/p&gt;

&lt;p&gt;It Uses recursion or a stack and best for the Exploring complete paths, Detecting cycles, Maze solving, Topological sort.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is DFS?&lt;/strong&gt;&lt;br&gt;
Depth-First Search (DFS) is a classic graph traversal algorithm.&lt;br&gt;
Instead of exploring layer by layer like BFS, it dives deep into each branch of a graph or tree before backtracking.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How It Works:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1.Start from a source node&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Choose a node to begin with (say, node 0).&lt;/li&gt;
&lt;li&gt;This will be the root of your traversal.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;2.Use a stack (or recursion) to track the path&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;DFS relies on a stack data structure.&lt;/li&gt;
&lt;li&gt;You can either explicitly use a stack or let recursion handle it for 
you (since function calls internally use a call stack).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;3.Maintain a visited[] array&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;To avoid infinite loops (especially in cyclic graphs), keep track of nodes you’ve already visited.&lt;/li&gt;
&lt;li&gt;This ensures each node is processed only once.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;4.Process nodes deeply before backtracking&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Visit the current node and mark it as visited.&lt;/li&gt;
&lt;li&gt;Then, move to its first unvisited neighbor.&lt;/li&gt;
&lt;li&gt;Continue this process until there are no unvisited neighbors left.&lt;/li&gt;
&lt;li&gt;When stuck, backtrack to the most recent node that still has unvisited neighbors, and repeat.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh0dxcmrt08c112eig66c.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh0dxcmrt08c112eig66c.png" alt=" " width="535" height="366"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;🧠 Graph Structure (Adjacency List based on the image):&lt;br&gt;
From the graph:&lt;br&gt;
1: [2, 3]&lt;br&gt;
2: [1, 4]&lt;br&gt;
3: [1, 5]&lt;br&gt;
4: [2, 5]&lt;br&gt;
5: [3, 4]&lt;/p&gt;

&lt;p&gt;We’ll assume:&lt;br&gt;
It’s an undirected graph.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Start DFS at 1.&lt;/li&gt;
&lt;li&gt;Mark 1 as visited → Visited = [1].&lt;/li&gt;
&lt;li&gt;Explore first neighbor → 2.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcpiqvpm7qbgb7sl8t2gf.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcpiqvpm7qbgb7sl8t2gf.jpeg" alt=" " width="800" height="419"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;At node 2.&lt;/li&gt;
&lt;li&gt;Mark 2 as visited → Visited = [1, 2].&lt;/li&gt;
&lt;li&gt;Explore first neighbor → 4.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxi8nwhutr0zczuhv778w.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxi8nwhutr0zczuhv778w.jpeg" alt=" " width="800" height="437"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;At node 4.&lt;/li&gt;
&lt;li&gt;Mark 4 as visited → Visited = [1, 2, 4].&lt;/li&gt;
&lt;li&gt;Neighbors of 4 = (2, 5).

&lt;ul&gt;
&lt;li&gt;2 already visited → skip.&lt;/li&gt;
&lt;li&gt;Move to neighbor 5.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxoghjcq8wgcawzju0pie.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxoghjcq8wgcawzju0pie.jpeg" alt=" " width="800" height="486"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;At node 5.&lt;/li&gt;
&lt;li&gt;Mark 5 as visited → Visited = [1, 2, 4, 5].&lt;/li&gt;
&lt;li&gt;Neighbors of 5 = (3, 4).

&lt;ul&gt;
&lt;li&gt;4 already visited → skip.&lt;/li&gt;
&lt;li&gt;Next is 3.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F980tbb8kipxcllwlx4ek.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F980tbb8kipxcllwlx4ek.jpeg" alt=" " width="800" height="490"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 5&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;At node 3.&lt;/li&gt;
&lt;li&gt;Mark 3 as visited → Visited = [1, 2, 4, 5, 3].&lt;/li&gt;
&lt;li&gt;Neighbors of 3 = (1, 5). Both already visited → backtrack.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmuus81m2yxablnpn4ixm.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmuus81m2yxablnpn4ixm.jpeg" alt=" " width="800" height="492"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Step-by-Step (Recursive DFS)&lt;br&gt;
Start at 1, mark visited → dfs = [1]&lt;br&gt;
Go to neighbor 2 (first unvisited of 1) → dfs = [1, 2]&lt;br&gt;
Go to neighbor 4 (from 2) → dfs = [1, 2, 4]&lt;br&gt;
Go to neighbor 5 (from 4) → dfs = [1, 2, 4, 5]&lt;br&gt;
Go to neighbor 3 (from 5) → dfs = [1, 2, 4, 5, 3]&lt;br&gt;
All neighbors visited. Backtrack until all paths are done.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;🔄 Final DFS Traversal Order:&lt;/strong&gt;&lt;br&gt;
1 → 2 → 4 → 5 → 3&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;DFS Code in C++&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#include &amp;lt;iostream&amp;gt;
#include &amp;lt;vector&amp;gt;
using namespace std;

// Helper method to perform DFS recursively
void dfsHelper(int node, vector&amp;lt;int&amp;gt; adj[], vector&amp;lt;bool&amp;gt;&amp;amp; visited, vector&amp;lt;int&amp;gt;&amp;amp; dfs) {
    visited[node] = true;
    dfs.push_back(node);

    for (int neighbor : adj[node]) {
        if (!visited[neighbor]) {
            dfsHelper(neighbor, adj, visited, dfs);
        }
    }
}

// DFS function that calls the helper
vector&amp;lt;int&amp;gt; dfsOfGraph(int V, vector&amp;lt;int&amp;gt; adj[]) {
    vector&amp;lt;int&amp;gt; dfs;
    vector&amp;lt;bool&amp;gt; visited(V, false);
    dfsHelper(0, adj, visited, dfs); // starting from node 0
    return dfs;
}

int main() {
    int V = 5;
    vector&amp;lt;int&amp;gt; adj[V];


    // 1-2, 1-3, 2-4, 3-5, 4-5
    adj[0] = {1, 2};    // node 1 -&amp;gt; 2, 3
    adj[1] = {0, 3};    // node 2 -&amp;gt; 1, 4
    adj[2] = {0, 4};    // node 3 -&amp;gt; 1, 5
    adj[3] = {1, 4};    // node 4 -&amp;gt; 2, 5
    adj[4] = {2, 3};    // node 5 -&amp;gt; 3, 4

    vector&amp;lt;int&amp;gt; result = dfsOfGraph(V, adj);

    cout &amp;lt;&amp;lt; "DFS Traversal: ";
    for (int node : result)
        cout &amp;lt;&amp;lt; node + 1 &amp;lt;&amp;lt; " ";  
    cout &amp;lt;&amp;lt; endl;

    return 0;
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;⏱ Time Complexity → O(V + E)&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;V (Vertices):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Every vertex is visited exactly once.&lt;/li&gt;
&lt;li&gt;Visiting a node + marking as visited = O(1) each → overall O(V).&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;E (Edges):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;For every node, DFS explores its adjacency list (all connected edges).&lt;/li&gt;
&lt;li&gt;Across the whole graph, each edge is checked at most twice (once from each endpoint in an undirected graph, once in a directed graph).&lt;/li&gt;
&lt;li&gt;Total edge exploration = O(E).&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;So, the total time = O(V + E).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Space Complexity → O(V)&lt;/strong&gt;&lt;br&gt;
DFS requires memory for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Visited array → To keep track of visited nodes (size = V).&lt;/li&gt;
&lt;li&gt;Recursion stack (or explicit stack) →&lt;/li&gt;
&lt;li&gt;In the worst case (graph shaped like a linked list), recursion depth can go up to V.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So, maximum call stack size = O(V).&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Breadth-First Search (BFS) Algorithm</title>
      <dc:creator>Khushi</dc:creator>
      <pubDate>Mon, 14 Apr 2025 03:46:49 +0000</pubDate>
      <link>https://dev.to/khushi2488/breadth-first-search-bfs-algorithm-3lmh</link>
      <guid>https://dev.to/khushi2488/breadth-first-search-bfs-algorithm-3lmh</guid>
      <description>&lt;p&gt;&lt;strong&gt;Let's Imagine This first&lt;/strong&gt;,&lt;br&gt;
You’re in a maze of rooms. You want to find the shortest way out. So you start from the first room, check all the adjacent rooms, then go to the next layer of rooms, and keep expanding until you reach the exit.&lt;/p&gt;

&lt;p&gt;That’s Breadth-First Search (BFS) – you explore layer by layer, level by level.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj628uqdux0v3nuujnyvz.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj628uqdux0v3nuujnyvz.jpg" alt=" " width="256" height="256"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is BFS?&lt;/strong&gt;&lt;br&gt;
Breadth-First Search (BFS) is one of the most fundamental graph traversal algorithms. Instead of diving deep into a single path, BFS explores nodes level by level, ensuring that all neighbors of a node are visited before moving deeper.&lt;/p&gt;

&lt;p&gt;It’s especially powerful when you need to find the shortest path in an unweighted graph&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How It Works&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;1.Start from a source node&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Choose a node to begin with (for example, node 0).&lt;/li&gt;
&lt;li&gt;This node will be the root of your traversal.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;2.Use a queue (FIFO) to track nodes to visit next&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A queue is the ideal data structure because BFS needs to process nodes    in the same order they are discovered.&lt;/li&gt;
&lt;li&gt;First In → First Out ensures that we finish exploring one "level" of neighbors before moving on to the next.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;3.Keep a visited[] array (or set)&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;To avoid infinite loops (e.g., in cyclic graphs), we maintain a record of which nodes we have already visited.&lt;/li&gt;
&lt;li&gt;This ensures we never process the same node twice.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;4.Process nodes one by one&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Take the front node from the queue.&lt;/li&gt;
&lt;li&gt;Look at all of its neighbors:

&lt;ul&gt;
&lt;li&gt;If a neighbor has not been visited → mark it visited and add it to the queue.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Continue this until the queue becomes empty.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Let's understand With Dry Run of Simple Example (Graph)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjudx3dedo3qofnvcsh7q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjudx3dedo3qofnvcsh7q.png" alt=" " width="389" height="282"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1&lt;/strong&gt;&lt;br&gt;
Visited Node: 1&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F95agz5xx4cvju6gbb0kn.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F95agz5xx4cvju6gbb0kn.jpeg" alt=" " width="800" height="288"&gt;&lt;/a&gt;&lt;br&gt;
We start with node 1, mark it visited, and add its neighbors (2 and 3) to the queue.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2&lt;/strong&gt;&lt;br&gt;
Visited Node: 2 (front of queue is dequeued)&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxvhb699lhfseoz74scdq.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxvhb699lhfseoz74scdq.jpeg" alt=" " width="800" height="307"&gt;&lt;/a&gt;&lt;br&gt;
From node 2, we explore its neighbors. Node 4 is unvisited, so it is added to the queue.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3&lt;/strong&gt;&lt;br&gt;
Visited Node: 3&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx7w62a3uzcasfzx1ksj4.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx7w62a3uzcasfzx1ksj4.jpeg" alt=" " width="800" height="307"&gt;&lt;/a&gt;&lt;br&gt;
From node 3, we explore neighbors. Node 5 is unvisited, so it gets added to the queue.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4&lt;/strong&gt;&lt;br&gt;
Visited Node: 4&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fppy8u78hpu8t03s2xmqd.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fppy8u78hpu8t03s2xmqd.jpeg" alt=" " width="800" height="298"&gt;&lt;/a&gt;&lt;br&gt;
Node 4 has neighbors (2 and 5). Both are already visited/queued, so nothing new is added.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 5&lt;/strong&gt;&lt;br&gt;
Visited Node: 5&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frfoyx0d5g4x1777xb5m1.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frfoyx0d5g4x1777xb5m1.jpeg" alt=" " width="800" height="241"&gt;&lt;/a&gt;&lt;br&gt;
Node 5 has neighbors (3 and 4). Both are already visited, so BFS traversal is complete.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fonfz00f1z87nefhrvtzz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fonfz00f1z87nefhrvtzz.png" alt=" " width="675" height="210"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;✅ Traversal Order:&lt;/strong&gt; 1 → 2 → 3 → 4 → 5&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#include &amp;lt;iostream&amp;gt;
#include &amp;lt;vector&amp;gt;
#include &amp;lt;queue&amp;gt;
using namespace std;

vector&amp;lt;int&amp;gt; bfsOfGraph(int V, vector&amp;lt;int&amp;gt; adj[]) {
    vector&amp;lt;int&amp;gt; bfs;
    vector&amp;lt;bool&amp;gt; visited(V, false);
    queue&amp;lt;int&amp;gt; q;

    // Start BFS from node 0
    q.push(0);
    visited[0] = true;

    while (!q.empty()) {
        int node = q.front();
        q.pop();
        bfs.push_back(node);

        for (int neighbor : adj[node]) {
            if (!visited[neighbor]) {
                visited[neighbor] = true;
                q.push(neighbor);
            }
        }
    }

    return bfs;
}

int main() {
    int V = 5;
    vector&amp;lt;int&amp;gt; adj[V];

    // Undirected Graph Example
    adj[0] = {1, 2};
    adj[1] = {0, 3};
    adj[2] = {0, 4};
    adj[3] = {1};
    adj[4] = {2};

    vector&amp;lt;int&amp;gt; result = bfsOfGraph(V, adj);

    cout &amp;lt;&amp;lt; "BFS Traversal: ";
    for (int node : result)
        cout &amp;lt;&amp;lt; node &amp;lt;&amp;lt; " ";
    cout &amp;lt;&amp;lt; endl;

    return 0;
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;📈 Time &amp;amp; Space Complexity of BFS&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;⏱ Time Complexity → O(V + E)&lt;/strong&gt;&lt;br&gt;
V (Vertices):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Each vertex is enqueued and dequeued at most once.&lt;/li&gt;
&lt;li&gt;Visiting and marking a node takes constant time → O(V).
E (Edges):&lt;/li&gt;
&lt;li&gt;For each vertex, BFS explores its adjacency list (all connected edges).&lt;/li&gt;
&lt;li&gt;Across the entire graph, every edge is considered once (in undirected graphs, twice — but still O(E)).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Therefore, the total work done = O(V) + O(E) = O(V + E).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Space Complexity&lt;/strong&gt; → O(V)&lt;/p&gt;

&lt;p&gt;BFS requires extra memory for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Visited array → To track whether a node has already been explored (size = V).&lt;/li&gt;
&lt;li&gt;Queue → In the worst case, the queue may hold all vertices of a level at once (up to V).&lt;/li&gt;
&lt;li&gt;Together, this means BFS uses memory proportional to the number of vertices → O(V).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Use-Cases of BFS&lt;/strong&gt;&lt;br&gt;
1.Shortest Path in Unweighted Graphs&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;BFS is the go-to algorithm when you need the minimum number of steps to move from one node to another.
Example:&lt;/li&gt;
&lt;li&gt;In a city represented as a graph (intersections = nodes, roads = edges), BFS can find the shortest route if all roads have equal weight (like equal distance or equal travel cost).&lt;/li&gt;
&lt;li&gt;That’s why BFS is widely used in GPS navigation systems (when edge weights are uniform).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;2.Peer-to-Peer (P2P) Networks&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In P2P systems (like BitTorrent), BFS helps in discovering nearby peers efficiently.
Example:&lt;/li&gt;
&lt;li&gt;A node starts with a small list of peers.&lt;/li&gt;
&lt;li&gt;BFS can explore connections level by level to quickly discover all reachable peers in the network.&lt;/li&gt;
&lt;li&gt;This ensures faster file sharing and efficient resource discovery.&lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
    <item>
      <title>🔍 Rabin-Karp Algorithm</title>
      <dc:creator>Khushi</dc:creator>
      <pubDate>Sun, 13 Apr 2025 16:35:59 +0000</pubDate>
      <link>https://dev.to/khushi2488/rabin-karp-algorithm-21ao</link>
      <guid>https://dev.to/khushi2488/rabin-karp-algorithm-21ao</guid>
      <description>&lt;p&gt;Ever tried finding a word in a huge chunk of text and thought,&lt;br&gt;
There has to be a faster way than checking every single character!😩&lt;br&gt;
Enter the Rabin-Karp Algorithm — a smart and efficient way to search for patterns in strings using a clever trick called rolling hash 🪄&lt;br&gt;
Let’s break it down step by step — no jargon, no confusion, just clarity and fun. 🧠✨&lt;br&gt;
🤔 &lt;strong&gt;The Problem&lt;/strong&gt;&lt;br&gt;
You’re given:&lt;br&gt;
&lt;strong&gt;Text&lt;/strong&gt; → "geeksforgeeks"&lt;br&gt;
&lt;strong&gt;Pattern&lt;/strong&gt; → "geek"&lt;br&gt;
And your task is to find all positions where the pattern appears in the text.&lt;br&gt;
✅ &lt;strong&gt;Expected Output&lt;/strong&gt;:&lt;br&gt;
[1, 9] // Using 1-based indexing (because humans count from 1 😅)&lt;/p&gt;

&lt;p&gt;🧠 &lt;strong&gt;The Big Idea (Why Rabin-Karp Is Smart?)&lt;/strong&gt;&lt;br&gt;
Instead of comparing every letter of the pattern with every possible substring of the text, what if we could:&lt;br&gt;
1.Convert the pattern into a number (hash)&lt;br&gt;
2.Convert parts of the text into numbers too&lt;br&gt;
3.Compare those numbers instead of characters&lt;br&gt;
 → Much faster than checking each letter!&lt;br&gt;
     If the hashes match, we double-check the actual characters — just to be sure (because different strings can sometimes have the same hash — that's called a collision).&lt;/p&gt;

&lt;p&gt;🔢 &lt;strong&gt;What's a Hash Anyway?&lt;/strong&gt;&lt;br&gt;
Think of a hash as a string’s fingerprint:&lt;br&gt;
Same strings → same hash&lt;br&gt;
Different strings → usually different hashes&lt;br&gt;
Rabin-Karp uses a &lt;strong&gt;rolling hash&lt;/strong&gt;, which is just a clever way of updating the hash as we move through the text, without recalculating everything from scratch.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;🔄Rolling Hash — The Star of the Show🌟&lt;/strong&gt;&lt;br&gt;
Let’s say we’ve already calculated the hash for "geek" — now we want the hash for the next window: "eeks".&lt;br&gt;
Instead of this:&lt;br&gt;
Recalculate hash("eeks") from scratch 😩&lt;/p&gt;

&lt;p&gt;We do this:&lt;br&gt;
t_new = (d * (t_old - old_char * h) + new_char) % q&lt;br&gt;
Where:&lt;br&gt;
-t_old is the previous window’s hash&lt;br&gt;
-old_char is the character sliding out of the window&lt;br&gt;
-new_char is the character sliding into the window&lt;br&gt;
-d is the number of characters in the alphabet (e.g., 256 for extended -ASCII)&lt;br&gt;
-h = d^(M-1) % q is used to remove the effect of the oldest character&lt;br&gt;
-q is a large prime number used to reduce hash collisions&lt;/p&gt;

&lt;p&gt;We move the window and get a new hash in constant time! 🔥&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;💥Why Rabin-Karp is Awesome&lt;/strong&gt;&lt;br&gt;
✅ Fast search using hashing&lt;br&gt;
✅ Great for multiple pattern searches&lt;br&gt;
✅ Average time complexity: O(N + M)&lt;br&gt;
✅ Used in:&lt;br&gt;
 1.Plagiarism detection 🧑‍🎓&lt;br&gt;
 2.Big text search engines&lt;br&gt;
 3.DNA sequence matching 🧬&lt;br&gt;
 4.Intrusion detection systems 🔐&lt;/p&gt;

&lt;p&gt;🧾 &lt;strong&gt;Dry Run&lt;/strong&gt;: Let’s See It in Action&lt;br&gt;
🔸 Input:&lt;br&gt;
Text: "geeksforgeeks"&lt;br&gt;
Pattern: "geek"&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1&lt;/strong&gt;: Initialize Variables&lt;br&gt;
M = 4     // length of pattern&lt;br&gt;
N = 13    // length of text&lt;br&gt;
d = 256   // characters in the alphabet&lt;br&gt;
q = 101   // prime number for hashing&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2&lt;/strong&gt;: Compute Hash Multiplier&lt;br&gt;
h = pow(d, M-1) % q = (256^3) % 101 = 88&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3&lt;/strong&gt;: Calculate Initial Hashes&lt;br&gt;
p = hash("geek") % q = 27&lt;br&gt;
t = hash("geek") % q = 27&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4&lt;/strong&gt;: Slide the Window and Compare&lt;br&gt;
Iteration 1: "geek" → Match found ✅ at index 1&lt;br&gt;
Iteration 2: "eeks" → Hash ≠ 27 → skip ❌&lt;br&gt;
Iteration 3: "eksf" → Hash ≠ 27 → skip ❌&lt;br&gt;
...&lt;br&gt;
Iteration 9: "geek" → Match found ✅ at index 9&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;🧑‍💻Final Result:&lt;/strong&gt;&lt;br&gt;
[1, 9]&lt;/p&gt;

&lt;p&gt;👨‍💻 &lt;strong&gt;C++ Code: Rabin-Karp Algorithm&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#include &amp;lt;iostream&amp;gt;
#include &amp;lt;vector&amp;gt;
using namespace std;

// Your Rabin-Karp search function
vector&amp;lt;int&amp;gt; search(string pattern, string text) {
    vector&amp;lt;int&amp;gt; result;
    int d = 256;   // Total characters (ASCII)
    int q = 101;   // Prime number to avoid collision
    int M = pattern.length();
    int N = text.length();
    int p = 0;     // hash for pattern
    int t = 0;     // hash for text window
    int h = 1;

    // Step 1: Calculate hash multiplier h = pow(d, M-1) % q
    for (int i = 0; i &amp;lt; M - 1; i++)
        h = (h * d) % q;

    // Step 2: Initial hash values
    for (int i = 0; i &amp;lt; M; i++) {
        p = (d * p + pattern[i]) % q;
        t = (d * t + text[i]) % q;
    }

    // Step 3: Slide pattern over text
    for (int i = 0; i &amp;lt;= N - M; i++) {
        // If hash values match, check characters one by one
        if (p == t) {
            bool match = true;
            for (int j = 0; j &amp;lt; M; j++) {
                if (text[i + j] != pattern[j]) {
                    match = false;
                    break;
                }
            }
            if (match)
                result.push_back(i + 1); // 1-based indexing
        }

        // Step 4: Update hash using rolling hash
        if (i &amp;lt; N - M) {
            t = (d * (t - text[i] * h) + text[i + M]) % q;
            if (t &amp;lt; 0)
                t = t + q; // Make sure hash stays positive
        }
    }

    return result;
}

// Main Function
int main() {
    string text = "ababcabcabababd";
    string pattern = "ababd";

    vector&amp;lt;int&amp;gt; positions = search(pattern, text);

    if (!positions.empty()) {
        cout &amp;lt;&amp;lt; "Pattern found at position(s): ";
        for (int pos : positions)
            cout &amp;lt;&amp;lt; pos &amp;lt;&amp;lt; " ";
        cout &amp;lt;&amp;lt; endl;
    } else {
        cout &amp;lt;&amp;lt; "Pattern not found in the text." &amp;lt;&amp;lt; endl;
    }

    return 0;
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;🧠 &lt;strong&gt;Key Takeaways&lt;/strong&gt;&lt;br&gt;
Rabin-Karp is a hash-based string searching algorithm.&lt;br&gt;
It uses a rolling hash to speed up window comparisons.&lt;br&gt;
Perfect for situations where you need to find multiple patterns efficiently.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>🔍 Let's Find It – Binary Search! 🚀</title>
      <dc:creator>Khushi</dc:creator>
      <pubDate>Sat, 12 Apr 2025 04:05:53 +0000</pubDate>
      <link>https://dev.to/khushi2488/lets-find-it-binary-search-6n0</link>
      <guid>https://dev.to/khushi2488/lets-find-it-binary-search-6n0</guid>
      <description>&lt;p&gt;🧠 &lt;strong&gt;The Magic of Smart Searching&lt;/strong&gt;&lt;br&gt;
    A Tale of Sorted Boxes &amp;amp; Smart Moves&lt;br&gt;
    A Tale of Two Halves: The Smart Snack Box 🍫&lt;br&gt;
    You're back in your room and spot your snack stash – but this time &lt;br&gt;
    it’s super organized.&lt;br&gt;
    All your chocolates are sorted in alphabetical order:&lt;br&gt;
    [5-Star, Dairy Milk, KitKat, Munch, Perk]&lt;br&gt;
    Now, you want to grab your fav – Munch.&lt;br&gt;
    But wait! 🤔 Instead of checking each one (like in linear search), &lt;br&gt;
    you’ve got a smarter plan this time!&lt;/p&gt;

&lt;p&gt;🧠 &lt;strong&gt;What is Binary Search?&lt;/strong&gt;&lt;br&gt;
Binary Search is like being a smart detective in a sorted list.&lt;br&gt;
Rather than checking one-by-one, you go straight to the middle!&lt;br&gt;
You divide and conquer!&lt;br&gt;
     Cut the list in half&lt;br&gt;
 🔍 Check the middle item&lt;br&gt;
 👉 Decide: Go left? Or go right?&lt;br&gt;
Repeat till you find it (or not)!&lt;/p&gt;

&lt;p&gt;🎯 &lt;strong&gt;Let’s Do a Dry Run&lt;/strong&gt;:&lt;br&gt;
Snack box = [5-Star, Dairy Milk, KitKat, Munch, Perk]&lt;br&gt;
You're searching for Munch 🍫&lt;br&gt;
Let’s turn the story into steps:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fiy8wjmy85mcq9tsvzu4q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fiy8wjmy85mcq9tsvzu4q.png" alt=" " width="564" height="163"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;❓ &lt;strong&gt;What if it’s not there?&lt;/strong&gt;&lt;br&gt;
Snack box: [5-Star, Dairy Milk, KitKat, Munch, Perk]&lt;br&gt;
You search for Snickers 🍫 (not in list)&lt;br&gt;
You do the same process:&lt;br&gt;
Middle = KitKat → not Snickers&lt;br&gt;
Move right or left → check → still not there ❌&lt;br&gt;
Eventually, you return -1 = Not Found.&lt;/p&gt;

&lt;p&gt;📌 &lt;strong&gt;Steps to Remember:&lt;/strong&gt;&lt;br&gt;
Check if the array is sorted ✅&lt;br&gt;
Set two pointers → low and high&lt;br&gt;
While low &amp;lt;= high:&lt;br&gt;
Find mid = (low + high)/2&lt;br&gt;
Is it the target? → 🎯 Return index!&lt;br&gt;
If target &amp;lt; mid → Move high = mid - 1&lt;br&gt;
If target &amp;gt; mid → Move low = mid + 1&lt;br&gt;
If not found → return -1 ❌&lt;/p&gt;

&lt;p&gt;🧑‍💻 &lt;strong&gt;Code Time! (C++)&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int binarySearch(vector&amp;lt;string&amp;gt;&amp;amp; arr, string target) {
    int low = 0, high = arr.size() - 1;

    while (low &amp;lt;= high) {
        int mid = low + (high - low) / 2;

        if (arr[mid] == target)
            return mid;
        else if (arr[mid] &amp;lt; target)
            low = mid + 1;
        else
            high = mid - 1;
    }

    return -1; // Not Found
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;💡 &lt;strong&gt;Quick Recap&lt;/strong&gt;:&lt;br&gt;
✅ Binary Search works only on sorted arrays&lt;br&gt;
    Cut the search space in half every time&lt;br&gt;
⚡ Time Complexity: O(log n)&lt;br&gt;
🚀 Way faster than Linear Search!&lt;br&gt;
 Real-World Analogy:&lt;br&gt;
 Imagine finding a name in a phone book 📖&lt;br&gt;
 Do you start from page 1?&lt;br&gt;
 Nope! You flip to the middle 📖 → Then go left or right!&lt;br&gt;
 That’s Binary Search 💡&lt;/p&gt;

&lt;p&gt;💬 &lt;strong&gt;Wrapping Up&lt;/strong&gt;&lt;br&gt;
Binary Search is like Sherlock Holmes — it doesn’t check everything, it thinks, splits, and finds smartly!&lt;br&gt;
So next time you’re searching something in a sorted list —&lt;br&gt;
Don't be slow… go Binary! 🧠&lt;/p&gt;

</description>
    </item>
    <item>
      <title>🔍Linear Search Made Easy – Find Like a Detective!</title>
      <dc:creator>Khushi</dc:creator>
      <pubDate>Fri, 11 Apr 2025 13:09:39 +0000</pubDate>
      <link>https://dev.to/khushi2488/lets-find-it-linear-search-2o42</link>
      <guid>https://dev.to/khushi2488/lets-find-it-linear-search-2o42</guid>
      <description>&lt;p&gt;🍫 A Chocolate Hunt Story&lt;br&gt;
Imagine you're hungry (like really hungry 😋) and craving your favorite chocolate...&lt;br&gt;
You open your snack box, and here’s what you see:[KitKat, Dairy Milk, Munch, Perk, 5-Star]&lt;br&gt;
Now, you don’t know where Dairy Milk is, so what do you do?&lt;br&gt;
👉 Start checking one by one!&lt;br&gt;
KitKat? → ❌ Nope&lt;br&gt;
Dairy Milk? → ✅ Yesss! Found it &lt;/p&gt;

&lt;p&gt;That's exactly what Linear Search does in coding too!&lt;br&gt;
🧠 &lt;strong&gt;What is Linear Search?&lt;/strong&gt;&lt;br&gt;
Linear Search is like going through each item one-by-one until you find the one you want.&lt;br&gt;
No shortcuts. No tricks. Just pure checking — start to end! 🚶‍♀️&lt;/p&gt;

&lt;p&gt;💡&lt;strong&gt;Real Example&lt;/strong&gt; :&lt;br&gt;
Let's say you have:&lt;br&gt;
arr = [10, 25, 30, 45, 50]&lt;br&gt;
target = 30&lt;/p&gt;

&lt;p&gt;We want to find the number 30.&lt;br&gt;
Let’s dry run this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzjm30nyhobkveabuvoqp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzjm30nyhobkveabuvoqp.png" alt=" " width="283" height="146"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;📢 &lt;strong&gt;Result&lt;/strong&gt;: Found at index 2.&lt;/p&gt;

&lt;p&gt;❓ &lt;strong&gt;What if it’s not there?&lt;/strong&gt;&lt;br&gt;
arr = [10, 25, 30, 45, 50]&lt;br&gt;
target = 100&lt;/p&gt;

&lt;p&gt;We check all… and nope! It’s not there &lt;br&gt;
👉 In that case, we return -1 (means "Not Found").&lt;/p&gt;

&lt;p&gt;📌 &lt;strong&gt;Simple Steps to Remember&lt;/strong&gt;: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Start from the first item&lt;/li&gt;
&lt;li&gt;Check: Is this the one?&lt;/li&gt;
&lt;li&gt;If yes → 🎉 return the index!&lt;/li&gt;
&lt;li&gt;If not → move to next&lt;/li&gt;
&lt;li&gt;Repeat until:
You find the item ✔️
Or reach the end ❌ (return -1)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;🧑‍💻&lt;strong&gt;Code Time! (C++)&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#include &amp;lt;iostream&amp;gt;
using namespace std;
int linearSearch(int arr[], int n, int target) {
    for (int i = 0; i &amp;lt; n; i++) {
        if (arr[i] == target) {
            return i; // Found!
        }
    }
    return -1; // Not found
}
int main() {
    int chocoBox[] = {4, 8, 15, 23, 42};
    int size = sizeof(chocoBox) / sizeof(chocoBox[0]);
    int myFavorite = 23;
     int result = linearSearch(chocoBox, size, myFavorite);
     if (result != -1)
       cout &amp;lt;&amp;lt; "Yay! Found your favorite chocolate at position: " &amp;lt;&amp;lt; result;
    else
        cout &amp;lt;&amp;lt; "Oops! Your chocolate isn't in the box!" &amp;lt;&amp;lt; endl;
        return 0;
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;🖥️ &lt;strong&gt;Output&lt;/strong&gt; :Yay! Found your favorite chocolate at position: 3&lt;/p&gt;

&lt;p&gt;💬 &lt;strong&gt;Wrapping Up&lt;/strong&gt;&lt;br&gt;
Linear Search = The simplest way to find something — one step at a time.&lt;br&gt;
 No rocket science. Just like finding your Dairy Milk 🍫 in a snack box!&lt;br&gt;
Want more fun DSA stuff like this daily?&lt;br&gt;
✨ Follow me — I’m Khushi, a fellow learner sharing daily bite-sized DSA logic, patterns &amp;amp; stories 💛&lt;br&gt;
Let’s learn &amp;amp; grow together 🚀&lt;/p&gt;

</description>
      <category>dsa</category>
      <category>linearsearch</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Let’s Solve DSA Together – With Khushi! 🎯</title>
      <dc:creator>Khushi</dc:creator>
      <pubDate>Thu, 10 Apr 2025 17:53:47 +0000</pubDate>
      <link>https://dev.to/khushi2488/lets-solve-dsa-together-with-khushi-3fgk</link>
      <guid>https://dev.to/khushi2488/lets-solve-dsa-together-with-khushi-3fgk</guid>
      <description>&lt;p&gt;Hey awesome minds! 👋&lt;br&gt;
I’m Khushi — a fellow learner on this wild ride called DSA 🚀&lt;br&gt;
And guess what? I’m not a pro (yet 😉), but I believe DSA becomes fun when we solve it together. So I’m starting this series where…&lt;/p&gt;

&lt;p&gt;👉 We crack logic together, like teammates&lt;br&gt;
👉 We grow, one bug at a time 🐞&lt;br&gt;
🧩 Why “DSA with Khushi”?&lt;br&gt;
Because Khushi means joy — and I want to bring joy to problem solving 💛&lt;br&gt;
No boring explanations. Just clear logic, real learning, and a pinch of fun.&lt;br&gt;
Let’s skip the stress and high-fives the success! &lt;br&gt;
💡 What’s Coming?&lt;br&gt;
🔍 1 Pattern a Day (with real problems)&lt;br&gt;
✨ Explained simply (no rocket science!)&lt;br&gt;
🤝 Solve-along style — you + me + logic!&lt;br&gt;
🔄 Practice = Progress = Confidence!&lt;/p&gt;

&lt;p&gt;So if you’re learning DSA too, come join me!&lt;br&gt;
Let’s make this our logic ritual 🧠&lt;br&gt;
Drop a 💥 if you're in — and see you in the next post where we’ll start with our first DSA pattern!&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
