Skip to content

Commit

Permalink
Changed encoding of vertices in order to speedup GetComponents
Browse files Browse the repository at this point in the history
  • Loading branch information
cem-okulmus authored and cem-okulmus committed Aug 7, 2022
1 parent f8a0de1 commit 92be450
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 24 deletions.
4 changes: 1 addition & 3 deletions balanced.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

// BalancedGo - A research prototype to compute structural decompositions of Conjunctive Queries and CSPs
// via the use of Balanced Separators with a focus on parallelism using the programming language Go.
//
Expand Down Expand Up @@ -32,7 +31,7 @@ import (
"time"

jsoniter "github.com/json-iterator/go"

algo "github.com/cem-okulmus/BalancedGo/algorithms"
"github.com/cem-okulmus/BalancedGo/lib"
)
Expand Down Expand Up @@ -130,7 +129,6 @@ func outputStanza(algorithm string, decomp Decomp, times []labelTime, graph Grap
}
}


func main() {

// ==============================================
Expand Down
18 changes: 10 additions & 8 deletions lib/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,11 @@ func (g Graph) GetComponents(sep Edges, vertices map[int]*disjoint.Element) ([]G
var compsSp = make(map[*disjoint.Element][]Edges)

balsepVert := sep.Vertices()
balSepCache := make(map[int]bool, len(balsepVert))
// var balSepCache

balSepCache := make([]bool, encode)
for _, v := range balsepVert {
balSepCache[v] = true
balSepCache[v-1] = true
}

// Set up the disjoint sets for each node
Expand All @@ -125,11 +127,11 @@ func (g Graph) GetComponents(sep Edges, vertices map[int]*disjoint.Element) ([]G
// Merge together the connected components
for k := range g.Edges.Slice() {
for i := 0; i < len(g.Edges.Slice()[k].Vertices); i++ {
if balSepCache[g.Edges.Slice()[k].Vertices[i]] {
if balSepCache[g.Edges.Slice()[k].Vertices[i]-1] {
continue
}
for j := i + 1; j < len(g.Edges.Slice()[k].Vertices); j++ {
if balSepCache[g.Edges.Slice()[k].Vertices[j]] {
if balSepCache[g.Edges.Slice()[k].Vertices[j]-1] {
continue
}

Expand All @@ -142,11 +144,11 @@ func (g Graph) GetComponents(sep Edges, vertices map[int]*disjoint.Element) ([]G

for k := range g.Special {
for i := 0; i < len(g.Special[k].Vertices())-1; i++ {
if balSepCache[g.Special[k].Vertices()[i]] {
if balSepCache[g.Special[k].Vertices()[i]-1] {
continue
}
for j := i + 1; j < len(g.Special[k].Vertices()); j++ {
if balSepCache[g.Special[k].Vertices()[j]] {
if balSepCache[g.Special[k].Vertices()[j]-1] {
continue
}
disjoint.Union(vertices[g.Special[k].Vertices()[i]], vertices[g.Special[k].Vertices()[j]])
Expand All @@ -163,7 +165,7 @@ func (g Graph) GetComponents(sep Edges, vertices map[int]*disjoint.Element) ([]G
var vertexRep int
found := false
for _, v := range g.Edges.Slice()[i].Vertices {
if balSepCache[v] {
if balSepCache[v-1] {
continue
}
vertexRep = v
Expand All @@ -190,7 +192,7 @@ func (g Graph) GetComponents(sep Edges, vertices map[int]*disjoint.Element) ([]G
var vertexRep int
found := false
for _, v := range g.Special[i].Vertices() {
if balSepCache[v] {
if balSepCache[v-1] {
continue
}
vertexRep = v
Expand Down
45 changes: 32 additions & 13 deletions lib/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"sync"

jsoniter "github.com/json-iterator/go"

"github.com/alecthomas/participle"
"github.com/alecthomas/participle/lexer"
"github.com/alecthomas/participle/lexer/ebnf"
Expand All @@ -33,6 +33,14 @@ type ParseGraph struct {
Encoding map[string]int
}

// TransparentEncoding will overwrite the encoding map in order to print out the exact underlying integer encoding.
// Needs to be called after GetGraph to be effective.
func TransparentEncoding() {
for i := 0; i < encode; i++ {
m[i] = strconv.Itoa(i)
}
}

// GetGraph parses a string in HyperBench format into a graph
func GetGraph(s string) (Graph, ParseGraph) {

Expand Down Expand Up @@ -61,34 +69,47 @@ func GetGraph(s string) (Graph, ParseGraph) {
}
encoding := make(map[int]string)
encode = 1 // initialize to 1

pgraph.Encoding = make(map[string]int)
//fix first numbers for edge names

// first fix the encoding, starting with vertices
for _, e := range pgraph.Edges {
for _, n := range e.Vertices {
_, ok := pgraph.Encoding[n]
if !ok {
pgraph.Encoding[n] = encode
encoding[encode] = n
encode++
}
}

}
for _, e := range pgraph.Edges {
_, ok := pgraph.Encoding[e.Name]
if ok {
log.Panicln("Edge names not unique, not a vald hypergraph!")
log.Panicln("Edge names not unique, not a valid hypergraph!")
}

pgraph.Encoding[e.Name] = encode
encoding[encode] = e.Name
encode++
}

// now create the edges
for _, e := range pgraph.Edges {
var outputEdges []int
for _, n := range e.Vertices {
i, ok := pgraph.Encoding[n]
if ok {
outputEdges = append(outputEdges, i)
} else {
pgraph.Encoding[n] = encode
encoding[encode] = n
outputEdges = append(outputEdges, encode)
encode++
if !ok {
log.Panicln("Vertex not properly encoded, something went wrong with the parsing process!")
}
outputEdges = append(outputEdges, i)

}
edges = append(edges, Edge{Name: pgraph.Encoding[e.Name], Vertices: outputEdges})
}

encode = encode + len(pgraph.Edges)

output.Edges = NewEdges(edges)
m = encoding
return output, pgraph
Expand Down Expand Up @@ -267,7 +288,6 @@ func (n Node) IntoJson() NodeJson {
output.Children = append(output.Children, n.Children[i].IntoJson())
}


return output
}

Expand Down Expand Up @@ -295,7 +315,6 @@ func (n NodeJson) IntoNode(graph Graph, encoding map[string]int) Node {
}
output.Cover = NewEdges(cover)


for i := range n.Children {
output.Children = append(output.Children, n.Children[i].IntoNode(graph, encoding))
}
Expand Down

0 comments on commit 92be450

Please sign in to comment.