简体   繁体   中英

To check if a graph is connected using BFS and print MST

I have stored a graph with nodes 1,2,3,4,... in an adjacency list. I wrote this code to do Breadth First Search (BFS). The BFS works perfect but I don't know what the procedure is to find out if the graph is connected or disconnected and then print the Minimum Spanning Tree of the graph IF it is connected? An example of the graph which I stored in adjacency list:

 1 3 4
 2 4
 3 1 4
 4 2 1 3 

And the code for BFS:

 int visit[999] = { 0 };
 Q.enqueue(0);
 while (!Q.isEmpty()) 
    {
            y = Q.dequeue();

            traverse = g[y];

            while (traverse->link != 0)
            {

                if (visit[traverse->data-1] == 0)
                {

                    visit[traverse->data-1] = 1;
                    parent = traverse->data;
                    Q.enqueue(traverse->data-1);
                }
                traverse = traverse->link;


            }   

            if (visit[traverse->data - 1] == 0)
            {

                visit[traverse->data - 1] = 1;
                parent = traverse->data;
                Q.enqueue(traverse->data - 1);
            }

    }

So I figured it out and put it as a reference for others :)

    int parent = 1;
    gcounter++;
    p = 0;
    int visit[999] = { 0 };
    int c[999] = { 0 };
    int k = 0;
    int z = 0;
    Queue Q;
    Q.enqueue(0);
    while (!Q.isEmpty()) 
    {
            y = Q.dequeue();
            traverse = g[y];

            while (traverse->link != 0)
            {

                if (visit[traverse->data-1] == 0)
                {

                    if (y + 1 != traverse->data)
                    {
                        c[z] = y + 1;z++;
                        c[z] = traverse->data; z++;

                    }
                    p++;
                    visit[traverse->data-1] = 1;
                    parent = traverse->data;
                    Q.enqueue(traverse->data-1);
                }
                traverse = traverse->link;


            }   

            if (visit[traverse->data - 1] == 0)
            {

                if (y + 1 != traverse->data)
                {
                    c[z] = y + 1; z++;
                    c[z] = traverse->data; z++;
                }
                p++;
                visit[traverse->data - 1] = 1;
                parent = traverse->data;
                Q.enqueue(traverse->data - 1);
            }

    }



        if (p < lcounter) //lcounter-> the lines -> total number of nodes
        {
            writeFile << "The Graph is disconnected" << endl;


        }
        else
        {
            writeFile << "The Graph is connected and it's MST is: ";
            for (z = 0; c[z+1] != 0; z++)
            {
                writeFile << "(" << c[z] << "," << c[z + 1] << ") ";
                z++;
            }
            writeFile << endl;

        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM