Skip to content

Lab 5 #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
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
25 changes: 2 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,3 @@
# Lab-1
# Lab 5 - Kruskal's algorithm

## Task
Implement a sorting algorithm - HeapSort(ascending/descending)

## Result output
- Algorithm's name
- Execution time
- Counters: swaps, comparisons
- Sorting result

## Code must be covered with tests
- sort the input array
- sort in ascending order of sorted array in ascending order
- sort in descending order of sorted array in ascending order
- sort in ascending order of sorted array in descending order
- sort in descending order of sorted array in descending order

## How to run
- 'cd' into folder where you want to store this repository
- Clone this repository with command 'git clone https://github.com/yeldmitrenko/Algorithms_Labs.git'
- Choose branch lab_1 with command 'git checkout lab_1'
- Go into folder with files with command 'cd Algorithms_Labs'
- run command 'python main.py'
### Kruskal's algorithm for finding a minimum spanning tree
64 changes: 0 additions & 64 deletions heap_sort.py

This file was deleted.

78 changes: 78 additions & 0 deletions kruskal's.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
from typing import List


class Edge:
def __init__(self, start_vertex, end_vertex, weight):
self.start_vertex = start_vertex
self.end_vertex = end_vertex
self.weight = weight


class Graph:
def __init__(self, nodes_number):
self.nodes_number = nodes_number
self.edge_list = List[Edge]
self.mst = []
self.rank = 0
self.parent = self

def add(self, listkk: list[Edge]):
self.edge_list = listkk

def find_root(self):
if self.parent is not self:
self.parent = self.parent.find_root()
return self.parent

def union_sets(self, other):
first_set_root = self.find_root()
second_set_root = other.find_root()
if first_set_root is second_set_root:
return
if first_set_root.rank < second_set_root.rank:
first_set_root.parent = second_set_root
elif first_set_root.rank > second_set_root.rank:
second_set_root.parent = first_set_root
else:
second_set_root.parent = first_set_root
first_set_root.rank += 1

def kruskal_mst(self):
nodes_not_unique = []
for edge in sorted(self.edge_list, key=lambda Edge: Edge.weight):
nodes_not_unique.append(edge.start_vertex)
nodes_not_unique.append(edge.end_vertex)

nodes = list(set(nodes_not_unique))

result = dict((node, Graph(node)) for node in nodes)
print(result)
for edge in sorted(self.edge_list, key=lambda Edge: Edge.weight):

start = result[edge.start_vertex].find_root()
end = result[edge.end_vertex].find_root()

if start is not end:
self.mst.append(edge)
start.union_sets(end)


if __name__ == '__main__':
nodes_number = 7
e1 = Edge(1, 2, 7)
e2 = Edge(1, 4, 5)
e3 = Edge(3, 5, 5)
e4 = Edge(2, 5, 7)
e5 = Edge(5, 7, 9)
e6 = Edge(4, 6, 6)

graph = Graph(nodes_number)
graph.add([e1, e2, e3, e4, e5, e6])
graph.kruskal_mst()

print("\nEdges of minimum spanning tree in graph :", end=' ')
cost = 0
for edge in graph.mst:
print("[" + str(edge.start_vertex) + "-" + str(edge.end_vertex) + "](" + str(edge.weight) + ")", end=' ')
cost += edge.weight
print("\nCost of minimum spanning tree : " + str(cost))
28 changes: 0 additions & 28 deletions test.py

This file was deleted.