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 1c37692

Browse files
committed
Add Dijkstra's algorithm implementation
1 parent 25d71ff commit 1c37692

File tree

6 files changed

+85
-16
lines changed

6 files changed

+85
-16
lines changed

‎.gitignore‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
graphs_in_python_lessions/representing_graphs_in_code/base_classes/__pycache__
2-
graphs_in_python_lessions/representing_graphs_in_code/__pycache__
3-
1+
__pycache__/
2+
*.py[cod]
3+
*$py.class
-2.37 KB
Binary file not shown.
-1.84 KB
Binary file not shown.
-1.94 KB
Binary file not shown.
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
from queue import PriorityQueue
2+
3+
class Graph:
4+
def __init__(self, num_of_vertices):
5+
self.v = num_of_vertices
6+
self.edges = [[-1 for i in range(num_of_vertices)] for j in range(num_of_vertices)]
7+
self.visited = []
8+
9+
def add_edge(self, u, v, weight):
10+
self.edges[u][v] = weight
11+
self.edges[v][u] = weight
12+
13+
def dijkstra(self, start_vertex):
14+
D = {v:float('inf') for v in range(self.v)}
15+
D[start_vertex] = 0
16+
17+
pq = PriorityQueue()
18+
pq.put((0, start_vertex))
19+
20+
while not pq.empty():
21+
(dist, current_vertex) = pq.get()
22+
self.visited.append(current_vertex)
23+
24+
for neighbor in range(self.v):
25+
if self.edges[current_vertex][neighbor] != -1:
26+
distance = self.edges[current_vertex][neighbor]
27+
if neighbor not in self.visited:
28+
old_cost = D[neighbor]
29+
new_cost = D[current_vertex] + distance
30+
if new_cost < old_cost:
31+
pq.put((new_cost, neighbor))
32+
D[neighbor] = new_cost
33+
return D
34+
35+
36+
def main():
37+
g = Graph(9)
38+
g.add_edge(0, 1, 4)
39+
g.add_edge(0, 6, 7)
40+
g.add_edge(1, 6, 11)
41+
g.add_edge(1, 7, 20)
42+
g.add_edge(1, 2, 9)
43+
g.add_edge(2, 3, 6)
44+
g.add_edge(2, 4, 2)
45+
g.add_edge(3, 4, 10)
46+
g.add_edge(3, 5, 5)
47+
g.add_edge(4, 5, 15)
48+
g.add_edge(4, 7, 1)
49+
g.add_edge(4, 8, 5)
50+
g.add_edge(5, 8, 12)
51+
g.add_edge(6, 7, 1)
52+
g.add_edge(7, 8, 3)
53+
54+
D = g.dijkstra(0)
55+
print(D)
56+
57+
for vertex in range(len(D)):
58+
print("Distance from vertex 0 to vertex", vertex, "is", D[vertex])
59+
60+
61+
if __name__=="__main__":
62+
main()

‎structure.md‎

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
### Repo Structure
1+
##Graphs in Python Repo Structure
22
- base_classes
33
- `node.py`
44
- `graph.py`
@@ -8,18 +8,25 @@
88
- `adj_matrix_graph.py`
99
- `adj_list_graph.py`
1010

11-
- lessions
12-
- representing_graphs_in_code
13-
- base_classes
14-
- `node.py`
15-
- `graph.py`
16-
- `edge_list_graph.py`
17-
- `adj_matrix_graph.py`
18-
- `adj_list_graph.py`
19-
- `test.py`
20-
- depth_first_search
21-
- breadth_first_search
22-
- dijkstra
11+
- graphs_in_python_lessions
12+
- 01_representing_graphs_in_code
13+
- advanced_implementations
14+
- base_classes
15+
- `node.py`
16+
- `graph.py`
17+
- `edge_list_graph.py`
18+
- `adj_matrix_graph.py`
19+
- `adj_list_graph.py`
20+
- `test.py`
21+
- `edge_list_graph_basic.py`
22+
- `adj_matrix_graph_basic.py`
23+
- `adj_list_graph_basic.py`
24+
- 02_depth_first_search
25+
- `dfs.py`
26+
- 03_breadth_first_search
27+
- `bfs.py`
28+
- 04_dijkstra
29+
- `dijkstra.py`
2330
- a*
2431
- mst_boruvka
2532
- mst_kruskal

0 commit comments

Comments
(0)

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