Implementation of Prim's algorithm in C++ Could anyone comment on what could be done better and if I made any mistakes?

#include &lt;iostream&gt;
#include &lt;vector&gt;
#include &lt;algorithm&gt;


using namespace std;

typedef pair&lt;pair&lt;int, int&gt;, int&gt; Edge; //a, b, length
// A---------B
//   length


vector&lt;Edge&gt; prims_algo(vector&lt;vector&lt;int&gt;&gt; graph, int number_of_nodes){
    vector&lt;int&gt; unvisited;
    vector&lt;int&gt; visited;
    vector&lt;Edge&gt; result;

    //mark first as visited and mark the rest as unvisited
    for (int i = 1; i &lt; number_of_nodes; i++)
        unvisited.push_back(i);
    visited.push_back(0);

    while (!unvisited.empty()) {

        vector&lt;Edge&gt; edges_with_lengths;
        //put all edges (with their lengths) from nodes that are in visited
        for(auto node : visited) {
            for (int sec_node = 0; sec_node &lt; number_of_nodes; sec_node++) {
                if (graph[node][sec_node] &gt; 0 &amp;&amp; find(unvisited.begin(), unvisited.end(), sec_node) != unvisited.end()) {
                    //add if there is connection and second node is not visited yet
                    Edge e = make_pair(make_pair(node, sec_node), graph[node][sec_node]);
                    edges_with_lengths.push_back(e);
                }
            }
        }

        //find the shortest edge
        pair&lt;pair&lt;int, int&gt;, int&gt; the_shortest;
        the_shortest = edges_with_lengths.front();
        for(auto i: edges_with_lengths){
            if(the_shortest.second &gt; i.second)
                the_shortest = i;
        }

        //add the shortest path to the result
        result.push_back(the_shortest);

        //remove a node that the shortest edge leads to
        unvisited.erase(remove(unvisited.begin(), unvisited.end(), the_shortest.first.second), unvisited.end());

        //mark this node as visited
        visited.push_back(the_shortest.first.second);
    };
    return result;
}


int main() {
    /*
       2    3
   (0)--(1)--(2)
    |   / \   |
   6| 8/   \5 |7
    | /     \ |
   (3)-------(4)
         9          */
    vector&lt;vector&lt;int&gt;&gt; graph =
                     {{{0, 2, 0, 6, 0},
                       {2, 0, 3, 8, 5},
                       {0, 3, 0, 0, 7},
                       {6, 8, 0, 0, 9},
                       {0, 5, 7, 9, 0},
    }};

    vector&lt;Edge&gt; result = prims_algo(graph, 5);
    for(auto i:result){
        cout&lt;&lt;"EDGE [ " &lt;&lt;i.first.first&lt;&lt;", "&lt;&lt;i.first.second&lt;&lt;"], length: "&lt;&lt;i.second&lt;&lt;endl;
    }

    return 0;
}
Code looks OK.

I would suggest you to do some class instead of Edge:

//typedef pair&lt;pair&lt;int, int&gt;, int&gt; Edge; //a, b, length
struct Edge{
   int a;
   int b;
   int length;
};


Also I suggest to have some predefined type for vector&lt;Edge&gt;

using EdgeContainer = vector&lt;Edge&gt;;


You have duplication near //find the shortest edge, you probably need Edge there

Here the_shortest is probably not initialized. Also you probably might use something from &lt;algorithm&gt; (but I will do it in same way as you).

    //find the shortest edge
    pair&lt;pair&lt;int, int&gt;, int&gt; the_shortest;
    the_shortest = edges_with_lengths.front();
    for(auto i: edges_with_lengths){
        if(the_shortest.second &gt; i.second)
            the_shortest = i;
    }


Another probably micro optimization would be to write ++i in loops:

//for (int i = 1; i &lt; number_of_nodes; i++)
for (int i = 1; i &lt; number_of_nodes; ++i){
   // ...
}


Also in C++11 main() does not need to return 0;.