wolffd@0: % GENERAL GRAPH THEORY CLASS wolffd@0: % wolffd@0: % CAVE: Any special application oriented stuff wolffd@0: % should be outside here wolffd@0: wolffd@0: classdef Graph < DiGraph wolffd@0: wolffd@0: methods wolffd@0: wolffd@0: % --- wolffd@0: % Constructor wolffd@0: % --- wolffd@0: function G = Graph(V, E) wolffd@0: wolffd@0: if nargin == 1 wolffd@0: % --- wolffd@0: % this uses an imput grpaph wolffd@0: % to create a new graph wolffd@0: % --- wolffd@0: G = Graph.copy(V); wolffd@0: wolffd@0: elseif nargin == 2 wolffd@0: wolffd@0: if numel(V) == size(V,2) wolffd@0: G.V = V; wolffd@0: else wolffd@0: G.V = V'; wolffd@0: end wolffd@0: wolffd@0: G.E = E; wolffd@0: end wolffd@0: end wolffd@0: function w = edge(G, V1, V2) wolffd@0: wolffd@0: sm = min([V1, V2]); wolffd@0: bg = max([V1, V2]); wolffd@0: wolffd@0: w = G.E(sm, bg); wolffd@0: end wolffd@0: wolffd@0: % --- wolffd@0: % add an edge wolffd@0: % the undirected case: G.E is triangular wolffd@0: % --- wolffd@0: function add_edge(G, V1, V2, weight) wolffd@0: wolffd@0: G.add_node(V1); wolffd@0: G.add_node(V2); wolffd@0: wolffd@0: sm = min([V1, V2]); wolffd@0: bg = max([V1, V2]); wolffd@0: wolffd@0: if G.E(sm, bg) == 0 wolffd@0: wolffd@0: % save weight wolffd@0: G.E(sm, bg) = weight; wolffd@0: else wolffd@0: wolffd@0: join_edge(G, V1, V2, weight); wolffd@0: end wolffd@0: end wolffd@0: wolffd@0: % remove an edge wolffd@0: function remove_edge(G, V1, V2) wolffd@0: wolffd@0: sm = min([V1, V2]); wolffd@0: bg = max([V1, V2]); wolffd@0: wolffd@0: G.E(sm,bg) = 0; wolffd@0: end wolffd@0: wolffd@0: % --- wolffd@0: % sets the edge without considering earlier weights wolffd@0: % --- wolffd@0: function set_edge(G, V1, V2, weight) wolffd@0: wolffd@0: sm = min([V1, V2]); wolffd@0: bg = max([V1, V2]); wolffd@0: wolffd@0: G.E(sm, bg) = weight; wolffd@0: wolffd@0: end wolffd@0: wolffd@0: % --- wolffd@0: % Graph-specific functions wolffd@0: % in the undirected case, these are the same wolffd@0: % --- wolffd@0: function c = children(G, Vid) wolffd@0: wolffd@0: c = [find(G.E(Vid,:) > 0) find(G.E(:,Vid) > 0)']; wolffd@0: end wolffd@0: wolffd@0: function p = parents(G, Vid) wolffd@0: wolffd@0: p = [find(G.E(Vid,:) > 0) find(G.E(:,Vid) > 0)']; wolffd@0: end wolffd@0: wolffd@0: % --- wolffd@0: % Vertex Degree wolffd@0: % --- wolffd@0: function out = degree(G, V) wolffd@0: wolffd@0: out = sum(G.E(V,:) > 0) + sum(G.E(:,V) > 0); wolffd@0: end wolffd@0: wolffd@0: % --- wolffd@0: % Vertex Degree wolffd@0: % --- wolffd@0: function out = degree_in(G, V) wolffd@0: wolffd@0: out = sum(G.E(V,:) > 0) + sum(G.E(:,V) > 0); wolffd@0: end wolffd@0: wolffd@0: % --- wolffd@0: % Vertex Degree wolffd@0: % --- wolffd@0: function out = degree_out(G, V) wolffd@0: wolffd@0: out = sum(G.E(:,V) > 0) + sum(G.E(:,V) > 0); wolffd@0: end wolffd@0: wolffd@0: end wolffd@0: wolffd@0: wolffd@0: methods (Static) wolffd@0: function G = copy(Gin) wolffd@0: wolffd@0: if strcmp(class(Gin), 'Graph') wolffd@0: wolffd@0: G = Graph(Gin.V, Gin.E); wolffd@0: G.Vlbl = Gin.Vlbl; wolffd@0: wolffd@0: else wolffd@0: warning ('cannot copy graph, casting instead') wolffd@0: G = Graph.cast(Gin); wolffd@0: end wolffd@0: wolffd@0: end wolffd@0: wolffd@0: function G = cast(Gin) wolffd@0: wolffd@0: % --- wolffd@0: % this uses an imput grpaph wolffd@0: % to create a new digraph wolffd@0: % --- wolffd@0: G = Graph(); wolffd@0: wolffd@0: % --- wolffd@0: % Add the input graph to the empty one wolffd@0: % --- wolffd@0: G.add_graph(Gin); wolffd@0: end wolffd@0: end wolffd@0: end