Last active
December 5, 2022 20:33
-
-
Save ynyBonfennil/7c6a6b7cae7727efea274692643281ac to your computer and use it in GitHub Desktop.
Gonum Graph Weighted Undirected Graph : Getting Started
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ( | |
"math" | |
"gonum.org/v1/gonum/graph/simple" | |
) | |
func main() { | |
self := 0 // the cost of self connection | |
absent := math.Inf(1) // the wieght returned for absent edges | |
graph := simple.NewWeightedUndirectedGraph(self, absent) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// create node instance, and add | |
// It panics if the added node ID matches an existing node ID. | |
var id int64 = 0 | |
node := simple.Node(id) | |
graph.AddNode(node) | |
// or you can add node without specifying id | |
node = graph.NewNode() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Get the specific node in the graph | |
// returns nil if it doesn't exist | |
var id int64 = 1 | |
node := graph.Node(id) | |
// Get all the nodes in the graph | |
nodes := graph.Nodes() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from := simple.Node(int64(0)) | |
to := simple.Node(int64(1)) | |
weight := float64(40) | |
// Pattern 1 (directly add edge to graph) | |
_ = graph.NewEdge(from, to) | |
_ = graph.NewWeightedEdge(from, to, weight) | |
// Pattern 2 (create edge instance, then set it to graph) | |
edge := simple.Edge{F: from, T: to} | |
graph.SetEdge(edge) | |
weightedEdge := simple.WeightedEdge{F: from, T: to, W: weight} | |
graph.SetWeightedEdge(weightedEdge) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// get edge | |
edge := graph.Edge(id_from ,id_to) | |
// get edge | |
edge = graph.EdgeBeween(id_from, id_to) | |
// get the specific weighted edge | |
weightedEdge := graph.WeightedEdge(id_from, id_to) | |
// get the specific weighted edge | |
weightedEdge = graph.WeightedEdgeBetween(id_from, id_to) | |
// get all the edges | |
edges := graph.Edges() | |
// get all the edges in the graph | |
weightedEdges := graph.WeightedEdges() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 2 40 | |
1 5 50 | |
2 3 30 | |
2 4 40 | |
3 4 40 | |
3 9 40 | |
4 5 30 | |
4 7 50 | |
4 8 50 | |
4 9 70 | |
5 6 50 | |
5 7 50 | |
6 7 30 | |
7 8 30 | |
7 11 80 | |
7 12 90 | |
8 9 60 | |
8 10 60 | |
8 11 70 | |
8 13 70 | |
9 10 60 | |
10 13 70 | |
10 14 30 | |
11 12 20 | |
11 13 30 | |
12 15 40 | |
12 16 40 | |
13 14 70 | |
13 19 40 | |
14 19 70 | |
14 21 20 | |
15 16 40 | |
16 17 40 | |
17 18 30 | |
17 19 30 | |
18 20 30 | |
19 20 30 | |
20 21 20 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"os" | |
"io" | |
"math" | |
"strconv" | |
"encoding/csv" | |
"gonum.org/v1/gonum/graph/simple" | |
) | |
func main() { | |
// Define Graph | |
var self float64 = 0 | |
var absent float64 = math.Inf(1) | |
graph := simple.NewWeightedUndirectedGraph(self, absent) | |
// Open File | |
fp, err := os.Open("./sample.edgelist") | |
if err != nil { | |
panic(err) | |
} | |
defer fp.Close() | |
// Read rows and add edges | |
reader := csv.NewReader(fp) | |
reader.Comma = '\t' | |
for { | |
record, err := reader.Read() | |
if err == io.EOF { | |
break | |
} else if err != nil { | |
panic(err) | |
} | |
id_from, _ := strconv.ParseInt(record[0], 10, 64) | |
id_to, _ := strconv.ParseInt(record[1], 10, 64) | |
weight, _ := strconv.ParseFloat(record[2], 64) | |
weightedEdge := simple.WeightedEdge{F: simple.Node(id_from), T: simple.Node(id_to), W: weight} | |
graph.SetWeightedEdge(weightedEdge) | |
} | |
// Print all the edges | |
fmt.Print(graph.WeightedEdges()) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"os" | |
"io" | |
"math" | |
"strconv" | |
"encoding/csv" | |
"gonum.org/v1/gonum/graph/simple" | |
"gonum.org/v1/gonum/graph/path" | |
) | |
func main() { | |
// Define Graph | |
var self float64 = 0 | |
var absent float64 = math.Inf(1) | |
graph := simple.NewWeightedUndirectedGraph(self, absent) | |
// Open File | |
fp, err := os.Open("./sample.edgelist") | |
if err != nil { | |
panic(err) | |
} | |
defer fp.Close() | |
// Read rows and add edges | |
reader := csv.NewReader(fp) | |
reader.Comma = '\t' | |
for { | |
record, err := reader.Read() | |
if err == io.EOF { | |
break | |
} else if err != nil { | |
panic(err) | |
} | |
id_from, _ := strconv.ParseInt(record[0], 10, 64) | |
id_to, _ := strconv.ParseInt(record[1], 10, 64) | |
weight, _ := strconv.ParseFloat(record[2], 64) | |
weightedEdge := simple.WeightedEdge{F: simple.Node(id_from), T: simple.Node(id_to), W: weight} | |
graph.SetWeightedEdge(weightedEdge) | |
} | |
// calculate shortest path | |
allShortest := path.DijkstraAllPaths(graph) | |
fmt.Print("from 1 to 18: ") | |
fmt.Print(allShortest.AllBetween(int64(1), int64(18))) | |
fmt.Print("\n") | |
fmt.Print("from 3 to 15: ") | |
fmt.Print(allShortest.AllBetween(int64(3), int64(15))) | |
fmt.Print("\n") | |
fmt.Print("from 6 to 17: ") | |
fmt.Print(allShortest.AllBetween(int64(6), int64(17))) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import matplotlib.pyplot as plt | |
import networkx as nx | |
fig = plt.figure(figsize=(10, 10)) | |
ax = plt.subplot(111) | |
# グラフの定義 | |
G = nx.read_edgelist("./sample.edgelist", data=(("weight", int),)) | |
pos = nx.spring_layout(G) | |
# 描画 | |
nx.draw_networkx_nodes(G, pos, node_color="r", alpha=0.6, node_size=500) | |
nx.draw_networkx_labels(G, pos) | |
nx.draw_networkx_edges(G, pos, alpha=0.4, edge_color="C") | |
edge_labels = nx.get_edge_attributes(G,'weight') | |
nx.draw_networkx_edge_labels(G, pos, edge_labels=edge_labels) | |
plt.axis("off") | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment