0
2
1
... | ... |
@@ -21,9 +21,13 @@ |
21 | 21 |
///\brief Demonstrating graph input and output |
22 | 22 |
/// |
23 |
/// This simple demo program gives an example of how to read and write |
|
24 |
/// a graph and additional maps (on the nodes or the edges) from/to a |
|
25 |
/// |
|
23 |
/// This program gives an example of how to load a directed graph from |
|
24 |
/// an \ref lgf-format "LGF" file with the \ref lemon::DigraphReader |
|
25 |
/// "DigraphReader" class. |
|
26 | 26 |
/// |
27 |
/// \ |
|
27 |
/// The \c "digraph.lgf" file: |
|
28 |
/// \include digraph.lgf |
|
29 |
/// |
|
30 |
/// And the program which reads it: |
|
31 |
/// \include lgf_demo.cc |
|
28 | 32 |
|
29 | 33 |
#include <iostream> |
... | ... |
@@ -36,118 +40,27 @@ |
36 | 40 |
using namespace lemon; |
37 | 41 |
|
38 |
int main(int argc, const char *argv[]) { |
|
39 |
const int n = argc > 1 ? std::atoi(argv[1]) : 20; |
|
40 |
const int e = argc > 2 ? std::atoi(argv[2]) : static_cast<int>(n * std::log(double(n))); |
|
41 |
const int m = argc > 3 ? std::atoi(argv[3]) : 100; |
|
42 |
int main() { |
|
43 |
SmartDigraph g; |
|
44 |
SmartDigraph::ArcMap<int> cap(g); |
|
45 |
SmartDigraph::Node s, t; |
|
42 | 46 |
|
43 |
SmartDigraph digraph; |
|
44 |
|
|
45 |
std::stringstream ss; |
|
46 |
|
|
47 |
try { |
|
48 |
|
|
49 |
typedef SmartDigraph Digraph; |
|
50 |
typedef Digraph::Node Node; |
|
51 |
typedef Digraph::Arc Arc; |
|
52 |
typedef Digraph::ArcIt ArcIt; |
|
53 |
|
|
54 |
typedef Digraph::NodeMap<int> PotentialMap; |
|
55 |
typedef Digraph::ArcMap<int> CapacityMap; |
|
56 |
typedef Digraph::ArcMap<std::string> NameMap; |
|
57 |
|
|
58 |
Digraph digraph; |
|
59 |
PotentialMap potential(digraph); |
|
60 |
CapacityMap capacity(digraph); |
|
61 |
NameMap name(digraph); |
|
62 |
|
|
63 |
std::vector<Node> nodes; |
|
64 |
for (int i = 0; i < n; ++i) { |
|
65 |
Node node = digraph.addNode(); |
|
66 |
potential[node] = rnd[m]; |
|
67 |
nodes.push_back(node); |
|
68 |
} |
|
69 |
|
|
70 |
std::vector<Arc> arcs; |
|
71 |
for (int i = 0; i < e; ++i) { |
|
72 |
int s = rnd[n]; |
|
73 |
int t = rnd[n]; |
|
74 |
int c = rnd[m]; |
|
75 |
Arc arc = digraph.addArc(nodes[s], nodes[t]); |
|
76 |
capacity[arc] = c; |
|
77 |
std::ostringstream os; |
|
78 |
os << "arc \t" << i << std::endl; |
|
79 |
name[arc] = os.str(); |
|
80 |
arcs.push_back(arc); |
|
81 |
} |
|
82 |
|
|
83 |
|
|
84 |
DigraphWriter<Digraph>(ss, digraph). |
|
85 |
nodeMap("potential", potential). |
|
86 |
arcMap("capacity", capacity). |
|
87 |
arcMap("name", name). |
|
88 |
node("source", nodes[0]). |
|
89 |
node("target", nodes[1]). |
|
90 |
arc("bottleneck", arcs[e / 2]). |
|
91 |
|
|
47 |
digraphReader("digraph.lgf", g). // read the directeg graph into g |
|
48 |
arcMap("capacity", cap). // read the 'capacity' arc map into cap |
|
49 |
node("source", s). // read 'source' node to s |
|
50 |
node("target", t). // read 'target' node to t |
|
92 | 51 |
run(); |
93 | 52 |
|
94 |
} catch (DataFormatError& error) { |
|
95 |
std::cerr << error.what() << std::endl; |
|
96 |
|
|
53 |
std::cout << "Digraph read from 'digraph.lgf'" << std::endl; |
|
54 |
std::cout << "Number of nodes: " << countNodes(g) << std::endl; |
|
55 |
std::cout << "Number of arcs: " << countArcs(g) << std::endl; |
|
97 | 56 |
|
98 |
|
|
57 |
std::cout << "We can write it to the standard output:" << std::endl; |
|
99 | 58 |
|
100 |
typedef SmartDigraph Digraph; |
|
101 |
typedef Digraph::Node Node; |
|
102 |
typedef Digraph::Arc Arc; |
|
103 |
typedef Digraph::ArcIt ArcIt; |
|
104 |
|
|
105 |
typedef Digraph::NodeMap<int> LabelMap; |
|
106 |
typedef Digraph::NodeMap<int> PotentialMap; |
|
107 |
typedef Digraph::ArcMap<int> CapacityMap; |
|
108 |
typedef Digraph::ArcMap<std::string> NameMap; |
|
109 |
|
|
110 |
Digraph digraph; |
|
111 |
LabelMap label(digraph); |
|
112 |
PotentialMap potential(digraph); |
|
113 |
CapacityMap capacity(digraph); |
|
114 |
NameMap name(digraph); |
|
115 |
|
|
116 |
Node s, t; |
|
117 |
Arc a; |
|
118 |
|
|
119 |
std::string creator; |
|
120 |
|
|
121 |
for (int i = 0; i < n; ++i) { |
|
122 |
Node node = digraph.addNode(); |
|
123 |
label[node] = i; |
|
124 |
} |
|
125 |
|
|
126 |
DigraphReader<Digraph>(ss, digraph). |
|
127 |
useNodes(label). |
|
128 |
nodeMap("potential", potential). |
|
129 |
arcMap("capacity", capacity). |
|
130 |
arcMap("name", name). |
|
131 |
node("source", s). |
|
132 |
node("target", t). |
|
133 |
arc("bottleneck", a). |
|
134 |
|
|
59 |
digraphWriter(std::cout, g). // write g to the standard output |
|
60 |
arcMap("capacity", cap). // write cap into 'capacity' |
|
61 |
node("source", s). // write s to 'source' |
|
62 |
node("target", t). // write t to 'target' |
|
135 | 63 |
run(); |
136 | 64 |
|
137 |
DigraphWriter<Digraph>(std::cout, digraph). |
|
138 |
nodeMap("potential", potential). |
|
139 |
arcMap("capacity", capacity). |
|
140 |
arcMap("name", name). |
|
141 |
node("source", s). |
|
142 |
node("target", t). |
|
143 |
arc("bottleneck", a). |
|
144 |
attribute("creator", creator). |
|
145 |
run(); |
|
146 |
|
|
147 |
} catch (DataFormatError& error) { |
|
148 |
std::cerr << error.what() << std::endl; |
|
149 |
} |
|
150 |
|
|
151 |
|
|
152 | 65 |
return 0; |
153 | 66 |
} |
0 comments (0 inline)