# MATLAB costumers and switches

1. Apr 5, 2008

### mralam

Hey guys i have to do the following:

we have n customers and m switches, and each switch can accomodate a maximum of c customers (where c > n=m). Write a function which uses a Greedy Algorithm to determine
which customers should be connected to which switch. it needs to generalise from two switches to m switches.
The algorithm that needs to be used is the following:
Code (Text):

L = 0.75*NC;

for i = 1:NC
for j=1:NS

for i = 1:NC
if distance(i,1) < distance(i,2)
if |S1| < L
S1 = S1 + {i}
else
S2 = S2 + {i}
end if
else
if |S2| < L then
S2 = S2 + {i}
else
S1 = S1 + {i}
end if
end if
end

where NC is the number of costumers
NS is the number of switches
S1 and S2 are the set of costumers connected to switch 1 and 2 respectively

I have done the following code:

Code (Text):

%Get the number of customers%
function retval = blah()
NC = input('Enter the number of costumers there is: ');
customers = [];

%Get each customers co-ordinates and place them into an array%
for i = 1:NC
%could also possibly get input as x y ..
mystr = sprintf('Enter the x value of costumer %i : ',i);
x = input(mystr);
mystr = sprintf('Enter the y value of costumer %i : ',i);
y = input(mystr);
%Array to hold customers%
customers = [customers;x y];
end

%Get the number of switches%
NS = input('Enter the number of switches there is: ');

switches=[];

%Get each switchs co-ordinates and place them into an array%
for i = 1:NS
%could also possibly get input as x y ..
mystr = sprintf('Enter the x value of switch %i : ',i);
x = input(mystr);
mystr = sprintf('Enter the y value of switch %i : ',i);
y = input(mystr);
%Array to hold customers%
switches = [switches;x y];
end

S1=[]; %S1 is the set of costumers connected to switch 1
S2=[]; %S2 is the set of costumers connected to switch 2

%Get the maximum number of costumers that the switch can accomandate

C = input('Enter maximum number of costumers that the switch can accomandate: ');

if C < (NC/NS)
frpintf('THis switch cannot be used')
else

%the following algorithm is for 2 switches only

L = 0.75*NC;

for i = 1:NC
for j=1:NS

for i = 1:NC
if distance(i,1) < distance(i,2)
if |S1| < L
S1 = S1 + {i}
else
S2 = S2 + {i}
end if
else
if |S2| < L then
S2 = S2 + {i}
else
S1 = S1 + {i}
end if
end if
end

but its not doing the job. Any suggestions on how i can fix this code up so it does what i need?

thanks