Skip to content

Commit 4f45397

Browse files
committed
Graph GFG
1 parent 673657a commit 4f45397

File tree

6 files changed

+341
-0
lines changed

6 files changed

+341
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
bool isSafe(int i, int j, int n, int m)
2+
{
3+
if (i < 0 || i >= n || j < 0 || j >= m)
4+
{
5+
return false;
6+
}
7+
return true;
8+
}
9+
10+
vector<vector<int>> solve(vector<vector<int>> &matrix)
11+
{
12+
vector<vector<int>> ans(matrix.begin(), matrix.end());
13+
int dx[8] = {1, 1, 0, -1, -1, -1, 0, 1};
14+
int dy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
15+
16+
// count the surrounding 1 for a given cell
17+
// if count ==2 || count ==3
18+
// cell = 1
19+
// else cell = 0
20+
21+
int n = matrix.size();
22+
int m = matrix[0].size();
23+
24+
for (int i = 0; i < n; ++i)
25+
{
26+
for (int j = 0; j < m; ++j)
27+
{
28+
int count = 0;
29+
for (int k = 0; k < 8; ++k)
30+
{
31+
int newi = i + dx[k];
32+
int newj = j + dy[k];
33+
if (isSafe(newi, newj, n, m) && matrix[newi][newj] == 1)
34+
{
35+
count++;
36+
}
37+
}
38+
if (matrix[i][j] == 1 && (count == 2 || count == 3))
39+
{
40+
ans[i][j] = 1;
41+
}
42+
else if (matrix[i][j] == 0 && (count == 3))
43+
{
44+
ans[i][j] = 1;
45+
}
46+
else
47+
{
48+
ans[i][j] = 0;
49+
}
50+
}
51+
}
52+
return ans;
53+
}

GeeksForGeeks/Alien Dictionary.cpp

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
class Solution
2+
{
3+
public:
4+
void dfs(vector<int> adj[], vector<int> &visited, stack<int> &st, int src)
5+
{
6+
visited[src] = 1;
7+
for (auto it : adj[src])
8+
{
9+
if (!visited[it])
10+
{
11+
dfs(adj, visited, st, it);
12+
}
13+
}
14+
st.push(src);
15+
}
16+
17+
string topo(vector<int> adj[], int vertices)
18+
{
19+
stack<int> st;
20+
vector<int> visited(vertices, 0);
21+
for (int i = 0; i < vertices; ++i)
22+
{
23+
if (!visited[i])
24+
{
25+
dfs(adj, visited, st, i);
26+
}
27+
}
28+
29+
string ans;
30+
while (!st.empty())
31+
{
32+
ans.push_back(st.top() + 'a');
33+
st.pop();
34+
}
35+
return ans;
36+
}
37+
38+
string findOrder(string dict[], int N, int K)
39+
{
40+
vector<int> adj[26];
41+
for (int i = 0; i < N - 1; ++i)
42+
{
43+
string s1 = dict[i];
44+
string s2 = dict[i + 1];
45+
for (int j = 0; j < min(s1.size(), s2.size()); ++j)
46+
{
47+
if (s1[j] != s2[j])
48+
{
49+
int idx1 = s1[j] - 'a';
50+
int idx2 = s2[j] - 'a';
51+
adj[idx1].push_back(idx2);
52+
break;
53+
}
54+
}
55+
}
56+
return topo(adj, K);
57+
}
58+
};
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution
2+
{
3+
public:
4+
// Function to find if the given edge is a bridge in graph.
5+
void dfs(vector<int> adj[], vector<int> &visited, int src, int c, int d)
6+
{
7+
visited[src] = true;
8+
for (auto it : adj[src])
9+
{
10+
if ((src == c && it == d) || (src == d && it == c))
11+
{
12+
continue;
13+
}
14+
if (!visited[it])
15+
{
16+
dfs(adj, visited, it, c, d);
17+
}
18+
}
19+
}
20+
21+
int isBridge(int V, vector<int> adj[], int c, int d)
22+
{
23+
vector<int> visited(V, 0);
24+
dfs(adj, visited, c, c, d);
25+
if (!visited[d])
26+
{
27+
return true;
28+
}
29+
return false;
30+
}
31+
};

GeeksForGeeks/Hamiltonian Path.cpp

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
class Solution
2+
{
3+
ic:
4+
bool dfs(int node, int c , vector<int> adj[ ], int n, vector<int> &
5+
vis)
6+
{
7+
8+
if (c == n)
9+
return true;
10+
11+
vis[no de] = 1;
12+
13+
for (auto x : adj[node])
14+
{
15+
if (!vis[x] && dfs(x, c + 1, adj, n, vis))
16+
{
17+
return true;
18+
}
19+
}
20+
vis[node] = 0 ;
21+
return false;
22+
}
23+
24+
25+
bool check(int N, int M, vector<vector<int>> Edges)
26+
{
27+
c for (auto &v : Edges)
28+
{
29+
adj[v[0]] .push_b ack(
30+
v[1]);
31+
a dj[v[1]].
32+
push_back(v[0]);
33+
}
34+
35+
vector<int> vis(N + 1, 0);
36+
for (int i = 1; i <= N; i++)
37+
{
38+
if (!vis[i])
39+
{
40+
if (dfs(i, 1, adj, N, vis))
41+
{
42+
return true;
43+
}
44+
}
45+
}
46+
return false;
47+
}
48+
};

