annotate core/tools/Graph.m @ 0:cc4b1211e677 tip

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