0

I have the queue functions and I have to use them to count the number of graph components. These are my functions:

    struct TQueue {
    int value;
    TQueue * next;
};

void QueueInit(TQueue * & a_head, TQueue * & a_tail) {
    a_head = NULL;
    a_tail = NULL;
};

bool IsQueueEmpty(TQueue * a_head) {
    return !a_head;
};

void Enqueue(TQueue * & a_head, TQueue * & a_tail, int a_val) {
    TQueue * l_hlp = new TQueue;
    l_hlp->value = a_val;
    l_hlp->next = NULL;
    if (a_tail) a_tail->next = l_hlp;
    if (!a_head) a_head = l_hlp;
    a_tail = l_hlp;
}

int Dequeue(TQueue * & a_head, TQueue * & a_tail) {
    int l_val = a_head->value;
    TQueue * l_hlp = a_head;
    a_head = a_head->next;
    if (!a_head) a_tail = NULL;
    delete l_hlp;
    return l_val;

};

void EmptyQueue(TQueue * & a_head, TQueue * & a_tail) {
    TQueue * l_hlp;
    while (a_head) {
        l_hlp = a_head;
        a_head = a_head->next;
        delete l_hlp;
    }
    a_tail = NULL;
};

And this is my main() function and some variables:

    // constant - number of vertexes
    const int n = 9;

    // queue declaration
    TQueue *phead, *ptail;    
    QueueInit(phead, ptail);

    // array of distanc    
    int dist[n];

    // distanc array initialization   
    for (int i = 0; i < n; i++) dist[i] = -1;

    // current distance
    int d = 0;    
    // processingvertex
    int v;    
    // number of components
    int c = 0;

I prefer to use stack and DFS but it's a school projec so I must to use queue and BFS algorithm.

ZlyVlk
  • 29
  • 7
  • 1
    And what is your question? Did you read your algorithms handbook on how breadth-first search algorithms use a queue? Once you figure that out, read about algorithms for finding connected components, such as union find. – Botje Aug 06 '21 at 07:38

2 Answers2

0

The approach is same as you would do in a dfs based approach. Here is a general bfs implementation using queue which you can modify easily for your purpose(i.e.using a custom queue)

#include <iostream>
#include <vector>
#include <queue>
using namespace std;

void bfs(int src, vector<bool>& vis, vector<vector<int>>& adj) {
    queue<int> q;
    q.push(src);
    vis[src] = true;
    while(!q.empty()) {
        int u = q.front(); q.pop();
        vis[u] = true;
        for(int v : adj[u]) {
            if(!vis[v]) q.push(v);
        }
    }
}

int main() {
    int n, m;
    cin >> n >> m;

    vector<vector<int>> adj(n);

    for(int i = 0; i < m; i++) {
        int u, v;
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }

    int count = 0;
    vector<bool> vis(n);
    for(int i = 0; i < n; i++) {
        if(!vis[i]) {
            bfs(i, vis, adj);
            count++;
        }
    }
    cout << count << '\n';
}

Variables reference

n : number of nodes in graph
m : number of edges in graph
adj : adjacency list representation of graph
vis : array to store if node is visited or not
count : number of connected components in graph

Example Test Case

Input:
6 3
1 5
0 2
2 4
Output:
3

You can refer to this post on mathematics stack exchange for understanding the algorithm: Finding connected components in a graph using BFS

Nilesh Das
  • 9
  • 1
  • 3
0

This is my attempt to code compute components and assign vertices to components:

  for (int i = 0; i <= n - 1; i++) visited[i] = false;
            
            c = 0;
            while (1) {
                c++;
                all = true;
                for (int i = 0; i < n; i++)
    
                    if (!visited[i]) {
                        all = false;
                        visited[i] = c;
                        fronta::Enqueue(phead, ptail, i);
                        break;
                    }
                if (all) {                
                    c--;
                    break;
                }   
                fronta::Enqueue(phead, ptail, 0);                
                visited[0] = true;               
                while (!fronta::IsQueueEmpty(phead)) {                
                    v = fronta::Dequeue(phead, ptail); 
                    //cout << "processing vertex: " << v << endl;
                    
                    for (int j = 0; j < n; j++) {
                        if (gr3[v][j] && !visited[j]) {                                          
                            fronta::Enqueue(phead, ptail, j);                           
                            visited[j] = true;
    
                        }
                    }
                   
                }
    
            }

But it doesn't work very well...

ZlyVlk
  • 29
  • 7