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