|
| 1 | +import java.io.File; |
| 2 | +import java.io.IOException; |
| 3 | +import java.util.Map; |
| 4 | +import java.util.TreeMap; |
| 5 | +import java.util.Scanner; |
| 6 | + |
| 7 | +// 带权图 |
| 8 | +public class WeightedGraph implements Cloneable{ |
| 9 | + |
| 10 | + private int V; // 顶点 |
| 11 | + private int E; // 边 |
| 12 | + private TreeMap<Integer,Integer>[] adj; //adj数组,里面保存着TreeMap。treemap参数 相邻的顶点 ,权值 |
| 13 | + |
| 14 | + public WeightedGraph(String filename) { |
| 15 | + File file = new File(filename); |
| 16 | + if(!file.exists()) throw new IllegalArgumentException("文件不存在"); |
| 17 | + |
| 18 | + try(Scanner scanner = new Scanner(file)){ |
| 19 | + |
| 20 | + V = scanner.nextInt(); // 读取第一行的第一个,即多少个顶点 |
| 21 | + if(V < 0) throw new IllegalArgumentException("V must be non-negative"); |
| 22 | + adj = new TreeMap[V]; // 根据顶点创建数组 |
| 23 | + |
| 24 | + for(int i = 0; i < V; i ++){ |
| 25 | + adj[i] = new TreeMap<Integer,Integer>(); // 数组里保存着treeMap |
| 26 | + } |
| 27 | + |
| 28 | + E = scanner.nextInt(); |
| 29 | + if(E < 0) throw new IllegalArgumentException("E must be non-negative"); |
| 30 | + for(int i = 0; i < E; i ++){ // |
| 31 | + int a = scanner.nextInt();validateVertex(a); |
| 32 | + int b = scanner.nextInt();validateVertex(b); |
| 33 | + int weight = scanner.nextInt(); //一行的第三个值是权重 |
| 34 | + |
| 35 | + if(a == b) throw new IllegalArgumentException("Self Loop is Detected!"); // 判断自环边 |
| 36 | + if(adj[a].containsKey(b)) throw new IllegalArgumentException("Parallel Edges are Detected!"); // 判断平行边 |
| 37 | + |
| 38 | + adj[a].put(b,weight); // adj[x]顶点是一个TreeMap,每个顶点对应着相连的顶点和权重 |
| 39 | + adj[b].put(a,weight); |
| 40 | + } |
| 41 | + } |
| 42 | + catch(IOException e){ |
| 43 | + e.printStackTrace(); |
| 44 | + } |
| 45 | + |
| 46 | + } |
| 47 | + |
| 48 | + |
| 49 | + public void validateVertex(int v){ |
| 50 | + if(v < 0 || v >= V) |
| 51 | + throw new IllegalArgumentException("vertex " + v + "is invalid"); |
| 52 | + } |
| 53 | + |
| 54 | + // 顶点 |
| 55 | + public int V(){ |
| 56 | + return V; |
| 57 | + } |
| 58 | + |
| 59 | + // 边 |
| 60 | + public int E(){ |
| 61 | + return E; |
| 62 | + } |
| 63 | + |
| 64 | + // 是否存在边 |
| 65 | + public boolean hasEdge(int v, int w){ |
| 66 | + validateVertex(v); |
| 67 | + validateVertex(w); |
| 68 | + return adj[v].containsKey(w); |
| 69 | + } |
| 70 | + |
| 71 | + |
| 72 | + // 返回v这个顶点相邻的顶点 |
| 73 | + public Iterable<Integer> adj(int v) { |
| 74 | + validateVertex(v); |
| 75 | + return adj[v].keySet(); // key是顶点、value是权重 |
| 76 | + } |
| 77 | + |
| 78 | + // 返回边对应的权重 |
| 79 | + public int getWeight(int v,int w){ |
| 80 | + if(hasEdge(v,w)) |
| 81 | + return adj[v].get(w); |
| 82 | + throw new IllegalArgumentException(String.format("no edge %d %d",v,w)); |
| 83 | + } |
| 84 | + |
| 85 | + // 返回一个顶点的度,即顶点相邻的点有几个 |
| 86 | + public int degree(int v){ |
| 87 | + validateVertex(v); |
| 88 | + return adj[v].size(); |
| 89 | + } |
| 90 | + |
| 91 | + |
| 92 | +// public void removeEdge(int v,int w){ |
| 93 | +// validateVertex(v); |
| 94 | +// validateVertex(w); |
| 95 | +// |
| 96 | +// adj[v].remove(w); |
| 97 | +// adj[w].remove(v); |
| 98 | +// } |
| 99 | +// |
| 100 | +// |
| 101 | +// @Override |
| 102 | +// public Object clone(){ |
| 103 | +// try{ |
| 104 | +// WeightedGraph cloned = (WeightedGraph)super.clone(); |
| 105 | +// cloned.adj = new TreeMap[V]; |
| 106 | +// for(int v = 0; v < V; v ++){ |
| 107 | +// cloned.adj[v] = new TreeMap<Integer,Integer>(); |
| 108 | +// for(Map.Entry<Integer,Integer> entry : adj[v].entrySet() ) //遍历map |
| 109 | +// cloned.adj[v].put(entry.getKey(),entry.getValue()); |
| 110 | +// } |
| 111 | +// return cloned; |
| 112 | +// }catch(CloneNotSupportedException e){ |
| 113 | +// e.printStackTrace(); |
| 114 | +// } |
| 115 | +// return null; |
| 116 | +// } |
| 117 | +//// |
| 118 | + @Override |
| 119 | + public String toString(){ |
| 120 | + StringBuilder sb = new StringBuilder(); |
| 121 | + |
| 122 | + sb.append(String.format("V = %d, E = %d\n", V, E)); |
| 123 | + for(int i = 0; i < V; i ++){ |
| 124 | + sb.append(String.format("%d :", i)); |
| 125 | + for(Map.Entry<Integer,Integer> entry : adj[i].entrySet() ) //遍历map |
| 126 | + sb.append(String.format("(%d : %d)", entry.getKey(),entry.getValue())); |
| 127 | + sb.append('\n'); |
| 128 | + } |
| 129 | + return sb.toString(); |
| 130 | + } |
| 131 | + |
| 132 | + public static void main(String[] args){ |
| 133 | + |
| 134 | + WeightedGraph g = new WeightedGraph("g6.txt"); |
| 135 | + System.out.print(g); |
| 136 | + } |
| 137 | +} |
0 commit comments