COIN-OR::LEMON - Graph Library

source: lemon-tutorial/demo/hello_lemon.cc @ 9:a48bf0d3a790

Last change on this file since 9:a48bf0d3a790 was 5:29baa4a189bf, checked in by Peter Kovacs <kpeter@…>, 16 years ago

Port demo/hello_lemon.cc from SVN -r3501

File size: 2.7 KB
Line 
1/* -*- mode: C++; indent-tabs-mode: nil; -*-
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///\file
20///\brief Demonstrating the basic concepts and fetures of LEMON
21///
22/// This program is intended to be a "Hello World!" program that shows
23/// the very basic notions of LEMON: \ref graphs "graphs" and
24/// \ref maps "maps".
25///
26/// \include hello_lemon.cc
27
28#include <iostream>
29#include <lemon/list_graph.h>
30
31int main()
32{
33  // Convenience type definitions
34  typedef lemon::ListDigraph Digraph;
35  typedef Digraph::Node Node;
36  typedef Digraph::Arc Arc;
37  typedef Digraph::NodeIt NodeIt;
38  typedef Digraph::ArcIt ArcIt;
39  typedef Digraph::ArcMap<int> LengthMap;
40  using lemon::INVALID;
41
42  // Create a directed graph
43  Digraph g;
44
45  // Add nodes to the digraph
46  Node v1 = g.addNode();
47  Node v2 = g.addNode();
48  Node v3 = g.addNode();
49  Node v4 = g.addNode();
50
51  // Add arcs to the digraph
52  Arc v1_v2 = g.addArc(v1, v2);
53  Arc v1_v3 = g.addArc(v1, v3);
54  Arc v2_v3 = g.addArc(v2, v3);
55  Arc v2_v4 = g.addArc(v2, v4);
56  Arc v3_v4 = g.addArc(v3, v4);
57
58  // Create an arc map (length)
59  LengthMap length(g);
60
61  // Set the length of each arc
62  length[v1_v2]  = 10;
63  length[v1_v3]  = 20;
64  length[v2_v3] = 5;
65  length[v2_v4] = 25;
66  length[v3_v4] = 10;
67
68  // Welcome message
69  std::cout << "Hello World!" << std::endl;
70  std::cout << "This is LEMON library here. We have a direceted graph.";
71  std::cout << std::endl << std::endl;
72
73  // Iterate through the nodes and print their IDs
74  std::cout << "Nodes:";
75  for (NodeIt n(g); n != INVALID; ++n)
76    std::cout << " " << g.id(n);
77  std::cout << std::endl;
78
79  // Iterate through the arcs and print the IDs of their
80  // source and target nodes
81  std::cout << "Arcs:";
82  for (ArcIt a(g); a != INVALID; ++a)
83    std::cout << " (" << g.id(g.source(a)) << ","
84              << g.id(g.target(a)) << ")";
85  std::cout << std::endl << std::endl;
86
87  // Iterate through the arcs and print their length
88  std::cout << "There is a map on the arcs (length):" << std::endl;
89  std::cout << std::endl;
90  for (ArcIt a(g); a != INVALID; ++a)
91    std::cout << "length(" << g.id(g.source(a)) << ","
92              << g.id(g.target(a)) << ")=" << length[a] << std::endl;
93  std::cout << std::endl;
94
95  return 0;
96}
Note: See TracBrowser for help on using the repository browser.