CSCI 8080 Design and Analysis of Algorithms: Greedy Algorithm
CSCI 8080 Design and Analysis of Algorithms: Greedy Algorithm
Programming Assignment #1
Greedy Algorithm
1. Develop a graphic user interface that allows a user to set up and display a 2D point set on
screen by (1) moving and clicking the mouse, and (2) using a random number generator.
2. Make all the points connected (i.e., a path between any two points) by implementing the
two algorithms listed below.
3. Display the connected point set (i.e., the points and connecting lines) on screen.
4. Test your program with 10, 100, 1000 points respectively.
5. Analyze the computational complexity, O(.), Ω(.), and Θ(.) of the algorithms.
1. A greedy algorithm
Give (and display) a 2D point set P
1. Initialize a point s = 0
2. s = a randomly selected point p0∈P; P ← P – {p0}
3. While P is not empty
Find a pi such that it has the minimum distance(pi, s) among all pi∈P
Draw line pi-s
s = pi; P ← P – {pi}
Endwhile
Computation:
1. lpu-pv ← 2D-SS(P); // lpu-pv denotes a line formed by two pints pu and pv; i.e., lpu-pv ::= { pu,
pv} ⊆ P; function 2D-SS(P) returns a support segment(SS) in 2D
2. For every point pj in P–{pu, pv} // bisect points between half hulls
If lpu-pv(pj) > 0 P+ ← P+ ∪ {pj}; Otherwise P- ← P- ∪ {pj};
// lpu-pv(pj) = 0 if pi is on line lpu-pv
// lpu-pv(pj) < 0 if pi is right of (below) line lpu-pv
// lpu-pv(pj) > 0 if pi is left of (above) line lpu-pv
// P+ ∪ P- ∪ {pu, pv} = P
3. 2D-Tri-Explore(lpu-pv, P+); // convex hull on points set P+ below line lpu-pv; P+ ⊂ P;
2D-Tri-Explore(lpv-pu, P-); // convex hull on point set P- above line lpu-pv; P- ⊂ P;
Computation:
0. If |Pss| =0 return lss; // |Pss| is the number of points in Pss.
1. pk ← pi : pi ∈ Pss ∩ max
i
lss(pi); // pk ∈ Pss and has maximum distance to lss.
2. For every point pj in set (Pss–{pk}) // bisect and prune points
If lpu-pk(pj) > 0 P+ ← P+ ∪ {pj}; If lpk-pv(pj) > 0 P- ← P- ∪ {pj};
// P+ ∪ P- ∪ {pk} ⊆ Pss; Points in Pss-P+-P--{pk} are pruned.
3. 2D-Tri-Explore(lpu-pk, P+);
// convex hull on points set P+ in the half-space below line lpu-pk;
2D-Tri-Explore(lpk-pv, P-);
// convex hull on point set P- in the half-space above line lpk-pv;
Function 2D-SS(P) // Compute the level one support surface (segment) of P
Pre-condition: P = {pi; i = 1, 2, …, N}; a set of N 2-dimensional points.
Post-condition: lpu-pv ::= {pu, pv}; a line segment defined on point set {pu, pv}.
Computation:
N N
1 1
1. pc = [c1, c2] = [
N
∑ p i1 ,
i =1 N
∑p
i =1
i2 ]; // c is the centroid of all points pi ∈ P
2. pu ← pi : pi ∈ P & max
i
D(pi, pc); // pu in P and farthest to pc.
1. Given a 2D point set P = {p0, p1, …, pn), design your own algorithm that draws a
minimum sized (i.e., minimum radius) circle that encloses all the points.
2. Display the point set and the circle on screen.
3. Describe your algorithm in English
4. Test your program with 10, 100, 1000 points respectively.
5. Analyze how close is the size of your circle (i.e., the result of your algorithm) to the
optimal one.
6. Analyze the computational complexity, O(.), Ω(.), and Θ(.) of your algorithm