Friday, 1 December 2017

Difference between Logistic regression and Neural networks


   
1.     1. Usually people use gradient descent in logistic regression to minimize the cost function and on each iteration better values to fit their data. But in Neural Networks you have a different (but not that different) way, we use back propagation to find the parameters (or weights) to our inputs.

2.     2. Simple logistic regression is not appropriate for large complex system (too many feature) but neural networks are much better for a complex nonlinear hypothesis even when feature space is huge. 

Tuesday, 7 November 2017

Cohent Sadalend Line clipping algorithm(Opengl) implementation using c++


Read Me:
For run this code you must install GLUT in your system
Installation Guide : GLUT Install

Algorithm:Cohen Sutherland Line Clipping

Source code :

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include<windows.h>
#include<iostream>
#include <chrono>
#include <thread>
#include <bitset>

using namespace std;

int wx1,wx2,wy1,wy2,line_no;
struct points
{
    int x_start;
    int y_start;
    int x_end;
    int y_end;
    int visible;
};
points line_arr[100];
void bit_generate(int a)
{
     bitset<4>binary(a);
     cout << binary << endl;
}
int visible_test(int x,int y)
{
    int res=0;
    if(x < wx1)
    {
        res+=1;
    }
    if(x > wx2)
    {
        res+=2;
    }
    if(y < wy1)
    {
        res+=4;
    }
    if(y > wy2)
    {
        res+=8;
    }
    return res;
}
 /***
    Tarequzzaman Khan
    CSE'12
    2012331514
    */

int Round(double x)
{
    return (floor(x+0.5));
}
void dda(double x_start, double y_start, double x_end,double y_end)
{
    double dx=(x_end-x_start);
    double dy=(y_end-y_start);
    double steps;
    float xInc,yInc,x_=x_start,y_=y_start;

    /* Find out whether to increment x or y */
    steps= max(abs(dx),abs(dy));

    xInc=dx/(float)steps;
    yInc=dy/(float)steps;



    /* Plot the first point */
    glBegin(GL_POINTS);
    glVertex2d(x_,y_);
    glEnd();
    glFlush();
    int k;

    /* For every step, find an intermediate vertex */

    for(k=0; k<steps; k++)
    {
        x_+=xInc;
        y_+=yInc;
        /* printf("%0.6lf %0.6lf\n",floor(x), floor(y)); */
        glBegin(GL_POINTS);
        glVertex2d(Round(x_), Round(y_));
        glEnd();
        glFlush();
    }
}
 /***
    Tarequzzaman Khan
    CSE'12
    2012331514
    */
void dda1(double x_start, double y_start, double x_end,double y_end)
{
    double dx=(x_end-x_start);
    double dy=(y_end-y_start);
    double steps;
    float xInc,yInc,x_=x_start,y_=y_start;

    /* Find out whether to increment x or y */
    steps= max(abs(dx),abs(dy));

    xInc=dx/(float)steps;
    yInc=dy/(float)steps;



    /* Plot the first point */

    int k;

    /* For every step, find an intermediate vertex */

    for(k=0; k<steps; k++)
    {

        /* printf("%0.6lf %0.6lf\n",floor(x), floor(y)); */
        int xx = round(x_);
        int yy = round(y_);
        int a = visible_test(xx,yy);
        if(a != 0)
        {
            glBegin(GL_POINTS);
            glVertex2d(xx, yy);
            glEnd();
            glFlush();
        }
        x_+=xInc;
        y_+=yInc;
    }
}
 /***
    Tarequzzaman Khan
    CSE'12
    2012331514
    */
void init()
{
    glClearColor(1.0, 1.0, 1.0,0);
    glColor3f(0.0,0.0,0.0);
    gluOrtho2D(-520,640,-440,480);
}

