-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a_star, hamiltonian_cycle, correct error in GraphNamedVertices
- Loading branch information
Showing
8 changed files
with
184 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
"""\ | ||
Shortest Path algorithm A*. | ||
jill-jênn vie et christoph dürr - 2023 | ||
""" | ||
|
||
from heapq import heappop, heappush | ||
|
||
|
||
def a_star(graph, start, lower_bound): | ||
"""single source shortest path by A* on an unweighted graph | ||
:param graph: iterator on adjacent vertices | ||
:param source: source vertex | ||
:param lower_bound: lb function on distance to target, | ||
must return 0 on target and only there. | ||
:returns: distance or -1 (target unreachable) | ||
:complexity: `O(|V| + |E|log|V|)` | ||
""" | ||
closedset = set() | ||
openset = set([start]) | ||
g = {start: 0 } | ||
Q = [(lower_bound(start), start)] | ||
while Q: | ||
(val, x) = heappop(Q) | ||
if lower_bound(x) == 0: | ||
return g[x] | ||
closedset.add(x) | ||
for y in graph(x): | ||
if not y in closedset and not y in openset: | ||
g[y] = g[x] + 1 | ||
val = g[y] + lower_bound(y) | ||
heappush(Q, (val, y)) | ||
openset.add(y) | ||
return -1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
"""\ | ||
Hamiltonian Cycle | ||
jill-jenn vie et christoph durr - 2023 | ||
""" | ||
|
||
|
||
# snip{ | ||
def hamiltonian_cycle(weight): | ||
"""Hamiltonian Cycle | ||
:param weight: matrix of edge weights of a complete graph | ||
:returns: minimum weight of a Hamiltonian cycle | ||
:complexity: O(n^2 2^n) | ||
""" | ||
n = len(weight) | ||
# A[S][v] = minimum weight path from vertex n-1, | ||
# then visiting all vertices in S exactly once, | ||
# and finishing in v (which is not in S) | ||
A = [[float('inf')] * n for _ in range(1 << (n - 1))] | ||
for v in range(n): # base case | ||
A[0][v] = weight[n - 1][v] | ||
for S in range(1, 1 << (n - 1)): | ||
for v in range(n): | ||
if not ((1 << v) & S): # v not in S | ||
for u in range(n - 1): | ||
U = 1 << u | ||
if U & S: # u in S | ||
alt = A[S ^ U][u] + weight[u][v] | ||
if alt < A[S][v]: | ||
A[S][v] = alt | ||
S = (1 << (n - 1)) - 1 # {0, 1, ..., n - 2} | ||
return A[S][n - 1] | ||
# snip} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters