{VERSION 6 0 "IBM INTEL NT" "6.0" } {USTYLETAB {CSTYLE "2D Output" -1 20 "Times" 1 12 0 0 255 1 0 0 0 2 2 1 0 0 0 1 }{CSTYLE "_cstyle1" -1 202 "Times" 1 16 0 0 0 1 2 1 2 2 2 2 0 0 0 1 }{CSTYLE "_cstyle2" -1 203 "Times" 1 12 0 0 0 1 2 2 2 2 2 2 0 0 0 1 }{CSTYLE "_cstyle3" -1 204 "Times" 1 14 0 0 0 1 2 1 1 2 2 2 0 0 0 1 }{CSTYLE "_cstyle4" -1 205 "Times" 1 12 0 0 0 1 2 1 2 2 2 2 0 0 0 1 }{CSTYLE "_cstyle5" -1 206 "Courier" 1 12 255 0 0 1 0 1 0 2 1 2 0 0 0 1 }{CSTYLE "_cstyle6" -1 207 "Times" 0 1 0 0 0 0 0 0 0 2 2 2 0 0 0 1 }{CSTYLE "_cstyle7" -1 208 "Times" 1 12 0 0 255 1 0 0 0 2 2 2 0 0 0 1 }{CSTYLE "_cstyle8" -1 209 "Times" 1 14 0 0 0 1 2 1 1 2 2 2 0 0 0 1 }{CSTYLE "_cstyle9" -1 210 "Times" 1 12 0 0 0 1 2 2 2 2 2 2 0 0 0 1 } {CSTYLE "_cstyle10" -1 211 "Times" 0 1 0 0 0 0 0 0 0 2 2 2 0 0 0 1 } {CSTYLE "_cstyle11" -1 212 "Times" 0 1 0 0 0 0 0 1 0 2 2 2 0 0 0 1 } {CSTYLE "_cstyle12" -1 213 "Times" 0 1 0 0 0 0 1 0 0 2 2 2 0 0 0 1 } {CSTYLE "_cstyle13" -1 214 "Times" 0 1 0 0 0 0 2 0 0 2 2 2 0 0 0 1 } {CSTYLE "_cstyle14" -1 215 "Times" 1 12 0 0 0 1 1 2 2 2 2 2 0 0 0 1 } {CSTYLE "_cstyle15" -1 216 "Times" 1 12 0 0 0 1 2 1 2 2 2 2 0 0 0 1 } {PSTYLE "_pstyle1" -1 200 1 {CSTYLE "" -1 -1 "Times" 1 12 0 0 0 1 2 2 2 2 2 2 1 0 0 1 }3 1 0 0 0 0 2 0 2 0 2 2 -1 1 }{PSTYLE "_pstyle2" -1 201 1 {CSTYLE "" -1 -1 "Times" 1 12 0 0 0 1 2 2 2 2 2 2 1 0 0 1 }1 1 0 0 0 0 2 0 2 0 2 2 -1 1 }{PSTYLE "_pstyle3" -1 202 1 {CSTYLE "" -1 -1 "Courier" 1 12 255 0 0 1 0 1 0 2 1 2 1 0 0 1 }1 1 0 0 0 0 2 0 2 0 2 2 -1 1 }{PSTYLE "_pstyle4" -1 203 1 {CSTYLE "" -1 -1 "" 0 1 0 0 0 0 0 0 0 2 2 2 0 0 0 1 }3 0 0 -1 -1 -1 1 0 1 0 2 2 -1 1 }{PSTYLE "_pstyle 5" -1 204 1 {CSTYLE "" -1 -1 "Times" 1 12 0 0 255 1 0 0 0 2 2 1 0 0 0 1 }3 3 0 -1 -1 -1 1 0 1 0 2 2 -1 1 }{PSTYLE "_pstyle6" -1 205 1 {CSTYLE "" -1 -1 "" 0 1 0 0 0 0 0 0 0 2 2 2 0 0 0 1 }0 0 0 -1 -1 -1 1 0 1 0 2 2 -1 1 }} {SECT 0 {EXCHG {PARA 200 "" 0 "" {TEXT 202 30 "Math 365D Lab Five - Fa ll 2007" }{TEXT 203 0 "" }}}{EXCHG {PARA 201 "" 0 "" {TEXT 204 7 "NEST ING" }{TEXT 203 0 "" }{TEXT 203 147 "\nWe can \"nest\" one loop within another (within another within another and so on if we like) and can \+ do the same for the decision-making statements " }{TEXT 205 10 "if...t hen " }{TEXT 203 4 "and " }{TEXT 205 14 "if..then..else" }{TEXT 203 56 ". We can also nest a procedure within another procedure." }{TEXT 203 0 "" }{TEXT 203 1 "\n" }}}{EXCHG {PARA 201 "" 0 "" {TEXT 203 311 " Here we will write a procedure to determine all the vertices that are \+ two edges away from a given vertex. We do this with a loop nested in a loop nested in yet another loop. (A practical application of such a p rocedure might be finding all airports reachable from your home airpor t with one stop along the way.)" }{TEXT 203 0 "" }}{PARA 201 "" 0 "" {TEXT 203 1 " " }{TEXT 203 0 "" }}{PARA 202 "> " 0 "" {MPLTEXT 1 206 15 "with(networks):" }{MPLTEXT 1 206 0 "" }{MPLTEXT 1 206 24 "\nH:=ran dom(8,9):draw(H);" }{MPLTEXT 1 206 0 "" }{MPLTEXT 1 206 1 "\n" }} {PARA 203 "" 1 "" {GLPLOT2D 400 400 400 {PLOTDATA 2 "6<-%'POINTSG6#7$$ \"\"\"\"\"!$F)F)-%%TEXTG6$7$$\"2-++++++?\"!#;$F)!\"\"Q\"16\"-F$6#7$$\" +6y1rq!#5$\"+8y1rqF;-F,6$7$$\"1)****4\"y1r!*F1$\"1-++8y1rqF1Q\"2F5-F$6 #7$$!+3Q.^?!#>F'-F,6$7$$!+-+++?F;$\"#5F3Q\"3F5-F$6#7$$!+6y1rqF;F<-F,6$ 7$$!+6y1r!*F;FCQ\"4F5-F$6#7$$F3F)$!+:w1-TFK-F,6$7$$!2-++++++?\"F1$F)! \"(Q\"5F5-F$6#7$$!+0y1rqF;$!+>y1rqF;-F,6$7$$!+0y1r!*F;F\\pQ\"6F5-F$6#7 $$\"+A95`hFKF\\o-F,6$7$$\"+1+++?F;$F;F3Q\"7F5-F$6#7$F<$!+5y1rqF;-F,6$7 $$\"1+++8y1r!*F1$!*\"y1rq!\"*Q\"8F5-%'CURVESG6$7$F8FH-%&COLORG6&%$RGBG F2FQF2-F_r6$7$FVFfpFbr-F_r6$7$F[oFbqFbr-F_r6$7$FHFVFbr-F_r6$7$FVF[oFbr -F_r6$7$F8FVFbr-F_r6$7$FioFfpFbr-F_r6$7$FioFbqFbr-F_r6$7$FfpFbqFbr-%*A XESSTYLEG6#%%NONEG" 1 2 2 0 10 1 2 6 1 1 2 1.000000 45.000000 45.000000 1 0 "Curve 1" "Curve 2" "Curve 3" "Curve 4" "Curve 5" "Curve 6" "Curve 7" "Curve 8" "Curve 9" "Curve 10" "Curve 11" "Curve 12" "Cu rve 13" "Curve 14" "Curve 15" "Curve 16" "Curve 17" "Curve 18" "Curve \+ 19" "Curve 20" "Curve 21" "Curve 22" "Curve 23" "Curve 24" "Curve 25" }}{TEXT 207 0 "" }}}{EXCHG {PARA 202 "> " 0 "" {MPLTEXT 1 206 32 "twoa way:=proc(G::graph, v)::set;" }{MPLTEXT 1 206 0 "" }{MPLTEXT 1 206 80 "\ndescription \"returns the set of all vertices that are two edges fr om vertex v\";" }{MPLTEXT 1 206 0 "" }{MPLTEXT 1 206 89 "\nlocal neigh bors1::set, neighbors2::set, answerset::set, currentvertex, vertex1, v ertex2;" }{MPLTEXT 1 206 0 "" }{MPLTEXT 1 206 65 "\nanswerset:=\{\}; # initialize the set that will contain our output" }{MPLTEXT 1 206 0 " " }{MPLTEXT 1 206 84 "\nfor currentvertex in (vertices(G) minus \{v\}) do # look at each vertex of the graph " }{MPLTEXT 1 206 0 "" } {MPLTEXT 1 206 48 "\n neighbors1:=neighbors(currentvertex,G);" }{MPLTEXT 1 206 0 "" }{MPLTEXT 1 206 78 "\n for vertex1 in neig hbors1 do # look at each neigbhor of currentvertex" }{MPLTEXT 1 206 0 "" }{MPLTEXT 1 206 50 "\n neighbors2:=neighbors(vertex1 ,G);" }{MPLTEXT 1 206 0 "" }{MPLTEXT 1 206 80 "\n for v ertex2 in neighbors2 do # look at each neighbor of vertex1" }{MPLTEXT 1 206 0 "" }{MPLTEXT 1 206 93 "\n if vertex2=v \+ then answerset:=answerset union \{currentvertex\} end if;" }{MPLTEXT 1 206 0 "" }{MPLTEXT 1 206 24 "\n end do;" }{MPLTEXT 1 206 0 "" }{MPLTEXT 1 206 16 "\n end do;" }{MPLTEXT 1 206 0 "" } {MPLTEXT 1 206 8 "\nend do;" }{MPLTEXT 1 206 0 "" }{MPLTEXT 1 206 11 " \nanswerset;" }{MPLTEXT 1 206 0 "" }{MPLTEXT 1 206 10 "\nend proc:" } {MPLTEXT 1 206 0 "" }}}{EXCHG {PARA 202 "> " 0 "" {MPLTEXT 1 206 13 "t woaway(H,3);" }{MPLTEXT 1 206 0 "" }}{PARA 204 "" 1 "" {XPPMATH 20 "6# <&\"\"#\"\"%\"\"&\"\"(" }{TEXT 208 0 "" }}}{EXCHG {PARA 201 "" 0 "" {TEXT 203 372 "When writing long and complex code, it is often prefera ble to break up the program into more than one procedure. This makes t he code more readable, and can save effort if the program requires the same steps to be followed at multiple points in the code, in which ca se we call the other procedure at each of these points rather than typ ing in the same code multiple times. " }{TEXT 203 0 "" }{TEXT 203 1 " \n" }{TEXT 203 56 "\nBelow is an example of one procedure calling anot her. " }{TEXT 203 0 "" }}{PARA 202 "> " 0 "" {MPLTEXT 1 206 51 "reach able_in_n:=proc(G::graph, n::integer, v)::set;" }{MPLTEXT 1 206 0 "" } {MPLTEXT 1 206 92 "\ndescription \"returns the set of all vertices tha t are reachable from v in at most n edges\";" }{MPLTEXT 1 206 0 "" } {MPLTEXT 1 206 34 "\nlocal i::integer, reachable_in_1;" }{MPLTEXT 1 206 0 "" }{MPLTEXT 1 206 18 "\nglobal answerset;" }{MPLTEXT 1 206 0 " " }{MPLTEXT 1 206 1 "\n" }{MPLTEXT 1 206 23 "\nreachable_in_1:=proc() " }{MPLTEXT 1 206 0 "" }{MPLTEXT 1 206 66 "\ndescription \"finds all v ertices that are neighbors of answerset\";" }{MPLTEXT 1 206 0 "" } {MPLTEXT 1 206 23 "\nlocal currentvertex; " }{MPLTEXT 1 206 0 "" } {MPLTEXT 1 206 79 "\nfor currentvertex in answerset do # do this for e ach vertex we've found so far" }{MPLTEXT 1 206 0 "" }{MPLTEXT 1 206 62 "\n answerset:=answerset union neighbors(currentvertex,G)" } {MPLTEXT 1 206 0 "" }{MPLTEXT 1 206 8 "\nend do;" }{MPLTEXT 1 206 0 " " }{MPLTEXT 1 206 10 "\nend proc:" }{MPLTEXT 1 206 0 "" }{MPLTEXT 1 206 1 "\n" }{MPLTEXT 1 206 70 "\nanswerset:=\{v\}; # start by placing \+ the given vertex in the output set" }{MPLTEXT 1 206 0 "" }{MPLTEXT 1 206 22 "\nfor i from 1 to n do " }{MPLTEXT 1 206 0 "" }{MPLTEXT 1 206 47 "\n reachable_in_1() # call this procedure" }{MPLTEXT 1 206 0 "" }{MPLTEXT 1 206 8 "\nend do;" }{MPLTEXT 1 206 0 "" }{MPLTEXT 1 206 1 "\n" }{MPLTEXT 1 206 10 "\nend proc:" }{MPLTEXT 1 206 0 "" }}} {EXCHG {PARA 202 "> " 0 "" {MPLTEXT 1 206 24 "reachable_in_n(H, 1, 6); " }{MPLTEXT 1 206 0 "" }}{PARA 204 "" 1 "" {XPPMATH 20 "6#<%\"\"'\"\"( \"\")" }{TEXT 208 0 "" }}}{EXCHG {PARA 202 "> " 0 "" {MPLTEXT 1 206 0 "" }}}{PARA 205 "" 0 "" {TEXT 209 0 "" }{TEXT 209 10 "\nEXERCISES" } {TEXT 210 0 "" }{TEXT 210 243 "\n1. Use nested loops to write a proced ure which takes a graph as its argument and prints a line listing the \+ vertices having no neighbors, then a line listing those having exactly one neighbor, and so on until all vertices have been displayed. " } {TEXT 211 0 "" }{TEXT 211 25 "\n2. Modify the procedure " }{TEXT 212 8 "twoaway " }{TEXT 211 71 "above so that it does NOT display a vertex that is two edges away from " }{TEXT 213 1 "v" }{TEXT 211 48 " if tha t vertex is also just one edge away from " }{TEXT 213 1 "v" }{TEXT 211 2 " (" }{TEXT 213 5 "i.e.," }{TEXT 214 28 " don't include neighbor s of " }{TEXT 213 1 "v" }{TEXT 214 2 ")." }{TEXT 211 0 "" }{TEXT 211 112 "\n3. Modify the procedure from the previous exercise to eliminate the innermost nested loop by instead using the " }{TEXT 212 7 "member " }{TEXT 211 8 "command." }{TEXT 211 0 "" }{TEXT 211 46 "\n4. Write a procedure that takes two integers " }{TEXT 213 1 "m" }{TEXT 214 5 " a nd " }{TEXT 213 1 "n" }{TEXT 214 29 " as arguments and returns an " } {TEXT 213 1 "m" }{TEXT 214 4 " by " }{TEXT 213 1 "n" }{TEXT 214 34 " a rray such that the entry in row " }{TEXT 213 1 "i" }{TEXT 214 9 ", col umn " }{TEXT 213 1 "j" }{TEXT 214 19 " is the product of " }{TEXT 213 1 "i" }{TEXT 214 5 " and " }{TEXT 213 1 "j" }{TEXT 214 2 ". " }{TEXT 211 0 "" }{TEXT 211 25 "\n5. Modify the procedure " }{TEXT 212 15 "rea chable_in_n " }{TEXT 211 76 "so that it contains a nested loop rather \+ than one procedure calling another." }{TEXT 211 0 "" }{TEXT 211 36 "\n 6. Write a procedure that takes two" }{TEXT 214 1 " " }{TEXT 213 1 "m " }{TEXT 214 4 " by " }{TEXT 213 1 "n" }{TEXT 214 242 " arrays as its \+ arguments and displays the matrix sum of the two. Do this with nesting rather than by using any of Maple's built-in matrix operation functio ns. See Lab Four for details on determining the number of rows and col umns in an array." }{TEXT 210 67 " [Be sure to initialize your array o r the output may look strange.]" }{TEXT 211 0 "" }{TEXT 211 33 "\n7. W rite a procedure that takes " }{TEXT 214 3 "an " }{TEXT 213 1 "m" } {TEXT 214 4 " by " }{TEXT 213 1 "n" }{TEXT 214 14 " array and an " } {TEXT 213 1 "n" }{TEXT 214 4 " by " }{TEXT 213 1 "p" }{TEXT 214 162 " \+ array as its arguments and displays the matrix product of the two. Do \+ this with nesting rather than by using any of Maple's built-in matrix \+ operation functions. " }{TEXT 210 150 "See Lab Four for details on det ermining the number of rows and columns in an array. [Be sure to initi alize your array or the output may look strange.]" }{TEXT 210 0 "" } {TEXT 210 71 "\n8. Write a procedure that takes a graph, two vertices, and an integer " }{TEXT 215 1 "n" }{TEXT 210 98 " as arguments and in dicates if there is a path from one vertex to the other that requires \+ at most " }{TEXT 215 1 "n" }{TEXT 210 7 " edges." }{TEXT 210 0 "" } {TEXT 210 162 "\n9. Write a procedure that takes a graph and three ver tices as arguments and indicates if there is a cycle connecting just t hese three vertices. Hint: use nested " }{TEXT 216 10 "if...then " } {TEXT 210 11 "statements." }{TEXT 210 0 "" }{TEXT 210 61 "\n10. Modify the previous procedure to consider four vertices." }{TEXT 211 0 "" } {TEXT 211 1 "\n" }{TEXT 211 1 "\n" }{TEXT 211 1 "\n" }}{PARA 205 "" 0 "" {TEXT 211 0 "" }}{PARA 205 "" 0 "" {TEXT 211 0 "" }}{PARA 205 "" 0 "" {TEXT 211 0 "" }}{PARA 205 "" 0 "" {TEXT 211 0 "" }}{PARA 205 "" 0 "" {TEXT 211 0 "" }}{PARA 205 "" 0 "" {TEXT 211 0 "" }}{PARA 205 "" 0 "" {TEXT 211 0 "" }}{PARA 205 "" 0 "" {TEXT 211 0 "" }}{PARA 205 "" 0 "" {TEXT 211 0 "" }}{PARA 205 "" 0 "" {TEXT 211 0 "" }}{PARA 205 "" 0 "" {TEXT 211 0 "" }}{PARA 205 "" 0 "" {TEXT 211 0 "" }}{PARA 205 "" 0 "" {TEXT -1 0 "" }}}{MARK "0 0 0" 30 }{VIEWOPTS 1 1 0 1 1 1803 1 1 1 1 }{PAGENUMBERS 0 1 2 33 1 1 }