Skip to content

Commit 98833c0

Browse files
committed
[Function add ]
1.Add comclusion and implement of derected graph.
1 parent 4681f47 commit 98833c0

File tree

5 files changed

+275
-1
lines changed

5 files changed

+275
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package ca.mcmaster.chapter.four.graph;
2+
3+
public interface Digraph {
4+
/**
5+
* @Description: Number of vertex.
6+
* @return
7+
*/
8+
public int V();
9+
/**
10+
* @Description: Number of edge.
11+
* @return
12+
*/
13+
public int E();
14+
/**
15+
* @Description: Add an edge between two vertex. v->w.
16+
* @param v
17+
* @param w
18+
*/
19+
public void addEdge(int v, int w);
20+
/**
21+
* @Description: Return vertex point out from v.
22+
* @param v
23+
* @return
24+
*/
25+
public Iterable<Integer> adj(int v);
26+
/**
27+
* @Description: Get the reverse graph of current graph.
28+
* @return
29+
*/
30+
public Digraph reverse();
31+
/**
32+
* @Description: Print current graph.
33+
*/
34+
public void display();
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package ca.mcmaster.chapter.four.graph;
2+
3+
import java.io.File;
4+
import java.io.FileInputStream;
5+
import java.io.IOException;
6+
import java.util.Scanner;
7+
8+
import ca.mcmaster.chapter.one.bag.ListBag;
9+
10+
public class DigraphImpl implements Digraph {
11+
private final int V; //Number of vertex in current digraph
12+
private int E; //Number of edges.
13+
private ListBag<Integer>[] adj;
14+
@SuppressWarnings("unchecked")
15+
public DigraphImpl(int V){
16+
this.V = V;
17+
adj = (ListBag<Integer>[])new ListBag[V];
18+
for (int i = 0; i < V; i++) {
19+
adj[i] = new ListBag<Integer>();
20+
}
21+
this.E = 0;
22+
}
23+
24+
@SuppressWarnings("unchecked")
25+
26+
public DigraphImpl(FileInputStream in) {
27+
Scanner s = null;
28+
s = new Scanner(in);
29+
this.V = s.nextInt();
30+
this.E = s.nextInt();
31+
adj = new ListBag[V];
32+
for (int i = 0; i < V; i++) {
33+
adj[i] = new ListBag<Integer>();
34+
}
35+
for(int i = 0; i < E; i ++){
36+
int v = s.nextInt();
37+
int w = s.nextInt();
38+
adj[v].add(w);
39+
}
40+
s.close();
41+
}
42+
@Override
43+
public int V() { return this.V; }
44+
@Override
45+
public void addEdge(int v, int w) {
46+
adj[v].add(w);
47+
E++;
48+
}
49+
@Override
50+
public int E() { return E; }
51+
@Override
52+
public Iterable<Integer> adj(int v) {
53+
return adj[v];
54+
}
55+
@Override
56+
public Digraph reverse() {
57+
DigraphImpl g = new DigraphImpl(V);
58+
for(int v = 0; v < V; v++)
59+
for(int w : adj(v))
60+
addEdge(w, v);
61+
return g;
62+
}
63+
@Override
64+
public void display() {
65+
for(int i = 0; i < V; i++){
66+
StringBuilder sb = new StringBuilder(i + ": ");
67+
for(int w : adj[i]){
68+
sb.append(w + "|");
69+
}
70+
System.out.println(sb.toString());
71+
}
72+
}
73+
public static void main(String[] args) throws IOException {
74+
FileInputStream in = new FileInputStream(new File("src/ca/mcmaster/chapter/four/graph/tinyDG.txt"));
75+
Digraph graph = new DigraphImpl(in);
76+
in.close();
77+
graph.display();
78+
}
79+
}

Algorithm(4th_Edition)/src/ca/mcmaster/chapter/four/graph/SymbolGraphImpl.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public static void main(String[] args) throws FileNotFoundException {
7272
sb.append(symbolGraphImpl.name(w) + "|");
7373
}
7474
BreadthFirstPath bfs = new BreadthFirstPath(graph, symbolGraphImpl.index("Bacon, Kevin"));
75-
Iterable<Integer> pathTo = bfs.pathTo(symbolGraphImpl.index("Kidman, Nicole"));
75+
Iterable<Integer> pathTo = bfs.pathTo(symbolGraphImpl.index("Grant, Cary"));
7676
System.out.println("Bacon, Kevin");
7777
for(int w : pathTo){
7878
System.out.println(symbolGraphImpl.name(w));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
13
2+
22
3+
4 2
4+
2 3
5+
3 2
6+
6 0
7+
0 1
8+
2 0
9+
11 12
10+
12 9
11+
9 10
12+
9 11
13+
7 9
14+
10 12
15+
11 4
16+
4 3
17+
3 5
18+
6 8
19+
8 6
20+
5 4
21+
0 5
22+
6 4
23+
6 9
24+
7 6

DataStructrue/Graph/DirectedGraph.md

+136
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
# Directed Graph
2+
>有向图,边是单向的:每条边所连接的两个顶点都是一个有序对,它的邻接性是单向的。
3+
>我们称一条有向边由第一个顶点指出并指向第二个顶点。
4+
>一个顶点的出度(out degree)由该顶点指出的边的总数。
5+
>一个顶点的入度(in degree)为指向该顶点的边的总数。
6+
>一条有向边的第一个顶点称为它的头,第二个顶点称为它的尾。
7+
8+
### 接口
9+
```Java
10+
public interface Digraph {
11+
/**
12+
* @Description: Number of vertex.
13+
* @return
14+
*/
15+
public int V();
16+
/**
17+
* @Description: Number of edge.
18+
* @return
19+
*/
20+
public int E();
21+
/**
22+
* @Description: Add an edge between two vertex. v->w.
23+
* @param v
24+
* @param w
25+
*/
26+
public void addEdge(int v, int w);
27+
/**
28+
* @Description: Return vertex point out from v.
29+
* @param v
30+
* @return
31+
*/
32+
public Iterable<Integer> adj(int v);
33+
/**
34+
* @Description: Get the reverse graph of current graph.
35+
* @return
36+
*/
37+
public Digraph reverse();
38+
/**
39+
* @Description: Print current graph.
40+
*/
41+
public void display();
42+
}
43+
```
44+
45+
### 实现类
46+
```Java
47+
public class DigraphImpl implements Digraph {
48+
private final int V; //Number of vertex in current digraph
49+
private int E; //Number of edges.
50+
private ListBag<Integer>[] adj;
51+
@SuppressWarnings("unchecked")
52+
public DigraphImpl(int V){
53+
this.V = V;
54+
adj = (ListBag<Integer>[])new ListBag[V];
55+
for (int i = 0; i < V; i++) {
56+
adj[i] = new ListBag<Integer>();
57+
}
58+
this.E = 0;
59+
}
60+
61+
@SuppressWarnings("unchecked")
62+
63+
public DigraphImpl(FileInputStream in) {
64+
Scanner s = null;
65+
s = new Scanner(in);
66+
this.V = s.nextInt();
67+
this.E = s.nextInt();
68+
adj = new ListBag[V];
69+
for (int i = 0; i < V; i++) {
70+
adj[i] = new ListBag<Integer>();
71+
}
72+
for(int i = 0; i < E; i ++){
73+
int v = s.nextInt();
74+
int w = s.nextInt();
75+
adj[v].add(w);
76+
}
77+
s.close();
78+
}
79+
@Override
80+
public int V() { return this.V; }
81+
@Override
82+
public void addEdge(int v, int w) { //只增加一条有向边
83+
adj[v].add(w);
84+
E++;
85+
}
86+
@Override
87+
public int E() { return E; }
88+
@Override
89+
public Iterable<Integer> adj(int v) {
90+
return adj[v];
91+
}
92+
@Override
93+
public Digraph reverse() {
94+
DigraphImpl g = new DigraphImpl(V);
95+
for(int v = 0; v < V; v++)
96+
for(int w : adj(v))
97+
addEdge(w, v);
98+
return g;
99+
}
100+
@Override
101+
public void display() {
102+
for(int i = 0; i < V; i++){
103+
StringBuilder sb = new StringBuilder(i + ": ");
104+
for(int w : adj[i]){
105+
sb.append(w + "|");
106+
}
107+
System.out.println(sb.toString());
108+
}
109+
}
110+
}
111+
```
112+
113+
### 测试
114+
```Java
115+
public static void main(String[] args) throws IOException {
116+
FileInputStream in = new FileInputStream(new File("src/ca/mcmaster/chapter/four/graph/tinyDG.txt"));
117+
Digraph graph = new DigraphImpl(in);
118+
in.close();
119+
graph.display();
120+
}
121+
```
122+
123+
### 结果
124+
>0: 5|1|
125+
>1:
126+
>2: 0|3|
127+
>3: 5|2|
128+
>4: 3|2|
129+
>5: 4|
130+
>6: 9|4|8|0|
131+
>7: 6|9|
132+
>8: 6|
133+
>9: 11|10|
134+
>10: 12|
135+
>11: 4|12|
136+
>12: 9|

0 commit comments

Comments
 (0)