Skip to content

Add doctests to graphs/basic_graphs.py #12881

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
50 changes: 40 additions & 10 deletions graphs/basic_graphs.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,14 @@ def bfs(g, s):

def dijk(g, s):
"""
dijk({1: [(2, 7), (3, 9), (6, 14)],
2: [(1, 7), (3, 10), (4, 15)],
3: [(1, 9), (2, 10), (4, 11), (6, 2)],
4: [(2, 15), (3, 11), (5, 6)],
5: [(4, 6), (6, 9)],
6: [(1, 14), (3, 2), (5, 9)]}, 1)
>>> dijk({
... 1: [(2, 7), (3, 9), (6, 14)],
... 2: [(1, 7), (3, 10), (4, 15)],
... 3: [(1, 9), (2, 10), (4, 11), (6, 2)],
... 4: [(2, 15), (3, 11), (5, 6)],
... 5: [(4, 6), (6, 9)],
... 6: [(1, 14), (3, 2), (5, 9)]
... }, 1)
7
9
11
Expand All @@ -165,7 +167,7 @@ def dijk(g, s):
if len(known) == len(g) - 1:
break
mini = 100000
for key, value in dist:
for key, value in dist.items():
if key not in known and value < mini:
mini = value
u = key
Expand All @@ -187,6 +189,15 @@ def dijk(g, s):


def topo(g, ind=None, q=None):
"""
Perform a topological sort on a directed acyclic graph.

>>> topo({1: [2, 3], 2: [4], 3: [4], 4: []})
1
2
3
4
"""
if q is None:
q = [1]
if ind is None:
Expand Down Expand Up @@ -256,16 +267,35 @@ def adjm():
"""


def floy(a_and_n):
def floyd_warshall(a_and_n):
"""
Floyd-Warshall algorithm to compute all-pairs shortest paths.

Parameters:
a_and_n (tuple): A tuple (a, n) where
a is an N x N adjacency matrix (list of lists),
n is the number of nodes.

Example:
>>> floyd_warshall(([
... [0, 5, float('inf')],
... [50, 0, 10],
... [float('inf'), float('inf'), 0]
... ], 3))
[[0, 5, 15], [50, 0, 10], [inf, inf, 0]]
"""

(a, n) = a_and_n
dist = list(a)
dist = [row[:] for row in a] # create a deep copy of matrix a
path = [[0] * n for i in range(n)]

for k in range(n):
for i in range(n):
for j in range(n):
if dist[i][j] > dist[i][k] + dist[k][j]:
dist[i][j] = dist[i][k] + dist[k][j]
path[i][k] = k
path[i][k] = k # possible error

print(dist)


Expand Down