Skip to content

Commit a316c8b

Browse files
committed
[Function add]
1.Add conclusion about minimum spanning tree. 2. Add conclsuion about the greedy algorithm.
1 parent f3b822c commit a316c8b

File tree

4 files changed

+132
-0
lines changed

4 files changed

+132
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package ca.mcmaster.chapter.four.graph.mstree;
2+
3+
public class Edge implements Comparable<Edge> {
4+
private final int v;
5+
private final int w;
6+
private final double weight;
7+
public Edge(int v, int w, double weight){
8+
this.v = v;
9+
this.w = w;
10+
this.weight = weight;
11+
}
12+
@Override
13+
public int compareTo(Edge o) {
14+
if(this.weight - o.weight > 0) return 1;
15+
else if(this.weight == o.weight) return 0;
16+
else return -1;
17+
}
18+
public double weight(){
19+
return this.weight;
20+
}
21+
public int either(){
22+
return v;
23+
}
24+
public int other(int v){
25+
if(v == this.v) return w;
26+
else return v;
27+
}
28+
@Override
29+
public String toString() {
30+
return String.format("%d-%d %.2f", v, w, weight);
31+
}
32+
}

Diff for: DataStructrue/Graph/Edge.java

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package ca.mcmaster.chapter.four.graph.mstree;
2+
3+
public class Edge implements Comparable<Edge> {
4+
private final int v;
5+
private final int w;
6+
private final double weight;
7+
public Edge(int v, int w, double weight){
8+
this.v = v;
9+
this.w = w;
10+
this.weight = weight;
11+
}
12+
@Override
13+
public int compareTo(Edge o) {
14+
if(this.weight - o.weight > 0) return 1;
15+
else if(this.weight == o.weight) return 0;
16+
else return -1;
17+
}
18+
public double weight(){
19+
return this.weight;
20+
}
21+
public int either(){
22+
return v;
23+
}
24+
public int other(int v){
25+
if(v == this.v) return w;
26+
else return v;
27+
}
28+
@Override
29+
public String toString() {
30+
return String.format("%d-%d %.2f", v, w, weight);
31+
}
32+
}

Diff for: DataStructrue/Graph/MinimumSpanningTrees.md

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Minimum Spanning trees最小生成树
2+
3+
### 加权图
4+
>每条边都给了一个权重的无向图。
5+
6+
### 最小生成树
7+
>给定一幅加权无向图,找到它的一颗最小生成树。
8+
9+
#### 约定
10+
1. 只考虑连通图。(非联通图无法只生成一棵树。)
11+
2. 边的权重不一定表示距离。(实际上所画出的图形是一种抽象且不唯一的图,顶点之间实际上是没有距离这个概念的)
12+
3. 边的权重可能是0或是负数。
13+
4. 所有边的权重都各不相同。(权重相同会造成生成的最小树不唯一)
14+
15+
#### 树的性质
16+
1. 用一条边连接树中的任意的两个顶点都会产生一个环。
17+
2. 从树中删去一条边会得到两棵独立的树。
18+
19+
#### 切分定理
20+
> 将图的所有顶点分成两个非空且不重合的两个集合,横切边是一条连接两个属于不同集合的顶点的边。
21+
* 在一幅加权图中,给定任意的切分,它的横切边中的权重最小者必然属于图的最小生成树。
22+
23+
### [贪心算法 Greedy Algorithm](https://www.cnblogs.com/MrSaver/p/8641971.html)
24+
* 贪心算法是使所做的选择看起来都是当前最佳的,期望通过所做的局部最优选择来产生一个全局最优解。
25+
26+
* 贪心算法设计步骤
27+
1. 将优化问题转换成这样一个问题,即先做出选择,再解决剩下的一个子问题。
28+
2. 证明原问题总是有一个最优解是贪心选择的得到的,从而说明贪心选择的安全。
29+
3. 说明在做出贪心选择后,剩下的子问题具有这样一个性质。即如果将子问题的最优解和我们所做的贪心选择联合起来,可以得到一个更加负责的动态规划解。
30+
31+
* 使用贪心算法找到最小树
32+
* 使用切分定理找到最小生成树的一边,不断重复直到找到最小生成树的所有边。
33+
* 一幅有V个顶点的图,初始状态下所有变均为灰色,找到一种切分,它产生的横切边均不为黑色。将他权重最小的横切边标记为黑色,反复,直到标记了V-1条黑边为止。
34+
35+
### Edge对象
36+
```Java
37+
public class Edge implements Comparable<Edge> {
38+
private final int v; //因为无向图,v和w分别表示一条无向边的两个顶点
39+
private final int w;
40+
private final double weight; //无向边的权重
41+
public Edge(int v, int w, double weight){
42+
this.v = v;
43+
this.w = w;
44+
this.weight = weight;
45+
}
46+
@Override
47+
public int compareTo(Edge o) { //继承Comparable接口,用于之后比较无向边的权重。
48+
if(this.weight - o.weight > 0) return 1;
49+
else if(this.weight == o.weight) return 0;
50+
else return -1;
51+
}
52+
public double weight(){
53+
return this.weight;
54+
}
55+
public int either(){
56+
return v;
57+
}
58+
public int other(int v){
59+
if(v == this.v) return w;
60+
else return v;
61+
}
62+
@Override
63+
public String toString() {
64+
return String.format("%d-%d %.2f", v, w, weight);
65+
}
66+
}
67+
```

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ I started learning the data struture systematically, I will list my notes in the
3737
### [Graph](https://github.com/Seanforfun/Algorithm/tree/master/DataStructrue/Graph)
3838
1. [Undirected Graph](https://github.com/Seanforfun/Algorithm/blob/master/DataStructrue/Graph/UndirectedGraph.md)
3939
2. [Directed Graph](https://github.com/Seanforfun/Algorithm/blob/master/DataStructrue/Graph/DirectedGraph.md)
40+
3. [Minimun Spanning Trees](https://github.com/Seanforfun/Algorithm/blob/master/DataStructrue/Graph/MinimumSpanningTrees.md)
4041

4142
## LeetCode
4243
This part I listed my [leetcode solutions](https://github.com/Seanforfun/Algorithm/tree/master/leetcode).

0 commit comments

Comments
 (0)