Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:code docuentation
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:50fa3760872076563f649458650d3834de2e03f9
User & Date: ravenspoint 2016-05-27 14:20:37
Context
2016-05-27
21:26
remove functions from global namespace check-in: 6a5330309e user: ravenspoint tags: trunk
14:20
code docuentation check-in: 50fa376087 user: ravenspoint tags: trunk
13:48
fix build check-in: 5fa42415a9 user: ravenspoint tags: trunk
Changes

Changes to main.cpp.

8
9
10
11
12
13
14


15
16
17
18
19


20
21
22
23
24
25
26
..
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
..
84
85
86
87
88
89
90





91
92
93
94
95


96
97
98
99
100
101
102
...
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
...
208
209
210
211
212
213
214
215

216
217
218
219
220
221
222










223
224
225
226
227
228
229
230



231
232

233
234
235
236
237
238

239
240
241

242
243




244
245
246
247


248
249
250
251
252
253
254
#include <boost/graph/topological_sort.hpp>
#include <boost/property_map/property_map.hpp>
#include <boost/graph/depth_first_search.hpp>

using namespace std;
using namespace boost;



class cVertex
{

};



class cEdge
{
public:
    void Weight( int w )
    {
        myWeight = w;
    }
................................................................................
    void negWeight()
    {
        myWeight = - myWeight;
    }

    int myWeight;
};



typedef adjacency_list<
vecS, vecS,  directedS,
      cVertex, cEdge  > graph_t;
typedef graph_traits< graph_t >::vertex_iterator vit_t;
typedef graph_traits< graph_t >::edge_iterator eit_t;
typedef graph_traits< graph_t >::edge_descriptor ed_t;

graph_t theGraph;



class cPredecessorMap
{
public:




    cPredecessorMap(graph_t& g )
        : myGraph( g )
    {
        p.resize( num_vertices(myGraph) );
    }
    vector<int>::iterator
    begin()
    {
        return p.begin();
    }








//    iterator_property_map<int*, property_map<graph_t,vertex_index>::type, int, int&>
//    iter()
//    {
//        return make_iterator_property_map(p.begin(), get(vertex_index, myGraph));
//    }






    vector<int>
    Path( int start, int end )
    {

        vector<int> path ;
        int next = end;
        while ( next != start )
................................................................................
    }
private:
    graph_t& myGraph;
    vector<int> p;

};






void Add( int src, int dst, int weight )
{
    theGraph[add_edge( src, dst, theGraph ).first].Weight( weight );
}



void Construct()
{
    std::random_device rd;
    std::mt19937_64 gen(rd());
    std::uniform_int_distribution<int> dis(0,9);
    for( int kvertex = 0; kvertex < 10; kvertex++ )
        add_vertex( theGraph );
................................................................................
            tmp = v1;
            v1 = v2;
            v2 = tmp;
        }
        Add( dis(gen), dis(gen), dis( gen ));
    }
}


void Construct2()
{
    for( int kvertex = 0; kvertex < 3; kvertex++ )
        add_vertex( theGraph );

    Add( 0, 1, 1 );
    Add( 0, 2, 2 );
    Add( 1, 2, 3 );
}


void ConstructWithCycle()
{
    for( int kvertex = 0; kvertex < 10; kvertex++ )
        add_vertex( theGraph );
    // connect successive vertices with random weights
    for( int kvertex = 1; kvertex < 10; kvertex++ )
        Add( kvertex-1, kvertex, 1 );

    Add( 6, 4, 1 );
    Add( 7, 3, 1 );
}


void DisplayEdges()
{
    graph_traits<graph_t>::edge_iterator ei, ei_end;
    for (boost::tie(ei, ei_end) = edges(theGraph); ei != ei_end; ++ei)
    {
        std::cout << "(" << source(*ei, theGraph)
                  << "," << target(*ei, theGraph)
                  << ", w= " << theGraph[ *ei ].Weight()
                  << " )\n ";

    }
    std::cout << std::endl;
}



