Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 47 additions & 3 deletions model/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ Example:

fmt.Println(undirectedGraph.Edges) // Output: map[1:[2] 2:[1]]
*/

func (g *UndirectedGraph) AddEdge(edge Edge) {
// Ensure the existence of the Edges map
if g.Edges == nil {
Expand All @@ -136,9 +137,11 @@ func (g *UndirectedGraph) AddEdge(edge Edge) {
g.AddNode(edge.Node1)
g.AddNode(edge.Node2)

// Add the edge to the Edges map
g.Edges[edge.Node1] = append(g.Edges[edge.Node1], edge.Node2)
g.Edges[edge.Node2] = append(g.Edges[edge.Node2], edge.Node1)
// Only add if edge doesn’t already exist (undirected)
if !g.HasEdge(edge.Node1, edge.Node2) {
g.Edges[edge.Node1] = append(g.Edges[edge.Node1], edge.Node2)
g.Edges[edge.Node2] = append(g.Edges[edge.Node2], edge.Node1)
}
}

func (g *UndirectedGraph) DFS(startNode Node) *UndirectedGraph {
Expand Down Expand Up @@ -371,6 +374,47 @@ func (g *UndirectedGraph) RemoveEdge(edge Edge) {
}
}

/*
HasEode checks if the UndirectedGraph contains a specific edge.

Parameters:
- edge: .

Returns:
- bool: True if the edge exists in the graph, otherwise false.

Description:
The function returns true if the specified edge is present in the UndirectedGraph, indicating its existence in the graph. Otherwise, it returns false.

Example:

undirectedGraph := UndirectedGraph{
Nodes: map[Node]bool{
1: true,
2: true,
3: true,
4: true,
},
Edges: map[Node][]Node{
1: {2, 3, 4},
2: {1, 3},
3: {1, 2, 4},
4: {1, 3},
},
}

result1 := undirectedGraph.HasEdge(2,3) // true
result2 := undirectedGraph.HasNode(4,2) // false
*/
func (g *UndirectedGraph) HasEdge(u, v Node) bool {
for _, nb := range g.Edges[u] {
if nb == v {
return true
}
}
return false
}

/*
RemoveNode removes a node from the UndirectedGraph and all associated edges.

Expand Down
86 changes: 80 additions & 6 deletions model/sampling.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package model

import (
"fmt"
"math"
"math/rand"
"sort"

Expand Down Expand Up @@ -238,6 +239,7 @@ type PreservationRandomWalkSampling struct{ ISamplingStrategy }
type PreservationRandomWalkWithJumpSampling struct{ ISamplingStrategy }
type PreservationRandomWalkWithRestartSampling struct{ ISamplingStrategy }
type PreservationTopKEdgeSampling struct{ ISamplingStrategy }
type PreservationTopRatioEdgeSampling struct{ ISamplingStrategy }

func (strategy *PreservationRandomNodeSampling) Sample(g UndirectedGraph, sampledGraphSizeRatio float32) (UndirectedGraph, error) {
ng := UndirectedGraph{
Expand Down Expand Up @@ -556,6 +558,68 @@ func (strategy *PreservationRandomWalkWithJumpSampling) Sample(graph UndirectedG
return ng, nil
}

func (strategy *PreservationTopRatioEdgeSampling) Sample(
g UndirectedGraph,
sampleNeighborRatio float32, // e.g., 0.3 keeps top 30% per node
) (UndirectedGraph, error) {

ng := UndirectedGraph{
Nodes: make(map[Node]bool),
Edges: make(map[Node][]Node),
}

for u := range g.Nodes {
nbrs := g.Edges[u]
if len(nbrs) == 0 {
continue
}

// Build (neighbor, weight) where weight = neighbor's degree
type we struct {
v Node
weight float32
}
ws := make([]we, 0, len(nbrs))
for _, v := range nbrs {
if v == u { // skip self-loops if any
continue
}
ws = append(ws, we{
v: v,
weight: float32(g.NodeDegree(v)),
})
}
if len(ws) == 0 {
continue
}

// Sort by weight DESC (keep highest-degree neighbors)
sort.SliceStable(ws, func(i, j int) bool { return ws[i].weight > ws[j].weight })

// Top-K per node, where K = ceil(ratio * deg(u))
k := int(math.Ceil(float64(sampleNeighborRatio) * float64(len(ws))))
if k < 0 {
k = 0
}
if k > len(ws) {
k = len(ws)
}
if k == 0 {
continue
}

// Add the top-K edges (once for undirected graphs)
for i := 0; i < k; i++ {
v := ws[i].v
if !ng.HasEdge(u, v) {
ng.AddEdge(Edge{Node1: u, Node2: v})
}
}
}

return ng, nil
}

func (strategy *PreservationTopKEdgeSampling) Sample(g UndirectedGraph, sampledGraphSizeRatio float32) (UndirectedGraph, error) {
// TODO: where do we get the K parameter?
// shall we change the interface to passing sampling parameters object?
Expand All @@ -568,6 +632,9 @@ func (strategy *PreservationTopKEdgeSampling) Sample(g UndirectedGraph, sampledG

for nodeId := range g.Nodes {
neighbours := g.Edges[nodeId]
if len(neighbours) == 0 {
continue
}
weightedNeighbours := make([]WeightedElement, 0)
for k := 0; k < len(neighbours); k++ {
weightedNeighbours = append(weightedNeighbours, WeightedElement{
Expand All @@ -576,13 +643,20 @@ func (strategy *PreservationTopKEdgeSampling) Sample(g UndirectedGraph, sampledG
})
}
sort.SliceStable(weightedNeighbours, func(i, j int) bool {
return weightedNeighbours[i].Weight < weightedNeighbours[j].Weight
return weightedNeighbours[i].Weight > weightedNeighbours[j].Weight
})
for idx := 0; idx < topK; idx++ {
ng.AddEdge(Edge{
Node1: nodeId,
Node2: weightedNeighbours[idx].Payload.(Node),
})

topK_adjusted := min(topK, len(neighbours))

for idx := 0; idx < topK_adjusted; idx++ {
nodeId_1 := nodeId
nodeId_2 := weightedNeighbours[idx].Payload.(Node)
if !ng.HasEdge(nodeId_1, nodeId_2) {
ng.AddEdge(Edge{
Node1: nodeId_1,
Node2: nodeId_2,
})
}
}
}
return ng, nil
Expand Down
Loading