void input()
{
    printf("Enter the window co-ordinate x_min and y_min\n");
    scanf("%d %d",&wx1,&wy1);
    printf("Enter the window co-ordinate x_max and y_max\n");
    while(true)
    {
        scanf("%d %d",&wx2,&wy2);
        if(wx1>=wx2 || wy1>=wy2)
        {
            printf("Enter valid end points\n");
        }
        else
        {
            break;
        }
    }
 /***
    Tarequzzaman Khan
    CSE'12
    2012331514
    */
    printf("How many line\n");
    scanf("%d",&line_no);
    printf("x1 y1 x2 y2\n");
    for(int i=0; i<line_no; i++)
    {
        scanf("%d %d",&line_arr[i].x_start, &line_arr[i].y_start);

        int a = visible_test(line_arr[i].x_start,line_arr[i].y_start);
         bit_generate(a);
        scanf("%d %d",&line_arr[i].x_end, &line_arr[i].y_end);

        int b = visible_test(line_arr[i].x_end,line_arr[i].y_end);
        bit_generate(b);
        int p = a&b;
        if(a==0 && b==0)
        {
            line_arr[i].visible = 2;///completely inside
        }
        else if(p==0)
        {
            line_arr[i].visible = 1;///clipping candidate
        }
        else
        {
            line_arr[i].visible = 0;///completely outside
        }
    }
}
void line_clip()
{
    glClear(GL_COLOR_BUFFER_BIT);

    dda(wx1,wy1,wx1,wy2);
    dda(wx1,wy2,wx2,wy2);
    dda(wx2,wy2,wx2,wy1);
    dda(wx2,wy1,wx1,wy1);


    for(int i=0; i<line_no; i++)
    {
        if(line_arr[i].visible==0)
        {
            glColor3f(1.0,0.0,0.0);///set red color which is completely outside
            std::this_thread::sleep_for(std::chrono::milliseconds(1000));

            dda(line_arr[i].x_start,line_arr[i].y_start,line_arr[i].x_end,line_arr[i].y_end); /// line drawing Algorithm

        }
        else  if(line_arr[i].visible==1)
        {
            glColor3f(0.0,0.0,1.0);/// set blue color for clipping candidate
            std::this_thread::sleep_for(std::chrono::milliseconds(1000));

            dda(line_arr[i].x_start,line_arr[i].y_start,line_arr[i].x_end,line_arr[i].y_end);

        }
        else
        {
            glColor3f(0.0,1.0,0.0);
            std::this_thread::sleep_for(std::chrono::milliseconds(1000));

            dda(line_arr[i].x_start,line_arr[i].y_start,line_arr[i].x_end,line_arr[i].y_end);

        }
    }
 /***
    Tarequzzaman Khan
    CSE'12
    2012331514
    */

    for(int i=0; i<line_no; i++)
    {
        if(line_arr[i].visible==0)///completely outside line
        {
            glColor3f(1.0,1.0,1.0);///set white color
             std::this_thread::sleep_for(std::chrono::milliseconds(1000));///sleep for 1 sec

            dda(line_arr[i].x_start,line_arr[i].y_start,line_arr[i].x_end,line_arr[i].y_end);

        }
    }

    for(int i=0; i<line_no; i++)
    {
        if(line_arr[i].visible==1)/// clipping candidate
        {
            glColor3f(1.0,1.0,1.0);///set white color
            std::this_thread::sleep_for(std::chrono::milliseconds(1000));
            dda1(line_arr[i].x_start,line_arr[i].y_start,line_arr[i].x_end,line_arr[i].y_end);
        }
    }
   glColor3f(0.0,0.0,0.0);///set default black color

}

int main(int argc,char ** argv)
{
    /***
    Tarequzzaman Khan
    CSE'12
    2012331514
    */
    glutInit(&argc,argv);
    input();
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowPosition(0,0);
    glutInitWindowSize(640,480);
    glutCreateWindow("Cohen-Sutherland Line Clipping");
    init();
    glutDisplayFunc(line_clip);
    glutMainLoop();
     /***
    Tarequzzaman Khan
    CSE'12
    2012331514
    */
}

Saturday, 21 October 2017

