Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create TraerseGraph.cpp #69

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 95 additions & 0 deletions TraerseGraph.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#include <bits/stdc++.h>
using namespace std;
const int maxx = 100005;
vector<int> graph[maxx];

// Function to perform the DFS calculating the
// count of the elements in a connected component
void dfs(int curr, int& cnt, int*
visited, vector<int>& duringdfs)
{
visited[curr] = 1;

// Number of nodes in this component
++cnt;

// Stores the nodes which belong
// to current component
duringdfs.push_back(curr);
for (auto& child : graph[curr]) {

// If the child is not visited
if (visited[child] == 0) {
dfs(child, cnt, visited, duringdfs);
}
}
}

// Function to return the desired
// count for every node in the graph
void MaximumVisit(int n, int k)
{
// To keep track of nodes we visit
int visited[maxx];

// Mark every node unvisited
memset(visited, 0, sizeof visited);
int ans[maxx];

// No of connected elements for each node
memset(ans, 0, sizeof ans);
vector<int> duringdfs;
for (int i = 1; i <= n; ++i) {
duringdfs.clear();
int cnt = 0;

// If a node is not visited, perform a DFS as
// this node belongs to a different component
// which is not yet visited
if (visited[i] == 0) {
cnt = 0;
dfs(i, cnt, visited, duringdfs);
}

// Now store the count of all the visited
// nodes for any particular component.
for (auto& x : duringdfs) {
ans[x] = cnt;
}
}

// Print the result
for (int i = 1; i <= n; ++i) {
cout << ans[i] << " ";
}
cout << endl;
return;
}

// Function to build the graph
void MakeGraph(){
graph[1].push_back(2);
graph[2].push_back(1);
graph[2].push_back(3);
graph[3].push_back(2);
graph[3].push_back(4);
graph[4].push_back(3);
graph[5].push_back(6);
graph[6].push_back(5);
graph[6].push_back(7);
graph[7].push_back(6);
graph[5].push_back(7);
graph[7].push_back(5);
}

// Driver code
int main()
{
int N = 7, K = 6;

// Build the graph
MakeGraph();

MaximumVisit(N, K);
return 0;
}