1
1
package model
2
2
3
- import "fmt"
3
+ import (
4
+ "fmt"
5
+ )
4
6
5
7
type Graph interface {
6
8
AddEdge (edge Edge )
7
9
AddNode (node Node )
8
10
GetEdgeTuples () []Edge
9
- Sample (sampler * SamplingStrategy , samplingRate float32 ) Graph
11
+ Sample (sampler SamplingStrategy , samplingRate float32 ) UndirectedGraph
12
+ NodeDegree (node Node ) int
13
+ NumberOfEdges () int
10
14
}
11
15
12
- type UndirectedGraph struct {
13
- Nodes map [int ]bool
14
- Edges map [int ][]int
15
- }
16
-
17
- func (g UndirectedGraph ) String () string {
18
- return fmt .Sprintf ("graph has %d nodes and %d edges" , len (g .Nodes ), len (g .Edges ))
19
- }
16
+ type Node int
20
17
21
18
type Edge struct {
22
19
Node1 Node
23
20
Node2 Node
24
21
}
25
22
26
- type Node struct {
27
- NodeId int
23
+ type UndirectedGraph struct {
24
+ Nodes map [Node ]bool
25
+ Edges map [Node ][]Node
26
+ }
27
+
28
+ func (g UndirectedGraph ) String () string {
29
+ return fmt .Sprintf ("graph has %d nodes and %d edges. Nodes: %v; Edges: %v" , len (g .Nodes ), len (g .Edges ), g .Nodes , g .Edges )
28
30
}
29
31
30
32
func (g * UndirectedGraph ) AddEdge (edge Edge ) {
33
+ if g .Edges == nil {
34
+ g .Edges = make (map [Node ][]Node )
35
+ }
31
36
g .AddNode (edge .Node1 )
32
37
g .AddNode (edge .Node2 )
33
- g .Edges [edge .Node1 .NodeId ] = append (g .Edges [edge .Node1 .NodeId ], edge .Node2 .NodeId )
38
+ g .Edges [edge .Node1 ] = append (g .Edges [edge .Node1 ], edge .Node2 )
39
+ g .Edges [edge .Node2 ] = append (g .Edges [edge .Node2 ], edge .Node1 )
34
40
}
35
41
36
42
func (g * UndirectedGraph ) AddNode (node Node ) {
37
- g .Nodes [node .NodeId ] = true
43
+ if g .Nodes == nil {
44
+ g .Nodes = make (map [Node ]bool )
45
+ }
46
+ g .Nodes [node ] = true
38
47
}
39
48
40
49
func (g * UndirectedGraph ) AddEdgesFromIntTupleList (edges [][2 ]int ) {
41
50
for _ , nodes := range edges {
42
- g .AddEdge (Edge {Node { nodes [0 ]} , Node { nodes [1 ]} })
51
+ g .AddEdge (Edge {Node ( nodes [0 ]) , Node ( nodes [1 ]) })
43
52
}
44
53
}
45
54
46
- func (g * UndirectedGraph ) AddEdgesFromIntEdgeList (sourceNode int , edges []int ) {
55
+ func (g * UndirectedGraph ) AddEdgesFromIntEdgeList (sourceNode Node , edges []Node ) {
47
56
for _ , node := range edges {
48
- g .AddEdge (Edge {Node { sourceNode }, Node { node } })
57
+ g .AddEdge (Edge {sourceNode , node })
49
58
}
50
59
}
51
60
52
- func (g * UndirectedGraph ) AddNodes (nodes map [ int ] bool ) {
53
- for node := range nodes {
54
- g .AddNode (Node { node } )
61
+ func (g * UndirectedGraph ) AddNodes (nodes [] Node ) {
62
+ for _ , node := range nodes {
63
+ g .AddNode (node )
55
64
}
56
65
}
57
66
67
+ // NodeDegree returns the degree (number of incident edges) of the specified node in the graph.
68
+ func (g * UndirectedGraph ) NodeDegree (node Node ) int {
69
+ // Check if the node exists in the graph
70
+ if _ , exists := g .Nodes [node ]; ! exists {
71
+ return 0 // Node not found, degree is 0
72
+ }
73
+
74
+ // Retrieve the neighbors of the node and return the degree
75
+ return len (g .Edges [node ])
76
+ }
77
+
58
78
// todo suggest rename to GetEdges
59
79
func (g * UndirectedGraph ) GetEdgeTuples () []Edge {
60
80
var edges []Edge
61
81
for node1 , array := range g .Edges {
62
- for node2 := range array {
63
- edges = append (edges , Edge {Node { node1 }, Node { node2 } })
82
+ for _ , node2 := range array {
83
+ edges = append (edges , Edge {node1 , node2 })
64
84
}
65
85
}
66
86
return edges
@@ -69,3 +89,16 @@ func (g *UndirectedGraph) GetEdgeTuples() []Edge {
69
89
func (g * UndirectedGraph ) Sample (sampler SamplingStrategy , samplingRate float32 ) UndirectedGraph {
70
90
return sampler .Sample (g , samplingRate )
71
91
}
92
+
93
+ // NumberOfEdges returns the total number of edges in the undirected graph.
94
+ func (g * UndirectedGraph ) NumberOfEdges () int {
95
+ totalEdges := 0
96
+
97
+ // Iterate over each node's neighbors and count the unique edges
98
+ for _ , neighbors := range g .Edges {
99
+ totalEdges += len (neighbors )
100
+ }
101
+
102
+ // Divide by 2 to account for the fact that each edge is counted twice (undirected graph)
103
+ return totalEdges / 2
104
+ }
0 commit comments