Skip to content

Commit

Permalink
refactory graph data structure, remove createGraphByType
Browse files Browse the repository at this point in the history
  • Loading branch information
Ubuntu committed May 16, 2018
1 parent a36b32d commit f7c90c5
Show file tree
Hide file tree
Showing 19 changed files with 110 additions and 140 deletions.
8 changes: 4 additions & 4 deletions graph/apsp.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func distFloydHandler(array *[][][]int, k, i, j int) {
}

func distFloydWarShall(g weightedGraph) weightedGraph {
newG := createGraphByType(g).(weightedGraph)
newG := newWeightedGraph()
rebuild := func(vertices []interface{}, array [][]int) {
for i := range vertices {
for j := range vertices {
Expand Down Expand Up @@ -98,7 +98,7 @@ func pathFloydWarShall(g weightedGraph) map[interface{}]weightedGraph {
rebuild := func(vertices []interface{}, array [][]int) {
pathForest = make(map[interface{}]weightedGraph)
for i := range vertices {
pathForest[vertices[i]] = createGraphByType(g).(weightedGraph)
pathForest[vertices[i]] = newWeightedGraph()
for j := range vertices {
if pathArray[i][j] < math.MaxInt32 {
//pathArray[i][j] -> j
Expand All @@ -114,7 +114,7 @@ func pathFloydWarShall(g weightedGraph) map[interface{}]weightedGraph {
}

func johnson(g weightedGraph) weightedGraph {
tempG := createGraphByType(g).(weightedGraph)
tempG := newWeightedGraph()
s := struct{}{}
//add e{s,v}, weight is 0
for _, e := range g.AllEdges() {
Expand All @@ -132,7 +132,7 @@ func johnson(g weightedGraph) weightedGraph {
tempG.AddEdgeWithWeight(e, tempG.Weight(e)+spfaE[e.Start].D-spfaE[e.End].D)
}

newG := createGraphByType(g).(weightedGraph)
newG := newWeightedGraph()
//exclude dummy vertex s, and iterate vertices, recover distance
delete(spfaE, s)
for v := range spfaE {
Expand Down
16 changes: 8 additions & 8 deletions graph/apsp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ func apspSetup(g weightedGraph) {
g.AddEdgeWithWeight(edge{4, 3}, 6)
}

func distFloydWarShallGolden(g weightedGraph) weightedGraph {
golden := createGraphByType(g).(weightedGraph)
func distFloydWarShallGolden() weightedGraph {
golden := newWeightedGraph()
for i := 0; i < 5; i++ {
golden.AddVertex(i)
golden.AddEdgeWithWeight(edge{i, i}, 0)
Expand Down Expand Up @@ -57,7 +57,7 @@ func distFloydWarShallGolden(g weightedGraph) weightedGraph {
func pathFloydWarShallGolden(g weightedGraph) map[interface{}]weightedGraph {
golden := make(map[interface{}]weightedGraph)
for i := 0; i < 5; i++ {
golden[i] = createGraphByType(g).(weightedGraph)
golden[i] = newWeightedGraph()
}
golden[0].AddEdgeWithWeight(edge{2, 1}, g.Weight(edge{2, 1}))
golden[0].AddEdgeWithWeight(edge{3, 2}, g.Weight(edge{3, 2}))
Expand Down Expand Up @@ -126,15 +126,15 @@ func checkApspOutOfOrder(t *testing.T, g, gGolden weightedGraph) {
}

func TestDistFloydWarShall(t *testing.T) {
g := newAdjacencyMatrixWithWeight()
g := newWeightedGraph()
apspSetup(g)
newG := distFloydWarShall(g)
goldenG := distFloydWarShallGolden(g)
goldenG := distFloydWarShallGolden()
checkApspOutOfOrder(t, newG, goldenG)
}

func TestPathFloydWarShall(t *testing.T) {
g := newAdjacencyMatrixWithWeight()
g := newWeightedGraph()
apspSetup(g)
newForest := pathFloydWarShall(g)
goldenForest := pathFloydWarShallGolden(g)
Expand All @@ -144,9 +144,9 @@ func TestPathFloydWarShall(t *testing.T) {
}

func TestJohnson(t *testing.T) {
g := newAdjacencyMatrixWithWeight()
g := newWeightedGraph()
apspSetup(g)
newG := johnson(g)
goldenG := distFloydWarShallGolden(g)
goldenG := distFloydWarShallGolden()
checkApspOutOfOrder(t, newG, goldenG)
}
2 changes: 1 addition & 1 deletion graph/bfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func bfsVisit(g graph, s interface{}, handler *bfsVisitHandler) {
}

func bfs(g graph, s interface{}) (bfsGraph graph) {
bfsGraph = createGraphByType(g)
bfsGraph = newGraph()
handler := newBFSVisitHandler()
handler.EdgeHandler = func(start, end *bfsElement) {
bfsGraph.AddEdge(edge{start, end})
Expand Down
4 changes: 2 additions & 2 deletions graph/bfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func bfsSetupGraph(g graph) {
}

func bfsGolden(g graph) (bfsGraph graph) {
bfsGraph = createGraphByType(g)
bfsGraph = newGraph()
vertexes := make(map[interface{}]*bfsElement)
vertexes["s"] = newBFSElement("s")
vertexes["s"].Dist = 0
Expand Down Expand Up @@ -106,7 +106,7 @@ func checkBFSGraphOutOfOrder(t *testing.T, g graph, gGolden graph) {
}

func TestBFS(t *testing.T) {
g := newAdjacencyMatrix()
g := newGraph()
bfsSetupGraph(g)
bfsGraph := bfs(g, "s")
expBfsGraph := bfsGolden(g)
Expand Down
8 changes: 4 additions & 4 deletions graph/bioConnectedComp.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
)

func vertexBCC(g graph) (cuts graph, comps []graph) {
cuts = createGraphByType(g)
cuts = newGraph()
comps = make([]graph, 0, 0)
lows := make(map[interface{}]int)
children := make(map[interface{}]int)
Expand Down Expand Up @@ -36,7 +36,7 @@ func vertexBCC(g graph) (cuts graph, comps []graph) {
if !(p.D == 1 && children[p.V] < 2) {
cuts.AddVertex(p.V)
}
comps = append(comps, createGraphByType(g))
comps = append(comps, newGraph())
curEdge := edge{p.V, v.V}
comps[len(comps)-1].AddEdgeBi(curEdge)
for e := edgeStack.Back().Value; e != curEdge; e = edgeStack.Back().Value {
Expand All @@ -56,7 +56,7 @@ func vertexBCC(g graph) (cuts graph, comps []graph) {
}

func edgeBCC(g graph) (bridges graph, comps []graph) {
bridges = createGraphByType(g)
bridges = newGraph()
comps = make([]graph, 0, 0)
lows := make(map[interface{}]int)
edgeStack := list.New()
Expand All @@ -82,7 +82,7 @@ func edgeBCC(g graph) (bridges graph, comps []graph) {
lows[p.V] = lows[v.V]
}
if lows[v.V] >= p.D {
comp := createGraphByType(g)
comp := newGraph()
curEdge := edge{p.V, v.V}
if lows[v.V] > p.D {
bridges.AddEdgeBi(curEdge)
Expand Down
37 changes: 10 additions & 27 deletions graph/bioConnectedComp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ func bccSetupGraph(g graph) {
g.AddEdgeBi(edge{17, 22})
}

func vertexBCCGolden(g graph) (cuts graph, comps []graph) {
cuts = createGraphByType(g)
func vertexBCCGolden() (cuts graph, comps []graph) {
cuts = newGraph()
comps = make([]graph, 12, 12)
for i := range comps {
comps[i] = createGraphByType(g)
comps[i] = newGraph()
}

cuts.AddVertex(19)
Expand Down Expand Up @@ -89,11 +89,11 @@ func vertexBCCGolden(g graph) (cuts graph, comps []graph) {
return
}

func edgeBCCGolden(g graph) (bridges graph, comps []graph) {
bridges = createGraphByType(g)
func edgeBCCGolden() (bridges graph, comps []graph) {
bridges = newGraph()
comps = make([]graph, 6, 6)
for i := range comps {
comps[i] = createGraphByType(g)
comps[i] = newGraph()
}

bridges.AddEdgeBi(edge{19, 17})
Expand Down Expand Up @@ -162,40 +162,23 @@ func checkBCCGraphOutOfOrder(t *testing.T, g graph, gGloden graph) {
}

func TestVertexBCC(t *testing.T) {
g := newAdjacencyMatrix()
g := newGraph()
bccSetupGraph(g)
cuts, comps := vertexBCC(g)
cutsExp, compsExp := vertexBCCGolden(g)
cutsExp, compsExp := vertexBCCGolden()
checkBCCGraphOutOfOrder(t, cuts, cutsExp)
for i := range comps {
checkBCCGraphOutOfOrder(t, comps[i], compsExp[i])
}
}

func TestEdgeBCC(t *testing.T) {
g := newAdjacencyMatrix()
g := newGraph()
bccSetupGraph(g)
bridges, comps := edgeBCC(g)
bridgesExp, compsExp := edgeBCCGolden(g)
bridgesExp, compsExp := edgeBCCGolden()
checkBCCGraphOutOfOrder(t, bridges, bridgesExp)
for i := range comps {
checkBCCGraphOutOfOrder(t, comps[i], compsExp[i])
}
//for _, v := range bridges.AllEdges() {
// t.Log(v)
// t.Log(v.Start)
// t.Log(v.End)
//}
//for i := range comps {
// t.Log("comps ", i, "vertices:")
// for _, v := range comps[i].AllVertices() {
// t.Log(v)
// }
// t.Log("comps ", i, "edges:")
// for _, v := range comps[i].AllEdges() {
// t.Log(v)
// t.Log(v.Start)
// t.Log(v.End)
// }
//}
}
18 changes: 9 additions & 9 deletions graph/dfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ type dfsForest struct {
Comps map[*dfsElement]*dfsForest
}

func (f *dfsForest) Init(g graph) *dfsForest {
f.Trees = createGraphByType(g)
f.BackEdges = createGraphByType(g)
f.ForwardEdges = createGraphByType(g)
f.CrossEdges = createGraphByType(g)
func (f *dfsForest) Init() *dfsForest {
f.Trees = newGraph()
f.BackEdges = newGraph()
f.ForwardEdges = newGraph()
f.CrossEdges = newGraph()
f.Comps = make(map[*dfsElement]*dfsForest)
return f
}
Expand Down Expand Up @@ -80,7 +80,7 @@ func (f *dfsForest) addCrossEdge(e edge) {
func (f *dfsForest) getRoot(e *dfsElement) *dfsElement {
root := e.FindRoot()
if _, ok := f.Comps[root]; !ok {
f.Comps[root] = newDFSForest(f.Trees)
f.Comps[root] = newDFSForest()
}
return root
}
Expand Down Expand Up @@ -150,8 +150,8 @@ func (f *dfsForest) AllEdges() []edge {
return edges
}

func newDFSForest(g graph) *dfsForest {
return new(dfsForest).Init(g)
func newDFSForest() *dfsForest {
return new(dfsForest).Init()
}

type dfsVisitHandler struct {
Expand Down Expand Up @@ -243,7 +243,7 @@ func dfsVisit(g graph, v interface{}, handler *dfsVisitHandler) {
}

func dfs(g graph, sorter func([]interface{})) (dfsForest *dfsForest) {
dfsForest = newDFSForest(g)
dfsForest = newDFSForest()
handler := newDFSVisitHandler()
handler.BeforeDfsHandler = func(v *dfsElement) {
dfsForest.AddVertex(v)
Expand Down
4 changes: 2 additions & 2 deletions graph/dfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func dfsSetupGraph(g graph) {
}

func dfsGolden(g graph) *dfsForest {
dfsForest := newDFSForest(g)
dfsForest := newDFSForest()
vertexes := make(map[interface{}]*dfsElement)

vertexes["u"] = newDFSElement("u")
Expand Down Expand Up @@ -89,7 +89,7 @@ func dfsGolden(g graph) *dfsForest {
}

func TestDFS(t *testing.T) {
g := newAdjacencyMatrix()
g := newGraph()
dfsSetupGraph(g)
dfsGraph := dfs(g, func(vertices []interface{}) {
sort.Slice(vertices, func(i, j int) bool {
Expand Down
16 changes: 8 additions & 8 deletions graph/eulerCircuit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func udEulerCircuitSetup(g graph) {
}

func TestDiEulerCircuitOK(t *testing.T) {
g := newAdjacencyMatrix()
g := newGraph()
diEulerCircuitSetup(g)
for i := 0; i < 3; i++ {
path := eulerCircuit(g)
Expand All @@ -41,7 +41,7 @@ func TestDiEulerCircuitOK(t *testing.T) {
}

func TestDiEulerCircuitWithSingleVertex(t *testing.T) {
g := newAdjacencyMatrix()
g := newGraph()
diEulerCircuitSetup(g)
g.AddVertex(6)
path := eulerCircuit(g)
Expand All @@ -52,7 +52,7 @@ func TestDiEulerCircuitWithSingleVertex(t *testing.T) {
}

func TestDiEulerCircuitWithSingleVertexLoop(t *testing.T) {
g := newAdjacencyMatrix()
g := newGraph()
diEulerCircuitSetup(g)
g.AddEdgeBi(edge{6, 6})
path := eulerCircuit(g)
Expand All @@ -63,7 +63,7 @@ func TestDiEulerCircuitWithSingleVertexLoop(t *testing.T) {
}

func TestDiEulerCircuitWithNonCircuit(t *testing.T) {
g := newAdjacencyMatrix()
g := newGraph()
diEulerCircuitSetup(g)
g.AddEdge(edge{6, 1})
path := eulerCircuit(g)
Expand All @@ -72,7 +72,7 @@ func TestDiEulerCircuitWithNonCircuit(t *testing.T) {
t.Fail()
}

g = newAdjacencyMatrix()
g = newGraph()
diEulerCircuitSetup(g)
g.AddEdge(edge{1, 6})
path = eulerCircuit(g)
Expand All @@ -83,7 +83,7 @@ func TestDiEulerCircuitWithNonCircuit(t *testing.T) {
}

func TestUdEulerCircuitOK(t *testing.T) {
g := newAdjacencyMatrix()
g := newGraph()
udEulerCircuitSetup(g)
path := eulerCircuit(g)
pathExp := eulerCircuitGolden()
Expand All @@ -94,7 +94,7 @@ func TestUdEulerCircuitOK(t *testing.T) {
}

func TestUdEulerCircuitWithSingleVertex(t *testing.T) {
g := newAdjacencyMatrix()
g := newGraph()
udEulerCircuitSetup(g)
g.AddVertex(6)
path := eulerCircuit(g)
Expand All @@ -105,7 +105,7 @@ func TestUdEulerCircuitWithSingleVertex(t *testing.T) {
}

func TestUdEulerCircuitWithNonCircuit(t *testing.T) {
g := newAdjacencyMatrix()
g := newGraph()
udEulerCircuitSetup(g)
g.AddEdgeBi(edge{1, 6})
path := eulerCircuit(g)
Expand Down
2 changes: 1 addition & 1 deletion graph/flowGraph.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func updateFlow(rg, g flowGraph, rc int, edges []edge) {
}

func edmondsKarp(g flowGraph, s interface{}, t interface{}) {
residualG := createGraphByType(g).(flowGraph)
residualG := newFlowGraph()
for _, e := range g.AllEdges() {
g.AddEdgeWithFlow(e, 0)
residualG.AddEdgeWithCap(e, g.Cap(e))
Expand Down
4 changes: 2 additions & 2 deletions graph/flowGraph_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func flowGraphSetup(g flowGraph) {
}

func flowGraphGolden(g flowGraph) flowGraph {
flowG := createGraphByType(g).(flowGraph)
flowG := newFlowGraph()
for _, e := range g.AllEdges() {
flowG.AddEdgeWithCap(e, g.Cap(e))
}
Expand Down Expand Up @@ -79,7 +79,7 @@ func checkFlowGraphOutOfOrder(t *testing.T, g, gGolden flowGraph) {
}

func TestEdmondsKarp(t *testing.T) {
g := newAdjacencyMatrixWithFlow()
g := newFlowGraph()
flowGraphSetup(g)
edmondsKarp(g, "s", "t")
gGolden := flowGraphGolden(g)
Expand Down
Loading

0 comments on commit f7c90c5

Please sign in to comment.