# মডিউল ৫\_১ঃ Cycle Detection Undirected গ্রাফে

## BFS দিয়ে Undirected গ্রাফে cycle detection

গ্রাফের ইনপুট \
**4 4**\ <mark style="color:blue;">0 1</mark>\ <mark style="color:blue;">1 2</mark>\ <mark style="color:blue;">2 3</mark>\ <mark style="color:blue;">3 1</mark>

নিচে ইনপুটটি অনুসারে গ্রাফটি আঁকা হল<br>

<figure><img src="/files/chrDAAYxBTxC15kfPTyi" alt="" width="563"><figcaption></figcaption></figure>

***

-> source = 0 ধরে BFS শুরু করছি।  0 নোডকে queue তে পুশ করা হয়েছে BFS চালানোর জন্য।  0 নোডকে\
ভিজিটেড করা হল। \
**তাহলে এখন queue তে ( 0 )  আছে।**&#x20;

<figure><img src="/files/sd10WkpB9MEkOPWSBDYy" alt="" width="563"><figcaption></figcaption></figure>

লুপের প্রবেশ করা হল এবং Queue থেকে  0 নোডকে বের করা হল ।

0 নোডের সাথে connected কিন্তু ভিজিটেড এবং  Parent\[0] != child কিনা চেক করা হল, যদি এমনটি পাওয়া যেত তাহলে আমরা বলে ফেলতে পারতাম গ্রাফের মধ্যে cycle আছে।&#x20;

&#x20;এরপর 0 নোডের সাথে connected এবং এখনো আনভিজিটেড এমন নোড 1 এবং  3 নোডকে ভিজিটেড করা হল। 1 এবং 3 কে queue তে পুশ করা হল এবং তাদের parent 0 করা হল।&#x20;

<figure><img src="/files/ZO4vPVAltIT9TdXCPBTw" alt="" width="443"><figcaption></figcaption></figure>

**তাহলে এখন queue তে ( 3, 1 )  আছে।**&#x20;

***

-> Queue যেহেতুল empty হয় নি, তাই লুপটি আবার চলবে। Queue থেকে  1 নোডকে বের করা হল ।\
**তাহলে এখন queue তে ( 3 )  আছে।**&#x20;

<figure><img src="/files/bAMzMX025i32bArXlENE" alt="" width="563"><figcaption></figcaption></figure>

1 সাথে connected  এবং ভিজিটেড নোড 0 আছে  কিন্তু Parent\[1] != 0 (child) এই conditions টি পরিপূর্ণ না হওয়াই আমরা বলতে পারি না যে এই গ্রাফটিতে cycle আছে।&#x20;

এরপর 1 নোডের সাথে connected এবং এখনো আনভিজিটেড এমন নোড 2  কে ভিজিটেড করা হল। 2 নোডকে queue তে পুশ করা হল এবং 2 নোডের parent 1 করা হল। \
&#x20;<br>

<figure><img src="/files/9K6FSo3hhANGDTGqX8dX" alt="" width="436"><figcaption></figcaption></figure>

**তাহলে এখন queue তে ( 2, 3 )  আছে।**&#x20;

***

-> Queue যেহেতু  empty হয় নি, তাই লুপটি আবার চলবে। Queue থেকে  3 নোডকে বের করা হল । \
**তাহলে এখন queue তে ( 2 )  আছে।**&#x20;

<figure><img src="/files/9XspxCj1VzBsQ14r3T6N" alt="" width="563"><figcaption></figcaption></figure>

3 সাথে connected  এবং ভিজিটেড নোড 0 , 2 আছে ,

* &#x20;0 নোড  Parent\[3] != 0 (child) এই conditions টি পরিপূর্ণ না হওয়াই আমরা বলতে পারি না যে এই গ্রাফটিতে cycle আছে।&#x20;
* **কিন্তু 2 নোড  Parent\[3] != 2 (child) এই conditions টি পরিপূর্ণ করাই আমরা বলতে পারি যে এই গ্রাফটিতে cycle আছে।**&#x20;

এরপর 3 নোডের সাথে connected এবং এখনো আনভিজিটেড এমন কোন নোড পাওয়া যায় নি।&#x20;

এইভাবে আমরা BFS দিয়ে undirected গ্রাফে cycle detect করে ফেললাম।

## DFS দিয়ে Undirected গ্রাফে cycle detection

গ্রাফের ইনপুট \
**4 4**\ <mark style="color:blue;">0 1</mark>\ <mark style="color:blue;">1 2</mark>\ <mark style="color:blue;">2 3</mark>\ <mark style="color:blue;">3 1</mark>

