Skip to content

Commit 0f2edef

Browse files
committed
Python Graph implementation
1 parent 2af624f commit 0f2edef

File tree

3 files changed

+162
-0
lines changed

3 files changed

+162
-0
lines changed

‎data_structures/Graph/Graph.py‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Author: OMKAR PATHAK
2+
3+
# We can use Python's dictionary for constructing the graph
4+
5+
classAdjacencyList(object):
6+
def__init__(self):
7+
self.List={}
8+
9+
defaddEdge(self, fromVertex, toVertex):
10+
# check if vertex is already present
11+
iffromVertexinself.List.keys():
12+
self.List[fromVertex].append(toVertex)
13+
else:
14+
self.List[fromVertex] = [toVertex]
15+
16+
defprintList(self):
17+
foriinself.List:
18+
print(i,'->',' -> '.join([str(j) forjinself.List[i]]))
19+
20+
if__name__=='__main__':
21+
al=AdjacencyList()
22+
al.addEdge(0, 1)
23+
al.addEdge(0, 4)
24+
al.addEdge(4, 1)
25+
al.addEdge(4, 3)
26+
al.addEdge(1, 0)
27+
al.addEdge(1, 4)
28+
al.addEdge(1, 3)
29+
al.addEdge(1, 2)
30+
al.addEdge(2, 3)
31+
al.addEdge(3, 4)
32+
33+
al.printList()
34+
35+
# OUTPUT:
36+
# 0 -> 1 -> 4
37+
# 1 -> 0 -> 4 -> 3 -> 2
38+
# 2 -> 3
39+
# 3 -> 4
40+
# 4 -> 1 -> 3
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Author: OMKAR PATHAK
2+
3+
classGraph():
4+
def__init__(self):
5+
self.vertex={}
6+
7+
# for printing the Graph vertexes
8+
defprintGraph(self):
9+
foriinself.vertex.keys():
10+
print(i,' -> ', ' -> '.join([str(j) forjinself.vertex[i]]))
11+
12+
# for adding the edge beween two vertexes
13+
defaddEdge(self, fromVertex, toVertex):
14+
# check if vertex is already present,
15+
iffromVertexinself.vertex.keys():
16+
self.vertex[fromVertex].append(toVertex)
17+
else:
18+
# else make a new vertex
19+
self.vertex[fromVertex] = [toVertex]
20+
21+
defBFS(self, startVertex):
22+
# Take a list for stoting already visited vertexes
23+
visited= [False] *len(self.vertex)
24+
25+
# create a list to store all the vertexes for BFS
26+
queue= []
27+
28+
# mark the source node as visited and enqueue it
29+
visited[startVertex] =True
30+
queue.append(startVertex)
31+
32+
whilequeue:
33+
startVertex=queue.pop(0)
34+
print(startVertex, end=' ')
35+
36+
# mark all adjacent nodes as visited and print them
37+
foriinself.vertex[startVertex]:
38+
ifvisited[i] ==False:
39+
queue.append(i)
40+
visited[i] =True
41+
42+
if__name__=='__main__':
43+
g=Graph()
44+
g.addEdge(0, 1)
45+
g.addEdge(0, 2)
46+
g.addEdge(1, 2)
47+
g.addEdge(2, 0)
48+
g.addEdge(2, 3)
49+
g.addEdge(3, 3)
50+
51+
g.printGraph()
52+
print('BFS:')
53+
g.BFS(2)
54+
55+
# OUTPUT:
56+
# 0 -> 1 -> 2
57+
# 1 -> 2
58+
# 2 -> 0 -> 3
59+
# 3 -> 3
60+
# BFS:
61+
# 2 0 3 1
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Author: OMKAR PATHAK
2+
3+
classGraph():
4+
def__init__(self):
5+
self.vertex={}
6+
7+
# for printing the Graph vertexes
8+
defprintGraph(self):
9+
print(self.vertex)
10+
foriinself.vertex.keys():
11+
print(i,' -> ', ' -> '.join([str(j) forjinself.vertex[i]]))
12+
13+
# for adding the edge beween two vertexes
14+
defaddEdge(self, fromVertex, toVertex):
15+
# check if vertex is already present,
16+
iffromVertexinself.vertex.keys():
17+
self.vertex[fromVertex].append(toVertex)
18+
else:
19+
# else make a new vertex
20+
self.vertex[fromVertex] = [toVertex]
21+
22+
defDFS(self):
23+
# visited array for storing already visited nodes
24+
visited= [False] *len(self.vertex)
25+
26+
# call the recursive helper function
27+
foriinrange(len(self.vertex)):
28+
ifvisited[i] ==False:
29+
self.DFSRec(i, visited)
30+
31+
defDFSRec(self, startVertex, visited):
32+
# mark start vertex as visited
33+
visited[startVertex] =True
34+
35+
print(startVertex, end=' ')
36+
37+
# Recur for all the vertexes that are adjacent to this node
38+
foriinself.vertex.keys():
39+
ifvisited[i] ==False:
40+
self.DFSRec(i, visited)
41+
42+
if__name__=='__main__':
43+
g=Graph()
44+
g.addEdge(0, 1)
45+
g.addEdge(0, 2)
46+
g.addEdge(1, 2)
47+
g.addEdge(2, 0)
48+
g.addEdge(2, 3)
49+
g.addEdge(3, 3)
50+
51+
g.printGraph()
52+
print('DFS:')
53+
g.DFS()
54+
55+
# OUTPUT:
56+
# 0 -> 1 -> 2
57+
# 1 -> 2
58+
# 2 -> 0 -> 3
59+
# 3 -> 3
60+
# DFS:
61+
# 0 1 2 3

0 commit comments

Comments
(0)