1 存储结构
2 遍历算法
DFS与BFS
BFS算法一:
void BFS(graphNode *node) {
queue<graphNode *>que;
que.push(node);
while(!que.empty()) {
cur = que.front();
que.pop();
if(!cur.isVisited()) {
cur.setVisited();
for(i = 0; i < cur->neighbors.size(); i++) {
que.push(cur->neighbors[i]);
}
}
}
}
BFS算法二:
void BFS(graphNode *node) {
queue<graphNode *>que;
que.push(node);
while(!que.empty()) {
cur = que.front();
que.pop();
cur.setVisited();
for(i = 0; i < cur->neighbors.size(); i++) {
if(!cur->neighbors[i]) {
que.push(cur->neighbors[i]);
}
}
}
}
DFS算法:
void DFS(graphNode *node) {
stack<graphNode *>st;
st.push(node); //output(node);
while(!st.empty()) {
cur = st.top();
for(x in cur->neighbors) {
if(!x.isVisited()) break;
}
if(!x.isVisited()) {
st.push(x);//outpust(x);
} else {
st.pop();
}
}
}