Skip to content

Commit 0f648a4

Browse files
authored
Move graph to the top
1 parent c9ae6f1 commit 0f648a4

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

graphs/breadth_first_search_shortest_path.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
"""Breath First Search (BFS) can be used when finding the shortest path
22
from a given source node to a target node in an unweighted graph.
33
"""
4-
4+
graph = {
5+
"A": ["B", "C", "E"],
6+
"B": ["A", "D", "E"],
7+
"C": ["A", "F", "G"],
8+
"D": ["B"],
9+
"E": ["A", "B", "D"],
10+
"F": ["C"],
11+
"G": ["C"],
12+
}
513

614
class Graph:
715
def __init__(self, graph, source_vertex):
@@ -43,15 +51,6 @@ def shortest_path(self, target_vertex):
4351

4452

4553
if __name__ == "__main__":
46-
graph = {
47-
"A": ["B", "C", "E"],
48-
"B": ["A", "D", "E"],
49-
"C": ["A", "F", "G"],
50-
"D": ["B"],
51-
"E": ["A", "B", "D"],
52-
"F": ["C"],
53-
"G": ["C"],
54-
}
5554
g = Graph(graph, "G")
5655
g.breath_first_search()
5756
print(g.shortest_path("D"))

0 commit comments

Comments
 (0)