3.17 Algorithmic Efficiency

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 the 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 take sa proportional amount of time. 1 job = +2 time
    • Exponential Efficiency (Bad): More work takes an exponential amount of time. 1 job = x2 time
  • Heuristic Approach: When optimal solutions are inefficient, look for a possibly optimal solution that is more effecient
  • Decidable Problem: A decision problme that has a clear solution that will always make a correct output
  • Undecidable Problem: A decision problem with no solution that is not guaranteed to produce the correct output

Notes

  • There are levels to Polynomial Efficiency
  • More cycles of code function = more time required to complete
  • Exponential efficiency is much less efficient because it takes many more cycles to complete
  • A Heuristic Approach sacrifices optimal solutions to improve efficiency and ease of programming

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):
    #--------------------
    # Basic Binary Search
    low = 0
    high = len(array) - 1
    while low < high:
        middle = (low + high) // 2
        if(array[middle] == value):
            return True
#            high = middle - 1
        elif array[middle] < value:
            low = 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') 
0.37116503715515137 seconds

3.18 Undecidable Problems

Notes

  • Undecidable Problems are paradoxical
  • In a divisible by 3 example, the answer will alwyas return the correct answer, either true or false. This is a decidable problem
  • Don't feed functions with their own instructions??
  • Some problems cannot be solved by a computer

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
    },
    'RanchoBernardo':{
        'Westview':45,
        'MtCarmel':30,
        'Poway':15,
        'DelNorte':50
    }
}


def fastestroute(start,data):
   drivetime = 0
   order = []
   for key1, value1 in dataset.items():
        print("key1", key1, "value1", value1)
        print(value1)
        temp = ""
        temp += key1
        lowestValue = 0
        for key2, value2 in value1.items():
            print("value2", value2)    
            if lowestValue == 0:
                 lowestValue = value2
            if value2 <= lowestValue:
                 lowestValue = value2
                 drivetime = drivetime + lowestValue
                 lowestKey = key2
                 order.append(key2)
            print("Lowest Value is:", lowestValue)
            print("Drivetime:", drivetime)
            print("Lowest Key", lowestKey)
        print(order)
        return(drivetime,order)
   dataset['start']
   for data in dataset:
        print(drivetime, order)

def nested_dict_values_iterator(data):
 for value in data.values():
     if isinstance(value, dict):
         for v in nested_dict_values_iterator(value):
             yield v
         else:
             yield value
             
#for value in nested_dict_values_iterator(dataset):
 #   print(value)
                 

def fastestroute2(start, data):
    drivetime = 0
    order = []
    order.append(start)
    global lowestKey
    lowestKey = start
    for i in dataset:
        #print("Dataset", dataset[i])
        #print("data i: ", data[i].keys())
        #print(data[i].values())
        #print("Data Poway", data['Poway'])
        for key2, value2 in dataset.items():
            key2 = lowestKey
            print("First Dictionary key2", key2, "value2", value2)
            #print(value2)
            temp = ""
            temp += key2
            lowestValue = 0
            for key2, value2 in value2.items():
                print("Second Dictionary key2", key2)
                print("value2", value2)    
                if lowestValue == 0:
                    lowestValue = value2
                if value2 <= lowestValue and key2 not in order:
                    lowestValue = value2
                    drivetime = drivetime + lowestValue
                    lowestKey = key2
                    print("Lowest key inside Second Dictionary : ", lowestKey)
                    # check if key is not already in order[]
                    if key2 not in order:
                        order.append(key2)
                print("Lowest Value is:", lowestValue)
                print("Drivetime:", drivetime)
                print("Lowest Key", lowestKey)
            print(order)
        return(drivetime,order)
start = 'DelNorte'
fastestroute2(start,dataset)

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

