You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: contrib/Data-Structure-Graphs/graph.md
+30-31
Original file line number
Diff line number
Diff line change
@@ -2,21 +2,22 @@
2
2
3
3
Graph is a non-linear data structure consisting of vertices and edges. It is a powerful tool for representing and analyzing complex relationships between objects or entities.
4
4
5
-
## Components of a Graph:
5
+
## Components of a Graph
6
6
7
-
**Vertices:** Vertices are the fundamental units of the graph. Sometimes, vertices are also known as vertex or nodes. Every node/vertex can be labeled or unlabeled.
8
-
**Edges:** Edges are drawn or used to connect two nodes of the graph. It can be ordered pair of nodes in a directed graph. Edges can connect any two nodes in any possible way. There are no rules. very edge can be labelled/unlabelled.
7
+
1.**Vertices:** Vertices are the fundamental units of the graph. Sometimes, vertices are also known as vertex or nodes. Every node/vertex can be labeled or unlabeled.
9
8
10
-
## Basic Operations on Graphs:
9
+
2.**Edges:** Edges are drawn or used to connect two nodes of the graph. It can be ordered pair of nodes in a directed graph. Edges can connect any two nodes in any possible way. There are no rules. very edge can be labelled/unlabelled.
10
+
11
+
## Basic Operations on Graphs
11
12
- Insertion of Nodes/Edges in the graph
12
13
- Deletion of Nodes/Edges in the graph
13
14
- Searching on Graphs
14
15
- Traversal of Graphs
15
16
16
-
## Types of Graph##
17
+
## Types of Graph
17
18
18
19
19
-
**1.Undirected Graph:** In an undirected graph, edges have no direction, and they represent symmetric relationships between nodes. If there is an edge between node A and node B, you can travel from A to B and from B to A.
20
+
**1.Undirected Graph:** In an undirected graph, edges have no direction, and they represent symmetric relationships between nodes. If there is an edge between node A and node B, you can travel from A to B and from B to A.
20
21
21
22
**2. Directed Graph (Digraph):** In a directed graph, edges have a direction, indicating a one-way relationship between nodes. If there is an edge from node A to node B, you can travel from A to B but not necessarily from B to A.
22
23
@@ -28,10 +29,10 @@ Graph is a non-linear data structure consisting of vertices and edges. It is a p
28
29
29
30
**6. Tree:** A tree is a special type of acyclic graph where each node has a unique parent except for the root node, which has no parent. Trees have a hierarchical structure and are frequently used in data structures like binary trees or decision trees.
30
31
31
-
## Representation of Graphs:##
32
+
## Representation of Graphs
32
33
There are two ways to store a graph:
33
34
34
-
**1. Adjacency Matrix**
35
+
1.**Adjacency Matrix**
35
36
In this method, the graph is stored in the form of the 2D matrix where rows and columns denote vertices. Each entry in the matrix represents the weight of the edge between those vertices.
36
37
37
38
```python
@@ -63,8 +64,8 @@ for row in adj_matrix:
63
64
64
65
```
65
66
66
-
**2. Adjacency List**
67
-
This graph is represented as a collection of linked lists. There is an array of pointer which points to the edges connected to that vertex.
67
+
2.**Adjacency List**
68
+
In this method, the graph is represented as a collection of linked lists. There is an array of pointer which points to the edges connected to that vertex.
68
69
69
70
```python
70
71
defcreate_adjacency_list(edges, num_vertices):
@@ -85,31 +86,31 @@ if __name__ == "__main__":
85
86
for i inrange(num_vertices):
86
87
print(f"{i} -> {''.join(map(str, adj_list[i]))}")
87
88
```
88
-
`Output
89
-
0 -> 1 2
90
-
1 -> 0 2 3
91
-
2 -> 0 1 3
92
-
3 -> 2 1 `
89
+
`Output`
90
+
`0 -> 1 2`
91
+
`1 -> 0 2 3`
92
+
`2 -> 0 1 3`
93
+
`3 -> 2 1 `
93
94
94
95
95
96
96
-
# Traversal Techniques#
97
+
# Traversal Techniques
97
98
98
-
## Breadth First Search (BFS)##
99
+
## Breadth First Search (BFS)
99
100
- It is a graph traversal algorithm that explores all the vertices in a graph at the current depth before moving on to the vertices at the next depth level.
100
101
- It starts at a specified vertex and visits all its neighbors before moving on to the next level of neighbors.
101
102
BFS is commonly used in algorithms for pathfinding, connected components, and shortest path problems in graphs.
102
103
103
104
**Steps of BFS algorithms**
104
105
105
106
106
-
-**Step1:** Initially queue and visited arrays are empty.
107
-
-**Step2:** Push node 0 into queue and mark it visited.
107
+
-**Step 1:** Initially queue and visited arrays are empty.
108
+
-**Step 2:** Push node 0 into queue and mark it visited.
108
109
-**Step 3:** Remove node 0 from the front of queue and visit the unvisited neighbours and push them into queue.
109
110
-**Step 4:** Remove node 1 from the front of queue and visit the unvisited neighbours and push them into queue.
110
111
-**Step 5:** Remove node 2 from the front of queue and visit the unvisited neighbours and push them into queue.
111
112
-**Step 6:** Remove node 3 from the front of queue and visit the unvisited neighbours and push them into queue.
112
-
-**Steps 7:** Remove node 4 from the front of queue and visit the unvisited neighbours and push them into queue.
113
+
-**Step 7:** Remove node 4 from the front of queue and visit the unvisited neighbours and push them into queue.
113
114
114
115
```python
115
116
@@ -154,16 +155,17 @@ if __name__ == "__main__": #Output : Breadth First Traversal 0 1 2 3 4
154
155
155
156
```
156
157
157
-
**Time Complexity:**`O(V+E)`, where V is the number of nodes and E is the number of edges.
158
-
**Auxiliary Space:**`O(V)`
158
+
-**Time Complexity:**`O(V+E)`, where V is the number of nodes and E is the number of edges.
159
+
-**Auxiliary Space:**`O(V)`
159
160
160
161
161
-
## Depth-first search##
162
+
## Depth-first search
162
163
163
164
Depth-first search is an algorithm for traversing or searching tree or graph data structures. The algorithm starts at the root node (selecting some arbitrary node as the root node in the case of a graph) and explores as far as possible along each branch before backtracking.
164
165
165
166
**Steps of DFS algorithms**
166
-
-**Step1:** Initially stack and visited arrays are empty.
167
+
168
+
-**Step 1:** Initially stack and visited arrays are empty.
167
169
-**Step 2:** Visit 0 and put its adjacent nodes which are not visited yet into the stack.
168
170
-**Step 3:** Now, Node 1 at the top of the stack, so visit node 1 and pop it from the stack and put all of its adjacent nodes which are not visited in the stack.
169
171
-**Step 4:** Now, Node 2 at the top of the stack, so visit node 2 and pop it from the stack and put all of its adjacent nodes which are not visited (i.e, 3, 4) in the stack.
@@ -178,14 +180,12 @@ from collections import defaultdict
178
180
classGraph:
179
181
180
182
def__init__(self):
181
-
182
183
self.graph = defaultdict(list)
183
184
184
185
defaddEdge(self, u, v):
185
186
self.graph[u].append(v)
186
187
187
188
defDFSUtil(self, v, visited):
188
-
189
189
visited.add(v)
190
190
print(v, end='')
191
191
@@ -194,9 +194,7 @@ class Graph:
194
194
self.DFSUtil(neighbour, visited)
195
195
196
196
defDFS(self, v):
197
-
198
197
visited =set()
199
-
200
198
self.DFSUtil(v, visited)
201
199
202
200
if__name__=="__main__":
@@ -208,12 +206,13 @@ if __name__ == "__main__":
208
206
g.addEdge(2, 3)
209
207
g.addEdge(3, 3)
210
208
211
-
print("Depth First Traversal (starting from vertex 2): ",g.DFS(2)) #Output: Depth First Traversal (starting from vertex 2): 2 0 1 3
209
+
print("Depth First Traversal (starting from vertex 2): ",g.DFS(2))
212
210
213
211
```
212
+
`Output: Depth First Traversal (starting from vertex 2): 2 0 1 3 `
214
213
215
-
**Time complexity:**`O(V + E)`, where V is the number of vertices and E is the number of edges in the graph.
216
-
**Auxiliary Space:**`O(V + E)`, since an extra visited array of size V is required, And stack size for iterative call to DFS function.
214
+
-**Time complexity:**`O(V + E)`, where V is the number of vertices and E is the number of edges in the graph.
215
+
-**Auxiliary Space:**`O(V + E)`, since an extra visited array of size V is required, And stack size for iterative call to DFS function.
0 commit comments