ডাটা স্ট্রাকচারঃ Directed Graph


Directed Graph আর Undirected Graph এর মধ্যে বিশেষ তফাৎ নেই। শুধুমাত্র পেছনে আসার রাস্তাটা বন্ধ করে দিলেই Undirected Graph টা Directed হয়ে যাবে।

কোডঃ

//Directed graph
//Author: Milon

#include<iostream>
#include<cstdio>
using namespace std;

int main(){
    int n;
    cout<<"Enter the number of nodes: ";
    cin>>n;
    bool list[n][n];

    //Initialize
    for(int i=0;i<n;i++)
        for(int j=0;j<n;j++)
            list[i][j]=false;
    cout<<"Enter an edge(start_node end_node)"<<endl;
    cout<<"Press (0 0) to end:"<<endl;

    //Making graph
    while(true){
        int a,b;
        cin>>a>>b;
        if(a==0 && b==0)
            break;
        if(a>n || b>n || a<=0 || b<=0)
            cout<<"Invalid input"<<endl;
        else{
            list[a-1][b-1]=true;
            //list[b-1][a-1]=true;      //for undirected graph
            }
        }
    cout<<endl;

    //Print adjacency matrix
    cout<<"Adjacency matrix:"<<endl;
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++)
            printf("%4d",list[i][j]);
        cout<<endl;
        }
    return 0;
    }
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s