1

I write several graphs with n vertex, m edges and S attributes into file. now I need to read these unknown number of graphs from Graphviz DOT file by Boost graph library. How to read these unknown number of graphs with unknown number of vertex, egdes and properties? This is the file which saves the graphs.Thank you in advance!

digraph G {
0[Attribute0=0, Attribute1=1, Attribute2=0, ]
;
1[Attribute0=2, Attribute1=1, Attribute2=1, ]
;
2[Attribute0=1, Attribute1=2, Attribute2=2, ]
;
3[Attribute0=2, Attribute1=0, Attribute2=2, ]
;
4[Attribute0=2, Attribute1=1, Attribute2=0, ]
;
5[Attribute0=0, Attribute1=0, Attribute2=1, ]
;
6[Attribute0=0, Attribute1=0, Attribute2=2, ]
;
7[Attribute0=1, Attribute1=0, Attribute2=0, ]
;
8[Attribute0=0, Attribute1=1, Attribute2=1, ]
;
9[Attribute0=1, Attribute1=1, Attribute2=1, ]
;
0->4  [label="(0,4)"]
;
3->7  [label="(3,7)"]
;
5->6  [label="(5,6)"]
;
6->1  [label="(6,1)"]
;
9->5  [label="(9,5)"]
;
}

digraph G {
0[Attribute0=0, Attribute1=1, Attribute2=1, ]
;
1[Attribute0=1, Attribute1=0, Attribute2=1, ]
;
2[Attribute0=2, Attribute1=2, Attribute2=1, ]
;
3[Attribute0=0, Attribute1=2, Attribute2=2, ]
;
4[Attribute0=2, Attribute1=2, Attribute2=1, ]
;
5[Attribute0=0, Attribute1=1, Attribute2=0, ]
;
6[Attribute0=2, Attribute1=0, Attribute2=2, ]
;
7[Attribute0=2, Attribute1=2, Attribute2=2, ]
;
8[Attribute0=1, Attribute1=1, Attribute2=0, ]
;
9[Attribute0=0, Attribute1=2, Attribute2=0, ]
;
0->4  [label="(0,4)"]
;
3->6  [label="(3,6)"]
;
6->9  [label="(6,9)"]
;
9->1  [label="(9,1)"]
;
9->1  [label="(9,1)"]
;
}

digraph G {
0[Attribute0=1, Attribute1=0, Attribute2=2, ]
;
1[Attribute0=0, Attribute1=0, Attribute2=2, ]
;
2[Attribute0=0, Attribute1=0, Attribute2=1, ]
;
3[Attribute0=2, Attribute1=2, Attribute2=2, ]
;
4[Attribute0=1, Attribute1=0, Attribute2=2, ]
;
5[Attribute0=1, Attribute1=1, Attribute2=2, ]
;
6[Attribute0=2, Attribute1=0, Attribute2=1, ]
;
7[Attribute0=1, Attribute1=2, Attribute2=1, ]
;
8[Attribute0=1, Attribute1=1, Attribute2=0, ]
;
9[Attribute0=0, Attribute1=1, Attribute2=2, ]
;
1->1  [label="(1,1)"]
;
4->8  [label="(4,8)"]
;
5->8  [label="(5,8)"]
;
5->1  [label="(5,1)"]
;
8->1  [label="(8,1)"]
;
}

This is the programme to write several graphs with n vertex, m edges and S attributes into file:

    int const S=3; // number of attributes for each graph
    int const N=10;// number of vertex
    int const m=5;//number of edges
    int const SIZE=3;// number of graphs
    struct Attributes
     {

      int Attribute0[S];

    };

    struct EdgeAttributes
    {
      string name;
      double miles;
      int speed_limit;
      int lanes;
      bool divided;
    };

    typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::directedS,
    Attributes, EdgeAttributes> Map;

    void outputgraph(Map&);
      Map * ggg= new Map[SIZE] ;
    int main()
    {
        Map map; // load the map
        bool inserted;

        Map::vertex_descriptor v ;

        for(int k=0; k<SIZE; k++)
        {
     for(int j=0; j<N; j++)
     {
         v= add_vertex(ggg[k]);
         for(int i=0; i<S; i++)
  { 

      ggg[k][v].Attribute0[i]=0 + (int)3 * rand() / (RAND_MAX + 1);


  }

     }
        }
         Map::edge_descriptor e;





         for(int k=0; k<SIZE; k++)
        {
         for(int i=0;i<m;i++)
      {
        tie(e,inserted) = add_edge(N * rand() / (RAND_MAX + 1), N * rand()     /     (RAND_MAX + 1), ggg[k]);
      }

         }


        std::cout << std::endl;

        outputgraph(map);
        int d;
        std::cin>>d;
    }

    struct my_node_writer {
        // my_node_writer() {}
        my_node_writer(Map& g_) : g (g_) {};
        template <class Vertex>
        void operator()(std::ostream& out, Vertex v) {
            out << "[";
             for(int i=0; i<S; i++)
                out <<"Attribute"<<i<<"="<<g[v].Attribute0[i]<<", ";
             out<<"]" << std::endl;
        };

        Map g;
    };

    struct my_edge_writer {
        my_edge_writer(Map& g_) : g (g_) {};
        template <class Edge>
        void operator()(std::ostream& out, Edge e) {
                // just an example, showing that local options override     global

                out << " [label=\"" << e  << "\"]" << std::endl;
        };
        Map g;
    };

    struct my_graph_writer {
        void operator()(std::ostream& out) const {

        }
    } myGraphWrite;

    void outputgraph(Map& map){
        std::ofstream gout;
        gout.open("graphname.dot");
        for(int i=0; i<S; i++)
        {
            write_graphviz(gout,ggg[i],my_node_writer(ggg[i]),my_edge_writer(ggg[i]),myGraphWrite);
        }
        // std::cout() << "done writing graph" << std::endl;
        gout.close();
    }
david
  • 31
  • 3
  • It might help to give more information about what specifically isn't working with your program that you are trying. The question isn't exactly clear to me. – pbible Apr 29 '16 at 05:06

0 Answers0