3.17 Algorithmic Efficiency

Vocabulary

  • Problem: general description of a task which can / cant be solved using an algorithm
    • Decision Problem: yes / no answer
    • Organization Problem: problem with multiple aspects
  • Instance: specific input
  • Efficiency: making a large problem simpler using some method
    • Polynomial Efficiency (Good): more work takes proportional amount of time
    • Exponential Efficiency (Bad): more work takes exponential amount of time
  • Heuristic Approach: look for am optimal solution
  • Decidable Problem: problem with a clear decision as the right answer
  • Undecidable Problem: problem with no solution

Notes

  • an efficient program can solve the problem in a short amount of time
  • polynomial efficiency lot better than exponential efficiency (due to time)
  • Heuristic approaches used when the problem is really complex
  • undecidable problems are everywhere and need some thought

Challenge

Try and fix this ineficcient code! Only change the code between the two commented lines. Fully programmed solution will improve your grade, at a minimum show that you tried.

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):
    #--------------------
    for i in value:
        if i in array:
            return True
        else:
            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') 
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/mnt/c/Users/rohan/vscode/RohanRepository/_notebooks/2022-12-14-117-118-homework.ipynb Cell 6 in <cell line: 13>()
     <a href='vscode-notebook-cell://wsl%2Bubuntu/mnt/c/Users/rohan/vscode/RohanRepository/_notebooks/2022-12-14-117-118-homework.ipynb#W5sdnNjb2RlLXJlbW90ZQ%3D%3D?line=12'>13</a> for i in range(100000):
     <a href='vscode-notebook-cell://wsl%2Bubuntu/mnt/c/Users/rohan/vscode/RohanRepository/_notebooks/2022-12-14-117-118-homework.ipynb#W5sdnNjb2RlLXJlbW90ZQ%3D%3D?line=13'>14</a>     for i in range(len(valuelist)):
---> <a href='vscode-notebook-cell://wsl%2Bubuntu/mnt/c/Users/rohan/vscode/RohanRepository/_notebooks/2022-12-14-117-118-homework.ipynb#W5sdnNjb2RlLXJlbW90ZQ%3D%3D?line=14'>15</a>         x = isvalue(valuelist[i],numlist)
     <a href='vscode-notebook-cell://wsl%2Bubuntu/mnt/c/Users/rohan/vscode/RohanRepository/_notebooks/2022-12-14-117-118-homework.ipynb#W5sdnNjb2RlLXJlbW90ZQ%3D%3D?line=15'>16</a> endtime = time.time()
     <a href='vscode-notebook-cell://wsl%2Bubuntu/mnt/c/Users/rohan/vscode/RohanRepository/_notebooks/2022-12-14-117-118-homework.ipynb#W5sdnNjb2RlLXJlbW90ZQ%3D%3D?line=16'>17</a> print(endtime-starttime,'seconds')

/mnt/c/Users/rohan/vscode/RohanRepository/_notebooks/2022-12-14-117-118-homework.ipynb Cell 6 in isvalue(value, array)
      <a href='vscode-notebook-cell://wsl%2Bubuntu/mnt/c/Users/rohan/vscode/RohanRepository/_notebooks/2022-12-14-117-118-homework.ipynb#W5sdnNjb2RlLXJlbW90ZQ%3D%3D?line=3'>4</a> def isvalue(value,array):
      <a href='vscode-notebook-cell://wsl%2Bubuntu/mnt/c/Users/rohan/vscode/RohanRepository/_notebooks/2022-12-14-117-118-homework.ipynb#W5sdnNjb2RlLXJlbW90ZQ%3D%3D?line=4'>5</a>     #--------------------
----> <a href='vscode-notebook-cell://wsl%2Bubuntu/mnt/c/Users/rohan/vscode/RohanRepository/_notebooks/2022-12-14-117-118-homework.ipynb#W5sdnNjb2RlLXJlbW90ZQ%3D%3D?line=5'>6</a>     for i in value:
      <a href='vscode-notebook-cell://wsl%2Bubuntu/mnt/c/Users/rohan/vscode/RohanRepository/_notebooks/2022-12-14-117-118-homework.ipynb#W5sdnNjb2RlLXJlbW90ZQ%3D%3D?line=6'>7</a>         if i in array:
      <a href='vscode-notebook-cell://wsl%2Bubuntu/mnt/c/Users/rohan/vscode/RohanRepository/_notebooks/2022-12-14-117-118-homework.ipynb#W5sdnNjb2RlLXJlbW90ZQ%3D%3D?line=7'>8</a>             return True

TypeError: 'int' object is not iterable

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':{
        'Del Norte':15,
        'MtCarmel':35,
        'Poway':25,
        'RanchoBernrdo': 45
    },
    'MtCarmel':{
        'Westview':35,
        'Del Norte':20,
        'Poway':40,
        'RanchoBernrdo':30
    },
    'Poway':{
        'Westview':25,
        'MtCarmel':40,
        'Del Norte':35,
        'RanchoBernrdo':15
    },
    'RanchoBernardo':{
        'Westview':45,
        'MtCarmel':30,
        'Poway':15,
        'Del Norte':50
    }
}
def fastestroute(start,data):
    drivetime = 0
    order = []
   order.append(start)
   while len(order) < 5:
    return(drivetime,order)
        distance = 0
        for x in data
        for i in data [x]
    order.append (start)

start = 'DelNorte'
fastestroute(dataset)
# 'dataset' is the name of the nested key value pair

Struggles

  • Could not figure out the correct for, if, then, else loops
  • Looked at someone else's code and found some learning opportunities
  • they used two instances of order.append (start)
  • evaluated the data set and used if loops to find the smallest value.

Grading:

Challenge Homework
.15 pts for attempt .65 for attempt
.20 pts for complete .70 for complete
.25 pts for above and beyond .75 pts for above and beyond