📄 weightedgraph.java
字号:
/**
* @(#)Graph.java
*
*
* @author
* @version 1.00 2007/3/1
*/
import java.util.*;
public class WeightedGraph extends Graph {
public WeightedGraph() {
super();
initializeMatrix();
}
public WeightedGraph(int size) {
super(size);
initializeMatrix();
}
//adding an edge means putting in edge weight instead of 1 or 0
public void addEdge(WeightedEdge e) {
incrementEdges();
matrix[e.getV1().el] [e.getV2().el] = e.weight;
matrixCost[e.getV1().el] [e.getV2().el] = e.cost;
}
public void initializeMatrix() {
for(int i=0; i<matrix.length;i++ ) {
for(int j=0; j<matrix.length; j++)
matrix[i][j] = Integer.MAX_VALUE;
}
}
public void displayMatrix() {
for(int i=0; i<matrix.length;i++ ) {
for(int j=0; j<matrix.length; j++)
System.out.print(matrix[i][j] + "\t");
System.out.println();
}
}
// get the list of vertices
public Vector getVertexList() {
Vector list = new Vector();
for(int i=0; i<matrix.length;i++ ) {
for(int j=0; j<matrix.length; j++)
if (matrix[i][j] != Integer.MAX_VALUE) {
list.add(new Vertex(i));
break;
}
}
return list;
}
// get the list of edges
public Vector getEdgeList() {
Vector list = new Vector();
for(int i=0; i<matrix.length;i++ ) {
for(int j=0; j<matrix.length; j++)
if (matrix[i][j] != Integer.MAX_VALUE) {
Vertex v1 = new Vertex(i);
Vertex v2 = new Vertex(j);
int weight = matrix[i] [j];
int cost = matrixCost[i][j];
list.add(new WeightedEdge(v1,v2,weight,cost));
}
}
return list;
}
} //end WeightedGraph
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -