diff --git a/balanced.go b/balanced.go index 7cf247e..073e39d 100644 --- a/balanced.go +++ b/balanced.go @@ -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. // @@ -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" ) @@ -130,7 +129,6 @@ func outputStanza(algorithm string, decomp Decomp, times []labelTime, graph Grap } } - func main() { // ============================================== diff --git a/lib/graph.go b/lib/graph.go index ab1164f..743271a 100644 --- a/lib/graph.go +++ b/lib/graph.go @@ -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 @@ -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 } @@ -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]]) @@ -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 @@ -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 diff --git a/lib/parser.go b/lib/parser.go index 5e3964f..8ca0d95 100644 --- a/lib/parser.go +++ b/lib/parser.go @@ -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" @@ -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) { @@ -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 @@ -267,7 +288,6 @@ func (n Node) IntoJson() NodeJson { output.Children = append(output.Children, n.Children[i].IntoJson()) } - return output } @@ -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)) }