-
Notifications
You must be signed in to change notification settings - Fork 0
/
hamiltonian.m
149 lines (124 loc) · 3.04 KB
/
hamiltonian.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
%%
% this function comes from mathworks.com, by Pramit Biswas
%
% Let us create the following graph
% (1)--(2)--(3)-------(4)
% | / \ | |
% | / \ | |
% | / \ | |
% (5)-------(6) |
% | |
% | |
% | |
% (7)-----------------(8)
%
% g=[0 1 0 0 1 0 0 0;
% 1 0 1 0 1 1 0 0;
% 0 1 0 1 0 1 0 0;
% 0 0 1 0 0 0 0 1;
% 1 1 0 0 0 1 1 0;
% 0 1 1 0 1 0 0 0;
% 0 0 0 0 1 0 0 1;
% 0 0 0 1 0 0 1 0]
% s=5; % Source
% d=1; % Destination
%
% P = hamiltonianPath(g,s,d);
%
% P will be an array mentioning the path/cycle, if path/cycle found; or a
% string: 'No Path Found', if path/cycle not found
%
% #Note: This code can be used for finding Hamiltonian cycle also. For
% that, make sure Source and Destination are same.
%%
%{
Main Function
%}
function hamPath = hamiltonian(Graph, Source, Destination)
% Input Checking
if ~isreal(Graph)
error('Graph must be in real form');
elseif ~isnumeric(Graph)
error('Matrix must be numeric');
elseif ~ismatrix(Graph)
error('Check Matrix Dimensions');
else
[r, c] = size (Graph);
if r~=c
error('Matrix must be square matrix');
end
end
if ~(isreal(Source)||isreal(Destination)||(Source>0 && Source<=r) || (Destination>0 && Destination<=r))
error('improper Source/Destination');
end
clear c;
% Function call
hamPath = findHam(Graph, Source, Destination, r);
end
%%
%{
This functions sets some initial parameters, and calls the actual
function.
%}
function hamPath = findHam(Graph, Source, Destination, totalNodes)
hamPath = zeros(size(Graph(1,:)));
hamPath(1) = Source;
[Status, hamPath] = hamRec(Graph, hamPath, Source, Destination, totalNodes, 1);
if Status == 0
if Source ~= Destination
hamPath = 'No Path Found';
else
hamPath = 'No Cycle Found';
end
return;
end
end
%%
%{
This function recursively call itself, hence finding the solution
%}
function [Status, hamPath] = hamRec(Graph, hamPath, Source, Destination, totalNodes, nodesFound)
% Ending Condition check
if ( (nodesFound == totalNodes-1 && Source~=Destination) || (nodesFound == totalNodes && Source==Destination) )
if ( Graph(hamPath(nodesFound), Destination) ~= 0)
hamPath(nodesFound+1) = Destination;
Status = 1;
return;
else
Status = 0;
return;
end
end
for i=1:totalNodes
if i==Destination
continue;
end
if isSafe(Graph, hamPath, nodesFound, i)
hamPath(nodesFound+1) = i;
[Status, hamPath] = hamRec(Graph, hamPath, Source, Destination, totalNodes, nodesFound+1);
if Status
return;
end
hamPath(nodesFound+1) = 0;
end
end
Status = 0;
end
%%
%{
This function is used to check whether the current node can be added
or not for making the path/cycle.
%}
function Flag = isSafe(Graph, hamPath, nodesFound, i)
if Graph(hamPath(nodesFound),i) == 0
Flag = 0;
return;
end
for ii=1:nodesFound
if hamPath(ii) == i
Flag = 0;
return;
end
end
Flag = 1;
end