UVA 417 - Word Index c++ implementation


Problem Type: map (Java TreeMap)
Problem Link: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=24&problem=358&mosmsg=Submission+received+with+ID+20216008

Source code :

 /***
    Tarequzzaman Khan
    CSE'12
    2012331514
    */
#include<bits/stdc++.h>
using namespace std;
unordered_map<string,int>mp;
void load()
{
    queue<string> q;
    string s = "";
    int cou =1;
    for(int i=97; i<=122; i++)
    {
        char c = i;
        s+=c;
        mp[s]=cou;
        q.push(s);
        cou ++;
        s="";
    }
    while(!q.empty())
    {
        string s = q.front();
        q.pop();
        for(int i=97; i<=122; i++)
        {
            char c = i;
            int len = s.length();
            if(s[len-1]<c)
            {
                string m = s+c;
                if(m.length()>5)
                    break;
                mp[m]=cou;
                q.push(m);
                cou ++;
            }
        }
    }
}
int main()
{
 /***
    Tarequzzaman Khan
    CSE'12
    2012331514
    */
    load();
    string s ;
    while(cin >>s)
    {
     cout << mp[s] << endl;
    }
}

Friday, 20 October 2017

11988 - Broken Keyboard (a.k.a. Beiju Text) c++ implementation

Problem Type: C++ STL list (Java LinkedList)


Implementation:

#include<bits/stdc++.h>
using namespace std;
 /***
    Tarequzzaman Khan
    CSE'12
    2012331514
    */
int main()
{
    string s;
    while(cin >>s)
    {

        int tag =0;
        string sub="",output="";
        for(int i=0; i<s.length(); i++)
        {
            if(s[i]=='[')
            {
                tag =1;
                if(sub.compare("")!=0)
                {
                    output=sub + output;
                    sub ="";
                }
            }
            else if(s[i]==']')
            {
                tag = 0;
                if(sub.compare("")!=0)
                {
                    output=sub + output;
                    sub ="";
                }
            }
            else if(tag==1)
            {
                sub += s[i];
            }
            else
            {
                output += s[i];
            }
        }
        if(sub.compare("")!=0)
        {
         output = sub + output;
        }
        cout << output << endl;
    }
}

Monday, 16 October 2017

11362 - Phone List(C++ implementation)

Problem Type: Trie Data Structure


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

struct Node
{
    map<char, Node*> a;
    int flag;

    Node()
    {
        flag = 0;
    }
};

Node* root;
void insert_text(Node *x, string s)
{
    for(int i = 0; i < s.size(); i++)
    {
        if(x->a.count(s[i]) == 0)
        {
            x->a[ s[i] ] = new Node;
        }
        x = x->a[ s[i] ];
    }
    x->flag++;
}

int search_text(Node* x, string s)
{
    int len = s.length();
    int i=0;
    int flag_count =0;
    while (i<len)
    {
        char c = s[i];
        if(x->a.count(s[i]) != 0)
        {
            x = x->a[s[i]];
            if(x->flag==1)
            {
                flag_count++;
            }
            else if(x->flag>1)
            {
                return 0;
            }
        }
     
        i++;
    }
    if(flag_count>1)
    {
        return 0;
    }
    else
    {
        return 1;
    }
}

int main()
{
    int test;
    scanf("%d",&test);
    for(int i=0; i<test; i++)
    {
        int how_number,k=0;
        string str[10003];
        root = new Node;
        scanf("%d",&how_number);
        int j;
        for(j=0; j<how_number; j++)
        {
            string number;
            cin >> number;
            insert_text(root,number);
            str[j]=number;

        }
        int mm=0;
        for(int k=0; k<j; k++)
        {
            int p = search_text(root,str[k]);
            if(p==0)
            {
                mm=1;
                break;
            }

        }
        if(mm==0)
        {
            printf("YES\n");
        }
        else
        {
            printf("NO\n");
        }
    }

}

Tuesday, 15 August 2017

