> restart; Question1 > n:=6:E:=array(0..n-1,[seq(i,i=0..n-1)]): > i:=2:j:=4:E[j]:=i:print(E); array(0 .. 5, [ (0) = 0 (1) = 1 (2) = 2 (3) = 3 (4) = 2 (5) = 5 ]) Question 2 > representant:=proc(E,i) > local r; > r:=i; > while E[r]<>r do > r:=E[r] > od; > return r; > end: > E:=array(0..6,[0,1,1,6,5,5,2]): > representant(E,0); 0 Question 3 > sontAmis:=proc(E,i,j) > local r,s; > s:=representant(E,j); > r:=representant(E,i); > if r=s then return true > else return false > fi; > end: > sontAmis(E,0,4);sontAmis(E,3,1); false true Question 4 > ajout:=proc(E,i,j) > local F,r,s; > F:=E; > if sontAmis(E,i,j)=false then r:=representant(E,i); > s:=representant(E,j); > F[r]:=s; > fi; > return F; > end: > print(ajout(E,3,4)); array(0 .. 6, [ (0) = 0 (1) = 5 (2) = 1 (3) = 6 (4) = 5 (5) = 5 (6) = 2 ]) Question 6 > couleurNO:=proc(T,i,j) > if i*j=0 then return 0 > else return T[i-1,j-1] > fi; > end: > couleurN:=proc(T,i,j) > if i=0 then return 0 > else return T[i-1,j] > fi; > end: > couleurO:=proc(T,i,j) > if j=0 then return 0 > else return T[i,j-1] > fi; > end: > R:=6:T:=array(0..R-1,0..R-1,[[0,1,1,0,1,0],[0,1,1,0,1,0],[1,0,0,0,1,0],[1,0,0,0,1,0],[1,0,0,0,1,1],[1,1,1,1,1,0]]): > couleurN(T,0,3);couleurN(T,2,2); 0 1 Question 7 > C:=7:E:=array(0..C,[seq(i,i=0..C)]): > marquage:=proc(T) > local S,i,j,cno,cn,co; > global nc; > nc:=0; > S:=T; > for i from 0 to R-1 do > for j from 0 to R-1 do > if T[i,j]=0 then S[i,j]:=0 > else cno:=couleurNO(T,i,j); > cn:=couleurN(T,i,j); > co:=couleurO(T,i,j); > if cno<>0 then S[i,j]:=cno > elif co<>0 and cn=0 then S[i,j]:=co > elif cn<>0 and co=0 then S[i,j]:=cn > elif cn=0 and co=0 then nc:=nc+1;S[i,j]:=nc > elif cn=co and cn<>0 then S[i,j]:=cn > elif cn<>co and cn<>0 and co<>0 then S[i,j]:=cn; > ajout(E,cn,co) > else > fi; > fi; > od; > od; > return S; > end: > > print(marquage(T)); array(0 .. 5, 0 .. 5, [ (0, 0) = 0 (0, 1) = 1 (0, 2) = 1 (0, 3) = 0 (0, 4) = 2 (0, 5) = 0 (1, 0) = 0 (1, 1) = 1 (1, 2) = 1 (1, 3) = 0 (1, 4) = 2 (1, 5) = 0 (2, 0) = 3 (2, 1) = 0 (2, 2) = 0 (2, 3) = 0 (2, 4) = 2 (2, 5) = 0 (3, 0) = 3 (3, 1) = 0 (3, 2) = 0 (3, 3) = 0 (3, 4) = 2 (3, 5) = 0 (4, 0) = 3 (4, 1) = 0 (4, 2) = 0 (4, 3) = 0 (4, 4) = 2 (4, 5) = 2 (5, 0) = 3 (5, 1) = 3 (5, 2) = 3 (5, 3) = 3 (5, 4) = 2 (5, 5) = 0 ]) Question 8 > diffusion :=proc(T) > local S,i,j; > S:=T; > for i from 0 to R-1 do > for j from 0 to R-1 do > S[i,j]:=representant(E,T[i,j]) > od; > od; > return S; > end: > colorier:=proc(T) > global C; > local E; > E:=array(0..C,[seq(i,i=0..C)]); > marquage(T); > diffusion(T); > end: > print(colorier(T)); array(0 .. 5, 0 .. 5, [ (0, 0) = 0 (0, 1) = 1 (0, 2) = 1 (0, 3) = 0 (0, 4) = 3 (0, 5) = 0 (1, 0) = 0 (1, 1) = 1 (1, 2) = 1 (1, 3) = 0 (1, 4) = 3 (1, 5) = 0 (2, 0) = 3 (2, 1) = 0 (2, 2) = 0 (2, 3) = 0 (2, 4) = 3 (2, 5) = 0 (3, 0) = 3 (3, 1) = 0 (3, 2) = 0 (3, 3) = 0 (3, 4) = 3 (3, 5) = 0 (4, 0) = 3 (4, 1) = 0 (4, 2) = 0 (4, 3) = 0 (4, 4) = 3 (4, 5) = 3 (5, 0) = 3 (5, 1) = 3 (5, 2) = 3 (5, 3) = 3 (5, 4) = 3 (5, 5) = 0 ])