-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
168 lines (140 loc) · 4.12 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package main
import (
"bufio"
"fmt"
"os"
"sort"
"strconv"
"strings"
"time"
"gitlab.com/hcmi/graph-analyzer/adjmat"
"gonum.org/v1/gonum/blas/blas32"
blas_netlib "gonum.org/v1/netlib/blas/netlib"
)
type void struct{}
const modeMessage = `Modes are (comp)onent, (dia)meter, (dia)meter (fr)om a list of nodes, (dist)ance distribution,
(med)ian distance from all connected nodes.`
// ex ./graph-analyzer diafr data/monday.txt data/monday_giant_component.txt
func main() {
// TODO add help function
if len(os.Args) < 3 {
fmt.Printf("Usage: %s [mode] [file]\n", os.Args[0])
fmt.Println(modeMessage)
return
}
taskSelection := os.Args[1]
timeStart := time.Now()
fmt.Printf("Started %s at %v.\n", taskSelection, timeStart)
// use the c based library
blas32.Use(blas_netlib.Implementation{})
// do what the user said to
t, matrix := newTask()
t.run(matrix)
fmt.Printf("Done with task %s (%v).\n", taskSelection, time.Now().Sub(timeStart))
}
func toAdjacencyMatrix(input [][]uint16) *adjmat.AdjacencyMatrix {
nodes := len(input)
data := make([]float32, nodes*nodes)
for i := 0; i < nodes; i++ {
for k := 0; k < nodes; k++ {
data[i*nodes+k] = float32(input[i][k])
}
}
return adjmat.New(nodes, data)
}
type taskFunction func([][]uint16)
type task struct {
run taskFunction
}
func comp(adjacencyMatrix [][]uint16) {
components := findComponents(adjacencyMatrix)
// find largest component
largestSize := -1
largestIndex := -1
for i := 0; i < len(components); i++ {
if len(components[i]) > largestSize {
largestSize = len(components[i])
largestIndex = i
}
}
// create subgraph from largest component
var largestSubGraph [][]uint16
if len(components) > 1 {
largestSubGraph = createGraphFromComponent(components[largestIndex], adjacencyMatrix)
} else {
largestSubGraph = adjacencyMatrix
}
writeAdjacencyList("lc_"+getFileName(), toAdjacencyMatrix(largestSubGraph))
}
func dia(adjacencyMatrix [][]uint16) {
diameter := calculateDiameter(toAdjacencyMatrix(adjacencyMatrix))
fmt.Printf("The diameter %d.\n", diameter)
}
func dist(adjacencyMatrix [][]uint16) {
distanceDistribution := calculateDistDistribution(toAdjacencyMatrix(adjacencyMatrix))
fmt.Println("distance,frequency")
// display freqency distribution
for distance, frequency := range distanceDistribution {
fmt.Printf("%d,%d\n", distance, frequency)
}
saveDistanceDistributionPlot(getFileName(), distanceDistribution)
}
func medDist(adjacencyMatrix [][]uint16) {
distanceMatrix := makeDistMatrix(toAdjacencyMatrix(adjacencyMatrix))
medianDistances := findMedianDistances(distanceMatrix)
readFile := getFileName()
writeMedianDistanceCSV(readFile[:len(readFile)-4]+".csv", medianDistances)
sort.Slice(medianDistances, func(i, j int) bool { return medianDistances[i] < medianDistances[j] })
saveMedianDistancePlot(getFileName(), medianDistances)
}
func getFileName() string {
parts := strings.Split(os.Args[2], "/")
return parts[len(parts)-1]
}
//newTask returns a task and the matrix to run the task on
func newTask() (task, [][]uint16) {
taskName := os.Args[1]
fileName := os.Args[2]
// read graph from HDD
timeGraph := time.Now()
graph := readAdjacencyList(fileName)
fmt.Printf("Read %dx%d adjacency matrix (%v).\n", len(graph), len(graph[0]), time.Now().Sub(timeGraph))
var fn taskFunction
if taskName == "comp" {
fn = comp
} else if taskName == "dia" {
fn = dia
} else if taskName == "diafr" {
allowedNodes := readAllowedNodesFile(os.Args[3])
graph = createGraphFromComponent(allowedNodes, graph)
fn = dia
} else if taskName == "dist" {
fn = dist
} else if taskName == "med" {
fn = medDist
} else {
fn = invalidSelection
}
return task{run: fn}, graph
}
func invalidSelection(_ [][]uint16) {
fmt.Println(modeMessage)
}
func readAllowedNodesFile(fileName string) map[int]void {
file, err := os.Open(fileName)
if err != nil {
panic(err)
}
defer file.Close()
fileReader := bufio.NewReader(file)
scanner := bufio.NewScanner(fileReader)
allowedNodes := make(map[int]void)
for scanner.Scan() {
node, err := strconv.Atoi(scanner.Text())
if err != nil {
panic(err)
}
allowedNodes[node] = void{}
}
return allowedNodes
}