Home About My Portfolio Privacy Policy Terms

Friday, 10 February 2023

Multi Source BFS

In normal BFS, there is a single starting node (or source) which is pushed inside the queue from where we start traversing other nodes until the queue is not empty. Whereas in multi source BFS, we have multiple starting nodes, all of which are initially pushed into the queue and then traversal is started.

Let us consider an example of normal BFS

Given above graph, we have to find the distance between source node 1 and destination node 4.
Here's the simple BFS solution that works in O(n) time.

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

    int distBetween(vector<int>*adj,int n,int src,int dest){
        queue<pair<int,int>>q;
        int d=0;
        q.push({src,d});
        vector<int>vis(n+1);

        while(!q.empty()){
            src=q.front().first;
            d=q.front().second;
            q.pop();
            if(vis[src])continue;
            vis[src]=1;
            if(src==dest)return d;
            for(auto &v:adj[src])
                q.push({v,d+1});
        }
        return -1;
    }

    int main(){
        system("CLS");

        int n=6;
        vector<int>adj[n+1];
        adj[1]={3,5};
        adj[2]={3,6};
        adj[3]={1,2,4,5,6};
        adj[4]={3,6};
        adj[5]={1,3,6};
        adj[6]={2,3,4,5};

        int src=1, dest=6;
        cout<<distBetween(adj,n,src,dest);

        return 0;
    };



Now, let consider a new problem. Among the nodes 1, 3 and 5, what is the closest distance from node 2?
Naive solution to this problem is to use a BFS for each given source nodes and find distance of them from destination node and return the minimum distance.

Time complexity will be O(number of nodes * BFS time for each node)
In worst case, number of nodes can be n (where n is the number of nodes in the graph), in this case time complexity will be O(n*n).


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

    int distBetween(vector<int>*adj,int n,int src,int dest){
        queue<pair<int,int>>q;
        int d=0;
        q.push({src,d});
        vector<int>vis(n+1);

        while(!q.empty()){
            src=q.front().first;
            d=q.front().second;
            q.pop();
            if(vis[src])continue;
            vis[src]=1;
            if(src==dest)
                return d;
            for(auto &v:adj[src])
                q.push({v,d+1});
        }
        return -1;
    }

    int main(){
        system("CLS");

        int n=6;
        vector<int>adj[n+1];
        adj[1]={3,5};
        adj[2]={3,6};
        adj[3]={1,2,4,5,6};
        adj[4]={3,6};
        adj[5]={1,3,6};
        adj[6]={2,3,4,5};

        int src1=1,src2=3,src3=5, dest=6;
        cout<<min({distBetween(adj,n,src1,dest),
                   distBetween(adj,n,src2,dest),
                   distBetween(adj,n,src3,dest)});
        return 0;
    };



Efficient solution: Using multi source BFS.
It is similar to normal BFS, but unlike normal BFS, we push all the source nodes to the queue before starting the traversal.

Time complexity: O(n)


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

    int distBetween(vector<int>*adj,int n,vector<int>&src,int dest){
       
        queue<pair<int,int>>q;
        int d=0;

        for(auto &s:src)
            q.push({s,d});

        vector<int>vis(n+1);
        while(!q.empty()){
            int s=q.front().first;
            d=q.front().second;
            q.pop();
            if(vis[s])continue;
            vis[s]=1;
            if(s==dest)
                return d;
            for(auto &v:adj[s])
                q.push({v,d+1});
        }
        return -1;
    }

    int main(){
        system("CLS");

        int n=6;
        vector<int>adj[n+1];
        adj[1]={3,5};
        adj[2]={3,6};
        adj[3]={1,2,4,5,6};
        adj[4]={3,6};
        adj[5]={1,3,6};
        adj[6]={2,3,4,5};

        int dest=6;
        vector<int>src={1,2,3};
        cout<<distBetween(adj,n,src,dest);
        return 0;
    };





Related problems



Saturday, 3 December 2022

liveType

 liveType


What is liveType?

liveType is a web application used for real-time typing competition between two players. Users can join room from any corner of the globe and compete as well as chat in real-time.

Why liveType?

Many people use to test their typing speed on various wonderful platforms like monkeytype.com, typerush.com etc. But what they miss is real-time rivalry between their friends. Many studies have already proven that motivation to do a task comes when there is a win-reward system, so is the case with liveType, you feel overwhelmed beating your friend in a close compitition.

Project Inception

The idea to build the project came to my mind when I was quite bored with binged typing on monkeytype.com. The first commit was made on 27 th of November 2022 and it took three days to build the first substantial model.

Vulnerabilities to be probed

Although socket.io uses long polling for maintaining constant communication between server and the client, it has been observed that connection may broke on delayed inactivity from client's end. Thus it is advised to refresh the window when this happens.




Friday, 18 November 2022

beSocial : my own social media!

beSocial




I am extremely happy to share with you that I have made my own social media called "beSocial". It is a full stack project which is completely built with Nodejs, Expressjs, Mongodb and some node libraries. It took me almost four days to complete it. Since Heroku has already withdraw its free tier services, I have moved to Render.

 
I have tried to make development environment as comprehensive as possible. The file structure looks like this:



Nodejs libraries that has been incorporated to build this app
  • Expressjs - for creating various GET and POST APIs. 
  • EJS - for injecting dynamic content inside HTML.
  • JWT - for user authentication and setting up cookies.
  • Mongoose - for handling mongodb database.
  • Multer - for handling image uploads.
  • Sharp - for resizing images.
  • cookie-parser - for parsing cookies
  • nodemon - for auto-refreshing web page while development.
  • moment - for handling time related stuffs.
My profile (srudra754) on beSocial:



homepage:



Createpost page:



Hope you enjoyed this post, and I wish you to follow me on beSocial! 
My username is "srudra754". 

Tuesday, 30 August 2022

toastKing.js : A new JavaScript library!





toastKing.js is a light-weight JavaScript library for creating non-blocking, beautiful notification messages. 
I am very fond of beautiful notification or toast messages. They are often used in web applications. I searched the internet for such libraries and found some, but the problem was that they are very mediocre in looks or a kind of boring. Then I decided to make a library for notification cum toast messages that will be very beautiful in terms of graphics and will have some interactive features. Before developing the library, I made a layout of it, which looks like this:



After completing the design, I started the coding part. All the CSS and HTML is dynamic. User need not to including any additional CSS or HTML in his program.
For docs, please visit the github page of toastKing : https://rudraworks.github.io/toastKing/

Monday, 8 August 2022

Optimal String Search Using Genetic Algorithm


Optimal String Search Using Genetic Algorithm

About Genetic Algorithms (GA)

In Computer Science and Operations Research, Genetic Algorithms are often used for find the optimal solution of a optimization problem, more formally, GAs are heuristic algorithms. 
GAs are based on Darwin's theory of natural selection. i.e. survival of the fittest.

Heuristic Algorithms: In mathematical optimization and computer science, heuristic is a technique designed for solving a problem more quickly when classic methods are too slow or for finding an approximate solution when classic methods fail to find any exact solution.


Steps followed in designing a genetic algorithm 




What I have made?

I have made a simulation of GA in which the algorithm goes on computing until it founds the optimal string.