| 1 | /* -*- C++ -*- |
| 2 | * |
| 3 | * This file is a part of LEMON, a generic C++ optimization library |
| 4 | * |
| 5 | * Copyright (C) 2003-2008 |
| 6 | * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | * (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | * |
| 9 | * Permission to use, modify and distribute this software is granted |
| 10 | * provided that this copyright notice appears in all copies. For |
| 11 | * precise terms see the accompanying LICENSE file. |
| 12 | * |
| 13 | * This software is provided "AS IS" with no warranty of any kind, |
| 14 | * express or implied, and with no claim as to its suitability for any |
| 15 | * purpose. |
| 16 | * |
| 17 | */ |
| 18 | |
| 19 | ///\ingroup demos |
| 20 | ///\file |
| 21 | ///\brief Demonstrating the very basic notions of LEMON |
| 22 | /// |
| 23 | /// This program is intended to be a "Hello World!" program that shows |
| 24 | /// the very basic notions of LEMON: \ref graphs "graphs" and |
| 25 | /// \ref maps-page "maps". |
| 26 | /// |
| 27 | /// \include hello_lemon.cc |
| 28 | |
| 29 | #include <iostream> |
| 30 | #include <lemon/list_graph.h> |
| 31 | |
| 32 | int main() |
| 33 | { |
| 34 | // Convenient type definitions |
| 35 | typedef lemon::ListDigraph Digraph; |
| 36 | typedef Digraph::Node Node; |
| 37 | typedef Digraph::Arc Arc; |
| 38 | typedef Digraph::NodeIt NodeIt; |
| 39 | typedef Digraph::ArcIt ArcIt; |
| 40 | typedef Digraph::ArcMap<int> LengthMap; |
| 41 | using lemon::INVALID; |
| 42 | |
| 43 | // Create a directed graph |
| 44 | Digraph g; |
| 45 | |
| 46 | // Add nodes to the digraph |
| 47 | Node s = g.addNode(); |
| 48 | Node v2 = g.addNode(); |
| 49 | Node v3 = g.addNode(); |
| 50 | Node v4 = g.addNode(); |
| 51 | Node v5 = g.addNode(); |
| 52 | Node t = g.addNode(); |
| 53 | |
| 54 | // Add arcs to the digraph |
| 55 | Arc s_v2 = g.addArc(s, v2); |
| 56 | Arc s_v3 = g.addArc(s, v3); |
| 57 | Arc v2_v4 = g.addArc(v2, v4); |
| 58 | Arc v2_v5 = g.addArc(v2, v5); |
| 59 | Arc v3_v5 = g.addArc(v3, v5); |
| 60 | Arc v4_t = g.addArc(v4, t); |
| 61 | Arc v5_t = g.addArc(v5, t); |
| 62 | |
| 63 | // Create an arc map (length) |
| 64 | LengthMap length(g); |
| 65 | |
| 66 | // Set the length of each arc |
| 67 | length[s_v2] = 10; |
| 68 | length[s_v3] = 10; |
| 69 | length[v2_v4] = 5; |
| 70 | length[v2_v5] = 8; |
| 71 | length[v3_v5] = 5; |
| 72 | length[v4_t] = 8; |
| 73 | length[v5_t] = 8; |
| 74 | |
| 75 | std::cout << "Hello World!" << std::endl; |
| 76 | std::cout << std::endl; |
| 77 | std::cout << "This is library LEMON here. We have a direceted graph."; |
| 78 | std::cout << std::endl << std::endl; |
| 79 | |
| 80 | // Iterate through the nodes and print their IDs |
| 81 | std::cout << "Nodes:"; |
| 82 | for (NodeIt n(g); n != INVALID; ++n) |
| 83 | std::cout << " " << g.id(n); |
| 84 | std::cout << std::endl; |
| 85 | |
| 86 | // Iterate through the arcs and print the IDs of their |
| 87 | // source and target nodes |
| 88 | std::cout << "Arcs:"; |
| 89 | for (ArcIt a(g); a != INVALID; ++a) |
| 90 | std::cout << " (" << g.id(g.source(a)) << "," |
| 91 | << g.id(g.target(a)) << ")"; |
| 92 | std::cout << std::endl << std::endl; |
| 93 | |
| 94 | // Iterate through the arcs and print their length |
| 95 | std::cout << "There is a map on the arcs (length):" << std::endl; |
| 96 | std::cout << std::endl; |
| 97 | for (ArcIt a(g); a != INVALID; ++a) |
| 98 | std::cout << "length(" << g.id(g.source(a)) << "," |
| 99 | << g.id(g.target(a)) << ")=" << length[a] << std::endl; |
| 100 | std::cout << std::endl; |
| 101 | |
| 102 | return 0; |
| 103 | } |