##### HW 4 ####### Help:=proc(): print("SGwyt(a,b), WinningMove(G,i)"): end: ##### 2 ########## # We prove that if f is the Sprague-Grundy function on two pile nim games then 2f(m,n)=f(2m,2n), # 2f(m,n)+1=f(2m+1,2n)=f(2m,2n+1), and f(2m+1,2n+1)=2f(m,n) by induction on the total size # of the larger (doubled game) # The Base Case when the sums of the piles are 4 or less can easily be checked. # For the inductive step, we note that all legal moves have pile size strictly less than the current # pile size. First we take the case of both piles even, i.e. f(2m,2n). The set of all the legal moves # has the form {2(m',n)+(0/1,0) or 2(m,n')+(0,0/1) : m'0 then RETURN(true): fi: M1:=evalm(M1*M): od: false: end: #fGi(G,i), inputs a directed graph G and #an integer i between 1 and nops(G) and outputs #0(resp. 1) if vertex i is a Losing (Winning) position fGi:=proc(G,i) local M,m: option remember: if not (type(i,integer) and type(G,list) and i>=1 and i<=nops(G)) then print(`bad input`): RETURN(FAIL): fi: #if IsCyclic(G) then #print(`graph has cycles`): # RETURN(FAIL): #fi: if G[i]={} then RETURN(0): fi: M:=G[i]: if {seq(fGi(G,m), m in M)}={1} then RETURN(0): else RETURN(1): fi: end: with(linalg): #AM(G): adjacency matrix of G (written a matrix M) #M[i,j]=1 (0) of j belongs (does not belong) #to G[i] AM:=proc(G) local n,i,j,M: n:=nops(G): M:=matrix(n,n): for i from 1 to n do for j from 1 to n do if j in G[i] then M[i,j]:=1: else M[i,j]:=0: fi: od: od: op(M): end: #WList(G): Winning list #Inputs a directed G (representing a game) #and outputs a list of length nops(G) such that #L[i]=0 or 1 according to whether position (vertex) i #is a losing or winning position WList:=proc(G) local i: [seq(fGi(G,i),i=1..nops(G))]: end: #RandGame(n,k): inputs pos. integers n and k #and outputs a random digraph that has the #property that out of vertex i all the labels #are less i, and there are (usually) k outgoing #edges RandGame:=proc(n,k) local L,i,tomas,j: L:=[{}]: for i from 2 to n do tomas:={seq(rand(1..i-1)(),j=1..k)}: L:=[op(L), tomas]: od: L: end: #end old stuff