图的应用,dfs,bfs

#include <iostream>
#include <stdlib.h>
#include "stdio.h"
using namespace std;
const int maxweight=1000;
const int maxSize=50;
class SeqQueue {
protected:
     int rear, front;
     int*elements;
     int maxSize;
public:
    SeqQueue(int sz = 10);
    ~SeqQueue() { delete[ ] elements; }
     int EnQueue(int x);
     int DeQueue(int& x);
     int getFront(int& x);
     int IsFull() const
         { return ((rear+1)% maxSize == front); }
     void makeEmpty() { front = rear = 0; }
     int IsEmpty() const { return front == rear; }
     int getSize() const
         { return (rear-front+maxSize) % maxSize; }
};

SeqQueue::SeqQueue(int sz)  {
      front=0; rear=0; maxSize=sz;
      elements = new int[maxSize];
};
int SeqQueue::EnQueue(int  x) {
     elements[rear] = x;
     rear = (rear+1)%maxSize;
     return 1;
};

int SeqQueue::DeQueue(int & x) {
     if (IsEmpty()) return 0;
     x = elements[front];
     front = (front+1)%maxSize;
     return 1;
};

class graph{
private:
    char *vlist;    //顶点表
    int **edge;     //邻接矩阵
    int numv;     //顶点数
    int maxv;     //最大顶点数
    int nume;    //边数
    int getvertex(char ch);  //找出顶点在vlist中的下标
    void dfs1(graph &g,int loc,bool visited[]);
public:
    graph(int sz=100);
    ~graph()  {delete []vlist;delete []edge;}
    char getvalue(int i);  //取顶点i的值
    int getweight(int v1,int v2);//取边(v1,v2)的权值
    int getfirstNeighbor(int v);  //取顶点v的第一个邻接顶点
    int getnextNeighbor(int v,int w); //取v的邻接顶点w的下一邻接顶点
    bool insertv(const char v);
    bool inserte(int v1,int v2,int cost);
    bool removev(int v);
    bool removee(int v1,int v2);
    int numberofv();
    friend istream &operator>>(istream &in,graph &g);
    friend ostream &operator<<(istream &out,graph &g);
    void dfs(graph &g,char v);
    void bfs(graph &g,char &ch);
};
int graph::numberofv(){
    return numv;
}
graph::graph(int sz)  {
    maxv=sz;
    numv=0;
    nume=0;
    int i,j;
    vlist=new char[maxv];  //创建顶点表
    edge=new int*[maxv];   //创建邻接矩阵
    for(i=0;i<maxv;i++)
        edge[i]=new int[maxv];
    for(i=0;i<maxv;i++){
        for(j=0;j<maxv;j++) {
            edge[i][j]=(i==j)?0:maxweight;
        }
    }
}

int graph::getvertex(char ch) {
    for(int i=0;i<numv;i++) {
        if(vlist[i]==ch) return i;
    }
    return -1;
}

char graph::getvalue(int i) {
    if(i>=0&&i<numv)  return vlist[i];
    else {cout<<"error"<<endl; exit(1);
    }
}

int graph::getweight(int v1,int v2) {
    return (v1>-1&&v2>-1)?edge[v1][v2]:0;
}

int graph::getfirstNeighbor(int v) {
    if(v>-1) {
        for(int col=0;col<numv;col++) {
            if(edge[v][col]>0&&edge[v][col]<maxweight)
                return col;
        }
    }
    return -1;
}

int graph::getnextNeighbor(int v,int w){
    if(v>-1&&w>-1){
        for(int col=w+1;col<numv;col++)
            if(edge[v][col]>0&&edge[v][col]<maxweight)
               return col;
    }
     return -1;
}

bool graph::insertv(const char v){
    if(numv==maxv) return false;
    vlist[numv++]=v;
    return true;
}

bool graph::inserte(int v1,int v2,int cost) {
    if(v1<0||v1>=numv||v2<0||v2>=numv) return false;
    edge[v1][v2]=cost;
    edge[v2][v1]=cost;
    nume++;
    return true;
}

bool graph::removev(int v){
    if(v<0||v>=numv) return false;
    if(numv==1) return false;     //一个节点不删除
    int i;
    vlist[v]=vlist[numv-1];
    for(i=0;i<numv;i++){
        if(edge[i][v]>0&&edge[i][v]<maxweight)
            nume--;
    }
    for(i=0;i<numv;i++){
        edge[i][v]=edge[i][numv-1];
    }
    numv--;
    for(i=0;i<numv;i++){
        edge[v][i]=edge[v][numv];
    }
    return true;
}

bool graph::removee(int v1,int v2){
    if(v1>-1&&v2>-1&&v1<numv&&v2<numv&&edge[v1][v2]>0&&edge[v1][v2]<maxweight){
        edge[v1][v2]=maxweight;
        nume--;
        return true;
    }
    return false;
}

istream&operator>>(istream &in,graph &g){
    int i,j,k,m,n;
    char e1,e2;
    int weight;
    in>>n;
    for(i=0;i<n;i++) {  //输入顶点
        in>>e1;
        g.insertv(e1);
    }
    i=0;
    in>>m;
    while(i<m)  {
        in>>j>>k>>weight;
        //j=g.getvertex(e1);
       // k=g.getvertex(e2);
        if(j==-1||k==-1)
            cout<<"边两边信息有误,重新输入"<<endl;
        else{
            g.inserte(j,k,weight);
            i++;
        }
    }
    return in;
}

ostream &operator<<(ostream &out,graph &g) {
    int n=g.numberofv();
    int w;
    char e1,e2;
    cout<<"edges are:"<<endl;
    for(int i=0;i<n;i++)  {
        for(int j=i+1;j<n;j++) {
            w=g.getweight(i,j);
            if(w>0&&w<maxweight){
               e1=g.getvalue(i);
               e2=g.getvalue(j);
               out<<e1<<"-"<<e2<<":"<<w<<endl;
            }
        }
    }
return out;
}

void graph::dfs(graph &g,char v) {
       cout<<"DFS:";
    int n=g.numv;
    bool *visited=new bool[n];
    for(int i=0;i<n;i++)  visited[i]=false;
    int loc=g.getvertex(v);
    dfs1(g,loc,visited);
    delete []visited;
}

void graph::dfs1(graph &g,int loc,bool visited[])
{
    cout<<g.getvalue(loc)<<" ";
    visited[loc]=true;
    int w=g.getfirstNeighbor(loc);
    while(w!=-1){
        if(visited[w]==false)
            dfs1(g,w,visited);
        w=g.getnextNeighbor(loc,w);
    }
}
void graph::bfs(graph&G,char &v){
    cout<<"BFS:";
  int i,w,n=G.numberofv();
  bool *visited=new bool[n];
  for(i=0;i<n;i++)visited[i]=false;
  int loc=G.getvertex(v);
  cout<<G.getvalue(loc)<<" ";
  visited[loc]=true;
  SeqQueue Q;
  Q.EnQueue(loc);
  while(!Q.IsEmpty()){
    Q.DeQueue(loc);
    w=G.getfirstNeighbor(loc);
    while(w!=-1){
        if(!visited[w]){
            cout<<G.getvalue(w)<<" ";
            visited[w]=true;
            Q.EnQueue(w);
}
        w=G.getnextNeighbor(loc,w);
    }

  }
  cout<<endl;
  delete []visited;
}
int main()
{
    graph ni;
    char ch='a';
    cin>>ni;
    ni.dfs(ni,ch);
    cout<<endl;
    ni.bfs(ni,ch);
    cout<<ni;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/jthello123/article/details/80298078