From 43738f81c9075a42f4a4ba0be4f8d58ddf7b685c Mon Sep 17 00:00:00 2001 From: Nicolas Peugnet Date: Thu, 27 Mar 2025 20:10:58 +0100 Subject: [PATCH] graph/network: add diameter example for Eccentricity --- graph/network/distance_example_test.go | 34 ++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 graph/network/distance_example_test.go diff --git a/graph/network/distance_example_test.go b/graph/network/distance_example_test.go new file mode 100644 index 00000000..28485682 --- /dev/null +++ b/graph/network/distance_example_test.go @@ -0,0 +1,34 @@ +// Copyright ©2025 The Gonum Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package network_test + +import ( + "fmt" + + "gonum.org/v1/gonum/graph/network" + "gonum.org/v1/gonum/graph/path" + "gonum.org/v1/gonum/graph/simple" +) + +func ExampleEccentricity_diameter() { + // Build a simple graph: n1--n2--n3. + var n1, n2, n3 simple.Node = 1, 2, 3 + g := simple.NewUndirectedGraph() + g.SetEdge(simple.Edge{F: n1, T: n2}) + g.SetEdge(simple.Edge{F: n2, T: n3}) + + // Find shortest paths and calculate eccentricity values. + paths := path.DijkstraAllPaths(g) + e := network.Eccentricity(g, paths) + + // Compute the graph's diameter from the eccentricities. + var diameter float64 + for _, d := range e { + diameter = max(d, diameter) + } + fmt.Printf("diameter = %v", diameter) + + // Output: diameter = 2 +}