Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 201bafb

Browse files
committed
Added Dijkstra and UnionFind
1 parent 0b3cf64 commit 201bafb

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

algorithms/dijkstra.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Dijkstra's algorithm
2+
## \textbf{Note:} Replacing heapq with a priorityqueue from the \ms{queue}-package might be ncessary
3+
def dijkstra(a, b, nodes):
4+
import heapq
5+
visited = {}
6+
q = [(0, a)]
7+
distances = [float('inf') for _ in range(len(nodes))]
8+
distances[a] = 0
9+
while q:
10+
_, current = heapq.heappop(q)
11+
if current not in visited:
12+
visited.add(current)
13+
if current == b: return distances[b]
14+
for w, n in nodes[current]:
15+
if n in visited: continue
16+
if distances[n] > w + distances[current]:
17+
distances[n] = w + distances[current]
18+
heapq.heappush((distances[n], n))
19+
return float('inf')

algorithms/union_find.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Union Find
2+
class UnionFind:
3+
4+
def __init__(self, nodes):
5+
self.nodes = list(range(nodes))
6+
self.rank = [0]*nodes
7+
def union(self, x, y):
8+
rootx = self.nodes[x]
9+
rooty = self.nodes[y]
10+
if self.rank[rootx] > self.rank[rooty]:
11+
self.nodes[rooty] = rootx
12+
elif self.rank[rootx] < self.rank[rooty]:
13+
self.nodes[rootx] = rooty
14+
elif rootx != rooty:
15+
self.nodes[rooty] = rootx
16+
self.rank[rootx] += 1
17+
18+
def find(self, x):
19+
if self.nodes[x] == x:
20+
return x
21+
self.nodes[x] = self.find(self.nodes[x])
22+
return self.nodes[x]

substitute.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
# Get files in directory
77
algorithm_dir = sys.argv[1]
88
filelist = [f for f in listdir(algorithm_dir) if isfile(join(algorithm_dir, f))]
9+
filelist.sort()
910

1011
def get_lang(f):
1112
if f[-3:] == ".py": return "python"
@@ -17,6 +18,7 @@ def get_lang(f):
1718
for f in filelist:
1819
with open(join(algorithm_dir, f)) as file:
1920
data = file.read()
21+
code = "\n".join([i for i in data.partition('\n')[-1].splitlines() if not i.startswith("##")])
2022
title = data.splitlines()[0][2:].strip()
2123
extra_info = "\n\\\\".join([i.strip()[3:] for i in data.splitlines()[1:] if i.startswith("##")])
2224
inclusion += r"""
@@ -25,7 +27,7 @@ def get_lang(f):
2527
\begin{lstlisting}[language=%LANGUAGE%]
2628
%FILE%
2729
\end{lstlisting}
28-
""".replace("%LANGUAGE%", get_lang(f)).replace("%FILE%", data.partition('\n')[2]).replace("%TITLE%", title).replace("%EXTRA%", extra_info)
30+
""".replace("%LANGUAGE%", get_lang(f)).replace("%FILE%", code).replace("%TITLE%", title).replace("%EXTRA%", extra_info)
2931

3032
with open("main.tex") as infile:
3133
print("Writing to file")

0 commit comments

Comments
 (0)