নিচে ইনপুটটি অনুসারে গ্রাফটি আঁকা হল

<figure><img src="/files/C2vq534jHrSP6zfH0Abv" alt="" width="563"><figcaption></figcaption></figure>

***

-> source = 0 ধরে DFS শুরু করছি।  0 নোডকে ভিজিটেড করা হল। \
কিন্তু 2 নোড Parent\[3] != 0 (child) এই conditions টি পরিপূর্ণ করাই আমরা বলতে পারি যে এই গ্রাফটিতে cycle আছে।

<figure><img src="/files/mwaAkd6T9tdcUfGNQqo4" alt="" width="563"><figcaption></figcaption></figure>

0 নোডের সাথে connected আছে 1 এবং 2 নোড।&#x20;

0 নোডের সাথে connected কিন্তু ভিজিটেড এবং  Parent\[0] != child কিনা চেক করা হল, কিন্তু এমন নোড পাওয়ার যায় নি। যদি এমনটি পাওয়া যেত তাহলে আমরা বলে ফেলতে পারতাম গ্রাফের মধ্যে cycle আছে।&#x20;

0 নোডের সাথে connected এবং এখনো আনভিজিটেড এমন নোড হল 1 এবং 2। এখন 0 নোডকে 1 নোডের parent করা হল এবং DFS function কে recursive করা হল source = 1 ধরে।&#x20;

<figure><img src="/files/XiPJ6DfnBVBXvIg55pHl" alt="" width="550"><figcaption></figcaption></figure>

***

-> এখন source = 1  হল এবং 1 নোডকে ভিজিটেড করা হল।&#x20;

<figure><img src="/files/9Q59dqTaWupvkv4e0yob" alt="" width="563"><figcaption></figcaption></figure>

1 নোডের সাথে connected আছে 0 এবং 2 নোড।&#x20;

1 সাথে connected  এবং ভিজিটেড নোড 0 আছে  কিন্তু Parent\[1] != 0 (child) এই conditions টি পরিপূর্ণ না হওয়াই আমরা বলতে পারি না যে এই গ্রাফটিতে cycle আছে।&#x20;

1 নোডের সাথে connected এবং এখনো আনভিজিটেড এমন নোড হল  2। এখন 1 নোডকে 2 নোডের parent করা হল এবং DFS function কে recursive করা হল source = 2 ধরে। \ <br>

<figure><img src="/files/zA7VwuRSsuXAyo7CARz4" alt="" width="563"><figcaption></figcaption></figure>

***

-> এখন source = 2  হল এবং 2 নোডকে ভিজিটেড করা হল। <br>

<figure><img src="/files/zSFiSrUsg2gJoHYaqqQn" alt="" width="563"><figcaption></figcaption></figure>

2 নোডের সাথে connected আছে 1 এবং 3 নোড।&#x20;

2 সাথে connected  এবং ভিজিটেড নোড 1 আছে  কিন্তু Parent\[2] != 1 (child) এই conditions টি পরিপূর্ণ না হওয়াই আমরা বলতে পারি না যে এই গ্রাফটিতে cycle আছে।&#x20;

2 নোডের সাথে connected এবং এখনো আনভিজিটেড এমন নোড হল  3। এখন 2 নোডকে 3 নোডের parent করা হল এবং DFS function কে recursive করা হল source = 3 ধরে। <br>

<figure><img src="/files/TRYzDkIKtnCRSQLi7uXH" alt="" width="563"><figcaption></figcaption></figure>

***

-> এখন source = 3  হল এবং 3 নোডকে ভিজিটেড করা হল।&#x20;

<figure><img src="/files/yzljIeUuhgpCPJavHVsw" alt="" width="563"><figcaption></figcaption></figure>

3 নোডের সাথে connected আছে 2 এবং 0 নোড।&#x20;

3 সাথে connected  এবং ভিজিটেড নোড 2 এবং 0,

* 2 নোড  Parent\[3] != 2 (child) এই conditions টি পরিপূর্ণ না হওয়াই আমরা বলতে পারি না যে এই গ্রাফটিতে cycle আছে।&#x20;
* **কিন্তু 0 নোড  Parent\[3] != 2 (child) এই conditions টি পরিপূর্ণ করাই আমরা বলতে পারি যে এই গ্রাফটিতে cycle আছে।**&#x20;

এইভাবে আমরা DFS দিয়ে undirected গ্রাফে cycle detect করে ফেললাম।&#x20;


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://phitron.gitbook.io/algorithm/cycle-detection/_-cycle-detection-undirected.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
