COIN-OR::LEMON - Graph Library

Ticket #81: port_dijkstra_demo_21b33e184d46.patch

File port_dijkstra_demo_21b33e184d46.patch, 3.9 KB (added by Peter Kovacs, 15 years ago)

Port demo/dijkstra_demo.cc

  • demo/CMakeLists.txt

    # HG changeset patch
    # User Peter Kovacs <kpeter@inf.elte.hu>
    # Date 1215441865 -7200
    # Node ID 21b33e184d46773be0a962811caef77048514d71
    # Parent  65cba1032f90cd157a56b911b4917920b07fc546
    Port demo/dijkstra_demo.cc from SVN -r3501
    
    diff -r 65cba1032f90 -r 21b33e184d46 demo/CMakeLists.txt
    a b  
    44
    55set (DEMOS
    66  arg_parser_demo
     7  dijkstra_demo
    78  graph_to_eps_demo
    89  lgf_demo)
    910
  • demo/Makefile.am

    diff -r 65cba1032f90 -r 21b33e184d46 demo/Makefile.am
    a b  
    66
    77noinst_PROGRAMS += \
    88        demo/arg_parser_demo \
     9        demo/dijkstra_demo \
    910        demo/graph_to_eps_demo \
    1011        demo/lgf_demo
    1112
    1213endif WANT_DEMO
    1314
    1415demo_arg_parser_demo_SOURCES = demo/arg_parser_demo.cc
     16demo_dijkstra_demo_SOURCES = demo/dijkstra_demo.cc
    1517demo_graph_to_eps_demo_SOURCES = demo/graph_to_eps_demo.cc
    1618demo_lgf_demo_SOURCES = demo/lgf_demo.cc
  • new file demo/dijkstra_demo.cc

    diff -r 65cba1032f90 -r 21b33e184d46 demo/dijkstra_demo.cc
    - +  
     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 usage of Dijkstra algorithm
     22///
     23/// Dijkstra's algorithm computes shortest paths between two nodes in a
     24/// digraph with arc lengths. Here we only show some of the facilities
     25/// supplied by our implementation. For the detailed documentation of
     26/// the LEMON Dijkstra class read \ref lemon::Dijkstra "this".
     27///
     28/// \include dijkstra_demo.cc
     29
     30#include <iostream>
     31#include <lemon/list_graph.h>
     32#include <lemon/dijkstra.h>
     33
     34using namespace lemon;
     35
     36int main (int, char*[])
     37{
     38  // Convenient type definitions
     39  typedef ListDigraph Digraph;
     40  typedef Digraph::Node Node;
     41  typedef Digraph::Arc Arc;
     42  typedef Digraph::ArcMap<int> LengthMap;
     43
     44  // Create a directed graph and a length map
     45  Digraph g;
     46
     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  Arc s_v2=g.addArc(s, v2);
     55  Arc s_v3=g.addArc(s, v3);
     56  Arc v2_v4=g.addArc(v2, v4);
     57  Arc v2_v5=g.addArc(v2, v5);
     58  Arc v3_v5=g.addArc(v3, v5);
     59  Arc v4_t=g.addArc(v4, t);
     60  Arc v5_t=g.addArc(v5, t);
     61
     62  LengthMap len(g);
     63
     64  len.set(s_v2, 10);
     65  len.set(s_v3, 10);
     66  len.set(v2_v4, 5);
     67  len.set(v2_v5, 8);
     68  len.set(v3_v5, 5);
     69  len.set(v4_t, 8);
     70  len.set(v5_t, 8);
     71
     72  std::cout << "This program is a simple demo of the LEMON Dijkstra class."
     73            << std::endl;
     74  std::cout << "We calculate the shortest path from node s to node t in a digraph."
     75            << std::endl << std::endl;
     76
     77  std::cout << "The id of s is " << g.id(s) << ", "
     78            << "the id of t is " << g.id(t) << "." << std::endl;
     79
     80  // Run Dijkstra algorithm
     81  Dijkstra<Digraph, LengthMap> dijkstra(g,len);
     82  dijkstra.run(s,t);
     83
     84  // Print the distance
     85  std::cout << "The distance of node t from node s: "
     86            << dijkstra.dist(t) << std::endl;
     87
     88  // Print the shortest path
     89  std::cout << "The shortest path from s to t goes through the "
     90            << "following nodes (in reverse order): " << std::endl;
     91
     92  for (Node v = t; v != s; v = dijkstra.predNode(v)) {
     93    std::cout << g.id(v) << "<-";
     94  }
     95  std::cout << g.id(s) << std::endl;
     96
     97  return 0;
     98}