I am trying to create a maximum spanning tree in C++ but am having trouble preventing cycles. The code I have works alright for some cases, but for the majority of cases there is a cycle. I am using an adjacency matrix to find the edges.
double maximumST( vector< vector<double> > adjacencyMatrix ) {
    const int size = adjacencyMatrix.size();
    vector <double> edges;
    int edgeCount = 0;
    double value = 0;
    std::vector<std::vector<int>> matrix(size, std::vector<int>(size));
    for (int i = 0; i < size; i++) {
        for (int j = i; j < size; j++) {
            if (adjacencyMatrix[i][j] != 0) {
                edges.push_back(adjacencyMatrix[i][j]);
                matrix[i][j] = adjacencyMatrix[i][j];
                edgeCount++;
           }
       }
    }
    sort(edges.begin(), edges.end(), std::greater<int>());
    for (int i = 0; i < (size - 1); i++) {
        value += edges[i];
    }
    return value;
}
One I've tried to find a cycle was by creating a new adjacency matrix for the edges and checking that before adding a new edge, but that did not perform as expected. I also tried to build a 3D matrix, but I could not get that to work either.
What's a new approach I should try to prevent cycles?
 
    