Skip to content

Commit 1baac91

Browse files
authored
Merge pull request #2 from Snehal-Reddy/master
Added question - Product_of_lengths_all_cycles_undirected_graph
2 parents 28a8dc8 + c9fe772 commit 1baac91

File tree

2 files changed

+128
-0
lines changed

2 files changed

+128
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Given an undirected and unweighted graph. The task is to find the product of the lengths of all cycles formed in it.
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
//solution source - geeksforgeeks
2+
#include <bits/stdc++.h>
3+
using namespace std;
4+
const int N = 100000;
5+
6+
// variables to be used
7+
// in both functions
8+
vector<int> graph[N];
9+
10+
// Function to mark the vertex with
11+
// different colors for different cycles
12+
void dfs_cycle(int u, int p, int color[],
13+
int mark[], int par[], int& cyclenumber)
14+
{
15+
16+
// already (completely) visited vertex.
17+
if (color[u] == 2) {
18+
return;
19+
}
20+
21+
// seen vertex, but was not completely
22+
// visited -> cycle detected.
23+
// backtrack based on parents to find
24+
// the complete cycle.
25+
if (color[u] == 1) {
26+
27+
cyclenumber++;
28+
int cur = p;
29+
mark[cur] = cyclenumber;
30+
31+
// backtrack the vertex which are
32+
// in the current cycle thats found
33+
while (cur != u) {
34+
cur = par[cur];
35+
mark[cur] = cyclenumber;
36+
}
37+
return;
38+
}
39+
par[u] = p;
40+
41+
// partially visited.
42+
color[u] = 1;
43+
44+
// simple dfs on graph
45+
for (int v : graph[u]) {
46+
47+
// if it has not been visited previously
48+
if (v == par[u]) {
49+
continue;
50+
}
51+
dfs_cycle(v, u, color, mark, par, cyclenumber);
52+
}
53+
54+
// completely visited.
55+
color[u] = 2;
56+
}
57+
58+
// add the edges to the graph
59+
void addEdge(int u, int v)
60+
{
61+
graph[u].push_back(v);
62+
graph[v].push_back(u);
63+
}
64+
65+
// Function to print the cycles
66+
int productLength(int edges, int mark[], int& cyclenumber)
67+
{
68+
unordered_map<int, int> mp;
69+
70+
// push the edges that into the
71+
// cycle adjacency list
72+
for (int i = 1; i <= edges; i++) {
73+
if (mark[i] != 0)
74+
mp[mark[i]]++;
75+
}
76+
int cnt = 1;
77+
78+
// prodcut all the length of cycles
79+
for (int i = 1; i <= cyclenumber; i++) {
80+
cnt = cnt * mp[i];
81+
}
82+
if (cyclenumber == 0)
83+
cnt = 0;
84+
85+
return cnt;
86+
}
87+
88+
// Driver Code
89+
int main()
90+
{
91+
92+
// add edges
93+
addEdge(1, 2);
94+
addEdge(2, 3);
95+
addEdge(3, 4);
96+
addEdge(4, 6);
97+
addEdge(4, 7);
98+
addEdge(5, 6);
99+
addEdge(3, 5);
100+
addEdge(7, 8);
101+
addEdge(6, 10);
102+
addEdge(5, 9);
103+
addEdge(10, 11);
104+
addEdge(11, 12);
105+
addEdge(11, 13);
106+
addEdge(12, 13);
107+
108+
// arrays required to color the
109+
// graph, store the parent of node
110+
int color[N];
111+
int par[N];
112+
113+
// mark with unique numbers
114+
int mark[N];
115+
116+
// store the numbers of cycle
117+
int cyclenumber = 0;
118+
int edges = 13;
119+
120+
// call DFS to mark the cycles
121+
dfs_cycle(1, 0, color, mark, par, cyclenumber);
122+
123+
// function to print the cycles
124+
cout << productLength(edges, mark, cyclenumber);
125+
126+
return 0;
127+
}

0 commit comments

Comments
 (0)