Skip to content

Commit 0db9cef

Browse files
committed
g
1 parent 26053d5 commit 0db9cef

File tree

1 file changed

+30
-31
lines changed

1 file changed

+30
-31
lines changed

contrib/Data-Structure-Graphs/graph.md

+30-31
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,22 @@
22

33
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.
44

5-
## Components of a Graph:
5+
## Components of a Graph
66

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.
98

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
1112
- Insertion of Nodes/Edges in the graph
1213
- Deletion of Nodes/Edges in the graph
1314
- Searching on Graphs
1415
- Traversal of Graphs
1516

16-
## Types of Graph ##
17+
## Types of Graph
1718

1819

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.
2021

2122
**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.
2223

@@ -28,10 +29,10 @@ Graph is a non-linear data structure consisting of vertices and edges. It is a p
2829

2930
**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.
3031

31-
## Representation of Graphs:##
32+
## Representation of Graphs
3233
There are two ways to store a graph:
3334

34-
**1. Adjacency Matrix**
35+
1. **Adjacency Matrix**
3536
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.
3637

3738
```python
@@ -63,8 +64,8 @@ for row in adj_matrix:
6364

6465
```
6566

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.
6869

6970
```python
7071
def create_adjacency_list(edges, num_vertices):
@@ -85,31 +86,31 @@ if __name__ == "__main__":
8586
for i in range(num_vertices):
8687
print(f"{i} -> {' '.join(map(str, adj_list[i]))}")
8788
```
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 `
9394

9495

9596

96-
# Traversal Techniques #
97+
# Traversal Techniques
9798

98-
## Breadth First Search (BFS) ##
99+
## Breadth First Search (BFS)
99100
- 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.
100101
- It starts at a specified vertex and visits all its neighbors before moving on to the next level of neighbors.
101102
BFS is commonly used in algorithms for pathfinding, connected components, and shortest path problems in graphs.
102103

103104
**Steps of BFS algorithms**
104105

105106

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.
108109
- **Step 3:** Remove node 0 from the front of queue and visit the unvisited neighbours and push them into queue.
109110
- **Step 4:** Remove node 1 from the front of queue and visit the unvisited neighbours and push them into queue.
110111
- **Step 5:** Remove node 2 from the front of queue and visit the unvisited neighbours and push them into queue.
111112
- **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.
113114

114115
```python
115116

@@ -154,16 +155,17 @@ if __name__ == "__main__": #Output : Breadth First Traversal 0 1 2 3 4
154155

155156
```
156157

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)`
159160

160161

161-
## Depth-first search ##
162+
## Depth-first search
162163

163164
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.
164165

165166
**Steps of DFS algorithms**
166-
- **Step1:** Initially stack and visited arrays are empty.
167+
168+
- **Step 1:** Initially stack and visited arrays are empty.
167169
- **Step 2:** Visit 0 and put its adjacent nodes which are not visited yet into the stack.
168170
- **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.
169171
- **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
178180
class Graph:
179181

180182
def __init__(self):
181-
182183
self.graph = defaultdict(list)
183184

184185
def addEdge(self, u, v):
185186
self.graph[u].append(v)
186187

187188
def DFSUtil(self, v, visited):
188-
189189
visited.add(v)
190190
print(v, end=' ')
191191

@@ -194,9 +194,7 @@ class Graph:
194194
self.DFSUtil(neighbour, visited)
195195

196196
def DFS(self, v):
197-
198197
visited = set()
199-
200198
self.DFSUtil(v, visited)
201199

202200
if __name__ == "__main__":
@@ -208,12 +206,13 @@ if __name__ == "__main__":
208206
g.addEdge(2, 3)
209207
g.addEdge(3, 3)
210208

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))
212210

213211
```
212+
`Output: Depth First Traversal (starting from vertex 2): 2 0 1 3 `
214213

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.
217216

218217
</br>
219218

0 commit comments

Comments
 (0)