本文整理汇总了Golang中github.com/gonum/graph/concrete.NewDirectedGraph函数的典型用法代码示例。如果您正苦于以下问题:Golang NewDirectedGraph函数的具体用法?Golang NewDirectedGraph怎么用?Golang NewDirectedGraph使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewDirectedGraph函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestCyclesIn
func TestCyclesIn(t *testing.T) {
for i, test := range cyclesInTests {
g := concrete.NewDirectedGraph(0, math.Inf(1))
g.AddNode(concrete.Node(-10)) // Make sure we test graphs with sparse IDs.
for u, e := range test.g {
// Add nodes that are not defined by an edge.
if !g.Has(concrete.Node(u)) {
g.AddNode(concrete.Node(u))
}
for v := range e {
g.SetEdge(concrete.Edge{F: concrete.Node(u), T: concrete.Node(v)})
}
}
cycles := CyclesIn(g)
var got [][]int
if cycles != nil {
got = make([][]int, len(cycles))
}
// johnson.circuit does range iteration over maps,
// so sort to ensure consistent ordering.
for j, c := range cycles {
ids := make([]int, len(c))
for k, n := range c {
ids[k] = n.ID()
}
got[j] = ids
}
sort.Sort(internal.BySliceValues(got))
if !reflect.DeepEqual(got, test.want) {
t.Errorf("unexpected johnson result for %d:\n\tgot:%#v\n\twant:%#v", i, got, test.want)
}
}
}
示例2: TestSort
func TestSort(t *testing.T) {
for i, test := range tarjanTests {
g := concrete.NewDirectedGraph(0, math.Inf(1))
for u, e := range test.g {
// Add nodes that are not defined by an edge.
if !g.Has(concrete.Node(u)) {
g.AddNode(concrete.Node(u))
}
for v := range e {
g.SetEdge(concrete.Edge{F: concrete.Node(u), T: concrete.Node(v)})
}
}
sorted, err := Sort(g)
var gotSortedLen int
for _, n := range sorted {
if n != nil {
gotSortedLen++
}
}
if gotSortedLen != test.sortedLength {
t.Errorf("unexpected number of sortable nodes for test %d: got:%d want:%d", i, gotSortedLen, test.sortedLength)
}
if err == nil != test.sortable {
t.Errorf("unexpected sortability for test %d: got error: %v want: nil-error=%t", i, err, test.sortable)
}
if err != nil && len(err.(Unorderable)) != test.unorderableLength {
t.Errorf("unexpected number of unorderable nodes for test %d: got:%d want:%d", i, len(err.(Unorderable)), test.unorderableLength)
}
}
}
示例3: TestDepthFirst
func TestDepthFirst(t *testing.T) {
g := concrete.NewDirectedGraph()
a := concrete.Node(g.NewNodeID())
b := concrete.Node(g.NewNodeID())
g.AddNode(a)
g.AddNode(b)
g.SetEdge(concrete.Edge{F: a, T: b}, 1)
g.SetEdge(concrete.Edge{F: b, T: a}, 1)
count := 0
df := &DepthFirst{
EdgeFilter: func(graph.Edge) bool {
return true
},
Visit: func(u, v graph.Node) {
count++
t.Logf("%d -> %d\n", u.ID(), v.ID())
},
}
df.Walk(g, a, func(n graph.Node) bool {
if count > 100 {
t.Fatalf("looped")
return true
}
return false
})
}
示例4: TestHITS
func TestHITS(t *testing.T) {
for i, test := range hitsTests {
g := concrete.NewDirectedGraph(0, math.Inf(1))
for u, e := range test.g {
// Add nodes that are not defined by an edge.
if !g.Has(concrete.Node(u)) {
g.AddNode(concrete.Node(u))
}
for v := range e {
g.SetEdge(concrete.Edge{F: concrete.Node(u), T: concrete.Node(v)})
}
}
got := HITS(g, test.tol)
prec := 1 - int(math.Log10(test.wantTol))
for n := range test.g {
if !floats.EqualWithinAbsOrRel(got[n].Hub, test.want[n].Hub, test.wantTol, test.wantTol) {
t.Errorf("unexpected HITS result for test %d:\ngot: %v\nwant:%v",
i, orderedHubAuth(got, prec), orderedHubAuth(test.want, prec))
break
}
if !floats.EqualWithinAbsOrRel(got[n].Authority, test.want[n].Authority, test.wantTol, test.wantTol) {
t.Errorf("unexpected HITS result for test %d:\ngot: %v\nwant:%v",
i, orderedHubAuth(got, prec), orderedHubAuth(test.want, prec))
break
}
}
}
}
示例5: TestTarjan
func TestTarjan(t *testing.T) {
for i, test := range tarjanTests {
g := concrete.NewDirectedGraph()
for u, e := range test.g {
g.AddNode(concrete.Node(u))
for v := range e {
if !g.NodeExists(concrete.Node(v)) {
g.AddNode(concrete.Node(v))
}
g.AddDirectedEdge(concrete.Edge{H: concrete.Node(u), T: concrete.Node(v)}, 0)
}
}
gotSCCs := search.Tarjan(g)
// tarjan.strongconnect does range iteration over maps,
// so sort SCC members to ensure consistent ordering.
gotIDs := make([][]int, len(gotSCCs))
for i, scc := range gotSCCs {
gotIDs[i] = make([]int, len(scc))
for j, id := range scc {
gotIDs[i][j] = id.ID()
}
sort.Ints(gotIDs[i])
}
for _, iv := range test.ambiguousOrder {
sort.Sort(byComponentLengthOrStart(test.want[iv.start:iv.end]))
sort.Sort(byComponentLengthOrStart(gotIDs[iv.start:iv.end]))
}
if !reflect.DeepEqual(gotIDs, test.want) {
t.Errorf("unexpected Tarjan scc result for %d:\n\tgot:%v\n\twant:%v", i, gotIDs, test.want)
}
}
}
示例6: TestTarjanSCC
func TestTarjanSCC(t *testing.T) {
for i, test := range tarjanTests {
g := concrete.NewDirectedGraph(0, math.Inf(1))
for u, e := range test.g {
// Add nodes that are not defined by an edge.
if !g.Has(concrete.Node(u)) {
g.AddNode(concrete.Node(u))
}
for v := range e {
g.SetEdge(concrete.Edge{F: concrete.Node(u), T: concrete.Node(v)})
}
}
gotSCCs := TarjanSCC(g)
// tarjan.strongconnect does range iteration over maps,
// so sort SCC members to ensure consistent ordering.
gotIDs := make([][]int, len(gotSCCs))
for i, scc := range gotSCCs {
gotIDs[i] = make([]int, len(scc))
for j, id := range scc {
gotIDs[i][j] = id.ID()
}
sort.Ints(gotIDs[i])
}
for _, iv := range test.ambiguousOrder {
sort.Sort(internal.BySliceValues(test.want[iv.start:iv.end]))
sort.Sort(internal.BySliceValues(gotIDs[iv.start:iv.end]))
}
if !reflect.DeepEqual(gotIDs, test.want) {
t.Errorf("unexpected Tarjan scc result for %d:\n\tgot:%v\n\twant:%v", i, gotIDs, test.want)
}
}
}
示例7: directedEdgeAttrGraphFrom
func directedEdgeAttrGraphFrom(g []set, attr map[edge][]Attribute) graph.Directed {
dg := concrete.NewDirectedGraph(0, math.Inf(1))
for u, e := range g {
for v := range e {
dg.SetEdge(attrEdge{from: concrete.Node(u), to: concrete.Node(v), attr: attr[edge{from: u, to: v}]})
}
}
return dg
}
示例8: directedGraphFrom
func directedGraphFrom(g []set) graph.Directed {
dg := concrete.NewDirectedGraph(0, math.Inf(1))
for u, e := range g {
for v := range e {
dg.SetEdge(concrete.Edge{F: concrete.Node(u), T: concrete.Node(v)})
}
}
return dg
}
示例9: New
// New initializes a graph from input to output.
func New() Graph {
g := concrete.NewDirectedGraph()
return Graph{
Directed: g,
GraphDescriber: typedGraph{},
uniqueNamedGraph: newUniqueNamedGraph(g),
internal: g,
}
}
示例10: directedNamedIDGraphFrom
func directedNamedIDGraphFrom(g []set) graph.Directed {
dg := concrete.NewDirectedGraph(0, math.Inf(1))
for u, e := range g {
nu := namedNode{id: u, name: alpha[u : u+1]}
for v := range e {
nv := namedNode{id: v, name: alpha[v : v+1]}
dg.SetEdge(concrete.Edge{F: nu, T: nv})
}
}
return dg
}
示例11: ExampleBreadthFirstSearch
func ExampleBreadthFirstSearch() {
g := concrete.NewDirectedGraph()
var n0, n1, n2, n3 concrete.Node = 0, 1, 2, 3
g.AddDirectedEdge(concrete.Edge{n0, n1}, 1)
g.AddDirectedEdge(concrete.Edge{n0, n2}, 1)
g.AddDirectedEdge(concrete.Edge{n2, n3}, 1)
path, v := search.BreadthFirstSearch(n0, n3, g)
fmt.Println("path:", path)
fmt.Println("nodes visited:", v)
// Output:
// path: [0 2 3]
// nodes visited: 4
}
示例12: generateDummyGraph
func generateDummyGraph() *concrete.DirectedGraph {
nodes := [4]struct{ srcId, targetId int }{
{2, 1},
{1, 0},
{2, 0},
{0, 2},
}
g := concrete.NewDirectedGraph()
for _, n := range nodes {
g.SetEdge(concrete.Edge{concrete.Node(n.srcId), concrete.Node(n.targetId)}, 1)
}
return g
}
示例13: directedNodeAttrGraphFrom
func directedNodeAttrGraphFrom(g []set, attr [][]Attribute) graph.Directed {
dg := concrete.NewDirectedGraph()
for u, e := range g {
var at []Attribute
if u < len(attr) {
at = attr[u]
}
nu := attrNode{id: u, attr: at}
for v := range e {
if v < len(attr) {
at = attr[v]
}
nv := attrNode{id: v, attr: at}
dg.SetEdge(concrete.Edge{F: nu, T: nv}, 0)
}
}
return dg
}
示例14: directedNamedIDNodeAttrGraphFrom
func directedNamedIDNodeAttrGraphFrom(g []set, attr [][]Attribute) graph.Directed {
dg := concrete.NewDirectedGraph(0, math.Inf(1))
for u, e := range g {
var at []Attribute
if u < len(attr) {
at = attr[u]
}
nu := namedAttrNode{id: u, name: alpha[u : u+1], attr: at}
for v := range e {
if v < len(attr) {
at = attr[v]
}
nv := namedAttrNode{id: v, name: alpha[v : v+1], attr: at}
dg.SetEdge(concrete.Edge{F: nu, T: nv})
}
}
return dg
}
示例15: directedPortedAttrGraphFrom
func directedPortedAttrGraphFrom(g []set, attr [][]Attribute, ports map[edge]portedEdge) graph.Directed {
dg := concrete.NewDirectedGraph(0, math.Inf(1))
for u, e := range g {
var at []Attribute
if u < len(attr) {
at = attr[u]
}
nu := attrNode{id: u, attr: at}
for v := range e {
if v < len(attr) {
at = attr[v]
}
pe := ports[edge{from: u, to: v}]
pe.from = nu
pe.to = attrNode{id: v, attr: at}
dg.SetEdge(pe)
}
}
return dg
}