lundi 5 août 2019

GB's of memory being used when running a program

Basically I have a double for-loop, and in this double for-loop I loop through a vector of Graphs. Pretend this Graph is already populated with about 500 words. Each word is adj if WordOne is one letter away from WordTwo without adding letters. EX: "hit", "hat". These are adj.

So this double for loop loops through this Graph twice once to change the value of the Graph at one position and the second graph so that I am able to compare each word with each word. The problem is when I push back Graph B to Graph A it uses GB's of memory, and also runs indefinetly.

The idea of this double for loop is to populate each nodes adj list of graphs.

class Transform{
private:
    string word;
    vector<Transform> wordgraph;
public:
    bool discovered;
    void setNode(string _word){word = _word;}
    void setAdjNode(Transform _adjecent){wordgraph.push_back(_adjecent);} //taking a long time to run right here
    string getNode(){return word;}
    vector<Transform> getAdjNode(){return wordgraph;}
};



vector<Transform> Graph;

for(int i = 0; i < Graph.size(); i++){
        for(int j = 0; j < Graph.size(); j++){
            if(oneLetterApart(Graph[i].getNode(), Graph[j].getNode())){
                Graph[i].setAdjNode(Graph[j]);
                //Graph[j].setAdjNode(Graph[i]);
            }
        }
    }




bool oneLetterApart(string first, string second){
    //basis case
    if(first.size() != second.size()){
        return false;
    }

    int numOffCount = 0;
    for(int i = 0; i < first.size(); i++){
        if(first[i] != second[i]){
            numOffCount++;
        }
        if(numOffCount == 2){
            return false;
        }
    }

    if(numOffCount == 1){
        return true;
    } else {
        return false;
    }
}

This program will run indefinitely. Not sure why it is not in an infinite for loop.

Aucun commentaire:

Enregistrer un commentaire