-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph_Visualizer_using_BFS_DFS.cpp
More file actions
153 lines (130 loc) · 3.56 KB
/
Copy pathGraph_Visualizer_using_BFS_DFS.cpp
File metadata and controls
153 lines (130 loc) · 3.56 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include <iostream>
#include <vector>
#include <queue>
#include <stack>
using namespace std;
// Node structure to represent individuals in the social network
struct Node
{
int id;
vector<int> friends;
};
class SocialNetwork
{
private:
vector<Node> graph;
int numNodes;
public:
// Constructor to initialize the graph with a given number of nodes
SocialNetwork(int n) : numNodes(n)
{
graph.resize(n);
for (int i = 0; i < n; ++i)
{
graph[i].id = i;
}
}
// Function to add an edge between two nodes (individuals)
void addEdge(int u, int v) {
graph[u].friends.push_back(v);
graph[v].friends.push_back(u); // Assuming undirected graph
}
// BFS algorithm to find the shortest path between two individuals
vector<int> bfs(int src, int dest)
{
vector<bool> visited(numNodes, false);
vector<int> distance(numNodes, -1);
queue<int> q;
q.push(src);
visited[src] = true;
distance[src] = 0;
while (!q.empty())
{
int u = q.front();
q.pop();
for (int v : graph[u].friends)
{
if (!visited[v])
{
visited[v] = true;
distance[v] = distance[u] + 1;
q.push(v);
}
}
}
// Reconstructing the shortest path
vector<int> shortestPath;
int u = dest;
while (u != src) {
shortestPath.push_back(u);
for (int v : graph[u].friends) {
if (distance[v] == distance[u] - 1) {
u = v;
break;
}
}
}
shortestPath.push_back(src);
string reverse(shortestPath.begin(), shortestPath.end());
return shortestPath;
}
// DFS algorithm to check connectivity and identify connected components
void dfsUtil(int u, vector<bool>& visited)
{
visited[u] = true;
cout << u << " ";
for (int v : graph[u].friends)
{
if (!visited[v]) {
dfsUtil(v, visited);
}
}
}
void dfs(int src)
{
vector<bool> visited(numNodes, false);
cout << "Connected components:\n";
dfsUtil(src, visited);
// Check if any node remains unvisited (connected components)
for (int i = 0; i < numNodes; ++i) {
if (!visited[i]) {
cout << "\n";
dfsUtil(i, visited);
}
}
}
};
int main()
{
int numNodes, numEdges;
cout << "Enter the number of individuals in the social network: ";
cin >> numNodes;
SocialNetwork network(numNodes);
cout << "Enter the number of friendships: ";
cin >> numEdges;
cout << "Enter friendships (individual IDs):" << endl;
for (int i = 0; i < numEdges; ++i)
{
int u, v;
cin >> u >> v;
network.addEdge(u, v);
}
// Query for shortest path between individuals
int src, dest;
cout << "Enter source and destination nodes for shortest path: ";
cin >> src >> dest;
vector<int> shortestPath = network.bfs(src, dest);
cout << "Shortest path: ";
for (int node : shortestPath)
{
cout << node << " ";
}
cout << "\n";
// Query for connectivity and connected components
int startNode;
cout << "Enter a node to perform DFS from: ";
cin >> startNode;
cout << "DFS traversal:\n";
network.dfs(startNode);
return 0;
}