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

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

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

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

<figure><img src="https://1548341763-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FjliRFwU9cGQFGljHYgOZ%2Fuploads%2FVkVXalwqcfUW9xJfPzyd%2Fimage.png?alt=media&#x26;token=08867487-ccd8-4fdc-b836-a9ee127d03dc" alt="" width="563"><figcaption></figcaption></figure>

***

-> source=0  নিয়ে  dfs function কে কল করা হল।  0 নোডকে ভিজিটেড করা হল এবং Pathvisit এ 0 নোডকে true করা হল। <br>

<figure><img src="https://1548341763-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FjliRFwU9cGQFGljHYgOZ%2Fuploads%2FlV4BqQ9WQZK24SKdLNPu%2Fimage.png?alt=media&#x26;token=650215fb-5d8a-46d9-8f48-f1968aa6d222" alt="" width="563"><figcaption></figcaption></figure>

0 নোড থেকে 1 এবং 4 নোডে যাওয়া যায়। \
এরপর আমরা চেক করবো 1 নোড কি pathvisit এ true কিনা। যদি true হয় তাহলে আমরা বলতে পারবো গ্রাফটিতে cycle আছে।  যেহেতু 1 নোডের Pathvisit true নয়, তাই বলা যায় এখন পর্যন্ত গ্রাফে আমরা cycle পায় নি।\
1 নোড আনভিজিটেড তাই সেইটি দিয়ে dfs function কে recursive কল করা হল।&#x20;

***

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

<figure><img src="https://1548341763-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FjliRFwU9cGQFGljHYgOZ%2Fuploads%2FGcO5DZs7TrMFwN70yhFf%2Fimage.png?alt=media&#x26;token=1e81febe-954e-4741-a7cc-872501cc72bb" alt="" width="563"><figcaption></figcaption></figure>

1 নোড থেকে 2 নোডে যাওয়া যায়। \
এরপর আমরা চেক করবো 2 নোড কি pathvisit এ true কিনা। যদি true হয় তাহলে আমরা বলতে পারবো গ্রাফটিতে cycle আছে।  যেহেতু 2 নোডের Pathvisit true নয়, তাই বলা যায় এখন পর্যন্ত গ্রাফে আমরা cycle পায় নি।\
2 নোড আনভিজিটেড তাই সেইটি দিয়ে dfs function কে recursive কল করা হল।&#x20;

***

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

<figure><img src="https://1548341763-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FjliRFwU9cGQFGljHYgOZ%2Fuploads%2FTGkRKxDbb2s43wOobHgX%2Fimage.png?alt=media&#x26;token=283ec46b-751d-4840-a0de-b1de1998e93e" alt="" width="563"><figcaption></figcaption></figure>

2 নোড থেকে আর কোননোডে যাওয়া যায় নাহ। এখন আমরা ব্যাকট্র্যাক করবো। <br>

<figure><img src="https://1548341763-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FjliRFwU9cGQFGljHYgOZ%2Fuploads%2FxVEFIRFr9mBO76ixVNfh%2Fimage.png?alt=media&#x26;token=018c4bdf-dc70-45c5-96cf-97a7bfa0d6b6" alt="" width="563"><figcaption></figcaption></figure>

ব্যাক করা ফলে Pathvisit এ 2 নোডকে আবার false করে দেয়া হল। \
এখন 1 নোড থেকে আর কোন আনভিজিটেড নোডে যাওয়া যাচ্ছে তাই আবার আমরা ব্যাকট্র্যাক করবো। <br>

<figure><img src="https://1548341763-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FjliRFwU9cGQFGljHYgOZ%2Fuploads%2FYs70jwoDXkZULjcCyRCv%2Fimage.png?alt=media&#x26;token=dfc98b9b-fe03-4591-ab25-a3c617ed5216" alt="" width="563"><figcaption></figcaption></figure>

ব্যাক করা ফলে Pathvisit এ 1 নোডকে আবার false করে দেয়া হল। \
এখন 1 নোড থেকে 4 নোডে যাওয়া যায়। \
এরপর আমরা চেক করবো 4 নোড কি pathvisit এ true কিনা। যদি true হয় তাহলে আমরা বলতে পারবো গ্রাফটিতে cycle আছে।  যেহেতু 4 নোডের Pathvisit true নয়, তাই বলা যায় এখন পর্যন্ত গ্রাফে আমরা cycle পায় নি।\
4 নোড আনভিজিটেড তাই সেইটি দিয়ে dfs function কে recursive কল করা হল।&#x20;

***

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

<figure><img src="https://1548341763-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FjliRFwU9cGQFGljHYgOZ%2Fuploads%2FI8EDiPFYZVIVs7SSwpuh%2Fimage.png?alt=media&#x26;token=464e8c00-fa31-42a1-9c84-29dd539f1d39" alt="" width="563"><figcaption></figcaption></figure>

4 নোড থেকে 3 নোডে যাওয়া যায়। \
এরপর আমরা চেক করবো 3 নোড কি pathvisit এ true কিনা। যদি true হয় তাহলে আমরা বলতে পারবো গ্রাফটিতে cycle আছে।  যেহেতু 3 নোডের Pathvisit true নয়, তাই বলা যায় এখন পর্যন্ত গ্রাফে আমরা cycle পায় নি।\
3 নোড আনভিজিটেড তাই সেইটি দিয়ে dfs function কে recursive কল করা হল।&#x20;

***

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

<figure><img src="https://1548341763-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FjliRFwU9cGQFGljHYgOZ%2Fuploads%2FHmxuIHH5wiHRjDvDt6WG%2Fimage.png?alt=media&#x26;token=86e895d5-d487-4834-9adb-7802924e1738" alt="" width="563"><figcaption></figcaption></figure>

3 নোড থেকে 0 নোডে যাওয়া যায়। \
এরপর আমরা চেক করবো 0 নোড কি pathvisit এ true কিনা। যদি true হয় তাহলে আমরা বলতে পারবো গ্রাফটিতে cycle আছে।  3 নোডের Pathvisit true, তাই বলা যায় এই গ্রাফে cycle আছে।<br>

<figure><img src="https://1548341763-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FjliRFwU9cGQFGljHYgOZ%2Fuploads%2FjoI7tyjiiZy8mf02WZCg%2Fimage.png?alt=media&#x26;token=a8506b71-5596-4c02-aef2-fb1480795e81" alt="" width="563"><figcaption></figcaption></figure>

এইভাবে আমরা DFS দিয়ে Directed গ্রাফে cycle detection করতে পারি।&#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-directed.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.
