You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: bonus-assignment/README.md
+34-34Lines changed: 34 additions & 34 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,19 +1,19 @@
1
1
# K-Minimum Spanning Tree - Bonus Assignment
2
2
## Description
3
-
This project given n points in the
4
-
two-dimensional space and a positive integer k (k<=n),
5
-
constructs a tree spanning at least k of
6
-
these points such that the total length of the tree is at most O(k ^(1/4))
7
-
times that of a minimum-length tree spanning any k of the points.
3
+
This project given $n$ points in the
4
+
two-dimensional space and a positive integer $k$ $(k<=n) $,
5
+
constructs a tree spanning at least $k$ of
6
+
these points such that the total length of the tree is at most $O(k ^{1/4} )$
7
+
times that of a minimum-length tree spanning any $k$ of the points.
8
8
In the best case the minimum tree constructed consists of
9
-
exactly k points. If that is not possible the tree will
10
-
consist of k+m points where m>0 and k+m<n and m the least integer so that the square root
11
-
of (k+m) has no decimal places (root (k+m) is an integer).
9
+
exactly $k$ points. If that is not possible the tree will
10
+
consist of $k+m$ points where $m>0$ and $k+m < n$ and $m$ the least integer so that the square root
11
+
of $(k+m)$ has no decimal places ( $\sqrt{k+m}$ is an integer).
12
12
Lastly, in the worst case scenario if there is no such number
13
-
as m, the minimum spanning tree will consist of exactly n points.
13
+
as $m$, the minimum spanning tree will consist of exactly $n$ points.
14
14
The output of the project describes the number of points that the
15
15
MST (minimum-spanning tree) consists of, the length of the MST
16
-
(the sum of distances of all the connections used in the MST)
16
+
(the sum of distances of all the connections used in the MST)
17
17
and finally the connections of the points that are used to construct
18
18
the MST. Full description is found [here](https://github.com/stef4k/Algorithms-and-data-structures-assignments2019/blob/main/bonus-assignment/ravi1996.pdf)
19
19
@@ -32,37 +32,37 @@ have a good chance of containing oil under the sea bed. The company has a limite
32
32
number of oil rigs that it is willing to invest in the effort. The problem is to position
33
33
these oil rigs at marked places so that the cost of laying down pipelines between these
34
34
rigs is minimized. The problem at hand can be modeled as follows: given a graph with
35
-
nonnegative edge weights and a specified number k, find a tree of minimum weight
36
-
spanning at least k nodes. The solution to the problem will be a tree spanning
37
-
exactly k nodes. This problem is called the k-minimum spanning tree (kMST).
35
+
nonnegative edge weights and a specified number $k$, find a tree of minimum weight
36
+
spanning at least $k$ nodes. The solution to the problem will be a tree spanning
37
+
exactly $k$ nodes. This problem is called the k-minimum spanning tree (kMST).
38
38
39
39
## Algorithm
40
-
Before calculations start, k is checked if it has a square root without any decimal places. If not, k is continuously
41
-
increased till it becomes an integer whose square root has no decimal places or till it becomes equal to n . In those cases, k will be changed.
40
+
Before calculations start, $k$ is checked if it has a square root without any decimal places. If not, $k$ is continuously
41
+
increased till it becomes an integer whose square root has no decimal places or till it becomes equal to $n$. In those cases, $k$ will be changed.
42
42
43
-
If k is equal to n, there will be only one MST (the one that contains all the n points). As a result, the MST of the n points
43
+
If $k$ is equal to $n$, there will be only one MST (the one that contains all the $n$ points). As a result, the MST of the $n$ points
44
44
is constructed by using the Prim's algorithm for finding minimum spanning trees. Via the MST, the length of the tree is calculated
45
45
and the appropriate output is made.
46
46
47
-
In any other case ( it will be: k < n ), the right k points must be estimated in order to find the MST for those k points. Let's
48
-
call S={s1,s2,...,sn} the given set of points and d(i,j) the Euclidean distance between si and sj. In order to find
49
-
the right k points and followingly the MST, the following steps must be pursued, for each distinct pair of points si, sj in S:
50
-
1. Construct the circle C with diameter D = root(3) * d(i, j) centered at the
47
+
In any other case ( it will be: $k < n$ ), the right $k$ points must be estimated in order to find the MST for those $k$ points. Let's
48
+
call $S={s1,s2,...,sn}$ the given set of points and $d(i,j)$ the Euclidean distance between si and sj. In order to find
49
+
the right $k$ points and followingly the MST, the following steps must be pursued, for each distinct pair of points si, sj in S:
50
+
1. Construct the circle C with diameter $D = \sqrt{3} * d(i, j)$ centered at the
51
51
midpoint of the line segment (si, sj)
52
52
2. Let Sc be the subset of S contained in C. If Sc contains fewer than
53
-
k points, skip to the next iteration of the loop (i.e., try the next pair of
53
+
$k$ points, skip to the next iteration of the loop (i.e., try the next pair of
54
54
points). Otherwise, do the following.
55
55
3. Let Q be the square of side D circumscribing C.
56
-
4. Divide Q into k square cells each with side = D / root(k)
56
+
4. Divide Q into $k$ square cells each with $side = D / \sqrt{k}$
57
57
5. Sort the cells by the number of points from Sc they contain and
58
58
choose the minimum number of cells so that the chosen cells together
59
-
contain at least k points. In case there are more than k points in the chosen cells, randomly discard points from
59
+
contain at least $k$ points. In case there are more than $k$ points in the chosen cells, randomly discard points from
60
60
the last chosen cell so that the total number of points in all the cells is
61
-
equal to k.
62
-
6. Construct a minimum spanning tree for the k chosen points.
63
-
7. The solution value for the pair (si, sj) is the length of this MST.
61
+
equal to $k$.
62
+
6. Construct a minimum spanning tree for the $k$ chosen points.
63
+
7. The solution value for the pair $(si, sj)$ is the length of this MST.
64
64
65
-
From all the distinct pairs select the pair with the minimum length of its MST. That MST is the soltuion and together with the length and the connections,
65
+
From all the distinct pairs select the pair with the minimum length of its MST. That MST is the solution and together with the length and the connections,
66
66
the appropriate output is made.
67
67
68
68
@@ -92,11 +92,11 @@ pointn (abscissan, ordinaten)
92
92
93
93
```
94
94
95
-
* <k_number> is the minimum integer number of points that the tree will span, so the following must be valid: k_number > 1 and k_number <=n
95
+
* <k_number> is the minimum integer number of points that the tree will span, so the following must be valid: $knumber > 1$ and $knumber <=n$
96
96
97
97
## Examples
98
98
### Example 1
99
-
* Running the file points1.txt for k=4
99
+
* Running the file points1.txt for $k=4$
100
100
```
101
101
python kmst.py points1.txt 4
102
102
```
@@ -108,9 +108,9 @@ The links that were used are:
108
108
('s5', 's7'): 1.0,
109
109
('s7', 's6'): 1.4142135623730951}
110
110
```
111
-
In conlusion, it is noticed that the k we inputed has a square root without decimal places (root(4)=2), as a result the MST contains exactly 4 points.
111
+
In conlusion, it is noticed that the k we inputed has a square root without decimal places $(\sqrt{4} = 2)$, as a result the MST contains exactly 4 points.
112
112
113
-
* Running the file points1.txt for k=5
113
+
* Running the file points1.txt for $k=5$
114
114
```
115
115
python kmst.py points1.txt 5
116
116
```
@@ -131,7 +131,7 @@ In conclusion, it is noticable that the MST that is constructed contains 9 point
131
131
As a result, k is transformed into 9 whose root equals to 3.
132
132
133
133
### Example 2
134
-
* Running the file points2.txt for k=7
134
+
* Running the file points2.txt for $k=7$
135
135
```
136
136
python kmst.py points2.txt 7
137
137
```
@@ -150,7 +150,7 @@ The links that were used are:
150
150
```
151
151
To sum up, k is changed from 7 to 9 and the MST for 9 points is calculated.
152
152
153
-
* Running the file points2.txt for k=16
153
+
* Running the file points2.txt for $k=16$
154
154
```
155
155
python kmst.py points2.txt 16
156
156
```
@@ -178,7 +178,7 @@ Last but not least, k is not changed because square root of 16 equals to 4. As a
0 commit comments