Skip to content

Commit a9b7eba

Browse files
authored
Merge pull request #1525 from dhairyagothi/main
DSA graphs added
2 parents 35d9cad + f622d65 commit a9b7eba

File tree

3 files changed

+309
-0
lines changed

3 files changed

+309
-0
lines changed

docs/dsa/graphs/_category_.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"label": "Graphs",
3+
"position": 14,
4+
"link": {
5+
"type": "generated-index",
6+
"description": "Graphs are a data structure that represents a collection of interconnected nodes or vertices. They are commonly used in Data Structures and Algorithms (DSA) to model relationships between objects or entities."
7+
}
8+
}

docs/dsa/graphs/graphs.md

Lines changed: 301 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,301 @@
1+
2+
3+
# Graphs in Data Structures and Algorithms (DSA)
4+
5+
Graphs in Data Structures and Algorithms (DSA) are a non-linear data structure that consists of a set of vertices (nodes) connected by edges. They are widely used to represent relationships between objects or entities.
6+
7+
![alt text](image.png)
8+
9+
## Implementing a Graph using an Adjacency Matrix
10+
11+
To implement a graph, you can use various data structures such as an adjacency matrix or an adjacency list.
12+
13+
An adjacency matrix is a 2D array where each cell represents the presence or absence of an edge between two vertices. It requires O(V^2) space, where V is the number of vertices.
14+
15+
Here's an example of implementing a graph using an adjacency matrix in Python:
16+
17+
```python
18+
class Graph:
19+
def __init__(self, num_vertices):
20+
self.num_vertices = num_vertices
21+
self.adj_matrix = [[0] * num_vertices for _ in range(num_vertices)]
22+
23+
def add_edge(self, src, dest):
24+
self.adj_matrix[src][dest] = 1
25+
self.adj_matrix[dest][src] = 1
26+
27+
def remove_edge(self, src, dest):
28+
self.adj_matrix[src][dest] = 0
29+
self.adj_matrix[dest][src] = 0
30+
31+
def print_graph(self):
32+
for row in self.adj_matrix:
33+
print(row)
34+
35+
# Example usage:
36+
g = Graph(4)
37+
g.add_edge(0, 1)
38+
g.add_edge(1, 2)
39+
g.add_edge(2, 3)
40+
g.print_graph()
41+
```
42+
43+
Output:
44+
```plaintext
45+
[0, 1, 0, 0]
46+
[1, 0, 1, 0]
47+
[0, 1, 0, 1]
48+
[0, 0, 1, 0]
49+
```
50+
51+
## Implementing a Graph using an Adjacency List
52+
53+
Another way to implement a graph is using an adjacency list. It is a collection of linked lists, where each vertex has a list of its adjacent vertices. It requires O(V + E) space, where V is the number of vertices and E is the number of edges.
54+
55+
Here's an example of implementing a graph using an adjacency list in Python:
56+
57+
```python
58+
class Graph:
59+
def __init__(self, num_vertices):
60+
self.num_vertices = num_vertices
61+
self.adj_list = [[] for _ in range(num_vertices)]
62+
63+
def add_edge(self, src, dest):
64+
self.adj_list[src].append(dest)
65+
self.adj_list[dest].append(src)
66+
67+
def remove_edge(self, src, dest):
68+
self.adj_list[src].remove(dest)
69+
self.adj_list[dest].remove(src)
70+
71+
def print_graph(self):
72+
for vertex, adj_vertices in enumerate(self.adj_list):
73+
print(f"Vertex {vertex}: {adj_vertices}")
74+
75+
# Example usage:
76+
g = Graph(4)
77+
g.add_edge(0, 1)
78+
g.add_edge(1, 2)
79+
g.add_edge(2, 3)
80+
g.print_graph()
81+
```
82+
83+
Output:
84+
```plaintext
85+
Vertex 0: [1]
86+
Vertex 1: [0, 2]
87+
Vertex 2: [1, 3]
88+
Vertex 3: [2]
89+
```
90+
91+
## Additional Operations on Graphs
92+
93+
These are just basic operations on graphs. Depending on your requirements, you can perform various other operations like finding a path between two vertices, checking for cycles, or finding the shortest path using algorithms like Depth-First Search (DFS) or Breadth-First Search (BFS).
94+
95+
96+
Graphs in Data Structures and Algorithms (DSA) are a non-linear data structure that consists of a set of vertices (nodes) connected by edges. They are widely used to represent relationships between objects or entities.
97+
98+
To implement a graph, you can use various data structures such as an adjacency matrix or an adjacency list.
99+
100+
An adjacency matrix is a 2D array where each cell represents the presence or absence of an edge between two vertices. It requires O(V^2) space, where V is the number of vertices.
101+
102+
Here's an example of implementing a graph using an adjacency matrix in Python:
103+
104+
```python
105+
class Graph:
106+
def __init__(self, num_vertices):
107+
self.num_vertices = num_vertices
108+
self.adj_matrix = [[0] * num_vertices for _ in range(num_vertices)]
109+
110+
def add_edge(self, src, dest):
111+
self.adj_matrix[src][dest] = 1
112+
self.adj_matrix[dest][src] = 1
113+
114+
def remove_edge(self, src, dest):
115+
self.adj_matrix[src][dest] = 0
116+
self.adj_matrix[dest][src] = 0
117+
118+
def print_graph(self):
119+
for row in self.adj_matrix:
120+
print(row)
121+
122+
# Example usage:
123+
g = Graph(4)
124+
g.add_edge(0, 1)
125+
g.add_edge(1, 2)
126+
g.add_edge(2, 3)
127+
g.print_graph()
128+
```
129+
130+
Output:
131+
```
132+
[0, 1, 0, 0]
133+
[1, 0, 1, 0]
134+
[0, 1, 0, 1]
135+
[0, 0, 1, 0]
136+
```
137+
138+
Another way to implement a graph is using an adjacency list. It is a collection of linked lists, where each vertex has a list of its adjacent vertices. It requires O(V + E) space, where V is the number of vertices and E is the number of edges.
139+
140+
Here's an example of implementing a graph using an adjacency list in Python:
141+
142+
```python
143+
class Graph:
144+
def __init__(self, num_vertices):
145+
self.num_vertices = num_vertices
146+
self.adj_list = [[] for _ in range(num_vertices)]
147+
148+
def add_edge(self, src, dest):
149+
self.adj_list[src].append(dest)
150+
self.adj_list[dest].append(src)
151+
152+
def remove_edge(self, src, dest):
153+
self.adj_list[src].remove(dest)
154+
self.adj_list[dest].remove(src)
155+
156+
def print_graph(self):
157+
for vertex, adj_vertices in enumerate(self.adj_list):
158+
print(f"Vertex {vertex}: {adj_vertices}")
159+
160+
# Example usage:
161+
g = Graph(4)
162+
g.add_edge(0, 1)
163+
g.add_edge(1, 2)
164+
g.add_edge(2, 3)
165+
g.print_graph()
166+
```
167+
168+
Output:
169+
```
170+
Vertex 0: [1]
171+
Vertex 1: [0, 2]
172+
Vertex 2: [1, 3]
173+
Vertex 3: [2]
174+
```
175+
176+
These are just basic operations on graphs. Depending on your requirements, you can perform various other operations like finding a path between two vertices, checking for cycles, or finding the shortest path using algorithms like Depth-First Search (DFS) or Breadth-First Search (BFS).
177+
178+
## Implementing a Graph in Java
179+
180+
To implement a graph in Java, you can use similar approaches as in Python. Here's an example of implementing a graph using an adjacency matrix in Java:
181+
182+
```java
183+
public class Graph {
184+
private int numVertices;
185+
private int[][] adjMatrix;
186+
187+
public Graph(int numVertices) {
188+
this.numVertices = numVertices;
189+
this.adjMatrix = new int[numVertices][numVertices];
190+
}
191+
192+
public void addEdge(int src, int dest) {
193+
adjMatrix[src][dest] = 1;
194+
adjMatrix[dest][src] = 1;
195+
}
196+
197+
public void removeEdge(int src, int dest) {
198+
adjMatrix[src][dest] = 0;
199+
adjMatrix[dest][src] = 0;
200+
}
201+
202+
public void printGraph() {
203+
for (int i = 0; i < numVertices; i++) {
204+
for (int j = 0; j < numVertices; j++) {
205+
System.out.print(adjMatrix[i][j] + " ");
206+
}
207+
System.out.println();
208+
}
209+
}
210+
211+
public static void main(String[] args) {
212+
Graph g = new Graph(4);
213+
g.addEdge(0, 1);
214+
g.addEdge(1, 2);
215+
g.addEdge(2, 3);
216+
g.printGraph();
217+
}
218+
}
219+
```
220+
221+
Output:
222+
```
223+
0 1 0 0
224+
1 0 1 0
225+
0 1 0 1
226+
0 0 1 0
227+
```
228+
229+
## Implementing a Graph in C++
230+
231+
Similarly, you can implement a graph in C++ using an adjacency matrix. Here's an example:
232+
233+
```cpp
234+
#include <iostream>
235+
#include <vector>
236+
237+
using namespace std;
238+
239+
class Graph {
240+
private:
241+
int numVertices;
242+
vector<vector<int>> adjMatrix;
243+
244+
public:
245+
Graph(int numVertices) {
246+
this->numVertices = numVertices;
247+
this->adjMatrix.resize(numVertices, vector<int>(numVertices, 0));
248+
}
249+
250+
void addEdge(int src, int dest) {
251+
adjMatrix[src][dest] = 1;
252+
adjMatrix[dest][src] = 1;
253+
}
254+
255+
void removeEdge(int src, int dest) {
256+
adjMatrix[src][dest] = 0;
257+
adjMatrix[dest][src] = 0;
258+
}
259+
260+
void printGraph() {
261+
for (int i = 0; i < numVertices; i++) {
262+
for (int j = 0; j < numVertices; j++) {
263+
cout << adjMatrix[i][j] << " ";
264+
}
265+
cout << endl;
266+
}
267+
}
268+
};
269+
270+
int main() {
271+
Graph g(4);
272+
g.addEdge(0, 1);
273+
g.addEdge(1, 2);
274+
g.addEdge(2, 3);
275+
g.printGraph();
276+
277+
return 0;
278+
}
279+
```
280+
281+
Output:
282+
```
283+
0 1 0 0
284+
1 0 1 0
285+
0 1 0 1
286+
0 0 1 0
287+
```
288+
289+
Remember to adjust the number of vertices and edges according to your requirements.
290+
291+
In conclusion, graphs are a fundamental data structure in Data Structures and Algorithms (DSA) that are used to represent relationships between objects or entities. They can be implemented using various data structures such as an adjacency matrix or an adjacency list.
292+
293+
An adjacency matrix is a 2D array that represents the presence or absence of edges between vertices. It requires O(V^2) space, where V is the number of vertices. On the other hand, an adjacency list is a collection of linked lists where each vertex has a list of its adjacent vertices. It requires O(V + E) space, where V is the number of vertices and E is the number of edges.
294+
295+
Both implementations have their own advantages and disadvantages. The choice of implementation depends on the specific requirements of the problem at hand.
296+
297+
Additionally, there are various other operations that can be performed on graphs, such as finding a path between two vertices, checking for cycles, or finding the shortest path using algorithms like Depth-First Search (DFS) or Breadth-First Search (BFS).
298+
299+
Graphs can also be implemented in other programming languages like Java and C++. The implementation follows similar approaches using either an adjacency matrix or an adjacency list.
300+
301+
Overall, understanding graphs and their implementations is crucial for solving problems that involve relationships and connectivity between entities.

docs/dsa/graphs/image.png

71.5 KB
Loading

0 commit comments

Comments
 (0)