File tree Expand file tree Collapse file tree 1 file changed +14
-2
lines changed Expand file tree Collapse file tree 1 file changed +14
-2
lines changed Original file line number Diff line number Diff line change @@ -5,12 +5,12 @@ import math as math
5
5
# the dfault weight is 1 if not assigend but all the implementation is weighted
6
6
7
7
class DirectedGraph:
8
- # enter True or False for this constructor
9
8
def __init__(self):
10
9
self.graph = {}
11
10
12
11
# 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
14
14
def add_pair(self, u, v, w = 1):
15
15
if self.graph.get(u):
16
16
if self.graph[u].count([w,v]) == 0:
@@ -19,6 +19,8 @@ class DirectedGraph:
19
19
self.graph[u] = [[w, v]]
20
20
if not self.graph.get(v):
21
21
self.graph[v] = []
22
+
23
+ # handels if the input does not exist
22
24
def remove_pair(self, u, v):
23
25
if self.graph.get(u):
24
26
for _ in self.graph[u]:
@@ -93,3 +95,13 @@ class DirectedGraph:
93
95
d.append(__[1])
94
96
visited.append(__[1])
95
97
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()
You can’t perform that action at this time.
0 commit comments