Skip to content

Commit 3d85570

Browse files
committed
make graph
0 parents  commit 3d85570

12 files changed

+446
-0
lines changed

.idea/misc.xml

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

.idea/modules.xml

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

.idea/workspace.xml

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

Graph.iml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
</component>
11+
</module>

g.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
7 9
2+
0 1
3+
0 3
4+
1 2
5+
1 6
6+
2 3
7+
2 5
8+
3 4
9+
4 5
10+
5 6
3.71 KB
Binary file not shown.
3.62 KB
Binary file not shown.

out/production/Graph/Graph.class

3.7 KB
Binary file not shown.
16 Bytes
Binary file not shown.

src/AdjacencyList.java

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
import java.io.File;
2+
import java.io.IOException;
3+
import java.util.LinkedList;
4+
import java.util.Scanner;
5+
6+
// 邻接表
7+
public class AdjacencyList {
8+
9+
private int V; // 顶点
10+
private int E; // 边
11+
private LinkedList<Integer>[] adj; // 领接表
12+
13+
public AdjacencyList(String filename) {
14+
File file = new File(filename);
15+
if(!file.exists()) throw new IllegalArgumentException("文件不存在");
16+
17+
try(Scanner scanner = new Scanner(file)){
18+
19+
V = scanner.nextInt(); // 读取第一行的第一个,即多少个顶点
20+
if(V < 0) throw new IllegalArgumentException("V must be non-negative");
21+
adj = new LinkedList[V]; // 根据顶点创建
22+
23+
for(int i = 0; i < V; i ++){
24+
adj[i] = new LinkedList<Integer>();
25+
}
26+
27+
E = scanner.nextInt(); // 第一行的第二值个为边
28+
if(E < 0) throw new IllegalArgumentException("E must be non-negative");
29+
for(int i = 0; i < E; i ++){ //
30+
int a = scanner.nextInt();validateVertex(a);
31+
int b = scanner.nextInt();validateVertex(b);
32+
33+
if(a == b) throw new IllegalArgumentException("Self Loop is Detected!"); // 判断自环边
34+
if(adj[a].contains(b)) throw new IllegalArgumentException("Parallel Edges are Detected!"); // 判断平行边
35+
36+
adj[a].add(b);
37+
adj[b].add(a);
38+
}
39+
}
40+
catch(IOException e){
41+
e.printStackTrace();
42+
}
43+
44+
}
45+
46+
47+
private void validateVertex(int v){
48+
if(v < 0 || v >= V)
49+
throw new IllegalArgumentException("vertex " + v + "is invalid");
50+
}
51+
52+
// 顶点
53+
public int V(){
54+
return V;
55+
}
56+
57+
// 边
58+
public int E(){
59+
return E;
60+
}
61+
62+
// 是否存在边
63+
public boolean hasEdge(int v, int w){
64+
validateVertex(v);
65+
validateVertex(w);
66+
return adj[v].contains(w);
67+
}
68+
69+
70+
// 返回v这个顶点相邻的顶点
71+
public LinkedList<Integer> adj(int v) {
72+
validateVertex(v);
73+
return adj[v];
74+
}
75+
76+
// 返回一个顶点的度,即顶点相邻的点有几个
77+
public int degree(int v){
78+
return adj(v).size();
79+
}
80+
81+
82+
@Override
83+
public String toString(){
84+
StringBuilder sb = new StringBuilder();
85+
86+
sb.append(String.format("V = %d, E = %d\n", V, E));
87+
for(int i = 0; i < V; i ++){
88+
for(int w : adj[i])
89+
sb.append(String.format("%d ", w));
90+
sb.append('\n');
91+
}
92+
return sb.toString();
93+
}
94+
95+
public static void main(String[] args){
96+
97+
AdjacencyList adjacencyList = new AdjacencyList("g.txt");
98+
System.out.print(adjacencyList);
99+
}
100+
}

src/AdjacencyMatrix.java

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
2+
import java.io.File;
3+
import java.io.FileNotFoundException;
4+
import java.io.IOException;
5+
import java.util.ArrayList;
6+
import java.util.Scanner;
7+
8+
//邻接矩阵
9+
public class AdjacencyMatrix {
10+
11+
private int V; // 顶点
12+
private int E; // 边
13+
private int[][] adj; // 领接矩阵
14+
15+
public AdjacencyMatrix(String filename) {
16+
File file = new File(filename);
17+
if(!file.exists()) throw new IllegalArgumentException("文件不存在");
18+
19+
try(Scanner scanner = new Scanner(file)){
20+
21+
V = scanner.nextInt(); // 读取第一行的第一个,即多少个顶点
22+
if(V < 0) throw new IllegalArgumentException("V must be non-negative");
23+
adj = new int[V][V]; // 根据顶点创建矩阵
24+
25+
E = scanner.nextInt(); // 第一行的第二值个为边
26+
if(E < 0) throw new IllegalArgumentException("E must be non-negative");
27+
for(int i = 0; i < E; i ++){ //根据边生成邻接矩阵,每一行2个值表示a b ,表示这2个顶点相连
28+
int a = scanner.nextInt();validateVertex(a);
29+
int b = scanner.nextInt();validateVertex(b);
30+
31+
if(a == b) throw new IllegalArgumentException("Self Loop is Detected!"); // 判断自环边
32+
if(adj[a][b] == 1) throw new IllegalArgumentException("Parallel Edges are Detected!"); // 判断平行边
33+
34+
adj[a][b] = 1;
35+
adj[b][a] = 1;
36+
}
37+
}
38+
catch(IOException e){
39+
e.printStackTrace();
40+
}
41+
42+
}
43+
44+
45+
private void validateVertex(int v){
46+
if(v < 0 || v >= V)
47+
throw new IllegalArgumentException("vertex " + v + "is invalid");
48+
}
49+
50+
public int V(){
51+
return V;
52+
}
53+
54+
public int E(){
55+
return E;
56+
}
57+
58+
// 是否存在边
59+
public boolean hasEdge(int v, int w){
60+
validateVertex(v);
61+
validateVertex(w);
62+
return adj[v][w] == 1;
63+
}
64+
65+
66+
// 返回v这个顶点相邻的顶点
67+
public ArrayList<Integer> adj(int v) {
68+
validateVertex(v);
69+
ArrayList<Integer> res = new ArrayList<>();
70+
for (int i = 0; i < V; i++)
71+
if (adj[v][i] == 1)
72+
res.add(i);
73+
return res;
74+
}
75+
76+
// 返回一个顶点的度,即顶点相邻的点有几个
77+
public int degree(int v){
78+
return adj(v).size();
79+
}
80+
81+
82+
@Override
83+
public String toString(){
84+
StringBuilder sb = new StringBuilder();
85+
86+
sb.append(String.format("V = %d, E = %d\n", V, E));
87+
for(int i = 0; i < V; i ++){
88+
for(int j = 0; j < V; j ++)
89+
sb.append(String.format("%d ", adj[i][j]));
90+
sb.append('\n');
91+
}
92+
return sb.toString();
93+
}
94+
95+
public static void main(String[] args){
96+
97+
AdjacencyMatrix adjacencyMatrix = new AdjacencyMatrix("g.txt");
98+
System.out.print(adjacencyMatrix);
99+
}
100+
101+
}

0 commit comments

Comments
 (0)