Skip to content

Commit ddd03dd

Browse files
Updated Readme.
1 parent a17cd74 commit ddd03dd

File tree

3 files changed

+67
-7
lines changed

3 files changed

+67
-7
lines changed

Graphs/graphAM.py renamed to Graph/graph.py

+60-4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@
88

99
class Graph:
1010
def __init__(self, vertices, graph_type='directed', weighted = False):
11+
'''
12+
Initialises Adjacent Matrix with Zeros and other class variables.
13+
14+
Parameters:
15+
vertices -- number of vertices
16+
graph_type -- 'directed' or 'undirected'
17+
weighted -- True or False
18+
'''
1119
self._vertices = vertices
1220
self._type = graph_type
1321
self._weighted = weighted
@@ -16,22 +24,39 @@ def __init__(self, vertices, graph_type='directed', weighted = False):
1624
self._visited = [False] * self._vertices
1725

1826
def insert_edge(self, u, v, weight=1):
27+
'''
28+
Adds an edge between the passed vertices (u,v) by allocating the weight at that index position.
29+
If the graph is 'undirected', then same weights are also copied to (v,u).
30+
'''
1931
self._adjMAT[u][v] = weight
2032
if self._type == 'undirected':
2133
self._adjMAT[v][u] = weight
2234

2335
def remove_edge(self, u, v):
36+
'''
37+
Removes an edge between the passed vertices (u,v) by making that index position as 0.
38+
If the graph is 'undirected', then 0 is also copied to (v,u).
39+
'''
2440
self._adjMAT[u][v] = 0
2541
if self._type == 'undirected':
2642
self._adjMAT[v][u] = 0
2743

2844
def exist_edge(self, u, v):
45+
'''
46+
Returns True if edge exists between vertices (u,v), else False.
47+
'''
2948
return self._adjMAT[u][v] != 0
3049

3150
def vertex_count(self):
51+
'''
52+
Returns number of vertices present in the Graph.
53+
'''
3254
return self._vertices
3355

3456
def edge_count(self):
57+
'''
58+
Returns number of edges present in the Graph.
59+
'''
3560
count = 0
3661
for i in range(self._vertices):
3762
for j in range(self._vertices):
@@ -40,11 +65,17 @@ def edge_count(self):
4065
return count
4166

4267
def vertices(self):
68+
'''
69+
Prints all the vertices.
70+
'''
4371
for i in range(self._vertices):
4472
print(i, end=' ')
4573
print()
4674

4775
def edges(self):
76+
'''
77+
Prints all the edges with weights if the graph is undirected.
78+
'''
4879
for i in range(self._vertices):
4980
for j in range(self._vertices):
5081
if self._adjMAT[i][j] != 0 and self._weighted == True:
@@ -53,21 +84,30 @@ def edges(self):
5384
print(f'{i} -- {j}')
5485

5586
def outdegree(self, v):
87+
'''
88+
Returns the outdegree of the passed vertex v.
89+
'''
5690
count = 0
5791
for j in range(self._vertices):
5892
if self._adjMAT[v][j] != 0:
5993
count += 1
6094
return count
6195

6296
def indegree(self, v):
97+
'''
98+
Returns the indegree of the passed vertex v.
99+
'''
63100
count = 0
64101
for i in range(self._vertices):
65102
if self._adjMAT[i][v] != 0:
66103
count += 1
67104
return count
68105

69-
def BFS(self, start_vertext):
70-
i = start_vertext
106+
def BFS(self, start_vertex):
107+
'''
108+
Breadth First Search
109+
'''
110+
i = start_vertex
71111
q = Queue()
72112
visited = [False] * self._vertices
73113
print(i, end=' ')
@@ -83,6 +123,9 @@ def BFS(self, start_vertext):
83123
q.enqueue(j)
84124

85125
def DFS_iterative(self, start_vertex):
126+
'''
127+
Depth First Search using Stack.
128+
'''
86129
i = start_vertex
87130
s = Stack()
88131
visited = [False] * self._vertices
@@ -98,6 +141,9 @@ def DFS_iterative(self, start_vertex):
98141
s.push(j)
99142

100143
def DFS_recursive(self, start_vertex):
144+
'''
145+
Depth First Search using Recursion.
146+
'''
101147
if self._visited[start_vertex] == False:
102148
print(start_vertex, end=' ')
103149
self._visited[start_vertex] = True
@@ -107,8 +153,10 @@ def DFS_recursive(self, start_vertex):
107153
self.DFS_recursive(j)
108154

109155
def display(self):
156+
'''
157+
Displays Adjacency Matrix.
158+
'''
110159
n_edges = self.edge_count()
111-
112160
if self._type == 'undirected':
113161
n_edges = int(n_edges / 2)
114162

@@ -142,9 +190,17 @@ def display(self):
142190

143191
g.insert_edge(6, 3, 70)
144192

193+
print("\nAdjacency Matrix")
145194
g.display()
195+
196+
print("\nEdges")
146197
g.edges()
147-
print("DFS Iterative:")
198+
199+
print("\nBFS:")
200+
g.BFS(0)
201+
202+
print("\nDFS Iterative:")
148203
g.DFS_iterative(0)
204+
149205
print("\nDFS Iterative:")
150206
g.DFS_recursive(0)
File renamed without changes.

README.md

+7-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Find the detailed description on [Index](Sorting%20Algorithms/) page.
1515

1616
## Data Structures
1717

18-
### 1. Linked Lists
18+
### 1. Linked List
1919

2020
Find the detailed description on operations performed [here.](LinkedList/)
2121

@@ -33,7 +33,7 @@ Find the detailed description on operations performed [here.](LinkedList/)
3333
1. [Queue using inbuilt Python List](Queues/queues.py)
3434
2. [Queue using Linked List](Queues/queuesLL.py)
3535

36-
### 4. Binary Trees (using Linked List)
36+
### 4. Binary Tree (using Linked List)
3737

3838
Find the detailed description on operations performed [here.](BinaryTrees/)
3939

@@ -49,4 +49,8 @@ Find the detailed description on operations performed [here.](BinaryTrees/)
4949
Find the detailed description on operations performed [here.](Hashing/)
5050

5151
1. [Hashing using Chaining](Hashing/hashingChaining.py)
52-
2. [Hashing using Linear, Quadractic Probing and Double Hashing](Hashing/hashingStrategies.py)
52+
2. [Hashing using Linear, Quadractic Probing and Double Hashing](Hashing/hashingStrategies.py)
53+
54+
### 7. Graph
55+
56+
1. [Graph using Adjacency Matrix](Graph/graph.py)

0 commit comments

Comments
 (0)