|
205 | 205 | "cell_type": "markdown",
|
206 | 206 | "metadata": {},
|
207 | 207 | "source": [
|
208 |
| - "## 0-1 Knapsack" |
| 208 | + "### 0-1 Knapsack" |
209 | 209 | ]
|
210 | 210 | },
|
211 | 211 | {
|
|
336 | 336 | "cell_type": "markdown",
|
337 | 337 | "metadata": {},
|
338 | 338 | "source": [
|
339 |
| - "## Fractional Knapsack" |
| 339 | + "### Fractional Knapsack" |
340 | 340 | ]
|
341 | 341 | },
|
342 | 342 | {
|
|
457 | 457 | "knappsack_fract(capacity=10, weights=weights, values=values)"
|
458 | 458 | ]
|
459 | 459 | },
|
| 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 | + "" |
| 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 | + }, |
460 | 554 | {
|
461 | 555 | "cell_type": "markdown",
|
462 | 556 | "metadata": {},
|
|
0 commit comments