Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Tests for directed_and_undirected_weighted_graph.py #12412

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
{
"githubPullRequests.ignoredPullRequestBranches": [
"master"
]
],
"python.testing.pytestArgs": [
"."
],
"python.testing.unittestEnabled": false,
"python.testing.pytestEnabled": true
}
46 changes: 46 additions & 0 deletions graphs/directed_and_undirected_weighted_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,37 @@

class DirectedGraph:
def __init__(self):
"""
Initialize an empty directed graph.

Example:
>>> g = DirectedGraph()
>>> g.graph
{}
"""
self.graph = {}

# adding vertices and edges
# adding the weight is optional
# handles repetition
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[u].count([w, v]) == 0:
self.graph[u].append([w, v])
Expand All @@ -27,6 +52,21 @@ def all_nodes(self):

# handles if the input does not exist
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):
for _ in self.graph[u]:
if _[1] == v:
Expand Down Expand Up @@ -487,3 +527,9 @@ def bfs_time(self, s=-2):
self.bfs(s)
end = time()
return end - begin


if __name__ == "__main__":
import doctest

doctest.testmod(verbose=True)
8 changes: 5 additions & 3 deletions greedy_methods/fractional_knapsack.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ def frac_knapsack(vl, wt, w, n):
return (
0
if k == 0
else sum(vl[:k]) + (w - acc[k - 1]) * (vl[k]) / (wt[k])
if k != n
else sum(vl[:k])
else (
sum(vl[:k]) + (w - acc[k - 1]) * (vl[k]) / (wt[k])
if k != n
else sum(vl[:k])
)
)


Expand Down
8 changes: 5 additions & 3 deletions matrix/matrix_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,11 @@ def cofactors(self) -> Matrix:
return Matrix(
[
[
self.minors().rows[row][column]
if (row + column) % 2 == 0
else self.minors().rows[row][column] * -1
(
self.minors().rows[row][column]
if (row + column) % 2 == 0
else self.minors().rows[row][column] * -1
)
for column in range(self.minors().num_columns)
]
for row in range(self.minors().num_rows)
Expand Down
Loading