1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
#include <iostream> #include <vector> #include <queue> class NetworkTopologySimulator { private: int numNodes; // Number of nodes in the network std::vector<std::vector<int>> adjacencyMatrix; // Adjacency matrix representing network connections public: // Constructor NetworkTopologySimulator(int numNodes) : numNodes(numNodes), adjacencyMatrix(numNodes, std::vector<int>(numNodes, 0)) {} // Add connection between nodes void addConnection(int node1, int node2) { if (node1 < numNodes && node2 < numNodes) { adjacencyMatrix[node1][node2] = 1; adjacencyMatrix[node2][node1] = 1; // Assuming undirected graph } } // Perform BFS from a starting node and print the result void performBFS(int startNode) const { if (startNode >= numNodes) { std::cerr << "Invalid starting node.\n"; return; } std::vector<bool> visited(numNodes, false); std::queue<int> q; visited[startNode] = true; q.push(startNode); std::cout << "BFS traversal starting from node " << startNode << ": "; while (!q.empty()) { int node = q.front(); q.pop(); std::cout << node << " "; for (int i = 0; i < numNodes; ++i) { if (adjacencyMatrix[node][i] == 1 && !visited[i]) { visited[i] = true; q.push(i); } } } std::cout << "\n"; } }; int main() { int numNodes, numConnections, node1, node2, startNode; // Input number of nodes and connections std::cout << "Enter the number of nodes in the network: "; std::cin >> numNodes; NetworkTopologySimulator simulator(numNodes); std::cout << "Enter the number of connections: "; std::cin >> numConnections; // Input connections for (int i = 0; i < numConnections; ++i) { std::cout << "Enter connection " << i + 1 << " (node1 node2): "; std::cin >> node1 >> node2; simulator.addConnection(node1, node2); } // Input starting node for BFS std::cout << "Enter the starting node for BFS: "; std::cin >> startNode; // Perform BFS simulator.performBFS(startNode); return 0; } |
Explanation
- Class Definition (
NetworkTopologySimulator
):- Private Members:
numNodes
: Number of nodes in the network.adjacencyMatrix
: 2D vector representing connections between nodes, where a value of 1 indicates a connection.
- Private Members:
- Constructor:
- Initializes the adjacency matrix for the network with the specified number of nodes.
addConnection
Method:- Adds a connection between two nodes in the network by updating the adjacency matrix. Assumes an undirected graph (bi-directional connections).
performBFS
Method:- Performs Breadth-First Search (BFS) starting from a specified node.
- Uses a queue to explore nodes level by level.
- Marks nodes as visited to avoid re-processing and prints the traversal order.
main
Function:- Prompts the user to input the number of nodes and connections.
- Reads connections between nodes and adds them to the network using
addConnection
. - Asks for the starting node for BFS and performs the search using
performBFS
.