#print(data)
#    print(data, dataset[data])
#print(dataset.values())
#min(dataset, key=dataset.get)
#print(dataset.values())
#print(max(dataset.values()))
First Dictionary key2 DelNorte value2 {'Westview': 15, 'MtCarmel': 20, 'Poway': 35, 'RanchoBernrdo': 50}
Second Dictionary key2 Westview
value2 15
Lowest key inside Second Dictionary :  Westview
Lowest Value is: 15
Drivetime: 15
Lowest Key Westview
Second Dictionary key2 MtCarmel
value2 20
Lowest Value is: 15
Drivetime: 15
Lowest Key Westview
Second Dictionary key2 Poway
value2 35
Lowest Value is: 15
Drivetime: 15
Lowest Key Westview
Second Dictionary key2 RanchoBernrdo
value2 50
Lowest Value is: 15
Drivetime: 15
Lowest Key Westview
['DelNorte', 'Westview']
First Dictionary key2 Westview value2 {'DelNorte': 15, 'MtCarmel': 35, 'Poway': 25, 'RanchoBernrdo': 45}
Second Dictionary key2 DelNorte
value2 15
Lowest Value is: 15
Drivetime: 15
Lowest Key Westview
Second Dictionary key2 MtCarmel
value2 35
Lowest Value is: 15
Drivetime: 15
Lowest Key Westview
Second Dictionary key2 Poway
value2 25
Lowest Value is: 15
Drivetime: 15
Lowest Key Westview
Second Dictionary key2 RanchoBernrdo
value2 45
Lowest Value is: 15
Drivetime: 15
Lowest Key Westview
['DelNorte', 'Westview']
First Dictionary key2 Westview value2 {'Westview': 35, 'DelNorte': 20, 'Poway': 40, 'RanchoBernrdo': 30}
Second Dictionary key2 Westview
value2 35
Lowest Value is: 35
Drivetime: 15
Lowest Key Westview
Second Dictionary key2 DelNorte
value2 20
Lowest Value is: 35
Drivetime: 15
Lowest Key Westview
Second Dictionary key2 Poway
value2 40
Lowest Value is: 35
Drivetime: 15
Lowest Key Westview
Second Dictionary key2 RanchoBernrdo
value2 30
Lowest key inside Second Dictionary :  RanchoBernrdo
Lowest Value is: 30
Drivetime: 45
Lowest Key RanchoBernrdo
['DelNorte', 'Westview', 'RanchoBernrdo']
First Dictionary key2 RanchoBernrdo value2 {'Westview': 25, 'MtCarmel': 40, 'DelNorte': 35, 'RanchoBernrdo': 15}
Second Dictionary key2 Westview
value2 25
Lowest Value is: 25
Drivetime: 45
Lowest Key RanchoBernrdo
Second Dictionary key2 MtCarmel
value2 40
Lowest Value is: 25
Drivetime: 45
Lowest Key RanchoBernrdo
Second Dictionary key2 DelNorte
value2 35
Lowest Value is: 25
Drivetime: 45
Lowest Key RanchoBernrdo
Second Dictionary key2 RanchoBernrdo
value2 15
Lowest Value is: 25
Drivetime: 45
Lowest Key RanchoBernrdo
['DelNorte', 'Westview', 'RanchoBernrdo']
First Dictionary key2 RanchoBernrdo value2 {'Westview': 45, 'MtCarmel': 30, 'Poway': 15, 'DelNorte': 50}
Second Dictionary key2 Westview
value2 45
Lowest Value is: 45
Drivetime: 45
Lowest Key RanchoBernrdo
Second Dictionary key2 MtCarmel
value2 30
Lowest key inside Second Dictionary :  MtCarmel
Lowest Value is: 30
Drivetime: 75
Lowest Key MtCarmel
Second Dictionary key2 Poway
value2 15
Lowest key inside Second Dictionary :  Poway
Lowest Value is: 15
Drivetime: 90
Lowest Key Poway
Second Dictionary key2 DelNorte
value2 50
Lowest Value is: 15
Drivetime: 90
Lowest Key Poway
['DelNorte', 'Westview', 'RanchoBernrdo', 'MtCarmel', 'Poway']
(90, ['DelNorte', 'Westview', 'RanchoBernrdo', 'MtCarmel', 'Poway'])

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