comparison core/tools/Graph.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:e9a9cd732c1e
1 % GENERAL GRAPH THEORY CLASS
2 %
3 % CAVE: Any special application oriented stuff
4 % should be outside here
5
6 classdef Graph < DiGraph
7
8 methods
9
10 % ---
11 % Constructor
12 % ---
13 function G = Graph(V, E)
14
15 if nargin == 1
16 % ---
17 % this uses an imput grpaph
18 % to create a new graph
19 % ---
20 G = Graph.copy(V);
21
22 elseif nargin == 2
23
24 if numel(V) == size(V,2)
25 G.V = V;
26 else
27 G.V = V';
28 end
29
30 G.E = E;
31 end
32 end
33 function w = edge(G, V1, V2)
34
35 sm = min([V1, V2]);
36 bg = max([V1, V2]);
37
38 w = G.E(sm, bg);
39 end
40
41 % ---
42 % add an edge
43 % the undirected case: G.E is triangular
44 % ---
45 function add_edge(G, V1, V2, weight)
46
47 G.add_node(V1);
48 G.add_node(V2);
49
50 sm = min([V1, V2]);
51 bg = max([V1, V2]);
52
53 if G.E(sm, bg) == 0
54
55 % save weight
56 G.E(sm, bg) = weight;
57 else
58
59 join_edge(G, V1, V2, weight);
60 end
61 end
62
63 % remove an edge
64 function remove_edge(G, V1, V2)
65
66 sm = min([V1, V2]);
67 bg = max([V1, V2]);
68
69 G.E(sm,bg) = 0;
70 end
71
72 % ---
73 % sets the edge without considering earlier weights
74 % ---
75 function set_edge(G, V1, V2, weight)
76
77 sm = min([V1, V2]);
78 bg = max([V1, V2]);
79
80 G.E(sm, bg) = weight;
81
82 end
83
84 % ---
85 % Graph-specific functions
86 % in the undirected case, these are the same
87 % ---
88 function c = children(G, Vid)
89
90 c = [find(G.E(Vid,:) > 0) find(G.E(:,Vid) > 0)'];
91 end
92
93 function p = parents(G, Vid)
94
95 p = [find(G.E(Vid,:) > 0) find(G.E(:,Vid) > 0)'];
96 end
97
98 % ---
99 % Vertex Degree
100 % ---
101 function out = degree(G, V)
102
103 out = sum(G.E(V,:) > 0) + sum(G.E(:,V) > 0);
104 end
105
106 % ---
107 % Vertex Degree
108 % ---
109 function out = degree_in(G, V)
110
111 out = sum(G.E(V,:) > 0) + sum(G.E(:,V) > 0);
112 end
113
114 % ---
115 % Vertex Degree
116 % ---
117 function out = degree_out(G, V)
118
119 out = sum(G.E(:,V) > 0) + sum(G.E(:,V) > 0);
120 end
121
122 end
123
124
125 methods (Static)
126 function G = copy(Gin)
127
128 if strcmp(class(Gin), 'Graph')
129
130 G = Graph(Gin.V, Gin.E);
131 G.Vlbl = Gin.Vlbl;
132
133 else
134 warning ('cannot copy graph, casting instead')
135 G = Graph.cast(Gin);
136 end
137
138 end
139
140 function G = cast(Gin)
141
142 % ---
143 % this uses an imput grpaph
144 % to create a new digraph
145 % ---
146 G = Graph();
147
148 % ---
149 % Add the input graph to the empty one
150 % ---
151 G.add_graph(Gin);
152 end
153 end
154 end