template <classverType, classedgeType> void Graph<verType, edgeType>::DFS()const { seqStack<int> s; edgeNode<edgeType> *p; bool *visited; int i, start;
visited = newbool[verts]; if (!visited) throwillegalSize(); for (i=0; i<verts; i++) { visited[i]=false; }
for (i=0; i<verts; i++){ if (visited[i]) continue; s.push(i); while (!s.isEmpty()) { start = s.top(); s.pop(); if (visited[start]) continue; cout<<verList[start].data<<'\t’; visited[start] = true; p = verList[start].adj; while (p) { if (!visited[p->dest]) s.push(p->dest); p = p->link; } } cout<<'\n’; } }
template <classverType, classedgeType> void Graph<verType, edgeType>::BFS()const{ seqQueue<int> q; edgeNode<edgeType> *p; bool *visited; int i, start;
visited = newbool[verts]; if (!visited) throwillegalSize(); for (i=0; i<verts; i++) visited[i]=false;
for (i=0; i<verts; i++){ if (visited[i]) continue; q.enQueue(i); while (!q.isEmpty()) { start = q.front(); q.deQueue(); if (visited[start]) continue; cout<<verList[start].data<<'\t’; visited[start] = true; p = verList[start].adj; while (p) { if (!visited[p->dest]) q.enQueue(p->dest); p = p->link; } } cout<<'\n’; } }
template <classverType, classedgeType> bool Graph<verType, edgeType>::connected()const { seqQueue<int> q; edgeNode<edgeType> *p; bool *visited; int i, start, count=0; //count为计数器
visited = newbool[verts]; if (!visited) throwillegalSize(); for (i=0; i<verts; i++) visited[i]=false;
for (i=0; i<verts; i++) { if (visited[i]) continue; q.enQueue(i); count++; while (!q.isEmpty()){ start = q.front(); q.deQueue(); if (visited[start]) continue; cout<<verList[start].data<<'\t’; visited[start] = true; p = verList[start].adj; while (p) { if (!visited[p->dest]) q.enQueue(p->dest); p = p->link; } } cout<<'\n’; } if (count==1) returntrue; returnfalse; }
template <classedgeType> structDijkstraNode{ int source; //当前最短路径上前一顶点 int dist; //当前最短路径距离 bool selected; //顶点是否已经在S中的标志 };
template <classverType, classedgeType> void Graph<verType, edgeType>::Dijkstra (verType start) const{ DijkstraNode<edgeType> *DList; int i, j, startInt; int cnt; //记录集合U中顶点的个数 int min; //选出的当前离集合最短的顶点 int dist;
//查找起始点下标 for (i=0; i<verts; i++) if (verList[i] == start) break; if (i==verts) return;
//创建空间并初始化DList[i]数组 startInt = i; DList = new DijkstraNode<edgeType> [verts];
for (i = 0; i < verts; i++){ DList[i].source = -1; DList[i].dist = noEdge; DList[i].selected = false; } // 从下标为startInt的点开始 min = startInt; cnt = 1; DList[startInt].source = startInt; DList[startInt].dist = 0; DList[startInt].selected = true;
while (cnt < verts){ // 根据min顶点发出的边,判断是否修正相邻顶点的最短距离 for (j = 0; j < verts; j++){ if (edgeMatrix[min][j] == 0) continue; // 对角线元素 if (DList[j].selected) continue; // 已经加入集合S if (edgeMatrix[min][j] == noEdge) continue; // 无边 if (DList[min].dist + edgeMatrix[min][j] < DList[j].dist){ DList[j].dist = DList[min].dist + edgeMatrix[min][j]; DList[j].source = min; } } min = -1; dist = noEdge; for (i = 0; i < verts; i++){ if (DList[i].selected) continue; if (DList[i].dist < dist){ min = i; dist = DList[i].dist; } } //此时min一定为某个顶点的下标,如果仍然为-1表示该无相图不连通 //将顶点min加入集合S cnt++; DList[min].selected = true; } // 如果图用邻接矩阵来存储,可以看出时间复杂度为O(n^2)。 }