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 bd1b468

Browse files
updated tests
1 parent 3ba27ef commit bd1b468

File tree

2 files changed

+10
-25
lines changed

2 files changed

+10
-25
lines changed

‎Algorithm_tests/graphtheory_tests/bellman_ford_test.py‎

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,9 @@
99
# If run from local:
1010
#sys.path.append('../../Algorithms/graphtheory/bellman-ford')
1111

12-
from bellman_ford import bellman_ford, make_graph
12+
from bellman_ford import bellman_ford
1313

1414
class test_BellmanFord(unittest.TestCase):
15-
16-
def test_loadgraph(self):
17-
correct_graph = {1: {2: 5, 3: 10}, 2: {4: -5}, 3: {4: 15}}
18-
graph = make_graph('Algorithm_tests/graphtheory_tests/test_graph.txt')
19-
20-
self.assertEqual(graph, correct_graph)
21-
2215
def test_loadmissingfile(self):
2316
with self.assertRaises(IOError):
2417
graph = make_graph('this_file_doesnt_exist.txt')

‎Algorithms/graphtheory/dijkstra/dijkstra.py‎

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,20 @@
11
'''
2-
Dijkstra's algorithm for finding the shortest path in a graph
2+
Dijkstra's algorithm for finding the shortest path in a graph, this implementation
3+
is a naive implementation, check my Heap implementation for a more efficient algorithm
34
45
Programmed by Aladdin Persson <aladdin.persson at hotmail dot com>
56
2019年01月28日 Initial programming
67
2020年03月28日 Cleaned up code
78
'''
89

9-
def make_graph(file):
10-
try:
11-
f = open(file, 'r')
12-
except IOError:
13-
raise("File does not exist!")
14-
15-
line_list = f.readlines()
16-
17-
G = {int(line.split()[0]): {(int(tup.split(',')[0])): int(tup.split(',')[1])
18-
for tup in line.split()[1:] if tup} for line in line_list if line}
19-
20-
f.close()
21-
return G
22-
2310
def dijkstra(G, start, end):
11+
'''
12+
:param G: {from_node1: {to_node1:cost1, to_node2:cost2}, from_node2 : {.., etc.}, ...}
13+
:param start: starting node
14+
:param end: ending node where we want to find path to
15+
:return: path from starting node to end node and the cost to get between them
16+
'''
17+
2418
if start not in G or end not in G:
2519
return [], float('inf')
2620

@@ -70,8 +64,6 @@ def dijkstra(G, start, end):
7064

7165

7266
if __name__ == '__main__':
73-
#G = make_graph('dijkstraData.txt')
74-
7567
G = {1:{2:10, 3:20},
7668
2:{4:40},
7769
3:{4:5},

0 commit comments

Comments
(0)

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