graph_laplacian.hh 5.82 KB
Newer Older
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
// graph-tool -- a general graph modification and manipulation thingy
//
// Copyright (C) 2006-2013 Tiago de Paula Peixoto <tiago@skewed.de>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 3
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

#ifndef GRAPH_LAPLACIAN_HH
#define GRAPH_LAPLACIAN_HH

#include "graph.hh"
#include "graph_filtering.hh"
#include "graph_util.hh"

namespace graph_tool
{
Tiago Peixoto's avatar
Tiago Peixoto committed
27
using namespace boost;
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190

enum deg_t
{
    IN_DEG,
    OUT_DEG,
    TOTAL_DEG
};

template <class Graph, class Weight, class EdgeSelector>
typename property_traits<Weight>::value_type
sum_degree(Graph& g, typename graph_traits<Graph>::vertex_descriptor v,
           Weight w, EdgeSelector)
{
    typename property_traits<Weight>::value_type sum = 0;
    typename EdgeSelector::type e, e_end;
    for(tie(e, e_end) = EdgeSelector::get_edges(v, g); e != e_end; ++e)
        sum += get(w, *e);
    return sum;
}

template <class Graph, class EdgeSelector>
double
sum_degree(Graph& g, typename graph_traits<Graph>::vertex_descriptor v,
           ConstantPropertyMap<double, GraphInterface::edge_t> w, all_edges_iteratorS<Graph>)
{
    return total_degreeS()(v, g);
}

template <class Graph, class EdgeSelector>
double
sum_degree(Graph& g, typename graph_traits<Graph>::vertex_descriptor v,
           ConstantPropertyMap<double, GraphInterface::edge_t> w, in_edge_iteratorS<Graph>)
{
    return in_degreeS()(v, g);
}

template <class Graph, class EdgeSelector>
double
sum_degree(Graph& g, typename graph_traits<Graph>::vertex_descriptor v,
           ConstantPropertyMap<double, GraphInterface::edge_t> w, out_edge_iteratorS<Graph>)
{
    return out_degreeS()(v, g);
}

struct get_laplacian
{
    template <class Graph, class Index, class Weight>
    void operator()(const Graph& g, Index index, Weight weight, deg_t deg,
                    multi_array_ref<double,1>& data,
                    multi_array_ref<int32_t,1>& i,
                    multi_array_ref<int32_t,1>& j) const
    {
        int pos = 0;
        typename graph_traits<Graph>::edge_iterator e, e_end;
        for(tie(e, e_end) = edges(g); e != e_end; ++e)
        {
            if (source(*e, g) == target(*e, g))
                continue;

            data[pos] = -get(weight, *e);
            i[pos] = get(index, target(*e, g));
            j[pos] = get(index, source(*e, g));

            ++pos;
            if (!is_directed::apply<Graph>::type::value)
            {
                data[pos] = -get(weight, *e);
                i[pos] = get(index, source(*e, g));
                j[pos] = get(index, target(*e, g));
                ++pos;
            }
        }

        typename graph_traits<Graph>::vertex_iterator v, v_end;
        for(tie(v, v_end) = vertices(g); v != v_end; ++v)
        {
            double k = 0;
            switch (deg)
            {
            case OUT_DEG:
                k = sum_degree(g, *v, weight, out_edge_iteratorS<Graph>());
                break;
            case IN_DEG:
                k = sum_degree(g, *v, weight, in_edge_iteratorS<Graph>());
                break;
            case TOTAL_DEG:
                k = sum_degree(g, *v, weight, all_edges_iteratorS<Graph>());
            }
            data[pos] = k;
            i[pos] = j[pos] = get(index, *v);
            ++pos;
        }

    }
};



struct get_norm_laplacian
{
    template <class Graph, class Index, class Weight>
    void operator()(const Graph& g, Index index, Weight weight, deg_t deg,
                    multi_array_ref<double,1>& data,
                    multi_array_ref<int32_t,1>& i,
                    multi_array_ref<int32_t,1>& j) const
    {
        int pos = 0;
        typename graph_traits<Graph>::vertex_iterator v, v_end;
        for(tie(v, v_end) = vertices(g); v != v_end; ++v)
        {
            double ks = 0;
            switch (deg)
            {
            case OUT_DEG:
                ks = sum_degree(g, *v, weight, out_edge_iteratorS<Graph>());
                break;
            case IN_DEG:
                ks = sum_degree(g, *v, weight, in_edge_iteratorS<Graph>());
                break;
            case TOTAL_DEG:
                ks = sum_degree(g, *v, weight, all_edges_iteratorS<Graph>());
            }

            typename graph_traits<Graph>::out_edge_iterator e, e_end;
            for(tie(e, e_end) = out_edges(*v, g); e != e_end; ++e)
            {
                if (source(*e, g) == target(*e, g))
                    continue;
                double kt = 0;

                switch (deg)
                {
                case OUT_DEG:
                    kt = sum_degree(g, target(*e, g), weight, out_edge_iteratorS<Graph>());
                    break;
                case IN_DEG:
                    kt = sum_degree(g, target(*e, g), weight, in_edge_iteratorS<Graph>());
                    break;
                case TOTAL_DEG:
                    kt = sum_degree(g, target(*e, g), weight, all_edges_iteratorS<Graph>());
                }

                if (ks * kt > 0)
                    data[pos] = -get(weight, *e) / sqrt(ks * kt);
                i[pos] = get(index, target(*e, g));
                j[pos] = get(index, source(*e, g));

                ++pos;
            }

            if (ks > 0)
                data[pos] = 1;
            i[pos] = j[pos] = get(index, *v);
            ++pos;
        }

    }
};


} // namespace graph_tool

#endif // GRAPH_LAPLACIAN_HH