> For the complete documentation index, see [llms.txt](https://phitron.gitbook.io/algorithm/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://phitron.gitbook.io/algorithm/bfs-dfs-problem-solving/_-find-if-path-exists-in-graph-leetcode.md).

# মডিউল ৯\_২ঃ Find if Path Exists in Graph \[Leetcode]

এই প্রব্লেমে আপনাকে একটি গ্রাফের Number of Nodes (n), edges , source and destination দিয়ে দেয়া হবে। আপনার থেকে বের করতে হবে source থেকে destination এ যাওয়া সম্ভব কিনা।&#x20;

Example 1:

<figure><img src="/files/42RP8O5Cqrys132FUEhe" alt=""><figcaption></figcaption></figure>

source = 0 and destination = 2 \
গ্রাফ দেখে বুঝা যাচ্ছে 0 নোড থেকে 2 নোডে যাওয়া সম্ভব, তাই এইটির জন্য true return করতে হবে।&#x20;

Example 2:<br>

<figure><img src="/files/g5JRRNxjHqnvTp7kPGWX" alt=""><figcaption></figcaption></figure>

source = 0 and destination = 5\
গ্রাফ দেখে বুঝা যাচ্ছে 0 নোড থেকে 5 নোডে যাওয়া সম্ভব  নয়, তাই এইটির জন্য false return করতে হবে।&#x20;

## সমাধান&#x20;

```cpp
memset(vis, false, sizeof(vis));
for (int i = 0; i < edges.size(); i++)
{
    int a = edges[i][0];
    int b = edges[i][1];
    v[a].push_back(b);
    v[b].push_back(a);
}
dfs(source);
```

vis array তে false assign করেছি। এরপর Adjacency matrix কে Adjacency list এ convert করেছি। dfs function call করেছি source দিয়ে।&#x20;

```cpp
void dfs(int s)
{
    vis[s] = true;
    for (int child : v[s])
    {
        if (!vis[child])
        {
            dfs(child);
        }
    }
}
```

dfs function এর শুরুতে source কে ভিজিটেড করেছি, এরপর source এর child নোড বের করেছি, যদি child node টি আনভিজিটেড হয় তাহলে সেই child কে dfs function কে recursive কল করেছি। \
এইভাবে source থেকে  dfs চালানো পর , vis array তে চেক করবো destination ভিজিটেড হয়েছে কিনা। যদি destination node ভিজিটেড হয়ে থাকে তাহলে আমরা true return করবো কারণ source থেকে destination এ যাওয়া সম্ভব, অন্যথায় false return করবো।&#x20;

## সম্পূর্ণ কোড

```cpp
#include <bits/stdc++.h>
using namespace std;

class Solution
{
public:
    vector<int> v[200005];
    bool vis[200005];
    void dfs(int s)
    {
        vis[s] = true;
        for (int child : v[s])
        {
            if (!vis[child])
            {
                dfs(child);
            }
        }
    }
    bool validPath(int n, vector<vector<int>> &edges, int source, int d)
    {
        memset(vis, false, sizeof(vis));
        for (int i = 0; i < edges.size(); i++)
        {
            int a = edges[i][0];
            int b = edges[i][1];
            v[a].push_back(b);
            v[b].push_back(a);
        }
        dfs(source);
        return vis[d];
    }
};
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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/bfs-dfs-problem-solving/_-find-if-path-exists-in-graph-leetcode.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.