GeeksForGeeks/Knight Walk.cpp

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
class Solution
2+
{
3+
public:
4+
int dx[8] = {1, 1, -1, -1, 2, 2, -2, -2};
5+
int dy[8] = {2, -2, 2, -2, 1, -1, 1, -1};
6+
7+
bool isSafe(int i, int j, int n)
8+
{
9+
if (i < 1 || i > n || j < 1 || j > n)
10+
{
11+
return false;
12+
}
13+
return true;
14+
}
15+
16+
int minStepToReachTarget(vector<int> &start, vector<int> &end, int N)
17+
{
18+
int ans = 0;
19+
queue<pair<int, int>> q;
20+
vector<vector<int>> visited(N + 1, vector<int>(N + 1, 0));
21+
22+
int s0 = start[0], s1 = start[1];
23+
q.push({s0, s1});
24+
visited[s0][s1] = 1;
25+
26+
while (!q.empty())
27+
{
28+
int sx = q.size();
29+
while (sx--)
30+
{
31+
pair<int, int> curr = q.front();
32+
q.pop();
33+
int x = curr.first;
34+
int y = curr.second;
35+
36+
if (x == end[0] && y == end[1])
37+
{
38+
return ans;
39+
}
40+
41+
for (int i = 0; i < 8; ++i)
42+
{
43+
int newx = x + dx[i];
44+
int newy = y + dy[i];
45+
if (isSafe(newx, newy, N) && !visited[newx][newy])
46+
{
47+
q.push({newx, newy});
48+
visited[newx][newy] = 1;
49+
}
50+
}
51+
}
52+
ans++;
53+
}
54+
return ans;
55+
}
56+
};

SPOJ/COLORFUL ARRAY.cpp

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
#define HAR_HAR_MAHADEV int main()
5+
6+
#define mod int(1e9 + 7)
7+
#define ll long long
8+
#define tc \
9+
int t; \
10+
cin >> t; \
11+
while (t--)
12+
#define fastIO \
13+
ios_base::sync_with_stdio(false); \
14+
cin.tie(0); \
15+
cout.tie(0)
16+
17+
class Graph
18+
{
19+
public:
20+
Graph(ll vertex)
21+
: V(vertex)
22+
{
23+
adj = new vector<pair<ll, ll>>[V];
24+
}
25+
26+
void addEdge(ll u, ll v, ll weight)
27+
{
28+
adj[u].push_back({v, weight});
29+
adj[v].push_back({u, weight});
30+
}
31+
32+
void print(vector<ll> &ans);
33+
ll holiday();
34+
35+
private:
36+
ll V;
37+
vector<pair<ll, ll>> *adj;
38+
};
39+
40+
ll Graph::holiday()
41+
{
42+
vector<ll> visited(V, 0);
43+
vector<ll> distance(V, 1);
44+
function<ll(vector<ll> & visited, vector<ll> & distance, ll src, ll & ans)> dfs;
45+
dfs = [&](vector<ll> &visited, vector<ll> &distance, ll src, ll &ans)
46+
{
47+
visited[src] = 1;
48+
for (auto itr : adj[src])
49+
{
50+
ll node = itr.first;
51+
ll weight = itr.second;
52+
if (visited[node] == 0)
53+
{
54+
distance[src] += dfs(visited, distance, node, ans);
55+
ans += 2 * min(distance[node], V - 1 - distance[node]) * weight;
56+
}
57+
}
58+
return distance[src];
59+
};
60+
61+
ll ans = 0;
62+
dfs(visited, distance, 1, ans);
63+
return ans;
64+
}
65+
66+
void solve()
67+
{
68+
int n;
69+
cin >> n;
70+
Graph g(n + 1);
71+
for (int i = 1; i < n; ++i)
72+
{
73+
int u, v, w;
74+
cin >> u >> v >> w;
75+
g.addEdge(u, v, w);
76+
}
77+
cout << g.holiday();
78+
}
79+
80+
HAR_HAR_MAHADEV
81+
{
82+
fastIO;
83+
int counter = 1;
84+
#ifndef ONLINE_JUDGE
85+
cerr << "HEllo";
86+
#endif
87+
tc
88+
{
89+
cout << "Case #" << counter << ": ";
90+
solve();
91+
cout << "\n";
92+
counter++;
93+
}
94+
return 0;
95+
}

0 commit comments

Comments
 (0)