1

我将几个具有 n 个顶点、m 个边和 S 属性的图写入文件。现在我需要通过 Boost 图形库从 Graphviz DOT 文件中读取这些未知数量的图形。如何读取这些未知数量的顶点、边缘和属性数量未知的图?这是保存图表的文件。提前谢谢你!

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)"]
;
}

这是将具有 n 个顶点、m 个边和 S 属性的多个图写入文件的程序:

    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();
    }
4

0 回答 0