以邻接矩阵(数组表示)的方法来创建带权值的有向图(有向网)

1.数据结构

private String[] vertex; //顶点集合
private int[][] matrix; //边或弧,邻接矩阵

2.附上代码

package com.test.frame.fighting.graph;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

/**
 * MatrixDN class
 *
 * @author guanhuifang
 * @date 2018/1/19 下午11:14
 **/
public class MatrixDN {

    private String[] vertexs; //顶点集合
    private int[][] matrixs; //边或弧,邻接矩阵

    /*
         * 创建图(自己输入数据)
         */
    public MatrixDN() {

        // 输入"顶点数"和"边数"
        System.out.printf("input vertex number: ");
        int vlen = readInt();
        System.out.printf("input edge number: ");
        int elen = readInt();
        if ( vlen < 1 || elen < 1 || (elen > (vlen*(vlen - 1)))) {
            System.out.printf("input error: invalid parameters!\n");
            return ;
        }

        // 初始化"顶点"
        vertexs = new String[vlen];
        for (int i = 0; i < vertexs.length; i++) {
            System.out.printf("vertex(%d): ", i);
            vertexs[i] = String.valueOf(readChar());
        }

        // 初始化"边"
        matrixs=new int[vlen][vlen];
         for (int i = 0; i < elen; i++) {
            // 读取边的起始顶点和结束顶点
            System.out.printf("edge(%d):", i);
            String  c1 = String.valueOf(readChar());
             String c2 = String.valueOf(readChar());
            int p1 = getPostion(c1);
            int p2 = getPostion(c2);
            int weight = readInt();

            if (p1==-1 || p2==-1) {
                System.out.printf("input error: invalid edge!\n");
                return ;
            }

            matrixs[p1][p2] = weight;
        }
    }

    public MatrixDN(String[] vertex, ArrayList<String> matrix) {
        int verLen = vertex.length;
        int edgeLen = matrix.size();
        //初始化顶点
        vertexs = new String[verLen];
        for (int i = 0; i < verLen; i++) {
            vertexs[i] = vertex[i];
        }
        //初始化边或弧
        matrixs = new int[verLen][verLen];
        for (int i = 0; i < verLen; i++) {
            String v1 = matrix.get(i).substring(0, 1);
            String v2 = matrix.get(i).substring(1, 2);
            int p1 = getPostion(v1);
            int p2 = getPostion(v2);
            int weight = Integer.parseInt(matrix.get(i).substring(2));
            matrixs[p1][p2] = weight;
        }

    }

    public int getPostion(String v) {
        for (int i = 0; i < vertexs.length; i++) {
            if (vertexs[i].equals(v)) {
                return i;

            }
        }
        return -1;
    }


    /*
     * 读取一个输入字符
     */
    private char readChar() {
        char ch='0';

        do {
            try {
                ch = (char)System.in.read();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } while(!((ch>='a'&&ch<='z') || (ch>='A'&&ch<='Z')));

        return ch;
    }

    /*
     * 读取一个输入字符
     */
    private int readInt() {
        Scanner scanner = new Scanner(System.in);
        return scanner.nextInt();
    }

    public void print() {
        System.out.println("MatrixDN:\n");
        for(int i=0;i<matrixs.length;i++){
            for(int j=0;j<matrixs[i].length;j++){
                System.out.println("顶点"+vertexs[i]+"->顶点"+vertexs[j]+" weight:"+matrixs[i][j]);
            }
        }
    }

    /*
    * 打印矩阵队列图
    */
    public void print1() {
        System.out.printf("Martix Graph:\n");
        for (int i = 0; i < matrixs.length; i++) {
            for (int j = 0; j < matrixs.length; j++){
                System.out.printf("%d ", matrixs[i][j]);}
            System.out.printf("\n");
        }
    }



    public static void main(String[] args) {
        String[] vertexs = {"A","B","C","D"};
        ArrayList<String> arrayList = new ArrayList<>();
        arrayList.add("AB4");
        arrayList.add("BC5");
        arrayList.add("CD7");
        arrayList.add("BD7");
        arrayList.add("DB7");
        arrayList.add("DA7");
//        MatrixDN dn = new MatrixDN(vertexs,arrayList);
        MatrixDN dn = new MatrixDN();
        dn.print();
        dn.print1();

    }

}

3.运行结果:

转载于:https://my.oschina.net/u/2263272/blog/1609918

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值