Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ venv.bak/
# Rope project settings
.ropeproject

.idea

# mkdocs documentation
/site

Expand Down
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,12 @@
# Algorithms

# Algorithms lab_4

## Task
Build directed and weighted graph from input and implement Dijkstra algorithm to find average distance from vertex S
to all available vertexes

## How to run
+ `cd` into folder where you want to store this repository
+ Clone this repository `git clone https://github.com/MKruchok/Algorithms.git`
+ Choose branch lab_4 with command `git checkout lab_4`
+ Go into folder with files with command `cd Algorithms`
+ run `py main.py` on Windows
1 change: 1 addition & 0 deletions dijkstra.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
5.555555555555555
31 changes: 31 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from queue import PriorityQueue
from collections import defaultdict


class Graph:

def __init__(self, vertices):
self.vertices = vertices
self.graph = defaultdict(list)
self.distance = [999] * self.vertices

def add_line(self, first_vert, second_vert, weight):
self.graph[first_vert].append((second_vert, weight))
self.graph[second_vert].append((first_vert, weight))

def dijkstra(self, main_vertex):
self.distance[main_vertex] = 0
q = PriorityQueue()
q.put((self.distance[main_vertex], main_vertex))
while not q.empty():
dist, current_vertex = q.get()
for adj_vertex, weight in self.graph[current_vertex]:
if self.distance[current_vertex] + weight < self.distance[adj_vertex]:
self.distance[adj_vertex] = self.distance[current_vertex] + weight
q.put((self.distance[adj_vertex], adj_vertex))
return self.distance


def insert_out(result):
file = open("dijkstra.out", 'w')
file.write(str(result))
65 changes: 65 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import unittest
from main import Graph


class TestDijkstra(unittest.TestCase):
def setUp(self):
self.graph = Graph(9)
self.graph.add_line(2, 3, 6)
self.graph.add_line(1, 6, 12)
self.graph.add_line(1, 7, 24)
self.graph.add_line(1, 2, 9)
self.graph.add_line(0, 4, 1)
self.graph.add_line(0, 1, 14)
self.graph.add_line(4, 8, 5)
self.graph.add_line(3, 5, 5)
self.graph.add_line(2, 4, 34)
self.graph.add_line(3, 4, 3)
self.graph.add_line(4, 5, 9)
self.graph.add_line(4, 7, 1)
self.graph.add_line(5, 8, 12)
self.graph.add_line(7, 8, 3)
self.graph.add_line(6, 7, 1)

def test_0(self):
self.distance_main = self.graph.dijkstra(0)
self.distance_sum = 0
for end_vertex in range(len(self.distance_main)):
print("Distance from", 0, "to", end_vertex, "==", self.distance_main[end_vertex])
self.distance_sum += self.distance_main[end_vertex]
result = [0, 14, 10, 4, 1, 9, 3, 2, 5]
self.middle = self.distance_sum / len(self.distance_main)
print("Arithmetic mean ==", round(self.middle, 2))
self.assertEqual(self.distance_main, result)

def test_1(self):
result = [14, 0, 9, 15, 14, 20, 12, 13, 16]
self.assertEqual(self.graph.dijkstra(1), result)

def test_2(self):
result = [10, 9, 0, 6, 9, 11, 11, 10, 13]
self.assertEqual(self.graph.dijkstra(2), result)

def test_3(self):
result = [4, 15, 6, 0, 3, 5, 5, 4, 7]
self.assertEqual(self.graph.dijkstra(3), result)

def test_4(self):
result = [1, 14, 9, 3, 0, 8, 2, 1, 4]
self.assertEqual(self.graph.dijkstra(4), result)

def test_5(self):
result = [9, 20, 11, 5, 8, 0, 10, 9, 12]
self.assertEqual(self.graph.dijkstra(5), result)

def test_6(self):
result = [3, 12, 11, 5, 2, 10, 0, 1, 4]
self.assertEqual(self.graph.dijkstra(6), result)

def test_7(self):
result = [2, 13, 10, 4, 1, 9, 1, 0, 3]
self.assertEqual(self.graph.dijkstra(7), result)

def test_8(self):
result = [5, 16, 13, 7, 4, 12, 4, 3, 0]
self.assertEqual(self.graph.dijkstra(8), result)