從txt中讀取邊,判斷圖的連通性

溫柔且堅定的瓜瓜發表於2020-12-10

G = { V, E}
邊:連線兩個頂點
頂點和邊構成了圖,txt文件中儲存了邊的資訊。

從txt中讀取邊

typedef subgraph<adjacency_list<vecS, vecS, directedS, property<vertex_color_t, int>, property<edge_index_t, int>>> Graph;
std::ifstream ifs("edges.txt");
ifs >> std::noskipws;	//從檔案中讀取空格

boost::spirit::istream_iterator f(ifs), l;
//spirit 根據EBNF規則對檔案進行分析

std::vector<std::pair<int, int>> edges;
bool parse_ok = qi::phrase_parse(f, l, (qi::int_ >> qi::int_) % qi::eol, qi::blank, edges);	//從檔案中讀取邊

Graph g;

//將讀到的邊放入圖中
for(const auto edge : edges)
{
	add_edge(edge.first, edge.second, g);
}

計算連通子圖數目

std::vector<int> comp(num_vertices(g)); //將邊的頂點放入comp
int num = connected_components (g, comp.data()); //計算連通數目

全部程式碼

#include <fstream>
#include <sstream>
#include <iostream>
#include <boost/fusion/adapted/std_pair.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/graph/edge_list.hpp>
#include <boost/graph/subgraph.hpp>
#include <boost/graph/adjacency_list.hpp>

#include <boost/config.hpp>
#include <vector>
#include <boost/graph/connected_components.hpp>

using namespace std;
using namespace boost;

namespace qi = boost::spirit::qi;

// Underlying graph representation and implementation
//typedef adjacency_list_traits<vecS, vecS, directedS> Traits;

// Graph representation
typedef subgraph< adjacency_list<vecS, vecS, directedS,
    property<vertex_color_t, int>, property<edge_index_t, int> > > Graph;

// Iterating over vertices and edges
typedef graph_traits<Graph>::vertex_iterator vertex_iter;
typedef graph_traits<Graph>::edge_iterator edge_iter;

int main(void)
{
    std::ifstream ifs("edges0.txt");
    ifs >> std::noskipws;

    boost::spirit::istream_iterator f(ifs), l;

    std::vector<std::pair<int, int>> edges;
    bool parse_ok = qi::phrase_parse(f, l , (qi::int_ >> qi::int_) % qi::eol, qi::blank, edges);

    Graph g;

    for (const auto edge : edges )
    {
        add_edge(edge.first, edge.second, g);
    }
    

    std::vector<int> comp(num_vertices(g)); //將邊的頂點放入comp
    int num = connected_components (g, comp.data()); //計算連通數目
    std::cout << std::endl;
    std::vector < int >::iterator i;

    //將結果輸出到txt檔案
    std::ofstream connect("/data6/wcm/document/UAV1/connect.txt");

    connect << "Total number of components: " << num << std::endl;
    
    // for (i = comp.begin(); i != comp.end(); ++i)
    // std::cout << "Vertex " << i - comp.begin()
    //   << " is in component " << *i << std::endl;

      std::vector<Graph* > comps(num);

      for(size_t i=0;i<num;++i) {
          comps[i] = & g.create_subgraph(); //comps[i]為子圖
      }
      for(size_t i=0;i<num_vertices(g);++i) {
        //   cout<<"add vetex "<<i<<" to sub graph "<<comp[i]<<endl;
          add_vertex(i, *comps[comp[i]]);  //
      }

    pair<vertex_iter, vertex_iter> vip;

    connect << "Vertices in g  = [ ";
    vip = vertices(g);
    for(vertex_iter vi = vip.first; vi != vip.second; ++vi) {
        connect << *vi << " ";
    }
    connect << "]" << endl;
    for(size_t i=0;i<num;i++)
    {
        connect << "Vertices (local) in comps [ " << i << " ]' = [ " ;
        pair<vertex_iter, vertex_iter> lvip;
        lvip = vertices(*comps[i]);

        for(vertex_iter vi = lvip.first; vi != lvip.second; ++vi)
        {
                connect << (*comps[i]).local_to_global(*vi) << " ";
        }

        connect << "]" << endl;
    }

    return 0;
}

輸出txt

圖片: 有三個連通子圖,此圖不連通在這裡插入圖片描述

相關文章