-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathedmonds.m
123 lines (84 loc) · 2.89 KB
/
edmonds.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
function[ED]=edmonds(V,E)%
%input is a directed graph,
%V- Set of vertices [v1, v2,v3....]
%E- Set of edges is [ v1 v2 weight(v1,v2); ...] format
% Author: Ashish Choudhary, Ph.D
% Pharmaceutical genomics division, TGEN.
% 13208,E Shea Blvd, Scottsdale, AZ 85259.
% Note/Disclaimer: This code is for academic purposes only.
% The implementation is derived from the book by Alan Gibbons.
%initialization
ED(1).BV=[];% Bucket of vertices
ED(1).BE=[];% Bucket of Edges
ED(1).V=V;
ED(1).E=E;
CURRENT_i=1;
while(1) % breaking condition later
CURRENT_i
V=ED(CURRENT_i).V
E=ED(CURRENT_i).E
VERTICES_NOT_IN_BV=setdiff(V,ED(CURRENT_i).BV);
if (numel(VERTICES_NOT_IN_BV)==0)
break; %first phase
end
%let us add the first such
VERTEX_ADDED=VERTICES_NOT_IN_BV(1);
ED(CURRENT_i).BV=[ED(CURRENT_i).BV,VERTEX_ADDED];%
%now check if largest incoming edge has a positive value
[EDGE_VALUE,EDGE_ADDED]=index_of_max_value_incoming_edge(E,VERTEX_ADDED);
if EDGE_VALUE>0 %dont do anything otherwise
%upon adding check if there is a cicuit.
%If so, we will need to relabel everything
[dist,path]=iscycle([E(ED(CURRENT_i).BE,:)],E(EDGE_ADDED,1),E(EDGE_ADDED,2))
ED(CURRENT_i).VERTICESINCKT=path;
%now if the path was of finite length this
% now adding to edge buckets
ED(CURRENT_i).BE=[ED(CURRENT_i).BE,EDGE_ADDED];
if dist<Inf
[GSTR,MAPVERT,MAPEDGE]=relabelgraph(ED(CURRENT_i));
ED(CURRENT_i).MAPPINGVERT=MAPVERT;
ED(CURRENT_i).MAPPINGEDGE=MAPEDGE;
CURRENT_i=CURRENT_i+1
ED(CURRENT_i).BV=GSTR.BV;
ED(CURRENT_i).BE=GSTR.BE;
ED(CURRENT_i).V=GSTR.V;
ED(CURRENT_i).E=GSTR.E;
end
end% end of EDGE_VALUE>0
end
%And now the reconstruction phase
%TREEMAX=reconstruct(ED);
%%
function[FLAGS]=exists_incoming_edge(G,NODEARRAY)
%finds if the nodes listed in the array have an incoming edge
for i=1:numel(NODEARRAY)
FLAGS(i)=ismember(NODEARRAY(i),G(:,2));
end
%%
function[EDGE_VALUE,EDGE_INDEX]=index_of_max_value_incoming_edge(G,VERTEX)
%first find incoming edges
if numel(G)==0
EDGE_VALUE=-1;
EDGE_INDEX=NaN;
return;
end
%
INDICES=find(G(:,2)==VERTEX)
VALUES=(G(INDICES,3));
[EDGE_VALUE,LOC]=max(VALUES);
EDGE_INDEX=INDICES(LOC);
%%
function[dist,path]=iscycle(G,S,D)
if size(G,1)>0
MAXN=max([S,D,max( unique(G(:,1:2)) )]);
G=[G;MAXN,MAXN,0];
DG = sparse(G(:,1),G(:,2),G(:,3));
[dist,path]=graphshortestpath(DG,S,D);
%if there is no path from S to D try D to S
if dist==Inf
[dist,path]=graphshortestpath(DG,D,S);
end
else
dist=Inf;
path=[];
end