bool TopSort()
{
    try
    {
        std::vector< int > c;
        topological_sort(theGraph, std::back_inserter(c));
    }
    catch( not_a_dag )
    {



        cout << "not a dag\n";
        return false;
    }
    cout << "top sort OK\n";
    return true;
}
/* Find longest path through the graph
................................................................................
        .root_vertex( start )
    );

    path = pmap.Path(start,end);

    dist = - distance[ end ];
}


class dag_visitor:public default_dfs_visitor
{

public:
    unsigned int * src;
    unsigned int * dst;
    template < typename Edge, typename Graph >










    void back_edge( Edge e, Graph& g)
    {
        *src = source( e, g );
        *dst = target( e, g );
        throw runtime_error("cyclic");
    }
};




void ConvertToDAG( graph_t& g)
{

    dag_visitor vis;
    unsigned int src, dst;
    vis.src = &src;
    vis.dst = &dst;
    bool cyclic = true;
    while( cyclic )

    {
        try
        {

            depth_first_search(g, visitor(vis));
            cyclic = false;




        }
        catch( runtime_error& e )
        {
            cyclic = true;


             cout << "removing " << src << " -> " << dst << endl;
            remove_edge( src, dst, g );
        }
    }

}
int main()







>
>





>
>







 







>
>










>
>




>
>
>










>
>
>
>
>
>
>
>





>
>
>
>
>
>







 







>
>
>
>
>


|


>
>







 







>
>









|
>




|


>



|
>













|
>
>









>
>
>







 







|
>







>
>
>
>
>
>
>
>
>
>








>
>
>


>




|
|
>



>

<
>
>
>
>



|
>
>







8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
..
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
...
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
...
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
...
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301

302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#include <boost/graph/topological_sort.hpp>
#include <boost/property_map/property_map.hpp>
#include <boost/graph/depth_first_search.hpp>

using namespace std;
using namespace boost;

/** Bundled properties for graph vertices - currently none
*/
class cVertex
{

};

/** Bundled properties for graph edges - currently just weight
*/
class cEdge
{
public:
    void Weight( int w )
    {
        myWeight = w;
    }
................................................................................
    void negWeight()
    {
        myWeight = - myWeight;
    }

    int myWeight;
};

// graph definitions

typedef adjacency_list<
vecS, vecS,  directedS,
      cVertex, cEdge  > graph_t;
typedef graph_traits< graph_t >::vertex_iterator vit_t;
typedef graph_traits< graph_t >::edge_iterator eit_t;
typedef graph_traits< graph_t >::edge_descriptor ed_t;

graph_t theGraph;

/** The paths from a vertex to all other vertices
*/
class cPredecessorMap
{
public:

    /** Constructor
        @param[in] g the graph
    */
    cPredecessorMap(graph_t& g )
        : myGraph( g )
    {
        p.resize( num_vertices(myGraph) );
    }
    vector<int>::iterator
    begin()
    {
        return p.begin();
    }
    /** Make a property map iterator
        @return property map iterator

        This is a wrapper for boost::grapg::make_iterator_property_map()
        It is intended to make incocations of boost graph methods more readable
        However, it does not ( yet! ) compile because I cannot write the
        correct typedef for the iterator
    */
//    iterator_property_map<int*, property_map<graph_t,vertex_index>::type, int, int&>
//    iter()
//    {
//        return make_iterator_property_map(p.begin(), get(vertex_index, myGraph));
//    }

    /** The path from one vertex to another
        @param[in] start vertex
        @param[in] end vertex
        @return vector of vertex indices of path from start to end
    */
    vector<int>
    Path( int start, int end )
    {

        vector<int> path ;
        int next = end;
        while ( next != start )
................................................................................
    }
private:
    graph_t& myGraph;
    vector<int> p;

};

