C200 Programming Assignment № 11 Dr. M.M. Dalkilic Computer Science School of Informatics, Computing, and Engineering Indiana University, Bloomington, IN, USA December 6, 2020 Contents Introduction 1 Problem 1: Breadth First Search 3 Graph Visualization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Starting Code for Problem 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Output for Problem 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Deliverables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Problem 2: Huffman Encoding and Entropy 6 Huffman Wikipedia Entry . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Starting Code to Problem 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Illustration of Huffman Encoding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Deliverables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Problem 3: Database Queries 9 Output to Problem 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Starting Code to Problem 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Deliverables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Assignment №11 Last Assignment Page 1 Introduction In this homework, you’ll work on translating critical thinking to programming. Since this is your last formal homework, you’re expected to do a bit more. There is not any unit testing accompanying this homework–and, for SQL, you’ll have to read about some of the functions used that you may be unfamiliar with. You will complete this before 11pm, Thursday, December 10th 2020. As always, your group’s work should be your own. Assignment №11 Last Assignment Page 2 Problem 1: BFS For the following problem, we’ll be using our own Queue and Graph class. You’ll have to write the BFS method. Although we discussed the algorithm and we provided most of the code, there’s a little more to do. In the graph (Figure 1), the BFS will visit all nodes if starting with 1; Figure 1: A graph of order 8. however, any other node will not lead to all nodes being visited. BFS actually keeps track of not only visited nodes, but unvisited. Unvisited nodes are the nodes that are in the graph, but the BFS doesn’t visit them when the visited doesn’t change. For example, u = {1, 2, 3, 4, 5, 6, 7, 8} (u is unvisited). We do a BFS(5). We’ll see: 5,6,7,4 or 5,6,4,7 (either one). If v = {5, 6, 7, 4}, then we must look at u − v = {1, 2, 3, 8}. You can use the BFS shown in lecture as a starting point–you must obviously include a queue class (you may copy the one from lab). 1 # ####################### 2 # Problem One 3 # ####################### 4 5 #### Queue class omitted from PDF (for space) 6 #### It is availabe in the code 7 #### 8 9 10 class Graph: 11 def __init__(self, nodes): 12 self.nodes = nodes 13 self.edges = {} 14 for i in self.nodes: Assignment №11 Last Assignment Page 3 self.edges[i] = [] 15 16 def add_edge(self,pair): 17 start, end = pair 18 self.edges[start].append(end) 19 20 21 22 23 24 25 def children(self, node): return self.edges[node] def nodes(self): return str(self.nodes) def __str__(self): return str(self.edges) def bfs(self, node): 26 """ 27 Complete this function based on the information presented in ←Lecture 28. 28 29 Parameters: node (not to start with) 30 Returns: A list of visited nodes BFS -For all other nodes that are not visited (due to no path ←- 31 to the edge), do a BFS untill all notes are visited (Pick from the ←- 32 beginning of the unvisited list) 33 34 NOTE: There are no print statements in this function 35 """ 36 37 if __name__=="__main__": 38 print("Problem 1") 39 print() 40 my_graph = Graph([1,2,3,4,5,6,7,8]) 41 elst = [(1,2),(1,3),(2,8),(3,5),(3,4),(5,6),(6,4),(6,7)] 42 for i in elst: 43 my_graph.add_edge(i) 44 print("{}: {}".format("Edges", my_graph.edges)) 45 nlist = [1,3,6] 46 print() 47 for n in nlist: 48 print("BFS starting with {0}\n\t{1}".format(n, my_graph.bfs(n))) 49 print() Assignment №11 Last Assignment Page 4 Session Output Edges: {1: [2, 3], 2: [8], 3: [5, 4], 4: [], 5: [6], 6: [4, 7], 7: [], 8: []} BFS starting with 1 [1, 2, 3, 8, 5, 4, 6, 7] BFS starting with 3 [3, 5, 4, 6, 7, 1, 2, 8] BFS starting with 6 [6, 4, 7, 1, 2, 3, 8, 5] Starting at 1, all the nodes can be visited using BFS. Starting with 3, children are 4,5, then children of 5: 6,7, then picking randomly, 1, 2, 8. Starting at 6, children are 4,7. Pick one of the unvisited nodes, 1 and continue doing BFS on all the unvisited nodes. Programming Problem 1: BFS • Complete the function. Your output might differ (due to order of nodes of remaining nodes), but you can confirm it is BFS. • Only modify bfs(self, node). You are allowed to create a helper function inside of that function. Assignment №11 Last Assignment Page 5 Problem 2: Huffman encoding and Entropy Computer networks encode their data into bits, send the bits, then decode the bits into messages. Some data is sent often, and so should use fewer bits to represent them, while other data is rare, and, conversely use more bits. Huffman encoding is a way to arrive at a good encoding based upon the relative presence of the data. Assume we have these data and their counts and respective frequencies: Data Count Frequency w 7 0.097 u 12 0.167 x 15 0.208 y 18 0.250 z 20 0.278 We then perform this algorithm: 1 while there are at least two nodes in the list: 2 find the two smallest counts x, y 3 remove x,y from list 4 create a new node with the count x+y 5 and update dictionary with 6 1 added to prefix for all x, 7 0 added to prefix for all y Let’s look at the output before the input: Output Huffman Prcoess [[7, [’w’]], [12, [’u’]], [15, [’x’]], [18, [’v’]], [20, [’y’]]] [[15, [’x’]], [18, [’v’]], [19, [’w’, ’u’]], [20, [’y’]]] [[19, [’w’, ’u’]], [20, [’y’]], [33, [’x’, ’v’]]] [[33, [’x’, ’v’]], [39, [’w’, ’u’, ’y’]]] [[72, [’x’, ’v’, ’w’, ’u’, ’y’]]] Huffman Result {’w’: ’100’, ’u’: ’101’, ’x’: ’00’, ’y’: ’11’, ’v’: ’01’} The last line is the dictionary you’ve built from the Huffman. You can read more here: https://en.wikipedia.org/wiki/Huffman_coding To cut down on clutter, I’m assuming the key values in the dictionary are strings–but I’m representing them here as numbers. At each iteration, observe the two smallest values are combined. For example, [7, [‘w’]] and [12, [‘u’]] yielding [19, [‘w’, ‘u’]]. When combining nodes, all the sym- Assignment №11 Last Assignment Page 6 bols in the list [‘w’,...] have 0 prefixed to the values in the dictionary, and all the values in [‘u’, ...] have 1. This means the dictionary has {‘w’:0, ‘u’:1, ...} with the remainder unchanged. The second iteration combines [15, [‘x’]] and [18,[‘v’]] which gives [33, [‘x’,’v’]]. The dictionary is updated again giving d[‘x’] = 0 and d[‘v’] = 1. The third iteration combines [19,[‘w’,’u’]] and [20, [‘y’]]. This gives a new node [39, [‘w’,’u’,’y’]] where all the symbols in in the node with 19 have 0 prefixed and all the symbols in 20 have 1. The dictionary is then {‘w’:00, ‘u’:01, ‘y’:11,...} with the remainder unchanged. The loop continues until there is only one node. huffman 1 one = lambda x:"1" + x 2 zero = lambda x:"0" + x 3 4 def add_prefix(lst,fn, d): 5 """ 6 DO NOT MODIFY 7 """ 8 for i in lst: d[i] = fn(d[i]) 9 10 11 def make_huffman(data): 12 """ 13 PARAMETER: list 14 RETURN: Nothing -- combine members and update dictionary, use ‘print(←data)‘ to display the list 15 16 You can make use of the lambda functions above: one, zero 17 18 You will not return anything, you will be modifying ‘d‘ by calling ‘←add_prefix‘. 19 Do NOT modify ‘d‘ directly, just use ‘add_prefix‘ 20 21 NOTE: You will need at least 1 print statement, potentially 2 22 """ 23 pass 24 25 if __name__=="__main__": 26 print("") 27 28 print("*" * 40) print("Problem 2") 29 print() 30 empty = "" 31 data = [[7,[’w’]], [12,[’u’]], [15,[’x’]],[20, [’y’]],[18,[’v’]]] 32 d = {d[1][0]:empty for d in data} 33 34 print("Huffman Prcoess") Assignment №11 Last Assignment Page 7 35 make_huffman(data) 36 37 print() 38 print("Huffman Result") 39 print(d) Figure 2: The Huffman Encoding starting at A. The top of the circle is the letter and bottom is the count. B-F show how the algorithm works. In F, the encoding for x, for example, is the path from the top to x using the labels that yields 00. B combines the two smallest counts and creates a node with that count, adds 0,1 to edges. C shows that combining x and v yields the smallest count 33 and this is now added as a subtree. Similarly until we reach F. Then G is the dictionary we build from the tree. Sorting will help make this problem must easier. Programming Problem 2 • Complete the two Huffman functions that produce the dictionary. • You are allowed to use sort. Please ask if you are allowed to other functions that are uncertain about. Assignment №11 Last Assignment Page 8 Problem 3: Queries In class we were introduced to SQL and the relational model. You will have a great deal of freedom with this problem. Create a table called Weather with attributes City, State, High, Low populating it with the data shown in Table 1. Weather City State High Low Phoenix Arizona 105 90 Tucson Arizona 101 92 Flag Staff Arizona 105 90 San Diego California 77 60 Albuquerque New Mexico 80 72 Nome Alaska 64 -54 Table 1: Relation Weather and tuples . Once you’ve created and populated your table, write the following queries: 1. Select all the tuples (Query 1) 2. Select all the tuples where the High temperature is less than 80 (Query 2) 3. Select All the cities where the low temperature is strictly greater than the Low of Albuquerque – you cannot use the number 72.0 in the query (Query 3) 4. Select the city and temperature with the smallest low temperature (Query 4) 5. Select the city temperature with the largest high temperature–since there are two, both cities should be returned. (Query 5) 6. Display the average High and Low temperatures–you are not allowed to use Avg() (Query 6) 7. Give the counts of cities by their Low temperatures (Query 7) You should read about these SQL functions (NOTE: SQL functions, not Python functions): count(), sum(), min(), max() as well as “group by” and “in”. The results of these queries are shown in the output. Assignment №11 Last Assignment Page 9 Output Query 1 (’Phoenix’, ’Arizona’, 105.0, 90.0) (’Tucson’, ’Arizona’, 101.0, 92.0) (’Flag Staff’, ’Arizona’, 105.0, 90.0) (’San Diego’, ’California’, 77.0, 60.0) (’Albuquerque’, ’New Mexico’, 80.0, 72.0) (’Nome’, ’Alaska’, 64.0, -54.0) ****************************** Query 2 (’San Diego’, ’California’, 77.0, 60.0) (’Nome’, ’Alaska’, 64.0, -54.0) ****************************** Query 3 (’Phoenix’,) (’Tucson’,) (’Flag Staff’,) ****************************** Query 4 (’Nome’, -54.0) ****************************** Query 5 (’Phoenix’, 105.0) (’Flag Staff’, 105.0) ****************************** ****************************** Query 6 (88.66666666666667, 58.333333333333336) ****************************** Query 7 (-54.0, 1) (60.0, 1) (72.0, 1) (90.0, 2) (92.0, 1) Assignment №11 Last Assignment Page 10 SQL Queries 1 # ####################### 2 # Problem Three 3 # ####################### 4 5 import sqlite3 6 7 connection = sqlite3.connect("mydatabase.db") 8 my_cursor = connection.cursor() 9 10 my_cursor.execute("DROP TABLE IF EXISTS Weather") 11 12 #create and populate database *just once* 13 14 # Use ‘my_cursor.exectue()‘ to write the queries 15 16 # TODO: Create a table 17 18 # TODO: Insert all 6 rows into the table 19 20 connection.commit() 21 22 if __name__=="__main__": 23 print() 24 25 print("*" * 40) print("Problem 3") 26 print() 27 28 """ 29 # Template for each query. 30 # Replace QUERY_AS_STRING with your query as a string 31 for i in my_cursor.execute(QUERY_AS_STRING): print(i) 32 33 """ 34 35 print("Query 1\n") 36 # TODO: QUERY 1 Select All the tuples 37 38 print("*"*30) 39 print("Query 2\n") 40 #TODO: QUERY 2 Select All the tuples where the high temperature is ←less than 80 41 42 print("*"*30) 43 print("Query 3\n") 44 # TODO: QUERY 3 Select All the cities where the low temperature is ←greater than the low of Albuquerque Assignment №11 Last Assignment Page 11 45 # NOTE: You are not allowed to include the number "72.0" (or other ←variations) in the query itself 46 47 print("*"*30) 48 print("Query 4\n") 49 #TODO: QUERY 4 Select the city and temperature with the smallest low ←temperature 50 51 print("*"*30) 52 print("Query 5\n") 53 # TODO: QUERY 5 Select the city temperature with the largest high ←temperature 54 # NOTE: Since there are two, both cities should be returned (the query←will handle returning both) 55 56 print("*"*30) 57 print("Query 6\n") 58 #TODO: QUERY 6 Display the average High and Low temperatures 59 # NOTE: You are not allowed to use Avg() (HINT: You can do division in←a query) 60 61 print("*"*30) 62 print("Query 7\n") 63 # TODO: QUERY 7 Give the counts of cities by their Low temperatures 64 65 connection.close() Programming Problem 3 • Write the queries with the outputs. (There is a code snippet you can copy that shows where you can put the query in for all 7) • An extra file will be made after running the code file, mydatabse.db. You can leave this file in the repository. • (Fall 2020) Some of the lectures that cover this content are: Lecture 25, Lecture 27. (There might be more, these are just some quick looks) Assignment №11 Last Assignment Page 12 Pairs (jcachila & kanghong) ( risgupt & mafissel) (eebridge & nhuls) ( ajrivas & aduana) (yashgoll & matlawye) ( jrhusar & sstanik) (guptakar & micsiler) (cangelly & apatel02) (malderei & doolofin) ( salazz & ecfugate) (aidmbeck & ahawad) ( rashar & kjshoema) (sheayork & cabar) (acrayner & evjapund) ( ba13 & enocwang) ( pbao & bls1) (sthickma & rjzvonar) (tridewit & iawsmith) (lindrake & jenchong) (ahiemenz & sfharker) ( jm122 & kketsump) ( memeli & np8) (askhatra & sopher) ( panshuo & nboynick) ( & nsp1) ( kotten mk73 & creejone) ( & weenglan) ( amannin & grkilmer) (azguerre & armathur) ( & thomwagg) (jblitcho & gabford) ( apunji & jhread) (aarcjack & jabigbee) ( mcrusk & jciolko) (jasbaltz & maozhang) (benbutch & tmrowe) ( & ewanning) ( pbietry & anupgupt) (howardjh & ibhatia) (quimoore & wei11) ( mjmelin & jajsherm) ( huangba & mtimko) ( & grhollow) (epranger & zhyoung) ( lmbane & shen10) xy31 nban gu10 ehopf Assignment №11 Last Assignment Page 13 ( elyhend & coopwhit) ( tnspeer & cookalex) (apathare & jmajoros) (cjbednar & saw17) (ddelfine & saazeem) (jorkline & sopwhite) ( weishe & sawojcik) (spkrishn & ncseitz) ( snaraya & shaagupt) ( brimore & dparekh) (malfadhl & jakemage) ( naditya & spencham) (jsensanb & jdykstr) ( pcrnjak & kevschoo) ( albertl & huanshin) (janewqui & aamccann) (jatherbe & sc12) ( & vleo) (ktakesue & mereking) ( sdeline & glyou) (ashbkenn & robphong) ( jaszhen & dudelhov) ( rvishwa & suana) (xvinapat & novakc) (khapatel & emrvick) (jadamcda & chipvale) ( mbuzil & rileyjac) (reboland & mzachodn) (asfatele & ekvachko) (chenjiwe & jieshi) (gwurschm & hubbarcj) (aguvolan & dakinnir) ( griwall & adahmed) ( klpletz & jsnetter) (calebeer & attan) (kenharpe & controge) (marratia & danivarg) ( zaldar & aurabaza) ( & rdalkin) (clivermo & jfoll) (benmunoz & mikeosul) (sebogden & jerecunn) (qawillia & kingnj) ( rzakhem & dsp1) (jonabarg & greeneaj) (bbeharry & grahabr) ( dawrig & ramosjaa) tlk1 teh1 Assignment №11 Last Assignment Page 14 (ajfellow & bentorre) ( & emkalvel) ( yuexli & srfilipp) (isiddeeq & gtokusum) (erikklem & ehelwig) ( eliwebs & jlalezar) (pzastrow & machag) (mmhamman & meeriyer) (jargidde & jowamajo) ( & moorewt) (odsnyder & gavsimps) (tmcdonne & blhamm) (lucypark & jaybradb) (levlangl & oadeyonu) ( maccann & geseiler) ( cgstern & pjroeder) (parkjaeb & vicmerca) (wencheng & robmawi) ( cbylciw & fostertj) ( amohrs & dschonem) ( jaqian & bmdeluna) (dapullia & sgryna) (merefain & anisinh) (shengleo & ejimene) (antjacob & wcutchin) (talysmit & kalpatel) ( mw127 & stevenea) ( alexj & cdrassel hd1 dt11 & Assignment №11 Last Assignment aannangi) Page 15