16 graphs then you might find it useful to use our library LEMON. LEMON |
16 graphs then you might find it useful to use our library LEMON. LEMON |
17 defines various graph concepts depending on what you want to do with the |
17 defines various graph concepts depending on what you want to do with the |
18 graph: a very good description can be found in the page |
18 graph: a very good description can be found in the page |
19 about \ref graphs "graphs". |
19 about \ref graphs "graphs". |
20 |
20 |
21 You will also want to assign data to the edges or nodes of the graph, for example a length or capacity function defined on the edges. You can do this in LEMON using so called \ref maps "maps". You can define a map on the nodes or on the edges of the graph and the value of the map (the range of the function) can be practically almost any type. Read more about maps \ref maps-page "here". |
21 You will also want to assign data to the edges or nodes of the graph, for example a length or capacity function defined on the edges. You can do this in LEMON using so called \ref maps "maps". You can define a map on the nodes or on the edges of the graph and the value of the map (the range of the function) can be practically almost of any type. Read more about maps \ref maps-page "here". |
22 |
22 |
23 Some examples are the following (you will find links next to the code fragments that help to download full demo programs): |
23 Some examples are the following (you will find links next to the code fragments that help to download full demo programs: save them on your computer and compile them according to the description in the page about \ref getsart How to start using LEMON): |
24 |
24 |
25 - First we give two examples that show how to instantiate a graph. The |
25 - First we give two examples that show how to instantiate a graph. The |
26 first one shows the methods that add nodes and edges, but one will |
26 first one shows the methods that add nodes and edges, but one will |
27 usually use the second way which reads a graph from a stream (file). |
27 usually use the second way which reads a graph from a stream (file). |
28 -# The following code fragment shows how to fill a graph with data. It creates a complete graph on 4 nodes. The type Listgraph is one of the LEMON graph types: the typedefs in the beginning are for convenience and we will supppose them later as well. |
28 -# The following code fragment shows how to fill a graph with data. It creates a complete graph on 4 nodes. The type Listgraph is one of the LEMON graph types: the typedefs in the beginning are for convenience and we will suppose them later as well. |
29 \code |
29 \code |
30 typedef ListGraph Graph; |
30 typedef ListGraph Graph; |
31 typedef Graph::Edge Edge; |
|
32 typedef Graph::InEdgeIt InEdgeIt; |
|
33 typedef Graph::OutEdgeIt OutEdgeIt; |
|
34 typedef Graph::EdgeIt EdgeIt; |
|
35 typedef Graph::Node Node; |
|
36 typedef Graph::NodeIt NodeIt; |
31 typedef Graph::NodeIt NodeIt; |
37 |
32 |
38 Graph g; |
33 Graph g; |
39 |
34 |
40 for (int i = 0; i < 3; i++) |
35 for (int i = 0; i < 3; i++) |
43 for (NodeIt i(g); i!=INVALID; ++i) |
38 for (NodeIt i(g); i!=INVALID; ++i) |
44 for (NodeIt j(g); j!=INVALID; ++j) |
39 for (NodeIt j(g); j!=INVALID; ++j) |
45 if (i != j) g.addEdge(i, j); |
40 if (i != j) g.addEdge(i, j); |
46 \endcode |
41 \endcode |
47 |
42 |
|
43 See the whole program in file \ref helloworld.cc. |
|
44 |
48 If you want to read more on the LEMON graph structures and concepts, read the page about \ref graphs "graphs". |
45 If you want to read more on the LEMON graph structures and concepts, read the page about \ref graphs "graphs". |
49 |
46 |
50 -# The following code shows how to read a graph from a stream (e.g. a file). LEMON supports the DIMACS file format: it can read a graph instance from a file |
47 -# The following code shows how to read a graph from a stream (e.g. a file). LEMON supports the DIMACS file format: it can read a graph instance from a file |
51 in that format (find the documentation of the DIMECS file format on the web). |
48 in that format (find the documentation of the DIMACS file format on the web). |
52 \code |
49 \code |
53 Graph g; |
50 Graph g; |
54 std::ifstream f("graph.dim"); |
51 std::ifstream f("graph.dim"); |
55 readDimacs(f, g); |
52 readDimacs(f, g); |
56 \endcode |
53 \endcode |
99 len.set(v2_v5, 8); |
96 len.set(v2_v5, 8); |
100 len.set(v3_v5, 5); |
97 len.set(v3_v5, 5); |
101 len.set(v4_t, 8); |
98 len.set(v4_t, 8); |
102 len.set(v5_t, 8); |
99 len.set(v5_t, 8); |
103 |
100 |
104 std::cout << "The id of s is " << g.id(s)<< ", the id of t is " << g.id(t)<<"."<<std::endl; |
101 std::cout << "The id of s is " << g.id(s)<< std::endl; |
|
102 std::cout <<"The id of t is " << g.id(t)<<"."<<std::endl; |
105 |
103 |
106 std::cout << "Dijkstra algorithm test..." << std::endl; |
104 std::cout << "Dijkstra algorithm test..." << std::endl; |
107 |
105 |
108 Dijkstra<Graph, LengthMap> dijkstra_test(g,len); |
106 Dijkstra<Graph, LengthMap> dijkstra_test(g,len); |
109 |
107 |
110 dijkstra_test.run(s); |
108 dijkstra_test.run(s); |
111 |
109 |
112 |
110 |
113 std::cout << "The distance of node t from node s: " << dijkstra_test.dist(t)<<std::endl; |
111 std::cout << "The distance of node t from node s: " << dijkstra_test.dist(t)<<std::endl; |
114 |
112 |
115 std::cout << "The shortest path from s to t goes through the following nodes (the first one is t, the last one is s): "<<std::endl; |
113 std::cout << "The shortest path from s to t goes through the following nodes" <<std::endl; |
|
114 std::cout << " (the first one is t, the last one is s): "<<std::endl; |
116 |
115 |
117 for (Node v=t;v != s; v=dijkstra_test.predNode(v)){ |
116 for (Node v=t;v != s; v=dijkstra_test.predNode(v)){ |
118 std::cout << g.id(v) << "<-"; |
117 std::cout << g.id(v) << "<-"; |
119 } |
118 } |
120 std::cout << g.id(s) << std::endl; |
119 std::cout << g.id(s) << std::endl; |
134 of wires then you might be looking for a <b>minimum spanning tree</b> in |
133 of wires then you might be looking for a <b>minimum spanning tree</b> in |
135 an undirected graph. This can be found using the Kruskal algorithm: the |
134 an undirected graph. This can be found using the Kruskal algorithm: the |
136 class \ref lemon::Kruskal "LEMON Kruskal class" does this job for you. |
135 class \ref lemon::Kruskal "LEMON Kruskal class" does this job for you. |
137 The following code fragment shows an example: |
136 The following code fragment shows an example: |
138 |
137 |
|
138 Ide Zsuzska fog irni! |
|
139 |
|
140 - |
|
141 |
139 \code |
142 \code |
140 |
143 |
141 \endcode |
144 \endcode |
142 |
145 |
143 |
146 |