Professional Documents
Culture Documents
Heuristic
Heuristic
%Algorithm run in O(n^2) time, where n is the number of vertices (worst case).
%An independent set of a graph is a subset of vertices in which no two vertices are
%adjacent. Given a set of vertices, the maximum independent set problem calls
%Maximum Independent Set", Advanced Modeling and Optimization, Volume 12, Number 1, 2010
%Notation:
%neighbors of v.
%The SUPPORT of a vertex v in V is defined by the sum of the degree of the vertices
%which are adjacent to v, i.e., support(v) = s(v) = sum( deg(u) ) where u are all
%vertices in N(v).
%INPUTS:
% "priority" is used to break the ties (parity) situations that occur when more than one
% max set can be selected. Consider for example the trivial case of two nodes connected
% by one edge: there are two possible maximum independent set. By using priority you can
% and
% x=findMIS(logical([0 1; 1 0]),[2 1]) %Higher priority to node 2
%OUTPUTS:
%x is an binary array where x(i)=1 if vertex i belongs to the Maximum independent set
% % --------------------------------
% % --------------------------------
function x=findMIS(AD,priority)
[N M]=size(AD);
if N~=M
end
x=-ones(N,1);
nID=1:N;
degree=sum(AD,1);
md = min(degree);
if sum(minDeg)>1 %If more than one vertex have the min number of adjacent vertices:
support=zeros(size(minDeg));
%For each minimum degree vertex consider the support (sum of degrees of all it adjacent vertices)
for i=find(minDeg)
support(i)=sum(degree(AD(i,:)));
end
%The corresponding rows and columns of the adjacency matrix will be removed
%The algorithm is called recursively until the adjacency matrix is not empty
ms=max(support);
if ms>0
%minDeg_maxSup -> vertices with minimum degree which maximize the support
minDeg_maxSup = find(support==ms);
else %if support is full of 0 -> all the vertices minDeg have deg=0
minDeg_maxSup=find(minDeg);
end
else
minDeg_maxSup=find(minDeg);
end
if length(minDeg_maxSup)>1
[mp j]=min(priority(minDeg_maxSup));
nodSel=minDeg_maxSup(j);
else
nodSel=minDeg_maxSup;
end
x(nodSel)=1;
x(AD(nodSel,:))=0;
assigned=x>-1;
AD(assigned,:)=[];
AD(:,assigned)=[];
nID(assigned)=[];
priority(assigned)=[];
if numel(AD)
x(nID)=findMIS(AD,priority);
end
% % % --------------------------------
% % % --------------------------------
Kode 2
%Anthony Chrabieh
%Bonus Problem
%A* Algorithm
clear all
clc
clf
xmax = 80;
ymax = 40;
start = [10,ymax/2];
goal = [xmax,ymax/2];
%Nodes
MAP = zeros(xmax,ymax);
MAP(40,15:25) = inf;
MAP(35:40,15) = inf;
MAP(35:40,25) = inf;
% NumberOfObjects = 50;
% k = 0;
% length = max(3,randi(20));
% x = randi(xmax-2*length)+length;
% y = randi(ymax-2*length)+length;
% direction = randi(4);
% if (direction == 1)
% x = x:x+length;
% elseif (direction == 2)
% x = x-length:x;
% elseif (direction == 3)
% y = y:y+length;
% elseif (direction == 4)
% y = y-length:y;
% end
% if (sum(isinf(MAP(x,y)))>0)
% continue
% end
% MAP(x,y) = inf;
% k = k + 1;
% end
%Heuristic Weight%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for x = 1:size(MAP,1)
for y = 1:size(MAP,2)
if(MAP(x,y)~=inf)
H(x,y) = weight*norm(goal-[x,y]);
G(x,y) = inf;
end
end
end
%Plotting%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
surf(MAP')
colormap(gray)
view(2)
hold all
plot(start(1),start(2),'s','MarkerFaceColor','b')
plot(goal(1),goal(2),'s','MarkerFaceColor','m')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%initial conditions%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
G(start(1),start(2)) = 0;
F(start(1),start(2)) = H(start(1),start(2));
closedNodes = [];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Solve
solved = false;
while(~isempty(openNodes))
pause(0.001)
[A,I] = min(openNodes(:,4));
current = openNodes(I,:);
plot(current(1),current(2),'o','color','g','MarkerFaceColor','g')
if(current(1:2)==goal)
closedNodes = [closedNodes;current];
solved = true;
break;
end
%remove current node from open set and add it to closed set
openNodes(I,:) = [];
closedNodes = [closedNodes;current];
for x = current(1)-1:current(1)+1
for y = current(2)-1:current(2)+1
if (x<1||x>xmax||y<1||y>ymax)
continue
end
if (isinf(MAP(x,y)))
continue
end
if (x==current(1)&&y==current(2))
continue
end
%if already in closed set skip
skip = 0;
for j = 1:size(closedNodes,1)
skip = 1;
break;
end
end
if(skip == 1)
continue
end
A = [];
if(~isempty(openNodes))
for j = 1:size(openNodes,1)
A = j;
break;
end
end
end
if(isempty(A))
G(x,y) = newG;
plot(x,y,'x','color','b')
continue
end
continue
end
G(x,y) = newG;
end
end
end
if (solved)
%Path plotting
j = size(closedNodes,1);
path = [];
while(j > 0)
x = closedNodes(j,1);
y = closedNodes(j,2);
j = closedNodes(j,5);
path = [x,y;path];
end
for j = 1:size(path,1)
plot(path(j,1),path(j,2),'x','color','r')
pause(0.01)
end
else
end