Skip to content

Commit

Permalink
Added W07 materials
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanSol committed Nov 20, 2023
1 parent 1a4c364 commit 1a07bd5
Show file tree
Hide file tree
Showing 5 changed files with 232 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@
| 04 | Dynamic programming | [Slides][Slides_W04] | [Test][WarmUp_test_W04] | [Contest][ContestID_W04] | 01.11.2023 19:00 UTC+3 |
| 05 | Knapsack problem | [Slides][Slides_W05] | [Test][WarmUp_test_W05] | [Contest][ContestID_W05] | 13.11.2023 19:00 UTC+3 |
| 06 | KMP & Heap | [Slides][Slides_W06] | [Test][WarmUp_test_W06] | [Contest][ContestID_W06] | 20.11.2023 19:00 UTC+3 |
<!---
| 07 | DFS & BFS | [Slides][Slides_W07] | [Test][WarmUp_test_W07] | [Contest][ContestID_W07] | 27.11.2023 19:00 UTC+3 |
<!---
| 08 | Shortest paths | [Slides][Slides_W08] | [Test][WarmUp_test_W08] | [Contest][ContestID_W08] | 04.12.2023 19:00 UTC+3 |
| 09 | RSQ & RMQ | [Slides][Slides_W09] | [Test][WarmUp_test_W09] | [Contest][ContestID_W09] | 11.12.2023 19:00 UTC+3 |
| 10 | Hashing | [Slides][Slides_W10] | [Test][WarmUp_test_W10] | [Contest][ContestID_W10] | 18.12.2023 19:00 UTC+3 |
Expand Down
Binary file added week07_dfs_bfs/MSAI.2023.Algo.W07.slides.pdf
Binary file not shown.
61 changes: 61 additions & 0 deletions week07_dfs_bfs/bfs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
from collections import deque
INF = 10**9

# with one color:
def bfs(G, s):
visited = [False] * len(G)
vertex_queue = deque([s])
while vertex_queue:
v = vertex_queue.popleft()
if not visited[v]:
visited[v] = True
for u in G[v]:
if not visited[u]:
vertex_queue.append(u)
return visited


# with two colors for calculating path (+d, p):
def bfs_p(G, s):
visited = [False] * len(G)
d = [INF] * len(G)
p = [-1] * len(G)
d[s] = 0

vertex_queue = deque([s])
while vertex_queue:
v = vertex_queue.popleft()
visited[v] = 2
for u in G[v]:
if not visited[u]:
visited[u] = 1
vertex_queue.append(u)
d[u] = d[v] + 1
p[u] = v
return d, p


if __name__ == '__main__':
N, M = map(int, input().split())
G = [[] for i in range(N)]

for i in range(M):
x, y = map(int, input().split())
G[x].append(y)
G[y].append(x)
s, f = map(int, input().split())

#searching shortest path s -> f:
d, p = bfs_p(G, s)

if d[f] == INF:
print(-1)
else:
print(d[f])
path = []
i = f
while i != s:
path.append(i)
i = p[i]
path.append(s)
print(' '.join(map(str, path[::-1])))
116 changes: 116 additions & 0 deletions week07_dfs_bfs/dfs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
from itertools import groupby
#import sys
#sys.setrecursionlimit(640000000)


# just traverses all vertices reachable from v:
def dfs(G, visited, v):
visited[v] = True
for n in G[v]:
if not visited[n]:
dfs(G, visited, n)


# just traverses all vertices reachable from v:
def dfs_nonrec_1(G, visited, s):
vertex_stack = [s]
while vertex_stack:
v = vertex_stack.pop()
if not visited[v]:
visited[v] = True
for u in G[v]:
if not visited[u]:
vertex_stack.append(u)


# allows to get a moment when we color vertex with blue:
# (like ending of executing dfs(v) for recursive case)
def dfs_nonrec_2(G, visited, s):
vertex_stack = [s]
while vertex_stack:
v = vertex_stack[-1]
if not visited[v]:
visited[v] = True
for u in G[v]:
if not visited[u]:
vertex_stack.append(u)
else:
# all neighbours of v were visited
vertex_stack.pop()


# checks for cycles in undirected graph:
def dfs_cycles_undirected(G, visited, v, p=-1):
visited[v] = True
for u in G[v]:
if not visited[u]:
dfs_cycles_undirected(G, visited, u, v)
else:
if u != p:
print('Cycle found!')


def dfs_cycles_undirected_nonrec(G, visited, s):
vertex_stack = [(s, -1)]
while vertex_stack:
v, p = vertex_stack[-1]
if not visited[v]:
visited[v] = True
for u in G[v]:
if not visited[u]:
vertex_stack.append((u, v))
else:
if u != p:
print('Cycle found!')
else:
# all neighbours of v were visited
vertex_stack.pop()


# checks for cycles in directed graph:
def dfs_cycles_directed(G, visited, v):
visited[v] = 1
for u in G[v]:
if not visited[u]:
dfs(G, visited, u)
elif visited[u] == 1:
print('Cycle found!')
visited[v] = 2


def dfs_cycles_directed_nonrec(G, visited, s):
vertex_stack = [s]
while vertex_stack:
v = vertex_stack[-1]
if not visited[v]:
visited[v] = 1
for u in G[v]:
if not visited[u]:
vertex_stack.append(u)
elif visited[u] == 1:
print('Cycle found!')
else:
# all neighbours of v were visited
visited[v] = 2
vertex_stack.pop()


if __name__ == '__main__':
N, M = map(int, input().split())
G = [[] for i in range(N)]

for i in range(M):
x, y = map(int, input().split())
G[x].append(y)
G[y].append(x)

# calculating number of connected components:

visited = [False] * len(G)
n_components = 0
for i in range(N):
if not visited[i]:
n_components += 1
dfs(G, visited, i)

print(n_components)
54 changes: 54 additions & 0 deletions week07_dfs_bfs/topsort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import sys


def dfs(G, visited, topsort, v):
visited[v] = 1
for u in G[v]:
if not visited[u]:
dfs(G, visited, topsort, u)
elif visited[u] == 1:
print('No')
sys.exit(0)
visited[v] = 2
topsort.append(v)


def dfs_nonrec(G, visited, topsort, s):
vertex_stack = [s]
while vertex_stack:
v = vertex_stack[-1]
if not visited[v]:
visited[v] = 1
for u in G[v]:
if not visited[u]:
vertex_stack.append(u)
elif visited[u] == 1:
print('No')
sys.exit(0)
else:
# all neighbours of v were visited
if visited[v] == 1:
topsort.append(v)
visited[v] = 2
vertex_stack.pop()


if __name__ == '__main__':
N, M = map(int, input().split())
G = [[] for i in range(N)]

for i in range(M):
x, y = map(int, input().split())
G[x].append(y)

visited = [False] * len(G)

topsort = []
for i in range(N):
if not visited[i]:
dfs_nonrec(G, visited, topsort, i)

topsort = [v for v in topsort]

print('Yes')
print(' '.join(map(str, topsort[::-1])))

0 comments on commit 1a07bd5

Please sign in to comment.