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

Overview
Comment:remove functions from global namespace
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:6a5330309ea0a936949ebb0cc0261b9592a55292
User & Date: ravenspoint 2016-05-27 21:26:15
Context
2016-05-28
20:27
add trade check-in: 01a7d7f020 user: ravenspoint tags: trunk
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
Changes

Changes to main.cpp.

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
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
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
255
256
257
258
259
260
261
262
263
264
265
266
...
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
319
320
321
322


323
324
325
326
327
328
329


330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
    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 )
        {
            path.push_back( next );
            next = p[ next ];
        }
        path.push_back( start );

        // reverse into path from to
        reverse(path.begin(),path.end());

        return path;
    }
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 );
    // connect successive vertices with random weights
    for( int kvertex = 1; kvertex < 10; kvertex++ )
        Add( kvertex-1, kvertex, dis( gen ) );
    // connect random edges
    for( int krandom = 0; krandom < 3; krandom++ )
    {
        int v1, v2;
        do
        {
            v1 = dis(gen);
            v2 = dis(gen);
        }
        while( v1 == v2 );
        if( v1 > v2 )
        {
            int tmp;
            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
    @param[in] g the graph
    @param[in] start vertex
    @param[in] end vertex
    @param[out] path of vertices
    @param[out] dist length of path
*/
void Longest(
    graph_t& g,
    int start,
    int end,
    vector<int>& path,
    int& dist )
{
    // create copy of graph with negative weights
    graph_t negGraph = g;
    graph_traits<graph_t>::edge_iterator ei, ei_end;
    for (boost::tie(ei, ei_end) = edges(negGraph); ei != ei_end; ++ei)
    {
        negGraph[ *ei ].negWeight( );
    }

    // find shortest paths through negative weight graph
    int N = num_vertices(negGraph);
    cPredecessorMap pmap( theGraph );
    vector<int> distance(N);
    bellman_ford_shortest_paths(
        negGraph,
        N,
        weight_map(get(&cEdge::myWeight, negGraph))
        .predecessor_map(make_iterator_property_map(pmap.begin(), get(vertex_index, negGraph)))
        .distance_map(&distance[0])
        .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 >

................................................................................
    {
        *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()
{
//    // Construct test graph, ensuring it is a DAG
//    do
//    {


//        Construct();
//    }
//    while( ! TopSort() );

    ConstructWithCycle();
    graph_t dag( theGraph );
    ConvertToDAG( dag );



    // find longest path from first to last vertex
    vector< int > path;
    int dist;
    Longest( dag, 0, 9, path, dist );

    DisplayEdges();
    cout << "Longest path: ";
    for( int v : path )
        cout << v << " ";
    cout << " length " << dist << endl;


    return 0;
}







>
>
>
>
>
>
>
>
>
>










<
<
<
<
|



<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|
<

<
<
|



|
|
|
|
|

<
<
<
>
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|
|
|
|
<
<
<
<
<
<
<
<
|
<
<
<
|
<
<
>
|
<
<
<
<
>

>
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|
<



<
|
|
<
|
|
|
<
|
<
<
<
<
<
<
|

<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<


|







 







<
>
>
>
>
>
>
>
>
|
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>

|










|










|
|


|
|
>
>
>
>
>
>
>


<
<
<
>
>
|
<
<

<
<
<
>
>


|
<
|

|

<
<
|




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
...
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
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
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
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362



363
364
365


366



367
368
369
370
371

372
373
374
375


376
377
378
379
380
    void negWeight()
    {
        myWeight = - myWeight;
    }

    int myWeight;
};

/** Pass through a graph */
class cPath
{
public:
    vector<int> myPath;     // the vertices visited
    int myDistance;         // the sum of the edge weights

    void Display();
};

// 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;





class cGraph
{
public:




















































    graph_t myGraph;




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




    void DisplayEdges();








































    /** Construct a cyclic graph
    */
    void ConstructWithCycle();









};






class cDAG : public cGraph
{




public:

    cDAG( const cGraph& g );
























    /* Find longest path through the graph

    @param[in] start vertex
    @param[in] end vertex
    @param[out] path of vertices

    */
    void Longest(

        int start,
        int end,
        cPath& path );








};


















/** Specialized depth first search visitor to detect cycles
*/
class cDagVisitor:public default_dfs_visitor
{

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

................................................................................
    {
        *src = source( e, g );
        *dst = target( e, g );
        throw runtime_error("cyclic");
    }
};



/** 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::graph::make_iterator_property_map()
        It is intended to make invocations 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_t>::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
    */
    cPath
    Path( int start, int end )
    {
        cPath path;

        int next = end;
        while ( next != start )
        {
            path.myPath.push_back( next );
            next = p[ next ];
        }
        path.myPath.push_back( start );

        // reverse into path from to
        reverse(path.myPath.begin(),path.myPath.end());

        return path;
    }
private:
    graph_t& myGraph;
    vector<int> p;

};



//
///** 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 );
//    // connect successive vertices with random weights
//    for( int kvertex = 1; kvertex < 10; kvertex++ )
//        Add( kvertex-1, kvertex, dis( gen ) );
//    // connect random edges
//    for( int krandom = 0; krandom < 3; krandom++ )
//    {
//        int v1, v2;
//        do
//        {
//            v1 = dis(gen);
//            v2 = dis(gen);
//        }
//        while( v1 == v2 );
//        if( v1 > v2 )
//        {
//            int tmp;
//            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 );
//}

    void cGraph::ConstructWithCycle()
    {
        for( int kvertex = 0; kvertex < 10; kvertex++ )
            add_vertex( myGraph );
        // 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 cGraph::DisplayEdges()
{
    graph_traits<graph_t>::edge_iterator ei, ei_end;
    for (boost::tie(ei, ei_end) = edges(myGraph); ei != ei_end; ++ei)
    {
        std::cout << "(" << source(*ei, myGraph)
                  << "," << target(*ei, myGraph)
                  << ", w= " << myGraph[ *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;
//}

void cDAG::Longest(
    int start,
    int end,
    cPath& path )
{
    // create copy of graph with negative weights
    graph_t negGraph = myGraph;
    graph_traits<graph_t>::edge_iterator ei, ei_end;
    for (boost::tie(ei, ei_end) = edges(negGraph); ei != ei_end; ++ei)
    {
        negGraph[ *ei ].negWeight( );
    }

    // find shortest paths through negative weight graph
    int N = num_vertices(negGraph);
    cPredecessorMap pmap( negGraph );
    vector<int> distance(N);
    bellman_ford_shortest_paths(
        negGraph,
        N,
        weight_map(get(&cEdge::myWeight, negGraph))
        .predecessor_map(make_iterator_property_map(pmap.begin(), get(vertex_index, negGraph)))
        .distance_map(&distance[0])
        .root_vertex( start )
    );

    path = pmap.Path(start,end);

    path.myDistance = - distance[ end ];
}


cDAG::cDAG( const cGraph& g )
{
    myGraph = g.myGraph;

    // visitor to detect back edges
    cDagVisitor 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(myGraph, 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, myGraph );
        }
    }
}

void cPath::Display()
{
    for( int v : myPath )
        cout << v << " ";
    cout << " length " << myDistance << endl;
}

int main()
{



    // Construct cyclic test graph
    cGraph g;
    g.ConstructWithCycle();






    // convert to DAG
    cDAG dag( g );

    // find longest path from first to last vertex
    cPath path;

    dag.Longest( 0, 9, path );

    g.DisplayEdges();
    cout << "Longest path: ";


    path.Display();


    return 0;
}