练习13——图

建立图(邻接矩阵、邻近表任选其一)的存储结构、实现图的深度优先遍历和广度优先遍历。

测试用例:

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

代码:

#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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值