# Ok to post homework # Lucy Martinez, 02-28-2025, Assignment 11 with(combinat): ###################### #Problem 1: Using the corrected Mul(A,B), verify that indeed # An*Bn=sqrt(n)*Bn for small n # How far were you able to go with in a reasonable amount of time? # It was pretty fast up to n=8. For n=9, it took a bit longer but it did # finish within 10 seconds at most. For n=10, it took about a minute. CheckProd:=proc(n) local A,B,L,R,i,j: A:=An(n): B:=Bn(n): L:=Mul(A,B): R:=[seq([seq(sqrt(n)*B[i][j],j=1..nops(B[1]))],i=1..nops(B))]: evalb(R=L): end: #Problem 2: As Pablo noticed the largest r such that MaxNN(G,r) is 0 # is the independence number of G # (the largest subset of vertices that have no edges between them). # Use MaxNNpablo(G,r) to write a procedure IndNu(G) # that finds the independence number of a graph G=[n,E] IndNu:=proc(G) local r: for r from 1 while MinMaxNNpablo(G, r)=0 do od: r-1: end: # What are the independence numbers of Gnd(n,2) for n from 3 to 15? # Can you make a conjecture? [5 dollars to be divided] Can you prove it? # Here is what I got: a pair [Gnd(n,2), Nu] denotes the independence number Nu # of Gnd(n,2) # The pattern I notice is the following: # For every i>=1, and n=3*i the independence number of # the graphs G(n,2), G(n+1,2) and G(n+2,2) is "i" # [Gnd(3,2), 1] # [Gnd(4,2), 1] # [Gnd(5,2), 1] # [Gnd(6,2), 2] # [Gnd(7,2), 2] # [Gnd(8,2), 2] # [Gnd(9,2), 3] # [Gnd(10,2), 3] # [Gnd(11,2), 3] # [Gnd(12,2), 4] # [Gnd(13,2), 4] # [Gnd(14,2), 4] # [Gnd(15,2), 5] # What are the independence numbers of Gnd(n,3) for n from 3 to 15? # Can you make a conjecture? [5 dollars to be divided] Can you prove it? # Here is what I got: a pair [Gnd(n,3), Nu] denotes the independence number Nu # of Gnd(n,3) # The pattern I notice is the following: # For every i>=1, and n=4*i the independence number of # the graphs G(n,2), G(n+1,2), G(n+2,2) and G(n+3,2) is "i" # [Gnd(4,3), 1] # [Gnd(5,3), 1] # [Gnd(6,3), 1] # [Gnd(7,3), 1] # [Gnd(8,3), 2] # [Gnd(9,3), 2] # [Gnd(10,3), 2] # [Gnd(11,3), 2] # [Gnd(12,3), 3] # [Gnd(13,3), 3] # [Gnd(14,3), 3] # [Gnd(15,3), 3] #Problem 3: Write a procedure UpperBoundMinMaxNN(G,r,K) # that uses combinat[randcomb]({seq(i,i=1..n)},r) K times and # finds the minimum of MaxNN(G,H) for K random r-element subsets # of the set of vertices {1,..,n}. UpperBoundMinMaxNN:=proc(G,r,K) local n,i,H,m,mi: n:=G[1]: mi:=infinity: for i from 1 to K do H:=randcomb({seq(i,i=1..n)},r): m:=MaxNN(G,H): if m=sqrt(n) MinMaxNN:=proc(G,r) local n,i,S,s: n:=G[1]: #all subsets of cardinality r S:=choose({seq(i,i=1..n)},r): min(seq(MaxNN(G,s),s in S)): end: #MinMaxNNpablo(G,r): minimum of MaxNN(G,H) over all subsets H of [n] with size r. # more memory-efficient way suggested by Pablo Blanco # (after discussions with Auora Hiveley) MinMaxNNpablo:=proc(G,r) local n,i,c,mini,curr: n:=G[1]: c:=firstcomb(n,r): mini:=MaxNN(G,c): #check {1,..,r} first while nextcomb(c,n)<>FAIL do: #check all other r-subsets of [n] and update the minimum if they are better c:=nextcomb(c,n): curr:=MaxNN(G,c): if curr < mini then: mini:=curr: fi: od: mini: end: #An(n): the 2^n by 2^n matrix (given as a list of lists) in Knuth's write-up # of Huang's proof An:=proc(n) local A,i: option remember: if n=0 then return([[0]]): fi: A:=An(n-1): [seq([op(A[i]),0$(i-1),1,0$(nops(A[i])-i)],i=1..nops(A)), seq([0$(i-1),1,0$(nops(A[i])-i), op(-A[i])],i=1..nops(A)) ]: end: #Bn(n): the 2^(n-1) by 2^n matrix in the paper Bn:=proc(n) local A,i: A:=An(n-1): [seq(A[i]+[0$(i-1),sqrt(n),0$(nops(A[i])-i)],i=1..nops(A)), seq([0$(i-1),1,0$(nops(A[i])-i)],i=1..nops(A)) ]: end: #Mul(A,B): the product of matrix A and B (assuming that it exists) Mul:=proc(A,B) local i,j,k,n: [seq([seq(add(A[i][k]*B[k][j],k=1..nops(A[i])),j=1..nops(B[1]))],i=1..nops(A))]: end: ##############################################from previous classes #C2.txt: Jan. 27, 2025 Help2:=proc(): print(`LC(p), RG(n,p), Cliques(G,k)`): end: #LC(p): inputs a rational number between 0 and 1 and outputs true with prob. p LC:=proc(p) local a,b,ra: if not (type(p,fraction) and p>= 0 and p<=1) then return fail: fi: a:=numer(p): b:=denom(p): ra:=rand(1..b)(): if ra<=a then true: else false: fi: end: RG:=proc(n,p) local E,i,j: E:={}: for i from 1 to n do for j from i+1 to n do if LC(p) then E:=E union {{i,j}}: fi: od: od: [n,E]: end: #Cliques(G,k): inputs a graph G and a positive integer k, outputs # the set of k-cliques Cliques:=proc(G,k) local n, E,S,i,c,C: n:=G[1]: E:=G[2]: S:={}: C:=choose({seq(i,i=1..n)},k): for c in C do if choose(c,2) minus E={} then S:= S union {c}: fi: od: S: end: ############################### #C1.txt: Jan. 23 Help1:=proc(): print(`Graphs(n),Tri(G),Comp(G),TotTri(G)`): end: with(combinat): #An undirected graph is a set of vertices V and a set of edges #[V,E] and an edge is defined as e={i,j} where i and j belong to V #Convention: our vertices are labeled {1,2,...,n} #Our data structure is [n,E] where E is a set of edges #[3,{{1,2},{1,3},{2,3}}]; #If there are n vertices, how many (undirected) graphs are there? # answer: 2^(n choose 2) since you either include the vertex or not and # then you choose which two to connect #Graphs(n): inputs a non-neg. integer n and outputs the set of ALL # graphs on {1,2,...,n} Graphs:=proc(n) local i,j,S,E,s: E:={seq(seq({i,j},j=i+1..n),i=1..n)}: S:=powerset(E): {seq([n,s],s in S)}: end: #Tri(G): inputs a graph [n,E] and outputs the set of all triples {i,j,k} # such {{i,j},{i,k},{j,k}} is a subset of E Tri:=proc(G) local n, S,E,i,j,k: S:={}: n:=G[1]: E:=G[2]: for i from 1 to n do for j from i+1 to n do for k from j+1 to n do #if member({i,j},E) and member({i,k},E) and member({j,k},E) then if {{i,j},{i,k},{j,k}} minus E={} then S:=S union {{i,j,k}}: fi: od: od: od: S: end: #Comp(G): the complement of G=[n,E] Comp:=proc(G) local n,i,j,E: n:=G[1]: E:=G[2]: [n,{seq(seq({i,j},j=i+1..n),i=1..n)} minus E]: end: #TotTri(G): the total number of love triangles and hate triangles TotTri:=proc(G): nops(Tri(G))+nops(Tri(Comp(G))): end: ############################## #C9.txt, Feb. 20, 2025 Help9:=proc(): print(`Gnd(n,d),AM(G),lam2(G),Vk(k),Nei(v),Ck(k)`): end: #Expanders with(linalg): #Gnd(n,d): the graph whose vertices are {0,1,...,n-1} # and edges {i,i+j mod n} where j=1..d. The vertices will have degree 2d Gnd:=proc(n,d) local i,j,E: E:={seq(seq({i,i+j mod n},j=1..d),i=0..n-1)}: [n,subs({seq(i=i+1,i=0..n-1)},E)]: end: #AM(G): the adjancency matrix of the graph G (the stupid way) AM:=proc(G) local n, E,e,i,j,A: n:=G[1]: E:=G[2]: for i from 1 to n do for j from 1 to n do A[i,j]:=0: od: od: for e in E do A[e[1],e[2]]:=1: A[e[2],e[1]]:=1: od: [seq([seq(A[i,j],j=1..n)],i=1..n)]: end: #lam2(G): inputs a graph and returns FAIL if it is not regular, otherwise # it returns [degree,Lambda_2] (Lambda_2 is the second largest eigenvalue # of the adjancencey matrix) lam2:=proc(G) local A,x,d,S,i: A:=AM(G): S:={seq(add(A[i]),i=1..nops(A))}: if nops(S)<>1 then return(FAIL): fi: d:=S[1]: [d,fsolve(charpoly(A,x))[-2]]: end: #Vk(k): The list of all 0-1 vectors of length k in lex order # (starting at 00...0 and ends with 11..11 Vk:=proc(k) local S,i: option remember: if k=0 then return([[]]): fi: S:=Vk(k-1): [seq([0,op(S[i])],i=1..nops(S)),seq([1,op(S[i])],i=1..nops(S))]: end: #Nei(v): inputs the 0-1 vector and outputs all the vectors where exactly # one bit is changed (hamming distance is 1) from the vector v Nei:=proc(v) local k,i: k:=nops(v): if {op(v)} minus {0,1} <> {} then return(FAIL): fi: {seq([op(1..i-1,v),1-v[i] ,op(i+1..k,v)],i=1..k)} end: #Ck(k): the k-dimensional unit cube as a graph in our format Ck:=proc(k) local V,E,i,v: V:=Vk(k): E:={seq(seq({V[i],v},v in Nei(V[i])), i=1..nops(V))}: E:=subs({seq(V[i]=i,i=1..nops(V))},E): [nops(V),E]: end: