github.com/jingcheng-WU/gonum@v0.9.1-0.20210323123734-f1a2a11a8f7b/graph/community/k_communities_test.go (about) 1 // Copyright ©2017 The Gonum Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package community 6 7 import ( 8 "fmt" 9 "reflect" 10 "sort" 11 "testing" 12 13 "github.com/jingcheng-WU/gonum/graph" 14 "github.com/jingcheng-WU/gonum/graph/internal/ordered" 15 "github.com/jingcheng-WU/gonum/graph/simple" 16 ) 17 18 // batageljZaversnikGraph is the example graph from 19 // figure 1 of http://arxiv.org/abs/cs/0310049v1 20 var batageljZaversnikGraph = []intset{ 21 0: nil, 22 23 1: linksTo(2, 3), 24 2: linksTo(4), 25 3: linksTo(4), 26 4: linksTo(5), 27 5: nil, 28 29 6: linksTo(7, 8, 14), 30 7: linksTo(8, 11, 12, 14), 31 8: linksTo(14), 32 9: linksTo(11), 33 10: linksTo(11), 34 11: linksTo(12), 35 12: linksTo(18), 36 13: linksTo(14, 15), 37 14: linksTo(15, 17), 38 15: linksTo(16, 17), 39 16: nil, 40 17: linksTo(18, 19, 20), 41 18: linksTo(19, 20), 42 19: linksTo(20), 43 20: nil, 44 } 45 46 var kCliqueCommunitiesTests = []struct { 47 name string 48 g []intset 49 k int 50 want [][]graph.Node 51 }{ 52 { 53 name: "simple", 54 g: []intset{ 55 0: linksTo(1, 2, 4, 6), 56 1: linksTo(2, 4, 6), 57 2: linksTo(3, 6), 58 3: linksTo(4, 5), 59 4: linksTo(6), 60 5: nil, 61 6: nil, 62 }, 63 k: 3, 64 want: [][]graph.Node{ 65 {simple.Node(0), simple.Node(1), simple.Node(2), simple.Node(4), simple.Node(6)}, 66 {simple.Node(3)}, 67 {simple.Node(5)}, 68 }, 69 }, 70 { 71 name: "Batagelj-Zaversnik Graph", 72 g: batageljZaversnikGraph, 73 k: 3, 74 want: [][]graph.Node{ 75 {simple.Node(0)}, 76 {simple.Node(1)}, 77 {simple.Node(2)}, 78 {simple.Node(3)}, 79 {simple.Node(4)}, 80 {simple.Node(5)}, 81 {simple.Node(6), simple.Node(7), simple.Node(8), simple.Node(14)}, 82 {simple.Node(7), simple.Node(11), simple.Node(12)}, 83 {simple.Node(9)}, 84 {simple.Node(10)}, 85 {simple.Node(13), simple.Node(14), simple.Node(15), simple.Node(17)}, 86 {simple.Node(16)}, 87 {simple.Node(17), simple.Node(18), simple.Node(19), simple.Node(20)}, 88 }, 89 }, 90 { 91 name: "Batagelj-Zaversnik Graph", 92 g: batageljZaversnikGraph, 93 k: 4, 94 want: [][]graph.Node{ 95 {simple.Node(0)}, 96 {simple.Node(1)}, 97 {simple.Node(2)}, 98 {simple.Node(3)}, 99 {simple.Node(4)}, 100 {simple.Node(5)}, 101 {simple.Node(6), simple.Node(7), simple.Node(8), simple.Node(14)}, 102 {simple.Node(9)}, 103 {simple.Node(10)}, 104 {simple.Node(11)}, 105 {simple.Node(12)}, 106 {simple.Node(13)}, 107 {simple.Node(15)}, 108 {simple.Node(16)}, 109 {simple.Node(17), simple.Node(18), simple.Node(19), simple.Node(20)}, 110 }, 111 }, 112 } 113 114 func TestKCliqueCommunities(t *testing.T) { 115 for _, test := range kCliqueCommunitiesTests { 116 g := simple.NewUndirectedGraph() 117 for u, e := range test.g { 118 // Add nodes that are not defined by an edge. 119 if g.Node(int64(u)) == nil { 120 g.AddNode(simple.Node(u)) 121 } 122 for v := range e { 123 g.SetEdge(simple.Edge{F: simple.Node(u), T: simple.Node(v)}) 124 } 125 } 126 got := KCliqueCommunities(test.k, g) 127 128 for _, c := range got { 129 sort.Sort(ordered.ByID(c)) 130 } 131 sort.Sort(ordered.BySliceIDs(got)) 132 133 if !reflect.DeepEqual(got, test.want) { 134 t.Errorf("unexpected k-connected components for %q k=%d:\ngot: %v\nwant:%v", test.name, test.k, got, test.want) 135 } 136 } 137 } 138 139 func BenchmarkKCliqueCommunities(b *testing.B) { 140 for _, test := range kCliqueCommunitiesTests { 141 g := simple.NewUndirectedGraph() 142 for u, e := range test.g { 143 // Add nodes that are not defined by an edge. 144 if g.Node(int64(u)) == nil { 145 g.AddNode(simple.Node(u)) 146 } 147 for v := range e { 148 g.SetEdge(simple.Edge{F: simple.Node(u), T: simple.Node(v)}) 149 } 150 } 151 152 b.Run(fmt.Sprintf("%s-k=%d", test.name, test.k), func(b *testing.B) { 153 var got [][]graph.Node 154 for i := 0; i < b.N; i++ { 155 got = KCliqueCommunities(test.k, g) 156 } 157 if len(got) != len(test.want) { 158 b.Errorf("unexpected k-connected components for %q k=%d:\ngot: %v\nwant:%v", test.name, test.k, got, test.want) 159 } 160 }) 161 } 162 }