Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 16c14a3

Browse files
logging in graphs
1 parent b7008d3 commit 16c14a3

File tree

3 files changed

+48
-28
lines changed

3 files changed

+48
-28
lines changed

‎competitivepython/graphs/bfs.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
1+
import logging
2+
13
def breadth_first_search(graph, node):
2-
visited, queue = set(), [node]
3-
visited.add(node)
4+
try:
5+
visited, queue = set(), [node]
6+
visited.add(node)
7+
8+
while queue:
9+
vertex = queue.pop(0)
10+
visited.add(vertex)
411

5-
whilequeue:
6-
vertex=queue.pop(0)
7-
visited.add(vertex)
12+
forneighbouringraph[vertex]:
13+
ifneighbournotinvisited:
14+
queue.append(neighbour)
815

9-
for neighbour in graph[vertex]:
10-
if neighbour not in visited:
11-
queue.append(neighbour)
16+
return visited
17+
except Exception as e:
18+
logging.exception("An error occurred during binary search: %s", e)
19+
return "An error occurred during breadth first search: {}".format(str(e))
1220

13-
return visited

‎competitivepython/graphs/dfs.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
1-
def depth_first_search(graph, node):
2-
visited = set()
3-
visited = dfs_helper(graph, node, visited)
4-
return visited
1+
import logging
2+
3+
def depth_first_search(graph, node):
4+
try:
5+
visited = set()
6+
visited = dfs_helper(graph, node, visited)
7+
return visited
8+
except Exception as e:
9+
logging.exception("An error occurred during binary search: %s", e)
10+
return "An error occurred during deapth first search: {}".format(str(e))
11+
512

613
def dfs_helper(graph, node, visited):
714
if node not in visited:

‎competitivepython/graphs/dijkstra.py

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,29 @@
11
import heapq
2+
import logging
23

34
def dijkstra(graph, start, end):
4-
distances = {vertex: float('inf') for vertex in graph}
5-
distances[start] = 0
6-
path = {vertex: [] for vertex in graph}
7-
queue = [(0, start)]
5+
try:
6+
distances = {vertex: float('inf') for vertex in graph}
7+
distances[start] = 0
8+
path = {vertex: [] for vertex in graph}
9+
queue = [(0, start)]
810

9-
while queue:
10-
(current_distance, current_vertex) = heapq.heappop(queue)
11+
while queue:
12+
(current_distance, current_vertex) = heapq.heappop(queue)
1113

12-
if current_vertex == end:
13-
break
14+
if current_vertex == end:
15+
break
1416

15-
for neighbor, weight in graph[current_vertex].items():
16-
distance = current_distance + weight
17+
for neighbor, weight in graph[current_vertex].items():
18+
distance = current_distance + weight
1719

18-
if distance < distances[neighbor]:
19-
distances[neighbor] = distance
20-
heapq.heappush(queue, (distance, neighbor))
21-
path[neighbor] = path[current_vertex] + [neighbor]
20+
if distance < distances[neighbor]:
21+
distances[neighbor] = distance
22+
heapq.heappush(queue, (distance, neighbor))
23+
path[neighbor] = path[current_vertex] + [neighbor]
24+
25+
return {'distance': distances[end], 'path': path[end]}
26+
except Exception as e:
27+
logging.exception("An error occurred during binary search: %s", e)
28+
return "An error occurred during dijkstra shortest path: {}".format(str(e))
2229

23-
return {'distance': distances[end], 'path': path[end]}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /