Skip to content

Commit

Permalink
Merge pull request #16 from LdDl/new-export-import
Browse files Browse the repository at this point in the history
replace int64 with ContractionPath + eliminate duplicates from contra…
  • Loading branch information
LdDl authored Mar 31, 2021
2 parents e850b66 + b55c3b2 commit 38afed2
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 65 deletions.
2 changes: 1 addition & 1 deletion bidirectional_ch.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func (graph *Graph) ComputePath(middleID int64, prevF, prevR map[int64]int64) []
for e := l.Front(); e.Next() != nil; e = e.Next() {
if contractedNode, ok2 := graph.contracts[e.Value.(int64)][e.Next().Value.(int64)]; ok2 {
ok = true
l.InsertAfter(contractedNode, e)
l.InsertAfter(contractedNode.ViaVertex, e)
}
}
}
Expand Down
29 changes: 23 additions & 6 deletions contraction.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@ func (graph *Graph) callNeighbors(inEdges, outEdges []int64) {
}
}

// ContractionPath
//
// ViaVertex - ID of vertex through which the contraction exists
// Cost - summary cost of path between two vertices
//
type ContractionPath struct {
ViaVertex int64
Cost float64
}

// contractNode
//
// vertex Vertex to be contracted
Expand Down Expand Up @@ -96,17 +106,24 @@ func (graph *Graph) contractNode(vertex *Vertex, contractID int64) {
if graph.Vertices[outVertex].contracted {
continue
}
if graph.Vertices[outVertex].distance.contractID != contractID || graph.Vertices[outVertex].distance.sourceID != int64(i) || graph.Vertices[outVertex].distance.distance > incost+outcost {
summaryCost := incost + outcost
if graph.Vertices[outVertex].distance.contractID != contractID || graph.Vertices[outVertex].distance.sourceID != int64(i) || graph.Vertices[outVertex].distance.distance > summaryCost {
if _, ok := graph.contracts[inVertex]; !ok {
graph.contracts[inVertex] = make(map[int64]int64)
graph.contracts[inVertex][outVertex] = vertex.vertexNum
graph.contracts[inVertex] = make(map[int64]*ContractionPath)
graph.contracts[inVertex][outVertex] = &ContractionPath{
ViaVertex: vertex.vertexNum,
Cost: summaryCost,
}
} else {
graph.contracts[inVertex][outVertex] = vertex.vertexNum
graph.contracts[inVertex][outVertex] = &ContractionPath{
ViaVertex: vertex.vertexNum,
Cost: summaryCost,
}
}
graph.Vertices[inVertex].outEdges = append(graph.Vertices[inVertex].outEdges, outVertex)
graph.Vertices[inVertex].outECost = append(graph.Vertices[inVertex].outECost, incost+outcost)
graph.Vertices[inVertex].outECost = append(graph.Vertices[inVertex].outECost, summaryCost)
graph.Vertices[outVertex].inEdges = append(graph.Vertices[outVertex].inEdges, inVertex)
graph.Vertices[outVertex].inECost = append(graph.Vertices[outVertex].inECost, incost+outcost)
graph.Vertices[outVertex].inECost = append(graph.Vertices[outVertex].inECost, summaryCost)
}
}
}
Expand Down
74 changes: 22 additions & 52 deletions export.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,65 +88,15 @@ func (graph *Graph) ExportToFile(fname string) error {
return err
}

// Write reference information about "incoming" adjacent vertices
incomingNeighbors := graph.Vertices[i].inEdges
incomingCosts := graph.Vertices[i].inECost
for j := 0; j < len(incomingNeighbors); j++ {
fromVertexExternal := graph.Vertices[incomingNeighbors[j]].Label
fromVertexInternal := incomingNeighbors[j]
cost := incomingCosts[j]
if v, ok := graph.contracts[fromVertexInternal][currentVertexInternal]; ok {
isContractExternal := graph.Vertices[v].Label
isContractInternal := v
err = writerContractions.Write([]string{
fmt.Sprintf("%d", fromVertexExternal),
fmt.Sprintf("%d", currentVertexExternal),
fmt.Sprintf("%d", fromVertexInternal),
fmt.Sprintf("%d", currentVertexInternal),
strconv.FormatFloat(cost, 'f', -1, 64),
fmt.Sprintf("%d", isContractExternal),
fmt.Sprintf("%d", isContractInternal),
})
if err != nil {
return err
}
} else {
err = writer.Write([]string{
fmt.Sprintf("%d", fromVertexExternal),
fmt.Sprintf("%d", currentVertexExternal),
fmt.Sprintf("%d", fromVertexInternal),
fmt.Sprintf("%d", currentVertexInternal),
strconv.FormatFloat(cost, 'f', -1, 64),
})
if err != nil {
return err
}
}
}

// Write reference information about "outcoming" adjacent vertices
// Why don't write info about "incoming" adjacent vertices also? Because all edges will be covered due the loop iteration mechanism
outcomingNeighbors := graph.Vertices[i].outEdges
outcomingCosts := graph.Vertices[i].outECost
for j := 0; j < len(outcomingNeighbors); j++ {
toVertexExternal := graph.Vertices[outcomingNeighbors[j]].Label
toVertexInternal := outcomingNeighbors[j]
cost := outcomingCosts[j]
if v, ok := graph.contracts[currentVertexInternal][toVertexInternal]; ok {
isContractExternal := graph.Vertices[v].Label
isContractInternal := v
err = writerContractions.Write([]string{
fmt.Sprintf("%d", currentVertexExternal),
fmt.Sprintf("%d", toVertexExternal),
fmt.Sprintf("%d", currentVertexInternal),
fmt.Sprintf("%d", toVertexInternal),
strconv.FormatFloat(cost, 'f', -1, 64),
fmt.Sprintf("%d", isContractExternal),
fmt.Sprintf("%d", isContractInternal),
})
if err != nil {
return err
}
} else {
if _, ok := graph.contracts[currentVertexInternal][toVertexInternal]; !ok {
err = writer.Write([]string{
fmt.Sprintf("%d", currentVertexExternal),
fmt.Sprintf("%d", toVertexExternal),
Expand All @@ -161,5 +111,25 @@ func (graph *Graph) ExportToFile(fname string) error {
}
}

// Write reference information about contractions
for sourceVertexInternal, to := range graph.contracts {
sourceVertexExternal := graph.Vertices[sourceVertexInternal].Label
for targetVertexInternal, viaNodeInternal := range to {
targetVertexExternal := graph.Vertices[targetVertexInternal].Label
viaNodeExternal := graph.Vertices[viaNodeInternal.ViaVertex].Label
err = writerContractions.Write([]string{
fmt.Sprintf("%d", sourceVertexExternal),
fmt.Sprintf("%d", targetVertexExternal),
fmt.Sprintf("%d", sourceVertexInternal),
fmt.Sprintf("%d", targetVertexInternal),
strconv.FormatFloat(viaNodeInternal.Cost, 'f', -1, 64),
fmt.Sprintf("%d", viaNodeExternal),
fmt.Sprintf("%d", viaNodeInternal.ViaVertex),
})
if err != nil {
return err
}
}
}
return err
}
6 changes: 3 additions & 3 deletions graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type Graph struct {
Vertices []*Vertex
nodeOrdering []int64

contracts map[int64]map[int64]int64
contracts map[int64]map[int64]*ContractionPath
restrictions map[int64]map[int64]int64

frozen bool
Expand All @@ -47,7 +47,7 @@ func (graph *Graph) CreateVertex(label int64) error {
graph.mapping = make(map[int64]int64)
}
if graph.contracts == nil {
graph.contracts = make(map[int64]map[int64]int64)
graph.contracts = make(map[int64]map[int64]*ContractionPath)
}

if _, ok := graph.mapping[label]; !ok {
Expand Down Expand Up @@ -78,7 +78,7 @@ func (graph *Graph) AddVertex(labelExternal, labelInternal int64) error {
graph.mapping = make(map[int64]int64)
}
if graph.contracts == nil {
graph.contracts = make(map[int64]map[int64]int64)
graph.contracts = make(map[int64]map[int64]*ContractionPath)
}

if _, ok := graph.mapping[labelExternal]; !ok {
Expand Down
12 changes: 9 additions & 3 deletions import.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,16 @@ func ImportFromFile(edgesFname, verticesFname, contractionsFname string) (*Graph
return nil, errors.Wrap(err, fmt.Sprintf("Can't add edge with source_internal_ID = '%d' and target_internal_ID = '%d'", sourceExternal, targetExternal))
}
if _, ok := graph.contracts[sourceInternal]; !ok {
graph.contracts[sourceInternal] = make(map[int64]int64)
graph.contracts[sourceInternal][targetInternal] = contractionInternal
graph.contracts[sourceInternal] = make(map[int64]*ContractionPath)
graph.contracts[sourceInternal][targetInternal] = &ContractionPath{
ViaVertex: contractionInternal,
Cost: weight,
}
}
graph.contracts[sourceInternal][targetInternal] = &ContractionPath{
ViaVertex: contractionInternal,
Cost: weight,
}
graph.contracts[sourceInternal][targetInternal] = contractionInternal
}
return &graph, nil
}
Expand Down

0 comments on commit 38afed2

Please sign in to comment.