图的矩阵表示

package structure.graph.create;

import java.util.ArrayList;
import java.util.Scanner;

import structure.graph.node.GNode_01;
import utils.com.Matrixer;

/**
 * 矩阵表示
 * 
 * @author Toy
 * 
 */
public class GraphCreater_01 {

	public ArrayList<GNode_01> vertexList;

	public GraphCreater_01() {
		vertexList = new ArrayList<GNode_01>();
	}

	/**
	 * isDirect:是否为有向图
	 * 
	 * @param isDirect
	 * @return
	 */
	public int[][] createGraph(boolean isDirect) {
		vertexList.clear();
		while (true) {
			System.out.println("please input a vertex: ");
			Scanner s = new Scanner(System.in);
			String v = s.next().trim();

			if ("#".equals(v)) {
				break;
			}
			int check = getIndex(v);
			if (check >= 0) {
				System.err.println("duplicate vertex");
				continue;
			}

			GNode_01 vertex = new GNode_01(v);
			vertexList.add(vertex);
		}
		int msize = vertexList.size();
		int[][] g = new int[msize][msize];
		while (true) {
			Scanner s = new Scanner(System.in);

			System.out.println("please input a start vertex: ");
			String from = s.next().trim();

			int fi = getIndex(from);
			System.out.println("please input an end vertex: ");
			String to = s.next().trim();
			int ti = getIndex(to);

			if ("#".equals(from)) {
				break;
			}
			if (fi < 0 || ti < 0) {
				System.err.println("invalid vertex");
				continue;
			}

			System.out.println("please input their edge weight: ");
			int weight = Integer.parseInt(s.next().trim());

			g[fi][ti] = weight;
			if (!isDirect) {
				g[ti][fi] = weight;
			}
		}
		return g;
	}

	// 添加顶点
	public void addVertex(String v) {

		int check = getIndex(v);
		if (check >= 0) {
			System.err.println("duplicate vertex");
			return;
		}
		System.out.println("add vertex: " + v);
		GNode_01 vertex = new GNode_01(v);
		vertexList.add(vertex);
	}

	public int[][] addEdge(int[][] g, String from, String to, int w,
			boolean isDirect) {

		int fi = getIndex(from);
		int ti = getIndex(to);

		if (fi < 0 || ti < 0) {
			System.err.println("invalid vertex");
			return g;
		}

		g[fi][ti] = w;

		if (!isDirect) {
			g[ti][fi] = w;
		}

		System.out.println("add edge: " + from + " " + to + " " + w);
		return g;
	}

	public void show(int[][] g, int n) {
		System.out.println("show graph: " + n);
		for (int i = 0; i < vertexList.size(); i++) {
			System.out.print("\t" + vertexList.get(i).getLabel());
		}
		System.out.println();
		for (int i = 0; i < n; i++) {
			System.out.print(vertexList.get(i).getLabel() + "\t");
			for (int j = 0; j < n; j++) {
				System.out.print(g[i][j] + "\t");
			}
			System.out.println();
		}

	}

	private int getIndex(String vlabel) {
		for (int i = 0; i < vertexList.size(); i++) {
			if (vlabel.equals(vertexList.get(i).getLabel())) {
				return i;
			}
		}

		return -1;
	}

	public int[][] createTestGraph_01(boolean isDirect) {
		addVertex("A");
		addVertex("B");
		addVertex("C");
		addVertex("D");
		int size = vertexList.size();
		int[][] g = new int[size][size];
		g = addEdge(g, "A", "B", 1, isDirect);
		g = addEdge(g, "A", "C", 2, isDirect);
		g = addEdge(g, "B", "C", 3, isDirect);
		g = addEdge(g, "C", "D", 4, isDirect);

		return g;

	}

	
	public int[][] creatMSTTestGraph_02(boolean isDirect){
		
		addVertex("0");
		addVertex("1");
		addVertex("2");
		addVertex("3");
		addVertex("4");
		addVertex("5");
		
		int size = vertexList.size();
		int[][] g = new int[size][size];
		g = addEdge(g, "0", "1", 6, isDirect);
		g = addEdge(g, "0", "2", 1, isDirect);
		g = addEdge(g, "0", "3", 5, isDirect);
		
		g = addEdge(g, "1", "2", 5, isDirect);
		g = addEdge(g, "1", "4", 3, isDirect);
		
		g = addEdge(g, "2", "3", 7, isDirect);
		g = addEdge(g, "2", "4", 5, isDirect);
		g = addEdge(g, "2", "5", 4, isDirect);
		
		g = addEdge(g, "3", "5", 2, isDirect);
		g = addEdge(g, "4", "5", 6, isDirect);
		
		return g;
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		GraphCreater_01 c = new GraphCreater_01();

		// int[][] g = c.createGraph(false);
		// c.show(g, c.vertexList.size());

		int[][] g = c.createTestGraph_01(false);
		System.out.println("vertexList size: " + c.vertexList.size());
		c.show(g, c.vertexList.size());
	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值