10878 - Decode the tape C++ implementation

#include <iostream>
#include<stdio.h>
using namespace std;

int main()
{
    string s ;
    getline(cin,s);
    while(getline(cin,s))
    {
        if(s[0]=='-')
            break;
        int m=0;
        int sum=1;
        for(int i=9; i>=1; i--)
        {
            if (s[i]!='.')
            {
                if (s[i]=='o')
                {
                    m += sum;
                }
                sum *= 2;
            }
        }
        printf("%c",m);
    }
}

Thursday, 27 July 2017

Bubble Sort using link list in java

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication2;

/**
 *
 * @author Tarequzzaman
 */

import java.util.Scanner;

public class Link_Sort
{
    node head,current;

    class node
    {
        int data;
        node next;
        node(int data)
        {
            this.data = data;
            next = null;
        }
    }


    void insert(int data)
    {
        node new_node = new node(data);
        if(head==null)
        {
            head = new_node;
            current = new_node;
        }
        else
        {
            current.next = new_node;
            current = current.next;
        }

    }

    void display()
    {
        node tnode = head;
        while (tnode != null)
        {
            System.out.print(tnode.data + " ");
            tnode = tnode.next;
        }
        System.out.println("");
    }


    void sort(int count)
    {
        node current = head;
        for(int i=0; i<count; i++)
        {
            node next_node= current.next;
            for(int j=i; j<count-1; j++)
            {
                if(current.data > next_node.data)
                {
                    int temp = current.data;
                    current.data = next_node.data;
                    next_node.data = temp;
                }
                next_node = next_node.next;
            }
            current= current.next;
        }
    }


    public static void main(String args[])
    {
        int count=0;
        Link_Sort s = new Link_Sort();
        System.out.println("Give input enter 0 to break");
        while (true)
        {
            Scanner sc = new Scanner(System.in);
            int i = sc.nextInt();
            if (i == 0)
            {
                break;
            }
            count++;
            s.insert(i);
        }
        System.out.println("Current Output");
        s.display();
        System.out.println("Sorted Output");
        s.sort(count);
        s.display();
    }

}

Friday, 14 July 2017

Reading data from a binary file (c++) implementation

#include<bits/stdc++.h>
#include <fstream>
#include <vector>

using namespace std;


int main()
{
    vector<unsigned char> bytes;

    ifstream file1("C:\\Users\\Tarequzzaman\\Desktop\\decoded.bin", ios_base::in | ios_base::binary);
    unsigned char ch = file1.get();
    while (file1.good())
    {
        bytes.push_back(ch);
        ch = file1.get();
    }
    size_t size = bytes.size();
    cout  << size << endl;
    return 0;
}

Friday, 5 May 2017

A* search implementation in c++





CODE :
#include<bits/stdc++.h>
#define mx 1000
using namespace std;
map <string,int>mymap;
map<int,string>mymap1;
int N,E,mat[mx][mx],start,end1,par[mx],cost[mx];


struct node
{
    int node_no ;

    int huristic_value;

    bool operator<(const node& target) const
    {
        return huristic_value > target.huristic_value;
    }
};
node arr[mx];



void  Printpath(int V)
{
    if(start==V)
    {
        string s = mymap1[start];
        cout << s;
        return;
    }
    else if(par[V]==0)
    {
        printf("No path exist\n");
    }
    else
    {
        Printpath(par[V]);
        string s = mymap1[V];
        cout <<"-->" << s ;
    }
}

