Read graph empty?

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Read graph empty?

Jens Müller
Hi, can you tell me what I am doing wrong in the attached code?

The graph I used can be found here:
www.tessarakt.de/stuff/planar100k.graphml.zip (1,5 MB ...)

When I run it, it says

$ ./1
#vertices: 0
make_map_as_colormap entered
writing something to MapAsColorMap
Speicherzugriffsfehle

The crash is due to the empty graph ...

It should be possible to add vertices to adjacency_list<vecS, vecS,
bidirectionalS, VertexProperties, EdgeProperties>?!?!

Cheers,

Jens

// system headers
#include <string>
#include <iostream>
#include <fstream>

// program options
// #include <boost/program_options.hpp>

// Boost graph library, including LEDA wrapper
// #include <boost/graph/leda_graph.hpp>
//#include "./leda_graph.hpp"
#include <boost/graph/adjacency_list.hpp>
#include <boost/dynamic_property_map.hpp>
#include <boost/utility.hpp>
#include <boost/graph/visitors.hpp>
#include <boost/graph/breadth_first_search.hpp>
//#include "breadth_first_search.hpp"

// graph_tool: graphml reader
#include "graphml.hpp"

#include "bfs_distmap_as_color_map.hpp"



struct EdgeProperties;

struct VertexProperties
{
  double x;
  double y;
  double weight;
  int dist;
  boost::graph_traits<
      boost::adjacency_list<boost::vecS, boost::vecS, boost::bidirectionalS, VertexProperties, EdgeProperties>
      >::edge_descriptor pred;
      int app_height;
      bool mark;
};

struct EdgeProperties
{
  double length;
};




int main() {
  using namespace boost;
  using namespace std;
 
  typedef adjacency_list<vecS, vecS, bidirectionalS, VertexProperties, EdgeProperties> Graph;
 
 

  Graph G; //(5);
  /*boost::add_edge(0, 2, G);
  boost::add_edge(1, 1, G);
  boost::add_edge(1, 3, G);
  boost::add_edge(1, 4, G);
  boost::add_edge(2, 1, G);
  boost::add_edge(2, 3, G);
  boost::add_edge(2, 4, G);
  boost::add_edge(3, 1, G);
  boost::add_edge(3, 4, G);
  boost::add_edge(4, 0, G);
  boost::add_edge(4, 1, G); */
 
  dynamic_properties dp;
  dp.property("coord1", get(&VertexProperties::x, G));
  dp.property("coord2", get(&VertexProperties::y, G));
  dp.property("length", get(&EdgeProperties::length, G));
 
 
  std::ifstream in;
  in.open("planar100k.graphml", ios::in);
 
  read_graphml(in, G, dp);
 
  graph_traits<Graph>::vertex_descriptor s = *((vertices(G)).first);
 
 
  typedef graph_traits<Graph>::vertex_iterator v_iter;
  v_iter vi, vi_end;
  for (tie(vi, vi_end)=vertices(G); vi!=vi_end; vi++)
    G[*vi].dist = -1;
 
  std::cout << "#vertices: " << num_vertices(G) << std::endl;
 
  breadth_first_visit(G, s,
                      visitor(boost::make_bfs_visitor
                          (record_distances(get(&VertexProperties::dist, G), on_tree_edge())))
                           .color_map(make_map_as_colormap(get(&VertexProperties::dist, G), -1))
                     );
   
  std::cout << "done" << std::endl;

}


//
//=======================================================================
// Copyright 2007 University of Karlsruhe
//  Author: Jens Mueller
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//=======================================================================
//
#ifndef BFS_DISTMAP_8737
#define BFS_DISTMAP_8737

/* Use distance map (or anything else which suits the following
   requirements) as a color map.

   Write operations are just ignored.

   Read operations work as follows: When the value in the underlying map
   equals a specified value, e.g. -1 in a distance  map, or the null pointer
   in a predecessor map, the 'White' value is returned, otherwise the
   'Black' value is returned.

   Note: This only is usable for application where there is no need to
   distinguish gray and black non-tree edge targets.
   
   Note: Setting the value in the underlying map so that it no longer
   corresponds to a 'White' value will have to be done by the visitor
   on the discover_vertex event.
*/

