Skip to content

Commit 649c6c0

Browse files
Graphs in Python
0 parents  commit 649c6c0

File tree

36 files changed

+4799
-0
lines changed

36 files changed

+4799
-0
lines changed

graphs/README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Data Structures and Algorithms (DSA) In Python
2+
3+
This repository is for the students of book [Data Structures and Algorithms In Python](http://coursegalaxy.com/) and online courses available on CourseGalaxy. It contains the source code of all the programs used in the book and courses.
4+
5+
### About the DSA Masterclass Courses
6+
* Thoroughly detailed courses with complete working programs
7+
* Contains lots of animations to help you visualize the concepts
8+
* Includes various Data Structures and Algorithms
9+
* Builds a solid foundation in Data Structures and Algorithms
10+
* Prepares you for coding interviews
11+
* Lifetime Access
12+
13+
### DSA Masterclass Courses on [CourseGalaxy](http://coursegalaxy.com/)
14+
15+
[![data-structures-algorithms-python](https://user-images.githubusercontent.com/96913690/200234827-86aec10a-bfab-4371-91fc-e2be855ff1ff.jpg)](https://coursegalaxy.usefedora.com/p/data-structures-algorithms-python?coupon_code=GITHUB75PER)
16+
[![data-structures-algorithms-java](https://user-images.githubusercontent.com/96913690/200234744-14a5ed97-085f-44f3-9298-979c2053c580.jpg)](https://coursegalaxy.usefedora.com/p/data-structures-algorithms-java?coupon_code=GITHUB75PER)
17+
[![data-structures-algorithms-c](https://user-images.githubusercontent.com/96913690/200234592-25d33957-0e9e-4cc0-b324-2a73325aca85.jpg)](https://coursegalaxy.usefedora.com/p/data-structures-algorithms-c?coupon_code=GITHUB75PER)
18+
[![data-structures-algorithms-csharp](https://user-images.githubusercontent.com/96913690/200234905-67b85dfd-20c4-4f4b-afd2-e10d3568fff8.jpg)](https://coursegalaxy.usefedora.com/p/data-structures-algorithms-csharp?coupon_code=GITHUB75PER)
19+
20+
## Copyright
21+
© Copyright Suresh Kumar Srivastava : All rights reserved.
22+
Not to be used for commercial purposes.
Lines changed: 323 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,323 @@
1+
# Copyright (C) Suresh Kumar Srivastava - All Rights Reserved
2+
# DSA Masterclass courses are available on CourseGalaxy.com
3+
4+
# directed_graph_list.py : Program for directed graph using adjacency list.
5+
6+
class VertexNode:
7+
def __init__(self, name):
8+
self.name = name
9+
self.next_vertex = None
10+
self.first_edge = None
11+
12+
class EdgeNode:
13+
def __init__(self, v):
14+
self.end_vertex = v
15+
self.next_edge = None
16+
17+
class DirectedGraphList:
18+
def __init__(self):
19+
self._nvertices = 0
20+
self._nedges = 0
21+
self._start = None
22+
23+
def insert_vertex(self, vertex_name):
24+
ptr = None
25+
temp = None
26+
vertex_found = False
27+
28+
ptr = self._start
29+
30+
if ptr == None:
31+
temp = VertexNode(vertex_name)
32+
self._start = temp
33+
self._nvertices += 1
34+
else:
35+
while ptr.next_vertex != None:
36+
if ptr.name == vertex_name:
37+
vertex_found = True
38+
break
39+
ptr = ptr.next_vertex
40+
41+
if vertex_found or ptr.name==vertex_name:
42+
print("Vertex already present")
43+
else:
44+
temp = VertexNode(vertex_name)
45+
ptr.next_vertex = temp
46+
self._nvertices += 1
47+
48+
def _find_vertex(self, vertex_name):
49+
ptr = self._start
50+
51+
while ptr != None:
52+
if ptr.name == vertex_name:
53+
break
54+
55+
ptr = ptr.next_vertex
56+
57+
return ptr
58+
59+
def insert_edge(self, source, destination):
60+
u = None
61+
v = None
62+
edge_ptr = None
63+
temp = None
64+
65+
edge_found = False
66+
67+
if source == destination:
68+
print("Invalid Edge : source and destination vertices are same")
69+
else:
70+
u = self._find_vertex(source)
71+
v = self._find_vertex(destination)
72+
73+
if u == None:
74+
print("Source vertex not present, first insert vertex " + source)
75+
elif v == None:
76+
print("Destination vertex not present, first insert vertex " + destination)
77+
else:
78+
if u.first_edge == None:
79+
temp = EdgeNode(v)
80+
u.first_edge = temp
81+
self._nedges += 1
82+
else:
83+
edge_ptr = u.first_edge
84+
85+
while edge_ptr.next_edge != None:
86+
if edge_ptr.end_vertex.name == v.name:
87+
edge_found = True
88+
break
89+
90+
edge_ptr = edge_ptr.next_edge
91+
92+
if edge_found or edge_ptr.end_vertex.name==destination:
93+
print("Edge already present")
94+
else:
95+
temp = EdgeNode(v)
96+
edge_ptr.next_edge = temp
97+
self._nedges += 1
98+
99+
def delete_vertex(self, vertex_name):
100+
self._delete_from_edge_lists(vertex_name)
101+
self._delete_from_vertex_list(vertex_name)
102+
103+
# Delete incoming edges
104+
def _delete_from_edge_lists(self, vertex_name):
105+
vertex_ptr = None
106+
edge_ptr = None
107+
108+
vertex_ptr = self._start
109+
110+
while vertex_ptr != None:
111+
if vertex_ptr.first_edge != None:
112+
if vertex_ptr.first_edge.end_vertex.name == vertex_name:
113+
vertex_ptr.first_edge = vertex_ptr.first_edge.next_edge
114+
self._nedges -= 1
115+
continue
116+
117+
edge_ptr = vertex_ptr.first_edge
118+
while edge_ptr.next_edge != None:
119+
if edge_ptr.next_edge.end_vertex.name == vertex_name:
120+
edge_ptr.next_edge = edge_ptr.next_edge.next_edge
121+
self._nedges -= 1
122+
continue
123+
edge_ptr = edge_ptr.next_edge
124+
125+
vertex_ptr = vertex_ptr.next_vertex
126+
127+
# Delete outgoing edges and vertex
128+
def _delete_from_vertex_list(self, vertex_name):
129+
vertex_ptr = None
130+
temp_vertex = None
131+
edge_ptr = None
132+
133+
if self._start == None:
134+
print("No vertices to be deleted")
135+
return
136+
137+
if self._start.name == vertex_name:
138+
temp_vertex = self._start
139+
self._start = self._start.next_vertex
140+
else: # vertex to be deleted is in between or at last
141+
vertex_ptr = self._start
142+
while vertex_ptr.next_vertex != None:
143+
if vertex_ptr.next_vertex.name == vertex_name:
144+
break
145+
vertex_ptr = vertex_ptr.next_vertex
146+
147+
if vertex_ptr.next_vertex != None:
148+
temp_vertex = vertex_ptr.next_vertex
149+
vertex_ptr.next_vertex = vertex_ptr.next_vertex.next_vertex
150+
else:
151+
print("Vertex not found")
152+
153+
if temp_vertex != None:
154+
edge_ptr = temp_vertex.first_edge
155+
while edge_ptr != None:
156+
edge_ptr = edge_ptr.next_edge
157+
self._nedges -= 1
158+
self._nvertices -= 1
159+
160+
def delete_edge(self, source, destination):
161+
vertex_ptr = None
162+
edge_ptr = None
163+
164+
vertex_ptr = self._find_vertex(source)
165+
166+
if vertex_ptr == None:
167+
print("Edge not found")
168+
else:
169+
edge_ptr = vertex_ptr.first_edge
170+
171+
if edge_ptr == None:
172+
print("Edge not found")
173+
else:
174+
if edge_ptr.end_vertex.name == destination:
175+
vertex_ptr.first_edge = edge_ptr.next_edge
176+
self._nedges -= 1
177+
else:
178+
while edge_ptr.next_edge != None:
179+
if edge_ptr.next_edge.end_vertex.name == destination:
180+
break
181+
edge_ptr = edge_ptr.next_edge
182+
183+
if edge_ptr.next_edge == None:
184+
print("Edge not found")
185+
else:
186+
edge_ptr.next_edge = edge_ptr.next_edge.next_edge
187+
self._nedges -= 1
188+
189+
def display(self):
190+
vertex_ptr = None
191+
edge_ptr = None
192+
193+
vertex_ptr = self._start
194+
195+
while vertex_ptr != None:
196+
print("Vertex : " + vertex_ptr.name)
197+
198+
edge_ptr = vertex_ptr.first_edge
199+
while edge_ptr != None:
200+
print("Edge : " + vertex_ptr.name + " -> " + edge_ptr.end_vertex.name)
201+
edge_ptr = edge_ptr.next_edge
202+
203+
vertex_ptr = vertex_ptr.next_vertex
204+
205+
def edge_exists(self, source, destination):
206+
vertex_ptr = None
207+
edge_ptr = None
208+
edge_found = False
209+
210+
vertex_ptr = self._find_vertex(source)
211+
212+
if vertex_ptr != None:
213+
edge_ptr = vertex_ptr.first_edge
214+
while edge_ptr != None:
215+
if edge_ptr.end_vertex.name == destination:
216+
edge_found = True
217+
break
218+
edge_ptr = edge_ptr.next_edge
219+
220+
return edge_found
221+
222+
def get_outdegree(self, vertex):
223+
vertex_ptr = None
224+
edge_ptr = None
225+
outdegree = 0
226+
227+
vertex_ptr = self._find_vertex(vertex)
228+
if vertex_ptr == None:
229+
raise Exception("Invalid Vertex")
230+
231+
edge_ptr = vertex_ptr.first_edge
232+
while edge_ptr != None:
233+
outdegree += 1
234+
edge_ptr = edge_ptr.next_edge
235+
236+
return outdegree
237+
238+
def get_indegree(self, vertex):
239+
vertex_ptr = None
240+
edge_ptr = None
241+
indegree = 0
242+
243+
if self._find_vertex(vertex) == None:
244+
raise Exception("Invalid Vertex")
245+
246+
vertex_ptr = self._start
247+
while vertex_ptr != None:
248+
edge_ptr = vertex_ptr.first_edge
249+
while edge_ptr != None:
250+
if edge_ptr.end_vertex.name == vertex:
251+
indegree += 1
252+
edge_ptr = edge_ptr.next_edge
253+
vertex_ptr = vertex_ptr.next_vertex
254+
255+
return indegree
256+
257+
if __name__ == '__main__':
258+
259+
d_graph = DirectedGraphList()
260+
261+
try:
262+
# Creating the graph, inserting the vertices and edges
263+
d_graph.insert_vertex("0")
264+
d_graph.insert_vertex("1")
265+
d_graph.insert_vertex("2")
266+
d_graph.insert_vertex("3")
267+
268+
d_graph.insert_edge("0","3")
269+
d_graph.insert_edge("1","2")
270+
d_graph.insert_edge("2","3")
271+
d_graph.insert_edge("3","1")
272+
d_graph.insert_edge("0","2")
273+
274+
# Display the graph
275+
d_graph.display()
276+
print()
277+
278+
# Deleting an edge
279+
d_graph.delete_edge("0","2")
280+
281+
# Display the graph
282+
d_graph.display()
283+
print()
284+
285+
# Deleting a vertex
286+
d_graph.delete_vertex("0")
287+
288+
# Display the graph
289+
d_graph.display()
290+
print()
291+
292+
# Check if there is an edge between two vertices
293+
print("Edge exist : ", d_graph.edge_exists("2","3"))
294+
295+
# Display Outdegree and Indegree of a vertex
296+
print("Outdegree : ", d_graph.get_outdegree("3"))
297+
print("Indegree : ", d_graph.get_indegree("3"))
298+
299+
except Exception as e:
300+
print(e)
301+
302+
303+
304+
305+
306+
307+
308+
309+
310+
311+
312+
313+
314+
315+
316+
317+
318+
319+
320+
321+
322+
323+

graphs/adjacency-matrix/README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Data Structures and Algorithms (DSA) In Python
2+
3+
This repository is for the students of book [Data Structures and Algorithms In Python](http://coursegalaxy.com/) and online courses available on CourseGalaxy. It contains the source code of all the programs used in the book and courses.
4+
5+
### About the DSA Masterclass Courses
6+
* Thoroughly detailed courses with complete working programs
7+
* Contains lots of animations to help you visualize the concepts
8+
* Includes various Data Structures and Algorithms
9+
* Builds a solid foundation in Data Structures and Algorithms
10+
* Prepares you for coding interviews
11+
* Lifetime Access
12+
13+
### DSA Masterclass Courses on [CourseGalaxy](http://coursegalaxy.com/)
14+
15+
[![data-structures-algorithms-python](https://user-images.githubusercontent.com/96913690/200234827-86aec10a-bfab-4371-91fc-e2be855ff1ff.jpg)](https://coursegalaxy.usefedora.com/p/data-structures-algorithms-python?coupon_code=GITHUB75PER)
16+
[![data-structures-algorithms-java](https://user-images.githubusercontent.com/96913690/200234744-14a5ed97-085f-44f3-9298-979c2053c580.jpg)](https://coursegalaxy.usefedora.com/p/data-structures-algorithms-java?coupon_code=GITHUB75PER)
17+
[![data-structures-algorithms-c](https://user-images.githubusercontent.com/96913690/200234592-25d33957-0e9e-4cc0-b324-2a73325aca85.jpg)](https://coursegalaxy.usefedora.com/p/data-structures-algorithms-c?coupon_code=GITHUB75PER)
18+
[![data-structures-algorithms-csharp](https://user-images.githubusercontent.com/96913690/200234905-67b85dfd-20c4-4f4b-afd2-e10d3568fff8.jpg)](https://coursegalaxy.usefedora.com/p/data-structures-algorithms-csharp?coupon_code=GITHUB75PER)
19+
20+
## Copyright
21+
© Copyright Suresh Kumar Srivastava : All rights reserved.
22+
Not to be used for commercial purposes.

0 commit comments

Comments
 (0)