analyze

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 13, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package analyze provides graph analysis functions.

Package analyze provides graph analysis features including hub detection, community detection, and graph comparison.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AuthorityScore

func AuthorityScore(nodeID string, edges []*graph.Edge) int

AuthorityScore calculates a simple authority score based on in-degree. High in-degree nodes are frequently referenced entities.

func CohesionScore

func CohesionScore(members []string, adj map[string]map[string]bool) float64

CohesionScore calculates the ratio of actual intra-community edges to maximum possible.

func EdgesByConfidence

func EdgesByConfidence(edges []*graph.Edge) map[graph.Confidence][]*graph.Edge

EdgesByConfidence groups edges by their confidence level.

func EdgesByType

func EdgesByType(edges []*graph.Edge) map[string][]*graph.Edge

EdgesByType groups edges by their type for analysis.

func HubScore

func HubScore(nodeID string, edges []*graph.Edge) int

HubScore calculates a simple hub score based on out-degree. High out-degree nodes are potential architectural hubs.

func InferredEdges

func InferredEdges(edges []*graph.Edge) []*graph.Edge

InferredEdges returns edges with INFERRED or AMBIGUOUS confidence.

func IsolatedNodes

func IsolatedNodes(nodes []*graph.Node, edges []*graph.Edge, threshold int, excludeTypes []string) []*graph.Node

IsolatedNodes returns nodes with degree <= threshold. Use excludeTypes to filter out structural nodes.

func NodesByType

func NodesByType(nodes []*graph.Node) map[string][]*graph.Node

NodesByType groups nodes by their type for analysis.

Types

type ClusterAlgorithm

type ClusterAlgorithm string

ClusterAlgorithm specifies the community detection algorithm to use.

const (
	// AlgorithmLouvain uses the Louvain modularity optimization algorithm.
	AlgorithmLouvain ClusterAlgorithm = "louvain"

	// AlgorithmConnectedComponents uses connected components.
	AlgorithmConnectedComponents ClusterAlgorithm = "components"
)

type ClusterOptions

type ClusterOptions struct {
	// Algorithm specifies which algorithm to use.
	// Default: AlgorithmLouvain
	Algorithm ClusterAlgorithm

	// Resolution for Louvain algorithm (higher = smaller communities).
	// Default: 1.0
	Resolution float64

	// ExcludeEdgeTypes lists edge types to exclude from community detection.
	ExcludeEdgeTypes []string

	// ExcludeNodeTypes lists node types to exclude from community detection.
	ExcludeNodeTypes []string
}

ClusterOptions configures community detection.

func DefaultClusterOptions

func DefaultClusterOptions() ClusterOptions

DefaultClusterOptions returns sensible defaults.

type ClusterResult

type ClusterResult struct {
	Communities   []Community    `json:"communities"`
	NodeCommunity map[string]int `json:"node_community"`
	Modularity    float64        `json:"modularity,omitempty"`
}

ClusterResult contains the results of community detection.

func DetectCommunities

func DetectCommunities(nodes []*graph.Node, edges []*graph.Edge) *ClusterResult

DetectCommunities performs community detection using the Louvain algorithm by default.

func DetectCommunitiesWithOptions

func DetectCommunitiesWithOptions(nodes []*graph.Node, edges []*graph.Edge, opts ClusterOptions) *ClusterResult

DetectCommunitiesWithOptions performs community detection with configurable algorithm.

func LouvainToClusters

func LouvainToClusters(result *LouvainResult, nodes []*graph.Node, edges []*graph.Edge) *ClusterResult

LouvainToClusters converts LouvainResult to ClusterResult for compatibility.

type Community

type Community struct {
	ID       int      `json:"id"`
	Size     int      `json:"size"`
	Cohesion float64  `json:"cohesion"`
	Members  []string `json:"members"`
	Label    string   `json:"label,omitempty"`
}

Community represents a detected community in the graph.

type EdgeChange

type EdgeChange struct {
	From       string `json:"from"`
	To         string `json:"to"`
	Type       string `json:"type"`
	Confidence string `json:"confidence"`
}

EdgeChange represents an edge that was added or removed.

type GraphDiff

type GraphDiff struct {
	NewNodes     []NodeChange `json:"new_nodes"`
	RemovedNodes []NodeChange `json:"removed_nodes"`
	NewEdges     []EdgeChange `json:"new_edges"`
	RemovedEdges []EdgeChange `json:"removed_edges"`
	Summary      string       `json:"summary"`
}

GraphDiff represents the difference between two graph snapshots.

func DiffGraphs

func DiffGraphs(oldNodes, newNodes []*graph.Node, oldEdges, newEdges []*graph.Edge) *GraphDiff

DiffGraphs compares two graph snapshots and returns what changed.

type HubNode

type HubNode struct {
	ID        string `json:"id"`
	Label     string `json:"label"`
	Type      string `json:"type"`
	InDegree  int    `json:"in_degree"`
	OutDegree int    `json:"out_degree"`
	Total     int    `json:"total"`
}

HubNode represents a highly connected node in the graph.

func FindHubs

func FindHubs(nodes []*graph.Node, edges []*graph.Edge, topN int, excludeTypes []string) []HubNode

FindHubs returns the top N most connected nodes in the graph. Use excludeTypes to filter out structural hub nodes (e.g., packages, files).

type LouvainOptions

type LouvainOptions struct {
	// Resolution controls community granularity. Higher values = smaller communities.
	// Default: 1.0 (standard modularity)
	Resolution float64

	// Seed for random number generator. Use 0 for non-deterministic.
	Seed uint64

	// ExcludeEdgeTypes lists edge types to exclude from community detection.
	// Default: ["contains", "imports"] (structural edges)
	ExcludeEdgeTypes []string

	// ExcludeNodeTypes lists node types to exclude from community detection.
	// Default: ["package", "file"] (hub nodes)
	ExcludeNodeTypes []string
}

LouvainOptions configures the Louvain algorithm.

func DefaultLouvainOptions

func DefaultLouvainOptions() LouvainOptions

DefaultLouvainOptions returns sensible defaults for Louvain.

type LouvainResult

type LouvainResult struct {
	// Communities maps community ID to member node IDs.
	Communities map[int][]string

	// NodeCommunity maps node ID to community ID.
	NodeCommunity map[string]int

	// Modularity is the Q score of the detected communities.
	Modularity float64

	// NumLevels is the number of hierarchical levels in the dendrogram.
	NumLevels int
}

LouvainResult contains the results of Louvain community detection.

func DetectCommunitiesLouvain

func DetectCommunitiesLouvain(nodes []*graph.Node, edges []*graph.Edge, opts LouvainOptions) *LouvainResult

DetectCommunitiesLouvain performs community detection using the Louvain algorithm.

type NodeChange

type NodeChange struct {
	ID    string `json:"id"`
	Label string `json:"label"`
	Type  string `json:"type"`
}

NodeChange represents a node that was added or removed.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL