Unit 3 Sections 17 and 18
Vocabulary
- Problem: a general description of a task that can or cannot be solved algorithmically
- Decision Problem: A problem with a yes or no answer
- Organization Problem: a problem with a goal of finding the best answer
- Instance: a problem with a specific input
- Efficiency: amount of computing needed to solve a problem
- Polynomial Efficiency (Good): more work takes a proportional amount of time (1 job is +2 time)
- Exponential Efficiency (Bad): more work takes an exponential amount more time (1 job is 2x time)
- Heuristic Approach: When optimal solutions are inefficient, look for a possibly optimal solution that is more efficient
- Decidable Problem: A decision problem that has a clear solution that will always make a correct output
- Undecidable Problem: A decision problem with no solution that is not gaurenteed to produce the correct output
import time
numlist = [1,3,5,7,9,11,13,15,17,19]
valuelist = [0,3,6,9,12,15,18,21]
def isvalue(value,array):
#--------------------
up = len(array) - 1
down = 0
while up >= down:
middle = (up + down) // 2
if array[middle] == value:
return True
elif array[middle] > value:
up = middle - 1
else:
down = middle + 1
return False
#--------------------
starttime = time.time()
for i in range(100000):
for i in range(len(valuelist)):
x = isvalue(valuelist[i],numlist)
endtime = time.time()
print(endtime-starttime,'seconds')
Homework!
Make an algorithm that finds the fastest route that hits every location once starting and ending at Del Norte. Make sure to show your thinking. If you are strugling, try using a huristic approach. Remember, what matters more than having perfectly functioning code is that you tried your hardest.
dataset = {
'DelNorte':{
'Westview':15,
'MtCarmel':20,
'Poway':35,
'RanchoBernrdo':50
},
'Westview':{
'DelNorte':15,
'MtCarmel':35,
'Poway':25,
'RanchoBernrdo': 45
},
'MtCarmel':{
'Westview':35,
'DelNorte':20,
'Poway':40,
'RanchoBernrdo':30
},
'Poway':{
'Westview':25,
'MtCarmel':40,
'DelNorte':35,
'RanchoBernrdo':15
},
'RanchoBernrdo':{
'Westview':45,
'MtCarmel':30,
'Poway':15,
'Del Norte':50
}
}
I wasn't sure how to do this... So instead I just found the place with the shortest distance from each place (excluding the places it's already been to), and then went to Del Norte at the end to complete the loop...
answer = 0
full = ['Poway', 'Westview', 'MtCarmel', 'RanchoBernrdo']
debug = []
def find(question):
global answer
global debug
dist = 1000
for i in dataset[question]:
temp = dataset[question][i]
if (temp < dist):
if (i in full):
dist = temp
place = i
else:
continue
answer = answer + dist
full.remove(place)
debug.append(place)
return(place)
start = 'DelNorte'
for i in range (0, 4):
new = find(start)
start = new
answer = answer + dataset[start]['DelNorte']
print(answer)
print(debug)