Skip to content

Commit 3ae8738

Browse files
committed
使用git管理my-lib
1 parent e28c74d commit 3ae8738

File tree

91 files changed

+15468
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

91 files changed

+15468
-0
lines changed

pom.xml

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>cn.navyd</groupId>
7+
<artifactId>my-lib</artifactId>
8+
<version>1.0-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
11+
<name>my-lib</name>
12+
<url>http://maven.apache.org</url>
13+
14+
<properties>
15+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
16+
<maven.compiler.source>1.8</maven.compiler.source>
17+
<maven.compiler.target>1.8</maven.compiler.target>
18+
19+
<junit.version>4.12</junit.version>
20+
<lombok.version>1.18.0</lombok.version>
21+
<slf4j.version>1.7.25</slf4j.version>
22+
<logback.version>1.2.3</logback.version>
23+
</properties>
24+
25+
<dependencies>
26+
<dependency>
27+
<groupId>junit</groupId>
28+
<artifactId>junit</artifactId>
29+
<version>${junit.version}</version>
30+
<scope>test</scope>
31+
</dependency>
32+
33+
<dependency>
34+
<groupId>org.projectlombok</groupId>
35+
<artifactId>lombok</artifactId>
36+
<version>${lombok.version}</version>
37+
<scope>provided</scope>
38+
</dependency>
39+
40+
<!-- 日志框架 -->
41+
<dependency>
42+
<groupId>org.slf4j</groupId>
43+
<artifactId>slf4j-api</artifactId>
44+
<version>${slf4j.version}</version>
45+
</dependency>
46+
<dependency>
47+
<groupId>ch.qos.logback</groupId>
48+
<artifactId>logback-classic</artifactId>
49+
<version>${logback.version}</version>
50+
<scope>runtime</scope>
51+
</dependency>
52+
</dependencies>
53+
</project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package cn.navyd.lib;
2+
3+
/**
4+
* Hello world!
5+
*
6+
*/
7+
public class App
8+
{
9+
public static void main( String[] args )
10+
{
11+
System.out.println( "Hello World!" );
12+
}
13+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package cn.navyd.lib.algs.graph;
2+
3+
import cn.navyd.lib.algs.util.In;
4+
import cn.navyd.lib.algs.util.ArrayStack;
5+
6+
/**
7+
* 无环加权有向图的单点最长路径算法:<br>
8+
* 与最短路径算法相似,这里只是修改了distTo的初始值为负无穷
9+
* 并改变relax中不等式的方向
10+
* @author Navy D
11+
* @date 20170912180300
12+
*/
13+
public class AcyclicLP {
14+
// 最长路径的边
15+
private DirectedEdge[] edgeTo;
16+
// 最长路径
17+
private double[] distTo;
18+
19+
public AcyclicLP(EdgeWeightedDigraph g, int s) {
20+
edgeTo = new DirectedEdge[g.getV()];
21+
distTo = new double[g.getV()];
22+
23+
validateVertex(s);
24+
// 将每个顶点初始化为负无穷
25+
for (int v = 0; v < g.getV(); v++)
26+
distTo[v] = Double.NEGATIVE_INFINITY;
27+
distTo[s] = 0.0;
28+
// 使用拓扑排序的顶点顺序作为取到顶点权重最小的边
29+
Topological top = new TopologicalDFS(g);
30+
if (!top.hasOrder())
31+
throw new IllegalArgumentException("Digraph is not acyclic.");
32+
for (int v : top.order())
33+
// 放松每个顶点
34+
relax(g, v);
35+
}
36+
37+
private void relax(EdgeWeightedDigraph g, int v) {
38+
for (DirectedEdge e : g.adj(v)) {
39+
int w = e.to();
40+
// 取更大的路径长度
41+
if (distTo[w] < distTo[v] + e.weight()) {
42+
distTo[w] = distTo[v] + e.weight();
43+
edgeTo[w] = e;
44+
}
45+
}
46+
}
47+
48+
/**
49+
* 返回从起点s到顶点v的最长距离
50+
* @param v
51+
* @return
52+
* @author Navy D
53+
* @date 20170917113226
54+
*/
55+
public double distTo(int v) {
56+
validateVertex(v);
57+
return distTo[v];
58+
}
59+
60+
/**
61+
* 如果图中存在从s到达顶点v的最长路径就返回true
62+
* @param v
63+
* @return
64+
* @author Navy D
65+
* @date 20170917113307
66+
*/
67+
public boolean hasPathTo(int v) {
68+
validateVertex(v);
69+
return distTo[v] > Double.NEGATIVE_INFINITY;
70+
71+
}
72+
73+
/**
74+
* 返回起点s到顶点v的最长路径集合,如果不存在这个路径就返回null
75+
* @param v
76+
* @return
77+
* @author Navy D
78+
* @date 20170917113852
79+
*/
80+
public Iterable<DirectedEdge> pathTo(int v) {
81+
validateVertex(v);
82+
if (!hasPathTo(v))
83+
return null;
84+
ArrayStack<DirectedEdge> path = new ArrayStack<>();
85+
for (DirectedEdge e = edgeTo[v]; e != null; e = edgeTo[e.from()]) {
86+
path.push(e);
87+
}
88+
return path;
89+
}
90+
91+
private void validateVertex(int v) {
92+
if (v < 0 || v >= distTo.length)
93+
throw new IllegalArgumentException("vertex " + v + " is not between 0 and " + (distTo.length-1));
94+
}
95+
96+
public static void main(String[] args) {
97+
EdgeWeightedDigraph G = new EdgeWeightedDigraph(new In("../Algs_4/tinyEWDAG.txt"));
98+
int s = 1;
99+
AcyclicLP lp = new AcyclicLP(G, s);
100+
101+
for (int v = 0; v < G.getV(); v++) {
102+
if (lp.hasPathTo(v)) {
103+
System.out.printf("%d to %d (%.2f) ", s, v, lp.distTo(v));
104+
for (DirectedEdge e : lp.pathTo(v)) {
105+
System.out.print(e + " ");
106+
}
107+
System.out.println();
108+
}
109+
else {
110+
System.out.printf("%d to %d no path\n", s, v);
111+
}
112+
}
113+
}
114+
}
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
package cn.navyd.lib.algs.graph;
2+
3+
import cn.navyd.lib.algs.util.In;
4+
import cn.navyd.lib.algs.util.ArrayStack;
5+
6+
/**
7+
* 无环加权有向图的最短路径算法:<br>
8+
* 思想:按照拓扑排序的顺序放松每个顶点。因为拓扑顺序的顶点w除了之前的顶点v是不会有顶点指向w的,
9+
* 顶点w除了树中的路径v指向,没有树外的顶点指向边了,一旦顶点w加入最短路径中,路径不会再变化。<br>
10+
* 复杂度:所需时间为e+v
11+
* @author Navy D
12+
* @date 20170912172917
13+
*/
14+
public class AcyclicSP implements ShortestPath {
15+
// 最短路径的存储数组
16+
private DirectedEdge[] edgeTo;
17+
// 最短路径权重edgeTo[v] = s->v 最短路径权值
18+
private double[] distTo;
19+
20+
/**
21+
* 在一个无环有向图计算从起点s开始的最短路径
22+
* @param g
23+
* @param s
24+
*/
25+
public AcyclicSP(EdgeWeightedDigraph g, int s) {
26+
edgeTo = new DirectedEdge[g.getV()];
27+
distTo = new double[g.getV()];
28+
29+
validateVertex(s);
30+
// 将所有顶点距离初始化最大
31+
for (int v = 0; v < g.getV(); v++)
32+
distTo[v] = Double.POSITIVE_INFINITY;
33+
// 初始化起点s
34+
distTo[s] = 0.0;
35+
// 使用拓扑排序的顶点顺序作为取到顶点权重最小的边
36+
Topological top = new TopologicalDFS(g);
37+
// 如果图g含有环
38+
if (!top.hasOrder())
39+
throw new IllegalArgumentException("Digraph is not acyclic.");
40+
for (int v : top.order())
41+
// 放松每个顶点
42+
relax(g, v);
43+
}
44+
45+
/**
46+
* 顶点的松弛:
47+
* 对于给定顶点的所有边,检查每条边v-w的最短路径是否是当前路径s-v-w最短,如果是则更新路径
48+
* @param g
49+
* @param v
50+
* @author Navy D
51+
* @date 20170912172541
52+
*/
53+
private void relax(EdgeWeightedDigraph g, int v) {
54+
for (DirectedEdge e : g.adj(v)) {
55+
int w = e.to();
56+
// 如果发现加上当前顶点边v-w比之前树中边连接距离要短就更新
57+
if (distTo[w] > distTo[v] + e.weight()) {
58+
distTo[w] = distTo[v] + e.weight();
59+
edgeTo[w] = e;
60+
}
61+
}
62+
}
63+
64+
/**
65+
* 返回从起点s到顶点v的最短距离
66+
* @param v
67+
* @return
68+
* @author Navy D
69+
* @date 20170917113226
70+
*/
71+
public double distTo(int v) {
72+
validateVertex(v);
73+
return distTo[v];
74+
}
75+
76+
/**
77+
* 如果图中存在从s到达顶点v的最短路径就返回true
78+
* @param v
79+
* @return
80+
* @author Navy D
81+
* @date 20170917113307
82+
*/
83+
public boolean hasPathTo(int v) {
84+
validateVertex(v);
85+
return distTo[v] < Double.POSITIVE_INFINITY;
86+
87+
}
88+
89+
/**
90+
* 返回起点s到顶点v的最短路径集合,如果不存在这个路径就返回null
91+
* @param v
92+
* @return
93+
* @author Navy D
94+
* @date 20170917113852
95+
*/
96+
public Iterable<DirectedEdge> pathTo(int v) {
97+
validateVertex(v);
98+
if (!hasPathTo(v))
99+
return null;
100+
ArrayStack<DirectedEdge> path = new ArrayStack<>();
101+
for (DirectedEdge e = edgeTo[v]; e != null; e = edgeTo[e.from()]) {
102+
path.push(e);
103+
}
104+
return path;
105+
}
106+
107+
private void validateVertex(int v) {
108+
if (v < 0 || v >= distTo.length)
109+
throw new IllegalArgumentException("vertex " + v + " is not between 0 and " + (distTo.length-1));
110+
}
111+
112+
113+
public static void main(String[] args) {
114+
EdgeWeightedDigraph g = new EdgeWeightedDigraph(new In("./tinyEWDAG.txt"));
115+
int s = 1;
116+
AcyclicSP sp = new AcyclicSP(g, s);
117+
for (int v = 0; v < g.getV(); v++) {
118+
if (sp.hasPathTo(v)) {
119+
System.out.printf("%d to %d (%.2f) ", s, v, sp.distTo(v));
120+
for (DirectedEdge e : sp.pathTo(v)) {
121+
System.out.print(e + " ");
122+
}
123+
System.out.println();
124+
}
125+
else {
126+
System.out.printf("%d to %d no path\n", s, v);
127+
}
128+
}
129+
}
130+
}

0 commit comments

Comments
 (0)