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());
}
}
图的矩阵表示
最新推荐文章于 2021-11-06 21:17:42 发布