Skip to content

Commit 6244492

Browse files
author
Tanzim Hossain Romel
committed
Added codes from CSE 208
1 parent 42d02e2 commit 6244492

File tree

376 files changed

+208170
-8161
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

376 files changed

+208170
-8161
lines changed
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{\rtf1}

Offline 4 (Prim, Kruskal)/mst.cpp renamed to 14. Prim, Kruskal/1705069/1705069/mst.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,8 @@ class Graph{
181181

182182

183183
int main(){
184-
// FILE *fp = freopen("result.txt", "w", stdout);
185-
// FILE *fp1 = freopen("in5.txt", "r", stdin);
184+
FILE *fp = freopen("result.txt", "w", stdout);
185+
FILE *fp1 = freopen("in5.txt", "r", stdin);
186186
int n, m;
187187
cin>>n>>m;
188188

819 Bytes
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
7 11
2+
0 1 10
3+
0 2 20
4+
1 2 10
5+
1 3 5
6+
1 4 10
7+
2 5 5
8+
3 4 2
9+
3 6 5
10+
4 5 2
11+
4 6 5
12+
5 6 10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
4 6
2+
0 1 5
3+
0 2 6
4+
0 3 6
5+
1 2 5
6+
2 3 7
7+
1 0 8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
29
2+
Kruskal's algorithm:
3+
3 4
4+
4 5
5+
1 3
6+
2 5
7+
3 6
8+
0 1
9+
Prim's Algorithm:
10+
Root node = 3
11+
3 4
12+
4 5
13+
2 5
14+
3 1
15+
3 6
16+
1 0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
16
2+
Kruskal's algorithm:
3+
0 1
4+
1 2
5+
0 3
6+
Prim's Algorithm:
7+
Root node = 2
8+
2 1
9+
1 0
10+
0 3

14. Prim, Kruskal/in5.txt

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
7 11
2+
0 1 10
3+
0 2 20
4+
1 2 10
5+
1 3 5
6+
1 4 10
7+
2 5 5
8+
3 4 2
9+
3 6 5
10+
4 5 2
11+
4 6 5
12+
5 6 10

14. Prim, Kruskal/mst.cpp

+200
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
#include<iostream>
2+
#include<vector>
3+
#include<queue>
4+
#include<algorithm>
5+
using namespace std;
6+
7+
int INF = numeric_limits<int>::max();
8+
int NULL_VALUE = -1;
9+
10+
class disjoint_set{
11+
private:
12+
int *parent = NULL;
13+
int *rank = NULL;
14+
int length;
15+
16+
public:
17+
disjoint_set(int numOfElem){
18+
length = numOfElem;
19+
parent = new int[numOfElem];
20+
rank = new int[numOfElem];
21+
22+
for(int i=0;i<numOfElem;i++){
23+
rank[i] = 0;
24+
parent[i] = -1;
25+
}
26+
27+
}
28+
void make_set(int v){
29+
parent[v] = v;
30+
rank[v] = 0;
31+
}
32+
33+
int find_set(int v){
34+
if(parent[v] == v)
35+
return v;
36+
else
37+
return parent[v] = find_set(parent[v]);
38+
}
39+
40+
void union_set(int a, int b){
41+
42+
if (parent[a] == -1 || parent[b] == -1){
43+
return;
44+
}
45+
46+
a = find_set(a);
47+
b = find_set(b);
48+
49+
if(a!=b){
50+
if(rank[a]<rank[b])
51+
swap(a,b);
52+
parent[b] = a;
53+
if(rank[a] == rank[b])
54+
rank[a]++;
55+
}
56+
}
57+
58+
~disjoint_set(){
59+
delete[] parent;
60+
delete[] rank;
61+
}
62+
63+
};
64+
65+
class Edge{
66+
public:
67+
int u, v;
68+
double w;
69+
70+
Edge(int u, int v, int w){
71+
this->u = u;
72+
this->v = v;
73+
this->w = w;
74+
}
75+
76+
};
77+
78+
class Graph{
79+
int nVertices, nEdges;
80+
vector<Edge> *list;
81+
int *key, *parent;
82+
public:
83+
84+
Graph(int n){
85+
this->nVertices = n;
86+
this->nEdges = 0;
87+
88+
list = new vector<Edge>[nVertices];
89+
key = new int[nVertices];
90+
parent = new int[nVertices];
91+
92+
}
93+
94+
bool addEdge(int u, int v, double w){
95+
if (u >= nVertices || v >= nVertices || u < 0 || v < 0)
96+
return false;
97+
98+
Edge e(u, v, w);
99+
Edge e1 (v, u, w);
100+
list[u].push_back(e);
101+
list[v].push_back(e1);
102+
++nEdges;
103+
return true;
104+
}
105+
106+
void mst_prim(int root = 0){
107+
fill(key, key + nVertices, INF);
108+
fill(parent, parent + nVertices, NULL_VALUE);
109+
int weight = 0;
110+
bool visited[nVertices] = {0};
111+
priority_queue<pair<int, int>, vector<pair<int, int>>,
112+
greater<pair<int, int>>> q;
113+
114+
key[root] = 0;
115+
q.push({0, root});
116+
117+
while(!q.empty()){
118+
int u = q.top().second;
119+
q.pop();
120+
visited[u] = true;
121+
122+
for(Edge e : list[u]){
123+
if (visited[e.v] == false && key[e.v] > e.w ){
124+
parent[e.v] = u;
125+
key[e.v] = e.w;
126+
q.push({key[e.v], e.v});
127+
}
128+
}
129+
}
130+
131+
cout<<"Prim's Algorithm: \n";
132+
cout<<"Root Node: "<<root<<endl;
133+
134+
135+
for(int i = 0; i < nVertices; ++i){
136+
if (parent[i] != -1){
137+
cout<<parent[i]<<" "<<i<<endl;
138+
}
139+
}
140+
cout<<endl;
141+
142+
}
143+
144+
void mst_kruskal(){
145+
146+
vector<Edge> edgeList, mst;
147+
disjoint_set ds(nVertices);
148+
149+
for(int i = 0; i < nVertices; ++i){
150+
ds.make_set(i);
151+
for(auto e : list[i]){
152+
edgeList.push_back(e);
153+
}
154+
}
155+
int weight = 0;
156+
157+
sort(edgeList.begin(), edgeList.end(), [ ]( const auto& lhs, const auto& rhs ){
158+
return lhs.w < rhs.w;});
159+
160+
for(auto e : edgeList){
161+
int up = ds.find_set(e.u), vp = ds.find_set(e.v);
162+
163+
if (up != vp){
164+
ds.union_set(up, vp);
165+
mst.push_back(e);
166+
weight += e.w;
167+
}
168+
}
169+
cout<<weight<<endl;
170+
cout<<"Kruskal's Algorithm: \n";
171+
172+
173+
for(auto e : mst){
174+
cout<<e.u<<" "<<e.v<<endl;
175+
}
176+
177+
178+
}
179+
180+
};
181+
182+
183+
int main(){
184+
//FILE *fp = freopen("result.txt", "w", stdout);
185+
//FILE *fp1 = freopen("in5.txt", "r", stdin);
186+
int n, m;
187+
cin>>n>>m;
188+
189+
Graph g(n);
190+
for(int i = 0; i < m; ++i){
191+
int u, v, w;
192+
cin>>u>>v>>w;
193+
g.addEdge(u, v, w);
194+
}
195+
196+
g.mst_kruskal();
197+
g.mst_prim();
198+
199+
200+
}

14. Prim, Kruskal/mst.exe

149 KB
Binary file not shown.

14. Prim, Kruskal/result.txt

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
29
2+
Kruskal's Algorithm:
3+
5 4
4+
3 4
5+
6 4
6+
1 3
7+
5 2
8+
0 1
9+
Prim's Algorithm:
10+
Root Node: 0
11+
0 1
12+
5 2
13+
1 3
14+
3 4
15+
4 5
16+
3 6
17+

15. Max Flow, Matching/1705069.zip

1.71 KB
Binary file not shown.

0 commit comments

Comments
 (0)