So I was training to solve problems with old tracks of old mock interview videos and I came across this track:
Consider an undirected graph with ???? n nodes e ???? w arcs. It requires writing a program in C++ that finds a node such that its removal completely disconnects the graph, that is, it divides the graph into two or more connected components.
The program should read the number of nodes from input ???? n is the number of edges ???? w, followed by the list of edges of the graph. Next, it should determine and output a node that, if removed, completely disconnects the graph. If there is no node disconnecting the graph, the program should print an appropriate message.
Now I’ve done the program but I have a problem with the final result as it comes out as 0 regardless and I don’t understand where the problem is, below you will find the code I wrote:
#include <iostream>
#include <vector>
#include <unordered_set>
using namespace std;
// Funzione per eseguire una DFS per contare il numero di componenti connesse nel grafo
void dfs(int node, vector<vector<int>>& graph, vector<bool>& visited) {
visited[node] = true;
for (int neighbor : graph[node]) {
if (!visited[neighbor]) {
vector<bool> visited_copy = visited;
dfs(neighbor, graph, visited);
}
}
}
// Funzione per trovare il nodo che disconnette completamente il grafo
int findDisconnectingNode(vector<vector<int>>& graph, int n) {
for (int node = 0; node < n; ++node) {
// Memorizza lo stato originale del grafo prima di rimuovere temporaneamente il nodo
vector<vector<int>> original_graph = graph;
// Rimuovi il nodo dalla lista di adiacenza del grafo temporaneamente
graph[node].clear();
// Esegui una DFS per contare il numero di componenti connesse nel grafo senza il nodo
vector<bool> visited(n, false);
int components = 0;
for (int i = 0; i < n; ++i) {
if (!visited[i]) {
dfs(i, graph, visited);
components++;
}
}
// Ripristina lo stato originale del grafo
graph = original_graph;
// Se ci sono più di una componente connessa, restituisci il nodo che disconnette il grafo
if (components > 1) {
return node;
}
}
// Se non viene trovato nessun nodo che disconnette il grafo, restituisci -1
return -1;
}
int main() {
int n, w;
cout << "Inserisci il numero di nodi e di archi:n";
cin >> n >> w;
// Inizializza il grafo con n nodi
vector<vector<int>> graph(n);
cout << "Inserisci gli archi del grafo:n";
for (int i = 0; i < w; ++i) {
int u, v;
cin >> u >> v;
// Aggiungi l'arco u-v al grafo
graph[u].push_back(v);
graph[v].push_back(u);
}
int disconnectingNode = findDisconnectingNode(graph, n);
if (disconnectingNode != -1) {
cout << "Il nodo che disconnette completamente il grafo e': " << disconnectingNode << "n";
} else {
cout << "Non esiste un nodo che disconnette completamente il grafo.n";
}
// Attendi l'input dall'utente prima di terminare il programma
cin.get();
return 0;
}
Input:
6 7
0 1
0 2
1 2
1 3
2 4
3 5
4 5
Output atteso:
The node that completely disconnects the graph is: 2
If anyone can explain to me where I went wrong and tell me what I can study more to stop making these mistakes I would be very grateful.
Ciro is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
5