练习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 
#include 
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-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-1){
        for(int col=0;col-1&&w>-1){
        for(int col=w+1;col=numVertices){
        cout<<"error"<=numVertices||v2<0||v2>=numVertices){
        cout<<"error"<=numVertices||v2<0||v2>=numVertices){
        cout<<"error"<0&&Edge[i][j]>n1;
    char ch;
    cin>>ch;
    g.insertVertex(ch);
    for(int i=1;i>ch;
        g.insertVertex(ch);
    }
    cin>>n2;
    for(int i=0;i>v1>>v2>>cost;
        g.insertEdge(v1,v2,cost);
    }
    cout<<"DFS:";
    DFS(g,ch);
    cout<

 

你可能感兴趣的:(练习13——图)