/* Template parameters:
   - ColorValue (default value used by helper function: default_color_type)
   - ReadablePropertyMap (underlying map)
*/

/* Runtime parameters:
   - ReadablePropertyMap pm
   - ReadablePropertyMap::value_type white
     (must be == comparable)
*/

#include <boost/property_map.hpp>
#include <iostream>

template<typename ReadablePropertyMap, typename ColorValue>
class MapAsColorMap
{
private:
  typename boost::property_traits<ReadablePropertyMap>::value_type white_;
  const ReadablePropertyMap& pm_;
 
public:
  typedef typename boost::property_traits<ReadablePropertyMap>::key_type key_type;
  typedef ColorValue value_type;
  typedef ColorValue reference;
  typedef boost::read_write_property_map_tag category;
  ColorValue get(key_type key);
  MapAsColorMap(const ReadablePropertyMap& pm,
    typename boost::property_traits<ReadablePropertyMap>::value_type white):
   white_(white), pm_(pm) { /* nothing */ };
};

template<typename ReadablePropertyMap>
MapAsColorMap<ReadablePropertyMap, boost::default_color_type>
make_map_as_colormap(const ReadablePropertyMap& pm,
  typename boost::property_traits<ReadablePropertyMap>::value_type white)
{
  std::cout << "make_map_as_colormap entered" << std::endl;
 
  return MapAsColorMap<ReadablePropertyMap, boost::default_color_type>(pm, white);
}


template<typename ReadablePropertyMap, typename ColorValue>
ColorValue MapAsColorMap<ReadablePropertyMap, ColorValue>::get(key_type key)
{
  std::cout << "get entered" << std::endl;
  std::cout << key << " " << boost::get(pm_, key) << std::endl;
  return (boost::get(pm_, key)==white_)
     ? boost::color_traits<ColorValue>::white()
     : boost::color_traits<ColorValue>::black();
}

template<typename ReadablePropertyMap, typename ColorValue>
ColorValue get(MapAsColorMap<ReadablePropertyMap, ColorValue> pmap,
  typename boost::property_traits<ReadablePropertyMap>::key_type key)
{
  return pmap.get(key);
}

template<typename ReadablePropertyMap, typename ColorValue>
void put(MapAsColorMap<ReadablePropertyMap, ColorValue> pmap,
  typename boost::property_traits<ReadablePropertyMap>::key_type key,
  typename boost::property_traits<ReadablePropertyMap>::value_type value)
{ std::cout << "writing something to MapAsColorMap" << std::endl;
/* nothing */ }

#endif
Reply | Threaded
Open this post in threaded view
|

Re: Read graph empty?

Jens Müller
Jens Müller wrote:

> Hi, can you tell me what I am doing wrong in the attached code?
>
> The graph I used can be found here:
> www.tessarakt.de/stuff/planar100k.graphml.zip (1,5 MB ...)
>
> When I run it, it says
>
> $ ./1
> #vertices: 0
> make_map_as_colormap entered
> writing something to MapAsColorMap
> Speicherzugriffsfehle
>
> The crash is due to the empty graph ...
>

I tried your first example graph at
https://projects.forked.de/graph-tool/wiki/GraphToolGuide#Fileformatdescription

That gives me:

terminate called after throwing an instance of 'boost::property_not_found'
   what():  Property not found: weight.


My dynamic properties are:

   dynamic_properties dp;
   dp.property("coord1", get(&VertexProperties::x, g));
   dp.property("coord2", get(&VertexProperties::y, g));
   dp.property("length", get(&EdgeProperties::length, g));

So I think this error message is a bit strange ...



Reading the 2nd example with edgedefault="undirected" changed to
"directed" seems to work, even with the dp as above.

Reading the 1st example btw does not even work with an empty dp object.

