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

Overview
Comment:initial
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:0f5e9a977b5b186095975b80a77879d6457f981f
User & Date: James 2016-05-26 18:30:48
Context
2016-05-27
13:48
fix build check-in: 5fa42415a9 user: ravenspoint tags: trunk
2016-05-26
18:30
initial check-in: 0f5e9a977b user: James tags: trunk
18:23
initial empty check-in check-in: fb1a4909b3 user: ravenspoint tags: trunk
Changes

Added longestpath.cbp.























































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_project_file>
	<FileVersion major="1" minor="6" />
	<Project>
		<Option title="longestpath" />
		<Option pch_mode="2" />
		<Option compiler="gcc" />
		<Build>
			<Target title="Debug">
				<Option output="../bin/longestpath" prefix_auto="1" extension_auto="1" />
				<Option object_output="obj/Debug/" />
				<Option type="1" />
				<Option compiler="gcc" />
				<Compiler>
					<Add option="-g" />
				</Compiler>
			</Target>
			<Target title="Release">
				<Option output="../bin/longestpath" prefix_auto="1" extension_auto="1" />
				<Option object_output="obj/Release/" />
				<Option type="1" />
				<Option compiler="gcc" />
				<Compiler>
					<Add option="-O2" />
				</Compiler>
				<Linker>
					<Add option="-s" />
				</Linker>
			</Target>
		</Build>
		<Compiler>
			<Add option="-Wall" />
			<Add option="-fexceptions" />
		</Compiler>
		<Unit filename="main.cpp" />
		<Extensions>
			<code_completion />
			<envvars />
			<debugger />
			<lib_finder disable_auto="1" />
		</Extensions>
	</Project>
</CodeBlocks_project_file>

Added main.cpp.

















































































































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
27
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
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
#include <iostream>
#include <random>

#include <boost/graph/graph_traits.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/dijkstra_shortest_paths.hpp>
#include <boost/graph/bellman_ford_shortest_paths.hpp>
#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;
    }
    int Weight()
    {
        return myWeight;
    }
    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 )
        {
            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;

};

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 );
    // 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 ));
    }
}
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
    @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 ];
}

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()
{
//    // 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;
}