Stack Exchange
Add missing nodes to adjacency list in boost graph
I have a txt file that looks something like this:
2 

0 8 40 
39 204 238 
71 75 77 

meaning that node 0 should be connected to node 2, node 1 should not be connected to anything (but still should be present in a graph), node 3 connected to nodes 0, 8, 40; etc. 
I have this piece of code:
typedef adjacency_list < vecS, vecS, undirectedS> Graph;
typedef typename boost::graph_traits<Graph>::vertex_iterator vertex_iterator;
typedef typename boost::graph_traits<Graph>::vertex_descriptor Vertex;

while(getline(infile, line)) {
        tokenizer<> tok(line);
        for(tokenizer<>::iterator beg=tok.begin(); beg!=tok.end();++beg){
            int v=lexical_cast<int>(*beg);
            if (i<v){
                tie(e,inserted)=add_edge(i, v, G);
                if (!inserted) {
                    std::cout << "Unable to insert edge\n";
                }
            }
        }
        i++;
    }

It reads a txt file line by line and adds edges to the graph. It works just fine, but if a node i doesn't have a neighbor it won't be present in resulting graph. Eventually, the number of edges will be correct, but the number of vertices will be fewer than in the file. 
How can I add isolated nodes to the graph? 
Nodes do not have any other properties except their numbers (that correspond to the line number in a txt file). 
EDIT 1:
I want to have some condition after for loop that checks if there were no insertion, then this node is single and I a new node. The problem that add_vertex doesn't accept a parameter for a node. 
while(getline(infile, line)) {
    tokenizer<> tok(line);
    int neighbors = 0;
    for(tokenizer<>::iterator beg=tok.begin(); beg!=tok.end();++beg){
        neighbors++;
        int v=lexical_cast<int>(*beg);
        if (i<v){
            tie(e,inserted)=add_edge(i, v, G);
            if (!inserted) {
                std::cout << "Unable to insert edge\n";
            }
        }
    }
    if no_new_edge {
        add_vertex(i, G);
    }
    i++;
}


a simple hack will be to add a self edge and delete it..

@PRP It's a little bit overwhelming...

Can u please tell me how u have made ur graph object...

How did you declare the graph type? In a vecS adjacency list, vertices virtually donot exist. You'd need a setS (with quite a bit more complicated algorithmic handling due vertex_index mapping).

typedef adjacency_list < vecS, vecS, undirectedS> Graph;

You declared the graph type with vecS storage containers. In this case vertices virtually do not exist (they're indices into a vector whose size signifies the number of vertices. All the intermediate vertices are "virtually" present). 
You'd need a setS, listS etc. (with quite a bit more complicated algorithmic handling due vertex index mapping).

Live On Coliru This little sample demonstrates how vecS does what you want out of the box:
Edge added: 0 -> 2
Number of edges: 1
Number of vertices: 3
Number of edges: 1
Number of vertices: 3
Edge added: 2 -> 8
Edge added: 2 -> 40
Number of edges: 3
Number of vertices: 41
Edge added: 3 -> 39
Edge added: 3 -> 204
Edge added: 3 -> 238
Number of edges: 6
Number of vertices: 239
Edge added: 4 -> 71
Edge added: 4 -> 75
Edge added: 4 -> 77
Number of edges: 9
Number of vertices: 239

You can easily find which nodes have no edges by doing something like:
Live On Coliru
for(auto vd : g.vertex_set())
    if (!g.out_edge_list(vd).empty())
        std::cout << "vertex " << vd << " has outgoing edges\n";

Output:
vertex 0 has outgoing edges
vertex 2 has outgoing edges
vertex 3 has outgoing edges
vertex 4 has outgoing edges
vertex 8 has outgoing edges
vertex 39 has outgoing edges
vertex 40 has outgoing edges
vertex 71 has outgoing edges
vertex 75 has outgoing edges
vertex 77 has outgoing edges
vertex 204 has outgoing edges
vertex 238 has outgoing edges

If you insist on explicit control over which vertices are created, with or without edges just change the Graph definition to your liking, e.g.:
Live On Coliru
#include <boost/graph/adjacency_list.hpp>
#include <sstream>
#include <iostream>

typedef boost::adjacency_list<boost::setS, boost::listS, boost::undirectedS, boost::property<boost::vertex_index_t, int> > Graph;
typedef typename boost::graph_traits<Graph>::vertex_iterator vertex_iterator;
typedef typename boost::graph_traits<Graph>::vertex_descriptor Vertex;

int main() {
    Graph g;
    std::map<int, Graph::vertex_descriptor> vertex_map;

    int linenum = 0;
    std::string line;
    while (getline(std::cin, line)) {
        std::istringstream iss(line);
        for (std::istream_iterator<int> f(iss), l; f != l; ++f) {
            int v = *f;
            if (linenum < v) {
                auto lvd = vertex_map[linenum];
                if (lvd == g.null_vertex()) lvd = vertex_map[linenum] = boost::add_vertex(linenum, g);

                auto vvd = vertex_map[v];
                if (vvd == g.null_vertex()) vvd = vertex_map[v] = boost::add_vertex(v, g);

                Graph::edge_descriptor e;
                bool inserted;
                boost::tie(e, inserted) = add_edge(lvd, vvd, g);

                if (!inserted) {
                    std::cout << "Unable to insert edge\n";
                }
            }
        }

        linenum++;
    }

    boost::property_map<Graph, boost::vertex_index_t>::const_type index_map(&g, boost::vertex_index);
    for(auto vd : g.vertex_set())
    {
        std::cout << "vertex " << boost::get(index_map, vd) << ": ";
        for(auto& e: g.out_edge_list(vd))
            std::cout << boost::get(index_map, e.get_target()) << " ";
        std::cout << "\n";
    }
}

which prints
vertex 0: 2 
vertex 2: 0 8 40 
vertex 8: 2 
vertex 40: 2 
vertex 3: 39 204 238 
vertex 39: 3 
vertex 204: 3 
vertex 238: 3 
vertex 4: 71 75 77 
vertex 71: 4 
vertex 75: 4 
vertex 77: 4 



Added samples of how you can use the current graph declaration, or modify it for more specifity

Can you clarify, please, what's happening on lines 20-21 in the last example? Do you insert every vertex once time only? Or when you insert edge it also "inserts" (overwrites the previous one) two vertices?

I insert them only once. This is why I look the number up in the vertex map and just use the vertex descriptor found there if it was already inserted

This can be useful :
If u come across a blank line do this :
add_vertex()
It is a function of the Boost Library details can be found here 
Also to specify the node number u have to set the properties.
Properties can be attached to the vertices or edges of an adjacency_list graph via the property interface
vertex_index_t is the property tag that u should use here.
Scroll Down on the same link and u will find it.

It inserts additional vertices, because it doesn't specify the node number.

From my code I don't have any properties on edges (and I don't want them to have). I insert an edge between two integers and I want to preserve. Additional properties will impose additional complexity to other parts of the code.

u are confused with edge properties and vertex properties