Somehow you seem to have a hard-coded weight property?




Reply | Threaded
Open this post in threaded view
|

Re: Read graph empty?

Tiago de Paula Peixoto
On 02/23/2007 02:02 PM, Jens Müller wrote:

> I tried your first example graph at
> https://projects.forked.de/graph-tool/wiki/GraphToolGuide#Fileformatdescription
>
> That gives me:
>
> terminate called after throwing an instance of 'boost::property_not_found'
>    what():  Property not found: weight.
>
>
> My dynamic properties are:
>
>    dynamic_properties dp;
>    dp.property("coord1", get(&VertexProperties::x, g));
>    dp.property("coord2", get(&VertexProperties::y, g));
>    dp.property("length", get(&EdgeProperties::length, g));
>
> So I think this error message is a bit strange ...
>
>
>
> Reading the 2nd example with edgedefault="undirected" changed to
> "directed" seems to work, even with the dp as above.
>
> Reading the 1st example btw does not even work with an empty dp object.
>
> Somehow you seem to have a hard-coded weight property?

The reader will always try to put a property it finds in the file in the
dynamic_properties you pass to it. If the property doesn't exist
already, it is up to the dynamic_property object to create it or not. By
default it throws an exception, instead of creating. You need to supply
it with a function in the constructor that will create it automatically.
Take a look at:

http://www.boost.org/libs/property_map/doc/dynamic_property_map.html#member-functions

--
Tiago de Paula Peixoto <[hidden email]>



Reply | Threaded
Open this post in threaded view
|

Re: Read graph empty?

Jens Müller
Tiago de Paula Peixoto schrieb:

> The reader will always try to put a property it finds in the file in the
> dynamic_properties you pass to it. If the property doesn't exist
> already, it is up to the dynamic_property object to create it or not. By
> default it throws an exception, instead of creating. You need to supply
> it with a function in the constructor that will create it automatically.
> Take a look at:
>
> http://www.boost.org/libs/property_map/doc/dynamic_property_map.html#member-functions
>

Sorry, yeah ... I think we got that topic already ... Nevertheless, I
don't understand the fundamental differences of your GraphML files and
mine that would make the reader do strange things, like not adding
vertices ...


Reply | Threaded
Open this post in threaded view
|

Re: Read graph empty?

Jens Müller
Jens Müller schrieb:

> Tiago de Paula Peixoto schrieb:
>
>> The reader will always try to put a property it finds in the file in the
>> dynamic_properties you pass to it. If the property doesn't exist
>> already, it is up to the dynamic_property object to create it or not. By
>> default it throws an exception, instead of creating. You need to supply
>> it with a function in the constructor that will create it automatically.
>> Take a look at:
>>
>> http://www.boost.org/libs/property_map/doc/dynamic_property_map.html#member-functions
>>
>
> Sorry, yeah ... I think we got that topic already ... Nevertheless, I
> don't understand the fundamental differences of your GraphML files and
> mine that would make the reader do strange things, like not adding
> vertices ...

OK, it was due to the strange namespace declaration in my files:

Yours:

<graphml xmlns="http://graphml.graphdrawing.org/xmlns"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns
http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd">



Mine:

<graphml
xmlns="http://graphml.graphdrawing.org/xmlns/1.0rc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns/1.0rc
                    graphml-structure-1.0rc.xsd

           http://graphml.graphdrawing.org/xmlns/1.0rc
                    graphml-attributes-1.0rc.xsd"
>

Sorry for the bother ... With your version, it works fine.


Reply | Threaded
Open this post in threaded view
|

Re: Read graph empty?

Tiago de Paula Peixoto
On 02/23/2007 09:01 PM, Jens Müller wrote:

> Sorry for the bother ... With your version, it works fine.

Good you figured it out... I'm completely out of time currently, that's
why I didn't look at it carefully. Fell free to ask about any other
problem you may have.

Cheers.
--
Tiago de Paula Peixoto <[hidden email]>