Skip to content

Commit 79aaad4

Browse files
Merge pull request #214 from makjn10/add-graph-algos
Added following new graph algorithms in folder Graph Algorithms (all in C++):
2 parents 3d06274 + f0e95f7 commit 79aaad4

File tree

12 files changed

+870
-0
lines changed

12 files changed

+870
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
//this code can be used to find Articulation points in a Graph
2+
#include <bits/stdc++.h>
3+
using namespace std;
4+
5+
int timee;// a variable to keep track of the time at which a particular vertex is encounterd
6+
7+
vector<int> graph[100];//adjacency list for the graph (100 is the max number of vertices.. you can change as per your needs)
8+
bool visited[100] , AP[100];
9+
//visited - bool array used in DFS to know whether the node is visited or not
10+
//AP - bool array to tell whether ith vertex is a AP or NOT
11+
12+
int disc[100] , low[100] , parent[100];
13+
//disc - discovery time of a vertex
14+
//low - exit time of a source
15+
//parent - array to tell the parent of ith vertex
16+
17+
//finding all APs
18+
void printAP2(int source){
19+
visited[source] = true;
20+
disc[source] = timee++;
21+
low[source] = disc[source];
22+
int child = 0;
23+
for(auto i : graph[source]){
24+
if(!visited[i]){
25+
child++;
26+
parent[i] = source;
27+
printAP2(i);
28+
low[source] = min(low[i] , low[source]);
29+
30+
if(parent[source] == source){
31+
if(child > 1) AP[source] = true;
32+
}
33+
else{
34+
if(low[i] >= disc[source]) AP[source] = true; // IMP
35+
}
36+
}
37+
else if(i != parent[source]) low[source] = min(low[i] , low[source]);
38+
}
39+
}
40+
41+
//find and print all APs
42+
void printAP(int n , int source){
43+
for(int i = 0 ; i < n ; i++) low[i] = INT_MAX;
44+
parent[source] = source; //initializing source os source vertex to itself
45+
printAP2(source);
46+
for(int i = 0 ; i < n ; i++){
47+
if(!visited[i]){
48+
parent[i] = i;
49+
printAP2(i);
50+
}
51+
}
52+
for(int i = 0 ; i < n ; i++){
53+
if(AP[i]) cout << i << " ";
54+
}
55+
cout << endl;
56+
}
57+
58+
int main(){
59+
//n - no. of vertices , e - no. of edges
60+
int n , e , u , v;
61+
cin >> n >> e;
62+
//inputting e edges
63+
for(int i = 0 ; i < e ; i++){
64+
cin >> u >> v;
65+
graph[u].push_back(v);
66+
graph[v].push_back(u);
67+
}
68+
printAP(n , 0);
69+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
//this code can be used to find Articulation points in a Graph
2+
#include <bits/stdc++.h>
3+
using namespace std;
4+
5+
int timee;// a variable to keep track of the time at which a particular vertex is encounterd
6+
7+
vector<int> graph[100];//adjacency list for the graph (100 is the max number of vertices.. you can change as per your needs)
8+
bool visited[100] , AP[100];
9+
//visited - bool array used in DFS to know whether the node is visited or not
10+
//AP - bool array to tell whether ith vertex is a AP or NOT
11+
12+
int disc[100] , low[100] , parent[100];
13+
//disc - discovery time of a vertex
14+
//low - exit time of a source
15+
//parent - array to tell the parent of ith vertex
16+
17+
//finding all APs
18+
void printAP2(int source){
19+
visited[source] = true;
20+
disc[source] = timee++;
21+
low[source] = disc[source];
22+
int child = 0;
23+
for(auto i : graph[source]){
24+
if(!visited[i]){
25+
child++;
26+
parent[i] = source;
27+
printAP2(i);
28+
low[source] = min(low[i] , low[source]);
29+
30+
if(parent[source] == source){
31+
if(child > 1) AP[source] = true;
32+
}
33+
else{
34+
if(low[i] >= disc[source]) AP[source] = true; // IMP
35+
}
36+
}
37+
else if(i != parent[source]) low[source] = min(low[i] , low[source]);
38+
}
39+
}
40+
41+
//find and print all APs
42+
void printAP(int n , int source){
43+
for(int i = 0 ; i < n ; i++) low[i] = INT_MAX;
44+
parent[source] = source;
45+
printAP2(source);
46+
for(int i = 0 ; i < n ; i++){
47+
if(!visited[i]){
48+
parent[i] = i;
49+
printAP2(i);
50+
}
51+
}
52+
for(int i = 0 ; i < n ; i++){
53+
if(AP[i]) cout << i << " ";
54+
}
55+
cout << endl;
56+
}
57+
58+
int main(){
59+
//n - no. of vertices , e - no. of edges
60+
int n , e , u , v;
61+
cin >> n >> e;
62+
//inputting e edges
63+
for(int i = 0 ; i < e ; i++){
64+
cin >> u >> v;
65+
graph[u].push_back(v);
66+
graph[v].push_back(u);
67+
}
68+
printAP(n , 0);
69+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
//this code can be used to find Articulation points(APs) and bridges in a Graph
2+
#include <bits/stdc++.h>
3+
using namespace std;
4+
5+
int timee;// a variable to keep track of the time at which a particular vertex is encounterd
6+
vector<int> graph[10001];//adjacency list for the graph (10000 is the max number of vertices.. you can change as per your needs)
7+
bool visited[10001] , AP[10001];
8+
//visited - bool array used in DFS to know whether the node is visited or not
9+
//AP - bool array to tell whether ith vertex is a AP or NOT
10+
11+
int disc[10001] , low[10001] , parent[10001];
12+
//disc - discovery time of a vertex
13+
//low - exit time of a source
14+
//parent - array to tell the parent of ith vertex
15+
16+
//function to find APs and Bridges
17+
void printAP2(int source , vector<int> &Ap , vector<pair<int , int> > &bridges){
18+
visited[source] = true;
19+
disc[source] = timee++;
20+
low[source] = disc[source];
21+
int child = 0;
22+
for(auto i : graph[source]){
23+
if(!visited[i]){
24+
child++;
25+
parent[i] = source;
26+
printAP2(i , Ap , bridges);
27+
low[source] = min(low[i] , low[source]);
28+
29+
if(parent[source] == source){
30+
if(child > 1) Ap.push_back(source);
31+
}
32+
else{
33+
if(low[i] >= disc[source]) Ap.push_back(source); // IMP
34+
}
35+
36+
if(low[i] > disc[source]){
37+
pair<int , int> temp;
38+
temp.first = source;
39+
temp.second = i;
40+
bridges.push_back(temp);
41+
}
42+
}
43+
else if(i != parent[source]) low[source] = min(low[source] , disc[i]);
44+
}
45+
}
46+
47+
void printAP(int n , int source){
48+
vector<int> Ap;
49+
vector<pair<int , int> > bridges; //contains edges which are bridges
50+
parent[source] = source;
51+
printAP2(source , Ap , bridges);
52+
for(int i = 0 ; i < n ; i++){
53+
if(!visited[i]){
54+
parent[i] = i;
55+
printAP2(i , Ap , bridges);
56+
}
57+
}
58+
//print number of APs and Bridges
59+
//printing the APS and Bridges
60+
sort(Ap.begin() , Ap.end());
61+
sort(bridges.begin() , bridges.end());
62+
cout << Ap.size() << endl;
63+
for(int i = 0 ; i < Ap.size() ; i++){
64+
cout << Ap[i] << " ";
65+
}
66+
cout << endl;
67+
68+
cout << bridges.size() << endl;
69+
for(int i = 0 ; i < bridges.size() ; i++){
70+
cout << bridges[i].first << " " << bridges[i].second << endl;
71+
}
72+
}
73+
74+
int main(){
75+
//n - no. of vertices , e - no. of edges
76+
int n , e , u , v;
77+
cin >> n >> e;
78+
//inputting e edges
79+
for(int i = 0 ; i < e ; i++){
80+
cin >> u >> v;
81+
graph[u].push_back(v);
82+
graph[v].push_back(u);
83+
}
84+
printAP(n , 0);
85+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
//this code can be used to find Articulation points(APs) and bridges in a Graph
2+
#include <bits/stdc++.h>
3+
using namespace std;
4+
5+
int timee;// a variable to keep track of the time at which a particular vertex is encounterd
6+
vector<int> graph[10001];//adjacency list for the graph (10000 is the max number of vertices.. you can change as per your needs)
7+
bool visited[10001] , AP[10001];
8+
//visited - bool array used in DFS to know whether the node is visited or not
9+
//AP - bool array to tell whether ith vertex is a AP or NOT
10+
11+
int disc[10001] , low[10001] , parent[10001];
12+
//disc - discovery time of a vertex
13+
//low - exit time of a source
14+
//parent - array to tell the parent of ith vertex
15+
16+
void printAP2(int source , vector<int> &Ap , vector<pair<int , int> > &bridges){
17+
visited[source] = true;
18+
disc[source] = timee++;
19+
low[source] = disc[source];
20+
int child = 0;
21+
for(auto i : graph[source]){
22+
if(!visited[i]){
23+
child++;
24+
parent[i] = source;
25+
printAP2(i , Ap , bridges);
26+
low[source] = min(low[i] , low[source]);
27+
28+
if(parent[source] == source){
29+
if(child > 1) Ap.push_back(source);
30+
}
31+
else{
32+
if(low[i] >= disc[source]) Ap.push_back(source); // IMP
33+
}
34+
35+
if(low[i] > disc[source]){
36+
pair<int , int> temp;
37+
temp.first = source;
38+
temp.second = i;
39+
bridges.push_back(temp);
40+
}
41+
}
42+
else if(i != parent[source]) low[source] = min(low[source] , disc[i]);
43+
}
44+
}
45+
46+
void printAP(int n , int source){
47+
vector<int> Ap;
48+
vector<pair<int , int> > bridges;
49+
parent[source] = source;
50+
printAP2(source , Ap , bridges);
51+
for(int i = 0 ; i < n ; i++){
52+
if(!visited[i]){
53+
parent[i] = i;
54+
printAP2(i , Ap , bridges);
55+
}
56+
}
57+
//print number of APs and Bridges
58+
//printing the APS and Bridges
59+
sort(Ap.begin() , Ap.end());
60+
sort(bridges.begin() , bridges.end());
61+
cout << Ap.size() << endl;
62+
for(int i = 0 ; i < Ap.size() ; i++){
63+
cout << Ap[i] << " ";
64+
}
65+
cout << endl;
66+
67+
cout << bridges.size() << endl;
68+
for(int i = 0 ; i < bridges.size() ; i++){
69+
cout << bridges[i].first << " " << bridges[i].second << endl;
70+
}
71+
}
72+
73+
int main(){
74+
//n - no. of vertices , e - no. of edges
75+
int n , e , u , v;
76+
cin >> n >> e;
77+
//inputting e edges
78+
for(int i = 0 ; i < e ; i++){
79+
cin >> u >> v;
80+
graph[u].push_back(v);
81+
graph[v].push_back(u);
82+
}
83+
printAP(n , 0);
84+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
#define pb push_back
5+
#define mp make_pair
6+
7+
// edge class
8+
class edge{
9+
public:
10+
int u , v , weight; // u and v are the end points of an edge
11+
// weight is the weight of the edge
12+
edge(){}
13+
edge(int a , int b , int c){
14+
u = a , v = b , weight = c;
15+
}
16+
};
17+
18+
bool compare(edge a , edge b){
19+
return a.weight < b.weight;
20+
}
21+
22+
vector<edge> edges; //vector of all the edges of the graph
23+
vector<edge> result; //vector that will contain the edges present in MST
24+
25+
//creating a DSU (DISJOINT SET UNION) - to find and detect cycles
26+
int parent[100001] , ranks[100001];
27+
void make_set(int v){
28+
parent[v] = v;
29+
}
30+
int find_set(int v){
31+
if(v == parent[v])
32+
return v;
33+
return parent[v] = find_set(parent[v]);
34+
}
35+
bool check(int a , int b){
36+
a = find_set(a);
37+
b = find_set(b);
38+
if(a == b) return false;
39+
40+
if(ranks[a] < ranks[b]) swap(a , b);
41+
parent[b] = a;
42+
if(ranks[a] == ranks[b]) ranks[a]++;
43+
return true;
44+
}
45+
46+
// function to find the MST that returns the overall weight os the MST
47+
int calcMST(int n , int m){
48+
int weight = 0 , totaledges = 0;
49+
//sorting the edges as per their weights
50+
sort(edges.begin() , edges.end() , compare);
51+
//initializing dsu
52+
for(int i = 1 ; i <= n ; i++){
53+
make_set(i);
54+
}
55+
//picking edges and checking
56+
for(auto i : edges){
57+
if(check(i.u , i.v)){
58+
result.pb(i);
59+
weight += i.weight;
60+
totaledges++;
61+
if(totaledges == n - 1) break;
62+
}
63+
}
64+
return weight;
65+
}
66+
67+
int main(){
68+
//n - no. of vertices , m - no. of edges
69+
int n , m , u , v , weight;
70+
cin >> n >> m;
71+
//inputting m edges
72+
for(int i = 0 ; i < m ; i++){
73+
cin >> u >> v >> weight;
74+
edge temp(u , v , weight);
75+
edges.pb(temp);
76+
}
77+
int ans = calcMST(n , m);
78+
cout << ans << endl;
79+
80+
//printing all the edges present in result vector
81+
for(int i = 0 ; i < result.size() ; i++){
82+
cout << result[i].u << " " << result[i].v << " " << result[i].weight << endl;
83+
}
84+
}

0 commit comments

Comments
 (0)