grid_graph_demo.cc File Reference


Detailed Description

Labirinth example with grid graph.

The input file is:

10 8
1 1 10 8
..X....X.X
.XX.X.XX..
....X..XX.
XXXXXX.X..
.........X
.X.XXXXXXX
.X...XX...
.X.X....X.

The result:

grid_graph_demo.png

The source:

/* -*- C++ -*-
 *
 * This file is a part of LEMON, a generic C++ optimization library
 *
 * Copyright (C) 2003-2006
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
 *
 * Permission to use, modify and distribute this software is granted
 * provided that this copyright notice appears in all copies. For
 * precise terms see the accompanying LICENSE file.
 *
 * This software is provided "AS IS" with no warranty of any kind,
 * express or implied, and with no claim as to its suitability for any
 * purpose.
 *
 */

#include <lemon/grid_graph.h>
#include <lemon/graph_adaptor.h>
#include <lemon/graph_to_eps.h>
#include <lemon/bfs.h>
#include <lemon/xy.h>

#include <iostream>
#include <fstream>


using namespace lemon;
using namespace std;

int main() {
  ifstream in("grid_graph_demo.in");
  int width, height;
  in >> width >> height;
  int start_x, start_y;
  in >> start_x >> start_y;
  int stop_x, stop_y;
  in >> stop_x >> stop_y;

  GridGraph graph(width, height);
  GridGraph::Node start = graph(start_x - 1, start_y - 1);
  GridGraph::Node stop = graph(stop_x - 1, stop_y - 1);
  GridGraph::NodeMap<bool> filter(graph);

  for (int j = 0; j < height; ++j) {
    in >> ws;
    for (int i = 0; i < width; ++i) {
      char c; in >> c;
      filter[graph(i, j)] = (c == '.');
    }
  }

  typedef NodeSubGraphAdaptor<GridGraph,
    GridGraph::NodeMap<bool> > FilteredGraph;

  FilteredGraph filtered(graph, filter);

  Bfs<FilteredGraph> bfs(filtered);
  std::cout << "The length of shortest path: " << 
    bfs.run(start, stop) << std::endl;

  FilteredGraph::EdgeMap<bool> path(filtered, false);
  
  for (GridGraph::Node node = stop; 
       node != start; node = bfs.predNode(node)) {
    path[bfs.predEdge(node)] = true;
  }
  
  graphToEps(filtered, "grid_graph_demo.eps").scaleToA4().
    title("Grid graph").
    copyright("(C) 2006 LEMON Project").
    coords(scaleMap(indexMap(graph), 10)).
    enableParallel().
    nodeScale(0.5).
    drawArrows().
    edgeColors(composeMap(ColorSet(), path)).
    run();
  
  std::cout << "The shortest path is written to grid_graph_demo.eps" 
            << std::endl;

  return 0;
}

#include <lemon/grid_graph.h>
#include <lemon/graph_adaptor.h>
#include <lemon/graph_to_eps.h>
#include <lemon/bfs.h>
#include <lemon/xy.h>
#include <iostream>
#include <fstream>


Generated on Fri Feb 3 18:39:53 2006 for LEMON by  doxygen 1.4.6