demo/grid_graph_demo.cc
author alpar
Mon, 06 Feb 2006 09:11:53 +0000
changeset 1960 a60b681d0825
parent 1875 98698b69a902
permissions -rw-r--r--
- Increased max. number of iteration
- Better tests.
alpar@1956
     1
/* -*- C++ -*-
alpar@1956
     2
 *
alpar@1956
     3
 * This file is a part of LEMON, a generic C++ optimization library
alpar@1956
     4
 *
alpar@1956
     5
 * Copyright (C) 2003-2006
alpar@1956
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@1956
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@1956
     8
 *
alpar@1956
     9
 * Permission to use, modify and distribute this software is granted
alpar@1956
    10
 * provided that this copyright notice appears in all copies. For
alpar@1956
    11
 * precise terms see the accompanying LICENSE file.
alpar@1956
    12
 *
alpar@1956
    13
 * This software is provided "AS IS" with no warranty of any kind,
alpar@1956
    14
 * express or implied, and with no claim as to its suitability for any
alpar@1956
    15
 * purpose.
alpar@1956
    16
 *
alpar@1956
    17
 */
alpar@1956
    18
deba@1680
    19
#include <lemon/grid_graph.h>
deba@1680
    20
#include <lemon/graph_adaptor.h>
deba@1680
    21
#include <lemon/graph_to_eps.h>
deba@1681
    22
#include <lemon/bfs.h>
deba@1680
    23
#include <lemon/xy.h>
deba@1680
    24
deba@1680
    25
#include <iostream>
deba@1680
    26
#include <fstream>
deba@1680
    27
deba@1775
    28
///\ingroup demos
deba@1775
    29
///\file
deba@1775
    30
///\brief Labirinth example with grid graph.
deba@1775
    31
///
deba@1775
    32
///  Labirinth example with grid graph.
deba@1775
    33
///
deba@1775
    34
/// The input file is:
deba@1775
    35
///
deba@1775
    36
/// \include grid_graph_demo.in
deba@1775
    37
///
deba@1775
    38
/// The result:
deba@1775
    39
///
deba@1775
    40
/// \image html grid_graph_demo.png
deba@1775
    41
/// \image latex grid_graph_demo.eps "The labirinth" width=\textwidth
deba@1775
    42
///
deba@1775
    43
/// The source:
deba@1775
    44
///
deba@1775
    45
/// \include grid_graph_demo.cc
deba@1775
    46
deba@1680
    47
using namespace lemon;
deba@1680
    48
using namespace std;
deba@1680
    49
deba@1680
    50
int main() {
deba@1681
    51
  ifstream in("grid_graph_demo.in");
deba@1681
    52
  int width, height;
deba@1681
    53
  in >> width >> height;
deba@1681
    54
  int start_x, start_y;
deba@1681
    55
  in >> start_x >> start_y;
deba@1681
    56
  int stop_x, stop_y;
deba@1681
    57
  in >> stop_x >> stop_y;
deba@1681
    58
deba@1681
    59
  GridGraph graph(width, height);
deba@1681
    60
  GridGraph::Node start = graph(start_x - 1, start_y - 1);
deba@1681
    61
  GridGraph::Node stop = graph(stop_x - 1, stop_y - 1);
deba@1681
    62
  GridGraph::NodeMap<bool> filter(graph);
deba@1681
    63
deba@1681
    64
  for (int j = 0; j < height; ++j) {
deba@1681
    65
    in >> ws;
deba@1681
    66
    for (int i = 0; i < width; ++i) {
deba@1681
    67
      char c; in >> c;
deba@1681
    68
      filter[graph(i, j)] = (c == '.');
deba@1681
    69
    }
deba@1681
    70
  }
deba@1681
    71
deba@1681
    72
  typedef NodeSubGraphAdaptor<GridGraph,
deba@1681
    73
    GridGraph::NodeMap<bool> > FilteredGraph;
deba@1681
    74
deba@1681
    75
  FilteredGraph filtered(graph, filter);
deba@1681
    76
deba@1681
    77
  Bfs<FilteredGraph> bfs(filtered);
deba@1681
    78
  std::cout << "The length of shortest path: " << 
deba@1681
    79
    bfs.run(start, stop) << std::endl;
deba@1681
    80
deba@1693
    81
  FilteredGraph::EdgeMap<bool> path(filtered, false);
deba@1681
    82
  
deba@1681
    83
  for (GridGraph::Node node = stop; 
deba@1681
    84
       node != start; node = bfs.predNode(node)) {
deba@1763
    85
    path[bfs.predEdge(node)] = true;
deba@1681
    86
  }
deba@1681
    87
  
deba@1775
    88
  graphToEps(filtered, "grid_graph_demo.eps").scaleToA4().
deba@1680
    89
    title("Grid graph").
alpar@1875
    90
    copyright("(C) 2006 LEMON Project").
deba@1693
    91
    coords(scaleMap(indexMap(graph), 10)).
deba@1680
    92
    enableParallel().
deba@1693
    93
    nodeScale(0.5).
deba@1680
    94
    drawArrows().
deba@1693
    95
    edgeColors(composeMap(ColorSet(), path)).
deba@1680
    96
    run();
deba@1681
    97
  
deba@1775
    98
  std::cout << "The shortest path is written to grid_graph_demo.eps" 
deba@1775
    99
	    << std::endl;
deba@1681
   100
deba@1680
   101
  return 0;
deba@1680
   102
}