> #Attendence Q1. ; > #Describe the problem that Euler solved regardinig 7 bridges. ; > #There are (or were, in Euler’s time) seven bridges to connect the two islands and the downstream parts of the town. ; > #Euler wondered if a person could walk across each of the seven bridges once and only once to touch every part of the town. Starting and ending at the same spot was not a requirement ; # > ; > ; > ; > #Attendence Q2 ; > #Draw it on a piece of paper with line segment representing every edge ; > #G=[{2,3}, {1,3,4},{1,2,4},{2,3}]; > #V={1,2,3,4} ; > ; # > #Attendence Q3 ; > #If toss a fair coin 2000 times, what is the probability that you get the exact 1000 heads (and 1000 tails) ; > with(combinat) [Chi, bell, binomial, cartprod, character, choose, composition, conjpart, decodepart, encodepart, eulerian1, eulerian2, fibonacci, firstcomb, firstpart, firstperm, graycode, inttovec, lastcomb, lastpart, lastperm, multinomial, nextcomb, nextpart, nextperm, numbcomb, numbcomp, numbpart, numbperm, partition , permute, powerset, prevcomb, prevpart, prevperm, randcomb, randpart, randperm , rankcomb, rankperm, setpartition, stirling1, stirling2, subsets, unrankcomb, unrankperm, vectoint] ; > a:=binomial(2000,1000); Typesetting:-mprintslash([(a := 20481516269894897143351625029808250443964248879\ 81397033820382637671748186202083755828932994)],[2048151626989489714335162502980\ 825044396424887981397033820382637671748186202083755828932994]) ; > b:=2^2000; Typesetting:-mprintslash([(b := 11481306952742545242328332011776819840223177020\ 8869520047764273682576626139237031385665948631)],[11481306952742545242328332011\ 7768198402231770208869520047764273682576626139237031385665948631]) ; > evalf(a/b) .1783901115e-1 ; > ; > ; > #Attdence Q4 ; > #If you toss a dice 6000 times, what is the probability of that each of posssible outccomes {1,2,3,4,5,6} ; > #p(1)=p(2)=p(3)=p(4)=p(5)=p(6)=1/6 ; > ; > ; > ; > #Attendence Q5 ; > #Pick 5 random facebook friends ; > #for each of them pick 3 friends ; > #for each of friends pick 3 friends ; > #Label the people icked 1,2,...,n ; > #write the graph in data structure ; > #V:={Tom, Jake, Alissa, Bob, Lucy}; > #G:=[{Jake,Alissa,Bob}, {Lucy, Alissam Tom},{Tom, Jake, Bob}, {Jakem Tom, Lucy}, {Jake, Aliisa, Bob}]; > ; > ; > #Attendence Q6 ; > #Look at the cities that border Piscataway, and for each of them those that border them, ; > #and again until you get to princton ; > #(i) construct the graph ; > #(ii) Find the path from piscataway to princiton ; > #(a) find the actual path using Paths( and list them) ; > #(b) by using NuPathro ; > #(c) by using GFt ; > ; > #South Plainfield:=1 Edison:=2, North Brunswick: =3, South Brunswick:=4, New Brunswick:=5, Franklin: 6, Princton:=7, Piscataway: 8 ; > #V:={1, 2, 3, 4, 5, 6, 7, 8,} ; > G:=[{8, 2}, {8, 1, 5}, {5, 6, 4}, {7, 6, 3},{8, 2, 3, 6}, {8, 5, 3, 4, 7}, {6, 4},{1, 2, 5, 6}]; Typesetting:-mprintslash([(G := [{2, 8}, {1, 5, 8}, {4, 5, 6}, {3, 6, 7}, {2, 3 , 6, 8}, {3, 4, 5, 7, 8}, {4, 6}, {1, 2, 5, 6}])],[[{2, 8}, {1, 5, 8}, {4, 5, 6 }, {3, 6, 7}, {2, 3, 6, 8}, {3, 4, 5, 7, 8}, {4, 6}, {1, 2, 5, 6}]]) ; > Paths:=proc(G,u,v,k) local Neighs, PA,nei,PA1,pa1: > option remember: > > #INITIAL CONDITIONS, paths of length 0 are just lists of length 1 > if k=0 then > if u=v then > RETURN({[u]}): > else > RETURN({}): > fi: > fi: > > #Every path from u to v of length k starts with a first step that is towards one of the neighbors of u > #so let's find the set of neighbors of u > Neighs:=G[u]: > > #We will dynamically construct all the paths from u to v of length k by > #going through all the options for first step > #PA is the desired output > #We start out PA the empty set > PA:={}: > > for nei in Neighs do > #We call the procedure recursively to find the set of paths from a typical neighbor of u, to v of length one less, i.e. k-1 > PA1:=Paths(G,nei,v,k-1): > > #For each of these paths we stick u at the beginning and add these to our desired output > PA:=PA union {seq([u,op(pa1)], pa1 in PA1)}: > od: > > PA: > > end: > Paths(G, 8,7 , 1) {} ; > Paths(G,8,7,2) {[8, 6, 7]} ; > Paths(G,8,7,3) {[8, 5, 6, 7], [8, 6, 4, 7]} ; > Paths(G,8,7,4) {[8, 1, 8, 6, 7], [8, 2, 5, 6, 7], [8, 2, 8, 6, 7], [8, 5, 3, 4, 7], [8, 5, 3, 6, 7], [8, 5, 6, 4, 7], [8, 5, 8, 6, 7], [8, 6, 3, 4, 7], [8, 6, 3, 6, 7], [8, 6, 4, 6, 7], [8, 6, 5, 6, 7], [8, 6, 7, 4, 7], [8, 6, 7, 6, 7], [8, 6, 8, 6, 7] } ; > Paths(G,8,7,5) {[8, 1, 2, 5, 6, 7], [8, 1, 2, 8, 6, 7], [8, 1, 8, 5, 6, 7], [8, 1, 8, 6, 4, 7] , [8, 2, 1, 8, 6, 7], [8, 2, 5, 3, 4, 7], [8, 2, 5, 3, 6, 7], [8, 2, 5, 6, 4, 7 ], [8, 2, 5, 8, 6, 7], [8, 2, 8, 5, 6, 7], [8, 2, 8, 6, 4, 7], [8, 5, 2, 5, 6, 7], [8, 5, 2, 8, 6, 7], [8, 5, 3, 4, 6, 7], [8, 5, 3, 5, 6, 7], [8, 5, 3, 6, 4, 7], [8, 5, 6, 3, 4, 7], [8, 5, 6, 3, 6, 7], [8, 5, 6, 4, 6, 7], [8, 5, 6, 5, 6, 7], [8, 5, 6, 7, 4, 7], [8, 5, 6, 7, 6, 7], [8, 5, 6, 8, 6, 7], [8, 5, 8, 5, 6, 7], [8, 5, 8, 6, 4, 7], [8, 6, 3, 4, 6, 7], [8, 6, 3, 5, 6, 7], [8, 6, 3, 6, 4, 7], [8, 6, 4, 3, 4, 7], [8, 6, 4, 3, 6, 7], [8, 6, 4, 6, 4, 7], [8, 6, 4, 7, 4, 7], [8, 6, 4, 7, 6, 7], [8, 6, 5, 3, 4, 7], [8, 6, 5, 3, 6, 7], [8, 6, 5, 6, 4, 7], [8, 6, 5, 8, 6, 7], [8, 6, 7, 4, 6, 7], [8, 6, 7, 6, 4, 7], [8, 6, 8, 5, 6, 7], [8, 6, 8, 6, 4, 7]} ; > #for step length = 6,7,8 there are too many paths. ; > ; > #By using NuPaths() ; > NuPaths:=proc(G,u,v,k) local Neighs,nei: > option remember: > > #INITIAL CONDITIONS, paths of length 0 are just lists of length 1 > if k=0 then > if u=v then > RETURN(1): > else > RETURN(0): > fi: > fi: > > #Every path from u to v of length k starts with a first step that is towards one of the neighbors of u > #so let's find the set of neighbors of u > Neighs:=G[u]: > > #The number of paths of length k from u to v is the sum of the number of paths from each neighbor of u to v, of length k-1 > > add(NuPaths(G,nei,v,k-1),nei in Neighs): > > end: > ; > NuPaths(G,8,7,2) 1 ; > NuPaths(G,8,7,3) 2 ; > NuPaths(G,8,7,4) 14 ; > NuPaths(G,8,7,5) 41 ; > NuPaths(G,8,7,6) 174 ; > #Now, using GFT ; > GFt:=proc(G,v,t) local n,eq,var,X,u,Neighs,eq1,nei: > > n:=nops(G): > > #var is the set of desired quantities. The outputs is going to be [X[1], ..., X[n]] after it is SOLVED > var:={seq(X[u],u=1..n)}: > > #We now use the powerful technique of weight-enumerators to set a SYSTEM of equations for these quantities > #The weight enumerator according to the weight t^LengthOfPath of all paths from u to v > #is t times the sum of the weight enumerators of all path from neighbors of u and if u=v you have to add > #1 for the weight of the empty path > eq:={}: > > > #Neig is the set of neighbors of u > for u from 1 to n do > Neighs:=G[u]: > if u=v then > eq1:=X[u]=1+t*add(X[nei],nei in Neighs): > else > eq1:=X[u]=t*add(X[nei],nei in Neighs): > fi: > eq:=eq union {eq1}: > od: > > > > #We now let Maple solve the linear system of equations > var:=solve(eq,var): > > #we now plug the solution into seq vector of unknowns [X[1], ..., X[n]]: > > factor(subs(var,[seq(X[u], u=1..n)])): > > end: > ; > normal(add(convert(GFt(G,i,t),`+`), i=1..8)) -2*(3*t^7+18*t^6+10*t^5-47*t^4-56*t^3-6*t^2+13*t+4)/(3*t^8+12*t^7-7*t^6-52*t^5-\ 35*t^4+12*t^3+13*t^2-1) ; > ;