Skip to content

Commit

Permalink
Added W08 materials
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanSol committed Dec 2, 2024
1 parent 89ddf35 commit 260fc80
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 2 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
[WarmUp_test_W05]: https://forms.gle/F6V36zU14HkvFMq5A
[WarmUp_test_W06]: https://forms.gle/uxtkHdyrxKR8c8i56
[WarmUp_test_W07]: https://forms.gle/EzjU2FB1hDY3QQkJA
[WarmUp_test_W08]: https://forms.gle/ftxDfdVeZDQZB1JfA
[WarmUp_test_W08]: https://forms.gle/bbr21MMSv3Ziyska8
[WarmUp_test_W09]: https://forms.gle/g9QA4LTiZwg3Nbka7
[WarmUp_test_W10]: https://forms.gle/aJ5chmuBhfviNn8Q8
[WarmUp_test_W11]: https://forms.gle/AshGbamo5f7Qi8uL9
Expand All @@ -46,8 +46,8 @@
| 05 | Knapsack problem | [Slides][Slides_W05] | [Test][WarmUp_test_W05] | [Contest][ContestID_W05] | 11.11.2024 19:00 UTC+3 |
| 06 | KMP & Heap | [Slides][Slides_W06] | [Test][WarmUp_test_W06] | [Contest][ContestID_W06] | 18.11.2024 19:00 UTC+3 |
| 07 | DFS & BFS | [Slides][Slides_W07] | [Test][WarmUp_test_W07] | [Contest][ContestID_W07] | 25.11.2024 19:00 UTC+3 |
| 08 | Shortest paths | [Slides][Slides_W08] | [Test][WarmUp_test_W08] | [Contest][ContestID_W08] | 02.12.2024 19:00 UTC+3 |
<!---
| 08 | Shortest paths | [Slides][Slides_W08] | [Test][WarmUp_test_W08] | [Contest][ContestID_W08] | ??.12.2024 19:00 UTC+3 |
| 09 | RSQ & RMQ | [Slides][Slides_W09] | [Test][WarmUp_test_W09] | [Contest][ContestID_W09] | ??.12.2024 19:00 UTC+3 |
| 10 | Hashing | [Slides][Slides_W10] | [Test][WarmUp_test_W10] | [Contest][ContestID_W10] | ??.12.2024 19:00 UTC+3 |
| 11 | Binary Search Tree | [Slides][Slides_W11] | None | None | None |
Expand Down
Binary file not shown.
50 changes: 50 additions & 0 deletions week08_shortest_paths/bellman_ford.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
inf = 10**9
noedge = 100000


def get_cycle(p, v):
for i in range(len(p)):
v = p[v]
cycle = [v]
cycle_start = v
v = p[v]
while v != cycle_start:
cycle.append(v)
v = p[v]
cycle.append(cycle_start)
return cycle[::-1]


def bellman_ford(N, E, s):
d = [inf] * N
p = [-1] * N
d[s] = 0
for i in range(N - 1):
for (v, u, w) in E:
if d[u] > d[v] + w:
d[u] = d[v] + w
p[u] = v
for (v, u, w) in E:
if d[u] > d[v] + w:
p[u] = v
print('Negative cycle!')
c = get_cycle(p, u)
print(len(c))
print(' '.join(map(str, c)))
return None
return d


if __name__ == '__main__':
N, M = map(int, input().split())
E = [] # tuples (v, u, w)
for i in range(M):
E.append(tuple(map(int, input().split())))
s = int(input())

# checks for negative cycles reachable from s and prints one of them if any
# otherwise prints d for paths s -> i

d = bellman_ford(N, E, s)
if d is not None:
print(' '.join(map(str, d)))
32 changes: 32 additions & 0 deletions week08_shortest_paths/dijkstra.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from heapq import heappush, heappop
inf = 10 ** 9


if __name__ == '__main__':
N, M = map(int, input().split())
G = [{} for i in range(N)]
for i in range(M):
x, y, weight = map(int, input().split())
G[x][y] = weight
s = int(input())

h = []
d = [inf] * N
v = -1
# added fictitious vertex to simplify
# while with heappop:
S = {-1}
d[s] = 0
heappush(h, (d[s], s))
while h:
while v in S and h:
v = heappop(h)[1]
if v not in S:
S.add(v)
for u in G[v]:
new_d = d[v] + G[v][u]
if new_d < d[u]:
heappush(h, (new_d, u))
d[u] = new_d

print(' '.join(map(str, d)))
16 changes: 16 additions & 0 deletions week08_shortest_paths/floyd_warshall.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
if __name__ == '__main__':
N = int(input())
G = [{} for v in range(N)]
E = []
a = []
for i in range(N):
a.append(list(map(int, input().split())))

d = [list(ai) for ai in a]
for k in range(N):
for v in range(N):
for u in range(N):
d[v][u] = min(d[v][u], d[v][k] + d[k][u])

for di in d:
print(' '.join(map(str, di)))

0 comments on commit 260fc80

Please sign in to comment.