int GBFS(int ux)
{
    node current;
    priority_queue<node>q;
    q.push(arr[ux]);
    par[ux]=0;
    cost[ux] = 0;

    while(!q.empty())
    {
        current = q.top();
        int vx = current.node_no;
        q.pop();
        if(vx == end1)
            return vx;
        for(int i=1; i<=N; i++)
        {


            if(mat[vx][i]!=-1)
            {
                cost[i]= cost[vx] + mat[vx][i];
                arr[i].huristic_value+=cost[i];
                q.push(arr[i]);
                if(par[vx]!=i)
                    par[i] = vx;

            }
        }
    }
}
int main()
{
    string s;
    int hv;
    cout <<"Enter the number of Node : ";
    cin >> N;

    cout << "N H" << endl;///N for node ;H for huristic value
    for(int i=1; i<=N; i++)
    {
        cin >> s >> hv;
        mymap[s] = i;
        mymap1[i] = s;
        arr[i].node_no = i;
        arr[i].huristic_value = hv;
    }

    cout <<"Enter the number of Edge : " ;
    cin >> E ;


    string source, des;
    int g_n;


    for(int i=1; i<=N; i++)
    {
        for(int j=1; j<=N; j++)
        {
            mat[i][j] = -1;                   ///Matrix initialization
        }
    }


    for(int i = 1; i <= E; i++)
    {
        cin >> source >> des >> g_n;
        int x = mymap[source];               ///Input the edges :)
        int y = mymap[des];
        mat[x][y] = g_n;
        mat[y][x] = g_n;
    }


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

    cout << "enter the start and goal node" <<endl;
    cin >> source >> des;
    start = mymap[source];
    end1 = mymap[des];
    int path =   GBFS(start);
    Printpath(end1);
    cout << endl << "Path cost : "<<cost[path] << endl;

}

INPUT : 
8
A 336
T 329
S 253
R 193
F 176
C 160
P 100
B 0
9
A T 118
A S 140
S R 80
S F 99
R C 146
R P 97
F B 211
C P 138
P B 101
A B

OUTPUT :
A-->S-->R-->P-->B
Path cost : 418

Greedy Best First Search(GBST) c++



CODE :

#include<bits/stdc++.h>
#define mx 1000
using namespace std;
map <string,int>mymap;
map<int,string>mymap1;
int N,E,mat[mx][mx],start,end1,par[mx],cost[mx];

struct node
{
    int node_no ;
    int huristic_value;

    bool operator<(const node& target) const
    {
        return huristic_value > target.huristic_value;
    }
};
node arr[mx];

void  Printpath(int V)
{
    if(start==V)
    {
        string s = mymap1[start];
        cout << s;
        return;
    }
    else if(par[V]==0)
    {
        printf("No path exist\n");
    }
    else
    {
        Printpath(par[V]);
        string s = mymap1[V];
        cout <<"-->" << s ;
    }
}

int GBFS(int ux)
{
    node current;
    priority_queue<node>q;
    q.push(arr[ux]);
    par[ux]=0;
    cost[ux] = 0;

    while(!q.empty())
    {
        current = q.top();
        int vx = current.node_no;
        q.pop();
        for(int i=1; i<=N; i++)
        {
            if(mat[vx][i]!=-1)
            {
                cost[i]= cost[vx] + mat[vx][i];
                q.push(arr[i]);
                if(par[vx]!=i)
                    par[i]=vx;
                if(i == end1)
                {
                    return cost[i];
                }
            }
        }
    }
}
int main()
{
    string s;
    int hv;
    cout <<"Enter the number of Node : ";
    cin >> N;

    cout << "N H" << endl;///N for node ;H for huristic value
    for(int i=1; i<=N; i++)
    {
        cin >> s >> hv;
        mymap[s] = i;
        mymap1[i] = s;
        arr[i].node_no = i;
        arr[i].huristic_value = hv;
    }

    cout <<"Enter the number of Edge : " ;
    cin >> E ;

    string source, des;
    int g_n;

    for(int i=1; i<=N; i++)
    {
        for(int j=1; j<=N; j++)
        {
            mat[i][j] = -1;                   ///Matrix initialization
        }
    }

    for(int i = 1; i <= E; i++)
    {
        cin >> source >> des >> g_n;
        int x = mymap[source];               ///Input the edges :)
        int y = mymap[des];
        mat[x][y] = g_n;
        mat[y][x] = g_n;
    }


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

    cout << "enter the start and goal node" <<endl;
    cin >> source >> des;
    start = mymap[source];
    end1 = mymap[des];

    int path=   GBFS(start);
    Printpath(end1);
    cout << endl << "Path cost : "<<path << endl;

}


