数据结构-图-组建图(Java语言)

详细的代码可见github:

https://github.com/AbitGo/myClassWork/tree/master/workspace_ds

 图结点代码:

package com.company.ch6;

public class ArcNode {
    //该弧所指向的顶点位置
    public int adjVex;
    //边或弧的权值
    public int value;
    //指向下一个弧
    public ArcNode nextArc;

    public ArcNode() {
        this(-1, 0, null);
    }

    public ArcNode(int adjVex, int value, ArcNode nextArc) {
        this.adjVex = adjVex;
        this.value = value;
        this.nextArc = nextArc;
    }
    public ArcNode(int adjVex, int value) {
        this(adjVex, value, null);
    }

    public ArcNode(int adjVex) {
        this.adjVex = adjVex;
    }
}

图类型文件:

package com.company.ch6;

public enum GraphKind {
    //图与网络之间的区别为时候有权值
    UDG,//无向图
    DG,//有向图

    UDN,//无向网
    DN//有向网
}

 

图需要实现的方法:

package com.company.ch6;

public interface IGraph {
    void createGraph(Object[] param1,String[] param2);
    int getVexNum();
    int getArcNum();
    Object getVex(int x) throws Exception;
    int locateVex(Object x);
    int firstAdjVex(int x) throws Exception;
    int nextAdjVex(int x,int w) throws Exception;
}

详细构建代码:

package com.company.ch6;

public class ALGraph implements IGraph {
    private GraphKind graphKind;//图的种类标识
    private int vexNum, arcNum;//图当前定点数和边数
    private VNode[] vexs;//顶点

    public ALGraph(GraphKind graphKind, int vexNum, int arcNum, VNode[] vexs) {
        this.graphKind = graphKind;
        this.vexNum = vexNum;
        this.arcNum = arcNum;
        this.vexs = vexs;
    }

    public ALGraph() {
        this(null, 0, 0, null);
    }

    @Override
    public void createGraph(Object[] param1, String[] param2) {
        //创建图;
        GraphKind graphKind = GraphKind.valueOf((String) param1[0]);

        switch (graphKind) {
            case DG: {
                this.graphKind = GraphKind.DG;
                createDG(param1, param2);
                break;
            }
            case DN: {
                createDN(param1, param2);
                this.graphKind = GraphKind.DN;
                break;
            }
            case UDG: {
                createUDG(param1, param2);
                this.graphKind = GraphKind.UDG;
                break;
            }
            case UDN: {
                createUDN(param1, param2);
                this.graphKind = GraphKind.UDN;
                break;
            }

        }
        return;
    }

    public void createUDG(Object[] param1, String[] param2) {
        //创建无向图
        System.out.println("创建无向图");
        //顶点数
        vexNum = (int) param1[1];
        //图的边数
        arcNum = (int) param1[2];

        vexs = new VNode[vexNum];

        for (int v = 0; v < ((String) param1[3]).length(); v++) {
            //分别赋值ABCDE
            vexs[v] = new VNode(((String) param1[3]).charAt(v));
        }
        for (int i = 0; i < arcNum; i++) {
            //通过拆解字符串AB这类字符串,以获得顶点
            int v = locateVex(param2[i].charAt(0));
            int u = locateVex(param2[i].charAt(1));
            //图无权值
            addArc(v, u, 1);
            addArc(u, v, 1);
        }
    }

    public void createDG(Object[] param1, String[] param2) {
        //创建有向图
        System.out.println("创建无向图");
        //顶点数
        vexNum = (int) param1[1];
        //图的边数
        arcNum = (int) param1[2];

        vexs = new VNode[vexNum];

        for (int v = 0; v < ((String) param1[3]).length(); v++) {
            //分别赋值ABCDE
            vexs[v] = new VNode(((String) param1[3]).charAt(v));
        }
        for (int i = 0; i < arcNum; i++) {
            //通过拆解字符串AB这类字符串,以获得顶点
            int v = locateVex(param2[i].charAt(0));
            int u = locateVex(param2[i].charAt(1));
            //图无权值
            addArc(v, u, 1);
        }
    }

