Skip to content

Commit ab7ad40

Browse files
author
whoparthgarg
committed
graphs
1 parent c99e2cb commit ab7ad40

12 files changed

+243
-175
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
int main()
5+
{
6+
/*
7+
8+
V : vertices, E : edges
9+
10+
1.Directed Graph :
11+
12+
sum of indegrees = E
13+
sum of outdegrees = E
14+
maximum number of edges = V * ( V - 1 )
15+
16+
2.Undirected Graph :
17+
18+
sum of degrees = 2*E
19+
maximum number of edges = ( V * ( V - 1 ) ) / 2
20+
21+
3.Some common graph terms :
22+
23+
a. walk : A walk is a sequence of vertices and edges of a graph i.e.
24+
if we traverse a graph then we get a walk.
25+
Vertex can be repeated
26+
Edges can’t be repeated
27+
28+
b. path : A special walk in which Vertex and Edges can't be repeated.
29+
30+
c. cyclic : A special walk which begins and ends at same vertex.
31+
32+
d. acyclic : A graph which doesn't contain a cycle.
33+
34+
*/
35+
}

17. Graphs/01_Adjacency_List_Representaion.cpp

Lines changed: 0 additions & 43 deletions
This file was deleted.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
int main()
5+
{
6+
/*
7+
8+
Adjacency Matrix :
9+
10+
A B C
11+
-----------------
12+
A 0 1 1
13+
14+
B 1 0 1
15+
16+
C 1 1 0
17+
18+
1. size of matrix : (V * V)
19+
20+
2. for undirected graph : matrix is symmetric
21+
22+
3. 1 represents there is edge
23+
0 represents there is no edge
24+
25+
4. handle arbitrary vertices names with hash table eg: m["ABC"] = 2
26+
27+
5. Properties :
28+
29+
a. space required O(V*V)
30+
31+
b. check u and v adjacent O(V)
32+
33+
c. find all vertices adjacent to u O(V)
34+
35+
d. find degree of u O(V)
36+
37+
e. add/remove edge O(1)
38+
39+
f. add/remove vertex O(V*V)
40+
41+
*/
42+
43+
return 0;
44+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
int main()
5+
{
6+
/*
7+
8+
Adjacency List :
9+
10+
11+
0 - 1-->2
12+
1 - 0-->2
13+
2 - 0-->1-->3
14+
3 - 2
15+
16+
Properties :
17+
18+
a. space required O(V+E)
19+
20+
1. undirected O(V + 2 * E)
21+
2. directed O(V + E)
22+
23+
b. check u and v adjacent O(V)
24+
25+
c. find all vertices adjacent to u O(degree(u))
26+
27+
d. find degree of u O(1)
28+
29+
e. add edge O(1)
30+
31+
f. remove edge O(V)
32+
33+
*/
34+
35+
return 0;
36+
}

17. Graphs/03a DFS.cpp

Lines changed: 0 additions & 46 deletions
This file was deleted.
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
//Adjacenecy Matrix
5+
class Graph
6+
{
7+
bool **adj;
8+
int v;
9+
10+
public:
11+
12+
//constructor
13+
Graph(int vertices)
14+
{
15+
v = vertices;
16+
adj = new bool *[v];
17+
18+
//intialise matrix
19+
for (int i = 0; i < v; i++)
20+
{
21+
adj[i] = new bool[v];
22+
for (int j = 0; j < v; j++)
23+
{
24+
adj[i][j] = false;
25+
}
26+
}
27+
}
28+
29+
//add edge
30+
void addEdge(int i, int j)
31+
{
32+
adj[i][j] = true;
33+
adj[j][i] = true;
34+
}
35+
36+
//remove edge
37+
void removeEdge(int i, int j)
38+
{
39+
adj[i][j] = false;
40+
adj[j][i] = false;
41+
}
42+
43+
//print matrix
44+
void print()
45+
{
46+
for (int i = 0; i < v; i++)
47+
{
48+
cout << i << " : ";
49+
for (int j = 0; j < v; j++)
50+
{
51+
cout << adj[i][j] << " ";
52+
}
53+
cout << endl;
54+
}
55+
}
56+
};
57+
58+
int main()
59+
{
60+
Graph g(4);
61+
62+
g.addEdge(0, 1);
63+
g.addEdge(0, 2);
64+
g.addEdge(1, 2);
65+
g.addEdge(2, 0);
66+
g.addEdge(2, 3);
67+
68+
g.print();
69+
return 0;
70+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
//Adjacency List
5+
void addEdge(vector<int> adj[], int u, int v)
6+
{
7+
adj[u].push_back(v);
8+
adj[v].push_back(u);
9+
}
10+
11+
void print(vector<int> adj[], int v)
12+
{
13+
for (int i = 0; i < v; i++)
14+
{
15+
cout << i << " : ";
16+
for (auto x : adj[i])
17+
{
18+
cout << x << " ";
19+
}
20+
cout << endl;
21+
}
22+
}
23+
24+
int main()
25+
{
26+
int v = 4;
27+
vector<int> adj[v];
28+
29+
addEdge(adj, 0, 1);
30+
addEdge(adj, 0, 2);
31+
addEdge(adj, 1, 2);
32+
addEdge(adj, 1, 3);
33+
34+
print(adj, v);
35+
return 0;
36+
}

17. Graphs/04 Topological Sorting.cpp

Lines changed: 0 additions & 62 deletions
This file was deleted.

0 commit comments

Comments
 (0)