Skip to content

Commit c656394

Browse files
committed
Make comments concise and refactor some code
1 parent c8b0086 commit c656394

File tree

11 files changed

+223
-141
lines changed

11 files changed

+223
-141
lines changed

Detect_Cycle/directed.cc

+10-9
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
/** Given a directed graph, check whether the graph contains a cycle or not.
1+
/**
2+
* Given a directed graph, check whether the graph contains a cycle or not.
23
*
3-
* Note: There is a cycle in a graph only if there is a back edge present in
4-
* it. A back edge is an edge that is from a node to itself (self-loop) or
5-
* one of its ancestor i.e. an edge to the next node will form a cycle, only
6-
* if that node is an ancestor of the present node.
4+
* Note: There is a cycle in a graph only if there is a back edge present in
5+
* it. A back edge is an edge that is from a node to itself (self-loop) or
6+
* one of its ancestor i.e. an edge to the next node will form a cycle, only
7+
* if that node is an ancestor of the present node.
78
*
8-
* To detect a back edge, we can keep track of vertices currently in recursion
9-
* stack of function for DFS traversal. If we reach a vertex that is already in
10-
* the recursion stack, then there is a cycle in the tree.
9+
* To detect a back edge, we can keep track of vertices currently in recursion
10+
* stack of function for DFS traversal. If we reach a vertex that is already in
11+
* the recursion stack, then there is a cycle in the tree.
1112
*/
1213
#include <bits/stdc++.h>
1314
using namespace std;
@@ -83,4 +84,4 @@ int main() {
8384

8485
return 0;
8586
}
86-
// Time Complexity: Same as DFS i.e. O(V + E).
87+
// Time Complexity: Same as DFS i.e. O(V + E).

Detect_Cycle/directed_color.cc

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
/** Given a directed graph, check whether the graph contains a cycle or not.
1+
/**
2+
* Given a directed graph, check whether the graph contains a cycle or not.
23
*
3-
* Note: The idea is to do DFS of given graph and while doing traversal, assign
4-
* one of the three colors (white, gray, black) to every vertex.
4+
* Note: The idea is to do DFS of given graph and while doing traversal, assign
5+
* one of the three colors (white, gray, black) to every vertex.
56
*/
67
#include <bits/stdc++.h>
78
using namespace std;
@@ -72,4 +73,4 @@ int main() {
7273

7374
return 0;
7475
}
75-
// Time Complexity: Same as DFS i.e. O(V + E).
76+
// Time Complexity: Same as DFS i.e. O(V + E).

Detect_Cycle/undirected.cc

+10-8
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
/** Given a undirected graph, check whether the graph contains a cycle or not.
1+
/**
2+
* Given a undirected graph, check whether the graph contains a cycle or not.
23
*
3-
* Note: Like directed graphs, we can use DFS to detect cycle in an undirected graph.
4-
* For every visited vertex ‘v’, if there is an adjacent ‘u’ such that u has
5-
* already been visited and u is not parent of v, then there is a cycle in graph.
6-
*
7-
* The assumption of this approach is that there are no parallel edges between any
8-
* two vertices.
4+
* Note: Like directed graphs, we can use DFS to detect cycle in an undirected
5+
* graph. For every visited vertex ‘v’, if there is an adjacent ‘u’ such that
6+
* u has already been visited and u is not parent of v, then there is a cycle
7+
* in graph.
8+
*
9+
* The assumption of this approach is that there are no parallel edges between
10+
* any two vertices.
911
*/
1012
#include <bits/stdc++.h>
1113
using namespace std;
@@ -74,4 +76,4 @@ int main() {
7476

7577
return 0;
7678
}
77-
// Time Complexity: Same as DFS i.e. O(V + E).
79+
// Time Complexity: Same as DFS i.e. O(V + E).

Min_Spanning_Tree/prim_list.cc

+23-22
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
1-
/** Given an undirected, connected and weighted graph, find MST of the graphs
2-
* using Prim’s algorithm.
1+
/**
2+
* Prim’s MST algorithm for adjacency list representation of undirected,
3+
* connected and weighted graphs.
34
*/
45
#include <bits/stdc++.h>
56
using namespace std;
67

