我需要优化这个功能,但即时通讯难倒的想法,以改善它的速度......优化算法
bool generateShortestpathLink (const string& start, const string& finish, const book& db) {
vector <lib> bks;
vector <string> authors;
set <lib> storeBKS;
set <string> storeAuthor;
queue <pathLink> q;
pathLink p(start);
q.push(p);
while (!q.empty()) {
p = q.front();
if (p.getLength() > 6) return false;
db.getTitles(p.getLastPlayer(), bks);
for (int x = 0; x < (int) bks.size(); x++) {
const film& newBook = bks[x];
if (storeBKS.find(newBook) != storeBKS.end()) continue;
db.getAuthors(newBook, authors);
storeBKS.insert(newBook);
for (int i = 0; i < (int) authors.size(); i++) {
if (storeAuthor.find(authors[i]) != storeAuthor.end()) continue;
pathLink newpathLink(p);
newpathLink.addConnection(newBook, authors[i]);
if (authors[i] == finish) return true;
storeAuthor.insert(authors[i]);
q.push(newpathLink);
}
}
q.pop();
}
return false;
}
它的假设是对BFS,一个算法中它创建用于连接不同的路径作者的书名。 getTitles()
和getAuthors
都是无法更改的二进制搜索功能。任何人都可以帮助我吗?
我不能预先分配任何内存给它,因为我每次都会从头创建一个不同大小的路径(取决于每个lvl有多少个连接器分支) – SNpn
然后假设合理的最大路径大小,预先分配内存,如果事实证明你需要更大的路径,请扩展缓冲区。 –