    public void createUDN(Object[] param1, String[] param2) {
        //创建无向网
        System.out.println("创建无向图");
        //顶点数
        vexNum = (int) param1[1];
        //图的边数
        arcNum = (int) param1[2];

        vexs = new VNode[vexNum];

        for (int v = 0; v < ((String) param1[3]).length(); v++) {
            //分别赋值ABCDE
            vexs[v] = new VNode(((String) param1[3]).charAt(v));
        }
        for (int i = 0; i < arcNum; i++) {
            //通过拆解字符串AB这类字符串,以获得顶点
            int v = locateVex(param2[i].charAt(0));
            int u = locateVex(param2[i].charAt(1));
            int value = (int) param2[i].charAt(2);
            addArc(v, u, value);
            addArc(u, v, value);
        }
    }

    public void createDN(Object[] param1, String[] param2) {
        //创建有向网
        System.out.println("创建无向图");
        //顶点数
        vexNum = (int) param1[1];
        //图的边数
        arcNum = (int) param1[2];

        vexs = new VNode[vexNum];

        for (int v = 0; v < ((String) param1[3]).length(); v++) {
            //分别赋值ABCDE
            vexs[v] = new VNode(((String) param1[3]).charAt(v));
        }
        for (int i = 0; i < arcNum; i++) {
            //通过拆解字符串AB这类字符串,以获得顶点
            int v = locateVex(param2[i].charAt(0));
            int u = locateVex(param2[i].charAt(1));
            int value = (int) param2[i].charAt(2);
            addArc(v, u, value);
        }
    }

    @Override
    public int getVexNum() {
        return this.vexNum;
    }

    @Override
    public int getArcNum() {
        return this.arcNum;
    }

    @Override
    public Object getVex(int x) throws Exception {
        if (x < 0 || x > vexNum) {
            throw new Exception("顶点不存在");
        }
        return vexs[x].getData();
    }

    @Override
    public int locateVex(Object x) {
        for (int v = 0; v < vexNum; v++) {
            if (vexs[v].data.equals(v)) {
                return v;
            }
        }
        return -1;
    }

    public GraphKind getGraphKind() {
        return graphKind;
    }

    public void setGraphKind(GraphKind graphKind) {
        this.graphKind = graphKind;
    }

    @Override
    public int firstAdjVex(int x) throws Exception {
        if (x < 0 || x > vexNum) {
            throw new Exception("顶点不存在");
        }
        VNode vex = vexs[x];
        //指向第一条依附于该顶点的弧不为空时
        if (vex.firsArc != null) {
            //返回该弧指向的顶点位置
            return vex.firsArc.adjVex;
        } else
            return -1;
    }

    @Override
    public int nextAdjVex(int x, int w) throws Exception {
        if (x < 0 || x > vexNum) {
            throw new Exception("顶点不存在");
        }
        VNode vex = vexs[x];
        ArcNode arcw = null;

        //这段代码是为了寻找到w所在位置,并将arcw被赋值于arc
        for (ArcNode arc = vex.firsArc; arc != null; arc = arc.nextArc) {
            if (arc.adjVex == w) {
                arcw = arc;
                break;
            }
        }
        //当且仅当arcw不为空并且下一个节点也不为空时
        if (arcw != null && arcw.nextArc != null) {
            return arcw.nextArc.adjVex;
        } else
            return -1;
    }

    public void addArc(int v, int u, int value) {
        ArcNode arc = new ArcNode(u, value);
        arc.nextArc = vexs[v].firsArc;
        vexs[v].firsArc = arc;
    }


}

测试类:

package com.company.ch6;

public class GraphTest {
    public static void main(String[] args) throws Exception {
        Object[] param1 = {"UDG",5,5,"ABCDE"};
        String[] param2 = {"AB","BC","AD","AE","CE"};
        MGraph mGraph = new MGraph();
        mGraph.createGraph(param1,param2);
        GraphFS.BFSTraverse(mGraph);
    }
}

测试结果:

"C:\Program Files\Java\jdk1.8.0_101\bin\java.exe" 
创建无向图
A B D E C 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值