Wednesday, 3 May 2017

DFS(Depth-first search) to generate All possible Path in c++



CODE :

#include<bits/stdc++.h>
#define MAX 1000
using namespace std;
map<string,int >mymap;
map<int ,string>mymap1;
int node, edge,color[MAX],par[MAX],mat[MAX][MAX],start,end1;
void  Printpath(int V)
{
    if(start==V)
    {
        string s = mymap1[start];
        cout << s;
    }
    else if(par[V]==0)
    {
        printf("No path exist\n");
    }
    else
    {
        Printpath(par[V]);
        string s = mymap1[V];
        cout <<"-->" << s ;
    }
}

void dfs(int ux)
{
    color[ux]=1;
    if(ux==end1)
    {
        Printpath(ux);
        cout << endl;
        return;
    }
   for(int i=1; i<=node; i++)
    {
        if(mat[ux][i]==1)
        {
            if(color[i]==0)
            {
                par[i]=ux;
                dfs(i);
                color[i]=0;
            }
        }
    }
}
int main()
{
    int n=1;
    string source,des,s,e;
    cin >> node >> edge;
    getchar();
    for(int i=0; i<edge; i++)
    {
        cin >>source >>des;
        if(mymap.find(source)==mymap.end())
        {
            mymap[source]=n;
            mymap1[n]=source;
            n=n+1;
        }
        if(mymap.find(des)==mymap.end())
        {
            mymap[des]=n;
            mymap1[n]=des;
            n=n+1;
        }

        int  x =mymap[source];
        int  y =mymap[des];
        mat[x][y]=1;
        mat[y][x]=1;
    }
cout<<"Enter a Start and End" << endl;
    cin >> s >> e;
    start = mymap[s];
    end1 = mymap[e];


    for(int i=1; i<=node; i++)
    {
        color[i]=0;
        par[i]=0;
    }

  cout <<"ALL POSSIBLE PATH : " << endl;
    dfs(start);


}


INPUT : 
7 11
SYLHET DHAKA
SYLHET CHITTAGONG
CHITTAGONG BARISAL
CHITTAGONG DHAKA
DHAKA RAJSHAHI
DHAKA KHULNA
KHULNA RAJSHAHI
DHAKA RANGPUR
RAJSHAHI RANGPUR
KHULNA BARISAL
DHAKA BARISAL
SYLHET RANGPUR

OUTPUT : 

ALL POSSIBLE PATH :
SYLHET-->DHAKA-->CHITTAGONG-->BARISAL-->KHULNA-->RAJSHAHI-->RANGPUR
SYLHET-->DHAKA-->BARISAL-->KHULNA-->RAJSHAHI-->RANGPUR
SYLHET-->DHAKA-->RAJSHAHI-->RANGPUR
SYLHET-->DHAKA-->KHULNA-->RAJSHAHI-->RANGPUR
SYLHET-->DHAKA-->RANGPUR
SYLHET-->CHITTAGONG-->DHAKA-->BARISAL-->KHULNA-->RAJSHAHI-->RANGPUR
SYLHET-->CHITTAGONG-->DHAKA-->RAJSHAHI-->RANGPUR
SYLHET-->CHITTAGONG-->DHAKA-->KHULNA-->RAJSHAHI-->RANGPUR
SYLHET-->CHITTAGONG-->DHAKA-->RANGPUR
SYLHET-->CHITTAGONG-->BARISAL-->DHAKA-->RAJSHAHI-->RANGPUR
SYLHET-->CHITTAGONG-->BARISAL-->DHAKA-->KHULNA-->RAJSHAHI-->RANGPUR
SYLHET-->CHITTAGONG-->BARISAL-->DHAKA-->RANGPUR
SYLHET-->CHITTAGONG-->BARISAL-->KHULNA-->DHAKA-->RAJSHAHI-->RANGPUR
SYLHET-->CHITTAGONG-->BARISAL-->KHULNA-->DHAKA-->RANGPUR
SYLHET-->CHITTAGONG-->BARISAL-->KHULNA-->RAJSHAHI-->DHAKA-->RANGPUR
SYLHET-->CHITTAGONG-->BARISAL-->KHULNA-->RAJSHAHI-->RANGPUR

No comments:

Post a Comment

ট্রিগার এর মাধ্যমে ডাটা ইনসার্ট - insert data using Database Trigger (Mysql)

সর্বপ্রথম আমরা প্রবলেমটা বুঝিঃ আমি একটা টেবিলের একটা কলামের ভ্যালুর উপর ডিপেন্ড করে আরেকটা কলামে ডাটা insert করব । এই কাজটা ট্রি...