(*Example 1*)

A={{3,2,1,1,1,0,0,0,11},{1,0,1,5,0,1,0,0,5},{1,1,0,1,0,0,1,0,3},{0,1,0,0,0,0,0,1,2}};
target=-{1,2,1,4,0,0,0,0,0};

(*Using lexicographic pivot rule: pick entering variable with smallest index *)

tab1=tableau[A,target,{5,6,7,8}]
thetas[tab1,1]

tab2=tableau[A,target,{1,5,6,8}]
thetas[tab2,2]

tab3=tableau[A,target,{1,2,5,6}]
thetas[tab3,3]

tab4=tableau[A,target,{1,2,3,6}]
thetas[tab4,4]

tab5=tableau[A,target,{1,2,3,4}]
thetas[tab5,7]

tab6=tableau[A,target,{1,2,3,7}]

(*Optimal solution found!*)

(*Checking solution:*)
Maximize[x1+2x2+x3+4x4,3x1+2x2+x3+x4≤11&&x1+x3+5x4≤5&&x1+x2+x4≤3&&x2≤2&&x1≥0&&x2≥0&&x3≥0&&x4≥0,{x1,x2,x3,x4}]

(*Alternative solution, with pivot rule that selects largest coefficient*)

tab1=tableau[A,target,{5,6,7,8}]
thetas[tab1,4]

tab2=tableau[A,target,{5,4,7,8}]
thetas[tab2,2]

tab3=tableau[A,target,{5,4,2,8}]
thetas[tab3,3]

tab4=tableau[A,target,{5,4,2,3}]
thetas[tab4,1]

tab4=tableau[A,target,{5,1,2,3}]

(*Optimal solution found!*)

(*Checking solution:*)
Maximize[x1+2x2+x3+4x4,3x1+2x2+x3+x4≤11&&x1+x3+5x4≤5&&x1+x2+x4≤3&&x2≤2&&x1≥0&&x2≥0&&x3≥0&&x4≥0,{x1,x2,x3,x4}]

(*Exercise 3*)
A={{1,1,1,1,0,40},{1,2,3,0,1,100}};
target=-{7,9,10,0,0,0};
(*Using lexicographic pivot rule: pick entering variable with smallest index *)
tab1=tableau[A,target,{4,5}]
thetas[tab1,3]

tab2=tableau[A,target,{3,4}]
thetas[tab2,1]

tab3=tableau[A,target,{1,3}]
thetas[tab3,2]

tab4=tableau[A,target,{2,3}]

Maximize[7x1+9x2+10x3,x1+x2+x3<=40&&x1+2x2+3x3<=100&&x1>=0&&x2>=0&&x3>=0,{x1,x2,x3}]

(*Example 2*)
(*Unbounded LP*)
A={{1, -1, 1, 0, 1},{-1,1,0,1,2}};
target=-{1,0,0,0,0};

tab1=tableau[A,target,{3,4}]
thetas[tab1,1]

tab1=tableau[A,target,{1,4}]
thetas[tab1,2]

(* Now we can make x1 as large as we want, and target function too. LP is unbounded. *)

(*Example 3*)
(*Degeneracy*)
A={{-1, 1, 1, 0, 0},{1,0,0,1,2}};
target=-{0,1,0,0,0};

tab1=tableau[A,target,{3,4}]
thetas[tab1,2]

(*Similar degeneracies occurred in Example 1*)

tab2=tableau[A,target,{2,4}]
thetas[tab2,1]

tab3=tableau[A,target,{1,4}]
thetas[tab3,2]

tab4=tableau[A,target,{1,2}]