INPUT : 
8
A 336
T 329
S 253
R 193
F 176
C 160
P 100
B 0
9
A T 118
A S 140
S R 80
S F 99
R C 146
R P 97
F B 211
C P 138
P B 101
A B

OUTPUT :
A-->S-->F-->B
Path cost : 450

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

Tuesday, 2 May 2017

BFS(Breadth-first search) CPP

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 bfs(int ux)
{
    int vx,i;
    color[ux]=1;
    par[ux]=0;
    queue<int>Q;
    Q.push(ux);
    while(!Q.empty())
    {
        vx=Q.front();
        Q.pop();
        for(i=1; i<=node; i++)
        {
            if(mat[vx][i]==1 && color[i]==0)
            {
                color[i]=1;
                Q.push(i);
                par[i]=vx;
            }
        }
    }
}
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;
    }


    bfs(start);
    cout << "PATH" << endl;
    Printpath(end1);
    cout << endl;
}


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: 
PATH
SYLHET-->DHAKA-->RANGPUR



Wednesday, 26 April 2017

date to pick week number c++


Problem type: Real life (calendar Problem)

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int day,date_no,x;
    cout << "Enter any number of day (for example saturday = 1; sunday=2;monday =3 etc) that a month start" << endl;
    cin >>day;
    int next_day =7-day;
    next_day = 1 + next_day;
    cout << "Enter date to check which week :) " << endl;
    while(cin >> x)
    {
        if(x==-1)
        {
           break;
        }

        if(x <= next_day)
        {
            cout <<"1st week" << endl;

        }
        else{
                int cou =0;
                int week_count=1;
                for(int i=next_day+1;; i++)
                {
                    cou++;
                    if(cou==8)
                    {
                        cou=1;
                        week_count++;
                    }
                    if(i==x)
                        break;
                }
               cout << week_count +1 <<" number week" << endl;
        }
    }

}

Saturday, 25 February 2017

Formula Behind log Calculation


1. log(y)  means that
                                    base^(x) = y
 For example :
                         For example, the base 10 logarithm of 1000 is 3, as 10 to the        power 3 is 1000(1000 = 10 × 10 × 10 = 103); 10 is used as a factor three times.





2.  logxy = logY/logX
1+x1x1+x1x

Thursday, 23 February 2017

Reading All ASCII(with Extended 0-255) Character to a Binary file Using C++

#include<bits/stdc++.h>
using namespace std;
int main()
{
    ifstream myfile ("compress.bin", ios::binary);
    for(int i=0; i<256; i++)
    {
        unsigned char c ;
        myfile.seekg(i);//position of the character in the file
        myfile.read ((char*)&c, sizeof (c));
       cout << c;
    }
    cout << endl;
    return 0;

}

Writing All ASCII(with Extended 0-255) Character to a Binary file Using C++


The reason I use here binary file because using binary file we can  both read and write easily




#include<bits/stdc++.h>
using namespace std;
int main ()
{
    string s ="";
    fstream file("compress.bin", ios:: binary | ios::in | ios::out | ios:: trunc);
    for(int i= 0; i<256; i++)
    {
        char  c=i;
        file.write((char *)&c,sizeof(c));

    }
}

Saturday, 4 February 2017

Writing and Reading all ASCII character from a file(Binary) using c++



#include<bits/stdc++.h>
using namespace std;
int main ()
{
    for(int i=0; i<256; i++)
    {
        fstream file("compress.bin", ios:: binary | ios::in | ios::out | ios:: trunc);
        char  c=i;
        file.write((char *)&c,sizeof(c));
        file.seekg(0);
        unsigned char p;
        file.read((char *)&p,sizeof(c));
        cout << p ;
        printf(" %d\n",p);
    }
}

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

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