Skip to content

Commit 88069dd

Browse files
committed
Point-Cover-Interval Problem
1 parent 1c9333f commit 88069dd

File tree

2 files changed

+96
-2
lines changed

2 files changed

+96
-2
lines changed

ipython_nbs/essentials/greedy-algorithm-intro.ipynb

Lines changed: 96 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@
205205
"cell_type": "markdown",
206206
"metadata": {},
207207
"source": [
208-
"## 0-1 Knapsack"
208+
"### 0-1 Knapsack"
209209
]
210210
},
211211
{
@@ -336,7 +336,7 @@
336336
"cell_type": "markdown",
337337
"metadata": {},
338338
"source": [
339-
"## Fractional Knapsack"
339+
"### Fractional Knapsack"
340340
]
341341
},
342342
{
@@ -457,6 +457,100 @@
457457
"knappsack_fract(capacity=10, weights=weights, values=values)"
458458
]
459459
},
460+
{
461+
"cell_type": "markdown",
462+
"metadata": {},
463+
"source": [
464+
"## Example 3: Point-Cover-Interval Problem"
465+
]
466+
},
467+
{
468+
"cell_type": "markdown",
469+
"metadata": {},
470+
"source": [
471+
"The classic Point-Cover-Interval problem is another example that is well suited for demonstrating greedy algorithms. Here, we are given a set of Intervals *L*, and we want to find the minimum set of points so that each interval is covered at least once by a given point as illustrated in the example below:\n",
472+
"\n",
473+
"![](./images/point-cover-interval-ex.png)"
474+
]
475+
},
476+
{
477+
"cell_type": "markdown",
478+
"metadata": {},
479+
"source": [
480+
"Our greedy strategy, which finds the optimal solution for this problem, can be as follows:\n",
481+
"\n",
482+
"- sort intervals in increasing order by the value of their endpoints\n",
483+
"- for interval in interval-set:\n",
484+
" - if interval is not yet covered:\n",
485+
" - add interval-endpoint to the set of points"
486+
]
487+
},
488+
{
489+
"cell_type": "code",
490+
"execution_count": 2,
491+
"metadata": {
492+
"collapsed": true
493+
},
494+
"outputs": [],
495+
"source": [
496+
"def min_points(intervals):\n",
497+
" s_ints = sorted(intervals, key=lambda x: x[1])\n",
498+
"\n",
499+
" points = [s_ints[0][-1]]\n",
500+
"\n",
501+
" for interv in s_ints:\n",
502+
" if not(points[-1] >= interv[0] and points[-1] <= interv[-1]):\n",
503+
" points.append(interv[-1])\n",
504+
" \n",
505+
" return points"
506+
]
507+
},
508+
{
509+
"cell_type": "code",
510+
"execution_count": 4,
511+
"metadata": {
512+
"collapsed": false
513+
},
514+
"outputs": [
515+
{
516+
"data": {
517+
"text/plain": [
518+
"[3]"
519+
]
520+
},
521+
"execution_count": 4,
522+
"metadata": {},
523+
"output_type": "execute_result"
524+
}
525+
],
526+
"source": [
527+
"pts = [[2, 5], [1, 3], [3, 6]] \n",
528+
"min_points(pts)"
529+
]
530+
},
531+
{
532+
"cell_type": "code",
533+
"execution_count": 5,
534+
"metadata": {
535+
"collapsed": false
536+
},
537+
"outputs": [
538+
{
539+
"data": {
540+
"text/plain": [
541+
"[3, 6]"
542+
]
543+
},
544+
"execution_count": 5,
545+
"metadata": {},
546+
"output_type": "execute_result"
547+
}
548+
],
549+
"source": [
550+
"pts = [[4, 7], [1, 3], [2, 5], [5, 6]]\n",
551+
"min_points(pts)"
552+
]
553+
},
460554
{
461555
"cell_type": "markdown",
462556
"metadata": {},
Loading

0 commit comments

Comments
 (0)