Skip to content

Commit 992a946

Browse files
committed
KrusKal算法
1 parent 6afa3d4 commit 992a946

4 files changed

Lines changed: 11 additions & 28 deletions

File tree

.idea/misc.xml

Lines changed: 4 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/com/zejian/structures/Graph/WeightGraph/Edge.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ public Edge(int v, int w, Weight weight) {
1818

1919
public Edge(Edge<Weight> e)
2020
{
21-
this.v = e.w;
22-
this.w = e.w;
23-
this.weight = e.weight;
21+
this.v = e.v();
22+
this.w = e.w();
23+
this.weight = e.wt();
2424
}
2525

2626
/**

src/com/zejian/structures/Graph/WeightGraph/KruskalMST.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public KruskalMST(WeightGraph graph){
6363
//计算最小生成树的总权值
6464
mstWeight = mst.get(0).wt().doubleValue();
6565
for (int i = 1; i < mst.size() ; i++) {
66-
mstWeight =mstWeight.doubleValue() + mst.get(0).wt().doubleValue();
66+
mstWeight =mstWeight.doubleValue() + mst.get(i).wt().doubleValue();
6767
}
6868
}
6969

src/com/zejian/structures/Graph/WeightGraph/WeightSparseGraph.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ public void addEdge(Edge e) {
4141
assert e.v() >= 0 && e.v() < V ;
4242
assert e.w() >= 0 && e.w() < V ;
4343

44-
g[e.v()].add(e);
44+
g[e.v()].add(new Edge<Weight>(e));
4545
if( e.v() != e.w() && !directed ) {
46-
g[e.w()].add(e);
46+
g[e.w()].add(new Edge<Weight>(e.w(),e.v(), (Weight) e.wt()));
4747
}
4848
E++;
4949
}
@@ -89,7 +89,7 @@ public void show(){
8989
* @param args
9090
*/
9191
public static void main(String[] args){
92-
String filename = "weighttestG3.txt";
92+
String filename = "testWG1.txt";
9393

9494
WeightSparseGraph<Double> weightSparseGraph = new WeightSparseGraph<>(8,false);
9595
weightSparseGraph.readGraph(filename);

0 commit comments

Comments
 (0)