7-
typedef pair<int, int> int_pair;
8-
98
class graph {
109
int nv;
1110
// A weighted graph, so need to store a vertex and
1211
// weight pair for every single vertex.
13-
vector<vector<int_pair>> adj;
14-
12+
vector<vector<pair<int, int>>> adj;
1513
public:
1614
graph(int num) : nv(num), adj(num) {}
1715
void addEdge(int u, int v, int wt) {
@@ -24,42 +22,44 @@ class graph {
2422

2523
void graph::prims(int s) {
2624
// Create a min-heap using priority_queue container. priority_queue
27-
// container class provides a constructor that requires two extra
28-
// arguments to make it a min heap i.e. vector<...> & greater<...>.
29-
priority_queue<int_pair, vector<int_pair>, greater<int_pair>> heap;
30-
31-
// A vector for weight and initialise as INT_MAX.
32-
vector<int> weight(nv, INT_MAX);
25+
// container constructor requires two extra arguments to make it a
26+
// min heap i.e. vector<...> & greater<...>.
27+
priority_queue<pair<int, int>, vector<pair<int, int>>,
28+
greater<pair<int, int>> > heap;
3329

30+
// A vector to store weights of vertices with all weights
31+
// intialised as INT_MAX.
32+
vector<int> weight(nv, INT_MAX);
3433
// Another vector to store the parent of vertices.
3534
vector<int> parent(nv, -1);
36-
3735
// A vector to keep track of vertices included in MST.
3836
vector<bool> mst(nv, false);
39-
37+
4038
// Set weight of source vertex as zero.
4139
weight[s] = 0;
40+
4241
// Insert source vertex in the heap.
4342
heap.push(make_pair(weight[s], s));
44-
43+
4544
while (!heap.empty()) {
4645
// Extract the min vertex based on their associated weights.
4746
int u = heap.top().second;
4847
heap.pop();
49-
48+
5049
// Include extracted vertex in mst.
5150
mst[u] = true;
52-
vector<pair<int, int>>::iterator i;
53-
for (i = adj[u].begin(); i != adj[u].end(); ++i) {
54-
// Get the vertex label and the weight
51+
52+
for (auto i = adj[u].begin(); i != adj[u].end(); ++i) {
53+
// Get the vertex label and the weights
5554
int v = (*i).first;
5655
int v_wt = (*i).second;
57-
56+
5857
// If v is not in mst and edge (u, v) weight < weight of v
5958
if (mst[v] == false && v_wt < weight[v]) {
6059
// Update weight of v
6160
weight[v] = v_wt;
6261
heap.push(make_pair(weight[v], v));
62+
// Update parent of v
6363
parent[v] = u;
6464
}
6565
}
@@ -72,14 +72,15 @@ void graph::prims(int s) {
7272

7373
int main() {
7474
graph g(5);
75-
75+
7676
g.addEdge(0, 1, 1);
7777
g.addEdge(0, 2, 3);
7878
g.addEdge(1, 2, 7);
7979
g.addEdge(2, 3, 4);
8080
g.addEdge(3, 4, 6);
8181

8282
g.prims(0);
83-
83+
8484
return 0;
8585
}
86+
// Time complexity : O(E*Log V))

Min_Spanning_Tree/prim_matrix.cc

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/**
2+
* Prim’s MST algorithm for adjacency matrix representation of undirected,
3+
* connected and weighted graphs.
4+
*/
5+
#include <bits/stdc++.h>
6+
using namespace std;
7+
8+
int findMinKeyVertex(vector<int>& k, vector<bool>& mst, int v) {
9+
int min = INT_MAX, index;
10+
for (int i = 0; i < v; i++) {
11+
if (mst[i] == false && k[i] < min) {
12+
min = k[i];
13+
index = i;
14+
}
15+
}
16+
return index;
17+
}
18+
19+
void primsMST(vector<vector<int>>& graph, int v) {
20+
// A vector to store key values of all vertices with all keys
21+
// intialised as INT_MAX.
22+
vector<int> key(v, INT_MAX);
23+
24+
// A boolean vector to represent set of vertices included in MST.
25+
vector<bool> InMST(v, false);
26+
27+
// A vector to store indices of parents in MST.
28+
vector<int> parent(v);
29+
30+
// Starting from vertex 0 so set its key as 0 and as it is the root
31+
// of MST so set its parent as -1.
32+
key[0] = 0;
33+
parent[0] = -1;
34+
35+
for (int i = 0; i < v; i++) {
36+
// Find the min key vertex from the set of vertices
37+
// not included in MST.
38+
int u = findMinKeyVertex(key, InMST, v);
39+
40+
// Mark min key vertex as included in MST.
41+
InMST[u] = true;
42+
43+
// Update keys and parents of the adjacent vertices.
44+
for (int j = 0; j < v; j++) {
45+
if (graph[u][j] && InMST[j] == false
46+
&& graph[u][j] < key[j])
47+
parent[j] = u, key[j] = graph[u][j];
48+
}
49+
}
50+
51+
// Print the edges of MST.
52+
for (int i = 1; i < v; i++)
53+
cout << parent[i] << " - " << i << endl;
54+
}
55+
56+
int main() {
57+
int nv;
58+
cin >> nv;
59+
60+
vector<vector<int>> graph(nv, vector<int>(nv));
61+
62+
for (int i = 0; i < nv; i++)
63+
for (int j = 0; j < nv; j++)
64+
cin >> graph[i][j];
65+
66+
primsMST(graph, nv);
67+
68+
return 0;
69+
}
70+
// Time Complexity : O(V^2). With adjacency list representation, the time
71+
// complexity can be reduced to O(E*logV) with the help of a binary heap.

Representation/adjacency_list.cc

+25-27
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,30 @@
1-
/** Implementation of adjacency list representation of a graph.
2-
* E.g. Consider the following graph
3-
* A --- B
4-
* | \ |
5-
* | \ |
6-
* | \ |
7-
* D --- C
1+
/**
2+
* Adjacency list representation of a graph.
83
*
9-
* Adjacency matrix for this (undirected) graph is:
10-
* 0 -> 1 -> 2 -> 3
11-
* 1 -> 0 -> 2
12-
* 2 -> 0 -> 1 -> 3
13-
* 3 -> 0 -> 2
4+
* E.g. Consider the following graph
5+
* A --- B
6+
* | \ |
7+
* | \ |
8+
* | \ |
9+
* D --- C
10+
*
11+
* Adjacency matrix for this (undirected) graph is:
12+
* 0 -> 1 -> 2 -> 3
13+
* 1 -> 0 -> 2
14+
* 2 -> 0 -> 1 -> 3
15+
* 3 -> 0 -> 2
1416
*/
1517
#include <bits/stdc++.h>
1618
using namespace std;
1719

1820
class graph {
1921
// No. of vertices and edges
20-
int v;
21-
int e;
22-
// A pointer to a vector(or array) of lists
22+
int v, e;
23+
// A vector(or array) of lists
2324
vector<list<int>> adj_list;
2425

2526
public:
26-
graph(int nv, int ne) {
27-
v = nv;
28-
e = ne;
29-
// Resize adj_list to contain v elements
30-
adj_list.resize(v);
31-
}
27+
graph(int nv, int ne) : v(nv), e(ne), adj_list(nv) {}
3228

3329
~graph() {adj_list.clear();}
3430

@@ -65,9 +61,11 @@ int main() {
6561

6662
return 0;
6763
}
68-
/** Pros: Takes space O(|V|+|E|). In the worst case, there can be C(V, 2)
69-
* number of edges in a graph thus consuming O(V^2) space.
70-
*
71-
* Cons: Queries like whether there is an edge from vertex u to vertex v
72-
* are not efficient and take O(V) time. Deleting an edge is also ineffi-
73-
* -cient as it might requires traversing a long list in a large graph.
64+
/**
65+
* Pros: Takes space O(|V|+|E|). In the worst case, there can be C(V, 2)
66+
* number of edges in a graph thus consuming O(V^2) space.
67+
*
68+
* Cons: Queries like whether there is an edge from vertex u to vertex v
69+
* are not efficient and take O(V) time. Deleting an edge is also ineffi-
70+
* -cient as it might requires traversing a long list in a large graph.
71+
*/

0 commit comments

Comments
 (0)