weightedgraph.java
来自「A graph class for networks」· Java 代码 · 共 87 行
JAVA
87 行
/**
* @(#)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 + =
减小字号Ctrl + -
显示快捷键?