Skip to content

Commit 5db7aff

Browse files
committed
[Function add]
1.Add interface and default method for graph.
1 parent c2e889c commit 5db7aff

File tree

2 files changed

+186
-0
lines changed

2 files changed

+186
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package ca.mcmaster.chapter.four.graph;
2+
3+
import java.awt.DisplayMode;
4+
5+
public interface Graph {
6+
/**
7+
* @Description: Get the vertex number.
8+
* @return
9+
*/
10+
public int V();
11+
12+
/**
13+
* @Description: Get the edge number.
14+
* @return
15+
*/
16+
public int E();
17+
/**
18+
* @Description: Create an edge between w and v.
19+
* @param v
20+
* @param w
21+
*/
22+
public void addEdge(int v, int w);
23+
/**
24+
* @Description: Get all vertex adjacent to v.
25+
* @param v
26+
* @return
27+
*/
28+
public Iterable<Integer> adj(int v);
29+
30+
/**
31+
* @Description: Return degree of given vertex.
32+
* @param G
33+
* @param V
34+
* @return
35+
*/
36+
static Integer degree(Graph G, int V){
37+
Integer degree = new Integer(0);
38+
for(int w : G.adj(V)) degree++;
39+
return degree;
40+
}
41+
42+
/**
43+
* @Description: Find the largest degree in the graph
44+
* @param G
45+
* @param V
46+
* @return
47+
*/
48+
static int maxDegree(Graph G, int V){
49+
int max = 0;
50+
for(int w : G.adj(V) )
51+
if(w > max)
52+
max = w;
53+
return max;
54+
}
55+
56+
/**
57+
* @Description: Calculate the average degree for all vertex.
58+
* @param G
59+
* @return
60+
*/
61+
static double avgDegree(Graph G){
62+
return 2 * G.E() / G.V();
63+
}
64+
65+
/**
66+
* @Description: Get the number of selt loop.
67+
* @param G
68+
* @param V
69+
* @return
70+
*/
71+
static int numOfSelfLoop(Graph G, int V){
72+
int num = 0;
73+
int vNum = G.V();
74+
for(int v = 0; v < vNum; v++ )
75+
for(int w : G.adj(v))
76+
if(w == v)
77+
num ++;
78+
return num/2; //w,v and v,w will both be counted.
79+
}
80+
81+
/**
82+
* @Description: Print a graph.
83+
*/
84+
default void display(){
85+
int vertexNum = this.V();
86+
for(int v = 0; v < vertexNum; v++){
87+
StringBuilder sb = new StringBuilder(v + " -> ");
88+
for(int w : adj(v))
89+
sb.append(w + "");
90+
System.out.println(sb.toString());
91+
}
92+
}
93+
}
+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# Undirected Graph无向图
2+
>无向图由边(edge)和顶点(vertex)组成。
3+
>当两个顶点通过一条边相连时我们称这两个顶点相邻。同时这条边依附于这两个顶点
4+
>某个顶点的度数为依附它的边的条数。
5+
6+
* 连通图
7+
>如果从任意一个顶点都存在一条路径到达一个任意顶点,我们称这幅图是连通图
8+
9+
## 图的接口
10+
```Java
11+
public interface Graph {
12+
/**
13+
* @Description: Get the vertex number.
14+
* @return
15+
*/
16+
public int V();
17+
/**
18+
* @Description: Get the edge number.
19+
* @return
20+
*/
21+
public int E();
22+
/**
23+
* @Description: Create an edge between w and v.
24+
* @param v
25+
* @param w
26+
*/
27+
public void addEdge(int v, int w);
28+
/**
29+
* @Description: Get all vertex adjacent to v.
30+
* @param v
31+
* @return
32+
*/
33+
public Iterable<Integer> adj(int v);
34+
/**
35+
* @Description: Return degree of given vertex.
36+
* @param G
37+
* @param V
38+
* @return
39+
*/
40+
static Integer degree(Graph G, int V){
41+
Integer degree = new Integer(0);
42+
for(int w : G.adj(V)) degree++;
43+
return degree;
44+
}
45+
/**
46+
* @Description: Find the largest degree in the graph
47+
* @param G
48+
* @param V
49+
* @return
50+
*/
51+
static int maxDegree(Graph G, int V){
52+
int max = 0;
53+
for(int w : G.adj(V) )
54+
if(w > max)
55+
max = w;
56+
return max;
57+
}
58+
/**
59+
* @Description: Calculate the average degree for all vertex.
60+
* @param G
61+
* @return
62+
*/
63+
static double avgDegree(Graph G){
64+
return 2 * G.E() / G.V();
65+
}
66+
/**
67+
* @Description: Get the number of selt loop.
68+
* @param G
69+
* @param V
70+
* @return
71+
*/
72+
static int numOfSelfLoop(Graph G, int V){
73+
int num = 0;
74+
int vNum = G.V();
75+
for(int v = 0; v < vNum; v++ )
76+
for(int w : G.adj(v))
77+
if(w == v)
78+
num ++;
79+
return num/2; //w,v and v,w will both be counted.
80+
}
81+
/**
82+
* @Description: Print a graph.
83+
*/
84+
default void display(){
85+
int vertexNum = this.V();
86+
for(int v = 0; v < vertexNum; v++){
87+
StringBuilder sb = new StringBuilder(v + " -> ");
88+
for(int w : adj(v))
89+
sb.append(w + "");
90+
System.out.println(sb.toString());
91+
}
92+
}
93+
```

0 commit comments

Comments
 (0)