@@ -2,6 +2,7 @@ package graph
2
2
3
3
import (
4
4
"container/list"
5
+ "fmt"
5
6
)
6
7
7
8
const (
@@ -374,6 +375,122 @@ func newAdjacencyListWithWeight() *adjacencyListWithWeight {
374
375
return new (adjacencyListWithWeight ).Init ()
375
376
}
376
377
378
+ type flowGraph interface {
379
+ graph
380
+ Cap (edge ) int
381
+ Flow (edge ) int
382
+ AddEdgeWithCap (edge , int )
383
+ AddEdgeWithFlow (edge , int )
384
+ }
385
+
386
+ type adjacencyMatrixWithFlow struct {
387
+ adjacencyMatrix
388
+ cap map [edge ]int
389
+ flow map [edge ]int
390
+ }
391
+
392
+ func (g * adjacencyMatrixWithFlow ) Init () * adjacencyMatrixWithFlow {
393
+ g .adjacencyMatrix .init ()
394
+ g .cap = make (map [edge ]int )
395
+ g .flow = make (map [edge ]int )
396
+ return g
397
+ }
398
+
399
+ func (g * adjacencyMatrixWithFlow ) AddEdgeWithCap (e edge , c int ) {
400
+ g .adjacencyMatrix .AddEdge (e )
401
+ g .cap [e ] = c
402
+ }
403
+
404
+ func (g * adjacencyMatrixWithFlow ) AddEdgeWithFlow (e edge , f int ) {
405
+ if _ , ok := g .cap [e ]; ! ok {
406
+ panic (fmt .Sprintln ("cap of edge " , e , "has not been set yet!" ))
407
+ } else if f > g .cap [e ] {
408
+ panic (fmt .Sprintln ("flow of " , e , "is " , f , ", larger than cap " , g .cap [e ]))
409
+ }
410
+ g .adjacencyMatrix .AddEdge (e )
411
+ g .flow [e ] = f
412
+ }
413
+
414
+ func (g * adjacencyMatrixWithFlow ) Cap (e edge ) int {
415
+ return g .cap [e ]
416
+ }
417
+
418
+ func (g * adjacencyMatrixWithFlow ) Flow (e edge ) int {
419
+ return g .flow [e ]
420
+ }
421
+
422
+ func (g * adjacencyMatrixWithFlow ) DeleteEdge (e edge ) {
423
+ g .adjacencyMatrix .DeleteEdge (e )
424
+ delete (g .cap , e )
425
+ delete (g .flow , e )
426
+ }
427
+
428
+ func (g * adjacencyMatrixWithFlow ) AddEdgeBi (e edge ) {
429
+ panic (fmt .Sprintln ("not valid in flow graph!" ))
430
+ }
431
+
432
+ func (g * adjacencyMatrixWithFlow ) DeleteEdgeBi (e edge ) {
433
+ panic (fmt .Sprintln ("not valid in flow graph!" ))
434
+ }
435
+
436
+ func newAdjacencyMatrixWithFlow () * adjacencyMatrixWithFlow {
437
+ return new (adjacencyMatrixWithFlow ).Init ()
438
+ }
439
+
440
+ type adjacencyListWithFlow struct {
441
+ adjacencyList
442
+ cap map [edge ]int
443
+ flow map [edge ]int
444
+ }
445
+
446
+ func (g * adjacencyListWithFlow ) Init () * adjacencyListWithFlow {
447
+ g .adjacencyList .init ()
448
+ g .cap = make (map [edge ]int )
449
+ g .flow = make (map [edge ]int )
450
+ return g
451
+ }
452
+
453
+ func (g * adjacencyListWithFlow ) AddEdgeWithCap (e edge , c int ) {
454
+ g .adjacencyList .AddEdge (e )
455
+ g .cap [e ] = c
456
+ }
457
+
458
+ func (g * adjacencyListWithFlow ) AddEdgeWithFlow (e edge , f int ) {
459
+ if _ , ok := g .cap [e ]; ! ok {
460
+ panic (fmt .Sprintln ("cap of edge " , e , "has not been set yet!" ))
461
+ } else if f > g .cap [e ] {
462
+ panic (fmt .Sprintln ("flow of " , e , "is " , f , ", larger than cap " , g .cap [e ]))
463
+ }
464
+ g .adjacencyList .AddEdge (e )
465
+ g .flow [e ] = f
466
+ }
467
+
468
+ func (g * adjacencyListWithFlow ) Cap (e edge ) int {
469
+ return g .cap [e ]
470
+ }
471
+
472
+ func (g * adjacencyListWithFlow ) Flow (e edge ) int {
473
+ return g .flow [e ]
474
+ }
475
+
476
+ func (g * adjacencyListWithFlow ) DeleteEdge (e edge ) {
477
+ g .adjacencyList .DeleteEdge (e )
478
+ delete (g .cap , e )
479
+ delete (g .flow , e )
480
+ }
481
+
482
+ func (g * adjacencyListWithFlow ) AddEdgeBi (e edge ) {
483
+ panic (fmt .Sprintln ("not valid in flow graph!" ))
484
+ }
485
+
486
+ func (g * adjacencyListWithFlow ) DeleteEdgeBi (e edge ) {
487
+ panic (fmt .Sprintln ("not valid in flow graph!" ))
488
+ }
489
+
490
+ func newAdjacencyListWithFlow () * adjacencyListWithFlow {
491
+ return new (adjacencyListWithFlow ).Init ()
492
+ }
493
+
377
494
func adjacencyList2AdjacencyMatrix (l * adjacencyList ) * adjacencyMatrix {
378
495
m := newAdjacencyMatrix ()
379
496
for v := l .matrix .frontKey (); v != nil ; v = l .matrix .nextKey (v ) {
@@ -395,6 +512,12 @@ func adjacencyMatrix2AdjacencyList(m *adjacencyMatrix) *adjacencyList {
395
512
}
396
513
397
514
func createGraphByType (g graph ) graph {
515
+ if _ , isList := g .(* adjacencyListWithFlow ); isList {
516
+ return newAdjacencyListWithFlow ()
517
+ }
518
+ if _ , isMatrix := g .(* adjacencyMatrixWithFlow ); isMatrix {
519
+ return newAdjacencyMatrixWithFlow ()
520
+ }
398
521
if _ , isList := g .(* adjacencyListWithWeight ); isList {
399
522
return newAdjacencyListWithWeight ()
400
523
}
0 commit comments