/** Add edge to the global graph
    @param[in] source vertex
    @param[in] destination vertex
    @param[in] weight
*/
void Add( int src, int dst, int weight )
{
    theGraph[ add_edge( src, dst, theGraph ).first ].Weight( weight );
}

/** Construct graph with some randomness
*/
void Construct()
{
    std::random_device rd;
    std::mt19937_64 gen(rd());
    std::uniform_int_distribution<int> dis(0,9);
    for( int kvertex = 0; kvertex < 10; kvertex++ )
        add_vertex( theGraph );
................................................................................
            tmp = v1;
            v1 = v2;
            v2 = tmp;
        }
        Add( dis(gen), dis(gen), dis( gen ));
    }
}
/** Construct a test graph
*/
void Construct2()
{
    for( int kvertex = 0; kvertex < 3; kvertex++ )
        add_vertex( theGraph );

    Add( 0, 1, 1 );
    Add( 0, 2, 2 );
    Add( 1, 2, 3 );
}
/** Construct a cyclic graph
*/
void ConstructWithCycle()
{
    for( int kvertex = 0; kvertex < 10; kvertex++ )
        add_vertex( theGraph );
    // connect successive vertices
    for( int kvertex = 1; kvertex < 10; kvertex++ )
        Add( kvertex-1, kvertex, 1 );
    // add cycles
    Add( 6, 4, 1 );
    Add( 7, 3, 1 );
}
/** Display graph edges
*/
void DisplayEdges()
{
    graph_traits<graph_t>::edge_iterator ei, ei_end;
    for (boost::tie(ei, ei_end) = edges(theGraph); ei != ei_end; ++ei)
    {
        std::cout << "(" << source(*ei, theGraph)
                  << "," << target(*ei, theGraph)
                  << ", w= " << theGraph[ *ei ].Weight()
                  << " )\n ";

    }
    std::cout << std::endl;
}
/** Topological sort test for acyclic graph
    @return true if graph is acyclic
*/
bool TopSort()
{
    try
    {
        std::vector< int > c;
        topological_sort(theGraph, std::back_inserter(c));
    }
    catch( not_a_dag )
    {
        // boost topological sort method thres an exception
        // indicating the graph is not a directed acyclic graph

        cout << "not a dag\n";
        return false;
    }
    cout << "top sort OK\n";
    return true;
}
/* Find longest path through the graph
................................................................................
        .root_vertex( start )
    );

    path = pmap.Path(start,end);

    dist = - distance[ end ];
}
/** Specialized depth first search visitor to detect cycles
*/
class dag_visitor:public default_dfs_visitor
{

public:
    unsigned int * src;
    unsigned int * dst;
    template < typename Edge, typename Graph >

    /** Called by DFS when back edge found
        @param[in] e the back edge found
        @param[in] g the graph being searched

        A back edge is one that returns the search to a previously found vertex,
        indicating a cycle.

        This will throw an exception when it occurrs
    */
    void back_edge( Edge e, Graph& g)
    {
        *src = source( e, g );
        *dst = target( e, g );
        throw runtime_error("cyclic");
    }
};

/** Convert graph to acyclice by remove back edges if neccessary
    @param[in/out] g the graph to convert
*/
void ConvertToDAG( graph_t& g)
{
    // visitor to detect back edges
    dag_visitor vis;
    unsigned int src, dst;
    vis.src = &src;
    vis.dst = &dst;

    // loop while graph is still cyclic
    while( 1 )
    {
        try
        {
            // depth first search using back edge detecting visitor
            depth_first_search(g, visitor(vis));


            // the seach found no cycles
            // so we can break out of the loop
            break;
        }
        catch( runtime_error& e )
        {
            // the search through an excepting indicating a cycle

            // remove back edge found
             cout << "removing " << src << " -> " << dst << endl;
            remove_edge( src, dst, g );
        }
    }

}
int main()