first test

This commit is contained in:
zirtidik 2024-12-03 18:18:35 -05:00
parent b22fab0ea4
commit 4a414699eb
2 changed files with 51 additions and 1 deletions

View File

@ -1,5 +1,10 @@
{ {
"githubPullRequests.ignoredPullRequestBranches": [ "githubPullRequests.ignoredPullRequestBranches": [
"master" "master"
] ],
"python.testing.pytestArgs": [
"."
],
"python.testing.unittestEnabled": false,
"python.testing.pytestEnabled": true
} }

View File

@ -8,12 +8,37 @@ from time import time
class DirectedGraph: class DirectedGraph:
def __init__(self): def __init__(self):
"""
Initialize an empty directed graph.
Example:
>>> g = DirectedGraph()
>>> g.graph
{}
"""
self.graph = {} self.graph = {}
# adding vertices and edges # adding vertices and edges
# adding the weight is optional # adding the weight is optional
# handles repetition # handles repetition
def add_pair(self, u, v, w=1): def add_pair(self, u, v, w=1):
"""
Add a directed edge with an optional weight.
Args:
u: Starting node.
v: Destination node.
w: Weight (default is 1).
Example:
>>> g = DirectedGraph()
>>> g.add_pair(1, 2)
>>> g.graph
{1: [[1, 2]], 2: []}
>>> g.add_pair(2, 3, 5)
>>> g.graph
{1: [[1, 2]], 2: [[5, 3]], 3: []}
"""
if self.graph.get(u): if self.graph.get(u):
if self.graph[u].count([w, v]) == 0: if self.graph[u].count([w, v]) == 0:
self.graph[u].append([w, v]) self.graph[u].append([w, v])
@ -27,6 +52,21 @@ class DirectedGraph:
# handles if the input does not exist # handles if the input does not exist
def remove_pair(self, u, v): def remove_pair(self, u, v):
"""
Remove a directed edge from u to v.
Args:
u: Starting node.
v: Destination node.
Example:
>>> g = DirectedGraph()
>>> g.add_pair(1, 2)
>>> g.add_pair(1, 3)
>>> g.remove_pair(1, 2)
>>> g.graph
{1: [[1, 3]], 2: [], 3: []}
"""
if self.graph.get(u): if self.graph.get(u):
for _ in self.graph[u]: for _ in self.graph[u]:
if _[1] == v: if _[1] == v:
@ -487,3 +527,8 @@ class Graph:
self.bfs(s) self.bfs(s)
end = time() end = time()
return end - begin return end - begin
if __name__ == "__main__":
import doctest
doctest.testmod()