{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Workbook for Chapter 12 of Explorations in Computing"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Section 12.2: Exhaustive Search"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##### Execute this cell to initialize the notebook:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"from PythonLabs.TSPLab import *"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Exercises:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"T11. Make a string for the permutation experiments:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"T12. Before calling the iterator that generates all permutations of this string, can you predict how many permutations are possible?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"T13. Use `each_permutation` to print every possible ordering of the letters in the string:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"T14. The TSPLab module includes a method named `factorial` that will compute the factorial of a number. Call `factorial` to compute $4!$:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"T15. Another way to count permutations is to make a list of all permutations and then ask Python for the length of the list:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"T16. Call a method named `size` to get the number of cities in the map:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"T17. A method named `ntours` will compute $(n-1)! / 2$, the number of tours in a map of a specified size. Call `ntours` to compute the number of tours in the map of Ireland:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"T18. Verify the claim that calling `make_tour` with no arguments returns a Tour object with the cities in alphabetical order:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"T19. Type the `for` statement that uses `each_tour` to generate every tour:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### `xsearch` "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"T20. Use the code cell below to define your own version of `xsearch`, the function shown in Figure 12.5. If you want to copy and paste the code you'll find it at `xsearch.py`"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Execute the code in this cell to create a Map object containing the distances between the cities in Ireland:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"m = Map(path_to_data('ireland.txt'))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"T21. Call your `xsearch` function, passing it the map of Ireland:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.4.2"
}
},
"nbformat": 4,
"nbformat_minor": 0
}