建立图(邻接矩阵、邻近表任选其一)的存储结构、实现图的深度优先遍历和广度优先遍历。
例如:(这是个无向加权图)
///邻接表实现深搜广搜
#include <iostream>
#include <queue>
using namespace std;
int dis[100][100];
bool vis[100];
char st[100];
int n,m;
void create()///创建邻接矩阵
{
int a1,a2,a;
cin>>m;
while(m--){
cin>>a1>>a2>>a;
dis[a1][a2]=a;
dis[a2][a1]=a;
}
}
void DFS(int v)///递归求解深搜
{
vis[v]=true;
cout<<st[v]<<" ";
for(int i=0; i<n; i++)
{
if(dis[v][i]!=0&&vis[i]==false)
{
DFS(i);
}
}
}
void BFS()
{
for(int i=0;i<n;i++)
{
vis[i]=false;
}
queue<int> Q;
for(int i=0; i<n; i++)///广搜
{
if(vis[i]==false)
{
Q.push(i);
vis[i]=true;
while(!Q.empty())
{
int t=Q.front();
Q.pop();
cout<<st[t]<<" ";
for(int j=0; j<n; j++)
{
if(dis[t][j]!=0&&vis[j]==false)
{
Q.push(j);
vis[j]=true;
}
}
}
}
}
}
void show()
{
cout<<"edges are:"<<endl;;
for(int i=0;i<n-1;i++)
{
for(int j=i+1;j<n;j++)
{
if(dis[i][j]!=0)cout<<st[i]<<"-"<<st[j]<<":"<<dis[i][j]<<endl;
}
}
}
int main()
{
cin>>n;
for(int i=0;i<n;i++)
cin>>st[i];
create();
vis[0]=true;
cout<<"DFS:";
DFS(0);
cout<<endl;
cout<<"BFS:";
BFS();
cout<<endl;
show();
return 0;
}
///邻接表实现图
#include <iostream>
#include <stdlib.h>
using namespace std;
const int DefaultVertices=100;
const int maxWeight=1000;
typedef int E;
typedef char T;
class Graphmtx{
private:
T *VerticesList;
E **Edge;
int numVertices;
int maxVertices;
int numEdge;
public:
Graphmtx(int sz=DefaultVertices);
~Graphmtx(){
delete []VerticesList;
delete []Edge;
}
T getValue(int i){
if(i>=0&&i<numVertices){
return VerticesList[i];
}
else{
cout<<"error"<<endl;
exit(1);
}
}
int getNumVertices(){
return numVertices;
}
int getVertexPos(T vertex){
for(int i=0;i<numVertices;i++){
if(VerticesList[i]==vertex)
return i;
}
return -1;
}
E getWeight(int v1,int v2){
return (v1>-1&&v2>-1)?Edge[v1][v2]:0;
}
int getFirstNeighbor(int v);
int getNextNeighbor(int v,int w);
bool insertVertex(const T vertex);
bool removeVertex(int v);
bool insertEdge(int v1,int v2,E cost);
bool removeEdge(int v1,int v2);
void show();
};
Graphmtx::Graphmtx(int sz){
maxVertices=sz;
numVertices=0;
numEdge=0;
int i,j;
VerticesList=new T[maxVertices];
Edge=new E*[maxVertices];
for(i=0;i<maxVertices;i++){
Edge[i]=new E[maxVertices];
}
for(i=0;i<maxVertices;i++){
for(j=0;j<maxVertices;j++){
Edge[i][j]=((i==j)?0:maxWeight);
}
}
}
int Graphmtx::getFirstNeighbor(int v){
if(v>-1){
for(int col=0;col<numVertices;col++){
if(Edge[v][col]&&Edge[v][col]<maxWeight)
return col;
}
}
return -1;
}
int Graphmtx::getNextNeighbor(int v,int w){
if(v>-1&&w>-1){
for(int col=w+1;col<numVertices;col++)
if(Edge[v][col]&&Edge[v][col]<maxWeight)
return col;
}
return -1;
}
bool Graphmtx::insertVertex(const T vertex){
if(numVertices==maxVertices)
return false;
VerticesList[numVertices++]=vertex;
return true;
}
bool Graphmtx::removeVertex(int v){
if(v<0||v>=numVertices){
cout<<"error"<<endl;
return false;
}
if(numVertices==1)
return false;
int i,j;
VerticesList[v]=VerticesList[numVertices-1];
for(i=0;i<numVertices;i++){
Edge[i][v]=Edge[i][numVertices-1];
}
numVertices--;
for(j=0;i<numVertices;j++)
Edge[v][i]=Edge[numVertices][j];
return true;
}
bool Graphmtx::insertEdge(int v1,int v2,E cost){
if(v1<0||v1>=numVertices||v2<0||v2>=numVertices){
cout<<"error"<<endl;
return false;
}
Edge[v1][v2]=cost;
Edge[v2][v1]=cost;
return true;
}
bool Graphmtx::removeEdge(int v1,int v2){
if(v1<0||v1>=numVertices||v2<0||v2>=numVertices){
cout<<"error"<<endl;
return false;
}
Edge[v1][v2]=maxWeight;
Edge[v2][v1]=maxWeight;
numEdge--;
return true;
}
void Graphmtx::show(){
for(int i=0;i<numVertices;i++){
for(int j=i;j<numVertices;j++){
if(Edge[i][j]>0&&Edge[i][j]<maxWeight)
cout<<VerticesList[i]<<"-"<<VerticesList[j]<<":"<<Edge[i][j]<<endl;
}
}
cout<<endl;
}
class SeqQueue {
protected:
int rear, fron;
int*elements;
int maxSize;
public:
SeqQueue(int sz = 20){
fron=0; rear=0; maxSize=sz;
elements = new int[maxSize];
}
int push(int x){
elements[rear] = x;
rear = (rear+1) % maxSize;
return 1;
}
int pop(int& x){
if (IsEmpty()) return 0;
x = elements[fron];
fron = (fron+1) % maxSize;
return 1;
}
int IsEmpty() const { return fron == rear;}
};
void DFS (Graphmtx& G, int v, bool *visited) {
cout << G.getValue(v) << ' ';
visited[v] = true;
int w = G.getFirstNeighbor (v);
while (w != -1) {
if ( !visited[w] ) DFS(G, w, visited);
w = G.getNextNeighbor (v, w);
}
}
void DFS (Graphmtx& G, const T& v) {
int i, loc, n = G.getNumVertices();
bool *visited = new bool[n];
for (i = 0; i < n; i++) visited [i] = false;
loc = G.getVertexPos(v);
DFS(G,loc,visited);
delete [] visited;
}
void BFS (Graphmtx& G, const T& v) {
int i, w, n = G.getNumVertices();
bool *visited = new bool[n];
for (i = 0; i < n; i++) visited[i] = false;
int loc = G.getVertexPos (v);
cout<<G.getValue(loc)<<' ';
visited[loc]=true;
SeqQueue Q;Q.push (loc);
while (!Q.IsEmpty() ) {
Q.pop (loc);
w = G.getFirstNeighbor (loc);
while (w != -1) {
if (visited[w]==false){
cout<<G.getValue(w)<<' ';
visited[w]=true;
Q.push (w);
}
w = G.getNextNeighbor (loc, w);
}
}
delete [] visited;
}
int main(){
Graphmtx g;
int n1,n2;
cin>>n1;
char ch;
cin>>ch;
g.insertVertex(ch);
for(int i=1;i<n1;i++){
char ch;
cin>>ch;
g.insertVertex(ch);
}
cin>>n2;
for(int i=0;i<n2;i++){
int v1,v2,cost;
cin>>v1>>v2>>cost;
g.insertEdge(v1,v2,cost);
}
cout<<"DFS:";
DFS(g,ch);
cout<<endl;
cout<<"BFS:";
BFS(g,ch);cout<<endl;
cout<<"edges are:"<<endl;
g.show();
return 0;
}
输入 | Result |
---|---|
7 a b c d e f g 12 0 1 1 0 2 2 0 3 3 0 4 4 0 6 5 1 3 1 1 6 2 2 4 1 3 4 1 3 5 2 4 5 3 5 6 1 | DFS:a b d e c f g BFS:a b c d e g f edges are: a-b:1 a-c:2 a-d:3 a-e:4 a-g:5 b-d:1 b-g:2 c-e:1 d-e:1 d-f:2 e-f:3 f-g:1 |