Skip to content

Commit e79a801

Browse files
author
tianqing.liang
committed
'修改'
1 parent 0131b81 commit e79a801

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

25-GraphBascis/AdjMatrix.dart

+36-1
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,51 @@ class AdjMatrix {
2121
AdjMatrix( V,E,List list) {
2222
_V = V;
2323
_E = E ;
24-
24+
if(V < 0) throw Exception("V must be non-negative");
25+
if(E < 0) throw Exception("E must be non-negative");
2526
_adj = List.generate(V, (_)=>List.generate(V,(_)=> 0),growable: false);
2627
for(int i =1;i<E;i++){
2728
int a = int.parse(list[i].split(" ")[0]);
29+
_validateVertex(a);
2830
int b = int.parse(list[i].split(" ")[1]);
31+
_validateVertex(b);
2932
_adj![a][b] = 1;
3033
_adj![b][a] = 1;
3134
}
3235
}
3336

37+
_validateVertex(int v){
38+
if(v < 0 || v >= _V!)
39+
throw new Exception("vertex $v is invalid");
40+
}
41+
42+
int? V(){
43+
return _V;
44+
}
45+
46+
bool hasEdge(int v, int w){
47+
_validateVertex(v);
48+
_validateVertex(w);
49+
return _adj![v][w] == 1;
50+
}
51+
52+
int _degree(int v){
53+
return _adj![v].length;
54+
}
55+
56+
int? E(){
57+
return _E;
58+
}
59+
60+
List<int> adj(int v){
61+
_validateVertex(v);
62+
List<int> res = List.filled(_V!, 0,growable: true);
63+
for(int i = 0; i < _V!; i ++)
64+
if(_adj![v][i] == 1)
65+
res.add(i);
66+
return res;
67+
}
68+
3469
@override
3570
String toString() {
3671
StringBuffer sb = new StringBuffer();

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,4 @@
3636
#### SDK版本
3737
1. 版本:2.12.3
3838
2. Java版本课程地址:[慕课网算法与数据结构](https://class.imooc.com/sale/datastructure)
39+
3. Java版本图课程:[慕课网图论算法](https://coding.imooc.com/class/370.html)

0 commit comments

Comments
 (0)