@@ -9,7 +9,7 @@ private void adjIteratorMain() {
99 Random random = new Random ();
1010
1111 // weight.SparseGraph
12- SparseGraph < Integer > sparseGraph = new SparseGraph <> (N , false );
12+ SparseGraph sparseGraph = new SparseGraph (N , false );
1313 for (int i = 0 ; i < M ; i ++) {
1414 int a = random .nextInt (N );
1515 int b = random .nextInt (N );
@@ -31,7 +31,7 @@ private void adjIteratorMain() {
3131 System .out .println ("----------分割线----------" );
3232
3333 // weight.DenseGraph
34- DenseGraph < Integer > denseGraph = new DenseGraph <> (N , false );
34+ DenseGraph denseGraph = new DenseGraph (N , false );
3535 for (int i = 0 ; i < M ; i ++) {
3636 int a = random .nextInt (N );
3737 int b = random .nextInt (N );
@@ -57,7 +57,7 @@ private void iteratorMain() {
5757 Random random = new Random ();
5858
5959 // weight.SparseGraph
60- SparseGraph < Integer > sparseGraph = new SparseGraph <> (N , false );
60+ SparseGraph sparseGraph = new SparseGraph (N , false );
6161 for (int i = 0 ; i < M ; i ++) {
6262 int a = random .nextInt (N );
6363 int b = random .nextInt (N );
@@ -69,8 +69,8 @@ private void iteratorMain() {
6969 // O(E)
7070 for (int v = 0 ; v < N ; v ++) {
7171 System .out .print (v + " : " );
72- Iterable <Integer > iterator = sparseGraph .adj (v );
73- for (Integer w : iterator ) {
72+ Iterable <Edge > iterator = sparseGraph .adj (v );
73+ for (Edge w : iterator ) {
7474 System .out .print (w + " " );
7575 }
7676 System .out .println ();
@@ -79,7 +79,7 @@ private void iteratorMain() {
7979 System .out .println ("----------分割线----------" );
8080
8181 // weight.DenseGraph
82- DenseGraph < Integer > denseGraph = new DenseGraph <> (N , false );
82+ DenseGraph denseGraph = new DenseGraph (N , false );
8383 for (int i = 0 ; i < M ; i ++) {
8484 int a = random .nextInt (N );
8585 int b = random .nextInt (N );
@@ -90,18 +90,91 @@ private void iteratorMain() {
9090 // O(V^2)
9191 for (int v = 0 ; v < N ; v ++) {
9292 System .out .print (v + " : " );
93- Iterable <Integer > iterator = denseGraph .adj (v );
94- for (Integer w : iterator ) {
93+ Iterable <Edge > iterator = denseGraph .adj (v );
94+ for (Edge w : iterator ) {
9595 System .out .print (w + " " );
9696 }
9797 System .out .println ();
9898 }
9999 }
100100
101+ public void lazyPrimMst () {
102+ int N = 10 ;
103+ int M = 10 ;
104+ Random random = new Random ();
105+
106+ // weight.SparseGraph
107+ SparseGraph sparseGraph = new SparseGraph (N , false );
108+ for (int i = 0 ; i < M ; i ++) {
109+ int a = random .nextInt (N );
110+ int b = random .nextInt (N );
111+ double weight = Math .random ();
112+ sparseGraph .addEdge (a , b , weight );
113+ }
114+ System .out .println (sparseGraph );
115+
116+
117+ // O(E)
118+ // 打印所有边
119+ for (int v = 0 ; v < N ; v ++) {
120+ System .out .print (v + " : " );
121+ Iterable <Edge > iterator = sparseGraph .adj (v );
122+ for (Edge w : iterator ) {
123+ System .out .print (w + " " );
124+ }
125+ System .out .println ();
126+ }
127+
128+ // 打印最小生成树
129+ LazyPrimMST lazySparseGraphPrimMST = new LazyPrimMST (sparseGraph );
130+ Iterable <Edge > sparseGraphIterator = lazySparseGraphPrimMST .mstEdges ();
131+ for (Edge w : sparseGraphIterator ) {
132+ System .out .print (w + " " );
133+ }
134+ System .out .println ();
135+ System .out .println (lazySparseGraphPrimMST .reuslt ());
136+
137+
138+ System .out .println ("----------分割线----------" );
139+
140+ // weight.DenseGraph
141+ DenseGraph denseGraph = new DenseGraph (N , false );
142+ for (int i = 0 ; i < M ; i ++) {
143+ int a = random .nextInt (N );
144+ int b = random .nextInt (N );
145+ double weight = Math .random ();
146+ denseGraph .addEdge (a , b , weight );
147+ }
148+ System .out .println (denseGraph );
149+
150+ // O(V^2)
151+ // 打印所有边
152+ for (int v = 0 ; v < N ; v ++) {
153+ System .out .print (v + " : " );
154+ Iterable <Edge > iterator = denseGraph .adj (v );
155+ for (Edge w : iterator ) {
156+ System .out .print (w + " " );
157+ }
158+ System .out .println ();
159+ }
160+
161+ // 打印最小生成树
162+ LazyPrimMST lazyDenseGraphPrimMST = new LazyPrimMST (denseGraph );
163+ Iterable <Edge > denseGraphIterator = lazyDenseGraphPrimMST .mstEdges ();
164+ for (Edge w : denseGraphIterator ) {
165+ System .out .print (w + " " );
166+ }
167+ System .out .println ();
168+ System .out .println (lazyDenseGraphPrimMST .reuslt ());
169+
170+ }
171+
172+
101173 public static void main (String [] args ) {
102174 Main main = new Main ();
103175// main.adjIteratorMain();
104- main .iteratorMain ();
176+ // main.iteratorMain();
177+ main .lazyPrimMst ();
105178
106179 }
107180}
0 commit comments