Skip to content

Commit 687af17

Browse files
author
A Safari
authored
Added some examples.
Added examples and comments for more readable code.
1 parent fa2eecd commit 687af17

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

graphs/Directed (Weighted) Graph

+14-2
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ import math as math
55
# the dfault weight is 1 if not assigend but all the implementation is weighted
66

77
class DirectedGraph:
8-
# enter True or False for this constructor
98
def __init__(self):
109
self.graph = {}
1110

1211
# adding vertices and edges
13-
# note that self loops are not supported in undirected simpl graphs but it is in multigraphs
12+
# adding the weight is optional
13+
# handels repetition
1414
def add_pair(self, u, v, w = 1):
1515
if self.graph.get(u):
1616
if self.graph[u].count([w,v]) == 0:
@@ -19,6 +19,8 @@ class DirectedGraph:
1919
self.graph[u] = [[w, v]]
2020
if not self.graph.get(v):
2121
self.graph[v] = []
22+
23+
# handels if the input does not exist
2224
def remove_pair(self, u, v):
2325
if self.graph.get(u):
2426
for _ in self.graph[u]:
@@ -93,3 +95,13 @@ class DirectedGraph:
9395
d.append(__[1])
9496
visited.append(__[1])
9597
return visited
98+
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()

0 commit comments

Comments
 (0)