Skip to content

Commit e97565d

Browse files
author
A Safari
authored
Added (Weighted) Undirected graph
Python version 3
1 parent 691f4c0 commit e97565d

File tree

1 file changed

+106
-9
lines changed

1 file changed

+106
-9
lines changed

graphs/Directed (Weighted) Graph

+106-9
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,109 @@ class DirectedGraph:
9696
visited.append(__[1])
9797
return visited
9898

99-
if __name__ == "__main__":
100-
g = DirectedGraph()
101-
# add 50 random nodes to the graph
102-
g.fill_graph_randomly(50)
103-
# you can add or remove any edge and vertex
104-
g.add_pair(3, 5)
105-
g.remove_pair(3,5)
106-
g.dfs()
107-
g.bgs()
99+
100+
class Graph:
101+
def __init__(self):
102+
self.graph = {}
103+
104+
# adding vertices and edges
105+
# adding the weight is optional
106+
# handels repetition
107+
def add_pair(self, u, v, w = 1):
108+
# check if the u exists
109+
if self.graph.get(u):
110+
# if there already is a edge
111+
if self.graph[u].count([w,v]) == 0:
112+
self.graph[u].append([w, v])
113+
else:
114+
# if u does not exist
115+
self.graph[u] = [[w, v]]
116+
# add the other way
117+
if self.graph.get(v):
118+
# if there already is a edge
119+
if self.graph[v].count([w,u]) == 0:
120+
self.graph[v].append([w, u])
121+
else:
122+
# if u does not exist
123+
self.graph[v] = [[w, u]]
124+
125+
# handels if the input does not exist
126+
def remove_pair(self, u, v):
127+
if self.graph.get(u):
128+
for _ in self.graph[u]:
129+
if _[1] == v:
130+
self.graph[u].remove(_)
131+
# the other way round
132+
if self.graph.get(v):
133+
for _ in self.graph[v]:
134+
if _[1] == u:
135+
self.graph[v].remove(_)
136+
137+
# if no destination is meant the defaut value is -1
138+
def dfs(self, s = -2, d = -1):
139+
if s == d:
140+
return []
141+
stack = []
142+
visited = []
143+
if s == -2:
144+
s = list(self.graph.keys())[0]
145+
stack.append(s)
146+
visited.append(s)
147+
ss = s
148+
149+
while True:
150+
# check if there is any non isolated nodes
151+
if len(self.graph[s]) != 0:
152+
ss = s
153+
for __ in self.graph[s]:
154+
if visited.count(__[1]) < 1:
155+
if __[1] == d:
156+
visited.append(d)
157+
return visited
158+
else:
159+
stack.append(__[1])
160+
visited.append(__[1])
161+
ss =__[1]
162+
break
163+
164+
# check if all the children are visited
165+
if s == ss :
166+
stack.pop()
167+
if len(stack) != 0:
168+
s = stack[len(stack) - 1]
169+
else:
170+
s = ss
171+
172+
# check if se have reached the starting point
173+
if len(stack) == 0:
174+
return visited
175+
176+
# c is the count of nodes you want and if you leave it or pass -1 to the funtion the count
177+
# will be random from 10 to 10000
178+
def fill_graph_randomly(self, c = -1):
179+
if c == -1:
180+
c = (math.floor(rand.random() * 10000)) + 10
181+
for _ in range(c):
182+
# every vertex has max 100 edges
183+
e = math.floor(rand.random() * 102) + 1
184+
for __ in range(e):
185+
n = math.floor(rand.random() * (c)) + 1
186+
if n == _:
187+
continue
188+
self.add_pair(_, n, 1)
189+
190+
def bfs(self, s = -2):
191+
d = deque()
192+
visited = []
193+
if s == -2:
194+
s = list(self.graph.keys())[0]
195+
d.append(s)
196+
visited.append(s)
197+
while d:
198+
s = d.popleft()
199+
if len(self.graph[s]) != 0:
200+
for __ in self.graph[s]:
201+
if visited.count(__[1]) < 1:
202+
d.append(__[1])
203+
visited.append(__[1])
204+
return visited

0 commit comments

Comments
 (0)